UserController.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. package com.cyksj.web.controller.user;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.extra.servlet.ServletUtil;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.cyksj.common.annotation.NoSubmit;
  8. import com.cyksj.common.exception.BusinessRuntimeException;
  9. import com.cyksj.common.util.Jsons;
  10. import com.cyksj.dto.Result;
  11. import com.cyksj.enums.GatewayApiCode;
  12. import com.cyksj.enums.GatewayResponse;
  13. import com.cyksj.mapper.*;
  14. import com.cyksj.mapper.manage.coupon.CouponUserMapper;
  15. import com.cyksj.mapper.manage.exchange.ExchangeCodeMapper;
  16. import com.cyksj.mapper.manage.distribute.*;
  17. import com.cyksj.mapper.manage.exchange.ExchangeCodeMapper;
  18. import com.cyksj.mapper.mirrorshop.UserMirrorShopMapper;
  19. import com.cyksj.model.dto.UserWhoami;
  20. import com.cyksj.model.entity.*;
  21. import com.cyksj.model.manage.views.GroupsRelationView;
  22. import com.cyksj.model.request.*;
  23. import com.cyksj.model.response.UserAuthLoginResp;
  24. import com.cyksj.model.views.*;
  25. import com.cyksj.service.corp.CorpInteractiveReaderFrontService;
  26. import com.cyksj.service.corp.CropChatService;
  27. import com.cyksj.service.exchange.ExchangeCodeService;
  28. import com.cyksj.service.order.UserRealOrderBenefitsService;
  29. import com.cyksj.service.shop.YhShopFrontService;
  30. import com.cyksj.service.user.UserBindRelationService;
  31. import com.cyksj.service.user.UserService;
  32. import com.cyksj.service.user.WorkOrderFrontService;
  33. import com.cyksj.web.util.StpUserUtil;
  34. import com.ejlchina.searcher.BeanSearcher;
  35. import com.ejlchina.searcher.SearchResult;
  36. import com.ejlchina.searcher.param.Operator;
  37. import com.ejlchina.searcher.util.MapBuilder;
  38. import com.ejlchina.searcher.util.MapUtils;
  39. import lombok.RequiredArgsConstructor;
  40. import org.springframework.dao.DuplicateKeyException;
  41. import org.springframework.util.Assert;
  42. import org.springframework.validation.annotation.Validated;
  43. import org.springframework.web.bind.annotation.*;
  44. import javax.servlet.http.HttpServletRequest;
  45. import java.math.BigDecimal;
  46. import java.math.RoundingMode;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import java.util.stream.Collectors;
  50. /**
  51. * @author chan
  52. * @date 2021/10/11 下午11:05
  53. */
  54. @RequestMapping("/applets/user")
  55. @RestController
  56. @RequiredArgsConstructor
  57. public class UserController {
  58. private final UserService userService;
  59. private final ExchangeCodeService exchangeCodeService;
  60. private final ExchangeCodeMapper exchangeCodeMapper;
  61. private final CouponUserMapper couponUserMapper;
  62. private final OrderDonMapper orderDonMapper;
  63. private final UserRealOrderBenefitsService userRealOrderBenefitsService;
  64. private final BeanSearcher beanSearcher;
  65. private final WorkOrderFrontService workOrderFrontService;
  66. private final HttpServletRequest request;
  67. private final CropChatService cropChatService;
  68. private final UserBindRelationService userBindRelationService;
  69. private final YhShopFrontService yhShopFrontService;
  70. private final CorpInteractiveReaderFrontService corpInteractiveReaderFrontService;
  71. private final UserPlatSkuDistributeRateMapper userPlatSkuDistributeRateMapper;
  72. private final UserPlatDistributeRateMapper userPlatDistributeRateMapper;
  73. private final DistributeGeneralSkuRateMapper distributeGeneralSkuRateMapper;
  74. private final GoodsDonSkuMapper skuMapper;
  75. private final UserDistributeMapper userDistributeMapper;
  76. private final UserDistributePosterRecordMapper userDistributePosterRecordMapper;
  77. private final DistributePosterGoodsMapper distributePosterGoodsMapper;
  78. private final UserIpRecordMapper userIpRecordMapper;
  79. private final BusinessDistributeDefaultRateConfigMapper businessDistributeDefaultRateConfigMapper;
  80. private final BusinessDistributeDefaultSkuRateConfigMapper businessDistributeDefaultSkuRateConfigMapper;
  81. private final UserMirrorShopMapper userMirrorShopMapper;
  82. private final UserGalaxyCoinRecordMapper userGalaxyCoinRecordMapper;
  83. @GetMapping(value = "/wx/whoami")
  84. public Result<UserWhoami> whoami(Long popularizeGoodsId) {
  85. long userId = StpUserUtil.getLoginIdAsLong();
  86. UserWhoami userWhoami = userService.whoami(userId, popularizeGoodsId);
  87. return GatewayResponse.SUCCESS.newBuilder().toResult(userWhoami);
  88. }
  89. /**
  90. * 兑换码兑换车票
  91. */
  92. @PostMapping("/get/ticket")
  93. public Result<String> getTicketByExchangeCode(@RequestBody ExchangeCodeTicket exchangeCodeTicket) throws Exception {
  94. Long userId = StpUserUtil.getLoginIdAsLong();
  95. return exchangeCodeService.getTicketByExchangeCode(exchangeCodeTicket, userId);
  96. }
  97. /**
  98. * 车票兑换码详情
  99. */
  100. @GetMapping("/get/ticket/code/detail")
  101. public Result<ExchangeCodeDetailView> getTicketCodeDetail(
  102. @RequestParam(value = "code", required = false) String code) throws Exception {
  103. StpUserUtil.getLoginIdAsLong();
  104. // Spring already decodes query parameters. Never decode a second time.
  105. // Accept legacy underscore/hyphen codes, but reject whitespace and encoded payloads.
  106. if (code == null || !code.matches("[A-Za-z0-9_-]{1,256}")) {
  107. throw BusinessRuntimeException.getInstance("错误兑换码");
  108. }
  109. // Do not pass request parameters to BeanSearcher: all filters are server-owned.
  110. ExchangeCode record = exchangeCodeMapper.selectOne(Wrappers.lambdaQuery(ExchangeCode.class)
  111. .eq(ExchangeCode::getCode, code).last("LIMIT 1"));
  112. // Also reject case-insensitive matches from a *_ci database collation.
  113. if (record == null || !code.equals(record.getCode())) {
  114. throw BusinessRuntimeException.getInstance("错误兑换码");
  115. }
  116. ExchangeCodeDetailView detail = new ExchangeCodeDetailView();
  117. detail.setSkuIds(record.getSkuIds());
  118. detail.setUseStatus(record.getUseStatus());
  119. detail.setUseTime(record.getUseTime());
  120. detail.setCodeType(record.getCodeType());
  121. List<Long> skuIds = Jsons.parseList(record.getSkuIds(), Long.class);
  122. detail.setSkuDetails(skuMapper.selectExchangeTitleSkus(skuIds));
  123. return GatewayResponse.SUCCESS.newBuilder().toResult(detail);
  124. }
  125. /**
  126. * 个人中心详情
  127. */
  128. @GetMapping("/personal")
  129. public Result<UserPersonalView> getPersonalView(String customId, Integer cmt) {
  130. long userId = StpUserUtil.getLoginIdAsLong();
  131. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  132. String userIds = userIdList.toString().replace("[", "(").replace("]", ")");
  133. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  134. if (cmt != null && cmt == 2 && StrUtil.isNotBlank(customId)) {
  135. UserMirrorShop userMirrorShop = userMirrorShopMapper.selectOne(Wrappers.lambdaQuery(UserMirrorShop.class)
  136. .eq(UserMirrorShop::getCustomId, customId)
  137. .last("limit 1"));
  138. if (userMirrorShop != null) {
  139. builder.field(RenewalView::getCmt, cmt).field(RenewalView::getYhsId, userMirrorShop.getId());
  140. }
  141. }
  142. if (cmt == null || cmt != 2) {
  143. // SQL NULL does not satisfy `<> 2`; keep legacy platform tickets visible
  144. // while still excluding mirror-shop tickets (cmt = 2).
  145. builder.field(RenewalView::getCmt).sql("$1 is null or $1 <> ?", 2);
  146. }
  147. List<RenewalView> renewalViews = beanSearcher.searchAll(RenewalView.class, builder
  148. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  149. .field(RenewalView::getUserId, userIdList).op(Operator.InList)
  150. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  151. .field(RenewalView::getIsVip, false)
  152. .orderBy(RenewalView::getExpiryTime).asc()
  153. .onlySelect(RenewalView::getExpiryTime)
  154. .build());
  155. DateTime now = DateTime.now();
  156. UserPersonalView personalView = new UserPersonalView();
  157. personalView.setTotalOfTickets(renewalViews.size());
  158. renewalViews = renewalViews.stream().filter(e -> {
  159. if (e.getExpiryTime() != null && e.getExpiryTime().before(now)) {
  160. return false;
  161. }
  162. return true;
  163. }).collect(Collectors.toList());
  164. //车票
  165. personalView.setNumOfTickets(renewalViews.size());
  166. //优惠券 需要排除已删除/后台失效的优惠券
  167. Integer couponSize = couponUserMapper.selectAvailableCoupons(userId, CouponUser.Status.unused, now);
  168. personalView.setNumOfCoupons(couponSize);
  169. //积分
  170. User user = userService.getById(userId);
  171. personalView.setGalaxyCoin(user.getGalaxyCoin());
  172. //30天内即将过期银河币数量
  173. BigDecimal expiredGalaxyCoinAfterDays = userGalaxyCoinRecordMapper.selectExpiredGalaxyCoinAfterDays(userId, DateUtil.offsetDay(now, -30));
  174. personalView.setExpiredGalaxyCoin(expiredGalaxyCoinAfterDays);
  175. //推广积分
  176. personalView.setPopularizePoints(user.getPoints());
  177. personalView.setBalance(user.getBalance());
  178. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  179. .eq(OrderDon::getUserId, userId)
  180. .eq(OrderDon::getStatus, OrderDon.Status.noPayment).last("limit 1"));
  181. personalView.setIsNoPayment(orderDon != null ? true : false);
  182. //礼品卡可用数量
  183. Number numOfGiftCard = beanSearcher.searchCount(GiftCardUserRecordView.class, MapUtils.builder()
  184. .put("uid", String.format("((user_id in %s and is_send = 0) or (to_user_id in %s and is_used = 0))", userIds, userIds))
  185. .field(GiftCardUserRecordView::getIsPay, true)
  186. .build());
  187. personalView.setNumOfGiftCard(numOfGiftCard.intValue());
  188. return GatewayResponse.SUCCESS.newBuilder().toResult(personalView);
  189. }
  190. /**
  191. * 用户已有实物权益 可选车票
  192. */
  193. @GetMapping("/get/available/benefits/tickets")
  194. public Result<List<UserAvailableGoodsTicketView>> getAvailableBenefitsTickets(Long orderId, String customId, Integer cmt) {
  195. Long userId = StpUserUtil.getLoginIdAsLong();
  196. List<UserAvailableGoodsTicketView> tickets = userRealOrderBenefitsService.getAvailableBenefitsTickets(userId, orderId, customId, cmt);
  197. return GatewayResponse.SUCCESS.newBuilder().toResult(tickets);
  198. }
  199. /**
  200. * 用户领取实物权益
  201. */
  202. @PostMapping("/receive/benefits")
  203. public Result<List<GroupsRelationView>> receiveRealGoodsBenefits(@RequestBody List<ReceiveBenefitsReq> reqList) {
  204. List<GroupsRelationView> re = new ArrayList<>();
  205. Long userId = StpUserUtil.getLoginIdAsLong();
  206. reqList.forEach(req -> {
  207. Long benefitsId = req.getBenefitsId(), goodsId = req.getGoodsId(), skuId = req.getSkuId(), relationId = req.getRelationId();
  208. Assert.notNull(benefitsId, "可获取的权益不存在");
  209. Assert.notNull(goodsId, "请选择对应平台");
  210. Assert.notNull(skuId, "请选择对应权益规格");
  211. Assert.notNull(relationId, "请选择对应车票");
  212. GroupsRelationView groupsRelationView = userRealOrderBenefitsService.receiveRealGoodsBenefits(userId, benefitsId, goodsId, skuId, relationId);
  213. re.add(groupsRelationView);
  214. });
  215. return GatewayResponse.SUCCESS.newBuilder().toResult(re);
  216. }
  217. /**
  218. * 用户提交工单
  219. */
  220. @PostMapping("/workOrder/submit")
  221. @NoSubmit
  222. public Result<String> submitWorkOrder(@RequestBody @Validated WorkOrderReq workOrderReq) {
  223. long userId = StpUserUtil.getLoginIdAsLong();
  224. workOrderReq.setUserId(userId);
  225. workOrderFrontService.submitWorkOrder(workOrderReq);
  226. return GatewayResponse.SUCCESS.newBuilder().toResult();
  227. }
  228. /**
  229. * 用户工单历史列表
  230. */
  231. @GetMapping("/workOrder/get")
  232. public Result<SearchResult<UserWorkOrderView>> getWorkOrder(String customId) {
  233. Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  234. long userId = StpUserUtil.getLoginIdAsLong();
  235. SearchResult<UserWorkOrderView> search = beanSearcher.search(UserWorkOrderView.class, MapUtils.flatBuilder(request.getParameterMap())
  236. .field(UserWorkOrderView::getUserId, userId)
  237. .field(UserWorkOrderView::getYhsId, yhsId)
  238. .orderBy(UserWorkOrderView::getCreatedTime).desc()
  239. .build());
  240. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  241. }
  242. /**
  243. * 用户工单详情
  244. */
  245. @GetMapping("/workOrder/get/detail/{id}")
  246. public Result<SearchResult<WorkOrderChatHistoryView>> getWorkOrderDetails(@PathVariable Long id) {
  247. long userId = StpUserUtil.getLoginIdAsLong();
  248. SearchResult<WorkOrderChatHistoryView> search = beanSearcher.search(WorkOrderChatHistoryView.class, MapUtils.builder()
  249. .field(WorkOrderChatHistoryView::getUserId, userId)
  250. .field(WorkOrderChatHistoryView::getWorkOrderId, id)
  251. .orderBy(WorkOrderChatHistoryView::getId).desc()
  252. .selectExclude(WorkOrderChatHistoryView::getReplyCustomerServiceId,
  253. WorkOrderChatHistoryView::getCustomer)
  254. .build());
  255. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  256. }
  257. /**
  258. * 工单状态查询
  259. */
  260. @GetMapping("/workOrder/get/status/{id}")
  261. public Result<WorkOrder> getWorkOrderStatus(@PathVariable Long id) {
  262. long userId = StpUserUtil.getLoginIdAsLong();
  263. WorkOrder workOrder = beanSearcher.searchFirst(WorkOrder.class, MapUtils.builder()
  264. .field(WorkOrder::getUserId, userId)
  265. .field(WorkOrder::getId, id)
  266. .onlySelect(WorkOrder::getId, WorkOrder::getUserId, WorkOrder::getStatus)
  267. .build());
  268. if (workOrder == null) {
  269. throw BusinessRuntimeException.getInstance("该工单不存在");
  270. }
  271. return GatewayResponse.SUCCESS.newBuilder().toResult(workOrder);
  272. }
  273. /**
  274. * 用户回复客服
  275. */
  276. @PostMapping("/workOrder/reply")
  277. public Result<String> userReplyWorkOrder(@RequestBody @Validated WorkOrderChatHistory history) {
  278. long userId = StpUserUtil.getLoginIdAsLong();
  279. history.setUserId(userId);
  280. workOrderFrontService.replyCustomer(history);
  281. return GatewayResponse.SUCCESS.newBuilder().toResult();
  282. }
  283. /**
  284. * 修改用户信息
  285. */
  286. @PutMapping("/update/userInfo")
  287. public Result<String> updateHeadImg(@RequestBody UserReq request) {
  288. long userId = StpUserUtil.getLoginIdAsLong();
  289. User user = userService.getById(userId);
  290. if (user == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  291. Boolean isFlag = false;
  292. if (StrUtil.isNotBlank(request.getNickname())) {
  293. if (request.getNickname().length() > 32) {
  294. throw BusinessRuntimeException.getInstance("名称过长");
  295. }
  296. user.setNickname(request.getNickname());
  297. isFlag = true;
  298. }
  299. if (request.getSex() != null) {
  300. user.setSex(request.getSex());
  301. isFlag = true;
  302. }
  303. if (StrUtil.isNotBlank(request.getHeadimgurl())) {
  304. user.setHeadimgurl(request.getHeadimgurl());
  305. isFlag = true;
  306. }
  307. if (isFlag) {
  308. userService.updateById(user);
  309. }
  310. return GatewayResponse.SUCCESS.newBuilder().toResult();
  311. }
  312. /**
  313. * 更新用户最后一次登录时间
  314. */
  315. @PutMapping("/update/lastTime")
  316. public Result<String> updateLastTime() {
  317. long userId = StpUserUtil.getLoginIdAsLong();
  318. User user = userService.getById(userId);
  319. if (user == null) {
  320. return GatewayResponse.SUCCESS.newBuilder().toResult();
  321. }
  322. user.setLastTime(DateTime.now());
  323. userService.updateById(user);
  324. return GatewayResponse.SUCCESS.newBuilder().toResult();
  325. }
  326. /**
  327. * 查看用户所有余额
  328. */
  329. @GetMapping("/get/balance")
  330. public Result<User> getUserBalance() {
  331. long userId = StpUserUtil.getLoginIdAsLong();
  332. User user = userService.getOne(Wrappers.lambdaQuery(User.class)
  333. .eq(User::getId, userId)
  334. .select(User::getNickname, User::getBalance, User::getGalaxyCoin)
  335. .last("limit 1"));
  336. if (user != null && user.getBalance() != null) {
  337. // 余额单位为分,仅返回可抵扣的整分金额,避免展示四舍五入后超额扣款;不回写数据库。
  338. user.setBalance(user.getBalance().setScale(0, RoundingMode.FLOOR));
  339. }
  340. return GatewayResponse.SUCCESS.newBuilder().toResult(user);
  341. }
  342. /**
  343. * 获取群聊二维码
  344. */
  345. @GetMapping("/get/corp/ChatCode/{goodsId}")
  346. public Result<String> getCorpChatCode(@PathVariable Long goodsId) throws Exception {
  347. long userId = StpUserUtil.getLoginIdAsLong();
  348. //获取对应产品的企业微信客户群
  349. String qrCode = cropChatService.getCropWxChatGroupByGoodsId(goodsId);
  350. return GatewayResponse.SUCCESS.newBuilder().toResult(qrCode);
  351. }
  352. /**
  353. * 获取奈飞客服活码弹窗
  354. */
  355. @GetMapping("/get/corp/serviceCode/{goodsId}")
  356. public Result<String> getCorpServiceCode(@PathVariable Long goodsId) {
  357. long userId = StpUserUtil.getLoginIdAsLong();
  358. String qrCode = cropChatService.getCorpServiceCode(goodsId);
  359. return GatewayResponse.SUCCESS.newBuilder().toResult(qrCode);
  360. }
  361. /**
  362. * 用户账号关联列表
  363. */
  364. @GetMapping("/get/userAccounts")
  365. public Result<List<UserBindAccountView>> getUserBindAccountViews() {
  366. long userId = StpUserUtil.getLoginIdAsLong();
  367. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  368. List<UserBindAccountView> accountViews = beanSearcher.searchAll(UserBindAccountView.class, MapUtils.builder().field(UserBindAccountView::getUserId, relationUserIdList)
  369. .op(Operator.InList)
  370. .build());
  371. if (relationUserIdList.size() > 1) {
  372. for (UserBindAccountView accountView : accountViews) {
  373. if (accountView.getUserId().equals(userId)) {
  374. accountViews.remove(accountView);
  375. accountViews.add(0, accountView);
  376. break;
  377. }
  378. }
  379. }
  380. return GatewayResponse.SUCCESS.newBuilder().toResult(accountViews);
  381. }
  382. /**
  383. * 切换账号
  384. */
  385. @PutMapping("/switch/account")
  386. public Result<UserAuthLoginResp> switchOtherAccount(@RequestBody UserBindAccountView request) {
  387. long userId = StpUserUtil.getLoginIdAsLong();
  388. Long switchUserId = request.getUserId();
  389. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  390. if (!relationUserIdList.contains(switchUserId)) {
  391. throw BusinessRuntimeException.getInstance("切换账号异常,请刷新页面重试");
  392. }
  393. User switch_user = userService.getById(switchUserId);
  394. Assert.notNull(switch_user, "切换的账号不存在");
  395. StpUserUtil.login(switch_user.getId());
  396. UserAuthLoginResp loginPhoneRep = new UserAuthLoginResp(StpUserUtil.getTokenValue(), switch_user.getShowId(), switch_user.getNickname(), switch_user.getHeadimgurl());
  397. return GatewayResponse.SUCCESS.newBuilder().toResult(loginPhoneRep);
  398. }
  399. /**
  400. * 个人礼品卡列表
  401. * @return
  402. */
  403. @GetMapping("/get/giftCard")
  404. public Result<SearchResult<GiftCardUserPayFrontView>> getGiftCard() {
  405. long userId = StpUserUtil.getLoginIdAsLong();
  406. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  407. String userIds = userIdList.toString().replace("[", "(").replace("]", ")");
  408. String condition = String.format("and (user_id in %s or to_user_id in %s)", userIds, userIds);
  409. SearchResult<GiftCardUserPayFrontView> view = beanSearcher.search(GiftCardUserPayFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  410. .put("condition", condition)
  411. .put("uid", userIds)
  412. .orderBy(GiftCardUserRecordView::getIsUsed).asc()
  413. .orderBy(GiftCardUserPayFrontView::getId).desc().build());
  414. return GatewayResponse.SUCCESS.newBuilder().toResult(view);
  415. }
  416. /**
  417. * 可用现金礼品卡列表
  418. */
  419. @GetMapping("/get/available/giftCard")
  420. public Result<List<GiftCardUserPayFrontView>> getAvailGiftCard() {
  421. long userId = StpUserUtil.getLoginIdAsLong();
  422. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  423. String userIds = userIdList.toString().replace("[", "(").replace("]", ")");
  424. String condition = String.format("and to_user_id in %s", userIds);
  425. List<GiftCardUserPayFrontView> view = beanSearcher.searchAll(GiftCardUserPayFrontView.class, MapUtils.builder()
  426. .put("condition", condition)
  427. .put("uid", userIds)
  428. //现金卡
  429. .field(GiftCardUserPayFrontView::getGcType, 1)
  430. .orderBy(GiftCardUserRecordView::getIsUsed).asc()
  431. .orderBy(GiftCardUserPayFrontView::getId).desc().build());
  432. return GatewayResponse.SUCCESS.newBuilder().toResult(view);
  433. }
  434. /**
  435. * 礼品卡分享详情
  436. */
  437. @GetMapping("/get/giftCard/detail/{code}")
  438. public Result<GiftCardDetailFrontView> getGiftCardDetailByCode(@PathVariable String code) {
  439. GiftCardDetailFrontView detail = beanSearcher.searchFirst(GiftCardDetailFrontView.class, MapUtils.builder().field(GiftCardDetailFrontView::getCode, code).build());
  440. return GatewayResponse.SUCCESS.newBuilder().toResult(detail);
  441. }
  442. /**
  443. * 领取他人礼品卡
  444. */
  445. @PostMapping("/receive/giftCard")
  446. public Result<String> receiveGiftCardByRc(@RequestBody GiftCardReceiveReq req) {
  447. long userId = StpUserUtil.getLoginIdAsLong();
  448. Long relationId = req.getRelationId();
  449. userService.receiveGiftCardByRc(userId, req.getCode(), relationId);
  450. return GatewayResponse.SUCCESS.newBuilder().toResult();
  451. }
  452. /**
  453. * 点击雷达链接
  454. */
  455. @PostMapping("/click/reader/{radarId}")
  456. public Result<String> clickInteractiveReader(@PathVariable Long radarId) throws Exception {
  457. long userId = StpUserUtil.getLoginIdAsLong();
  458. corpInteractiveReaderFrontService.clickReader(userId, radarId);
  459. return GatewayResponse.SUCCESS.newBuilder().toResult();
  460. }
  461. /**
  462. * 推广平台海报列表
  463. */
  464. @GetMapping("/get/ds/poster/goods")
  465. public Result<List<DistributePosterGoodsFrontView>> getPosterGoods() {
  466. long userId = StpUserUtil.getLoginIdAsLong();
  467. UserDistribute userDistribute = userDistributeMapper.selectOne(Wrappers.lambdaQuery(UserDistribute.class)
  468. .eq(UserDistribute::getUserId, userId)
  469. .eq(UserDistribute::getDeleted, 1)
  470. .last("limit 1"));
  471. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  472. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  473. List<DistributePosterGoodsFrontView> searchAll = beanSearcher.searchAll(DistributePosterGoodsFrontView.class, builder.build());
  474. searchAll.forEach(e -> {
  475. if (userDistribute != null) {
  476. UserPlatDistributeRate userPlatDistributeRate = userPlatDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatDistributeRate.class)
  477. .eq(UserPlatDistributeRate::getDistributeId, userDistribute.getId())
  478. .eq(UserPlatDistributeRate::getGoodsId, e.getGoodsId())
  479. .last("limit 1"));
  480. if (userPlatDistributeRate != null) {
  481. e.setRate(userPlatDistributeRate.getRate());
  482. } else {
  483. BusinessDistributeDefaultRateConfig businessDistributeDefaultRateConfig = businessDistributeDefaultRateConfigMapper.selectOne(Wrappers.lambdaQuery(BusinessDistributeDefaultRateConfig.class)
  484. .eq(BusinessDistributeDefaultRateConfig::getGoodsId, e.getGoodsId())
  485. .last("limit 1"));
  486. if (businessDistributeDefaultRateConfig != null) {
  487. e.setRate(businessDistributeDefaultRateConfig.getRate());
  488. }
  489. }
  490. }
  491. if (e.getIsSkuRate()) {
  492. GoodsDonSku sku = skuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  493. .eq(GoodsDonSku::getGoodsId, e.getGoodsId())
  494. .eq(GoodsDonSku::getStatus, 1)
  495. .orderByAsc(GoodsDonSku::getPrice)
  496. .last("limit 1"));
  497. //先获取价格最低的规格对应的 比例
  498. //渠道
  499. if (userDistribute != null) {
  500. UserPlatSkuDistributeRate minPriceSkuRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  501. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
  502. .eq(UserPlatSkuDistributeRate::getGoodsId, e.getGoodsId())
  503. .eq(UserPlatSkuDistributeRate::getSkuId, sku.getId())
  504. .last("limit 1"));
  505. if (minPriceSkuRate != null && minPriceSkuRate.getRate() != 0) {
  506. e.setRate(minPriceSkuRate.getRate());
  507. } else {
  508. UserPlatSkuDistributeRate userPlatSkuDistributeRate = userPlatSkuDistributeRateMapper.selectOne(Wrappers.lambdaQuery(UserPlatSkuDistributeRate.class)
  509. .eq(UserPlatSkuDistributeRate::getDistributeId, userDistribute.getId())
  510. .eq(UserPlatSkuDistributeRate::getGoodsId, e.getGoodsId())
  511. .ne(UserPlatSkuDistributeRate::getRate, 0)
  512. //上架
  513. .apply("exists (select 1 from goods_don_sku sku where sku.id = sku_id and sku.status is true limit 1)")
  514. .orderByAsc(UserPlatSkuDistributeRate::getRate)
  515. .last("limit 1"));
  516. if (userPlatSkuDistributeRate != null) {
  517. e.setRate(userPlatSkuDistributeRate.getRate());
  518. sku = skuMapper.selectById(userPlatSkuDistributeRate.getSkuId());
  519. e.setPrice(sku.getPrice());
  520. } else {
  521. BusinessDistributeDefaultSkuRateConfig businessDistributeDefaultSkuRateConfig = businessDistributeDefaultSkuRateConfigMapper.selectOne(Wrappers.lambdaQuery(BusinessDistributeDefaultSkuRateConfig.class)
  522. .eq(BusinessDistributeDefaultSkuRateConfig::getGoodsId, e.getGoodsId())
  523. .gt(BusinessDistributeDefaultSkuRateConfig::getRate, 0)
  524. .apply("exists (select 1 from goods_don_sku sku where sku.id = sku_id and sku.status is true limit 1)")
  525. .orderByAsc(BusinessDistributeDefaultSkuRateConfig::getRate)
  526. .last("limit 1"));
  527. if (businessDistributeDefaultSkuRateConfig != null) {
  528. e.setRate(businessDistributeDefaultSkuRateConfig.getRate());
  529. }
  530. }
  531. }
  532. } else {
  533. //普通
  534. DistributeGeneralSkuRate minPriceRate = distributeGeneralSkuRateMapper.selectOne(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  535. .eq(DistributeGeneralSkuRate::getGoodsId, e.getGoodsId())
  536. .eq(DistributeGeneralSkuRate::getSkuId, sku.getId())
  537. .last("limit 1"));
  538. if (minPriceRate != null && minPriceRate.getRate() > 0) {
  539. e.setRate(minPriceRate.getRate());
  540. } else {
  541. DistributeGeneralSkuRate distributeGeneralSkuRate = distributeGeneralSkuRateMapper.selectOne(Wrappers.lambdaQuery(DistributeGeneralSkuRate.class)
  542. .eq(DistributeGeneralSkuRate::getGoodsId, e.getGoodsId())
  543. .apply("exists (select 1 from goods_don_sku sku where sku.id = sku_id and sku.status is true limit 1)")
  544. .ne(DistributeGeneralSkuRate::getRate, 0)
  545. .orderByAsc(DistributeGeneralSkuRate::getRate)
  546. .last("limit 1"));
  547. if (distributeGeneralSkuRate != null) {
  548. e.setRate(distributeGeneralSkuRate.getRate());
  549. sku = skuMapper.selectById(distributeGeneralSkuRate.getSkuId());
  550. e.setPrice(sku.getPrice());
  551. }
  552. }
  553. }
  554. }
  555. UserDistributePosterRecord distributePosterRecord = userDistributePosterRecordMapper.selectOne(Wrappers.lambdaQuery(UserDistributePosterRecord.class)
  556. .eq(UserDistributePosterRecord::getPosterId, e.getId())
  557. .in(UserDistributePosterRecord::getUserId, userIdList)
  558. .last("limit 1"));
  559. if (distributePosterRecord != null) {
  560. e.setRid(distributePosterRecord.getId());
  561. } });
  562. return GatewayResponse.SUCCESS.newBuilder().toResult(searchAll);
  563. }
  564. /**
  565. * 海报详情
  566. */
  567. @GetMapping("/get/poster/{goodsId}")
  568. public Result<DistributePosterGoodsFrontView> getPosterDetail(@PathVariable Long goodsId) {
  569. DistributePosterGoodsFrontView distributePosterGoodsView = beanSearcher.searchFirst(DistributePosterGoodsFrontView.class, MapUtils.flatBuilder(request.getParameterMap())
  570. .field(DistributePosterGoodsFrontView::getGoodsId, goodsId)
  571. .build());
  572. return GatewayResponse.SUCCESS.newBuilder().toResult(distributePosterGoodsView);
  573. }
  574. /**
  575. * 用户下载海报记录
  576. */
  577. @PostMapping("/poster/record/{posterId}")
  578. public Result<Long> posterRecord(@PathVariable Long posterId) {
  579. long userId = StpUserUtil.getLoginIdAsLong();
  580. DistributePosterGoods distributePosterGoods = distributePosterGoodsMapper.selectById(posterId);
  581. if (distributePosterGoods == null) {
  582. return GatewayResponse.SUCCESS.newBuilder().toResult();
  583. }
  584. UserDistributePosterRecord distributePosterRecord = new UserDistributePosterRecord();
  585. distributePosterRecord.setPosterId(posterId);
  586. distributePosterRecord.setUserId(userId);
  587. try {
  588. userDistributePosterRecordMapper.insert(distributePosterRecord);
  589. } catch (DuplicateKeyException e) {
  590. distributePosterRecord = userDistributePosterRecordMapper.selectOne(Wrappers.lambdaQuery(UserDistributePosterRecord.class)
  591. .eq(UserDistributePosterRecord::getUserId, userId)
  592. .eq(UserDistributePosterRecord::getPosterId, posterId)
  593. .last("limit 1"));
  594. }
  595. return GatewayResponse.SUCCESS.newBuilder().toResult(distributePosterRecord.getId());
  596. }
  597. /**
  598. * 记录用户ip
  599. */
  600. @PostMapping("/user/ip/record")
  601. public Result<String> recordUserIpAddress() {
  602. long userId = StpUserUtil.getLoginIdAsLong();
  603. String ip = ServletUtil.getClientIP(request);
  604. Integer selectCount = userIpRecordMapper.selectCount(Wrappers.lambdaQuery(UserIpRecord.class)
  605. .eq(UserIpRecord::getUserId, userId)
  606. .eq(UserIpRecord::getIp, ip));
  607. if (selectCount == 0) {
  608. try {
  609. UserIpRecord userIpRecord = new UserIpRecord();
  610. userIpRecord.setUserId(userId);
  611. userIpRecord.setIp(ip);
  612. userIpRecordMapper.insert(userIpRecord);
  613. } catch (DuplicateKeyException e) {
  614. }
  615. }
  616. return GatewayResponse.SUCCESS.newBuilder().toResult();
  617. }
  618. /**
  619. * 用户认证信息总览
  620. */
  621. @GetMapping("/get/cert/overview")
  622. public Result<UserCertInfoView> getCertOverView() {
  623. long userId = StpUserUtil.getLoginIdAsLong();
  624. UserCertInfoView userCertInfoView = userService.getCertOverView(userId);
  625. return GatewayResponse.SUCCESS.newBuilder().toResult(userCertInfoView);
  626. }
  627. /**
  628. * 用户同步功能信息
  629. */
  630. @GetMapping("/get/user/syncInfo")
  631. public Result<UserSyncInfoView> getUserSyncInfo() {
  632. long userId = StpUserUtil.getLoginIdAsLong();
  633. UserSyncInfoView syncInfoView = userService.getUserSyncInfo(userId);
  634. return GatewayResponse.SUCCESS.newBuilder().toResult(syncInfoView);
  635. }
  636. /**
  637. * 注销账号
  638. */
  639. @PutMapping("/revoked/account")
  640. public Result<String> revokedAccount() {
  641. long userId = StpUserUtil.getLoginIdAsLong();
  642. userService.revokedAccount(userId);
  643. return GatewayResponse.SUCCESS.newBuilder().toResult();
  644. }
  645. }