| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- package com.cyksj.web.controller.manage;
- import cn.dev33.satoken.annotation.SaCheckPermission;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.toolkit.Assert;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.cyksj.common.constant.Constant;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.dto.Result;
- import com.cyksj.enums.GatewayResponse;
- import com.cyksj.mapper.GroupsRelationMapper;
- import com.cyksj.mapper.OrderDonMapper;
- import com.cyksj.mapper.UserFuncPropertyMapper;
- import com.cyksj.mapper.UserMapper;
- import com.cyksj.mapper.corp.CorpUserFollowRelationMapper;
- import com.cyksj.model.entity.*;
- import com.cyksj.model.manage.views.UserView;
- import com.cyksj.model.request.HomeManagementClickRecordReq;
- import com.cyksj.model.request.UserFuncPropertyReq;
- import com.cyksj.model.views.*;
- import com.cyksj.service.home.HomeManagementFrontService;
- import com.cyksj.service.relation.GroupRelationClearService;
- import com.cyksj.service.user.UserBindRelationService;
- import com.cyksj.web.util.StpUserUtil;
- import com.ejlchina.searcher.BeanSearcher;
- import com.ejlchina.searcher.SearchResult;
- import com.ejlchina.searcher.param.Operator;
- import com.ejlchina.searcher.util.MapBuilder;
- import com.ejlchina.searcher.util.MapUtils;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.dao.DuplicateKeyException;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletRequest;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.List;
- import java.util.Optional;
- /**
- * @author chan
- * @date 2022/9/27 4:42 PM
- */
- @Slf4j
- @RequiredArgsConstructor
- @RestController
- @RequestMapping("/manage/user")
- public class CmsUserController{
- private final HttpServletRequest request;
- private final BeanSearcher beanSearcher;
- private final UserFuncPropertyMapper userFuncPropertyMapper;
- private final UserMapper userMapper;
- private final GroupRelationClearService groupRelationClearService;
- private final GroupsRelationMapper groupsRelationMapper;
- private final UserBindRelationService userBindRelationService;
- private final CorpUserFollowRelationMapper cmsUserFollowRelationMapper;
- private final OrderDonMapper orderDonMapper;
- @GetMapping("/get")
- @SaCheckPermission("user:view")
- public Result<SearchResult<UserView>> get(String otherPhone, Long sharedId, String userid, Long businessId, String sort, String order) {
- MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
- String otherSql = StrUtil.EMPTY;
- if (StrUtil.isNotBlank(otherPhone)) {
- 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);
- }
- if (businessId != null) {
- if (StrUtil.isNotBlank(otherSql)) {
- otherSql += " and ";
- }
- otherSql += String.format("us.shared_id in (select user_id from user_distribute where business_id = %s)", businessId);
- }
- if (StrUtil.isNotBlank(otherSql)) {
- builder.put("otherConditionSql", otherSql);
- }
- SearchResult<UserView> search = beanSearcher.search(UserView.class, builder.build());
- search.getDataList().forEach(data->{
- Long userId = data.getId();
- UserBindDetail userBindDetail = beanSearcher.searchFirst(UserBindDetail.class, MapUtils.builder().field(UserBindDetail::getUserId, userId).onlySelect(UserBindDetail::getPhone, UserBindDetail::getEmail).build());
- Optional.ofNullable(userBindDetail).ifPresent(bind->{
- data.setBindEmail(bind.getEmail());
- data.setBindPhone(bind.getPhone());
- });
- if (data.getSharedId() != null) {
- UserDistributeBusinessInfoView userDistributeBusinessInfoView = beanSearcher.searchFirst(UserDistributeBusinessInfoView.class, MapUtils.builder().field(UserDistributeBusinessInfoView::getUserId, data.getSharedId()).build());
- Optional.ofNullable(userDistributeBusinessInfoView).ifPresent(info->{
- data.setDistributeName(info.getDistributeName());
- data.setBusinessName(info.getBusinessName());
- });
- }
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 修改用户的拉黑状态
- */
- @PutMapping("/put/black/status/{id}")
- @SaCheckPermission("user:black")
- public Result<String> putBlackStatus(@PathVariable Long id) {
- User user = userMapper.selectById(id);
- Optional.ofNullable(user).ifPresent(u -> {
- u.setIsBlack(!u.getIsBlack());
- if (u.getIsBlack()) {
- List<Long> userIds = userBindRelationService.getRelationUserIdList(id, user);
- List<RenewalView> groupsRelations = beanSearcher.searchAll(RenewalView.class, MapUtils.builder()
- .field(RenewalView::getUserId, 0).op(Operator.GreaterThan)
- .field(RenewalView::getUserId, userIds).op(Operator.InList)
- .field(RenewalView::getStatus, List.of("validity", "overdue", "outside")).op(Operator.InList)
- .orderBy(RenewalView::getExpiryTime).asc()
- .build());
- groupsRelations.forEach(ticket -> {
- GroupsRelation relation = groupsRelationMapper.selectById(ticket.getRelationId());
- groupRelationClearService.clearTicket(relation, UserTicketClearedRecord.Source.black);
- });
- }
- userMapper.updateById(u);
- });
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- /**
- * 修改用户查看验证码次数
- */
- @PutMapping("/put/verifyCheck/num")
- public Result<String> updateCheckCodeNum(@RequestBody UserFuncPropertyReq req) {
- if (req.getUserId() == null) throw BusinessRuntimeException.getInstance("用户id为空");
- if (req.getVerifyCheckNum() == null || req.getVerifyCheckNum() < 0) {
- throw BusinessRuntimeException.getInstance("请输入正确的查看验证码次数");
- }
- UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class)
- .eq(UserFuncProperty::getUserId, req.getUserId()).last("limit 1"));
- if (userFuncProperty == null) {
- try {
- userFuncProperty = new UserFuncProperty();
- userFuncProperty.setUserId(req.getUserId());
- userFuncProperty.setVerifyCheckNum(req.getVerifyCheckNum());
- userFuncPropertyMapper.insert(userFuncProperty);
- } catch (DuplicateKeyException e) {
- }
- }
- userFuncProperty.setVerifyCheckNum(req.getVerifyCheckNum());
- userFuncPropertyMapper.updateById(userFuncProperty);
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- /**
- * 查看用户验证码次数
- */
- @GetMapping("/get/verifyCheck/num")
- public Result<Integer> getVerifyCheckNum(Long userId) {
- if (userId==null) throw BusinessRuntimeException.getInstance("用户id为空");
- UserFuncProperty userFuncProperty = userFuncPropertyMapper.selectOne(Wrappers.lambdaQuery(UserFuncProperty.class)
- .eq(UserFuncProperty::getUserId, userId).last("limit 1"));
- //默认三次
- Integer verifyCheckNum = 3;
- if (userFuncProperty != null) verifyCheckNum = userFuncProperty.getVerifyCheckNum();
- return GatewayResponse.SUCCESS.newBuilder().toResult(verifyCheckNum);
- }
- /**
- * 用户加入企业微信客服详情
- */
- @GetMapping("/get/corpFollow/detail/{userId}")
- public Result<String> getCorpFollowDetailById(@PathVariable Long userId) {
- User user = userMapper.selectById(userId);
- if (user == null || StrUtil.isEmpty(user.getUnionid())) {
- return GatewayResponse.SUCCESS.newBuilder().toResult();
- }
- Long wxCorpId = 2l;
- String corpCustomers = cmsUserFollowRelationMapper.selectCorpCustomers(user.getUnionid(), wxCorpId);
- return GatewayResponse.SUCCESS.newBuilder().toResult(corpCustomers);
- }
- /**
- * 企业微信客服列表
- */
- @GetMapping("/get/corpFollow")
- public Result<List<CorpFollow>> getCorpFollow() {
- List<CorpFollow> corpFollows = beanSearcher.searchAll(CorpFollow.class, MapUtils.flatBuilder(request.getParameterMap())
- .field(CorpFollow::getWxCorpId, 2)
- .build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(corpFollows);
- }
- /**
- * 用户订单详情title
- */
- @GetMapping("/get/order/title")
- public Result<UserView> getUserOrderDetailTitle(Long userId) {
- UserView userView = new UserView();
- List<OrderDon> orderDons = orderDonMapper.selectList(Wrappers.lambdaQuery(OrderDon.class)
- .eq(OrderDon::getUserId, userId)
- .notIn(OrderDon::getStatus, Constant.noOrderStatus)
- .orderByAsc(OrderDon::getId));
- //首单产品
- Optional<OrderDon> firstOrder = orderDons.stream().filter(don -> don.getStatus() != OrderDon.Status.refund).findFirst();
- if (firstOrder.isPresent()) {
- OrderDon orderDon = firstOrder.get();
- BigDecimal money = orderDon.getMoney().divide(BigDecimal.valueOf(100));
- Optional.ofNullable(beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getSkuId, orderDon.getSkuId()).onlySelect(GoodsDonSkuView::getGoodsTitle, GoodsDonSkuView::getSpecVal).build()))
- .ifPresent(goodsDonSkuView -> {
- String msg = String.format("%s%s %s元", goodsDonSkuView.getGoodsTitle(), goodsDonSkuView.getSpecVal(), money);
- if (orderDon.getCouponUserId() != 0) {
- BigDecimal subMoney = orderDon.getCouponMoney().divide(BigDecimal.valueOf(100));
- msg = String.format("%s(减免%s元)", msg, subMoney);
- }
- userView.setFirstOrderDetail(msg);
- });
- }
- //有无路由器订单
- Optional<OrderDon> routeOrder = orderDons.stream().filter(don -> don.getGoodsId() == 15).findFirst();
- userView.setIsHadRouteOrder(routeOrder.isPresent() ? true : false);
- Integer noRefundOrders = 0;
- Integer refundOrders = 0;
- //除去退款 下单金额
- BigDecimal orderMoney = BigDecimal.ZERO;
- BigDecimal refundMoney = BigDecimal.ZERO;
- for (OrderDon orderDon : orderDons) {
- if (orderDon.getStatus() == OrderDon.Status.refund) {
- refundOrders++;
- if (orderDon.getRefundMoney() != null) {
- refundMoney = refundMoney.add(orderDon.getRefundMoney());
- }
- }else {
- noRefundOrders++;
- orderMoney = orderMoney.add(orderDon.getMoney());
- }
- }
- BigDecimal refundRate = BigDecimal.ZERO;
- if (!orderDons.isEmpty()) {
- //退款率
- refundRate = BigDecimal.valueOf(refundOrders).divide(BigDecimal.valueOf(orderDons.size()), 2, RoundingMode.HALF_UP);
- }
- userView.setOrderMoney(orderMoney);
- userView.setNumOfOrder(noRefundOrders);
- userView.setRefundMoney(refundMoney);
- userView.setRefundRate(refundRate);
- return GatewayResponse.SUCCESS.newBuilder().toResult(userView);
- }
- /**
- * 用户下单详情
- */
- @GetMapping("/get/order/list")
- public Result<SearchResult<UserOrderDonDetail>> getUserOrderDetails() {
- SearchResult<UserOrderDonDetail> search = beanSearcher.search(UserOrderDonDetail.class, MapUtils.flatBuilder(request.getParameterMap())
- .field(UserOrderDonDetail::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn)
- .build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(search);
- }
- /**
- * 问卷回答列表
- */
- @GetMapping("/get/question/response/list")
- public Result<List<QuestionResponseRecordView>> getQuestionResponseList(String first, String end) {
- String timeSql = StrUtil.EMPTY;
- if (StrUtil.isNotBlank(first)) {
- timeSql += String.format(" and created_time >= '%s'", first);
- }
- if (StrUtil.isNotBlank(end)) {
- timeSql += String.format(" and created_time < '%s'", end);
- }
- MapBuilder builder = MapUtils.builder();
- if (StrUtil.isNotBlank(timeSql)) {
- builder.put("time", timeSql);
- }
- List<QuestionResponseRecordView> views = beanSearcher.searchAll(QuestionResponseRecordView.class, builder.build());
- return GatewayResponse.SUCCESS.newBuilder().toResult(views);
- }
- }
|