| package com.xzx.gc.order.service;  | 
|   | 
| import cn.hutool.core.collection.CollUtil;  | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.text.StrBuilder;  | 
| import com.xzx.gc.common.constant.Constants;  | 
| import com.xzx.gc.common.constant.UserEnum;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.common.utils.StringUtils;  | 
| import com.xzx.gc.entity.OtherUserInfo;  | 
| import com.xzx.gc.entity.RebateRule;  | 
| import com.xzx.gc.entity.RedPaperRule;  | 
| import com.xzx.gc.entity.UserInfo;  | 
| import com.xzx.gc.order.dto.RuleDto;  | 
| import com.xzx.gc.order.mapper.OrderMapper;  | 
| import com.xzx.gc.order.mapper.RebateRuleMapper;  | 
| import com.xzx.gc.order.mapper.UserMapper;  | 
| 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.ArrayList;  | 
| import java.util.Comparator;  | 
| import java.util.List;  | 
| import java.util.stream.Collectors;  | 
|   | 
| @Service  | 
| @Transactional  | 
| @Slf4j  | 
| public class RuleService {  | 
|   | 
|     @Autowired  | 
|     private RedPaperRuleService redPaperRuleService;  | 
|   | 
|     @Autowired  | 
|     private OrderService orderService;  | 
|   | 
|     @Autowired  | 
|     private RebateRuleService rebateRuleService;  | 
|   | 
|   | 
|     @Autowired  | 
|     private OtherUserService otherUserService;  | 
|   | 
|     @Autowired  | 
|     private UserService userService;  | 
|     /**  | 
|      * 查询用户重量返利  | 
|      * @param userId  | 
|      * @param  checkFlag 检查参与用户是平台用户则弃掉  | 
|      * @return  | 
|      */  | 
|     public RuleDto findWeightRebate(String userId,boolean checkFlag,String partnerId){  | 
|         RuleDto ruleDto = new RuleDto();  | 
|         if(checkFlag){  | 
|             UserInfo byId = userService.findById(userId);  | 
|             if(byId!=null){  | 
|                 String mobilePhone = byId.getMobilePhone();  | 
|                 List<OtherUserInfo> byMobile = otherUserService.findByMobile(mobilePhone);  | 
|                 if(CollUtil.isNotEmpty(byMobile)) {  | 
|                     return ruleDto;  | 
|                 }  | 
|             }  | 
|         }  | 
|         RedPaperRule one = redPaperRuleService.getOne(UserEnum.重量返利规则.getValue(),partnerId);  | 
|         if (one != null) {  | 
|             ruleDto.setRuleId(one.getId());  | 
|             String s = changeProductType(one.getProductType());  | 
|             ruleDto.setProductType(s);  | 
|             List<RebateRule> rebateRules = rebateRuleService.findByRuleId(one.getId());  | 
|             ruleDto.setRebateRules(rebateRules);  | 
|         } else {  | 
|             log.warn("重量返利规则不存在");  | 
|         }  | 
|         return ruleDto;  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 查询用户阶梯返利  | 
|      * @param userId  | 
|      * @return  | 
|      */  | 
|     public RuleDto findStepRebate(String userId,boolean checkFlag,String partnerId){  | 
|         RuleDto ruleDto=new RuleDto();  | 
|         ruleDto.setScale(UserEnum.无返利.getValue());  | 
|         if(checkFlag){  | 
|             UserInfo byId = userService.findById(userId);  | 
|             if(byId!=null){  | 
|                 String mobilePhone = byId.getMobilePhone();  | 
|                 List<OtherUserInfo> byMobile = otherUserService.findByMobile(mobilePhone);  | 
|                 if(CollUtil.isNotEmpty(byMobile)) {  | 
|                     return ruleDto;  | 
|                 }  | 
|             }  | 
|         }  | 
|         RedPaperRule one = redPaperRuleService.getOne(UserEnum.阶梯返利规则.getValue(),partnerId);  | 
|         if(one!=null){  | 
|             ruleDto.setRuleId(one.getId());  | 
|             ruleDto.setConversionPerUnit(one.getConversionPerUnit());  | 
|             List<RebateRule> rebateRules = rebateRuleService.findByRuleId(one.getId());  | 
|             String rate=null;  | 
|             String minWeight=null;  | 
|             String minScale=null;  | 
|             if(CollUtil.isNotEmpty(rebateRules)){  | 
|                 //sort升序  | 
|                 List<RebateRule> collect = rebateRules.stream().sorted(Comparator.comparing(RebateRule::getSort)).collect(Collectors.toList());  | 
|                 ruleDto.setRebateRules(collect);  | 
|                 ruleDto.setMinScale(collect.get(0).getRate());  | 
|                 ruleDto.setMinWeight(collect.get(0).getWeight());  | 
|                 //用户以前下单的总重量  | 
|                 BigDecimal sumWeight = orderService.findSumWeight(userId,one);  | 
|                 ruleDto.setSumWeight(sumWeight);  | 
|   | 
|                 //从大到小  | 
|                 rebateRules = rebateRules.stream().sorted(Comparator.comparing(RebateRule::getSort).reversed()).collect(Collectors.toList());  | 
|                 for (RebateRule rebateRule : rebateRules) {  | 
|                     String weight = rebateRule.getWeight();  | 
|                     String rate1 = rebateRule.getRate();  | 
|                     if(sumWeight.compareTo(Convert.toBigDecimal(weight))>=0){  | 
|                         rate = rate1;  | 
|                         break;  | 
|                     }  | 
|                 }  | 
|             }  | 
|   | 
|             if(rate!=null){  | 
|                 ruleDto.setScale(rate);  | 
|             }else{  | 
|                 //无返利  | 
|                 ruleDto.setMinScale(minScale);  | 
|                 ruleDto.setMinWeight(minWeight);  | 
|             }  | 
|         }  | 
|         return ruleDto;  | 
|     }  | 
|   | 
|     /**  | 
|      * 获取返利规则阶梯重量及返利  | 
|      * @return  | 
|      */  | 
|     public  List<RebateRule> getRebateRules(RedPaperRule one){  | 
|         List<RebateRule> rebateRules = rebateRuleService.findByRuleId(one.getId());  | 
|         if(CollUtil.isEmpty(rebateRules)) {  | 
|             rebateRules=new ArrayList<>();  | 
|         }  | 
|         return rebateRules;  | 
|     }  | 
|   | 
|     /**  | 
|      * 将类型转换成itemType  | 
|      * @param productType  | 
|      * @return  | 
|      */  | 
|     public String changeProductType(String productType){  | 
|         String[] split = productType.split(",");  | 
|         StrBuilder strBuilder=StrBuilder.create();  | 
|         for (String s : split) {  | 
|             if("1".equals(s)){  | 
|                 strBuilder.append(Constants.PAPER_WASTE_ITEM_TYPE).append(",");  | 
|             }else if("2".equals(s)){  | 
|                 strBuilder.append(Constants.PAPER_MIX_ITEM_TYPE).append(",");  | 
|             }  | 
|         }  | 
|         return StringUtils.removeLast(strBuilder, ",");  | 
|     }  | 
| }  |