UserController.java 33 KB

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