package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.BaseService;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmRefundCourseDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.mapper.RefundCourseMapper;
import com.ssm.manage.query.*;
import com.ssm.manage.response.DebtListResponse;
import com.ssm.manage.response.RefundCourseResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
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.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class SsmRefundCourseServiceImpl extends BaseServiceImpl<RefundCourse> implements SsmRefundCourseService {
    @Autowired
    private SsmDebtService ssmDebtService;

    @Autowired
    private RefundCourseMapper mapper;

    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;

    @Autowired
    private SsmRefundDebtService ssmRefundDebtService;

    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;

    @Autowired
    private SsmProjectService ssmProjectService;

    @Autowired
    private SsmUploadFilesService ssmUploadFilesService;

    @Autowired
    private SsmSalesmanService ssmSalesmanService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;


    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;

    @Autowired
    private SsmDocPromotionProjectService ssmDocPromotionProjectService;

    @Autowired
    private SsmMemberCsService ssmMemberCsService;

   @Autowired
   private SsmRefundCourseDao dao;

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

    @Override
    public List<RefundCourseResponse> selectList(RefundCourseRequest refundCourseRequest) {
        RefundCourse refundCourse = new RefundCourse();
        BeanUtils.copyProperties(refundCourseRequest, refundCourse);
        refundCourse.setStoreId(null);
        List<RefundCourse> list = dao.select(refundCourse);
        List<RefundCourseResponse> responseList = new ArrayList<>();
        for (RefundCourse course : list) {
            RefundCourseResponse courseResponse = new RefundCourseResponse();
            BeanUtils.copyProperties(course, courseResponse);
            //查询当前单据的高管
            Salesman salesman = new Salesman();
            salesman.setOrderNum(course.getRefundNum());
            salesman.setProjectId(course.getCourseProjectId());
//            salesman.setOrderId(String.valueOf(course.getId()));
            List<Salesman> salesmanList1 = ssmSalesmanService.select(salesman);
            List<Salesman> salesmanList = new ArrayList<>();
            List<BaseStaff> baseStaffs = ssmBaseStaffService.selectByAllSalemans();
            for (Salesman salesman1 : salesmanList1) {
                if(courseResponse.getCourseRefundMoney().compareTo(salesman1.getMoney().negate())==0){
                    salesmanList.add(salesman1);
                }
                for (BaseStaff baseStaff : baseStaffs) {
                    if(salesman1.getStaffName().equals(baseStaff.getStaffName()) && salesman1.getBrandNumber().equals(baseStaff.getBrandNumber())
//                            &&baseStaff.getStoreId()==salesman1.getStoreId()
                    ){
                        salesman1.setStaffId(baseStaff.getId());
                    }
                }
            }

            courseResponse.setSalesmans(salesmanList);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(course);
            //查询退款单确认文件
            UploadFilesRequest uploadFilesRequest = new UploadFilesRequest();
            uploadFilesRequest.setDocId(refundCourseRequest.getRefundOrderId());
            uploadFilesRequest.setDocNum(course.getRefundNum());
            uploadFilesRequest.setDocType(Constant.doc_type_refund_course);
            List<UploadFiles> filesList = ssmUploadFilesService.selectList(uploadFilesRequest);
            //设置返回List
            courseResponse.setEntities(entities);
            courseResponse.setFileLists(filesList);
            BaseProject baseProject = new BaseProject();
            baseProject.setId(course.getCourseProjectId());
            BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());
            courseResponse.setCourseProjectNum(baseProject1.getProjectNum());
            courseResponse.setCourseAccount(ssmCourseAccountService.selectByPrimaryKey(courseResponse.getCourseAccountId()));
            responseList.add(courseResponse);
        }
        return responseList;
    }

    public List<AchiCommEntity> getAchiCommList(RefundCourse refundCourse){
        List<AchiCommEntity> entities = new ArrayList<>();
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");

            if (null != refundCourse.getSaleOneStaffId()) {
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
                achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
                achiCommEntity.setMemberName(refundCourse.getMemberName());
                achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
                achiCommEntity.setDocNum(refundCourse.getRefundNum());
                achiCommEntity.setDocType(Constant.doc_type_refund_course);
                achiCommEntity.setDocDate(refundCourse.getRefundDate());
                achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
                achiCommEntity.setBrandNumber(refundCourse.getSaleOneNumber());
                achiCommEntity.setSaleStaffId(refundCourse.getSaleOneStaffId());
                achiCommEntity.setSaleStaffName(refundCourse.getSaleOneStaffName());
                achiCommEntity.setSaleIdentityCard(refundCourse.getSaleOneIdentity());
                achiCommEntity.setSaleRatio(refundCourse.getSaleOneRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                entities.add(achiCommEntity);
            }
            if (null != refundCourse.getSaleTwoStaffId()) {
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
                achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
                achiCommEntity.setMemberName(refundCourse.getMemberName());
                achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
                achiCommEntity.setDocNum(refundCourse.getRefundNum());
                achiCommEntity.setDocType(Constant.doc_type_refund_course);
                achiCommEntity.setDocDate(refundCourse.getRefundDate());
                achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
                achiCommEntity.setBrandNumber(refundCourse.getSaleTwoNumber());
                achiCommEntity.setSaleStaffId(refundCourse.getSaleTwoStaffId());
                achiCommEntity.setSaleStaffName(refundCourse.getSaleTwoStaffName());
                achiCommEntity.setSaleIdentityCard(refundCourse.getSaleTwoIdentity());
                achiCommEntity.setSaleRatio(refundCourse.getSaleTwoRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
                entities.add(achiCommEntity);
            }
            if (null != refundCourse.getSaleThreeStaffId()) {
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
                achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
                achiCommEntity.setMemberName(refundCourse.getMemberName());
                achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
                achiCommEntity.setDocNum(refundCourse.getRefundNum());
                achiCommEntity.setDocType(Constant.doc_type_refund_course);
                achiCommEntity.setDocDate(refundCourse.getRefundDate());
                achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
                achiCommEntity.setBrandNumber(refundCourse.getSaleThreeNumber());
                achiCommEntity.setSaleStaffId(refundCourse.getSaleThreeStaffId());
                achiCommEntity.setSaleStaffName(refundCourse.getSaleThreeStaffName());
                achiCommEntity.setSaleIdentityCard(refundCourse.getSaleThreeIdentity());
                achiCommEntity.setSaleRatio(refundCourse.getSaleThreeRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
                entities.add(achiCommEntity);
            }
            if (null != refundCourse.getSaleFourStaffId()) {
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
                achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
                achiCommEntity.setMemberName(refundCourse.getMemberName());
                achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
                achiCommEntity.setDocNum(refundCourse.getRefundNum());
                achiCommEntity.setDocType(Constant.doc_type_refund_course);
                achiCommEntity.setDocDate(refundCourse.getRefundDate());
                achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
                achiCommEntity.setBrandNumber(refundCourse.getSaleFourNumber());
                achiCommEntity.setSaleStaffId(refundCourse.getSaleFourStaffId());
                achiCommEntity.setSaleStaffName(refundCourse.getSaleFourStaffName());
                achiCommEntity.setSaleIdentityCard(refundCourse.getSaleFourIdentity());
                achiCommEntity.setSaleRatio(refundCourse.getSaleFourRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleFourRatio(), hundredBD)));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleFourRatio(), hundredBD)));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleFourRatio(), hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleFourRatio(), hundredBD)));
                entities.add(achiCommEntity);
            }if (null != refundCourse.getSaleFiveStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocNum(refundCourse.getRefundNum());
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setBrandNumber(refundCourse.getSaleFiveNumber());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleFiveStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleFiveStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleFiveIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleFiveRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
            entities.add(achiCommEntity);
        }if (null != refundCourse.getSaleSixStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocNum(refundCourse.getRefundNum());
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setBrandNumber(refundCourse.getSaleSixNumber());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleSixStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleSixStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleSixIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleSixRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            entities.add(achiCommEntity);
        }if (null != refundCourse.getSaleSevenStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocNum(refundCourse.getRefundNum());
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setBrandNumber(refundCourse.getSaleSevenNumber());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleSevenStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleSevenStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleSevenIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleSevenRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            entities.add(achiCommEntity);
        }if (null != refundCourse.getSaleEightStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocNum(refundCourse.getRefundNum());
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setBrandNumber(refundCourse.getSaleEightNumber());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleEightStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleEightStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleEightIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleEightRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleEightRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleEightRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getAchievementAmount(), BigDecimalUtils.div(refundCourse.getSaleEightRatio(), hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCommissionAmount(), BigDecimalUtils.div(refundCourse.getSaleEightRatio(), hundredBD)));
            entities.add(achiCommEntity);
        }

        return entities;
    }

    @Override
    public int addRefundCourse(List<RefundCourseRequest> CourseListRequest, RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        for (RefundCourseRequest refundCourseRequest : CourseListRequest) {
            BaseProject baseProject = new BaseProject();
            baseProject.setId(refundCourseRequest.getCourseProjectId());
            BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());

            //校验疗程退款金额和疗程账户剩余可用次数抵扣金额
            if ("1".equals(refundCourseRequest.getCourseType())) {
                if (BigDecimalUtils.compareTo(refundCourseRequest.getCourseRefundMoney(),
                        BigDecimalUtils.div(BigDecimalUtils.multiply(refundCourseRequest.getCourseProjectMoney(), BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseConsumeCount().toString())),
                                BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseCount().toString()))) == 1) {
                    throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN, "退款金额比疗程账户剩余可用次数抵扣金额");
                }
            }else if ("0".equals(refundCourseRequest.getCourseType())){
                //判断是否存在欠款
//                if (refundCourseRequest.getCourseRestCount() > refundCourseRequest.getCourseConsumeCount() || refundCourseRequest.getCourseRestCount() == 0){

                    DebtListRequest debtListRequest = new DebtListRequest();
                    debtListRequest.setAccountId(refundCourseRequest.getCourseAccountId());
                    debtListRequest.setStoreId(refundCourseRequest.getStoreId());
                    debtListRequest.setMemberId(refundCourseRequest.getMemberId());
                    PageInfo<DebtListResponse> debtPage =  ssmDebtService.selectPage(debtListRequest);

                //存在欠款
                if (!debtPage.getList().isEmpty()&&debtPage.getList().size()>0){
                    DebtList debtList = debtPage.getList().get(0);
                    //退款金额重新计算 = 支付金额(欠款成交金额-欠款金额) - 消费金额(账户成交金额/成交次数 * 欠款中repayCount-账户剩余次数) 即成交单价*消费次数
                    BigDecimal refundMoney = BigDecimalUtils.subtract(BigDecimalUtils.subtract(debtList.getTradeMoney(), debtList.getDebtMoney()),
                            BigDecimalUtils.multiply(BigDecimalUtils.div(refundCourseRequest.getCourseProjectMoney(), BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseCount().toString())) ,
                                    BigDecimalUtils.bigDecimal(ObjectUtils.toString(debtList.getRepayCount() - refundCourseRequest.getCourseRestCount()))));
                    if (BigDecimalUtils.compareTo(refundCourseRequest.getCourseRefundMoney(), refundMoney) == 1){
                        throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN, "退款金额比疗程账户剩余金额");
                    }
                    throw new SmsException(ResponseCode.MSG,"存在欠款，请先取消欠款再退疗程");
                }else {//不存在欠款
//                    if (BigDecimalUtils.compareTo(refundCourseRequest.getCourseRefundMoney(),
//                            BigDecimalUtils.div(BigDecimalUtils.multiply(refundCourseRequest.getCourseProjectMoney(),
//                                            BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseConsumeCount().toString())),
//                                    BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseCount().toString()))) == 1) {
//                        throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN, "退款金额比疗程账户剩余可用次数抵扣金额");
//                    }
                }
            }


            RefundCourse refundCourse = new RefundCourse();
            refundCourse.setCourseRefundAmount(refundCourseRequest.getCourseRefundMoney());
            //账户 退款相关信息赋值
            BeanUtils.copyProperties(refundCourseRequest, refundCourse);
            refundCourse.setRefundNumber(refundCourse.getCourseRefundNumber());
            refundCourse.setRefundNum(refundOrderList.getRefundNum());
            refundCourse.setRefundDate(refundOrderList.getRefundDate());
            refundCourse.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            refundCourse.setSalesAchievement(baseProject1.getSalesAchievement());
            refundCourse.setSalesCommission(baseProject1.getSalesCommission());
            refundCourse.setCardSalesAchievement(baseProject1.getCardSalesAchievement());
            refundCourse.setCardSalesCommission(baseProject1.getCardSalesCommission());
            //设置业务人员
            List<StaffRequest> staffRequests = refundCourseRequest.getStaffLists();
            BigDecimal reduce3 = staffRequests.stream().map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
            if(null != reduce3 && reduce3.compareTo(BigDecimal.valueOf(100))!=0 && reduce3.compareTo(BigDecimal.valueOf(0))!=0){
                throw new SmsException(ResponseCode.MSG,"业务员业绩占比和不等于100");
            }
            this.setStaffValue(staffRequests, refundCourse);

            //计算业绩
            this.setAchiComm(refundCourse);

            try {
                i = dao.saveNotNull(refundCourse);

                if(null != refundCourseRequest.getSalesmens()){
                    //高管业绩
                    List<Salesman> salesmens = refundCourseRequest.getSalesmens();
                    for (Salesman salesmen : salesmens) {
                        salesmen.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                        salesmen.setOrderId(String.valueOf(refundCourse.getId()));
                        salesmen.setOrderNum(refundCourse.getRefundNum());
                        salesmen.setProjectId(refundCourseRequest.getCourseProjectId());
                        salesmen.setOrderType(5);
                        salesmen.setOrderDate(new Date());
                        salesmen.setState(1);
                        salesmen.setMoney(BigDecimalUtils.subtract(BigDecimal.valueOf(0.00),refundCourseRequest.getCourseRefundMoney()));
                    }
                    ssmSalesmanService.addBatch(salesmens);
                }
            } catch (Exception e) {
                log.error("SsmRefundCourseServiceImpl.addRefundCourse:", e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            }

        }
        return i;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updateRefundCourse(List<RefundCourseRequest> courseListRequest, RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        for (RefundCourseRequest refundCourseRequest : courseListRequest) {
            //校验疗程退款金额和疗程账户剩余可用次数抵扣金额
            if ("1".equals(refundCourseRequest.getCourseType())) {
                if (BigDecimalUtils.compareTo(refundCourseRequest.getCourseRefundMoney(),
                        BigDecimalUtils.div(BigDecimalUtils.multiply(refundCourseRequest.getCourseProjectMoney(), BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseConsumeCount().toString())),
                                BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseCount().toString()))) == 1) {
                    throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN, "退款金额比疗程账户剩余可用次数抵扣金额");
                }
            }else if ("0".equals(refundCourseRequest.getCourseType())){
                //判断是否存在欠款
//                if (refundCourseRequest.getCourseRestCount() > refundCourseRequest.getCourseConsumeCount() || refundCourseRequest.getCourseRestCount() == 0){
                    //由于剩余次数和可用次数都是通过金额重新计算得到的 所以存在两者相等的情况 每一个都先查询欠款单 判断欠款单是否有值
                    DebtListRequest debtListRequest = new DebtListRequest();
                    debtListRequest.setAccountId(refundCourseRequest.getCourseAccountId());
                    debtListRequest.setStoreId(refundCourseRequest.getStoreId());
                    debtListRequest.setMemberId(refundCourseRequest.getMemberId());
                    PageInfo<DebtListResponse> debtPage =  ssmDebtService.selectPage(debtListRequest);
               if (!debtPage.getList().isEmpty()){
                   DebtList debtList = debtPage.getList().get(0);
                    //退款金额重新计算 = 支付金额(欠款成交金额-欠款金额) - 消费金额(账户成交金额/成交次数 * 欠款中repayCount-账户剩余次数) 即成交单价*消费次数
                    BigDecimal refundMoney = BigDecimalUtils.subtract(BigDecimalUtils.subtract(debtList.getTradeMoney(), debtList.getDebtMoney()),
                            BigDecimalUtils.multiply(BigDecimalUtils.div(refundCourseRequest.getCourseProjectMoney(), BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseCount().toString())) ,
                                    BigDecimalUtils.bigDecimal(ObjectUtils.toString(debtList.getRepayCount() - refundCourseRequest.getCourseRestCount()))));
                    if (BigDecimalUtils.compareTo(refundCourseRequest.getCourseRefundMoney(), refundMoney) == 1){
                        throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN, "退款金额比疗程账户剩余金额");
                    }
                }else {//不存在欠款
                    if (BigDecimalUtils.compareTo(refundCourseRequest.getCourseRefundMoney(),
                            BigDecimalUtils.div(BigDecimalUtils.multiply(refundCourseRequest.getCourseProjectMoney(), BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseConsumeCount().toString())),
                                    BigDecimalUtils.bigDecimal(refundCourseRequest.getCourseCount().toString()))) == 1) {
                        throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN, "退款金额比疗程账户剩余可用次数抵扣金额");
                    }
                }
            }
            RefundCourse refundCourse = new RefundCourse();
            //账户 退款相关信息赋值
            BeanUtils.copyProperties(refundCourseRequest, refundCourse);
            refundCourse.setRefundNumber(refundCourse.getCourseRefundNumber());
//            refundCourse.setRefundNum(refundCourseRequest.getRefundNum());
            refundCourse.setRefundDate(refundOrderList.getRefundDate());

            //设置业务人员
            List<StaffRequest> staffRequests = refundCourseRequest.getStaffLists();
            BigDecimal reduce3 = staffRequests.stream().map(aa -> aa.getRatio()).reduce(BigDecimal.ZERO, BigDecimal::add);
            if(null != reduce3 && reduce3.compareTo(BigDecimal.valueOf(100))!=0 && reduce3.compareTo(BigDecimal.valueOf(0))!=0){
                throw new SmsException(ResponseCode.MSG,"业务员业绩占比和不等于100");
            }
            this.setStaffValue(staffRequests, refundCourse);
            //计算业绩
            this.setAchiComm(refundCourse);
            try {
//                refundCourse.setStoreId(null);
//                refundCourse.setStoreName(null);
//                i = dao.updateByPrimaryKeySelective(refundCourse);
                i = dao.updateById(refundCourse);
                Salesman salesman = new Salesman();
                salesman.setOrderId(String.valueOf(refundCourse.getId()));
                salesman.setOrderNum(refundCourseRequest.getRefundNum());
                salesman.setProjectId(refundCourse.getCourseProjectId());
                ssmSalesmanService.delete(salesman);

//                salesman.setOrderNum(refundCourseRequest.getRefundNum());
//                salesman.setOrderId(null);
//                 ssmSalesmanService.delete(salesman);


                BaseStore baseStore = new BaseStore();
                baseStore.setId(refundOrderList.getStoreId());
                BaseStore baseStore1 = ssmBaseStoreService.selectOne(baseStore);
                List<Salesman> salesmens = refundCourseRequest.getSalesmens();
                for (Salesman salesmen2 : salesmens) {
                    Random r = new Random(3);
                    int i1 = r.nextInt();
                    salesmen2.setId(salesmen2.getId()+i1);
                    salesmen2.setOrderId(String.valueOf(refundCourse.getId()));
                    salesmen2.setOrderNum(refundCourseRequest.getRefundNum());
                    salesmen2.setOrderType(5);
                    salesmen2.setOrderDate(new Date());
                    salesmen2.setState(1);
                    salesmen2.setProjectId(refundCourse.getCourseProjectId());
                    salesmen2.setMoney(BigDecimalUtils.subtract(BigDecimal.valueOf(0.00),refundCourseRequest.getCourseRefundMoney()));
                    salesmen2.setStoreNum(baseStore1.getStoreNum());
                    salesmen2.setStoreName(baseStore1.getStoreName());
                    salesmen2.setStoreIdOrder(baseStore1.getId());
                }
                ssmSalesmanService.addBatch(salesmens);
            } catch (Exception e) {
                log.error("SsmRefundCourseServiceImpl.updateRefundCourse:", e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            }
        }
        return i;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int audit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        RefundCourse course = new RefundCourse();
        course.setStoreId(refundOrderList.getStoreId());
        course.setRefundNum(refundOrderList.getRefundNum());
        List<RefundCourse> refundCourseList = dao.select(course);
        for (RefundCourse refundCourse : refundCourseList) {
            /*检擦是否存在欠款*/
//            DebtList debtLista = new DebtList();
//            debtLista.setAccountId(refundCourse.getCourseAccountId());
//            DebtList debtList1 = ssmDebtService.selectOne(debtLista);
//            if(null != debtList1){
//                if(debtList1.getState()==0){
//                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS, "该账户存在欠款，不支持疗程退审核，请先还款或者取消欠款");
//                }
//            }

            //解锁账户
            ssmCourseAccountService.updateState(refundCourse.getCourseAccountId(), 1);
            //账户扣减次数

            CourseAccount courseAccount = ssmCourseAccountService.selectByPrimaryKey(refundCourse.getCourseAccountId());
//            //取消会员欠款
//            if(BigDecimalUtils.compareTo(courseAccount.getCourseRestPrice(),BigDecimal.valueOf(0))==0){
//                DebtList debtList = new DebtList();
//                debtList.setAccountId(refundCourse.getCourseAccountId());
//                debtList.setStoreId(refundOrderList.getStoreId());
//                debtList.setMemberId(refundOrderList.getMemberId());
//                List<DebtList> debtLists = ssmDebtService.select(debtList);
//                for (DebtList debt : debtLists) {
//                    //有欠款时 添加退款欠款记录 更新欠款信息
//                    if (debt.getState() == 0) {
//                        RefundDebtList refundDebtList = new RefundDebtList();
//                        BeanUtils.copyProperties(debt, refundDebtList);
//                        refundDebtList.setRefundTradeId(refundCourse.getId());
//                        refundDebtList.setDebtId(debt.getId());
//                        refundDebtList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
//                        ssmRefundDebtService.saveNotNull(refundDebtList);
//                        debt.setState(1);
//                        debt.setDebtMoney(BigDecimalUtils.bigDecimal("0"));
//                        ssmDebtService.updateNotNull(debt);
//                    }
//                }
//            }
            //插入账户交易记录
            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount, refundCourse, refundOrderList);
            i = ssmAccountTradeRecordService.addRecord(accountTradeRecord);

            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(refundCourse);
            //将疗程退款单据信息添加到评价表
            MemberCsRequest memberCsRequest = new MemberCsRequest();
            BeanUtils.copyProperties(refundCourse, memberCsRequest);
            memberCsRequest.setDocId(refundOrderList.getId());
            memberCsRequest.setDocNum(refundOrderList.getRefundNum());
            memberCsRequest.setDocType(Constant.doc_type_refund_course);
            memberCsRequest.setDocDate(refundOrderList.getRefundDate());
            memberCsRequest.setTradeId(refundCourse.getCourseProjectId());
            memberCsRequest.setTradeName(refundCourse.getCourseProjectName());
            memberCsRequest.setTradeMoney(refundCourse.getCourseProjectMoney());
            StringBuffer staffIds = new StringBuffer();
            StringBuffer staffNames = new StringBuffer();
            if (entities.size() > 0) {
                for (AchiCommEntity achiCommEntity : entities) {
                    staffIds = staffIds.append(achiCommEntity.getSaleStaffId());
                    staffNames = staffNames.append(achiCommEntity.getSaleStaffName());
                }
            }
            ssmMemberCsService.addMemberCs(memberCsRequest);

            /*//计算业绩和提成
            if(refundCourse.getSalesAchievement().compareTo(BigDecimal.valueOf(1))==1){
                refundCourse.setAchievementAmount(refundCourse.getSalesAchievement());
                if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                    refundCourse.setCommissionAmount(refundCourse.getSalesCommission());
                }else{
                    refundCourse.setCommissionAmount(refundCourse.getSalesCommission().multiply(refundCourse.getAchievementAmount()));
                }
            }else{
                refundCourse.setAchievementAmount(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()));
                if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                    refundCourse.setCommissionAmount(refundCourse.getSalesCommission());
                }else{
                    refundCourse.setCommissionAmount(refundCourse.getSalesCommission().multiply(refundCourse.getAchievementAmount()));
                }
            }*/
            int update = dao.updateByPrimaryKeySelective(refundCourse);
            if(update == 0){
                throw new SmsException(ResponseCode.MSG,"审核失败");
            }
        }
        return i;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int unAudit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        RefundCourse course = new RefundCourse();
        course.setStoreId(refundOrderList.getStoreId());
        course.setRefundNum(refundOrderList.getRefundNum());
        List<RefundCourse> refundCourseList = dao.select(course);
        for (RefundCourse refundCourse : refundCourseList) {
//            DebtList debtLists = new DebtList();
//            debtLists.setAccountId(refundCourse.getCourseAccountId());
//            DebtList debtList1 = ssmDebtService.selectOne(debtLists);
//            if(null != debtList1){
//                if(debtList1.getState()==0){
//                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS, "该账户存在欠款，不支持疗程退反审核，请先还款或者取消欠款");
//                }
//            }


            //账户添加次数
            CourseAccount courseAccount = ssmCourseAccountService.addCount(refundCourse);
            //查询退款欠款记录
            RefundDebtList refundDebtList = new RefundDebtList();
            refundDebtList.setRefundTradeId(refundCourse.getId());
            List<RefundDebtList> refundDebtLists =  ssmRefundDebtService.select(refundDebtList);
            for (RefundDebtList debt : refundDebtLists) {
                //更新欠款单信息
                DebtList debtList = ssmDebtService.selectByPrimaryKey(debt.getDebtId());
                debtList.setState(0);
                debtList.setDebtMoney(debt.getDebtMoney());
                ssmDebtService.updateNotNull(debtList);
            }
            //插入账户交易记录
            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount, refundCourse, refundOrderList);
            accountTradeRecord.setOrderStatu(2);
            accountTradeRecord.setInputBy(refundOrderList.getAuditBy());
            i = ssmAccountTradeRecordService.addRecord(accountTradeRecord);
        }
        //将评价单进行删除操作
        MemberCs memberCs = new MemberCs();
        memberCs.setDocId(refundOrderList.getId());
        ssmMemberCsService.delete(memberCs);
        return i;
    }

    @Transactional(rollbackFor = Exception.class)
    public AccountTradeRecord setAccountTradeRecord(CourseAccount courseAccount, RefundCourse refundCourse, RefundOrderList refundOrderList){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(refundOrderList.getStoreId());
        accountTradeRecord.setStoreName(refundOrderList.getStoreName());
        accountTradeRecord.setMemberId(refundOrderList.getMemberId());
        accountTradeRecord.setMemberName(refundOrderList.getMemberName());
        accountTradeRecord.setMemberIdentity(refundOrderList.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(refundOrderList.getMobilePhone());
        accountTradeRecord.setAccountId(courseAccount.getId());
        accountTradeRecord.setAccountName(courseAccount.getCourseProjectName());
        if (refundOrderList.getState() == 3){//审核
            //金额记录为疗程退款金额
            accountTradeRecord.setAccountSpendMoney(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()));
            accountTradeRecord.setAccountSpendCount(refundCourse.getCourseRestCount());
            accountTradeRecord.setAccountType(Constant.account_type_refund_course_audit);
            accountTradeRecord.setInputDate(refundOrderList.getAuditDate());
            accountTradeRecord.setInputBy(refundOrderList.getAuditBy());
            accountTradeRecord.setOrderStatu(0);
        }else if (refundOrderList.getState() == 4){//反审核、
            //金额记录为疗程退款金额
            accountTradeRecord.setAccountInMoney(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()));
            accountTradeRecord.setAccountInCount(refundCourse.getCourseRestCount());
            accountTradeRecord.setAccountType(Constant.account_type_refund_course_unaudit);
            accountTradeRecord.setInputBy(refundOrderList.getAuditBy());
            accountTradeRecord.setInputDate(new Date());
            accountTradeRecord.setOrderStatu(2);
        }
        accountTradeRecord.setDocId(refundOrderList.getId());
        accountTradeRecord.setDocNum(refundOrderList.getRefundNum());
        accountTradeRecord.setDocType(Constant.doc_type_refund_course);
        accountTradeRecord.setTradeId(refundCourse.getCourseProjectId());
        accountTradeRecord.setTradeName(refundCourse.getCourseProjectName());
        accountTradeRecord.setTradeMoney(refundCourse.getCourseRefundMoney());
        accountTradeRecord.setTradeCount(refundCourse.getRefundNumber());
        if(refundCourse.getCourseRefundMoney().compareTo(BigDecimal.valueOf(0))!=0){
            if(refundCourse.getRefundNumber()>0){
                accountTradeRecord.setPrice(refundCourse.getCourseRefundMoney().divide(BigDecimal.valueOf(refundCourse.getRefundNumber()),2,BigDecimal.ROUND_HALF_UP));
            }
        }
        if ("0".equals(courseAccount.getCourseType())) {
            accountTradeRecord.setAccountRestCount(courseAccount.getCourseRestCount());
            accountTradeRecord.setAccountConsumeCount(courseAccount.getCourseConsumeCount());
        }else {
            accountTradeRecord.setAccountGiftRestCount(courseAccount.getCourseRestCount());
            accountTradeRecord.setAccountGiftConsumeCount(courseAccount.getCourseConsumeCount());
        }
        accountTradeRecord.setAccountRestMoney(courseAccount.getCourseRestPrice());
        accountTradeRecord.setAccountConsumeMoney(courseAccount.getCourseConsumePrice());

        accountTradeRecord.setCardMoney(refundOrderList.getRefundMoney());
        accountTradeRecord.setCashMoney(BigDecimal.valueOf(0));
        return accountTradeRecord;
    }

    @Override
    public List<RefundCourseResponse> selectList(Long storeId, String date) {
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        refundOrderResquest.setDateStr(date);
        refundOrderResquest.setState(3);
        List<RefundCourse> list = dao.selectList(refundOrderResquest);
        List<RefundCourseResponse> responseList = new ArrayList<>();
        for (RefundCourse course : list) {
            RefundCourseResponse courseResponse = new RefundCourseResponse();
            BeanUtils.copyProperties(course, courseResponse);
            //设置业务员
            course.setRefundDate(course.getAuditDate());
            List<AchiCommEntity> entities = this.getAchiCommListNew(course, course.getCourseType());
            for (AchiCommEntity entity : entities) {
                entity.setDocId(course.getId());
                entity.setDocNum(course.getRefundNum());
                BaseProject baseProject = new BaseProject();
                baseProject.setId(course.getCourseProjectId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                entity.setProjectId(baseProject1.getProjectNum());

            }
            //设置返回List
            courseResponse.setEntities(entities);

            responseList.add(courseResponse);
        }
        return responseList;
    }

    @Override
    public List<RefundCourse> selectListByStartTimeAndEndTime(Long storeId, Date startTime,Date endTime) {
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
//        refundOrderResquest.setDateStr(date);
        refundOrderResquest.setStartTime(startTime);
        refundOrderResquest.setEndTime(endTime);

        refundOrderResquest.setState(3);
        List<RefundCourse> list = dao.selectList(refundOrderResquest);
        return list;
    }

    @Override
    public List<RefundCourseResponse> selectListDept(Long storeId, String date) {
//        Example example = new Example(RefundCourse.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andLike("refundDate",date + "%");
//        criteria.andEqualTo("storeId", storeId);
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        refundOrderResquest.setDateStr(date);
        refundOrderResquest.setState(3);
        List<RefundCourse> list = dao.selectList(refundOrderResquest);
        List<RefundCourseResponse> responseList = new ArrayList<>();
        for (RefundCourse course : list) {
            RefundCourseResponse courseResponse = new RefundCourseResponse();
            BeanUtils.copyProperties(course, courseResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(course, course.getCourseType());
            for (AchiCommEntity entity : entities) {
                entity.setDocId(course.getId());
                entity.setDocNum(course.getRefundNum());
                BaseProject baseProject = new BaseProject();
                baseProject.setId(course.getCourseProjectId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                entity.setProjectId(baseProject1.getProjectNum());

            }
            //设置返回List
            courseResponse.setEntities(entities);

            responseList.add(courseResponse);
        }
        return responseList;
    }

    @Override
    public List<RefundCourseResponse> selectListschia(Long storeId,Date startDate,Date endDate) {
//        Example example = new Example(RefundCourse.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andLike("refundDate",date + "%");
//        criteria.andEqualTo("storeId", storeId);
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        refundOrderResquest.setStartTime(startDate);
        refundOrderResquest.setEndTime(endDate);
        List<RefundCourse> list = dao.selectListsBetween(refundOrderResquest);
        List<RefundCourseResponse> responseList = new ArrayList<>();
        for (RefundCourse course : list) {
            RefundCourseResponse courseResponse = new RefundCourseResponse();
            BeanUtils.copyProperties(course, courseResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(course, course.getCourseType());
            for (AchiCommEntity entity : entities) {
                entity.setSaleAchievementAmount(BigDecimal.valueOf(0).subtract(entity.getSaleAchievementAmount()));
                entity.setSaleCommissionAmount(BigDecimal.valueOf(0).subtract(entity.getSaleCommissionAmount()));
                entity.setCashComm(BigDecimal.valueOf(0).subtract(entity.getCashComm()));
                entity.setCardComm(BigDecimal.valueOf(0).subtract(entity.getCardComm()));
                entity.setCashComm(BigDecimal.valueOf(0).subtract(entity.getCashComm()));
                entity.setCashAchi(BigDecimal.valueOf(0).subtract(entity.getCashAchi()));
                entity.setCardAchi(BigDecimal.valueOf(0).subtract(entity.getCardAchi()));
                entity.setTotalCommission(BigDecimal.valueOf(0).subtract(entity.getTotalCommission()));
                entity.setTotalPerformance(BigDecimal.valueOf(0).subtract(entity.getTotalPerformance()));
                entity.setDocNum(course.getRefundNum());
                BaseProject baseProject = new BaseProject();
                baseProject.setId(course.getCourseProjectId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                entity.setProjectId(baseProject1.getProjectNum());

            }
            //设置返回List
            courseResponse.setEntities(entities);

            responseList.add(courseResponse);
        }
        return responseList;
    }

    @Override
    public void sendAudit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        RefundCourse course = new RefundCourse();
        course.setStoreId(refundOrderList.getStoreId());
        course.setRefundNum(refundOrderList.getRefundNum());
        List<RefundCourse> refundCourseList = dao.select(course);
        for (RefundCourse refundCourse : refundCourseList) {
            /*检擦是否存在欠款*/
//            DebtList debtLista = new DebtList();
//            debtLista.setAccountId(refundCourse.getCourseAccountId());
//            DebtList debtList1 = ssmDebtService.selectOne(debtLista);
//            if(null != debtList1){
//                if(debtList1.getState()==0){
//                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS, "该账户存在欠款，不支持疗程退审核，请先还款或者取消欠款");
//                }
//            }

            //账户扣减次数
            //判断订单状态，回收送审不扣次数
            CourseAccount courseAccount = ssmCourseAccountService.subCount(refundCourse);
            //取消会员欠款
            if(BigDecimalUtils.compareTo(courseAccount.getCourseRestPrice(),BigDecimal.valueOf(0))==0){
                DebtList debtList = new DebtList();
                debtList.setAccountId(refundCourse.getCourseAccountId());
                debtList.setStoreId(refundOrderList.getStoreId());
                debtList.setMemberId(refundOrderList.getMemberId());
                List<DebtList> debtLists = ssmDebtService.select(debtList);
                for (DebtList debt : debtLists) {
                    //有欠款时 添加退款欠款记录 更新欠款信息
                    if (debt.getState() == 0) {
                        RefundDebtList refundDebtList = new RefundDebtList();
                        BeanUtils.copyProperties(debt, refundDebtList);
                        refundDebtList.setRefundTradeId(refundCourse.getId());
                        refundDebtList.setDebtId(debt.getId());
                        refundDebtList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                        ssmRefundDebtService.saveNotNull(refundDebtList);
                        debt.setState(1);
                        debt.setDebtMoney(BigDecimalUtils.bigDecimal("0"));
                        ssmDebtService.updateNotNull(debt);
                    }
                }
            }
        }
    }

    @Override
    public void revokeAudit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        RefundCourse course = new RefundCourse();
        course.setStoreId(refundOrderList.getStoreId());
        course.setRefundNum(refundOrderList.getRefundNum());
        List<RefundCourse> refundCourseList = dao.select(course);
        for (RefundCourse refundCourse : refundCourseList) {
//            DebtList debtLists = new DebtList();
//            debtLists.setAccountId(refundCourse.getCourseAccountId());
//            DebtList debtList1 = ssmDebtService.selectOne(debtLists);
//            if(null != debtList1){
//                if(debtList1.getState()==0){
//                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS, "该账户存在欠款，不支持疗程退反审核，请先还款或者取消欠款");
//                }
//            }


            //账户添加次数
            CourseAccount courseAccount = ssmCourseAccountService.addCount(refundCourse);
            //查询退款欠款记录
            RefundDebtList refundDebtList = new RefundDebtList();
            refundDebtList.setRefundTradeId(refundCourse.getId());
            List<RefundDebtList> refundDebtLists =  ssmRefundDebtService.select(refundDebtList);
            for (RefundDebtList debt : refundDebtLists) {
                //更新欠款单信息
                DebtList debtList = ssmDebtService.selectByPrimaryKey(debt.getDebtId());
                debtList.setState(0);
                debtList.setDebtMoney(debt.getDebtMoney());
                ssmDebtService.updateNotNull(debtList);
            }
            //插入账户交易记录
        }
        //将评价单进行删除操作
    }

    @Override
    public List<RefundCourse> selectListNew(RefundCourseRequest refundCourseRequest) {
        return mapper.selectListNew(refundCourseRequest);
    }


    @Override
    public List<RefundCourseResponse> selectLists(Long storeId, String startDate,String endDate) {
//        Example example = new Example(RefundCourse.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andLike("refundDate",date + "%");
//        criteria.andEqualTo("storeId", storeId);
        RefundOrderResquest refundOrderResquest = new RefundOrderResquest();
        refundOrderResquest.setStoreId(storeId);
        try {
            refundOrderResquest.setStartTime(DateUtils.YMDParse(startDate));
            refundOrderResquest.setEndTime(DateUtils.YMDParse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        refundOrderResquest.setState(3);
        List<RefundCourse> list = dao.selectLists(refundOrderResquest);
        List<RefundCourseResponse> responseList = new ArrayList<>();
        for (RefundCourse course : list) {
            RefundCourseResponse courseResponse = new RefundCourseResponse();
            BeanUtils.copyProperties(course, courseResponse);
            //设置业务员
            List<AchiCommEntity> entities = this.getAchiCommList(course, course.getCourseType());

            //设置返回List
            courseResponse.setEntities(entities);

            responseList.add(courseResponse);
        }
        return responseList;
    }

    public List<AchiCommEntity> getAchiCommList(RefundCourse refundCourse, String type){
        List<AchiCommEntity> entities = new ArrayList<>();
        refundCourse.setCourseRefundMoney(refundCourse.getCourseRefundMoney().negate());
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");
        //促销退款业绩提成也进行计算
//        if ("0".equals(type)) {
            if (null != refundCourse.getSaleOneStaffId()) {
                AchiCommEntity achiCommEntity = new AchiCommEntity();
                achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
                achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
                achiCommEntity.setMemberName(refundCourse.getMemberName());
                achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
                achiCommEntity.setDocType(Constant.doc_type_refund_course);
                achiCommEntity.setDocDate(refundCourse.getRefundDate());
                achiCommEntity.setBrandNumber(refundCourse.getSaleOneNumber());
                achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
                achiCommEntity.setSaleStaffId(refundCourse.getSaleOneStaffId());
                achiCommEntity.setSaleStaffName(refundCourse.getSaleOneStaffName());
                achiCommEntity.setSaleIdentityCard(refundCourse.getSaleOneIdentity());
                achiCommEntity.setSaleRatio(refundCourse.getSaleOneRatio());
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
                if(null == achiCommEntity.getCashAchi()){
                    achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
                }
                if(null == achiCommEntity.getCardAchi() ){
                    achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
                }
                if(null == achiCommEntity.getCashComm()){
                    achiCommEntity.setCashComm(BigDecimal.valueOf(0));
                }
                if(null == achiCommEntity.getCardComm()){
                    achiCommEntity.setCardComm(BigDecimal.valueOf(0));
                }
                achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
                achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

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

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

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

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

    public List<AchiCommEntity> getAchiCommListNew(RefundCourse refundCourse, String type){
        List<AchiCommEntity> entities = new ArrayList<>();
        refundCourse.setCourseRefundMoney(refundCourse.getCourseRefundMoney().negate());
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");
        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(refundCourse.getCourseProjectId());
        BigDecimal comm = BigDecimal.ZERO;
        //判断是否是合作项目
        if(baseProject.getSmokeCommissionWay()!=null && baseProject.getSmokeCommissionWay()==0){
            comm =baseProject.getBrokerage().divide(
                    (baseProject.getPrice().compareTo(BigDecimal.ZERO)==0?baseProject.getPriceMember():baseProject.getPrice())
                            .multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())),10,BigDecimal.ROUND_HALF_UP).multiply(refundCourse.getCourseRefundMoney());
            refundCourse.setCourseRefundMoney(refundCourse.getCourseRefundMoney()
                    .subtract(baseProject.getBrokerage().divide(
                            (baseProject.getPrice().compareTo(BigDecimal.ZERO)==0?baseProject.getPriceMember():baseProject.getPrice())
                                    .multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())),10,BigDecimal.ROUND_HALF_UP).multiply(refundCourse.getCourseRefundMoney())));

        }

        if(baseProject.getSmokeCommissionWay()!=null && baseProject.getSmokeCommissionWay()==1){
            comm = baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(refundCourse.getCourseRefundMoney());
            refundCourse.setCourseRefundMoney(refundCourse.getCourseRefundMoney()
                    .subtract(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(refundCourse.getCourseRefundMoney())));
        }
        if(refundCourse.getSalesCommission().compareTo(new BigDecimal("1"))==1){
            refundCourse.setSalesCommission(refundCourse.getSalesCommission().divide(refundCourse.getCourseRefundMoney(),10,BigDecimal.ROUND_HALF_UP));
        }
        //促销退款业绩提成也进行计算
//        if ("0".equals(type)) {
        if (null != refundCourse.getSaleOneStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setBrandNumber(refundCourse.getSaleOneNumber());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleOneStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleOneStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleOneIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleOneRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney()
                    .multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
//            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
            if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getSalesCommission(), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
            }else {

                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
            }
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundCourse.getSaleTwoStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setBrandNumber(refundCourse.getSaleTwoNumber());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleTwoStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleTwoStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleTwoIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleTwoRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
            if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getSalesCommission(), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
            }else {

                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleTwoRatio(), hundredBD)));
            }
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundCourse.getSaleThreeStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setBrandNumber(refundCourse.getSaleThreeNumber());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleThreeStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleThreeStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleThreeIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleThreeRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
            if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getSalesCommission(), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
            }else {

                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleThreeRatio(), hundredBD)));
            }
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

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

            entities.add(achiCommEntity);
        }

        if (null != refundCourse.getSaleFiveStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setBrandNumber(refundCourse.getSaleOneNumber());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleFiveStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleFiveStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleFiveIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleFiveRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
//            achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleFiveRatio(), hundredBD)));
            if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getSalesCommission(), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
            }else {

                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleOneRatio(), hundredBD)));
            }
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundCourse.getSaleSixStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setBrandNumber(refundCourse.getSaleSixNumber());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleSixStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleSixStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleSixIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleSixRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getSalesCommission(), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            }else {

                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleSixRatio(), hundredBD)));
            }
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != refundCourse.getSaleSevenStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(refundCourse.getCourseRefundMoney());
            achiCommEntity.setMemberNum(String.valueOf(refundCourse.getMemberId()));
            achiCommEntity.setMemberName(refundCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(refundCourse.getCourseProjectId()));
            achiCommEntity.setDocType(Constant.doc_type_refund_course);
            achiCommEntity.setDocDate(refundCourse.getRefundDate());
            achiCommEntity.setBrandNumber(refundCourse.getSaleSevenNumber());
            achiCommEntity.setTradeName(refundCourse.getCourseProjectName());
            achiCommEntity.setSaleStaffId(refundCourse.getSaleSevenStaffId());
            achiCommEntity.setSaleStaffName(refundCourse.getSaleSevenStaffName());
            achiCommEntity.setSaleIdentityCard(refundCourse.getSaleSevenIdentity());
            achiCommEntity.setSaleRatio(refundCourse.getSaleSevenRatio());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            achiCommEntity.setCashAchi(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesAchievement()), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            if(refundCourse.getSalesCommission().compareTo(BigDecimal.valueOf(1))==1){
                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getSalesCommission(), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            }else {

                achiCommEntity.setCashComm(BigDecimalUtils.multiply(refundCourse.getCourseRefundMoney().multiply(refundCourse.getSalesCommission()), BigDecimalUtils.div(refundCourse.getSaleSevenRatio(), hundredBD)));
            }
            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

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

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

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

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

            }else if (i == 4){
                refundCourse.setSaleFiveStaffId(staffRequest.getId());
                refundCourse.setSaleFiveStaffName(staffRequest.getStaffName());
                refundCourse.setSaleFiveIdentity(staffRequest.getIdentityCard());
                refundCourse.setSaleFiveRatio(staffRequest.getRatio());
                refundCourse.setSaleFiveNumber(staffRequest.getBrandNumber());
            }
            else if (i == 5){
                refundCourse.setSaleSixStaffId(staffRequest.getId());
                refundCourse.setSaleSixStaffName(staffRequest.getStaffName());
                refundCourse.setSaleSixIdentity(staffRequest.getIdentityCard());
                refundCourse.setSaleSixRatio(staffRequest.getRatio());
                refundCourse.setSaleSixNumber(staffRequest.getBrandNumber());
            }
            else if (i == 6){
                refundCourse.setSaleSevenStaffId(staffRequest.getId());
                refundCourse.setSaleSevenStaffName(staffRequest.getStaffName());
                refundCourse.setSaleSevenIdentity(staffRequest.getIdentityCard());
                refundCourse.setSaleSevenRatio(staffRequest.getRatio());
                refundCourse.setSaleSevenNumber(staffRequest.getBrandNumber());
            }
            else if (i == 7){
                refundCourse.setSaleEightStaffId(staffRequest.getId());
                refundCourse.setSaleEightStaffName(staffRequest.getStaffName());
                refundCourse.setSaleEightIdentity(staffRequest.getIdentityCard());
                refundCourse.setSaleEightRatio(staffRequest.getRatio());
                refundCourse.setSaleEightNumber(staffRequest.getBrandNumber());
            }
        }
    }

    public RefundCourse setAchiComm(RefundCourse refundCourse) {
        BaseProject project = new BaseProject();
        DocPromotion docPromotion = new DocPromotion();
        BigDecimal commRatio = BigDecimalUtils.bigDecimal("0");
        BigDecimal achiRatio = BigDecimalUtils.bigDecimal("0");
        CourseAccount courseAccount = ssmCourseAccountService.selectByPrimaryKey(refundCourse.getCourseAccountId());

        if ("0".equals(refundCourse.getCourseType())|| "3".equals(refundCourse.getCourseType()) || "4".equals(refundCourse.getCourseType())){
            project = ssmProjectService.selectByPrimaryKey(refundCourse.getCourseProjectId());
            //提成标准
            commRatio = project.getSalesCommission();
            //业绩标准
            achiRatio = project.getSalesAchievement();
            //合作项目抽佣比例
            BigDecimal cooperationRatio = project.getBrokerage();
            //正常项目
            if (project.getIsCooperation() == 0) {
                //业绩
                if (BigDecimalUtils.compareTo(achiRatio, BigDecimalUtils.bigDecimal("1")) != 1) {
                    refundCourse.setAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()), achiRatio));
                } else {
                    //固定金额 ((退款金额 + 赔偿金额) / 成交价) * 业绩标准 * 可用次数
                    refundCourse.setAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.multiply(
                            BigDecimalUtils.div(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()),
                                    courseAccount.getCourseProjectMoney()), achiRatio), BigDecimalUtils.bigDecimal(courseAccount.getCourseConsumeCount().toString())));
                }
                //提成
                if (BigDecimalUtils.compareTo(commRatio, BigDecimalUtils.bigDecimal("1")) != 1) {

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

                } else {
                    //固定金额 ((退款金额 + 赔偿金额) / 成交价) * 提成标准 * 可用次数
                    refundCourse.setCommissionAmount(BigDecimalUtils.multiply(BigDecimalUtils.multiply(
                            BigDecimalUtils.div(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()),
                                    courseAccount.getCourseProjectMoney()), commRatio), BigDecimalUtils.bigDecimal(courseAccount.getCourseConsumeCount().toString())));
                }
            }else {
                //合作项目
                //业绩
                if(project.getSmokeCommissionWay()==1) {//合作按比例
                    if (BigDecimalUtils.compareTo(achiRatio, BigDecimalUtils.bigDecimal("1")) != 1) {
                        refundCourse.setAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()), achiRatio));
                    } else {
                        //固定金额 ((退款金额 + 赔偿金额) / 成交价) * 业绩标准 * 可用次数
                        refundCourse.setAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.multiply(
                                BigDecimalUtils.div(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()),
                                        courseAccount.getCourseProjectMoney()), achiRatio), BigDecimalUtils.bigDecimal(courseAccount.getCourseConsumeCount().toString())));
                    }
                    //现付业绩-合作业绩（现付业绩*合作抽佣比例）
                    refundCourse.setAchievementAmount(refundCourse.getAchievementAmount().subtract(refundCourse.getAchievementAmount().multiply(cooperationRatio)));
                }else{//合作按固定
                    if (BigDecimalUtils.compareTo(achiRatio, BigDecimalUtils.bigDecimal("1")) != 1) {
                        refundCourse.setAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()), achiRatio));
                    } else {
                        //固定金额 ((退款金额 + 赔偿金额) / 成交价) * 业绩标准 * 可用次数
                        refundCourse.setAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.multiply(
                                BigDecimalUtils.div(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()),
                                        courseAccount.getCourseProjectMoney()), achiRatio), BigDecimalUtils.bigDecimal(courseAccount.getCourseConsumeCount().toString())));

                    }
                    //现付业绩-合作固定抽佣
                    refundCourse.setAchievementAmount(refundCourse.getAchievementAmount().subtract(cooperationRatio));
                }

                //提成
                if (BigDecimalUtils.compareTo(commRatio, BigDecimalUtils.bigDecimal("1")) != 1) {

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

                } else {
                    //固定金额 ((退款金额 + 赔偿金额) / 成交价) * 提成标准 * 可用次数
                    refundCourse.setCommissionAmount(BigDecimalUtils.multiply(BigDecimalUtils.multiply(
                            BigDecimalUtils.div(BigDecimalUtils.multiply(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()),cooperationRatio),
                                    courseAccount.getCourseProjectMoney()), commRatio), BigDecimalUtils.bigDecimal(courseAccount.getCourseConsumeCount().toString())));
                }
            }
        }else {
            //促销查询
            docPromotion.setDocNum(courseAccount.getCourseNum());
            docPromotion.setStoreId(courseAccount.getStoreId());
            List<DocPromotion> list = ssmDocPromotionService.select(docPromotion);
            //提成标准
            if (BigDecimalUtils.compareTo(list.get(0).getCollectionCashAmount(), BigDecimalUtils.bigDecimal("0")) == 1){
                commRatio = list.get(0).getSalesCommission();
            }else {
                commRatio = list.get(0).getCardSalesCommission();
            }
            refundCourse.setAchievementAmount(BigDecimalUtils.add(refundCourse.getCourseRefundMoney(), refundCourse.getCourseCompensation()));
            refundCourse.setSalesCommission(commRatio);
            //提成
            if (BigDecimalUtils.compareTo(commRatio, BigDecimalUtils.bigDecimal("1")) != 1) {

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

            } else {
                DocPromotionProject docPromotionProject = new DocPromotionProject();
                docPromotionProject.setDocNum(list.get(0).getDocNum());
//                docPromotionProject.setStoreId(list.get(0).getStoreId());
                docPromotionProject.setProjectId(refundCourse.getCourseProjectId());
                List<DocPromotionProject> docPromotionProjects = ssmDocPromotionProjectService.select(docPromotionProject);
                refundCourse.setCommissionAmount(BigDecimalUtils.multiply(BigDecimalUtils.div(refundCourse.getAchievementAmount(),
                        docPromotionProjects.get(0).getProjectTotalMoney().compareTo(BigDecimal.valueOf(0))==0?BigDecimal.valueOf(1):docPromotionProjects.get(0).getProjectTotalMoney()) , commRatio));
            }
        }
//        refundCourse.setSalesCommission(commRatio);
//        refundCourse.setSalesAchievement(project.getSalesAchievement());


        return refundCourse;
    }
}
