| package com.xzx.gc.order.service;  | 
|   | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.date.DateUtil;  | 
| import cn.hutool.core.util.NumberUtil;  | 
| import cn.hutool.json.JSONUtil;  | 
| import com.xzx.gc.common.constant.*;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.common.utils.IdUtils;  | 
| import com.xzx.gc.entity.*;  | 
| import com.xzx.gc.model.comon.account.AllAcountParamDto;  | 
| import com.xzx.gc.order.mapper.AccountLogMapper;  | 
| import com.xzx.gc.order.mapper.OrderMapper;  | 
| import com.xzx.gc.order.mapper.PayInfoMapper;  | 
| import com.xzx.gc.order.mapper.SysMessageMapper;  | 
| 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 tk.mybatis.mapper.entity.Example;  | 
|   | 
| import java.math.BigDecimal;  | 
| import java.util.ArrayList;  | 
| import java.util.HashMap;  | 
| import java.util.List;  | 
| import java.util.Map;  | 
|   | 
| @Service  | 
| @Transactional  | 
| @Slf4j  | 
| public class ShareRebateService extends BaseAccountService {  | 
|     @Autowired  | 
|     private UserShareService userShareService;  | 
|     @Autowired  | 
|     private UserService userService;  | 
|     @Autowired  | 
|     private BlackWhiteService blackWhiteService;  | 
|     @Autowired  | 
|     private OrderMapper orderMapper;  | 
|     @Autowired  | 
|     private RedPaperRuleService redPaperRuleService;  | 
|     @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 AccountLogMapper accountLogMapper;  | 
|   | 
|     @Autowired  | 
|     private IdUtils idUtils;  | 
|   | 
|   | 
|     @Override  | 
|     public Object account(AllAcountParamDto allAcountParamDto) {  | 
|         String now = DateUtil.now();  | 
|         RedPaperRule one = redPaperRuleService.getOne(UserEnum.分享分利规则.getValue(), allAcountParamDto.getPartnerId());  | 
|         if (one != null) {  | 
|             BigDecimal moneyDecimal = NumberUtil.toBigDecimal(allAcountParamDto.getMoney());  | 
|             //分享返利  | 
|             UserShareInfo userShareInfo1 = userShareService.findByRegister(allAcountParamDto.getMobile());  | 
|             if (userShareInfo1 != null) {  | 
|                 //邀请人手机号码  | 
|                 String mobilePhone = userShareInfo1.getMobilePhone();  | 
|                 UserInfo userInfo1 = userService.findByMobile(mobilePhone);  | 
|                 if (userInfo1 != null) {  | 
|                     //邀请人的Id  otherUserId为被被邀请人ID  | 
|                     allAcountParamDto.setUserId(userInfo1.getUserId());  | 
|                     //邀请人是否在白名单  如果不是在白名单则只有被邀请的用户首单才可以分享分利  | 
|                     boolean whiteByMobile = blackWhiteService.isWhiteByMobile(OrderEnum.分享.getValue(), mobilePhone, OrderEnum.白名单.getValue());  | 
|                     boolean hasMul = false;  | 
|                     if (whiteByMobile) {  | 
|                         hasMul = true;  | 
|                     } else {  | 
|                         Example example = new Example(OrderInfo.class);  | 
|                         Example.Criteria criteria = example.createCriteria();  | 
|                         criteria.andEqualTo("createUserId", allAcountParamDto.getOtherUserId());  | 
|                         List<String> list = new ArrayList<>();  | 
|                         list.add(OrderEnum.待入库.getValue());  | 
|                         list.add(OrderEnum.入库中.getValue());  | 
|                         list.add(OrderEnum.完成.getValue());  | 
|                         criteria.andIn("orderStatus", list);  | 
|                         if (orderMapper.selectCountByExample(example) == 0) {  | 
|                             hasMul = true;  | 
|                         }  | 
|                     }  | 
|   | 
|                     //存在返利 执行操作  | 
|                     if (hasMul) {  | 
|                         AccountInfo accountInfo1 = accountService.findByUserId(userInfo1.getUserId());  | 
|                         if (accountInfo1 != null) {  | 
|                             BigDecimal div = NumberUtil.div(Convert.toBigDecimal(one.getShareRatio(), new BigDecimal("0")), new BigDecimal("100"));  | 
|                             //获取返利金额  | 
|                             BigDecimal mul = NumberUtil.mul(moneyDecimal, div);  | 
|                             mul = Convert.toBigDecimal(businessUtil.changeMoney(Convert.toStr(mul, "0")));  | 
|                             if(mul.compareTo(BigDecimal.ZERO)==0){  | 
|                                 return null;  | 
|                             }  | 
|                             BigDecimal rewardMoney = Convert.toBigDecimal(userShareInfo1.getRewardMoney(), Constants.MONEY_INIT);  | 
|                             userShareInfo1.setRewardMoney(Convert.toStr(NumberUtil.add(rewardMoney, mul)));  | 
|                             userShareService.updateById(userShareInfo1);  | 
|                             allAcountParamDto.setMoney(mul);  | 
|                             allAcountParamDto.setAccountId(accountInfo1.getAccountId());  | 
|                             allAcountParamDto.setMobile(accountInfo1.getAccountName());  | 
|   | 
|   | 
|                             log.debug("准备进行分享返利操作:{}", JSONUtil.toJsonPrettyStr(allAcountParamDto));  | 
|                             //分享返利 用户加钱 合伙人扣钱  | 
|                             PartnerAccountLog partnerAccountLog = new PartnerAccountLog();  | 
|                             partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId());  | 
|                             partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());  | 
|                             partnerAccountLog.setType(OrderEnum.分享返利.getValue());  | 
|                             partnerAccountLog.setMoney("-" + allAcountParamDto.getMoney().toString());  | 
|                             partnerAccountLog.setRuleId(allAcountParamDto.getRuleId());  | 
|                             partnerAccountLogService.add(partnerAccountLog);  | 
|                             partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(), allAcountParamDto.getMoney());  | 
|   | 
|                             //消息 环保金明细  | 
|                             Map payMap = new HashMap();  | 
|                             String payOrderId = idUtils.generate("ZF", 0);  | 
|                             payMap.put("payOrderId", payOrderId);  | 
|                             payMap.put("orderId", allAcountParamDto.getFlowNo());  | 
|                             payMap.put("createUserId", allAcountParamDto.getUserId());  | 
|                             payMap.put("money", allAcountParamDto.getMoney());  | 
|                             payMap.put("accountId", allAcountParamDto.getAccountId());  | 
|                             payMap.put("payType", PayEnum.分享返利.getValue());  | 
|                             payMap.put("status", PayEnum.已支付.getValue());  | 
|                             payMap.put("createTime", now);  | 
|                             payInfoMapper.payInfoAdd(payMap);  | 
|   | 
|                             SysMessage sysMessage = new SysMessage();  | 
|                             sysMessage.setCreateTime(now);  | 
|                             sysMessage.setCreateUserId(allAcountParamDto.getUserId());  | 
|                             sysMessage.setFlag(SysEnum.未读.getValue());  | 
|                             sysMessage.setMessage("您邀请的好友“" + allAcountParamDto.getMobile() + "”已下单完成,您收到返利" + allAcountParamDto.getMoney().toString() + "元");  | 
|                             sysMessage.setMessageSubTypeName("返利");  | 
|                             sysMessage.setMessageType(SysEnum.返利.getValue());  | 
|                             sysMessage.setUserId(allAcountParamDto.getUserId());  | 
|                             sysMessageMapper.insert(sysMessage);  | 
|   | 
|                             //用户日志  | 
|                             accountService.updateMoneyByUserId(allAcountParamDto.getUserId(), allAcountParamDto.getMoney());  | 
|                             AccountLog accountLog = new AccountLog();  | 
|                             accountLog.setCreateTime(now);  | 
|                             accountLog.setAccountId(allAcountParamDto.getAccountId());  | 
|                             accountLog.setCreateUserId(allAcountParamDto.getUserId());  | 
|                             accountLog.setChannelType(Convert.toShort(CommonEnum.分享返利操作.getValue()));  | 
|                             accountLog.setOldMoney(allAcountParamDto.getMoney().toString());  | 
|                             accountLog.setNewMoney(Convert.toStr(NumberUtil.add(Convert.toBigDecimal(accountLog.getOldMoney()), allAcountParamDto.getMoney())));  | 
|                             accountLog.setOrderId(payOrderId);  | 
|                             accountLogMapper.insertSelective(accountLog);  | 
|                             log.debug("分享返利操作已完成:用户:{},金额:{}",allAcountParamDto.getUserId(), allAcountParamDto.getMoney().toString());  | 
|                         }  | 
|                     }  | 
|                 }  | 
|             }  | 
|         }  | 
|         return null;  | 
|     }  | 
| }  |