package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.core.utils.StoreParaUtils;
import com.ssm.manage.dao.SsmRefundRechargeDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.extendEntity.NegativeKazin;
import com.ssm.manage.extendEntity.RecDocList;
import com.ssm.manage.extendEntity.RechargeNegative;
import com.ssm.manage.mapper.RefundRechargeMapper;
import com.ssm.manage.query.*;
import com.ssm.manage.response.RefundRechargeResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Roman;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SsmRefundRechargeServiceImpl extends BaseServiceImpl<RefundRecharge> implements SsmRefundRechargeService {
    @Autowired
    private SsmRefundRechargeDao dao;

    @Autowired
    private SsmRechargeAccountGiveService ssmRechargeAccountGiveService;

    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;

    @Autowired
    private SsmDebtService ssmDebtService;

    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;
    @Autowired
    private SsmRefundRecordService ssmRefundRecordService;

    @Autowired
    private SsmRefundDebtService ssmRefundDebtService;

    @Autowired
    private SsmRechargeTypeSevice ssmRechargeTypeSevice;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmRechargeService ssmRechargeService;


    @Autowired
    private SsmSalesmanService ssmSalesmanService;

    @Autowired
    private SsmUploadFilesService ssmUploadFilesService;

    @Autowired
    private SsmRepayTradeService ssmRepayTradeService;

    @Autowired
    private SsmMemberCsService ssmMemberCsService;
    @Autowired
    private RefundRechargeMapper mapper;

    @Override
    public SsmRefundRechargeDao getDao() {
        return dao;
    }

    @Override
    public PageInfo<RefundRechargeResponse> selectPage(RefundRechargeRequest refundRechargeRequest) {
        PageInfo<RefundRecharge> pageInfo =  dao.selectPage(refundRechargeRequest);
        List<RefundRechargeResponse> list = new ArrayList<>();
        //遍历充值退款单 设置业务员和文件
        List<RefundRecharge> list1 = new ArrayList<>();
        List<RefundRecharge> list2 = new ArrayList<>();
        for (RefundRecharge refundRecharge : pageInfo.getList()) {
            if(refundRecharge.getRecRefundMoney().compareTo(BigDecimal.valueOf(0))==1){
                list1.add(refundRecharge);
            }else {
                list2.add(refundRecharge);
            }
        }
        for (RefundRecharge refundRecharge : list1) {
            RefundRechargeResponse refundRechargeResponse = new RefundRechargeResponse();
            BeanUtils.copyProperties(refundRecharge, refundRechargeResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(refundRecharge);
            //查询退款单确认文件
            UploadFilesRequest uploadFilesRequest = new UploadFilesRequest();
            uploadFilesRequest.setDocId(refundRechargeRequest.getRefundOrderId());
            uploadFilesRequest.setDocNum(refundRecharge.getRefundNum());
            uploadFilesRequest.setDocType(Constant.doc_type_refund_recharge);
            List<UploadFiles> filesList = ssmUploadFilesService.selectList(uploadFilesRequest);
            //设置返回List
            refundRechargeResponse.setEntities(entities);
            refundRechargeResponse.setFileLists(filesList);

            RechargeType rechargeType = new RechargeType();
            rechargeType.setId(refundRecharge.getRechargeTypeId());
            RechargeType rechargeType1 = ssmRechargeTypeSevice.selectByPrimaryKey(rechargeType.getId());
            refundRechargeResponse.setRechargeTypeNum(rechargeType1.getRechargeTypeNum());
            RechargeAccountGive rechargeAccountGive = new RechargeAccountGive();
            rechargeAccountGive.setRechargeNum(refundRechargeResponse.getRechargeNum());
            rechargeAccountGive.setMemberId(refundRechargeResponse.getMemberId());
//            rechargeAccountGive.setStoreId(refundRechargeResponse.getStoreId());
            List<RechargeAccountGive> select = ssmRechargeAccountGiveService.select(rechargeAccountGive);
            if(select!=null && select.size()>0){
                List<RechargeAccountGive> collect = select.stream().filter(item -> item.getGiftConsumeMoney().compareTo(BigDecimal.valueOf(0)) == -1).collect(Collectors.toList());
                refundRechargeResponse.setRechargeAccountGiveList(collect);
            }
            refundRechargeResponse.setRefundRechargeList(list2);
            list.add(refundRechargeResponse);

        }
        PageInfo<RefundRechargeResponse> p = new PageInfo<>(list);
        return p;
    }

    public List<AchiCommEntity> getAchiCommList(RefundRecharge refundRecharge){
        List<AchiCommEntity> entities = new ArrayList<>();
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");
        if (null != refundRecharge.getSaleOneStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocNum(refundRecharge.getRefundNum());
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleOneNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleOneStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleOneStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleOneIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleOneRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleOneRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundRecharge.getSaleTwoStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleTwoNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleTwoStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleTwoStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleTwoIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleTwoRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleTwoRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleTwoRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleTwoRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleTwoRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundRecharge.getSaleThreeStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleThreeNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleThreeStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleThreeStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleThreeIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleThreeRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleThreeRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleThreeRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleThreeRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleThreeRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundRecharge.getSaleFourStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleFourNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleFourStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleFourStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleFourIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleFourRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleFourRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleFourRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleFourRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleFourRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundRecharge.getSaleFiveStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleFiveNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleFiveStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleFiveStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleFiveIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleFiveRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleFiveRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleFiveRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleFiveRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleFiveRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundRecharge.getSaleSixStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleSixNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleSixStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleSixStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleSixIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleSixRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleSixRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleSixRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleSixRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleSixRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundRecharge.getSaleSevenStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleSevenNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleSevenStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleSevenStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleSevenIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleSevenRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleSevenRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleSevenRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleSevenRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleSevenRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundRecharge.getSaleEightStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundRecharge.getRecRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundRecharge.getMemberId()));
            achiCommEntity.setMemberName(refundRecharge.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundRecharge.getRefundNum()));
            achiCommEntity.setDocType(Constant.doc_type_refund_recharge);
            achiCommEntity.setDocDate(refundRecharge.getRefundDate());
            achiCommEntity.setTradeName(refundRecharge.getRechargeTypeName());
            achiCommEntity.setBrandNumber(refundRecharge.getSaleEightNumber());
            achiCommEntity.setSaleStaffId(refundRecharge.getSaleEightStaffId());
            achiCommEntity.setSaleStaffName(refundRecharge.getSaleEightStaffName());
            achiCommEntity.setSaleIdentityCard(refundRecharge.getSaleEightIdentity());
            achiCommEntity.setSaleRatio(refundRecharge.getSaleEightRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleEightRatio(),hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleEightRatio(),hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundRecharge.getAchievementAmount(),BigDecimalUtils.div(refundRecharge.getSaleEightRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundRecharge.getCommissionAmount(),BigDecimalUtils.div(refundRecharge.getSaleEightRatio(),hundredBD)));
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        return entities;
    }


    @Override
    public List<RecDocList> recDocList(RefundRechargeRequest refundRechargeRequest) throws ParseException {
        Date date = new Date();
        List<RecDocList> recDocLists = new ArrayList<>();
        BigDecimal restMoney = refundRechargeRequest.getCardRestMoney();
        try {
            recDocLists = this.getRecDocList(date, recDocLists, refundRechargeRequest, restMoney);
        } catch (SmsException e) {
            return recDocLists;
        }
        return recDocLists;
    }

    public List<RecDocList> getRecDocList(Date date, List<RecDocList> recDocLists, RefundRechargeRequest refundRechargeRequest, BigDecimal restMoney) throws ParseException, SmsException {
        RechargeListRequest rechargeListRequest = new RechargeListRequest();
        rechargeListRequest.setRechargeDate(date);
        rechargeListRequest.setRechargeAccountId(refundRechargeRequest.getRecAccountId());
        rechargeListRequest.setStoreId(refundRechargeRequest.getStoreId());
        PageInfo<RechargeList> pageInfo = ssmRechargeService.page(rechargeListRequest);
        if (pageInfo.getList().size() > 0){
            for (RechargeList rechargeList : pageInfo.getList()) {
                RecDocList recDocList = new RecDocList();
                recDocList.setDate(rechargeList.getRechargeDate());
                recDocList.setDocNum(rechargeList.getRechargeNum());
                recDocList.setDocNumType(Constant.doc_type_recharge);
                recDocList.setMoney(rechargeList.getCollectionCashAmount());
                recDocLists.add(recDocList);
                //充值单有过欠款  查询还款记录
                if (rechargeList.getIsDebt() == 1){
                    RepayTradeList repayTradeList = new RepayTradeList();
                    repayTradeList.setDocId(rechargeList.getId());
                    List<RepayTradeList> tradeList = ssmRepayTradeService.select(repayTradeList);
                    if (null != tradeList && tradeList.size() > 0){
                        for (RepayTradeList list : tradeList) {
                            RecDocList recDoc = new RecDocList();
                            recDoc.setDate(list.getRepayDate());
                            recDoc.setDocNum(list.getRepayNum());
                            recDoc.setDocNumType(Constant.doc_type_repay);
                            recDoc.setMoney(list.getPayMoney());
                            recDocLists.add(recDoc);
                        }
                    }
                }
//                if (recDocLists.size() >= 4){
//                    break;
//                }
                if (restMoney==null || rechargeList.getRechargeAmount()==null || BigDecimalUtils.compareTo(restMoney, rechargeList.getRechargeAmount()) == 0){
                    //相等 判断是否欠款
                    if (rechargeList.getIsDebt() == 1){
                        restMoney = BigDecimalUtils.subtract(restMoney, rechargeList.getRechargeAmount());
                        continue;
                    }else {
                        //相等 没有欠款 结束 不在往上追
                        break;
                    }
                }else {
                    //退款账户剩余金额 > 标准充值金额
                    restMoney = BigDecimalUtils.subtract(restMoney, rechargeList.getRechargeAmount());

                }
            }
        }
//        if (recDocLists.size() < 4){
//            date = DateUtils.diffMonth(date, -1);
//            recDocLists = this.getRecDocList(date, recDocLists, refundRechargeRequest, restMoney);
//        }
        if (BigDecimalUtils.compareTo(restMoney, refundRechargeRequest.getCardRechargeMoney()) == 1){
            date = DateUtils.diffMonth(date, -1);
            recDocLists = this.getRecDocList(date, recDocLists, refundRechargeRequest, restMoney);
        }
        return recDocLists;
    }

    public BigDecimal refundRechargeMoney(RefundRechargeRequest refundRechargeRequest) throws ParseException {
        Date date = new Date();
//        List<RechargeList> list = new ArrayList<>();
        BigDecimal restMoney = refundRechargeRequest.getCardRestMoney();
        BigDecimal refundMoney = BigDecimalUtils.bigDecimal("0");
        try {
            refundMoney = this.getRefundRechargeMoney(date, refundMoney, refundRechargeRequest, restMoney);
        } catch (SmsException e) {
            log.error("SsmRefundRechargeServiceImpl.refundRechargeMoney:", e);
            return refundMoney;
        }
        return refundMoney;
    }

    @Override
    public List<RefundRecharge> selectListNew(RefundRechargeRequest refundRechargeRequest) {
        return mapper.selectListNew(refundRechargeRequest);
    }

    @Override
    public BigDecimal selectFKJByDate(StoreDailyRequest storeDailyRequest) {
        return mapper.selectFKJByDate(storeDailyRequest);
    }

    @Override
    public BigDecimal selectFKJByRechargeOut(StoreDailyRequest storeDailyRequest) {
        return mapper.selectFKJByRechargeOut(storeDailyRequest);
    }

    @Override
    public BigDecimal selectFKJByRechargeIn(StoreDailyRequest storeDailyRequest) {
        return mapper.selectFKJByRechargeIn(storeDailyRequest);
    }


    public BigDecimal getRefundRechargeMoney(Date date, BigDecimal refundMoney, RefundRechargeRequest refundRechargeRequest, BigDecimal restMoney) throws ParseException, SmsException {

        RechargeListRequest rechargeListRequest = new RechargeListRequest();
        rechargeListRequest.setRechargeDate(date);
        rechargeListRequest.setRechargeAccountId(refundRechargeRequest.getRecAccountId());
        rechargeListRequest.setStoreId(refundRechargeRequest.getStoreId());
        PageInfo<RechargeList> pageInfo = ssmRechargeService.page(rechargeListRequest);
        if (pageInfo.getList().size() > 0) {

            for (RechargeList rechargeList : pageInfo.getList()) {
                //退款账户剩余金额 < 标准充值金额 直接反算
                if (BigDecimalUtils.compareTo(restMoney, rechargeList.getRechargeAmount()) == -1) {
//                    refundMoney = restMoney;
                    break;
                } else if (BigDecimalUtils.compareTo(restMoney, rechargeList.getRechargeAmount()) == 0) {
                    //相等 判断是否欠款
                    if (rechargeList.getIsDebt() == 1) {
                        restMoney = BigDecimalUtils.subtract(restMoney, rechargeList.getRechargeAmount());
                        refundMoney = BigDecimalUtils.add(refundMoney, rechargeList.getRechargeAmount());
                        continue;
                    } else {
                        //相等 没有欠款 结束 不在往上追
                        refundMoney = BigDecimalUtils.add(refundMoney, rechargeList.getRechargeAmount());
                        return refundMoney;
                    }
                } else {
                    //退款账户剩余金额 > 标准充值金额
                    restMoney = BigDecimalUtils.subtract(restMoney, rechargeList.getRechargeAmount());
                    refundMoney = BigDecimalUtils.add(refundMoney, rechargeList.getRechargeAmount());
                    continue;
                }
            }
        }

            AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
            accountTradeRecordRequest.setStoreId(refundRechargeRequest.getStoreId());
            accountTradeRecordRequest.setInputDate(date);
            accountTradeRecordRequest.setAccountId(refundRechargeRequest.getRecAccountId());
            accountTradeRecordRequest.setDocTypes(refundRechargeRequest.getDocTypes());
            List<AccountTradeRecord> list = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
//            Map<String, List<AccountTradeRecord>> map = list.stream().collect(Collectors.groupingBy(AccountTradeRecord::getDocType));
            BigDecimal cashMoney = BigDecimalUtils.bigDecimal("0");
            if (!list.isEmpty()) {
                for (AccountTradeRecord accountTradeRecord : list) {
                    if (Constant.doc_type_store_cash.equals(accountTradeRecord.getDocType())) {
                        if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())) {
                            if (refundRechargeRequest.getType() == 1) {
                                cashMoney = BigDecimalUtils.add(cashMoney, accountTradeRecord.getAccountSpendMoney());
                            }else {
                                cashMoney = BigDecimalUtils.add(cashMoney, accountTradeRecord.getAccountGiftSpendMoney());
                            }
                        } else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getDocType())) {
                            if (refundRechargeRequest.getType() == 1) {
                                cashMoney = BigDecimalUtils.subtract(cashMoney, accountTradeRecord.getAccountSpendMoney());
                            }else {
                                cashMoney = BigDecimalUtils.subtract(cashMoney, accountTradeRecord.getAccountGiftSpendMoney());
                            }
                        }
                    }
                }
                //折扣卡
                if (refundRechargeRequest.getType() == 1) {
                    BigDecimal discount = BigDecimalUtils.div(refundRechargeRequest.getDiscount(), BigDecimalUtils.bigDecimal("100"));
                    cashMoney = BigDecimalUtils.multiply(BigDecimalUtils.div(cashMoney, discount),
                            BigDecimalUtils.subtract(BigDecimalUtils.bigDecimal("1"), discount));
                    //此时剩余金额 >= 该抵扣金额
                    if (BigDecimalUtils.compareTo(restMoney, cashMoney) >= 0) {
                        restMoney = BigDecimalUtils.subtract(restMoney, cashMoney);
                        refundMoney = BigDecimalUtils.add(refundMoney, restMoney);
                    } else {
                        // 剩余金额 < 该抵扣金额 即此时的restMoney全部抵扣 结束
                        restMoney = BigDecimalUtils.bigDecimal("0");
                        refundMoney = BigDecimalUtils.add(refundMoney, restMoney);
                    }
                } else {
                    //赠送卡 因为门店参数原因 会存在先使用实充再使用赠送 或者等比例使用的使用方式 所以此时的cashMoney使用赠送金额
                    if (BigDecimalUtils.compareTo(restMoney, cashMoney) >= 0) {
                        restMoney = BigDecimalUtils.subtract(restMoney, cashMoney);
                        refundMoney = BigDecimalUtils.add(refundMoney, restMoney);
                    }
                }
            }


        if (BigDecimalUtils.compareTo(restMoney, refundRechargeRequest.getCardRechargeMoney()) == 1 || pageInfo.getList().isEmpty()){
            date = DateUtils.diffMonth(date, -1);
            refundMoney = this.getRefundRechargeMoney(date, refundMoney, refundRechargeRequest, restMoney);
        }
        return refundMoney;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addRefundRecharge(RefundRechargeRequest refundRechargeRequest, RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        RefundRecharge refundRecharge = new RefundRecharge();

        //参数赋值
        BeanUtils.copyProperties(refundRechargeRequest, refundRecharge);
        refundRecharge.setRefundNum(refundOrderList.getRefundNum());
        refundRecharge.setRefundDate(refundOrderList.getRefundDate());
        refundRecharge.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        //设置业务人员
        List<StaffRequest> staffRequests = refundRechargeRequest.getStaffLists();
        BigDecimal reduce3 = staffRequests.stream().map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
        if(null != reduce3 && reduce3.compareTo(BigDecimal.valueOf(100))!=0 && reduce3.compareTo(BigDecimal.valueOf(0))!=0){
            throw new SmsException(ResponseCode.MSG,"业务员业绩占比和不等于100");
        }
        this.setStaffValue(staffRequests, refundRecharge);

        refundRecharge.setRefundAmount(refundRechargeRequest.getRecRefundMoney());
        refundRecharge.setRefundGiftAmount(refundRechargeRequest.getRefundGiftAmount());

        //取总店的门店参数设置的退款方式
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(refundRechargeRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        //总店Id
        Long collect = pagesorganizations.stream().filter(baseStores -> null == baseStores.getParentId()).map(BaseStores::getId).collect(Collectors.toList()).get(0);
        //查询最低价控制门店参数
        StoreParament storeParament = StoreParaUtils.getStorePara(collect, Constant.refundRechargeType);
        refundRecharge.setRefundType(Integer.valueOf(storeParament.getParamentValue()));
        //计算业绩
        List<RechargeAccountGive> rechargeAccountGiveList = refundRechargeRequest.getRechargeAccountGiveList();
        BigDecimal gifMoney = new BigDecimal("0");
        if(rechargeAccountGiveList!=null && rechargeAccountGiveList.size()>0){
            for (RechargeAccountGive rechargeAccountGive : rechargeAccountGiveList) {

                RechargeAccountGive accountGive = new RechargeAccountGive();
                //添加一个赠送金额扣款记录
                accountGive.setAccountId(rechargeAccountGive.getAccountId());
                accountGive.setStoreId(rechargeAccountGive.getStoreId());
                accountGive.setMemberId(rechargeAccountGive.getMemberId());
                accountGive = ssmRechargeAccountGiveService.selectOne(accountGive);
                if(accountGive!=null){
                if(rechargeAccountGive.getRefundGiftMoney()==null){
                    rechargeAccountGive.setRefundGiftMoney(BigDecimal.valueOf(0));
                }
                if(accountGive.getGiftConsumeMoney()==null){
                    accountGive.setGiftConsumeMoney(BigDecimal.valueOf(0));
                }
                if(rechargeAccountGive.getRefundGiftMoney().compareTo(accountGive.getGiftConsumeMoney())>0){
                    throw new SmsException(ResponseCode.MSG,"该门店可用赠送金额仅剩"+accountGive.getGiftConsumeMoney());
                }

                //添加退款单据
                RefundRecharge recharge = new RefundRecharge();
                BeanUtils.copyProperties(refundRecharge,recharge);
                recharge.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                recharge.setRecRefundMoney(BigDecimal.valueOf(0));
                recharge.setStoreId(accountGive.getStoreId());
                recharge.setStoreName(accountGive.getStoreName());
                recharge.setRefundGiftMoney(rechargeAccountGive.getRefundGiftMoney());
                recharge.setGiftConsumeMoney(accountGive.getGiftConsumeMoney());
                dao.saveNotNull(recharge);
                }
            }

        }
        this.setAchiComm(refundRecharge);
        if(null == refundRecharge.getRefundGiftMoney()){
            refundRecharge.setRefundGiftMoney(BigDecimal.valueOf(0));
//            refundRecharge.setRefundGiftMoney(gifMoney);
        }
        try {
            i = dao.saveNotNull(refundRecharge);

            if(null != refundRechargeRequest.getSalesmens()){
                //高管业绩
                List<Salesman> salesmens = refundRechargeRequest.getSalesmens();
                BigDecimal reduce5 = salesmens.stream().filter(salesman1 -> salesman1.getStaffType()==0).map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce6 = salesmens.stream().filter(salesman1 -> salesman1.getStaffType()==1).map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
                if(null != reduce5 && reduce5.compareTo(BigDecimal.valueOf(100))!=0 && reduce5.compareTo(BigDecimal.valueOf(0))!=0){
                    throw new SmsException(ResponseCode.MSG,"高管业绩占比和不等于100");
                }
                if(null != reduce6 && reduce6.compareTo(BigDecimal.valueOf(100))!=0 && reduce6.compareTo(BigDecimal.valueOf(0))!=0){
                    throw new SmsException(ResponseCode.MSG,"管理员业绩占比和不等于100");
                }
                for (Salesman salesmen : salesmens) {
                    salesmen.setOrderId(String.valueOf(refundRecharge.getId()));
                    salesmen.setOrderNum(refundRecharge.getRefundNum());
                    salesmen.setOrderType(4);
                    salesmen.setOrderDate(new Date());
                    salesmen.setState(1);
                    salesmen.setMoney(BigDecimalUtils.subtract(BigDecimal.valueOf(0.00),refundOrderList.getRefundMoney()));
                }
                ssmSalesmanService.addBatch(salesmens);
            }
        } catch (Exception e) {
            log.error("SsmRefundRechargeServiceImpl.addRefundRecharge:", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int NewaddRefundRecharge(RefundRechargeRequest refundRechargeRequest, RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        RefundRecharge refundRecharge = new RefundRecharge();
        //参数赋值
        BeanUtils.copyProperties(refundRechargeRequest, refundRecharge);
        refundRecharge.setRefundNum(refundOrderList.getRefundNum());
        refundRecharge.setRefundDate(refundOrderList.getRefundDate());
        refundRecharge.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        //设置业务人员
        List<StaffRequest> staffRequests = refundRechargeRequest.getStaffLists();
        BigDecimal reduce3 = staffRequests.stream().map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
        if(null != reduce3 && reduce3.compareTo(BigDecimal.valueOf(100))!=0 && reduce3.compareTo(BigDecimal.valueOf(0))!=0){
            throw new SmsException(ResponseCode.MSG,"业务员业绩占比和不等于100");
        }
        this.setStaffValue(staffRequests, refundRecharge);

        if(null != refundRechargeRequest.getSalesmens()){
            //高管业绩
            List<Salesman> salesmens = refundRechargeRequest.getSalesmens();
            BigDecimal reduce5 = salesmens.stream().filter(salesman1 -> salesman1.getStaffType()==0).map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal reduce6 = salesmens.stream().filter(salesman1 -> salesman1.getStaffType()==1).map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
            if(null != reduce5 && reduce5.compareTo(BigDecimal.valueOf(100))!=0 && reduce5.compareTo(BigDecimal.valueOf(0))!=0){
                throw new SmsException(ResponseCode.MSG,"高管业绩占比和不等于100");
            }
            if(null != reduce6 && reduce6.compareTo(BigDecimal.valueOf(100))!=0 && reduce6.compareTo(BigDecimal.valueOf(0))!=0){
                throw new SmsException(ResponseCode.MSG,"管理员业绩占比和不等于100");
            }
            for (Salesman salesmen : salesmens) {
                salesmen.setOrderId(String.valueOf(refundRecharge.getId()));
                salesmen.setOrderNum(refundRecharge.getRefundNum());
                salesmen.setOrderType(4);
                salesmen.setOrderDate(new Date());
                salesmen.setMoney(BigDecimalUtils.subtract(BigDecimal.valueOf(0.00),refundOrderList.getCouseRefundMoney()));
                salesmen.setProjectId(refundRechargeRequest.getRechargeTypeId());
            }
            ssmSalesmanService.addBatch(salesmens);
        }


        //计算业绩
        this.setAchiComm(refundRecharge);
        try {
            i = dao.saveNotNull(refundRecharge);
        } catch (Exception e) {
            log.error("SsmRefundRechargeServiceImpl.addRefundRecharge:", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updateRefundRecharge(RefundRechargeRequest refundRechargeRequest, RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        RefundRecharge refundRecharge = new RefundRecharge();
        //参数赋值
        BeanUtils.copyProperties(refundRechargeRequest, refundRecharge);
        refundRecharge.setRefundNum(refundOrderList.getRefundNum());
        refundRecharge.setRefundDate(refundOrderList.getRefundDate());
        //设置业务人员
        List<StaffRequest> staffRequests = refundRechargeRequest.getStaffLists();
        BigDecimal reduce3 = staffRequests.stream().map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
        if(null != reduce3 && reduce3.compareTo(BigDecimal.valueOf(100))!=0 && reduce3.compareTo(BigDecimal.valueOf(0))!=0){
            throw new SmsException(ResponseCode.MSG,"业务员业绩占比和不等于100");
        }

        this.setStaffValue(staffRequests, refundRecharge);
        List<RefundRecharge> refundRechargeList = refundRechargeRequest.getRefundRechargeList();
        if(refundRechargeList!=null&&refundRechargeList.size()>0){
            for (RefundRecharge recharge : refundRechargeList) {
                dao.updateByPrimaryKey(recharge);
            }
        }
        //计算业绩
        this.setAchiComm(refundRecharge);
        try {
            refundRecharge.setStoreId(null);
            refundRecharge.setStoreName(null);
            i = dao.updateByPrimaryKeySelective(refundRecharge);
            Salesman salesman = new Salesman();
            salesman.setOrderId(String.valueOf(refundRecharge.getId()));
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            salesman.setOrderNum(refundRecharge.getRefundNum());
            salesman.setOrderId(null);
            List<Salesman> salesmanList = ssmSalesmanService.selectList(salesman);
            if(salesmanList!=null && salesmanList.size()>0){

                salesmen.addAll(salesmanList);
            }
            for (Salesman salesman1 : salesmen) {
                ssmSalesmanService.delete(salesman1);
            }
            BaseStore baseStore = new BaseStore();
            baseStore.setId(refundOrderList.getStoreId());
            BaseStore baseStore1 = ssmBaseStoreService.selectOne(baseStore);
            if(null != refundRechargeRequest.getSalesmens()){
                List<Salesman> salesmens = refundRechargeRequest.getSalesmens();
                BigDecimal reduce5 = salesmens.stream().filter(salesman1 -> salesman1.getStaffType()==0).map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce6 = salesmens.stream().filter(salesman1 -> salesman1.getStaffType()==1).map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
                if(null != reduce5 && reduce5.compareTo(BigDecimal.valueOf(100))!=0 && reduce5.compareTo(BigDecimal.valueOf(0))!=0){
                    throw new SmsException(ResponseCode.MSG,"高管业绩占比和不等于100");
                }
                if(null != reduce6 && reduce6.compareTo(BigDecimal.valueOf(100))!=0 && reduce6.compareTo(BigDecimal.valueOf(0))!=0){
                    throw new SmsException(ResponseCode.MSG,"管理员业绩占比和不等于100");
                }
                for (Salesman salesmen2 : salesmens) {
                    Random r = new Random(3);
                    int i1 = r.nextInt();
                    salesmen2.setId(salesmen2.getId()+i1);
                    salesmen2.setOrderId(String.valueOf(refundRecharge.getId()));
                    salesmen2.setOrderNum(refundRecharge.getRefundNum());
                    salesmen2.setOrderType(4);
                    salesmen2.setOrderDate(new Date());
                    salesmen2.setState(1);
                    salesmen2.setMoney(BigDecimalUtils.subtract(BigDecimal.valueOf(0.00),refundOrderList.getRefundMoney()));
                    salesmen2.setStoreNum(baseStore1.getStoreNum());
                    salesmen2.setStoreName(baseStore1.getStoreName());
                    salesmen2.setStoreIdOrder(baseStore1.getId());
                }
                ssmSalesmanService.addBatch(salesmens);
            }
        } catch (Exception e) {
            log.error("SsmRefundRechargeServiceImpl.updateRefundRecharge:", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int audit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        //查询充值退款单
        RefundRecharge refundRecharge = new RefundRecharge();
        //refundRecharge.setStoreId(refundOrderList.getStoreId());
        refundRecharge.setRefundNum(refundOrderList.getRefundNum());
        refundRecharge.setMemberId(refundOrderList.getMemberId());
//        refundRecharge.setStoreId(refundOrderList.getStoreId());
        List<RefundRecharge> select = dao.select(refundRecharge);
//        RefundRecharge recharge = select.get(0);
        for (RefundRecharge recharge : select) {
            BeanUtils.copyProperties(recharge,refundRecharge);
            //解锁账户
            ssmRechargeAccountService.updateState(refundRecharge.getRecAccountId(), 1);
            //账户扣减金额
            RechargeAccount rechargeAccount = ssmRechargeAccountService.subMoney(refundRecharge);

            //判断是否全额退款
            if (BigDecimalUtils.compareTo(rechargeAccount.getCardRestMoney(), BigDecimal.valueOf(0)) == 0) {
                //将账户彻底锁定 如不在反审核 那么改会员再次购买充值卡时会生成新的充值账户
                ssmRechargeAccountService.updateState(refundRecharge.getRecAccountId(), 0);
                //取消会员欠款
                DebtList debtList = new DebtList();
                debtList.setAccountId(refundRecharge.getRecAccountId());
                //debtList.setStoreId(refundOrderList.getStoreId());
                debtList.setDocNum(refundRecharge.getRechargeNum());
                debtList.setMemberId(refundOrderList.getMemberId());
                List<DebtList> debtLists = ssmDebtService.select(debtList);
                for (DebtList debt : debtLists) {
                    //有欠款时 添加退款欠款记录 更新欠款信息
                    if (debt.getState() == 0) {
                        RefundDebtList refundDebtList = new RefundDebtList();
                        BeanUtils.copyProperties(debt, refundDebtList);
                        refundDebtList.setRefundTradeId(refundRecharge.getId());
                        refundDebtList.setDebtId(debt.getId());
                        refundDebtList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                        ssmRefundDebtService.saveNotNull(refundDebtList);
                        debt.setState(1);
                        debt.setDebtMoney(BigDecimalUtils.bigDecimal("0"));
                        ssmDebtService.updateNotNull(debt);
                    }
                }
                //插入账户交易记录
                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeAccount, refundRecharge, refundOrderList);
                i = ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                //设置业务员
                List<AchiCommEntity> entities = this.getAchiCommList(refundRecharge);
                //将充值退款单据信息添加到评价表
                MemberCsRequest memberCsRequest = new MemberCsRequest();
                BeanUtils.copyProperties(refundRecharge, memberCsRequest);
                memberCsRequest.setDocId(refundOrderList.getId());
                memberCsRequest.setDocNum(refundOrderList.getRefundNum());
                memberCsRequest.setDocType(Constant.doc_type_refund_recharge);
                memberCsRequest.setDocDate(refundOrderList.getRefundDate());
                memberCsRequest.setTradeId(refundRecharge.getRechargeTypeId());
                memberCsRequest.setTradeName(refundRecharge.getRechargeTypeName());
                memberCsRequest.setTradeMoney(refundRecharge.getCardRechargeMoney());
                StringBuffer staffIds = new StringBuffer();
                StringBuffer staffNames = new StringBuffer();
                if (entities.size() > 0) {
                    for (AchiCommEntity achiCommEntity : entities) {
                        staffIds = staffIds.append(achiCommEntity.getSaleStaffId());
                        staffNames = staffNames.append(achiCommEntity.getSaleStaffName());
                    }
                }
                ssmMemberCsService.addMemberCs(memberCsRequest);


            }
            else if (BigDecimalUtils.compareTo(rechargeAccount.getCardRestMoney(), BigDecimal.valueOf(0)) == 1) {
                //插入账户交易记录
                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeAccount, refundRecharge, refundOrderList);
                i = ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                //设置业务员
                List<AchiCommEntity> entities = this.getAchiCommList(refundRecharge);
                //将充值退款单据信息添加到评价表
                MemberCsRequest memberCsRequest = new MemberCsRequest();
                BeanUtils.copyProperties(refundRecharge, memberCsRequest);
                memberCsRequest.setDocId(refundOrderList.getId());
                memberCsRequest.setDocNum(refundOrderList.getRefundNum());
                memberCsRequest.setDocType(Constant.doc_type_refund_recharge);
                memberCsRequest.setDocDate(refundOrderList.getRefundDate());
                memberCsRequest.setTradeId(refundRecharge.getRechargeTypeId());
                memberCsRequest.setTradeName(refundRecharge.getRechargeTypeName());
                memberCsRequest.setTradeMoney(refundRecharge.getCardRechargeMoney());
                StringBuffer staffIds = new StringBuffer();
                StringBuffer staffNames = new StringBuffer();
                if (entities.size() > 0) {
                    for (AchiCommEntity achiCommEntity : entities) {
                        staffIds = staffIds.append(achiCommEntity.getSaleStaffId());
                        staffNames = staffNames.append(achiCommEntity.getSaleStaffName());
                    }
                }
                ssmMemberCsService.addMemberCs(memberCsRequest);
            }
            else if (BigDecimalUtils.compareTo(rechargeAccount.getCardRestMoney(), BigDecimal.valueOf(-2)) == 0) {
                i = 9;
            }


        }
        return i;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int unAudit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        //查询充值退款单
        RefundRecharge refundRecharge = new RefundRecharge();
        //refundRecharge.setStoreId(refundOrderList.getStoreId());
        refundRecharge.setRefundNum(refundOrderList.getRefundNum());
        List<RefundRecharge> select = dao.select(refundRecharge);
        for (RefundRecharge recharge : select) {
            BeanUtils.copyProperties(recharge, refundRecharge);

            //账户添加金额
            RechargeAccount rechargeAccount = ssmRechargeAccountService.addMoney(refundRecharge);
            //解锁账户
            ssmRechargeAccountService.updateState(refundRecharge.getRecAccountId(), 1);
            //查询退款欠款记录 更新欠款表信息
            RefundDebtList refundDebtList = new RefundDebtList();
            refundDebtList.setRefundTradeId(refundRecharge.getId());
            List<RefundDebtList> debtLists = ssmRefundDebtService.select(refundDebtList);
            for (RefundDebtList refundDebt : debtLists) {
                DebtList debtList = new DebtList();
                debtList.setId(refundDebt.getDebtId());
                DebtList debtList1 = ssmDebtService.selectByPrimaryKey(debtList.getId());
                debtList1.setDebtMoney(refundDebt.getDebtMoney());
                debtList1.setState(0);
                ssmDebtService.updateNotNull(debtList1);
            }
            //添加账户交易记录
            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeAccount, refundRecharge, refundOrderList);
            //设置审核人人信息
            accountTradeRecord.setInputBy(refundOrderList.getAuditBy());
            i = ssmAccountTradeRecordService.addRecord(accountTradeRecord);
        }
        //将评价单进行删除操作
        MemberCs memberCs = new MemberCs();
        memberCs.setDocId(refundOrderList.getId());
        ssmMemberCsService.delete(memberCs);
        return i;
    }

    public AccountTradeRecord setAccountTradeRecord(RechargeAccount rechargeAccount, RefundRecharge refundRecharge, RefundOrderList refundOrderList){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(refundOrderList.getStoreId());
        accountTradeRecord.setStoreName(refundOrderList.getStoreName());
        accountTradeRecord.setMemberId(refundOrderList.getMemberId());
        accountTradeRecord.setMemberName(refundOrderList.getMemberName());
        accountTradeRecord.setMemberIdentity(refundOrderList.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(refundOrderList.getMobilePhone());
        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        if (refundOrderList.getState() == 3){//审核
            //金额记录为退款金额
            accountTradeRecord.setAccountSpendMoney(refundRecharge.getRecRefundMoney());
//            //赠送卡花费金额保持为原账户赠送金额
//            accountTradeRecord.setAccountGiftSpendMoney(refundRecharge.getCardGiftMoney());
            accountTradeRecord.setAccountType(Constant.account_type_refund_recharge_audit);
            accountTradeRecord.setInputDate(refundOrderList.getAuditDate());
            accountTradeRecord.setOrderStatu(0);
            accountTradeRecord.setInputBy(refundOrderList.getAuditBy());
        }else if (refundOrderList.getState() == 4){//反审核
            //金额记录为退款金额
            accountTradeRecord.setAccountInMoney(refundRecharge.getRecRefundMoney());
//            //赠送卡花费金额
//            accountTradeRecord.setAccountGiftSpendMoney(refundRecharge.getCardGiftMoney());
            accountTradeRecord.setAccountType(Constant.account_type_refund_recharge_unaudit);
            accountTradeRecord.setInputDate(new Date());
            accountTradeRecord.setOrderStatu(2);
            accountTradeRecord.setInputBy(refundOrderList.getAuditBy());
        }
        accountTradeRecord.setDocId(refundOrderList.getId());
        accountTradeRecord.setDocNum(refundOrderList.getRefundNum());
        accountTradeRecord.setDocType(Constant.doc_type_refund_recharge);
        accountTradeRecord.setTradeId(refundRecharge.getRechargeTypeId());
        accountTradeRecord.setTradeName(refundRecharge.getRechargeTypeName());
        accountTradeRecord.setTradeMoney(refundRecharge.getRecRefundMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        accountTradeRecord.setPrice(refundRecharge.getRecRefundMoney());
        if(refundRecharge.getRecRefundMoney().compareTo(BigDecimal.valueOf(0))==1){
            accountTradeRecord.setCardMoney(refundRecharge.getRecRefundMoney());
        }else {
            accountTradeRecord.setCardMoney(refundRecharge.getRefundGiftMoney());
        }
        accountTradeRecord.setCashMoney(BigDecimal.valueOf(0));
        return accountTradeRecord;
    }


    @Override
    public List<RefundRechargeResponse> selectList(Long storeId, String date) {
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        refundOrderResquest.setDateStr(date);
        refundOrderResquest.setState(3);
        List<RefundRecharge> rechargeList =  dao.selectList(refundOrderResquest);
        List<RefundRechargeResponse> list = new ArrayList<>();
        //遍历充值退款单 设置业务员
        for (RefundRecharge refundRecharge : rechargeList) {
            refundRecharge.setRefundDate(refundRecharge.getAuditDate());
            RefundRechargeResponse refundRechargeResponse = new RefundRechargeResponse();
            BeanUtils.copyProperties(refundRecharge, refundRechargeResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(refundRecharge);
            for (AchiCommEntity entity : entities) {
                entity.setDocId(refundRecharge.getId());
                /*entity.setCardComm(BigDecimal.valueOf(0).subtract(entity.getCardComm()));
                entity.setCashComm(BigDecimal.valueOf(0).subtract(entity.getCashComm()));
                entity.setCashAchi(BigDecimal.valueOf(0).subtract(entity.getCashAchi()));
                entity.setCardAchi(BigDecimal.valueOf(0).subtract(entity.getCardAchi()));
                entity.setTotalCommission(BigDecimal.valueOf(0).subtract(entity.getTotalCommission()));
                entity.setTotalPerformance(BigDecimal.valueOf(0).subtract(entity.getTotalPerformance()));*/
                RechargeType rechargeType = new RechargeType();
                rechargeType.setId(refundRecharge.getRechargeTypeId());
                RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                entity.setProjectId(rechargeType1.getRechargeTypeNum());
            }
            //设置返回List
            refundRechargeResponse.setEntities(entities);
            list.add(refundRechargeResponse);
        }
        return list;
    }

    @Override
    public List<RefundRechargeResponse> selectListDept(Long storeId, String date) {
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        refundOrderResquest.setDateStr(date);
        refundOrderResquest.setState(3);
        List<RefundRecharge> rechargeList =  dao.selectList(refundOrderResquest);
        List<RefundRechargeResponse> list = new ArrayList<>();
        //遍历充值退款单 设置业务员
        for (RefundRecharge refundRecharge : rechargeList) {
            RefundRechargeResponse refundRechargeResponse = new RefundRechargeResponse();
            BeanUtils.copyProperties(refundRecharge, refundRechargeResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(refundRecharge);
            for (AchiCommEntity entity : entities) {
                entity.setDocId(refundRecharge.getId());
                RechargeType rechargeType = new RechargeType();
                rechargeType.setId(refundRecharge.getRechargeTypeId());
                RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                entity.setProjectId(rechargeType1.getRechargeTypeNum());
            }
            //设置返回List
            refundRechargeResponse.setEntities(entities);
            list.add(refundRechargeResponse);
        }
        return list;
    }

    @Override
    public List<RefundRechargeResponse> selectListchia(Long storeId,  Date startDate, Date endDate) {
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        refundOrderResquest.setStartTime(startDate);
        refundOrderResquest.setEndTime(endDate);
        List<RefundRecharge> rechargeList =  dao.selectListschia(refundOrderResquest);
        List<RefundRechargeResponse> list = new ArrayList<>();
        //遍历充值退款单 设置业务员
        for (RefundRecharge refundRecharge : rechargeList) {
            RefundRechargeResponse refundRechargeResponse = new RefundRechargeResponse();
            BeanUtils.copyProperties(refundRecharge, refundRechargeResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(refundRecharge);
            for (AchiCommEntity entity : entities) {
                entity.setCardComm(BigDecimal.valueOf(0).subtract(entity.getCardComm()));
                entity.setCashComm(BigDecimal.valueOf(0).subtract(entity.getCashComm()));
                entity.setCashAchi(BigDecimal.valueOf(0).subtract(entity.getCashAchi()));
                entity.setCardAchi(BigDecimal.valueOf(0).subtract(entity.getCardAchi()));
                entity.setTotalCommission(BigDecimal.valueOf(0).subtract(entity.getTotalCommission()));
                entity.setTotalPerformance(BigDecimal.valueOf(0).subtract(entity.getTotalPerformance()));
                RechargeType rechargeType = new RechargeType();
                rechargeType.setId(refundRecharge.getRechargeTypeId());
                RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                entity.setProjectId(rechargeType1.getRechargeTypeNum());
            }
            //设置返回List
            refundRechargeResponse.setEntities(entities);
            list.add(refundRechargeResponse);
        }
        return list;
    }

    @Override
    public List<RefundRechargeResponse> selectLists(Long storeId, String startDate , String endDate) {
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        try {
            refundOrderResquest.setStartTime(DateUtils.YMDParse(startDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            refundOrderResquest.setEndTime(DateUtils.YMDParse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        refundOrderResquest.setState(3);
        List<RefundRecharge> rechargeList =  dao.selectLists(refundOrderResquest);
        List<RefundRechargeResponse> list = new ArrayList<>();
        //遍历充值退款单 设置业务员
        for (RefundRecharge refundRecharge : rechargeList) {
            RefundRechargeResponse refundRechargeResponse = new RefundRechargeResponse();
            BeanUtils.copyProperties(refundRecharge, refundRechargeResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(refundRecharge);
            //设置返回List
            refundRechargeResponse.setEntities(entities);
            list.add(refundRechargeResponse);
        }
        return list;
    }

    /*
     * 业务员赋值
     * */
    public void setStaffValue(List<StaffRequest> staffLists, RefundRecharge refundRecharge) {

        for (int i = 0; i < staffLists.size(); i++) {
            StaffRequest staffRequest = staffLists.get(i);
            if (i == 0) {
                refundRecharge.setSaleOneStaffId(staffRequest.getId());
                refundRecharge.setSaleOneStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleOneIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleOneRatio(staffRequest.getRatio());
                refundRecharge.setSaleOneNumber(staffRequest.getBrandNumber());
            } else if (i == 1) {
                refundRecharge.setSaleTwoStaffId(staffRequest.getId());
                refundRecharge.setSaleTwoStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleTwoIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleTwoRatio(staffRequest.getRatio());
                refundRecharge.setSaleTwoNumber(staffRequest.getBrandNumber());
            } else if (i == 2) {
                refundRecharge.setSaleThreeStaffId(staffRequest.getId());
                refundRecharge.setSaleThreeStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleThreeIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleThreeRatio(staffRequest.getRatio());
                refundRecharge.setSaleThreeNumber(staffRequest.getBrandNumber());
            } else if (i == 3){
                refundRecharge.setSaleFourStaffId(staffRequest.getId());
                refundRecharge.setSaleFourStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleFourIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleFourRatio(staffRequest.getRatio());
                refundRecharge.setSaleFourNumber(staffRequest.getBrandNumber());
            }else if (i == 4){
                refundRecharge.setSaleFiveStaffId(staffRequest.getId());
                refundRecharge.setSaleFiveStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleFiveIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleFiveRatio(staffRequest.getRatio());
                refundRecharge.setSaleFiveNumber(staffRequest.getBrandNumber());
            }
            else if (i == 5){
                refundRecharge.setSaleSixStaffId(staffRequest.getId());
                refundRecharge.setSaleSixStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleSixIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleSixRatio(staffRequest.getRatio());
                refundRecharge.setSaleSixNumber(staffRequest.getBrandNumber());
            }
            else if (i == 6){
                refundRecharge.setSaleSevenStaffId(staffRequest.getId());
                refundRecharge.setSaleSevenStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleSevenIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleSevenRatio(staffRequest.getRatio());
                refundRecharge.setSaleSevenNumber(staffRequest.getBrandNumber());
            }
            else if (i == 7){
                refundRecharge.setSaleEightStaffId(staffRequest.getId());
                refundRecharge.setSaleEightStaffName(staffRequest.getStaffName());
                refundRecharge.setSaleEightIdentity(staffRequest.getIdentityCard());
                refundRecharge.setSaleEightRatio(staffRequest.getRatio());
                refundRecharge.setSaleEightNumber(staffRequest.getBrandNumber());
            }
        }
    }

    public RefundRecharge setAchiComm(RefundRecharge refundRecharge) {
        RechargeType rechargeType = ssmRechargeTypeSevice.selectByPrimaryKey(refundRecharge.getRechargeTypeId());
        refundRecharge.setTypeRatio(rechargeType.getTypeRatio());
        //提成标准
        BigDecimal commRatio = rechargeType.getTypeRatio();
        //业绩
        refundRecharge.setAchievementAmount(BigDecimalUtils.add(refundRecharge.getRecRefundMoney(),refundRecharge.getRecCompensation()));
        //提成
        if (BigDecimalUtils.compareTo(commRatio, BigDecimalUtils.bigDecimal("1")) != 1) {

            refundRecharge.setCommissionAmount(BigDecimalUtils.multiply(commRatio, BigDecimalUtils.add(refundRecharge.getRecRefundMoney(),refundRecharge.getRecCompensation())));

        } else {
            refundRecharge.setCommissionAmount(BigDecimalUtils.multiply(BigDecimalUtils.div(refundRecharge.getAchievementAmount(), rechargeType.getRechargeMoney()), commRatio));
        }
        return refundRecharge;
    }
}
