package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.core.utils.StoreParaUtils;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.RepayListMapper;
import com.ssm.manage.mapper.RepayTradeListMapper;
import com.ssm.manage.query.*;
import com.ssm.manage.response.RepayListResponse;
import com.ssm.manage.response.RepayTradeResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.*;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.SsmRepayDao;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import com.ssm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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 tk.mybatis.mapper.entity.Example;

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

@Service
@Slf4j
public class SsmRepayServiceImpl extends BaseServiceImpl<RepayList> implements SsmRepayService {
    @Autowired
    private SsmRepayTradeService ssmRepayTradeService;

    @Autowired
    private SsmDocPromotionProjectService ssmDocPromotionProjectService;

    @Autowired
    private SsmPayService ssmPayService;

    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;

    @Autowired
    private SsmRechargeTypeSevice ssmRechargeTypeSevice;
    @Autowired
    private SsmSalesmanService ssmSalesmanService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;
    @Autowired
    private SsmRepayAccountListService ssmRepayAccountListService;

    @Autowired
    private SsmRepayDao dao;

    @Autowired
    RedisClient<StaffResponse> redisClient;

    @Autowired
    private SsmStoreParaService ssmStoreParaService;
    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;
    @Autowired
    private SsmDebtService ssmDebtService;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;
    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;

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

    @Override
    public PageInfo<RepayListResponse> selectPage(RepayListRequest repayListRequest) {
        PageInfo<RepayList> pageInfo = dao.selectPage(repayListRequest);
       List<RepayListResponse> list = new ArrayList<>();
        for (RepayList repayList : pageInfo.getList()){
            RepayListResponse repayListResponse = new RepayListResponse();
            BeanUtils.copyProperties(repayList,repayListResponse);
            RepayTradeList repayTradeList = new RepayTradeList();
            //查询还款明细
            repayTradeList.setRepayNum(repayList.getRepayNum());
            repayTradeList.setStoreId(repayListRequest.getStoreId());
            List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectList(repayTradeList);
            //修改还款明细返回格式
            List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
            for (RepayTradeList tradeList : repayTradeLists) {
                RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
                BeanUtils.copyProperties(tradeList, repayTradeResponse);
                //设置业务人员列表
                List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                repayTradeResponse.setEntities(entities);
                tradeResponseList.add(repayTradeResponse);

                RepayAccountList repayAccountList = new RepayAccountList();
                repayAccountList.setRepayTradeId(tradeList.getId());
                List<RepayAccountList> select = ssmRepayAccountListService.select(repayAccountList);
                repayTradeResponse.setRepayAccountListRequests(select);
            }
            //查询支付方式
            PayList payList = new PayList();
            payList.setDocId(String.valueOf(repayList.getId()));
            List<PayList> payLists = ssmPayService.select(payList);

            repayListResponse.setRepayTradeLists(tradeResponseList);
            repayListResponse.setPayLists(payLists);

            list.add(repayListResponse);
        }
        PageInfo<RepayListResponse> p = new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<RepayListResponse> selectPageNew(RepayListRequest repayListRequest) {
        List<Long> lists = new ArrayList<>();
        if(repayListRequest.getSelectType()==0){
            lists=repayListRequest.getStoreList();
        }else{
            BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(repayListRequest.getStoreId());
            if(null == baseStore1.getParentId()&&(repayListRequest.getStoreList().size()<0||repayListRequest.getStoreList()==null)){
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(repayListRequest.getStoreId());
                List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
                List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
                lists=collect;
            }else{
                lists.add(repayListRequest.getStoreId());
            }
        }
        repayListRequest.setStoreList(lists);
        PageInfo<RepayList> pageInfo = dao.selectPageNew(repayListRequest);
       List<RepayListResponse> list = new ArrayList<>();
        for (RepayList repayList : pageInfo.getList()){
            RepayListResponse repayListResponse = new RepayListResponse();
            BeanUtils.copyProperties(repayList,repayListResponse);
            RepayTradeList repayTradeList = new RepayTradeList();
            //查询还款明细
            repayTradeList.setRepayNum(repayList.getRepayNum());
            repayTradeList.setStoreId(repayList.getStoreId());
            List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectList(repayTradeList);
            //修改还款明细返回格式
            List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
            for (RepayTradeList tradeList : repayTradeLists) {
                RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
                BeanUtils.copyProperties(tradeList, repayTradeResponse);
                //设置业务人员列表
                List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                repayTradeResponse.setEntities(entities);
                tradeResponseList.add(repayTradeResponse);

                RepayAccountList repayAccountList = new RepayAccountList();
                repayAccountList.setRepayTradeId(tradeList.getId());
                List<RepayAccountList> select = ssmRepayAccountListService.select(repayAccountList);
                repayTradeResponse.setRepayAccountListRequests(select);
            }
            //查询支付方式
            PayList payList = new PayList();
            payList.setDocId(repayList.getRepayNum());
            List<PayList> payLists = ssmPayService.select(payList);

            repayListResponse.setRepayTradeLists(tradeResponseList);
            repayListResponse.setPayLists(payLists);

            list.add(repayListResponse);
        }
        PageInfo<RepayListResponse> p = new PageInfo<>(list);
        p.setTotal(pageInfo.getTotal());
        return p;
    }

    @Override
    public PageInfo<RepayListResponse> pageDetail(RepayListRequest repayListRequest) {
        PageInfo<RepayList> pageInfo = dao.pageDetail(repayListRequest);
        List<RepayList> list1 = pageInfo.getList();
        List<RepayListResponse> list = new ArrayList<>();
        if(null == list1 || 0==list1.size()){
            RepayTradeList repayTradeList = new RepayTradeList();
            repayTradeList.setId(repayListRequest.getId());
            RepayTradeList repayTradeList1 = ssmRepayTradeService.selectOne(repayTradeList);
            if(null !=repayTradeList1){
                RepayList repayList = new RepayList();
                repayList.setRepayNum(repayTradeList1.getRepayNum());
                repayList.setStoreId(repayTradeList1.getStoreId());
                List<RepayList> select = dao.select(repayList);
                for (RepayList repayLists : select){
                    RepayListResponse repayListResponse = new RepayListResponse();
                    BeanUtils.copyProperties(repayLists,repayListResponse);
                    RepayTradeList repayTradeLists = new RepayTradeList();
                    //查询还款明细
                    repayTradeLists.setRepayNum(repayLists.getRepayNum());
                    repayTradeLists.setStoreId(repayListRequest.getStoreId());
                    List<RepayTradeList> repayTradeListss = ssmRepayTradeService.selectList(repayTradeLists);
                    //修改还款明细返回格式
                    List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
                    for (RepayTradeList tradeList : repayTradeListss) {
                        RepayTradeResponse repayTradeResponse = new RepayTradeResponse();

                        RepayAccountList repayAccountList = new RepayAccountList();
                        repayAccountList.setRepayTradeId(tradeList.getId());
                        List<RepayAccountList> selects = ssmRepayAccountListService.select(repayAccountList);
                        repayTradeResponse.setRepayAccountListRequests(selects);


                        BeanUtils.copyProperties(tradeList, repayTradeResponse);
                        //设置业务人员列表
                        List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                        repayTradeResponse.setEntities(entities);


                        tradeResponseList.add(repayTradeResponse);


                    }
                    //查询支付方式
                    PayList payList = new PayList();
                    payList.setDocId(String.valueOf(repayLists.getId()));
                    List<PayList> payLists = ssmPayService.select(payList);

                    repayListResponse.setRepayTradeLists(tradeResponseList);
                    repayListResponse.setPayLists(payLists);

                    list.add(repayListResponse);
                }
                PageInfo<RepayListResponse> p = new PageInfo<>(list);
                return p;
            }else{
                PageInfo<RepayListResponse> p = new PageInfo<>(list);
                return p;
            }

        }else{
            for (RepayList repayList : pageInfo.getList()){
                RepayListResponse repayListResponse = new RepayListResponse();
                BeanUtils.copyProperties(repayList,repayListResponse);
                RepayTradeList repayTradeList = new RepayTradeList();
                //查询还款明细
                repayTradeList.setRepayNum(repayList.getRepayNum());
                repayTradeList.setStoreId(repayListRequest.getStoreId());
                List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectList(repayTradeList);
                //修改还款明细返回格式
                List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
                for (RepayTradeList tradeList : repayTradeLists) {
                    RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
                    BeanUtils.copyProperties(tradeList, repayTradeResponse);
                    //设置业务人员列表
                    List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                    repayTradeResponse.setEntities(entities);

                    RepayAccountList repayAccountList = new RepayAccountList();
                    repayAccountList.setRepayTradeId(tradeList.getId());
                    List<RepayAccountList> selects = ssmRepayAccountListService.select(repayAccountList);
                    repayTradeResponse.setRepayAccountListRequests(selects);
                    tradeResponseList.add(repayTradeResponse);
                }
                //查询支付方式
                PayList payList = new PayList();
                payList.setDocId(String.valueOf(repayList.getId()));
                List<PayList> payLists = ssmPayService.select(payList);
                repayListResponse.setPayLists(payLists);
                if(payLists==null || payLists.size()==0){

                    payList.setDocId(repayList.getRepayNum());
                    List<PayList> payLists2 = ssmPayService.select(payList);
                    repayListResponse.setPayLists(payLists2);
                }
                repayListResponse.setRepayTradeLists(tradeResponseList);

                list.add(repayListResponse);
            }
            PageInfo<RepayListResponse> p = new PageInfo<>(list);
            return p;


        }

    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addRepay(RepayListRequest repayListRequest) throws SmsException {
        //还款明细
        List<RepayTradeRequest> repayTradeLists = repayListRequest.getRepayTradeLists();
        List<StaffRequest> staffRequests = repayTradeLists.get(0).getStaffRequests();
        BaseStore store = ssmBaseStoreService.selectByPrimaryKey(repayListRequest.getStoreId());
        if(store.getParentId()==null) {
            if (staffRequests != null && staffRequests.size() > 0) {
                StaffRequest staffRequest = staffRequests.get(0);
                if (staffRequest.getId() != null) {
                    BaseStaff baseStaff = ssmBaseStaffService.selectByPrimaryKey(staffRequest.getId());
                    BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(baseStaff.getStoreId());
                    if (baseStaff.getStoreId().compareTo(repayListRequest.getId()) != 0) {
                        repayListRequest.setStoreId(baseStore.getId());
                        repayListRequest.setStoreName(baseStore.getStoreName());
                        repayListRequest.setStoreNum(baseStore.getStoreNum());
                    }
                }
            }
        }
        int i = 0;
        RepayList repayList = new RepayList();
        repayList.setIsSendSms(repayListRequest.getIsSendSms());
        BeanUtils.copyProperties(repayListRequest,repayList);
        //主键ID
        Long repayId = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
        repayList.setId(repayId);
        //还款单号
        repayListRequest.setStoreNum(ssmBaseStoreService.selectByPrimaryKey(repayListRequest.getStoreId()).getStoreNum());
        String repayNum = this.getRepayNum(repayListRequest);
        repayList.setRepayNum(repayNum);

        repayList.setRepayDate(new Date());
        //不走补单 正常开单 补单时间默认为开单时间
        if (0 == repayList.getIsAddition()) {
            repayList.setAdditionDate(new Date());
        }
        repayList.setIsCancel(0);
        if (redisClient.hasKey(getKey(repayListRequest.getStaffPhone()))) {
            repayList.setInputId(redisClient.get(getKey(repayListRequest.getStaffPhone())).getId());
            repayList.setInputBy(redisClient.get(getKey(repayListRequest.getStaffPhone())).getStaffName());
        }
        Set<String> keys = redisClient.keys(getKey(repayListRequest.getStaffPhone()) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            StaffResponse staffResponse = redisClient.get(key);
            repayList.setInputId(staffResponse.getId());
            repayList.setInputBy(staffResponse.getStaffName());
        }
        //插入还款明细
        ssmRepayTradeService.add(repayTradeLists, repayList);
        //插入还款单
        try {
            i = dao.saveNotNull(repayList);
        }catch (Exception e){
            log.error("SsmRepayServiceImpl.addRepay:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        //添加现付支付方式
        List<PayList> payLists = repayListRequest.getPayLists();
        if (null != payLists && payLists.size() > 0) {
            for (PayList payList : payLists) {
                payList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                payList.setDocId(repayNum);
                payList.setType(Constant.doc_type_card_use);
                payList.setStoreId(repayListRequest.getStoreId());
                payList.setPayDate(new Date());
                payList.setIsCancel(0);
            }
            ssmPayService.addPayList(payLists);
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int addPromotionRecharge(RepayListRequest repayListRequest) throws SmsException {
        //还款明细
        List<RepayTradeRequest> repayTradeLists = repayListRequest.getRepayTradeLists();
        List<StaffRequest> staffRequests = repayTradeLists.get(0).getStaffRequests();
        if(staffRequests!=null&&staffRequests.size()>0){
            StaffRequest staffRequest = staffRequests.get(0);
            if(staffRequest.getId()!=null) {
                BaseStaff baseStaff = ssmBaseStaffService.selectByPrimaryKey(staffRequest.getId());
                BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(baseStaff.getStoreId());
                if (!(baseStaff.getStoreId() == repayListRequest.getStoreId()) && !(baseStaff.getStoreId().equals(repayListRequest.getStoreId()))) {
                    repayListRequest.setStoreId(baseStore.getId());
                    repayListRequest.setStoreName(baseStore.getStoreName());
                    repayListRequest.setStoreNum(baseStore.getStoreNum());
                } else {
                }
            }
        }
        int i = 0;
        RepayList repayList = new RepayList();
        repayList.setIsSendSms(repayListRequest.getIsSendSms());
        BeanUtils.copyProperties(repayListRequest,repayList);
        //主键ID
        Long repayId = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
        repayList.setId(repayId);
        //还款单号
        repayListRequest.setStoreNum(ssmBaseStoreService.selectByPrimaryKey(repayListRequest.getStoreId()).getStoreNum());
        String repayNum = this.getRepayNum(repayListRequest);
        repayList.setRepayNum(repayNum);

        repayList.setRepayDate(new Date());
        //不走补单 正常开单 补单时间默认为开单时间
        if (0 == repayList.getIsAddition()) {
            repayList.setAdditionDate(new Date());
        }
        repayList.setIsCancel(0);
        if (redisClient.hasKey(getKey(repayListRequest.getStaffPhone()))) {
            repayList.setInputId(redisClient.get(getKey(repayListRequest.getStaffPhone())).getId());
            repayList.setInputBy(redisClient.get(getKey(repayListRequest.getStaffPhone())).getStaffName());
        }
        Set<String> keys = redisClient.keys(getKey(repayListRequest.getStaffPhone()) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            StaffResponse staffResponse = redisClient.get(key);
            repayList.setInputId(staffResponse.getId());
            repayList.setInputBy(staffResponse.getStaffName());
        }
        //插入还款明细
        ssmRepayTradeService.addPromotionRecharge(repayTradeLists, repayList);
        //插入还款单
        try {
            i = dao.saveNotNull(repayList);
        }catch (Exception e){
            log.error("SsmRepayServiceImpl.addRepay:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        //添加现付支付方式
        List<PayList> payLists = repayListRequest.getPayLists();
        if (null != payLists && payLists.size() > 0) {
            for (PayList payList : payLists) {
                payList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                payList.setDocId(repayNum);
                payList.setType(Constant.doc_type_card_use);
                payList.setStoreId(repayListRequest.getStoreId());
                payList.setPayDate(new Date());
                payList.setIsCancel(0);
            }
            ssmPayService.addPayList(payLists);
        }
        return i;
    }

    public String getRepayNum(RepayListRequest repayListRequest){
        //查询数据库门店编码最新的一个
        String ymdDate = DateUtils.YMDFormat(new Date());

        Example example = new Example(RepayList.class);
        Example.Criteria criteria = example.createCriteria();
        //按门店编码降序排列
        example.setOrderByClause("repay_num desc");
        //通过时间模糊查询
        String repayNum = "HK";
        criteria.andLike("repayNum",repayNum+repayListRequest.getStoreNum() + ymdDate + "%");
        criteria.andEqualTo("storeId", repayListRequest.getStoreId());
        List<RepayList> repayLists = dao.selectByExample(example);
        String num;
        int mm = 0;
        if (repayLists != null && repayLists.size() > 0) {
            RepayList repayList = repayLists.get(0);
            int a=repayListRequest.getStoreNum().length();
            mm = Integer.parseInt(repayList.getRepayNum().substring(10+a)) + 1;
            if (mm < 10){
                num = "00" + mm;
            }else if (mm < 100){
                num = "0" + mm;
            }else {
                num = String.valueOf(mm);
            }
            repayNum += repayListRequest.getStoreNum() + ymdDate + num;

        }else {
            num = "001" ;
            repayNum += repayListRequest.getStoreNum() + ymdDate + num;
        }
        return repayNum;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updateStaff(RepayListRequest repayListRequest) throws SmsException {
        int i = 0;
        try {
            i = ssmRepayTradeService.updateStaff(repayListRequest.getRepayTradeLists());
        }catch (Exception e){
            log.error("SsmRepayServiceImpl.updateStaff:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }

        return i;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int cancel(RepayListRequest repayListRequest) throws SmsException {
        int i = 0;
        PayList payList = new PayList();
        payList.setDocId(repayListRequest.getRepayNum());
        List<PayList> payListList = ssmPayService.select(payList);
        if(payListList!=null&&payListList.size()>0) {
            for (PayList list : payListList) {
                list.setIsCancel(1);
                ssmPayService.updateByNum(list);
            }
        }
        RepayList repayList = new RepayList();
        BeanUtils.copyProperties(repayListRequest,repayList);
        //判断时间是否超过允许作废时间
        StoreParament storeParament = StoreParaUtils.getStorePara(repayList.getStoreId(),Constant.service_ticke);
        Long days =  DateUtils.getDayDiff(repayList.getRepayDate(),new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (days.longValue() > Long.parseLong(storeParament.getParamentValue())) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSION, "单据超过允许作废时间");
            }
        }
        repayList.setIsCancel(1);
        repayList.setCancelDate(new Date());
        if (redisClient.hasKey(getKey(repayListRequest.getStaffPhone()))) {
            repayList.setCancelId(redisClient.get(getKey(repayListRequest.getStaffPhone())).getId());
            repayList.setCancelBy(redisClient.get(getKey(repayListRequest.getStaffPhone())).getStaffName());
        }

        //还款明细
        List<RepayTradeRequest> repayTradeLists = repayListRequest.getRepayTradeLists();
        //还款明细作废操作
        int cancel = ssmRepayTradeService.cancel(repayTradeLists, repayList);
        if(cancel==8){
            return 8;
        }
        if(cancel==9){
            return 9;
        }

        try {
            i = dao.updateByPrimaryKeySelective(repayList);

        } catch (Exception e){
            log.error("SsmRepayServiceImpl.cancel:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int cancelPromotionRecharge(RepayListRequest repayListRequest) throws SmsException {
        int i = 0;
        PayList payList = new PayList();
        payList.setDocId(repayListRequest.getRepayNum());
        List<PayList> payListList = ssmPayService.select(payList);
        if(payListList!=null&&payListList.size()>0) {
            for (PayList list : payListList) {
                list.setIsCancel(1);
                ssmPayService.updateByNum(list);
            }
        }
        RepayList repayList = new RepayList();
        BeanUtils.copyProperties(repayListRequest,repayList);
        //判断时间是否超过允许作废时间
        StoreParament storeParament = StoreParaUtils.getStorePara(repayList.getStoreId(),Constant.service_ticke);
        Long days =  DateUtils.getDayDiff(repayList.getRepayDate(),new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (days.longValue() > Long.parseLong(storeParament.getParamentValue())) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSION, "单据超过允许作废时间");
            }
        }
        repayList.setIsCancel(1);
        repayList.setCancelDate(new Date());
        if (redisClient.hasKey(getKey(repayListRequest.getStaffPhone()))) {
            repayList.setCancelId(redisClient.get(getKey(repayListRequest.getStaffPhone())).getId());
            repayList.setCancelBy(redisClient.get(getKey(repayListRequest.getStaffPhone())).getStaffName());
        }

        //还款明细
        List<RepayTradeRequest> repayTradeLists = repayListRequest.getRepayTradeLists();
        //还款明细作废操作
        int cancel = ssmRepayTradeService.cancelPromotionRecharge(repayTradeLists, repayList);
        if(cancel==8){
            return 8;
        }
        if(cancel==9){
            return 9;
        }

        try {
            i = dao.updateByPrimaryKeySelective(repayList);

        } catch (Exception e){
            log.error("SsmRepayServiceImpl.cancel:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }

    @Override
    public List<RepayTradeResponse> selectList(Long storeId, String date, String type) {
        RepayListRequest repayListRequest = new RepayListRequest();
        repayListRequest.setStoreId(storeId);
        repayListRequest.setIsCancel(0);
        repayListRequest.setDateStr(date);
        repayListRequest.setDocType(type);
        List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectList(repayListRequest);
        //修改还款明细返回格式
        List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
        for (RepayTradeList tradeList : repayTradeLists) {
            RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
            BeanUtils.copyProperties(tradeList, repayTradeResponse);

            if(tradeList.getStoreId().equals(tradeList.getBelongingStoreId())){
                //设置业务人员列表
                List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                for (AchiCommEntity entity : entities) {
                    entity.setDocId(tradeList.getId());
                    if("card_recharge".equals(tradeList.getDocType())){//充值卡还款
                        entity.setOrderType("recharge");
                        RechargeType rechargeType = new RechargeType();
                        rechargeType.setId(tradeList.getTradeId());
                        RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                        if(null != rechargeType1){
                            entity.setProjectId(rechargeType1.getRechargeTypeNum());
                        }

                    }
                    else if("card_use".equals(tradeList.getDocType())){//疗程还款
                        entity.setOrderType("course");
                        BaseProject baseProject = new BaseProject();
                        baseProject.setId(tradeList.getTradeId());
                        BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                        if(null != baseProject1){
                            entity.setProjectId(baseProject1.getProjectNum());
                        }
                    }else if("card_change".equals(tradeList.getDocType())){//加价转疗程还款
                        entity.setOrderType("card_change");
                        BaseProject baseProject = new BaseProject();
                        baseProject.setId(tradeList.getTradeId());
                        BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                        if(null != baseProject1){
                            entity.setProjectId(baseProject1.getProjectNum());
                        }
                    }else{//疗程促销
                        entity.setOrderType("promotion_card_use");
                    }
                }
                repayTradeResponse.setEntities(entities);
                tradeResponseList.add(repayTradeResponse);
            }else{
                BaseStaff baseStaff = new BaseStaff();
                baseStaff.setStoreId(storeId);
                List<BaseStaff> select = ssmBaseStaffService.select(baseStaff);
                List<AchiCommEntity> entitie = new ArrayList<>();
                List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                for (AchiCommEntity entity : entities) {
                    for (BaseStaff staff : select) {
                        if(staff.getId().equals(entity.getSaleStaffId())){
                            entity.setDocId(tradeList.getId());
                            if("card_recharge".equals(tradeList.getDocType())){
                                entity.setOrderType("recharge");
                                RechargeType rechargeType = new RechargeType();
                                rechargeType.setId(tradeList.getTradeId());
                                RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                                if(null != rechargeType1){
                                    entity.setProjectId(rechargeType1.getRechargeTypeNum());
                                }

                            }else if("card_use".equals(tradeList.getDocType())){
                                entity.setOrderType("course");
                                BaseProject baseProject = new BaseProject();
                                baseProject.setId(tradeList.getTradeId());
                                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                                if(null != baseProject1){
                                    entity.setProjectId(baseProject1.getProjectNum());
                                }
                            }else if("card_change".equals(tradeList.getDocType())){
                                entity.setOrderType("course");
                                BaseProject baseProject = new BaseProject();
                                baseProject.setId(tradeList.getTradeId());
                                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                                if(null != baseProject1){
                                    entity.setProjectId(baseProject1.getProjectNum());
                                }
                            }else{
                                entity.setOrderType("promotion_card_use");
                            }
                            entitie.add(entity);
                        }
                    }

                }
                repayTradeResponse.setEntities(entitie);
                tradeResponseList.add(repayTradeResponse);
            }
        }

        //查询其他门店的单据
        /*StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(storeId);
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            RepayListRequest repayListRequests = new RepayListRequest();
            repayListRequests.setStoreId(pagesorganization.getId());
            repayListRequests.setIsCancel(0);
            repayListRequests.setDateStr(date);
            repayListRequests.setDocType(type);
//        List<RepayList> lists = dao.select(repayList);
//        Example example = new Example(RepayTradeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId", storeId);
//        criteria.andLike("repayDate",date + "%");
            List<RepayTradeList> repayTradeLists1 = ssmRepayTradeService.selectList(repayListRequests);
            for (RepayTradeList repayTradeList : repayTradeLists1) {
                RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
                BeanUtils.copyProperties(repayTradeList, repayTradeResponse);
                if(repayTradeList.getId().equals(storeId)){
                    continue;
                }

                if(!repayTradeList.getStoreId().equals(repayTradeList.getBelongingStoreId())){
                    BaseStaff baseStaff = new BaseStaff();
                    baseStaff.setStoreId(storeId);
                    List<BaseStaff> select = ssmBaseStaffService.select(baseStaff);
                    List<AchiCommEntity> entitie = new ArrayList<>();
                    List<AchiCommEntity> entities = this.getAchiCommList(repayTradeList);
                    for (AchiCommEntity entity : entities) {
                        for (BaseStaff staff : select) {
                            if(staff.getId().equals(entity.getSaleStaffId())){
                                entity.setDocId(repayTradeList.getId());
                                if("card_recharge".equals(repayTradeList.getDocType())){
                                    entity.setOrderType("recharge");
                                    RechargeType rechargeType = new RechargeType();
                                    rechargeType.setId(repayTradeList.getTradeId());
                                    RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                                    if(null != rechargeType1){
                                        entity.setProjectId(rechargeType1.getRechargeTypeNum());
                                    }

                                }else if("card_use".equals(repayTradeList.getDocType())){
                                    entity.setOrderType("course");
                                    BaseProject baseProject = new BaseProject();
                                    baseProject.setId(repayTradeList.getTradeId());
                                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                                    if(null != baseProject1){
                                        entity.setProjectId(baseProject1.getProjectNum());
                                    }
                                }
                                else if("card_change".equals(repayTradeList.getDocType())){
                                    entity.setOrderType("course");
                                    BaseProject baseProject = new BaseProject();
                                    baseProject.setId(repayTradeList.getTradeId());
                                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                                    if(null != baseProject1){
                                        entity.setProjectId(baseProject1.getProjectNum());
                                    }
                                }
                                else{
                                    entity.setOrderType("promotion_card_use");
                                }
                                entitie.add(entity);
                            }
                        }
                    }
                    repayTradeResponse.setEntities(entitie);
                    tradeResponseList.add(repayTradeResponse);
                }
            }
        }*/

        return tradeResponseList;
    }

    @Override
    public List<RepayTradeResponse> selectListByStartTimeAndEndTime(Long storeId, Date startTime, Date endTime) {
        RepayListRequest repayListRequest = new RepayListRequest();
        repayListRequest.setStoreId(storeId);
        repayListRequest.setIsCancel(0);
        repayListRequest.setStartTime(startTime);
        repayListRequest.setEndTime(endTime);
        List<RepayTradeList> repayTradeLists = new ArrayList<>();
        List<RepayTradeList> repayTradeLists1 = ssmRepayTradeService.selectList(repayListRequest);
        if(repayTradeLists1!=null && repayTradeLists1.size()>0){
            repayTradeLists.addAll( repayTradeLists1.stream().filter(item->item.getDocType().equals("card_use")||item.getDocType().equals("card_change")).collect(Collectors.toList()));
        }
        //修改还款明细返回格式
        List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
        for (RepayTradeList tradeList : repayTradeLists) {
            RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
            BeanUtils.copyProperties(tradeList, repayTradeResponse);

            if(tradeList.getStoreId().equals(tradeList.getBelongingStoreId())){
                //设置业务人员列表
                List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                for (AchiCommEntity entity : entities) {
                    entity.setDocId(tradeList.getId());
                    if("card_recharge".equals(tradeList.getDocType())){//充值卡还款
                        entity.setOrderType("recharge");
                        RechargeType rechargeType = new RechargeType();
                        rechargeType.setId(tradeList.getTradeId());
                        RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                        if(null != rechargeType1){
                            entity.setProjectId(rechargeType1.getRechargeTypeNum());
                        }

                    }
                    else if("card_use".equals(tradeList.getDocType())){//疗程还款
                        entity.setOrderType("course");
                        BaseProject baseProject = new BaseProject();
                        baseProject.setId(tradeList.getTradeId());
                        BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                        if(null != baseProject1){
                            entity.setProjectId(baseProject1.getProjectNum());
                        }
                    }else if("card_change".equals(tradeList.getDocType())){//加价转疗程还款
                        entity.setOrderType("card_change");
                        BaseProject baseProject = new BaseProject();
                        baseProject.setId(tradeList.getTradeId());
                        BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                        if(null != baseProject1){
                            entity.setProjectId(baseProject1.getProjectNum());
                        }
                    }else{//疗程促销
                        entity.setOrderType("promotion_card_use");
                    }
                }
                repayTradeResponse.setEntities(entities);
                tradeResponseList.add(repayTradeResponse);
            }else{
                BaseStaff baseStaff = new BaseStaff();
                baseStaff.setStoreId(storeId);
                List<BaseStaff> select = ssmBaseStaffService.select(baseStaff);
                List<AchiCommEntity> entitie = new ArrayList<>();
                List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                for (AchiCommEntity entity : entities) {
                    for (BaseStaff staff : select) {
                        if(staff.getId().equals(entity.getSaleStaffId())){
                            entity.setDocId(tradeList.getId());
                            if("card_recharge".equals(tradeList.getDocType())){
                                entity.setOrderType("recharge");
                                RechargeType rechargeType = new RechargeType();
                                rechargeType.setId(tradeList.getTradeId());
                                RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                                if(null != rechargeType1){
                                    entity.setProjectId(rechargeType1.getRechargeTypeNum());
                                }

                            }else if("card_use".equals(tradeList.getDocType())){
                                entity.setOrderType("course");
                                BaseProject baseProject = new BaseProject();
                                baseProject.setId(tradeList.getTradeId());
                                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                                if(null != baseProject1){
                                    entity.setProjectId(baseProject1.getProjectNum());
                                }
                            }else if("card_change".equals(tradeList.getDocType())){
                                entity.setOrderType("course");
                                BaseProject baseProject = new BaseProject();
                                baseProject.setId(tradeList.getTradeId());
                                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                                if(null != baseProject1){
                                    entity.setProjectId(baseProject1.getProjectNum());
                                }
                            }else{
                                entity.setOrderType("promotion_card_use");
                            }
                            entitie.add(entity);
                        }
                    }

                }
                repayTradeResponse.setEntities(entitie);
                tradeResponseList.add(repayTradeResponse);
            }
        }
        return tradeResponseList;
    }

    @Override
    public List<RepayTradeResponse> selectListchia(Long storeId,Date startDate,Date endDate) {
        RepayListRequest repayListRequest = new RepayListRequest();
        repayListRequest.setStoreId(storeId);
        repayListRequest.setStartTime(startDate);
        repayListRequest.setEndTime(endDate);
//        List<RepayList> lists = dao.select(repayList);
//        Example example = new Example(RepayTradeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId", storeId);
//        criteria.andLike("repayDate",date + "%");
        List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectListBetween(repayListRequest);
        //修改还款明细返回格式
        List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
        for (RepayTradeList tradeList : repayTradeLists) {
            RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
            BeanUtils.copyProperties(tradeList, repayTradeResponse);
            //设置业务人员列表
            List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
            for (AchiCommEntity entity : entities) {
                if("card_recharge".equals(tradeList.getDocType())){
                    RechargeType rechargeType = new RechargeType();
                    rechargeType.setId(tradeList.getTradeId());
                    RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                    if(null != rechargeType1){
                        entity.setProjectId(rechargeType1.getRechargeTypeNum());
                    }
                }else{
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(tradeList.getTradeId());
                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                    if(null != baseProject1){
                        entity.setProjectId(baseProject1.getProjectNum());
                    }
                }
            }
            repayTradeResponse.setEntities(entities);
            tradeResponseList.add(repayTradeResponse);
        }
        return tradeResponseList;
    }

    @Override
    public List<RepayTradeResponse> selectListAbnormal(Long storeId, String date, String type) {
        RepayListRequest repayListRequest = new RepayListRequest();
        repayListRequest.setStoreId(storeId);
        repayListRequest.setIsCancel(0);
        repayListRequest.setDateStr(date);
        repayListRequest.setDocType(type);
//        List<RepayList> lists = dao.select(repayList);
//        Example example = new Example(RepayTradeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId", storeId);
//        criteria.andLike("repayDate",date + "%");
        List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectList(repayListRequest);
        //修改还款明细返回格式
        List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
        for (RepayTradeList tradeList : repayTradeLists) {
            //总业绩
            BigDecimal totalPerformance = BigDecimal.valueOf(0);
            RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
            BeanUtils.copyProperties(tradeList, repayTradeResponse);
            if(tradeList.getDocNum().startsWith("LC")||tradeList.getDocNum().startsWith("ZLC")){
                repayTradeResponse.setProject(ssmProjectService.selectByPrimaryKey(tradeList.getTradeId()));
            }
            //设置业务人员列表
            List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
            for (AchiCommEntity entity : entities) {

                totalPerformance=totalPerformance.add(entity.getCashAchi().add(entity.getCardAchi()).add(entity.getCooperationAchi()==null?BigDecimal.ZERO:entity.getCooperationAchi()));
                if(entity.getType().equals("card_repay")){
                    totalPerformance = totalPerformance.subtract(entity.getCardAchi());
                }
                if("card_recharge".equals(tradeList.getDocType())){
                    RechargeType rechargeType = new RechargeType();
                    rechargeType.setId(tradeList.getTradeId());
                    RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                    if(null != rechargeType1){
                        entity.setProjectId(rechargeType1.getRechargeTypeNum());
                    }
                }else{
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(tradeList.getTradeId());
                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                    if(null != baseProject1){
                        entity.setProjectId(baseProject1.getProjectNum());
                    }
                }
            }
            if(totalPerformance.subtract(tradeList.getPayMoney().subtract(tradeList.getPayRechargeMoney())).abs().compareTo(new BigDecimal("0.05")) == 1){
                repayTradeResponse.setEntities(entities);
                repayTradeResponse.setErrorMessage("请检查是否填写员工或者漏填员工，无误则检查项目是否有业绩提成");
                tradeResponseList.add(repayTradeResponse);
            }
            List<AchiCommEntity> collect = entities.stream().filter(item -> item.getSaleStaffId()!=null &&item.getSaleRatio().compareTo(BigDecimal.valueOf(0)) == 0).collect(Collectors.toList());
            if(collect!=null && collect.size()>0){
                repayTradeResponse.setEntities(entities);
                repayTradeResponse.setErrorMessage("存在占比为零的员工存在");
                if(!tradeResponseList.contains(repayTradeResponse)){
                    tradeResponseList.add(repayTradeResponse);
                }
            }
        }
        return tradeResponseList;
    }

    @Override
    public List<RepayTradeResponse> selectLists(Long storeId, String startDate,String endDate, String type) throws Exception {
        RepayListRequest repayListRequest = new RepayListRequest();
        repayListRequest.setStoreId(storeId);
        repayListRequest.setIsCancel(0);
        repayListRequest.setStartTime(DateUtils.YMDParse(startDate));
        repayListRequest.setEndTime(DateUtils.YMDParse(endDate));
        repayListRequest.setDocType(type);
//        List<RepayList> lists = dao.select(repayList);
//        Example example = new Example(RepayTradeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId", storeId);
//        criteria.andLike("repayDate",date + "%");
        List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectLists(repayListRequest);
        //修改还款明细返回格式
        List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
        for (RepayTradeList tradeList : repayTradeLists) {
            RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
            BeanUtils.copyProperties(tradeList, repayTradeResponse);
            //设置业务人员列表
            List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
            repayTradeResponse.setEntities(entities);
            tradeResponseList.add(repayTradeResponse);
        }
        return tradeResponseList;
    }

    @Override
    public StoreDaily selectDayRecord(StoreDailyRequest storeDailyRequest) {
        return dao.selectDayRecord(storeDailyRequest);
    }

    @Override
    public StoreDaily selectRepay(StoreDailyRequest storeDailyRequest) {
        return dao.selectRepay(storeDailyRequest);
    }

    @Override
    public StoreDaily selectRepayType(StoreDailyRequest storeDailyRequest) {
        return dao.selectRepayType(storeDailyRequest);
    }

    @Override
    public StoreDaily selectCashRechargeMoney(StoreDailyRequest storeDailyRequest) {
        return dao.selectCashRechargeMoney(storeDailyRequest);
    }

    @Override
    public List<PayList> selectTypeMoneys(StoreDailyRequest storeDailyRequest) {
        return dao.selectTypeMoneys(storeDailyRequest);
    }
    @Autowired
    private RepayListMapper mapper;

    @Autowired
    private RepayTradeListMapper repayTradeListMapper;

    @Override
    public List<RepayList> selectNew(RepayListRequest repayListRequest) {
        return mapper.selectNew(repayListRequest);
    }

    @Override
    public PageInfo<RepayListResponse> selectPageNewByRepay(RepayListRequest repayListRequest) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String format = simpleDateFormat.format(repayListRequest.getRepayDate());
        repayListRequest.setIsCancel(0);

        //传单据类型
//        repayListRequest.setDocType(Constant.doc_type_recharge);
        List<RepayListResponse> list = new ArrayList<>();
        repayListRequest.setDateStr(format);

        Integer total = 0;
        List<RepayTradeList> repayTradeLists1 = repayTradeListMapper.selectList(repayListRequest);
        if(repayTradeLists1!=null && repayTradeLists1.size()>0){

            List<String> repayNumList = repayTradeLists1.stream().map(RepayTradeList::getRepayNum).collect(Collectors.toList());
            List<RepayList> repayLists = mapper.selectByRepayNum(repayNumList);
            total=repayLists.size();
            for (RepayList repayList : repayLists){
                RepayListResponse repayListResponse = new RepayListResponse();
                BeanUtils.copyProperties(repayList,repayListResponse);
                RepayTradeList repayTradeList = new RepayTradeList();
                //查询还款明细
                repayTradeList.setRepayNum(repayList.getRepayNum());
                repayTradeList.setStoreId(repayList.getStoreId());
                List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectList(repayTradeList);
                //修改还款明细返回格式
                List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
                for (RepayTradeList tradeList : repayTradeLists) {
                    RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
                    BeanUtils.copyProperties(tradeList, repayTradeResponse);
                    //设置业务人员列表
                    List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                    repayTradeResponse.setEntities(entities);
                    tradeResponseList.add(repayTradeResponse);

                    RepayAccountList repayAccountList = new RepayAccountList();
                    repayAccountList.setRepayTradeId(tradeList.getId());
                    List<RepayAccountList> select = ssmRepayAccountListService.select(repayAccountList);
                    repayTradeResponse.setRepayAccountListRequests(select);
                }
                BigDecimal reduce = repayTradeLists.stream().map(RepayTradeList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                repayListResponse.setPayMoney(reduce);
                //查询支付方式
                PayList payList = new PayList();
                payList.setDocId(repayList.getRepayNum());
                List<PayList> payLists = ssmPayService.select(payList);

                repayListResponse.setRepayTradeLists(tradeResponseList);
                repayListResponse.setPayLists(payLists);

                list.add(repayListResponse);
            }

        }

        PageInfo<RepayListResponse> p = new PageInfo<>(list);
        p.setTotal(total);
        return p;
    }

    @Override
    public PageInfo<RepayListResponse> selectPageNewByCoursePay(RepayListRequest repayListRequest) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String format = simpleDateFormat.format(repayListRequest.getRepayDate());
        repayListRequest.setIsCancel(0);

        //传单据类型
//        repayListRequest.setDocType(Constant.doc_type_recharge);
        List<RepayListResponse> list = new ArrayList<>();
        repayListRequest.setDateStr(format);

        Integer total = 0;
        List<RepayTradeList> repayTradeLists1 = repayTradeListMapper.selectCoursePayList(repayListRequest);
        if(repayTradeLists1!=null && repayTradeLists1.size()>0){

            List<String> repayNumList = repayTradeLists1.stream().map(RepayTradeList::getRepayNum).collect(Collectors.toList());
            List<RepayList> repayLists = mapper.selectByRepayNum(repayNumList);
            total=repayLists.size();
            for (RepayList repayList : repayLists){
                RepayListResponse repayListResponse = new RepayListResponse();
                BeanUtils.copyProperties(repayList,repayListResponse);
                RepayTradeList repayTradeList = new RepayTradeList();
                //查询还款明细
                repayTradeList.setRepayNum(repayList.getRepayNum());
                repayTradeList.setStoreId(repayList.getStoreId());
                List<RepayTradeList> repayTradeLists = ssmRepayTradeService.selectList(repayTradeList);
                //修改还款明细返回格式
                List<RepayTradeResponse> tradeResponseList = new ArrayList<>();
                for (RepayTradeList tradeList : repayTradeLists) {
                    RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
                    BeanUtils.copyProperties(tradeList, repayTradeResponse);
                    //设置业务人员列表
                    List<AchiCommEntity> entities = this.getAchiCommList(tradeList);
                    repayTradeResponse.setEntities(entities);
                    tradeResponseList.add(repayTradeResponse);

                    RepayAccountList repayAccountList = new RepayAccountList();
                    repayAccountList.setRepayTradeId(tradeList.getId());
                    List<RepayAccountList> select = ssmRepayAccountListService.select(repayAccountList);
                    repayTradeResponse.setRepayAccountListRequests(select);
                }
                //查询支付方式
                PayList payList = new PayList();
                payList.setDocId(repayList.getRepayNum());
                List<PayList> payLists = ssmPayService.select(payList);

                repayListResponse.setRepayTradeLists(tradeResponseList);
                repayListResponse.setPayLists(payLists);

                list.add(repayListResponse);
            }

        }

        PageInfo<RepayListResponse> p = new PageInfo<>(list);
        p.setTotal(total);
        return p;

    }

    public String getKey(String phone){
        String key = "";
        if (StringUtils.isNotBlank(phone)){
            key = Constant.staffKey + phone;
        }
        return key;
    }

    /*
     * 业务员列表设置返回值
     * */
    public List<AchiCommEntity> getAchiCommList(RepayTradeList repayTradeList){
        //充值欠款单，疗程付扣减提成
        BigDecimal rechargeDebtCourse = BigDecimal.valueOf(0);
        BigDecimal rechargeDebtCourseChia = BigDecimal.valueOf(0);

        //疗程欠款单，充值开卡添加提成
        BigDecimal coursDebteRecharge = BigDecimal.valueOf(0);
        BigDecimal coursDebteRechargechia = BigDecimal.valueOf(0);

        //疗程欠款单，疗程付款扣或添提成
        BigDecimal courseDebteCourse = BigDecimal.valueOf(0);
        BigDecimal courseDebteCoursechia = BigDecimal.valueOf(0);

        //卡付还款的总金额
        BigDecimal cardTotalMoney=BigDecimal.valueOf(0);
        List<AchiCommEntity> entities = new ArrayList<>();
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");

        RepayAccountList repayAccountList = new RepayAccountList();
        repayAccountList.setRepayTradeId(repayTradeList.getId());
        List<RepayAccountList> select = ssmRepayAccountListService.select(repayAccountList);
        BigDecimal reduce = select.stream().map(RepayAccountList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//        BigDecimal payMoney = repayTradeList.getPayMoney();
//        repayTradeList.setPayMoney(repayTradeList.getPayMoney().subtract(reduce));
//        repayTradeList = setAchiComm(repayTradeList);

        RepayAccountList repayAccountList1 = new RepayAccountList();
        repayAccountList1.setRepayTradeId(repayTradeList.getId());
        List<RepayAccountList> select1 = ssmRepayAccountListService.select(repayAccountList1);
        //充值欠款单
        if(repayTradeList.getDocType().equals("card_recharge")){
            BigDecimal totalMoney=BigDecimal.valueOf(0);
            //判断是否存在疗程付
            for (RepayAccountList accountList : select1) {
                totalMoney=totalMoney.add(accountList.getPayMoney());
                CourseAccount courseAccount = new CourseAccount();
                courseAccount.setId(accountList.getAccountId());


                if(accountList.getAccountType()==0){
                    RechargeAccount rechargeAccount=ssmRechargeAccountService.selectByPrimaryKey(courseAccount.getId());
                    RechargeType rechargeType=ssmRechargeTypeSevice.selectByPrimaryKey(rechargeAccount.getRechargeTypeId());
                    BigDecimal cardSalesAchievement = rechargeType.getTypeRatio();
                    if(cardSalesAchievement.compareTo(BigDecimal.valueOf(1))==1){//固定
                        rechargeDebtCourseChia = cardSalesAchievement;
                    }else {//比例
                        rechargeDebtCourseChia = cardSalesAchievement.multiply(accountList.getPayMoney());
                    }

                }else {
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectByPrimaryKey(courseAccount.getId());
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(courseAccount1.getCourseProjectId());
                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());
                    BigDecimal cardSalesAchievement = baseProject1.getCardSalesAchievement();
                    BigDecimal cardSalesCommission = baseProject1.getCardSalesCommission();

                    if(cardSalesAchievement.compareTo(BigDecimal.valueOf(1))==1){
                        rechargeDebtCourseChia = cardSalesAchievement;
                        //固定
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            rechargeDebtCourse=rechargeDebtCourse.add(cardSalesCommission);
                        }else{//比例
                            rechargeDebtCourse=rechargeDebtCourse.add(cardSalesCommission.multiply(cardSalesAchievement));
                        }
                    }else{//比例
                        rechargeDebtCourseChia = cardSalesAchievement.multiply(accountList.getPayMoney());
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            rechargeDebtCourse=rechargeDebtCourse.add(cardSalesCommission);
                        }else{//比例
                            rechargeDebtCourse=rechargeDebtCourse.add(cardSalesCommission.multiply(rechargeDebtCourseChia));
                        }
                    }
                }
            }
            //之前的 repayTradeList.setPayMoney(repayTradeList.getPayMoney().add(totalMoney));
            repayTradeList.setPayMoney(repayTradeList.getPayMoney());
            repayTradeList = this.setAchiComm(repayTradeList);
            repayTradeList.setCommissionAmount(repayTradeList.getCommissionAmount().subtract(rechargeDebtCourse));
//            repayTradeList.setCardAchievementAmount(rechargeDebtCourseChia.subtract(BigDecimal.valueOf(0)));
            repayTradeList.setCardCommissionAmount(BigDecimal.valueOf(0)
//                    .subtract(rechargeDebtCourse)
            );
            //之前的repayTradeList.setPayMoney(repayTradeList.getPayMoney().add(totalMoney));
            repayTradeList.setPayMoney(repayTradeList.getPayMoney());
            if (null != repayTradeList.getSaleOneStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();

                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                //achiCommEntity.setProjectId(String.valueOf(repayTradeList.get()));
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleOneNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleOneStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleOneStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleOneIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleOneRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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()));
                achiCommEntity.setTotalPerformance(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());

                //内调金额
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleTwoStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleTwoNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleTwoStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleTwoStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleTwoIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleTwoRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleThreeStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleThreeNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleThreeStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleThreeStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleThreeIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleThreeRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleFourStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleFourNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleFourStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleFourStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleFourIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleFourRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                entities.add(achiCommEntity);
            }

            if (null != repayTradeList.getSaleFiveStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();

                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                //achiCommEntity.setProjectId(String.valueOf(repayTradeList.get()));
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleFiveNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleFiveStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleFiveStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleFiveIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleFiveRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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()));
                achiCommEntity.setTotalPerformance(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());

                //内调金额
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleSixStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleSixNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleSixStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleSixStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleSixIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleSixRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleSevenStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleSevenNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleSevenStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleSevenStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleSevenIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleSevenRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleEightStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleEightNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleEightStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleEightStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleEightIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleEightRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi());
                achiCommEntity.setTotalCommission(achiCommEntity.getCashComm());
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(totalMoney,BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                entities.add(achiCommEntity);
            }

        }
        else if(repayTradeList.getDocType().equals("card_use") || repayTradeList.getDocType().equals("card_change")){
            for (RepayAccountList accountList : select1) {
                cardTotalMoney=cardTotalMoney.add(accountList.getPayMoney());
                if(accountList.getAccountType()==0){
                    //充值卡
                    DebtList debtList = new DebtList();
                    debtList.setId(repayTradeList.getDebtId());
                    DebtList debtList1 = ssmDebtService.selectByPrimaryKey(debtList.getId());
                    CourseAccount courseAccount = new CourseAccount();
                    courseAccount.setId(debtList1.getAccountId());
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectByPrimaryKey(courseAccount.getId());
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(courseAccount1.getCourseProjectId());
                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());
                    BigDecimal cardSalesAchievement = baseProject1.getCardSalesAchievement();
                    BigDecimal cardSalesCommission = baseProject1.getCardSalesCommission();
                    if(cardSalesAchievement.compareTo(BigDecimal.valueOf(1))==1){
                        coursDebteRechargechia = coursDebteRechargechia.add(cardSalesAchievement);
                        //固定
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            coursDebteRecharge=coursDebteRecharge.add(cardSalesCommission);
                        }else{//比例
                            coursDebteRecharge=coursDebteRecharge.add(cardSalesCommission.multiply(cardSalesAchievement));
                        }
                    }else{//比例
                        BigDecimal multiply = cardSalesAchievement.multiply(accountList.getPayMoney());
                        coursDebteRechargechia = coursDebteRechargechia.add(multiply);
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            coursDebteRecharge=coursDebteRecharge.add(cardSalesCommission);
                        }else{//比例
                            coursDebteRecharge=coursDebteRecharge.add(cardSalesCommission.multiply(multiply));
                        }
                    }
                }
                else{
                    BigDecimal chia = BigDecimal.valueOf(0);
                    BigDecimal comm = BigDecimal.valueOf(0);
                    BigDecimal chiaout = BigDecimal.valueOf(0);
                    BigDecimal commout = BigDecimal.valueOf(0);
                    //疗程卡
                    DebtList debtList = new DebtList();
                    debtList.setId(repayTradeList.getDebtId());
                    DebtList debtList1 = ssmDebtService.selectByPrimaryKey(debtList.getId());
                    CourseAccount courseAccount = new CourseAccount();
                    courseAccount.setId(debtList1.getAccountId());
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectByPrimaryKey(courseAccount.getId());
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(courseAccount1.getCourseProjectId());
                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());
                    BigDecimal cardSalesAchievement = baseProject1.getCardSalesAchievement();
                    BigDecimal cardSalesCommission = baseProject1.getCardSalesCommission();
                    if(cardSalesAchievement.compareTo(BigDecimal.valueOf(1))==1){
                        //固定
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            comm=comm.add(cardSalesCommission);
                        }else{//比例
                            comm=comm.add(cardSalesCommission.multiply(cardSalesAchievement));
                        }
                    }else{//比例
                        BigDecimal multiply = cardSalesAchievement.multiply(accountList.getPayMoney());
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            comm=comm.add(cardSalesCommission);
                        }else{//比例
                            comm=comm.add(cardSalesCommission.multiply(multiply));
                        }
                    }
                    CourseAccount courseAccount3 = new CourseAccount();
                    courseAccount3.setId(accountList.getAccountId());
                    courseAccount3 = ssmCourseAccountService.selectByPrimaryKey(courseAccount3.getId());
                    BaseProject baseProject3 = new BaseProject();
                    baseProject3.setId(courseAccount3.getCourseProjectId());
                    baseProject3 = ssmProjectService.selectByPrimaryKey(baseProject3.getId());
                    cardSalesAchievement = baseProject3.getCardSalesAchievement();
                    cardSalesCommission = baseProject3.getCardSalesCommission();
                    if(cardSalesAchievement.compareTo(BigDecimal.valueOf(1))==1){
                        //固定
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            commout=commout.add(cardSalesCommission);
                        }else{//比例
                            commout=commout.add(cardSalesCommission.multiply(cardSalesAchievement));
                        }
                    }else{//比例
                        BigDecimal multiply = cardSalesAchievement.multiply(accountList.getPayMoney());
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            commout=commout.add(cardSalesCommission);
                        }else{//比例
                            commout=commout.add(cardSalesCommission.multiply(multiply));
                        }
                    }
                    //本条疗程的提点
                    BigDecimal bigDecimals = BigDecimal.valueOf(0);
                    if(commout.compareTo(comm)==1){ //扣
                        bigDecimals= commout.subtract(comm);
                        courseDebteCourse=courseDebteCourse.subtract(bigDecimals);
                    }else if(commout.compareTo(comm)==-1){ //加
                        bigDecimals= comm.subtract(commout);
                        courseDebteCourse=courseDebteCourse.add(bigDecimals);
                    }
                }
            }
//            repayTradeList.setCommissionAmount(repayTradeList.getCommissionAmount());
//            repayTradeList.setCardAchievementAmount(repayTradeList.getCardAchievementAmount().add(coursDebteRechargechia));
//            repayTradeList.setCardCommissionAmount(repayTradeList.getCardCommissionAmount().add(coursDebteRecharge).add(courseDebteCourse));
            //之前的repayTradeList.setPayMoney(repayTradeList.getPayMoney().add(cardTotalMoney));
            repayTradeList.setPayMoney(repayTradeList.getPayMoney());
            BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
            BigDecimal broker = BigDecimal.valueOf(0);
//            if(baseProject.getPrice().compareTo(BigDecimal.ZERO)==0){
//                baseProject.setPrice(new BigDecimal("1"));
//            }
            if(baseProject.getSmokeCommissionWay()!=null && baseProject.getSmokeCommissionWay()==0){
                broker = baseProject.getBrokerage().divide(baseProject.getPriceMember(),10,BigDecimal.ROUND_HALF_UP).multiply(repayTradeList.getRepayMoney());
            }
            if(baseProject.getSmokeCommissionWay()!=null && baseProject.getSmokeCommissionWay()==1){
                broker = baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(repayTradeList.getRepayMoney());
            }
            if (null != repayTradeList.getSaleOneStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                //achiCommEntity.setProjectId(String.valueOf(repayTradeList.get()));
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleOneNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleOneStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleOneStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleOneIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleOneRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils
                        .add(BigDecimalUtils
                                        .multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils
                                                .div(repayTradeList.getSaleOneRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils
                                .div(repayTradeList.getSaleOneRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker)
                                .multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD))));
                repayTradeList.setCardAchievementAmount(cardTotalMoney);
                if(baseProject.getCardSalesCommission().compareTo(new BigDecimal("1"))<0){

                    repayTradeList.setCardCommissionAmount(cardTotalMoney.multiply(baseProject.getCardSalesCommission()));
                }
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleOneRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleTwoStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleTwoNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleTwoStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleTwoStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleTwoIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleTwoRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleTwoRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleThreeStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleThreeNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleThreeStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleThreeStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleThreeIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleThreeRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleThreeRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleFourStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleFourNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleFourStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleFourStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleFourIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleFourRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleFourRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }

            if (null != repayTradeList.getSaleFiveStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                //achiCommEntity.setProjectId(String.valueOf(repayTradeList.get()));
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleFiveNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleFiveStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleFiveStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleFiveIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleFiveRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils
                        .add(BigDecimalUtils
                                        .multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils
                                                .div(repayTradeList.getSaleFiveRatio(),hundredBD)),
                                BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils
                                        .div(repayTradeList.getSaleFiveRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker)
                                .multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD))));
                repayTradeList.setCardAchievementAmount(cardTotalMoney);
                if(baseProject.getCardSalesCommission().compareTo(new BigDecimal("1"))<0){

                    repayTradeList.setCardCommissionAmount(cardTotalMoney.multiply(baseProject.getCardSalesCommission()));
                }
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleFiveRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleSixStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleSixNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleSixStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleSixStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleSixIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleSixRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleSixRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleSevenStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleSevenNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleSevenStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleSevenStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleSevenIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleSevenRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleSevenRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleEightStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleEightNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleEightStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleEightStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleEightIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleEightRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesAchievement()),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getRepayMoney().subtract(broker).multiply(baseProject.getSalesCommission()),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi(repayTradeList.getPayMoney().subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCooperationAchi(broker.multiply(repayTradeList.getSaleEightRatio().divide(BigDecimal.valueOf(100))));
                achiCommEntity.setCardCoopAchi(BigDecimal.valueOf(0));
                achiCommEntity.setCashCoopAchi(achiCommEntity.getCooperationAchi());
                entities.add(achiCommEntity);
            }

        }else {//项目促销
            for (RepayAccountList accountList : select1) {
                cardTotalMoney=cardTotalMoney.add(accountList.getPayMoney());
                if(accountList.getAccountType()==0){
                    //充值卡
                    DocPromotion docPromotion = new DocPromotion();
                    docPromotion.setId(repayTradeList.getDocId());
                    DocPromotion docPromotion1 = ssmDocPromotionService.selectOne(docPromotion);
                    BigDecimal cardSalesCommission = docPromotion1.getCardSalesCommission();
                    if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                        coursDebteRecharge=coursDebteRecharge.add(cardSalesCommission);
                    }else{//比例
                        coursDebteRecharge=coursDebteRecharge.add(cardSalesCommission.multiply(accountList.getPayMoney()));
                    }
                }else{
                    BigDecimal chia = BigDecimal.valueOf(0);
                    BigDecimal comm = BigDecimal.valueOf(0);
                    BigDecimal chiaout = BigDecimal.valueOf(0);
                    BigDecimal commout = BigDecimal.valueOf(0);
                    //疗程卡
                    DebtList debtList = new DebtList();
                    debtList.setId(repayTradeList.getDebtId());
                    DebtList debtList1 = ssmDebtService.selectByPrimaryKey(debtList.getId());

                    List<DocPromotionProject> docPromotionProjects=new ArrayList<>();
                    DocPromotion docPromotion=ssmDocPromotionService.selectByPrimaryKey(debtList1.getDocId());
                    DocPromotionProject docPromotionProject=new DocPromotionProject();
                    docPromotionProject.setDocNum(docPromotion.getDocNum());
                    docPromotionProjects=ssmDocPromotionProjectService.select(docPromotionProject);
                    if(docPromotionProjects.size()>0){
                        for(DocPromotionProject docPromotionProject1:docPromotionProjects){
                            BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(docPromotionProject1.getProjectId());
                            BigDecimal cardSalesAchievement = baseProject1.getCardSalesAchievement();
                            BigDecimal cardSalesCommission = baseProject1.getCardSalesCommission();
                            if(cardSalesAchievement.compareTo(BigDecimal.valueOf(1))==1){
                                //固定
                                if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                                    comm=comm.add(cardSalesCommission);
                                }else{//比例
                                    comm=comm.add(cardSalesCommission.multiply(cardSalesAchievement));
                                }
                            }else{//比例
                                BigDecimal multiply = cardSalesAchievement.multiply(accountList.getPayMoney());
                                if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                                    comm=comm.add(cardSalesCommission);
                                }else{//比例
                                    comm=comm.add(cardSalesCommission.multiply(multiply));
                                }
                            }
                        }
                    }

                    /*CourseAccount courseAccount = new CourseAccount();
                    courseAccount.setId(debtList1.getAccountId());
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectByPrimaryKey(courseAccount.getId());
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(courseAccount1.getCourseProjectId());
                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());
                    BigDecimal cardSalesAchievement = baseProject1.getCardSalesAchievement();
                    BigDecimal cardSalesCommission = baseProject1.getCardSalesCommission();
                    if(cardSalesAchievement.compareTo(BigDecimal.valueOf(1))==1){
                        //固定
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            comm=comm.add(cardSalesCommission);
                        }else{//比例
                            comm=comm.add(cardSalesCommission.multiply(cardSalesAchievement));
                        }
                    }else{//比例
                        BigDecimal multiply = cardSalesAchievement.multiply(accountList.getPayMoney());
                        if(cardSalesCommission.compareTo(BigDecimal.valueOf(1))==1){ //固定
                            comm=comm.add(cardSalesCommission);
                        }else{//比例
                            comm=comm.add(cardSalesCommission.multiply(multiply));
                        }
                    }*/

                    BigDecimal cardSalesCommission1 = docPromotion.getCardSalesCommission();
                    if(cardSalesCommission1.compareTo(BigDecimal.valueOf(1))==1){ //固定
                        commout=commout.add(cardSalesCommission1);
                    }else{//比例
                        commout=commout.add(cardSalesCommission1.multiply(accountList.getPayMoney()));
                    }
                    //本条疗程的提点
                    BigDecimal bigDecimals = BigDecimal.valueOf(0);
                    if(commout.compareTo(comm)==1){ //扣
                        bigDecimals= commout.subtract(comm);
                        courseDebteCourse=courseDebteCourse.subtract(bigDecimals);
                    }else if(commout.compareTo(comm)==-1){ //加
                        bigDecimals= comm.subtract(commout);
                        courseDebteCourse=courseDebteCourse.add(bigDecimals);
                    }
                }
            }
            repayTradeList.setCardCommissionAmount(repayTradeList.getCardCommissionAmount().add(coursDebteRecharge).add(courseDebteCourse));

            //之前的repayTradeList.setPayMoney(repayTradeList.getPayMoney().add(cardTotalMoney));
            repayTradeList.setPayMoney(repayTradeList.getPayMoney());
            if (null != repayTradeList.getSaleOneStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                //achiCommEntity.setProjectId(String.valueOf(repayTradeList.get()));
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleOneNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleOneStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleOneStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleOneIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleOneRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setTotalPerformance(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleOneRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleTwoStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleTwoNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleTwoStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleTwoStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleTwoIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleTwoRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleTwoRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleThreeStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleThreeNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleThreeStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleThreeStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleThreeIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleThreeRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleThreeRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleFourStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleFourNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleFourStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleFourStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleFourIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleFourRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleFourRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }

            if (null != repayTradeList.getSaleFiveStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                //achiCommEntity.setProjectId(String.valueOf(repayTradeList.get()));
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleFiveNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleFiveStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleFiveStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleFiveIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleFiveRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setTotalPerformance(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleFiveRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleSixStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleSixNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleSixStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleSixStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleSixIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleSixRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleSixRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleSevenStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleSevenNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleSevenStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleSevenStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleSevenIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleSevenRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleSevenRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
            if (null != repayTradeList.getSaleEightStaffId()){
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(repayTradeList.getPayMoney());
                achiCommEntity.setMemberNum(String.valueOf(repayTradeList.getMemberId()));
                achiCommEntity.setMemberName(repayTradeList.getMemberName());
                achiCommEntity.setDocNum(repayTradeList.getRepayNum());
                achiCommEntity.setDocType(repayTradeList.getDocType());
                achiCommEntity.setType(Constant.doc_type_repay);
                achiCommEntity.setDocDate(repayTradeList.getRepayDate());
                achiCommEntity.setTradeName(repayTradeList.getTradeName());
                achiCommEntity.setBrandNumber(repayTradeList.getSaleEightNumber());
                achiCommEntity.setSaleStaffId(repayTradeList.getSaleEightStaffId());
                achiCommEntity.setSaleStaffName(repayTradeList.getSaleEightStaffName());
                achiCommEntity.setSaleIdentityCard(repayTradeList.getSaleEightIdentity());
                achiCommEntity.setSaleRatio(repayTradeList.getSaleEightRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD))));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.add(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)),
                        BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD))));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(repayTradeList.getAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(repayTradeList.getCommissionAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCardAchi(BigDecimalUtils.multiply(repayTradeList.getCardAchievementAmount(),BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(repayTradeList.getCardCommissionAmount(),BigDecimalUtils.div(repayTradeList.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(achiCommEntity.getCashAchi().add(achiCommEntity.getCardAchi()));
                achiCommEntity.setCooperationAchi((repayTradeList.getPayMoney().multiply(BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD))).subtract(achiCommEntity.getTotalPerformance()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));
                achiCommEntity.setNTMoney(BigDecimalUtils.multiply(cardTotalMoney,BigDecimalUtils.div(repayTradeList.getSaleEightRatio(),hundredBD)));

                entities.add(achiCommEntity);
            }
        }

        return entities;
    }

    public StoreParament getStorePara(Long storeId,String key){
        StoreParament storeParament = new StoreParament();
        storeParament.setStoreId(storeId);
        storeParament.setParamentName(key);
        storeParament = ssmStoreParaService.selectOne(storeParament);
        return storeParament;
    }

    public RepayTradeList setAchiComm(RepayTradeList repayTradeList) {
        //疗程还款 业绩提成计算
        if (Constant.doc_type_card_use.equals(repayTradeList.getDocType())) {
            CourseAccount courseAccount = ssmCourseAccountService.selectByPrimaryKey(repayTradeList.getAccountId());
            //现付销售业绩标准  >0 <=1比率   >1固定金额
            BigDecimal achiRatio = repayTradeList.getSalesAchievement();
            //现付销售提成标准  >0 <=1比率   >1固定金额
            BigDecimal commRatio = repayTradeList.getSalesCommission();
            //业绩
            if (BigDecimalUtils.compareTo(achiRatio, BigDecimalUtils.bigDecimal("1")) != 1) {
                repayTradeList.setAchievementAmount(BigDecimalUtils.multiply(achiRatio, repayTradeList.getPayMoney()));
            } else {
                //固定金额 还款金额/(成交金额/成交次数)*业绩标准
                repayTradeList.setAchievementAmount(BigDecimalUtils.multiply(
                        BigDecimalUtils.div(repayTradeList.getPayMoney(),
                                BigDecimalUtils.div(courseAccount.getCourseProjectMoney(), BigDecimalUtils.bigDecimal(courseAccount.getCourseCount().toString()))), achiRatio));
            }
            //提成
            if (BigDecimalUtils.compareTo(commRatio, BigDecimalUtils.bigDecimal("1")) != 1) {

                repayTradeList.setCommissionAmount(BigDecimalUtils.multiply(commRatio, repayTradeList.getAchievementAmount()));

            } else {
                //固定金额 还款金额/(成交金额/成交次数)*提成标准
                repayTradeList.setCommissionAmount(BigDecimalUtils.multiply(
                        BigDecimalUtils.div(repayTradeList.getPayMoney(),
                                BigDecimalUtils.div(courseAccount.getCourseProjectMoney(), BigDecimalUtils.bigDecimal(courseAccount.getCourseCount().toString()))), commRatio));
            }
        }else if (Constant.doc_type_recharge.equals(repayTradeList.getDocType())){
            RechargeAccount rechargeAccount = ssmRechargeAccountService.selectByPrimaryKey(repayTradeList.getAccountId());
            //提成标准
            BigDecimal commRatio = repayTradeList.getTypeRatio();
            //业绩
            repayTradeList.setAchievementAmount(repayTradeList.getRepayMoney());
            //提成
            if (BigDecimalUtils.compareTo(commRatio, BigDecimalUtils.bigDecimal("1")) != 1) {

                repayTradeList.setCommissionAmount(BigDecimalUtils.multiply(commRatio, repayTradeList.getPayMoney()));

            } else {
                repayTradeList.setCommissionAmount(BigDecimalUtils.multiply(
                        BigDecimalUtils.div(repayTradeList.getPayMoney(), rechargeAccount.getCardRechargeMoney()), commRatio));
            }
        }
        RepayTradeResponse repayTradeResponse = new RepayTradeResponse();
        BeanUtils.copyProperties(repayTradeList,repayTradeResponse);


        return repayTradeList;
    }
}
