CmsUserController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.cyksj.web.controller.manage;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.toolkit.Assert;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.cyksj.common.constant.Constant;
  7. import com.cyksj.common.exception.BusinessRuntimeException;
  8. import com.cyksj.dto.Result;
  9. import com.cyksj.enums.GatewayResponse;
  10. import com.cyksj.mapper.GroupsRelationMapper;
  11. import com.cyksj.mapper.OrderDonMapper;
  12. import com.cyksj.mapper.UserFuncPropertyMapper;
  13. import com.cyksj.mapper.UserMapper;
  14. import com.cyksj.mapper.corp.CorpUserFollowRelationMapper;
  15. import com.cyksj.model.entity.*;
  16. import com.cyksj.model.manage.views.UserView;
  17. import com.cyksj.model.request.HomeManagementClickRecordReq;
  18. import com.cyksj.model.request.UserFuncPropertyReq;
  19. import com.cyksj.model.views.*;
  20. import com.cyksj.service.home.HomeManagementFrontService;
  21. import com.cyksj.service.relation.GroupRelationClearService;
  22. import com.cyksj.service.user.UserBindRelationService;
  23. import com.cyksj.web.util.StpUserUtil;
  24. import com.ejlchina.searcher.BeanSearcher;
  25. import com.ejlchina.searcher.SearchResult;
  26. import com.ejlchina.searcher.param.Operator;
  27. import com.ejlchina.searcher.util.MapBuilder;
  28. import com.ejlchina.searcher.util.MapUtils;
  29. import lombok.RequiredArgsConstructor;
  30. import lombok.extern.slf4j.Slf4j;
  31. import org.springframework.dao.DuplicateKeyException;
  32. import org.springframework.web.bind.annotation.*;
  33. import javax.servlet.http.HttpServletRequest;
  34. import java.math.BigDecimal;
  35. import java.math.RoundingMode;
  36. import java.util.List;
  37. import java.util.Optional;
  38. /**
  39. * @author chan
  40. * @date 2022/9/27 4:42 PM
  41. */
  42. @Slf4j
  43. @RequiredArgsConstructor
  44. @RestController
  45. @RequestMapping("/manage/user")
  46. public class CmsUserController{
  47. private final HttpServletRequest request;
  48. private final BeanSearcher beanSearcher;
  49. private final UserFuncPropertyMapper userFuncPropertyMapper;
  50. private final UserMapper userMapper;
  51. private final GroupRelationClearService groupRelationClearService;
  52. private final GroupsRelationMapper groupsRelationMapper;
  53. private final UserBindRelationService userBindRelationService;
  54. private final CorpUserFollowRelationMapper cmsUserFollowRelationMapper;
  55. private final OrderDonMapper orderDonMapper;
  56. @GetMapping("/get")
  57. @SaCheckPermission("user:view")
  58. public Result<SearchResult<UserView>> get(String otherPhone, Long sharedId, String userid, Long businessId, String sort, String order) {
  59. MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
  60. String otherSql = StrUtil.EMPTY;
  61. if (StrUtil.isNotBlank(otherPhone)) {
  62. otherSql += String.format("(u.id in (select user_id from user_bind_detail where phone like '%s%%') or u.login_phone like '%s%%')", otherPhone, otherPhone);
  63. }
  64. if (businessId != null) {
  65. if (StrUtil.isNotBlank(otherSql)) {
  66. otherSql += " and ";
  67. }
  68. otherSql += String.format("us.shared_id in (select user_id from user_distribute where business_id = %s)", businessId);
  69. }
  70. if (StrUtil.isNotBlank(otherSql)) {
  71. builder.put("otherConditionSql", otherSql);
  72. }
  73. SearchResult<UserView> search = beanSearcher.search(UserView.class, builder.build());
  74. search.getDataList().forEach(data->{
  75. Long userId = data.getId();
  76. UserBindDetail userBindDetail = beanSearcher.searchFirst(UserBindDetail.class, MapUtils.builder().field(UserBindDetail::getUserId, userId).onlySelect(UserBindDetail::getPhone, UserBindDetail::getEmail).build());
  77. Optional.ofNullable(userBindDetail).ifPresent(bind->{
  78. data.setBindEmail(bind.getEmail());
  79. data.setBindPhone(bind.getPhone());
  80. });
  81. if (data.getSharedId() != null) {
  82. UserDistributeBusinessInfoView userDistributeBusinessInfoView = beanSearcher.searchFirst(UserDistributeBusinessInfoView.class, MapUtils.builder().field(UserDistributeBusinessInfoView::getUserId, data.getSharedId()).build());
  83. Optional.ofNullable(userDistributeBusinessInfoView).ifPresent(info->{
  84. data.setDistributeName(info.getDistributeName());
  85. data.setBusinessName(info.getBusinessName());
  86. });
  87. }
  88. });
  89. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  90. }
  91. /**
  92. * 修改用户的拉黑状态
  93. */
  94. @PutMapping("/put/black/status/{id}")
  95. @SaCheckPermission("user:black")
  96. public Result<String> putBlackStatus(@PathVariable Long id) {
  97. User user = userMapper.selectById(id);
  98. Optional.ofNullable(user).ifPresent(u -> {
  99. u.setIsBlack(!u.getIsBlack());
  100. if (u.getIsBlack()) {
  101. List<Long> userIds = userBindRelationService.getRelationUserIdList(id, user);
  102. List<RenewalView> groupsRelations = beanSearcher.searchAll(RenewalView.class, MapUtils.builder()
  103. .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
  104. .field(RenewalView::getUserId, userIds).op(Operator.InList)
  105. .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
  106. .orderBy(RenewalView::getExpiryTime).asc()
  107. .build());
  108. groupsRelations.forEach(ticket -> {
  109. GroupsRelation relation = groupsRelationMapper.selectById(ticket.getRelationId());
  110. groupRelationClearService.clearTicket(relation, UserTicketClearedRecord.Source.black);
  111. });
  112. }
  113. userMapper.updateById(u);
  114. });
  115. return GatewayResponse.SUCCESS.newBuilder().toResult();
  116. }
  117. /**
  118. * 修改用户查看验证码次数
  119. */
  120. @PutMapping("/put/verifyCheck/num")
  121. public Result<String> updateCheckCodeNum(@RequestBody UserFuncPropertyReq req) {
  122. if (req.getUserId() == null) throw BusinessRuntimeException.getInstance("用户id为空");
  123. if (req.getVerifyCheckNum() == null || req.getVerifyCheckNum() < 0) {
  124. throw BusinessRuntimeException.getInstance("请输入正确的查看验证码次数");
  125. }
  126. UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class)
  127. .eq(UserFuncProperty::getUserId, req.getUserId()).last("limit 1"));
  128. if (userFuncProperty == null) {
  129. try {
  130. userFuncProperty = new UserFuncProperty();
  131. userFuncProperty.setUserId(req.getUserId());
  132. userFuncProperty.setVerifyCheckNum(req.getVerifyCheckNum());
  133. userFuncPropertyMapper.insert(userFuncProperty);
  134. } catch (DuplicateKeyException e) {
  135. }
  136. }
  137. userFuncProperty.setVerifyCheckNum(req.getVerifyCheckNum());
  138. userFuncPropertyMapper.updateById(userFuncProperty);
  139. return GatewayResponse.SUCCESS.newBuilder().toResult();
  140. }
  141. /**
  142. * 查看用户验证码次数
  143. */
  144. @GetMapping("/get/verifyCheck/num")
  145. public Result<Integer> getVerifyCheckNum(Long userId) {
  146. if (userId==null) throw BusinessRuntimeException.getInstance("用户id为空");
  147. UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class)
  148. .eq(UserFuncProperty::getUserId, userId).last("limit 1"));
  149. //默认三次
  150. Integer verifyCheckNum = 3;
  151. if (userFuncProperty != null) verifyCheckNum = userFuncProperty.getVerifyCheckNum();
  152. return GatewayResponse.SUCCESS.newBuilder().toResult(verifyCheckNum);
  153. }
  154. /**
  155. * 用户加入企业微信客服详情
  156. */
  157. @GetMapping("/get/corpFollow/detail/{userId}")
  158. public Result<String> getCorpFollowDetailById(@PathVariable Long userId) {
  159. User user = userMapper.selectById(userId);
  160. if (user == null || StrUtil.isEmpty(user.getUnionid())) {
  161. return GatewayResponse.SUCCESS.newBuilder().toResult();
  162. }
  163. Long wxCorpId = 2l;
  164. String corpCustomers = cmsUserFollowRelationMapper.selectCorpCustomers(user.getUnionid(), wxCorpId);
  165. return GatewayResponse.SUCCESS.newBuilder().toResult(corpCustomers);
  166. }
  167. /**
  168. * 企业微信客服列表
  169. */
  170. @GetMapping("/get/corpFollow")
  171. public Result<List<CorpFollow>> getCorpFollow() {
  172. List<CorpFollow> corpFollows = beanSearcher.searchAll(CorpFollow.class, MapUtils.flatBuilder(request.getParameterMap())
  173. .field(CorpFollow::getWxCorpId, 2)
  174. .build());
  175. return GatewayResponse.SUCCESS.newBuilder().toResult(corpFollows);
  176. }
  177. /**
  178. * 用户订单详情title
  179. */
  180. @GetMapping("/get/order/title")
  181. public Result<UserView> getUserOrderDetailTitle(Long userId) {
  182. UserView userView = new UserView();
  183. List<OrderDon> orderDons = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
  184. .eq(OrderDon::getUserId, userId)
  185. .notIn(OrderDon::getStatus, Constant.noOrderStatus)
  186. .orderByAsc(OrderDon::getId));
  187. //首单产品
  188. Optional<OrderDon> firstOrder = orderDons.stream().filter(don -> don.getStatus() != OrderDon.Status.refund).findFirst();
  189. if (firstOrder.isPresent()) {
  190. OrderDon orderDon = firstOrder.get();
  191. BigDecimal money = orderDon.getMoney().divide(BigDecimal.valueOf(100));
  192. Optional.ofNullable(beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, orderDon.getSkuId()).onlySelect(GoodsDonSkuView::getGoodsTitle, GoodsDonSkuView::getSpecVal).build()))
  193. .ifPresent(goodsDonSkuView -> {
  194. String msg = String.format("%s%s %s元", goodsDonSkuView.getGoodsTitle(), goodsDonSkuView.getSpecVal(), money);
  195. if (orderDon.getCouponUserId() != 0) {
  196. BigDecimal subMoney = orderDon.getCouponMoney().divide(BigDecimal.valueOf(100));
  197. msg = String.format("%s(减免%s元)", msg, subMoney);
  198. }
  199. userView.setFirstOrderDetail(msg);
  200. });
  201. }
  202. //有无路由器订单
  203. Optional<OrderDon> routeOrder = orderDons.stream().filter(don -> don.getGoodsId() == 15).findFirst();
  204. userView.setIsHadRouteOrder(routeOrder.isPresent() ? true : false);
  205. Integer noRefundOrders = 0;
  206. Integer refundOrders = 0;
  207. //除去退款 下单金额
  208. BigDecimal orderMoney = BigDecimal.ZERO;
  209. BigDecimal refundMoney = BigDecimal.ZERO;
  210. for (OrderDon orderDon : orderDons) {
  211. if (orderDon.getStatus() == OrderDon.Status.refund) {
  212. refundOrders++;
  213. if (orderDon.getRefundMoney() != null) {
  214. refundMoney = refundMoney.add(orderDon.getRefundMoney());
  215. }
  216. }else {
  217. noRefundOrders++;
  218. orderMoney = orderMoney.add(orderDon.getMoney());
  219. }
  220. }
  221. BigDecimal refundRate = BigDecimal.ZERO;
  222. if (!orderDons.isEmpty()) {
  223. //退款率
  224. refundRate = BigDecimal.valueOf(refundOrders).divide(BigDecimal.valueOf(orderDons.size()), 2, RoundingMode.HALF_UP);
  225. }
  226. userView.setOrderMoney(orderMoney);
  227. userView.setNumOfOrder(noRefundOrders);
  228. userView.setRefundMoney(refundMoney);
  229. userView.setRefundRate(refundRate);
  230. return GatewayResponse.SUCCESS.newBuilder().toResult(userView);
  231. }
  232. /**
  233. * 用户下单详情
  234. */
  235. @GetMapping("/get/order/list")
  236. public Result<SearchResult<UserOrderDonDetail>> getUserOrderDetails() {
  237. SearchResult<UserOrderDonDetail> search = beanSearcher.search(UserOrderDonDetail.class, MapUtils.flatBuilder(request.getParameterMap())
  238. .field(UserOrderDonDetail::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn)
  239. .build());
  240. return GatewayResponse.SUCCESS.newBuilder().toResult(search);
  241. }
  242. /**
  243. * 问卷回答列表
  244. */
  245. @GetMapping("/get/question/response/list")
  246. public Result<List<QuestionResponseRecordView>> getQuestionResponseList(String first, String end) {
  247. String timeSql = StrUtil.EMPTY;
  248. if (StrUtil.isNotBlank(first)) {
  249. timeSql += String.format(" and created_time >= '%s'", first);
  250. }
  251. if (StrUtil.isNotBlank(end)) {
  252. timeSql += String.format(" and created_time < '%s'", end);
  253. }
  254. MapBuilder builder = MapUtils.builder();
  255. if (StrUtil.isNotBlank(timeSql)) {
  256. builder.put("time", timeSql);
  257. }
  258. List<QuestionResponseRecordView> views = beanSearcher.searchAll(QuestionResponseRecordView.class, builder.build());
  259. return GatewayResponse.SUCCESS.newBuilder().toResult(views);
  260. }
  261. }