UserController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package com.cyksj.web.controller.user;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.cyksj.common.annotation.NoSubmit;
  8. import com.cyksj.common.exception.BusinessRuntimeException;
  9. import com.cyksj.dto.Result;
  10. import com.cyksj.enums.GatewayApiCode;
  11. import com.cyksj.enums.GatewayResponse;
  12. import com.cyksj.mapper.OrderDonMapper;
  13. import com.cyksj.mapper.manage.coupon.CouponUserMapper;
  14. import com.cyksj.model.dto.UserWhoami;
  15. import com.cyksj.model.entity.*;
  16. import com.cyksj.model.manage.views.GroupsRelationView;
  17. import com.cyksj.model.request.*;
  18. import com.cyksj.model.response.UserAuthLoginResp;
  19. import com.cyksj.model.views.*;
  20. import com.cyksj.service.corp.CropChatService;
  21. import com.cyksj.service.exchange.ExchangeCodeService;
  22. import com.cyksj.service.order.UserRealOrderBenefitsService;
  23. import com.cyksj.service.shop.YhShopFrontService;
  24. import com.cyksj.service.user.UserBindRelationService;
  25. import com.cyksj.service.user.UserGalaxycoinRecordService;
  26. import com.cyksj.service.user.UserService;
  27. import com.cyksj.service.user.WorkOrderFrontService;
  28. import com.cyksj.web.util.StpUserUtil;
  29. import com.ejlchina.searcher.BeanSearcher;
  30. import com.ejlchina.searcher.SearchResult;
  31. import com.ejlchina.searcher.param.Operator;
  32. import com.ejlchina.searcher.util.MapBuilder;
  33. import com.ejlchina.searcher.util.MapUtils;
  34. import lombok.RequiredArgsConstructor;
  35. import org.springframework.util.Assert;
  36. import org.springframework.validation.annotation.Validated;
  37. import org.springframework.web.bind.annotation.*;
  38. import javax.servlet.http.HttpServletRequest;
  39. import java.util.ArrayList;
  40. import java.util.List;
  41. /**
  42. * @author chan
  43. * @date 2021/10/11 下午11:05
  44. */
  45. @RequestMapping("/applets/user")
  46. @RestController
  47. @RequiredArgsConstructor
  48. public class UserController {
  49. private final UserService userService;
  50. private final ExchangeCodeService exchangeCodeService;
  51. private final CouponUserMapper couponUserMapper;
  52. private final OrderDonMapper orderDonMapper;
  53. private final UserRealOrderBenefitsService userRealOrderBenefitsService;
  54. private final BeanSearcher beanSearcher;
  55. private final WorkOrderFrontService workOrderFrontService;
  56. private final HttpServletRequest request;
  57. private final CropChatService cropChatService;
  58. private final UserBindRelationService userBindRelationService;
  59. private final YhShopFrontService yhShopFrontService;
  60. private final UserGalaxycoinRecordService userGalaxycoinRecordService;
  61. @GetMapping(value = "/wx/whoami")
  62. public Result<UserWhoami> whoami() {
  63. long userId = StpUserUtil.getLoginIdAsLong();
  64. UserWhoami userWhoami = userService.whoami(userId);
  65. return GatewayResponse.SUCCESS.newBuilder().toResult(userWhoami);
  66. }
  67. /**
  68. * 兑换码兑换车票
  69. */
  70. @PostMapping("/get/ticket")
  71. public Result<String> getTicketByExchangeCode(@RequestBody ExchangeCodeTicket exchangeCodeTicket) {
  72. Long userId = StpUserUtil.getLoginIdAsLong();
  73. return exchangeCodeService.getTicketByExchangeCode(exchangeCodeTicket, userId);
  74. }
  75. /**
  76. * 个人中心详情
  77. */
  78. @GetMapping("/personal")
  79. public Result<UserPersonalView> getPersonalView(String customId) {
  80. long userId = StpUserUtil.getLoginIdAsLong();
  81. List<Long> userIds = userBindRelationService.getRelationUserIdList(userId, null);
  82. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  83. Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  84. Number ticketNum = beanSearcher.searchCount(UserTicketView.class, builder
  85. .field(UserTicketView::getYhsId, yhsId)
  86. .field(UserTicketView::getUserId, userIds).op(Operator.InList)
  87. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  88. .build());
  89. UserPersonalView personalView = new UserPersonalView();
  90. DateTime now = DateTime.now();
  91. //车票
  92. personalView.setNumOfTickets(ticketNum.intValue());
  93. //优惠券
  94. Integer couponSize = couponUserMapper.selectCount(Wrappers.lambdaQuery(CouponUser.class)
  95. .eq(CouponUser::getUserId, userId)
  96. .eq(CouponUser::getStatus, CouponUser.Status.unused)
  97. .le(CouponUser::getValidStartTime, now)
  98. .gt(CouponUser::getValidEndTime, now));
  99. personalView.setNumOfCoupons(couponSize);
  100. //积分
  101. User user = userService.getById(userId);
  102. personalView.setActivePoints(user.getActivePoints());
  103. //推广积分
  104. personalView.setPopularizePoints(user.getPoints());
  105. personalView.setBalance(user.getBalance());
  106. personalView.setGalaxycoin(user.getGalaxycoin());
  107. OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
  108. .eq(OrderDon::getUserId, userId)
  109. .eq(OrderDon::getStatus, OrderDon.Status.noPayment).last("limit 1"));
  110. personalView.setIsNoPayment(orderDon != null ? true : false);
  111. //礼品卡可用数量
  112. Number numOfGiftCard = beanSearcher.searchCount(GiftCardUserRecordView.class, MapUtils.builder()
  113. .put("uid", String.format("((user_id = %s and is_send = 0) or (to_user_id = %s and is_used = 0))", userId, userId))
  114. .field(GiftCardUserRecordView::getIsPay, true)
  115. .build());
  116. personalView.setNumOfGiftCard(numOfGiftCard.intValue());
  117. return GatewayResponse.SUCCESS.newBuilder().toResult(personalView);
  118. }
  119. /**
  120. * 用户已有实物权益 可选车票
  121. */
  122. @GetMapping("/get/available/benefits/tickets")
  123. public Result<List<UserAvailableGoodsTicketView>> getAvailableBenefitsTickets(Long orderId, String customId) {
  124. Long userId = StpUserUtil.getLoginIdAsLong();
  125. Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  126. List<UserAvailableGoodsTicketView> tickets = userRealOrderBenefitsService.getAvailableBenefitsTickets(userId, orderId, yhsId);
  127. return GatewayResponse.SUCCESS.newBuilder().toResult(tickets);
  128. }
  129. /**
  130. * 用户领取实物权益
  131. */
  132. @PostMapping("/receive/benefits")
  133. public Result<List<GroupsRelationView>> receiveRealGoodsBenefits(@RequestBody List<ReceiveBenefitsReq> reqList) {
  134. List<GroupsRelationView> re = new ArrayList<>();
  135. Long userId = StpUserUtil.getLoginIdAsLong();
  136. reqList.forEach(req -> {
  137. Long benefitsId = req.getBenefitsId(), goodsId = req.getGoodsId(), skuId = req.getSkuId(), relationId = req.getRelationId();
  138. Assert.notNull(benefitsId, "可获取的权益不存在");
  139. Assert.notNull(goodsId, "请选择对应平台");
  140. Assert.notNull(skuId, "请选择对应权益规格");
  141. Assert.notNull(relationId, "请选择对应车票");
  142. GroupsRelationView groupsRelationView = userRealOrderBenefitsService.receiveRealGoodsBenefits(userId, benefitsId, goodsId, skuId, relationId);
  143. re.add(groupsRelationView);
  144. });
  145. return GatewayResponse.SUCCESS.newBuilder().toResult(re);
  146. }
  147. /**
  148. * 用户提交工单
  149. */
  150. @PostMapping("/workOrder/submit")
  151. @NoSubmit
  152. public Result<String> submitWorkOrder(@RequestBody @Validated WorkOrderReq workOrderReq) {
  153. long userId = StpUserUtil.getLoginIdAsLong();
  154. workOrderReq.setUserId(userId);
  155. workOrderFrontService.submitWorkOrder(workOrderReq);
  156. return GatewayResponse.SUCCESS.newBuilder().toResult();
  157. }
  158. /**
  159. * 用户工单历史列表
  160. */
  161. @GetMapping("/workOrder/get")
  162. public Result<SearchResult<UserWorkOrderView>> getWorkOrder(String customId) {
  163. Long yhsId = yhShopFrontService.getYhsIdByCustomId(customId);
  164. long userId = StpUserUtil.getLoginIdAsLong();
  165. SearchResult<UserWorkOrderView> search = beanSearcher.search(UserWorkOrderView.class, MapUtils.flatBuilder(request.getParameterMap())
  166. .field(UserWorkOrderView::getUserId, userId)
  167. .field(UserWorkOrderView::getYhsId, yhsId)
  168. .orderBy(UserWorkOrderView::getCreatedTime).desc()
  169. .build());
  170. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  171. }
  172. /**
  173. * 用户工单详情
  174. */
  175. @GetMapping("/workOrder/get/detail/{id}")
  176. public Result<SearchResult<WorkOrderChatHistoryView>> getWorkOrderDetails(@PathVariable Long id) {
  177. long userId = StpUserUtil.getLoginIdAsLong();
  178. SearchResult<WorkOrderChatHistoryView> search = beanSearcher.search(WorkOrderChatHistoryView.class, MapUtils.builder()
  179. .field(WorkOrderChatHistoryView::getUserId, userId)
  180. .field(WorkOrderChatHistoryView::getWorkOrderId, id)
  181. .orderBy(WorkOrderChatHistoryView::getId).desc()
  182. .selectExclude(WorkOrderChatHistoryView::getReplyCustomerServiceId,
  183. WorkOrderChatHistoryView::getCustomer)
  184. .build());
  185. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  186. }
  187. /**
  188. * 工单状态查询
  189. */
  190. @GetMapping("/workOrder/get/status/{id}")
  191. public Result<WorkOrder> getWorkOrderStatus(@PathVariable Long id) {
  192. long userId = StpUserUtil.getLoginIdAsLong();
  193. WorkOrder workOrder = beanSearcher.searchFirst(WorkOrder.class, MapUtils.builder()
  194. .field(WorkOrder::getUserId, userId)
  195. .field(WorkOrder::getId, id)
  196. .onlySelect(WorkOrder::getId, WorkOrder::getUserId, WorkOrder::getStatus)
  197. .build());
  198. if (workOrder == null) {
  199. throw BusinessRuntimeException.getInstance("该工单不存在");
  200. }
  201. return GatewayResponse.SUCCESS.newBuilder().toResult(workOrder);
  202. }
  203. /**
  204. * 用户回复客服
  205. */
  206. @PostMapping("/workOrder/reply")
  207. public Result<String> userReplyWorkOrder(@RequestBody @Validated WorkOrderChatHistory history) {
  208. long userId = StpUserUtil.getLoginIdAsLong();
  209. history.setUserId(userId);
  210. workOrderFrontService.replyCustomer(history);
  211. return GatewayResponse.SUCCESS.newBuilder().toResult();
  212. }
  213. /**
  214. * 修改用户信息
  215. */
  216. @PutMapping("/update/userInfo")
  217. public Result<String> updateHeadImg(@RequestBody UserReq request) {
  218. long userId = StpUserUtil.getLoginIdAsLong();
  219. User user = userService.getById(userId);
  220. if (user == null) throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_TOKEN_VALID);
  221. Boolean isFlag = false;
  222. if (StrUtil.isNotBlank(request.getNickname())) {
  223. if (request.getNickname().length() > 32) {
  224. throw BusinessRuntimeException.getInstance("名称过长");
  225. }
  226. user.setNickname(request.getNickname());
  227. isFlag = true;
  228. }
  229. if (request.getSex() != null) {
  230. user.setSex(request.getSex());
  231. isFlag = true;
  232. }
  233. if (StrUtil.isNotBlank(request.getHeadimgurl())) {
  234. user.setHeadimgurl(request.getHeadimgurl());
  235. isFlag = true;
  236. }
  237. if (isFlag) {
  238. userService.updateById(user);
  239. }
  240. return GatewayResponse.SUCCESS.newBuilder().toResult();
  241. }
  242. /**
  243. * 更新用户最后一次登录时间
  244. */
  245. @PutMapping("/update/lastTime")
  246. public Result<String> updateLastTime() {
  247. long userId = StpUserUtil.getLoginIdAsLong();
  248. User user = userService.getById(userId);
  249. if (user == null) {
  250. return GatewayResponse.SUCCESS.newBuilder().toResult();
  251. }
  252. user.setLastTime(DateTime.now());
  253. userService.updateById(user);
  254. return GatewayResponse.SUCCESS.newBuilder().toResult();
  255. }
  256. /**
  257. * 查看用户所有余额
  258. */
  259. @GetMapping("/get/balance")
  260. public Result<User> getUserBalance() {
  261. long userId = StpUserUtil.getLoginIdAsLong();
  262. User user = userService.getOne(Wrappers.lambdaQuery(User.class)
  263. .eq(User::getId, userId)
  264. .select(User::getNickname, User::getBalance)
  265. .last("limit 1"));
  266. return GatewayResponse.SUCCESS.newBuilder().toResult(user);
  267. }
  268. /**
  269. * 获取群聊二维码
  270. */
  271. @GetMapping("/get/corp/ChatCode/{goodsId}")
  272. public Result<String> getCorpChatCode(@PathVariable Long goodsId) throws Exception {
  273. long userId = StpUserUtil.getLoginIdAsLong();
  274. //获取对应产品的企业微信客户群
  275. String qrCode = cropChatService.getCropWxChatGroupByGoodsId(goodsId);
  276. return GatewayResponse.SUCCESS.newBuilder().toResult(qrCode);
  277. }
  278. /**
  279. * 获取奈飞客服活码弹窗
  280. */
  281. @GetMapping("/get/corp/serviceCode/{goodsId}")
  282. public Result<String> getCorpServiceCode(@PathVariable Long goodsId) {
  283. long userId = StpUserUtil.getLoginIdAsLong();
  284. String qrCode = cropChatService.getCorpServiceCode(goodsId);
  285. return GatewayResponse.SUCCESS.newBuilder().toResult(qrCode);
  286. }
  287. /**
  288. * 用户账号关联列表
  289. */
  290. @GetMapping("/get/userAccounts")
  291. public Result<List<UserBindAccountView>> getUserBindAccountViews() {
  292. long userId = StpUserUtil.getLoginIdAsLong();
  293. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  294. List<UserBindAccountView> accountViews = beanSearcher.searchAll(UserBindAccountView.class, MapUtils.builder().field(UserBindAccountView::getUserId, relationUserIdList)
  295. .op(Operator.InList)
  296. .build());
  297. if (relationUserIdList.size() > 1) {
  298. for (UserBindAccountView accountView : accountViews) {
  299. if (accountView.getUserId().equals(userId)) {
  300. accountViews.remove(accountView);
  301. accountViews.add(0, accountView);
  302. break;
  303. }
  304. }
  305. }
  306. return GatewayResponse.SUCCESS.newBuilder().toResult(accountViews);
  307. }
  308. /**
  309. * 切换账号
  310. */
  311. @PutMapping("/switch/account")
  312. public Result<UserAuthLoginResp> switchOtherAccount(@RequestBody UserBindAccountView request) {
  313. long userId = StpUserUtil.getLoginIdAsLong();
  314. Long switchUserId = request.getUserId();
  315. List<Long> relationUserIdList = userBindRelationService.getRelationUserIdList(userId, null);
  316. if (!relationUserIdList.contains(switchUserId)) {
  317. throw BusinessRuntimeException.getInstance("切换账号异常,请刷新页面重试");
  318. }
  319. User switch_user = userService.getById(switchUserId);
  320. Assert.notNull(switch_user, "切换的账号不存在");
  321. StpUserUtil.login(switch_user.getId());
  322. UserAuthLoginResp loginPhoneRep = new UserAuthLoginResp(StpUserUtil.getTokenValue(), switch_user.getShowId(), switch_user.getNickname(), switch_user.getHeadimgurl());
  323. return GatewayResponse.SUCCESS.newBuilder().toResult(loginPhoneRep);
  324. }
  325. /**
  326. * 个人礼品卡列表
  327. * @return
  328. */
  329. @GetMapping("/get/giftCard")
  330. public Result<SearchResult<GiftCardUserPayFrontView>> getGiftCard() {
  331. long userId = StpUserUtil.getLoginIdAsLong();
  332. String condition = String.format("and (user_id = %s or to_user_id = %s)", userId, userId);
  333. SearchResult<GiftCardUserPayFrontView> view = beanSearcher.search(GiftCardUserPayFrontView.class, MapUtils.builder()
  334. .put("condition", condition)
  335. .put("uid", userId)
  336. .orderBy(GiftCardUserRecordView::getIsUsed).asc()
  337. .orderBy(GiftCardUserPayFrontView::getId).desc().build());
  338. return GatewayResponse.SUCCESS.newBuilder().toResult(view);
  339. }
  340. /**
  341. * 可用现金礼品卡列表
  342. */
  343. @GetMapping("/get/available/giftCard")
  344. public Result<List<GiftCardUserPayFrontView>> getAvailGiftCard() {
  345. long userId = StpUserUtil.getLoginIdAsLong();
  346. String condition = String.format("and to_user_id = %s", userId);
  347. List<GiftCardUserPayFrontView> view = beanSearcher.searchAll(GiftCardUserPayFrontView.class, MapUtils.builder()
  348. .put("condition", condition)
  349. .put("uid", userId)
  350. //现金卡
  351. .field(GiftCardUserPayFrontView::getGcType, 1)
  352. .orderBy(GiftCardUserRecordView::getIsUsed).asc()
  353. .orderBy(GiftCardUserPayFrontView::getId).desc().build());
  354. return GatewayResponse.SUCCESS.newBuilder().toResult(view);
  355. }
  356. /**
  357. * 礼品卡分享详情
  358. */
  359. @GetMapping("/get/giftCard/detail/{code}")
  360. public Result<GiftCardDetailFrontView> getGiftCardDetailByCode(@PathVariable String code) {
  361. GiftCardDetailFrontView detail = beanSearcher.searchFirst(GiftCardDetailFrontView.class, MapUtils.builder().field(GiftCardDetailFrontView::getCode, code).build());
  362. return GatewayResponse.SUCCESS.newBuilder().toResult(detail);
  363. }
  364. /**
  365. * 领取他人礼品卡
  366. */
  367. @PostMapping("/receive/giftCard")
  368. public Result<String> receiveGiftCardByRc(@RequestBody GiftCardReceiveReq req) {
  369. long userId = StpUserUtil.getLoginIdAsLong();
  370. Long relationId = req.getRelationId();
  371. userService.receiveGiftCardByRc(userId, req.getCode(), relationId);
  372. return GatewayResponse.SUCCESS.newBuilder().toResult();
  373. }
  374. /**
  375. * 查询用户银河币明细
  376. */
  377. @GetMapping("/get/galaxycoinRecord")
  378. public Result<IPage<UserGalaxycoinRecord>> getGalaxycoinRecord(@RequestParam(defaultValue = "1") Long start, @RequestParam(defaultValue = "5") Long limit){
  379. long userId = StpUserUtil.getLoginIdAsLong();
  380. Page<UserGalaxycoinRecord> page = userGalaxycoinRecordService.page(new Page<>(start, limit), Wrappers.lambdaQuery(UserGalaxycoinRecord.class).eq(UserGalaxycoinRecord::getUserId, userId));
  381. return GatewayResponse.SUCCESS.newBuilder().toResult(page);
  382. }
  383. }