| package com.xzx.gc.order.service;  | 
|   | 
| import cn.hutool.core.collection.CollUtil;  | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.date.DateUnit;  | 
| import cn.hutool.core.date.DateUtil;  | 
| import cn.hutool.core.util.NumberUtil;  | 
| import cn.hutool.core.util.StrUtil;  | 
| import cn.hutool.json.JSONUtil;  | 
| import com.xzx.gc.common.constant.CommonEnum;  | 
| import com.xzx.gc.common.constant.OrderEnum;  | 
| import com.xzx.gc.common.constant.PayEnum;  | 
| import com.xzx.gc.common.constant.SysEnum;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.common.utils.IdUtils;  | 
| import com.xzx.gc.common.utils.SpringUtil;  | 
| import com.xzx.gc.entity.*;  | 
| import com.xzx.gc.model.comon.account.AllAcountParamDto;  | 
| import com.xzx.gc.model.user.AccountVo;  | 
| import com.xzx.gc.order.mapper.*;  | 
| import com.xzx.gc.service.BaseAccountService;  | 
| import lombok.extern.slf4j.Slf4j;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.stereotype.Service;  | 
| import org.springframework.transaction.annotation.Transactional;  | 
|   | 
| import java.math.BigDecimal;  | 
| import java.util.*;  | 
| import java.util.stream.Collectors;  | 
|   | 
| @Service  | 
| @Transactional  | 
| @Slf4j  | 
| public class PromotionRebateService extends BaseAccountService {  | 
|   | 
|     @Autowired  | 
|     private UserService userService;  | 
|     @Autowired  | 
|     private OrderMapper orderMapper;  | 
|     @Autowired  | 
|     private AccountService accountService;  | 
|     @Autowired  | 
|     private BusinessUtil businessUtil;  | 
|   | 
|     @Autowired  | 
|     private PayInfoMapper payInfoMapper;  | 
|   | 
|     @Autowired  | 
|     private PartnerAccountLogService partnerAccountLogService;  | 
|   | 
|     @Autowired  | 
|     private PartnerAccountService partnerAccountService;  | 
|   | 
|     @Autowired  | 
|     private SysMessageMapper sysMessageMapper;  | 
|   | 
|   | 
|     @Autowired  | 
|     private IdUtils idUtils;  | 
|   | 
|     @Autowired  | 
|     private AccountLogService accountLogService;  | 
|   | 
|     @Autowired  | 
|     private ConfigService configService;  | 
|   | 
|     @Autowired  | 
|     private OtherUserService otherUserService;  | 
|   | 
|     @Autowired  | 
|     private UserTargetInfoService userTargetInfoService;  | 
|   | 
|     @Autowired  | 
|     private OrderItemService orderItemService;  | 
|   | 
|     @Autowired  | 
|     private AccountMapper accountMapper;  | 
|   | 
|   | 
|     @Override  | 
|     public Object account(AllAcountParamDto allAcountParamDto) {  | 
|         String now = DateUtil.now();  | 
|   | 
|         //推广返利  | 
|         //回收员返利开关 0 开启  | 
|         int receiveSwitch = 1;  | 
|   | 
|         //推广员返利开关  | 
|         int proSwitch = 1;  | 
|   | 
|         //返利比例  | 
|         BigDecimal scale = BigDecimal.ZERO;  | 
|   | 
|         //推广员返利有效时长 天  | 
|         int days = 0;  | 
|         //回收员返利有效时长 天  | 
|         int receiveDays = 0;  | 
|   | 
|         //支持的合伙人  | 
|         List<String> partnerIds=CollUtil.newArrayList();  | 
|   | 
|         List<ConfigInfo> promotion_rebate = configService.findByGroup("PROMOTION_REBATE");  | 
|         for (ConfigInfo configInfo : promotion_rebate) {  | 
|             String configTypeCode = configInfo.getConfigTypeCode();  | 
|             String configValue = configInfo.getConfigValue();  | 
|             if (configTypeCode.equals("PROMOTION_REBATE_RECEIVE_SWITCH")) {  | 
|                 receiveSwitch = Convert.toInt(configValue);  | 
|             } else if (configTypeCode.equals("PROMOTION_REBATE_SWITCH")) {  | 
|                 proSwitch = Convert.toInt(configValue);  | 
|             } else if (configTypeCode.equals("PROMOTION_REBATE_SCALE")) {  | 
|                 scale = Convert.toBigDecimal(configValue);  | 
|             } else if (configTypeCode.equals("PROMOTION_REBATE_DAYS")) {  | 
|                 days = Convert.toInt(configValue);  | 
|             } else if (configTypeCode.equals("PROMOTION_REBATE_RECEIVE_DAYS")) {  | 
|                 receiveDays = Convert.toInt(configValue);  | 
|             }else if(configTypeCode.equals("PROMOTION_PARTNER_ID")){  | 
|                 if(StrUtil.isNotBlank(configValue)){  | 
|                     partnerIds= CollUtil.toList(configValue.split(","));  | 
|                 }  | 
|             }  | 
|         }  | 
|   | 
|         //存在返利开关开启  | 
|         if (receiveSwitch == 0 || proSwitch == 0) {  | 
|             String orderId = allAcountParamDto.getFlowNo();  | 
|             //查询出所有订单列表  | 
|             List<OrderInfo> byIds = orderMapper.findByIds(orderId.split(","));  | 
|   | 
|             //去重用户  | 
|             ArrayList<OrderInfo> collect = byIds.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OrderInfo::getCreateUserId))), ArrayList::new));  | 
|   | 
|             //去重平台用户  | 
|             for (Iterator<OrderInfo> iterator = collect.iterator(); iterator.hasNext(); ) {  | 
|                 OrderInfo orderInfo = iterator.next();  | 
|                 String createMobile = orderInfo.getCreateMobile();  | 
|                 List<OtherUserInfo> byMobile = otherUserService.findByMobile(createMobile);  | 
|                 if (CollUtil.isNotEmpty(byMobile)) {  | 
|                     iterator.remove();  | 
|                 }  | 
|   | 
|   | 
|             }  | 
|   | 
|   | 
|             //先筛选是被推广的用户  | 
|             List<String> collect1 = collect.stream().map(OrderInfo::getCreateUserId).collect(Collectors.toList());  | 
|   | 
|             //所有下单的用户  | 
|             List<UserInfo> byIds1 = userService.findByIds(collect1);  | 
|   | 
|             if (CollUtil.isNotEmpty(collect)) {  | 
|                 List<String> collect2 = byIds1.stream().map(UserInfo::getMobilePhone).collect(Collectors.toList());  | 
|                 //筛选没推广人的用户  | 
|                 List<UserTargetInfo> byMobiles = userTargetInfoService.findByMobiles(collect2);  | 
|                 if (CollUtil.isNotEmpty(byMobiles)) {  | 
|                     List<String> mobiles = byMobiles.stream().map(UserTargetInfo::getMobile).collect(Collectors.toList());  | 
|                     List<OrderInfo> collect3 = collect.stream().filter(x -> mobiles.contains(x.getCreateMobile())).collect(Collectors.toList());  | 
|   | 
|                     if (CollUtil.isNotEmpty(collect3)) {  | 
| //  | 
| //                                //筛选当前还没被返利的用户  | 
| //                                for (Iterator<OrderInfo> iterator = collect3.iterator(); iterator.hasNext(); ) {  | 
| //                                    OrderInfo next =  iterator.next();  | 
| //                                    payInfoService.findByOtherAndUserAndType(next.getCreateUserId())  | 
| //                                    List<PlatformCapitalInfo> byInviteUserIdAndType = platformCapitalSevice.findByInviteUserIdAndType(next.getCreateUserId(), UserEnum.推广返利规则.getValue());  | 
| //                                    if(CollUtil.isNotEmpty(byInviteUserIdAndType)){  | 
| //                                        iterator.remove();  | 
| //                                    }  | 
| //                                }  | 
|   | 
|                         //设置当前用户的推广人  | 
|                         for (Iterator<OrderInfo> iterator = collect3.iterator(); iterator.hasNext(); ) {  | 
|                             OrderInfo orderInfo =  iterator.next();  | 
|                             String createMobile = orderInfo.getCreateMobile();  | 
|                             List<UserTargetInfo> collect4 = byMobiles.stream().filter(x -> x.getMobile().equals(createMobile)).collect(Collectors.toList());  | 
|                             String realUserId = collect4.get(0).getRealUserId();  | 
|                             orderInfo.setRealUserId(realUserId);  | 
|                             OtherUserInfo otherUserInfo = otherUserService.findById(realUserId);  | 
|                             //筛选出当前回收员的合伙人不等于配置的合伙人  | 
|                             if(!partnerIds.contains(otherUserInfo.getPartnerId())){  | 
|                                 iterator.remove();  | 
|                                 continue;  | 
|                             }  | 
|                             orderInfo.setRealUserType(otherUserInfo.getUserType());  | 
|                             orderInfo.setPartnerId(otherUserInfo.getPartnerId());  | 
|                         }  | 
|   | 
|                         //筛选注册时间和下单完成时间相差在有效范围内  | 
|                         for (Iterator<OrderInfo> iterator = collect3.iterator(); iterator.hasNext(); ) {  | 
|                             OrderInfo orderInfo = iterator.next();  | 
|                             //下单人注册时间  | 
|                             String registTime = orderInfo.getRegistTime();  | 
|                             //订单完成时间  | 
|                             String completeTime = orderInfo.getCompleteTime();  | 
|                             long l = 0;  | 
|                             if (SpringUtil.isProd()) {  | 
|                                 l = DateUtil.betweenDay(DateUtil.parseDateTime(registTime), DateUtil.parseDateTime(completeTime), true);  | 
|                             } else {  | 
|                                 l = DateUtil.between(DateUtil.parseDateTime(registTime), DateUtil.parseDateTime(completeTime), DateUnit.MINUTE);  | 
|                             }  | 
|                             if (StrUtil.isNotBlank(orderInfo.getRealUserType())) {  | 
|                                 if (CommonEnum.回收员.getValue().equals(orderInfo.getRealUserType())) {  | 
|                                     if (l > receiveDays) {  | 
|                                         iterator.remove();  | 
|                                     } else if (receiveSwitch == 1) {  | 
|                                         iterator.remove();  | 
|                                     }  | 
|   | 
|                                 } else if (CommonEnum.推广员.getValue().equals(orderInfo.getRealUserType())) {  | 
|                                     if (l > days) {  | 
|                                         iterator.remove();  | 
|                                     } else if (proSwitch == 1) {  | 
|                                         iterator.remove();  | 
|                                     }  | 
|                                 }  | 
|                             } else {  | 
|                                 iterator.remove();  | 
|                             }  | 
|                         }  | 
|   | 
|                         //筛选订单含有纸的小类信息  | 
|                         for (Iterator<OrderInfo> iterator = collect3.iterator(); iterator.hasNext(); ) {  | 
|                             OrderInfo orderInfo = iterator.next();  | 
|                             String orderId1 = orderInfo.getOrderId();  | 
|                             String addressId = orderInfo.getAddressId();  | 
|                             //查询回收员区域  | 
|                             List<OrderItemInfo> byOrderIdAndPaper = orderItemService.findByOrderIdAndPaper(orderId1, addressId);  | 
|                             if (CollUtil.isEmpty(byOrderIdAndPaper)) {  | 
|                                 iterator.remove();  | 
|                             } else {  | 
|                                 orderInfo.setPaperItemList(byOrderIdAndPaper);  | 
|                             }  | 
|                         }  | 
|   | 
|   | 
|                         //进行返利 注意如果推广用户有回收员身份,需要返利到其回收员账户上  | 
|                         List<AccountVo> accountVos=null;  | 
|                         for (OrderInfo orderInfo : collect3) {  | 
|                             //每个订单下纸类的总金额  | 
|                             BigDecimal paperMoney = new BigDecimal("0");  | 
|                             List<OrderItemInfo> paperItemList = orderInfo.getPaperItemList();  | 
|                             for (OrderItemInfo orderItemInfo : paperItemList) {  | 
|                                 String money = orderItemInfo.getMoney();  | 
|                                 paperMoney = NumberUtil.add(paperMoney, Convert.toBigDecimal(money));  | 
|                             }  | 
|   | 
|                             //返利的金额  | 
|                             BigDecimal mul = NumberUtil.mul(paperMoney, scale);  | 
|                             if(mul.compareTo(BigDecimal.ZERO)==0){  | 
|                                 continue;  | 
|                             }  | 
| //                            allMul = NumberUtil.add(allMul, mul);  | 
|                             String s = businessUtil.changeMoney(Convert.toStr(mul));  | 
|   | 
|                             String realUserId = orderInfo.getRealUserId();  | 
|                             if (CommonEnum.回收员.getValue().equals(orderInfo.getRealUserType())) {  | 
|   | 
|   | 
|                             } else if (CommonEnum.推广员.getValue().equals(orderInfo.getRealUserType())) {  | 
|                                 //如果存在回收员的身份。则转换成回收员或入库员的账户操作  | 
|                                 OtherUserInfo otherUserInfo1 = otherUserService.turnReceiver(realUserId);  | 
|                                 if (otherUserInfo1 != null) {  | 
|                                     realUserId = otherUserInfo1.getUserId();  | 
|                                 }  | 
|                             }  | 
|   | 
|                             Map hashMap = new HashMap();  | 
|                             hashMap.put("userId", realUserId);  | 
|                             accountVos = accountMapper.queryMyMoney(hashMap);  | 
|                             if (CollUtil.isNotEmpty(accountVos)) {  | 
|                                 AccountVo accountVo1 = accountVos.get(0);  | 
|                                 allAcountParamDto.setFlowNo(orderInfo.getOrderId());  | 
|                                 allAcountParamDto.setOtherUserId(realUserId);  | 
|                                 allAcountParamDto.setMoney(Convert.toBigDecimal(s));  | 
|                                 allAcountParamDto.setOtherAccountId(accountVo1.getAccountId());  | 
|                                 allAcountParamDto.setOtherOldMoney(Convert.toBigDecimal(accountVo1.getMoney()));  | 
|                                 allAcountParamDto.setPartnerId(orderInfo.getPartnerId());  | 
|   | 
|                                 log.debug("准备进行推广返利支出操作:{}", JSONUtil.toJsonPrettyStr(allAcountParamDto));  | 
|                                 //推广返利 合伙人扣钱,推广用户加钱  | 
|   | 
|   | 
|                                 Map<String, Object> payMap = new HashMap();  | 
|                                 String payOrderId = idUtils.generate("ZF", 0);  | 
|                                 payMap.put("payOrderId", payOrderId);  | 
|                                 payMap.put("orderId", allAcountParamDto.getFlowNo());  | 
|                                 payMap.put("createUserId", allAcountParamDto.getOtherUserId());  | 
|                                 payMap.put("money", allAcountParamDto.getMoney());  | 
|                                 payMap.put("accountId", allAcountParamDto.getOtherAccountId());  | 
|                                 payMap.put("payType", PayEnum.分享返利.getValue());  | 
|                                 payMap.put("status", PayEnum.已支付.getValue());  | 
|                                 payMap.put("createTime", now);  | 
|                                 payInfoMapper.payInfoAdd(payMap);  | 
|   | 
|                                 //推广员账号变更  | 
|                                 accountService.updateMoneyByUserId(allAcountParamDto.getOtherUserId(), allAcountParamDto.getMoney());  | 
|                                 AccountLog accountLog = new AccountLog();  | 
|                                 accountLog.setCreateTime(now);  | 
|                                 accountLog.setAccountId(allAcountParamDto.getOtherAccountId());  | 
|                                 accountLog.setCreateUserId(allAcountParamDto.getOtherUserId());  | 
|                                 accountLog.setChannelType(Convert.toShort(CommonEnum.推广返利.getValue()));  | 
|                                 accountLog.setOldMoney(Convert.toStr(allAcountParamDto.getOtherOldMoney()));  | 
|                                 accountLog.setNewMoney(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(accountLog.getOldMoney()), allAcountParamDto.getMoney())));  | 
|                                 accountLog.setOrderId(payOrderId);  | 
|                                 accountLogService.add(accountLog);  | 
|   | 
|                                 //合伙人账户变更  | 
|                                 PartnerAccountLog partnerAccountLog = new PartnerAccountLog();  | 
|                                 partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId());  | 
|                                 partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());  | 
|                                 partnerAccountLog.setType(OrderEnum.推广返利.getValue());  | 
|                                 partnerAccountLog.setMoney("-" + businessUtil.changeMoney(allAcountParamDto.getMoney()));  | 
|                                 partnerAccountLog.setRuleId(allAcountParamDto.getRuleId());  | 
|                                 partnerAccountLogService.add(partnerAccountLog);  | 
|                                 //修改合伙人账户  | 
|                                 partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(), allAcountParamDto.getMoney());  | 
|   | 
|                                 //入库员消息  | 
|                                 SysMessage sysMessage = new SysMessage();  | 
|                                 sysMessage.setCreateTime(now);  | 
|                                 sysMessage.setCreateUserId(allAcountParamDto.getUserId());  | 
|                                 sysMessage.setFlag(SysEnum.未读.getValue());  | 
|                                 sysMessage.setMessage("订单" + allAcountParamDto.getFlowNo() + "入库完成,您收到返利" + allAcountParamDto.getMoney() + "元");  | 
|                                 sysMessage.setMessageSubTypeName("推广返利");  | 
|                                 //充值提现根据文字匹配 不根据类型  | 
|                                 sysMessage.setMessageType(SysEnum.返利.getValue());  | 
|                                 sysMessage.setUserId(allAcountParamDto.getOtherUserId());  | 
|                                 sysMessageMapper.insert(sysMessage);  | 
|                             }  | 
|                         }  | 
|                         if (CollUtil.isNotEmpty(accountVos)) {  | 
|                             log.debug("推广返利支出操作已完成");  | 
|                         }  | 
|                     }  | 
|                 }  | 
|             }  | 
|         }  // if end  | 
|   | 
|         return null;  | 
|     }  | 
| }  |