UserController.java 18 KB

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