| New file | 
 |  |  | 
 |  |  | package com.xzx.gc.order.service; | 
 |  |  |  | 
 |  |  | import cn.hutool.core.util.StrUtil; | 
 |  |  | import com.xzx.gc.common.constant.CommonEnum; | 
 |  |  | import com.xzx.gc.entity.*; | 
 |  |  | import com.xzx.gc.order.mapper.*; | 
 |  |  | 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.Date; | 
 |  |  | import java.util.List; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @author wzy | 
 |  |  |  * @date 2021-07-14 | 
 |  |  |  **/ | 
 |  |  | @Slf4j | 
 |  |  | @Service | 
 |  |  | @Transactional(rollbackFor = Exception.class) | 
 |  |  | public class DistribService { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private UserHeadDetailsMapper userHeadDetailsMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private UserHeadRelateMapper userHeadRelateMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private JhyOrderMapper jhyOrderMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private RedPaperRuleMapper redPaperRuleMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private JhyOrderItemsMapper jhyOrderItemsMapper; | 
 |  |  |  | 
 |  |  |     public void distribRecord(Long orderId, String userId) { | 
 |  |  |         UserHeadRelate userHeadRelate = userHeadRelateMapper.selectRelateByUserId(userId); | 
 |  |  |         if (userHeadRelate == null) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         RedPaperRule redPaperRule = redPaperRuleMapper.selectDistribRule(); | 
 |  |  |         if (0 == redPaperRule.getStatus()) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Integer count = userHeadDetailsMapper.selectDetailsCount(userId); | 
 |  |  |  | 
 |  |  |         List<String> remark = new ArrayList<>(); | 
 |  |  |         BigDecimal totalReturnScore = BigDecimal.ZERO; | 
 |  |  |         BigDecimal totalReturnCoin = BigDecimal.ZERO; | 
 |  |  |  | 
 |  |  |         BigDecimal totalScore = jhyOrderItemsMapper.selectOrderScoreByOrderId(orderId); | 
 |  |  |         JhyOrder order = jhyOrderMapper.selectByPrimaryKey(orderId); | 
 |  |  |         if (count == 0) { | 
 |  |  |             String value = getRuleValue(redPaperRule, CommonEnum.团长首单奖励.getValue()); | 
 |  |  |  | 
 |  |  |             // 首单返利 | 
 |  |  |             if (StrUtil.isNotBlank(value) && !"0".equals(value)) { | 
 |  |  |                 BigDecimal money = new BigDecimal(value); | 
 |  |  |                 totalReturnScore = totalReturnScore.add(money); | 
 |  |  |                 remark.add("首单返利:" + money); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if (count > 0) { | 
 |  |  |             String value = getRuleValue(redPaperRule, CommonEnum.首单后返利数量.getValue()); | 
 |  |  |  | 
 |  |  |             if (StrUtil.isNotBlank(value) && !"0".equals(value)) { | 
 |  |  |                 // 首单完成后,在value单内,返利积分或者环保币 | 
 |  |  |                 if (count - 1 <= Integer.parseInt(value)) { | 
 |  |  |                     String scoreStr = getRuleValue(redPaperRule, CommonEnum.返利固定积分.getValue()); | 
 |  |  |                     String coinStr = getRuleValue(redPaperRule, CommonEnum.返利环保币比例.getValue()); | 
 |  |  |                     // 根据积分返利固定积分 | 
 |  |  |                     if (StrUtil.isNotBlank(scoreStr) && !"0".equals(scoreStr)) { | 
 |  |  |                         BigDecimal score = new BigDecimal(scoreStr); | 
 |  |  |                         totalReturnScore = totalReturnScore.add(score); | 
 |  |  |                         remark.add("首单完成后返固定积分:" + scoreStr); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                     // 根据订单总积分, 1:40比例,换算成环保币,返对应百分比 | 
 |  |  |                     if (StrUtil.isNotBlank(coinStr) && !"0".equals(coinStr)) { | 
 |  |  |                         BigDecimal coinRatio = new BigDecimal(coinStr).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_DOWN); | 
 |  |  |                         BigDecimal totalCoin = totalScore.multiply(new BigDecimal(40)); | 
 |  |  |  | 
 |  |  |                         BigDecimal returnCoin = totalCoin.multiply(coinRatio).setScale(2, BigDecimal.ROUND_DOWN); | 
 |  |  |                         totalReturnCoin = totalReturnCoin.add(returnCoin); | 
 |  |  |                         remark.add("首单完成后返比例环保币:" + returnCoin +"(" + coinRatio +")"); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                     // 当用户进行订单数量,超过value单后,每完成一单返利指定积分 | 
 |  |  |                 } else { | 
 |  |  |                     String finishValue = getRuleValue(redPaperRule, CommonEnum.完成数量后返积分.getValue()); | 
 |  |  |                     if (StrUtil.isNotBlank(finishValue) && !"0".equals(finishValue)) { | 
 |  |  |                         BigDecimal score = new BigDecimal(finishValue); | 
 |  |  |                         totalReturnScore = totalReturnScore.add(score); | 
 |  |  |                         remark.add("完成指定"+ Integer.parseInt(value) +"数量后每单返利:" + score); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         UserHeadDetails userHeadDetails = new UserHeadDetails(); | 
 |  |  |         userHeadDetails.setHeadUserId(userHeadRelate.getHeadUserId()); | 
 |  |  |         userHeadDetails.setUserId(userId); | 
 |  |  |         userHeadDetails.setOrderNo(order.getOrderNo()); | 
 |  |  |         userHeadDetails.setAmount(totalReturnCoin); | 
 |  |  |         userHeadDetails.setScore(totalReturnScore); | 
 |  |  |         userHeadDetails.setCreatedTime(new Date()); | 
 |  |  |         userHeadDetailsMapper.insert(userHeadDetails); | 
 |  |  |  | 
 |  |  |         userHeadRelate.setAmount(userHeadRelate.getAmount().add(totalReturnCoin)); | 
 |  |  |         userHeadRelate.setScore(userHeadDetails.getScore().add(totalReturnScore)); | 
 |  |  |         userHeadRelateMapper.updateByPrimaryKey(userHeadRelate); | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private String getRuleValue(RedPaperRule redPaperRule, String key) { | 
 |  |  |         List<String> rules = StrUtil.split(redPaperRule.getSharingProfitType(), ','); | 
 |  |  |         List<String> values = StrUtil.split(redPaperRule.getShareRatio(), ','); | 
 |  |  |  | 
 |  |  |         int i = rules.indexOf(key); | 
 |  |  |         return values.get(i); | 
 |  |  |     } | 
 |  |  | } |