package com.ssm.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.core.utils.StoreParaUtils;
import com.ssm.manage.dao.SsmRechargeDao;
import com.ssm.manage.dao.SsmRechargeTurnCourseDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.mapper.RechargeTurnCourseMapper;
import com.ssm.manage.query.*;
import com.ssm.manage.response.RechargeTurnCoursesResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import com.ssm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
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.util.Base64Utils;
import tk.mybatis.mapper.entity.Example;

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

@Service
@Slf4j
public class SsmRechargeTurnCourseServiceImpl  extends BaseServiceImpl<RechargeTurnCourse> implements SsmRechargeTurnCourseService {

    private static String staffKey = "staffKey";

    @Autowired
    private SsmRechargeTurnCourseDao dao;

    @Autowired
    private SsmCoursePayListService ssmCoursePayListService;

    @Autowired
    private SsmDebtService ssmDebtService;
    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmRechargeTypeSevice ssmRechargeTypeSevice;

    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;
    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;

    @Autowired
    private RedisClient<StaffResponse> redisClient;

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


    @Override
    @Transactional(rollbackFor=Exception.class)
    public int add(RechargeTurnCourseRequest rechargeTurnCourseRequest,String staffPhone) throws SmsException {

        if(rechargeTurnCourseRequest.getRechargeInMoney().compareTo(rechargeTurnCourseRequest.getCourseOutMoney())!=0){
            throw new SmsException(ResponseCode.MSG,"疗程转出总金额与充值卡转入金额不相等");
        }
        BigDecimal rollOffMoney  =  BigDecimal.valueOf(0);
        /*for (CoursePayList coursePayList : rechargeTurnCourseRequest.getCoursePayLists()) {
            rollOffMoney=rollOffMoney.add(coursePayList.getRollOffMoney());
        }*/
        for (CoursePayListRequest coursePayList : rechargeTurnCourseRequest.getCoursePayListRequests()) {
            rollOffMoney=rollOffMoney.add(coursePayList.getRollOffMoney());
        }
        if(rollOffMoney.compareTo(rechargeTurnCourseRequest.getCourseOutMoney())!=0){
            throw new SmsException(ResponseCode.MSG,"疗程转出列表金额总和与疗程转出总金额");
        }
        StaffResponse staffResponse = new StaffResponse();
        Set<String> keys = redisClient.keys(getStaffKey(staffPhone) + "*");
        //从redis查询 设置录单人信息 app使用
        if (keys.size() > 0){
            String key =  new ArrayList<>(keys).get(0);
            staffResponse = redisClient.get(key);
        }
        //创建SimpleDateFormat对象实例并定义好转换格式
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
        String formats = sdfs.format(new Date());
        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(rechargeTurnCourseRequest.getStoreId());
        String num = this.getNum(formats,baseStore.getStoreNum(), rechargeTurnCourseRequest.getStoreId());

        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        BeanUtils.copyProperties(rechargeTurnCourseRequest, rechargeTurnCourse);
        rechargeTurnCourse.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        rechargeTurnCourse.setState(0);
        rechargeTurnCourse.setDocNum(num);
        //是否补单
        if(rechargeTurnCourseRequest.getIsAddition()==0){
            rechargeTurnCourse.setDocDate(new Date());
        }else{
            rechargeTurnCourse.setDocDate(rechargeTurnCourseRequest.getDocDate());
        }
        rechargeTurnCourse.setInputId(staffResponse.getId());
        rechargeTurnCourse.setInputBy(staffResponse.getStaffName());
        rechargeTurnCourse.setInputDate(new Date());
        BigDecimal reduce3 = rechargeTurnCourseRequest.getStaffLists().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(rechargeTurnCourseRequest.getStaffLists(),rechargeTurnCourse);
        //获取设置充值类别的提成
        RechargeType rechargeType = new RechargeType();
        rechargeType.setId(rechargeTurnCourseRequest.getRechargeTypeId());
        RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
        rechargeTurnCourse.setTypeRatio(rechargeType1.getTypeRatio());
        int insert = dao.insert(rechargeTurnCourse);
        if(insert>0){
            int account=0;
            /*for (CoursePayList coursePayList : rechargeTurnCourseRequest.getCoursePayLists()) {
                account++;
//                DebtList debtList = new DebtList();
//                debtList.setAccountId(coursePayList.getCourseAccountId());
//                debtList.setState(0);
//                List<DebtList> select = ssmDebtService.selectLists(debtList);
//                if(select.size()>0){
//                    throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+coursePayList.getCourseProjectName()+"】存在欠款，请先取消欠款或者还款");
//                }
                BaseProject baseProject = new BaseProject();
                baseProject.setId(Long.valueOf(coursePayList.getCourseProjectId()));
                BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());
                coursePayList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                coursePayList.setRechargeTurnCourseId(rechargeTurnCourse.getId());
                coursePayList.setDocNum(rechargeTurnCourse.getDocNum());
                coursePayList.setStoreId(rechargeTurnCourse.getStoreId());
                coursePayList.setStoreName(rechargeTurnCourse.getStoreName());
                coursePayList.setSalesAchievement(baseProject1.getSalesAchievement());
                coursePayList.setSalesCommission(baseProject1.getSalesCommission());
                coursePayList.setCardSalesAchievement(baseProject1.getCardSalesAchievement());
                coursePayList.setCardSalesCommission(baseProject1.getCardSalesCommission());
                int i = ssmCoursePayListService.saveNotNull(coursePayList);
                if(i==0){
                    throw new SmsException(ResponseCode.MSG,"保存疗程转出记录异常");
                }
            }*/
            int i = ssmCoursePayListService.insert(rechargeTurnCourseRequest.getCoursePayListRequests(), rechargeTurnCourse);
            if(i==0){
                throw new SmsException(ResponseCode.MSG,"保存疗程转出记录异常");
            }
        }
        //添加充值卡金额
        if(null != rechargeTurnCourse.getRechargeAccountId()){
            RechargeAccount rechargeAccountRequest = new RechargeAccount();
            rechargeAccountRequest.setId(rechargeTurnCourse.getRechargeAccountId());
            RechargeAccount rechargeAccount = ssmRechargeAccountService.selectOne(rechargeAccountRequest);
            rechargeTurnCourse.setBeforeRestMoney(rechargeAccount.getCardRestMoney());
            rechargeTurnCourse.setBeforeConsumeMoney(rechargeAccount.getCardConsumeMoney());
            rechargeTurnCourse.setBeforeGiftRestMoney(rechargeAccount.getCardGiftMoney());
            rechargeTurnCourse.setBeforeGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());

            if(rechargeAccount.getCardRestMoney().compareTo(rechargeTurnCourse.getRechargeBeforeMoney())!=0){
                throw new SmsException(ResponseCode.MSG,"充值卡转入前剩余金额不相等");
            }
            rechargeAccount.setCardConsumeMoney(rechargeAccount.getCardConsumeMoney().add(rechargeTurnCourse.getRechargeInMoney()));
            rechargeAccount.setCardRestMoney(rechargeAccount.getCardRestMoney().add(rechargeTurnCourse.getRechargeInMoney()));
            RechargeAccount rechargeAccount1 = ssmRechargeAccountService.addMoney(rechargeAccount);

            rechargeTurnCourse.setBackRestMoney(rechargeAccount1.getCardRestMoney());
            rechargeTurnCourse.setBackConsumeMoney(rechargeAccount1.getCardConsumeMoney());
            rechargeTurnCourse.setBackGiftRestMoney(rechargeAccount1.getCardGiftMoney());
            rechargeTurnCourse.setBackGiftConsumeMoney(rechargeAccount1.getGiftConsumeMoney());

            //添加充值卡交易记录
            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeTurnCourse, rechargeAccount1);
            ssmAccountTradeRecordService.addRecord(accountTradeRecord);
        }
        else{
            StoreMemberList storeMemberList = new StoreMemberList();
            storeMemberList.setId(rechargeTurnCourse.getMemberId());
            StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

            RechargeType rechargeTypes = new RechargeType();
            rechargeTypes.setId(rechargeTurnCourse.getRechargeTypeId());
            RechargeType rechargeTypes1 = ssmRechargeTypeSevice.selectOne(rechargeTypes);

            RechargeAccount rechargeAccount2 = new RechargeAccount();
            rechargeAccount2.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            rechargeAccount2.setMemberId(storeMemberList1.getId());
            rechargeAccount2.setMemberName(storeMemberList1.getMemberName());
            rechargeAccount2.setMemberName(storeMemberList1.getMemberName());
            rechargeAccount2.setCardRestMoney(rechargeTurnCourse.getRechargeInMoney());
            rechargeAccount2.setCardConsumeMoney(rechargeTurnCourse.getRechargeInMoney());
            rechargeAccount2.setGiftConsumeMoney(BigDecimal.valueOf(0));
            rechargeAccount2.setCardGiftMoney(BigDecimal.valueOf(0));
            rechargeAccount2.setRechargeTypeId(rechargeTypes1.getId());
            rechargeAccount2.setRechargeTypeNum(rechargeTypes1.getRechargeTypeNum());
            rechargeAccount2.setRechargeTypeName(rechargeTypes1.getRechargeTypeName());
            rechargeAccount2.setType(rechargeTypes1.getType());
            rechargeAccount2.setDiscount(rechargeTypes1.getDiscount());
            rechargeAccount2.setDiscount(rechargeTypes1.getDiscount());
            rechargeAccount2.setStoreId(rechargeTypes1.getStoreId());
            rechargeAccount2.setStoreName(rechargeTypes1.getStoreName());
            rechargeAccount2.setState(1);
//            if(null != rechargeTypes1.getValidTime()){
//                rechargeAccount2.setEndTime(rechargeAccount1.getEndTime());
//            }
            rechargeAccount2.setFirstCharge(0L);
            rechargeAccount2.setCardNum(0);
            rechargeTurnCourse.setBeforeRestMoney(BigDecimal.valueOf(0));
            rechargeTurnCourse.setBeforeConsumeMoney(BigDecimal.valueOf(0));
            rechargeTurnCourse.setBeforeGiftRestMoney(BigDecimal.valueOf(0));
            rechargeTurnCourse.setBeforeGiftConsumeMoney(BigDecimal.valueOf(0));

            rechargeTurnCourse.setBackRestMoney(rechargeAccount2.getCardRestMoney());
            rechargeTurnCourse.setBackConsumeMoney(rechargeAccount2.getCardConsumeMoney());
            rechargeTurnCourse.setBackGiftRestMoney(rechargeAccount2.getCardGiftMoney());
            rechargeTurnCourse.setBackGiftConsumeMoney(rechargeAccount2.getGiftConsumeMoney());

            int save = ssmRechargeAccountService.save(rechargeAccount2);
            //充值卡开卡插入记录
            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeTurnCourse, rechargeAccount2);
            ssmAccountTradeRecordService.addRecord(accountTradeRecord);
            rechargeTurnCourse.setRechargeAccountId(rechargeAccount2.getId());

            if(save==0){
                throw new SmsException(ResponseCode.MSG,"添加转入会员卡失败");
            }
        }


        //扣减疗程卡余额和单价重算和剩余次数(转可用的，如果需要全部转，需要取消欠款)
        CoursePayList coursePayList = new CoursePayList();
        coursePayList.setDocNum(rechargeTurnCourse.getDocNum());
        coursePayList.setRechargeTurnCourseId(rechargeTurnCourse.getId());
        List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
        int account=0;
        for (CoursePayList payList : select) {
            account++;
            CourseAccount courseAccount = new CourseAccount();
            courseAccount.setId(payList.getCourseAccountId());
            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
            payList.setBeforePrice(courseAccount1.getPrice());
            payList.setBeforeRestMoney(courseAccount1.getCourseRestPrice());
            payList.setBeforeConsumeMoney(courseAccount1.getCourseConsumePrice());
            payList.setBeforeRestCount(courseAccount1.getCourseRestCount());
            payList.setBeforeConsumeCount(courseAccount1.getCourseConsumeCount());

            if(courseAccount1.getCourseConsumePrice().compareTo(payList.getCourseBeforeUsableMoney())!=0){
                throw new SmsException(ResponseCode.MSG,"疗程转出前可用金额不相等");
            }
//            DebtList debtList = new DebtList();
//            debtList.setAccountId(payList.getCourseAccountId());
//            debtList.setState(0);
//            List<DebtList> selects = ssmDebtService.selectLists(debtList);
//            if(selects.size()>0){
//                throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+coursePayList.getCourseProjectName()+"】存在欠款，请先取消欠款或者还款");
//            }
            //校验疗程账户是否足够可用金额可扣
            if(courseAccount1.getCourseConsumePrice().compareTo(payList.getRollOffMoney()) > -1){
                if(courseAccount1.getCourseConsumeCount()>=payList.getRollOffCount()){
                    courseAccount1.setCourseRestPrice(courseAccount1.getCourseRestPrice().subtract(payList.getRollOffMoney()));
                    courseAccount1.setCourseConsumePrice(courseAccount1.getCourseConsumePrice().subtract(payList.getRollOffMoney()));
                    courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-payList.getRollOffCount());
                    courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumeCount()-payList.getRollOffCount());
                    courseAccount1.setExpenditureMoney(courseAccount1.getExpenditureMoney().add(payList.getRollOffMoney()));
                    if(courseAccount1.getCourseRestCount()!=0){
                        BigDecimal subtract = courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney());
                        if(subtract.compareTo(BigDecimal.valueOf(0))!=0){
                            courseAccount1.setPrice(subtract.divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseRestCount().toString()),4,BigDecimal.ROUND_HALF_UP));
                        }
                    }else{
                        BigDecimal subtract = courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney());
                        if(subtract.compareTo(BigDecimal.valueOf(0))!=0){
                            courseAccount1.setPrice(subtract);
                        }
                    }
                    payList.setBackPrice(courseAccount1.getPrice());
                    payList.setBackRestMoney(courseAccount1.getCourseRestPrice());
                    payList.setBackConsumeMoney(courseAccount1.getCourseConsumePrice());
                    payList.setBackRestCount(courseAccount1.getCourseRestCount());
                    payList.setBackConsumeCount(courseAccount1.getCourseConsumeCount());
                    CourseAccount courseAccount2 = ssmCourseAccountService.subCount(courseAccount1);
                    ssmCoursePayListService.updateNotNull(payList);
                }else{
                    throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+courseAccount1.getCourseProjectName()+"】可用次数不足，审核失败");
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+courseAccount1.getCourseProjectName()+"】可用余额不足，审核失败");
            }
            //添加疗程卡交易记录
            AccountTradeRecord accountTradeRecord1 = this.setAccountTradeRecord(rechargeTurnCourse, courseAccount1, payList);
            ssmAccountTradeRecordService.addRecord(accountTradeRecord1);
        }
        insert = dao.updateByPrimaryKeySelective(rechargeTurnCourse);
        return insert;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int update(RechargeTurnCourseRequest rechargeTurnCourseRequest,String staffPhone) throws SmsException {
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setId(rechargeTurnCourseRequest.getId());
        RechargeTurnCourse rechargeTurnCourse1 = dao.selectOne(rechargeTurnCourse);

        StoreParament storeParament = StoreParaUtils.getStorePara(rechargeTurnCourseRequest.getStoreId(), Constant.editOrderNum);
        int days = (int) DateUtils.getDayDiff(rechargeTurnCourse1.getDocDate(), new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
            }
        }


        if(rechargeTurnCourse1.getState()==0||rechargeTurnCourse1.getState()==2||rechargeTurnCourse1.getState()==4){
            BigDecimal reduce3 = rechargeTurnCourseRequest.getStaffLists().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(rechargeTurnCourseRequest.getStaffLists(),rechargeTurnCourse1);
            StaffResponse staffResponse = new StaffResponse();
            Set<String> keys = redisClient.keys(getStaffKey(staffPhone) + "*");
            //从redis查询 设置录单人信息 app使用
            if (keys.size() > 0){
                String key =  new ArrayList<>(keys).get(0);
                staffResponse = redisClient.get(key);
            }
            rechargeTurnCourse.setUpdateId(staffResponse.getId());
            rechargeTurnCourse.setUpdateBy(staffResponse.getStaffName());
            rechargeTurnCourse.setUpdateDate(new Date());
            int update = dao.updateByPrimaryKey(rechargeTurnCourse1);
            return update;
        }else{
            throw new SmsException(ResponseCode.MSG,"该单据当前状态不允许修改");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int submit(RechargeTurnCourseRequest rechargeTurnCourseRequest,String staffPhone) throws SmsException {
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setId(rechargeTurnCourseRequest.getId());
        RechargeTurnCourse rechargeTurnCourse1 = dao.selectOne(rechargeTurnCourse);

        StoreParament storeParament = StoreParaUtils.getStorePara(rechargeTurnCourseRequest.getStoreId(), Constant.editOrderNum);
        int days = (int) DateUtils.getDayDiff(rechargeTurnCourse1.getDocDate(), new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
            }
        }

        if(rechargeTurnCourse1.getState()==0||rechargeTurnCourse1.getState()==2||rechargeTurnCourse1.getState()==4){
            rechargeTurnCourse1.setState(1);
            StaffResponse staffResponse = new StaffResponse();
            Set<String> keys = redisClient.keys(getStaffKey(staffPhone) + "*");
            //从redis查询 设置录单人信息 app使用
            if (keys.size() > 0){
                String key =  new ArrayList<>(keys).get(0);
                staffResponse = redisClient.get(key);
            }
            rechargeTurnCourse.setUpdateId(staffResponse.getId());
            rechargeTurnCourse.setUpdateBy(staffResponse.getStaffName());
            rechargeTurnCourse.setUpdateDate(new Date());
            int update = dao.updateByPrimaryKey(rechargeTurnCourse1);
            return update;
        }else{
            throw new SmsException(ResponseCode.MSG,"该单据当前状态不允许送审");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int recycle(RechargeTurnCourseRequest rechargeTurnCourseRequest,String staffPhone) throws SmsException {
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setId(rechargeTurnCourseRequest.getId());
        RechargeTurnCourse rechargeTurnCourse1 = dao.selectOne(rechargeTurnCourse);

        StoreParament storeParament = StoreParaUtils.getStorePara(rechargeTurnCourseRequest.getStoreId(), Constant.editOrderNum);
        int days = (int) DateUtils.getDayDiff(rechargeTurnCourse1.getDocDate(), new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
            }
        }

        if(rechargeTurnCourse1.getState()==1){
            rechargeTurnCourse1.setState(2);
            StaffResponse staffResponse = new StaffResponse();
            Set<String> keys = redisClient.keys(getStaffKey(staffPhone) + "*");
            //从redis查询 设置录单人信息 app使用
            if (keys.size() > 0){
                String key =  new ArrayList<>(keys).get(0);
                staffResponse = redisClient.get(key);
            }
            rechargeTurnCourse.setUpdateId(staffResponse.getId());
            rechargeTurnCourse.setUpdateBy(staffResponse.getStaffName());
            rechargeTurnCourse.setUpdateDate(new Date());
            int update = dao.updateByPrimaryKey(rechargeTurnCourse1);
            return update;
        }else{
            throw new SmsException(ResponseCode.MSG,"该单据当前状态不允许回收");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int audit(RechargeTurnCourseRequest rechargeTurnCourseRequest,String staffPhone) throws SmsException {
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setId(rechargeTurnCourseRequest.getId());
        RechargeTurnCourse rechargeTurnCourse1 = dao.selectOne(rechargeTurnCourse);

        StoreParament storeParament = StoreParaUtils.getStorePara(rechargeTurnCourseRequest.getStoreId(), Constant.editOrderNum);
        int days = (int) DateUtils.getDayDiff(rechargeTurnCourse1.getDocDate(), new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
            }
        }

        if(rechargeTurnCourse1.getState()==1){
            rechargeTurnCourse1.setState(3);
            StaffResponse staffResponse = new StaffResponse();
            Set<String> keys = redisClient.keys(getStaffKey(staffPhone) + "*");
            //从redis查询 设置录单人信息 app使用
            if (keys.size() > 0){
                String key =  new ArrayList<>(keys).get(0);
                staffResponse = redisClient.get(key);
            }
            rechargeTurnCourse.setUpdateId(staffResponse.getId());
            rechargeTurnCourse.setUpdateBy(staffResponse.getStaffName());
            rechargeTurnCourse.setUpdateDate(new Date());
            int update = dao.updateByPrimaryKey(rechargeTurnCourse1);
            if(update>0){

                //添加充值卡金额
                RechargeAccount rechargeAccountRequest = new RechargeAccount();
                rechargeAccountRequest.setId(rechargeTurnCourse1.getRechargeAccountId());
                RechargeAccount rechargeAccount = ssmRechargeAccountService.selectOne(rechargeAccountRequest);
                rechargeAccount.setCardConsumeMoney(rechargeAccount.getCardConsumeMoney().add(rechargeTurnCourse1.getRechargeInMoney()));
                rechargeAccount.setCardRestMoney(rechargeAccount.getCardRestMoney().add(rechargeTurnCourse1.getRechargeInMoney()));
                RechargeAccount rechargeAccount1 = ssmRechargeAccountService.addMoney(rechargeAccount);

                //添加充值卡交易记录
                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeTurnCourse1, rechargeAccount1);
                ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                //扣减疗程卡余额和单价重算和剩余次数(转可用的，如果需要全部转，需要取消欠款)
                CoursePayList coursePayList = new CoursePayList();
                coursePayList.setDocNum(rechargeTurnCourse1.getDocNum());
                coursePayList.setRechargeTurnCourseId(rechargeTurnCourse1.getId());
                List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
                int account=0;
                for (CoursePayList payList : select) {
                    account++;
                    CourseAccount courseAccount = new CourseAccount();
                    courseAccount.setId(payList.getCourseAccountId());
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);

                    DebtList debtList = new DebtList();
                    debtList.setAccountId(payList.getCourseAccountId());
                    debtList.setState(0);
                    List<DebtList> selects = ssmDebtService.selectLists(debtList);
                    if(selects.size()>0){
                        throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+coursePayList.getCourseProjectName()+"】存在欠款，请先取消欠款或者还款");
                    }
                    //校验疗程账户是否足够可用金额可扣
                    if(courseAccount1.getCourseConsumePrice().compareTo(payList.getRollOffMoney()) == 1){
                        if(courseAccount1.getCourseConsumeCount().compareTo(payList.getRollOffCount()) == 1){
                            courseAccount1.setCourseRestPrice(courseAccount1.getCourseRestPrice().subtract(payList.getRollOffMoney()));
                            courseAccount1.setCourseConsumePrice(courseAccount1.getCourseConsumePrice().subtract(payList.getRollOffMoney()));
                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-payList.getRollOffCount());
                            courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumeCount()-payList.getRollOffCount());
                            courseAccount1.setPrice(courseAccount1.getCourseRestPrice().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseRestCount().toString())));
                            CourseAccount courseAccount2 = ssmCourseAccountService.subCount(courseAccount1);
                        }else{
                            throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+courseAccount1.getCourseProjectName()+"】可用次数不足，审核失败");
                        }
                    }else{
                        throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+courseAccount1.getCourseProjectName()+"】可用余额不足，审核失败");
                    }
                    //添加疗程卡交易记录
                    AccountTradeRecord accountTradeRecord1 = this.setAccountTradeRecord(rechargeTurnCourse1, courseAccount1, payList);
                    ssmAccountTradeRecordService.addRecord(accountTradeRecord1);
                }
            }
            return update;
        }else{
            throw new SmsException(ResponseCode.MSG,"该单据当前状态不允许审核");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int theAudit(RechargeTurnCourseRequest rechargeTurnCourseRequest,String staffPhone) throws SmsException {
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setId(rechargeTurnCourseRequest.getId());
        RechargeTurnCourse rechargeTurnCourse1 = dao.selectOne(rechargeTurnCourse);

        StoreParament storeParament = StoreParaUtils.getStorePara(rechargeTurnCourseRequest.getStoreId(), Constant.editOrderNum);
        int days = (int) DateUtils.getDayDiff(rechargeTurnCourse1.getDocDate(), new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
            }
        }

        if(rechargeTurnCourse1.getState()==3){
            rechargeTurnCourse1.setState(4);
            StaffResponse staffResponse = new StaffResponse();
            Set<String> keys = redisClient.keys(getStaffKey(staffPhone) + "*");
            //从redis查询 设置录单人信息 app使用
            if (keys.size() > 0){
                String key =  new ArrayList<>(keys).get(0);
                staffResponse = redisClient.get(key);
            }
            rechargeTurnCourse.setUpdateId(staffResponse.getId());
            rechargeTurnCourse.setUpdateBy(staffResponse.getStaffName());
            rechargeTurnCourse.setUpdateDate(new Date());
            int update = dao.updateByPrimaryKey(rechargeTurnCourse1);
            if(update>0){

                //扣减充值卡金额
                RechargeAccount rechargeAccountRequest = new RechargeAccount();
                rechargeAccountRequest.setId(rechargeTurnCourse1.getRechargeAccountId());
                RechargeAccount rechargeAccount = ssmRechargeAccountService.selectOne(rechargeAccountRequest);
                if(rechargeAccount.getCardRestMoney().compareTo(rechargeTurnCourse.getRechargeAfterMoney())!=0){
                    throw new SmsException(ResponseCode.MSG,"充值卡转入后剩余金额不相等");
                }
                rechargeAccount.setCardConsumeMoney(rechargeAccount.getCardConsumeMoney().subtract(rechargeTurnCourse1.getRechargeInMoney()));
                rechargeAccount.setCardRestMoney(rechargeAccount.getCardRestMoney().subtract(rechargeTurnCourse1.getRechargeInMoney()));
                RechargeAccount rechargeAccount1 = ssmRechargeAccountService.addMoney(rechargeAccount);

                //添加充值卡交易记录
                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeTurnCourse1, rechargeAccount1);
                ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                //扣减疗程卡余额和单价重算和剩余次数(转可用的，如果需要全部转，需要取消欠款)
                CoursePayList coursePayList = new CoursePayList();
                coursePayList.setDocNum(rechargeTurnCourse1.getDocNum());
                coursePayList.setRechargeTurnCourseId(rechargeTurnCourse1.getId());
                List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
                int account=0;
                for (CoursePayList payList : select) {
                    account++;
                    CourseAccount courseAccount = new CourseAccount();
                    courseAccount.setId(payList.getCourseAccountId());
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                    if(courseAccount1.getCourseConsumePrice().compareTo(payList.getCourseAfterUsableMoney())!=0){
                        throw new SmsException(ResponseCode.MSG,"疗程卡转入后可用金额不相等");
                    }
                    DebtList debtList = new DebtList();
                    debtList.setAccountId(payList.getCourseAccountId());
                    debtList.setState(0);
                    List<DebtList> selects = ssmDebtService.selectLists(debtList);
                    if(selects.size()>0){
                        throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+coursePayList.getCourseProjectName()+"】存在欠款，请先取消欠款或者还款");
                    }
                    //校验疗程账户是否足够可用金额可扣
                    if(courseAccount1.getCourseConsumePrice().compareTo(payList.getRollOffMoney()) == 1){
                        if(courseAccount1.getCourseConsumeCount().compareTo(payList.getRollOffCount()) == 1){
                            courseAccount1.setCourseRestPrice(courseAccount1.getCourseRestPrice().add(payList.getRollOffMoney()));
                            courseAccount1.setCourseConsumePrice(courseAccount1.getCourseConsumePrice().add(payList.getRollOffMoney()));
                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()+payList.getRollOffCount());
                            courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumeCount()+payList.getRollOffCount());
                            courseAccount1.setPrice(courseAccount1.getCourseRestPrice().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseRestCount().toString())));
                            CourseAccount courseAccount2 = ssmCourseAccountService.subCount(courseAccount1);
                        }else{
                            throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+courseAccount1.getCourseProjectName()+"】可用次数不足，审核失败");
                        }
                    }else{
                        throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+courseAccount1.getCourseProjectName()+"】可用余额不足，审核失败");
                    }
                    //添加疗程卡交易记录
                    AccountTradeRecord accountTradeRecord1 = this.setAccountTradeRecord(rechargeTurnCourse1, courseAccount1, payList);
                    ssmAccountTradeRecordService.addRecord(accountTradeRecord1);
                }
            }
            return update;
        }else{
            throw new SmsException(ResponseCode.MSG,"该单据当前状态不允许审核");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int cancel(RechargeTurnCourseRequest rechargeTurnCourseRequest,String staffPhone) throws SmsException {
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setId(rechargeTurnCourseRequest.getId());
        RechargeTurnCourse rechargeTurnCourse1 = dao.selectOne(rechargeTurnCourse);

        StoreParament storeParament = StoreParaUtils.getStorePara(rechargeTurnCourse1.getStoreId(), Constant.service_ticke);
        int days = (int) DateUtils.getDayDiff(rechargeTurnCourse1.getDocDate(), new Date());
        if(!"0".equals(storeParament.getParamentValue())) {
            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许作废时间");
            }
        }

        if(rechargeTurnCourse1.getState()==0){
            rechargeTurnCourse1.setState(5);
            StaffResponse staffResponse = new StaffResponse();
            Set<String> keys = redisClient.keys(getStaffKey(staffPhone) + "*");
            //从redis查询 设置录单人信息 app使用
            if (keys.size() > 0){
                String key =  new ArrayList<>(keys).get(0);
                staffResponse = redisClient.get(key);
            }
            rechargeTurnCourse1.setUpdateId(staffResponse.getId());
            rechargeTurnCourse1.setUpdateBy(staffResponse.getStaffName());
            rechargeTurnCourse1.setUpdateDate(new Date());
            int update = dao.updateByPrimaryKey(rechargeTurnCourse1);
            if(update>0){

                //扣减充值卡金额
                RechargeAccount rechargeAccountRequest = new RechargeAccount();
                rechargeAccountRequest.setId(rechargeTurnCourse1.getRechargeAccountId());
                RechargeAccount rechargeAccount = ssmRechargeAccountService.selectOne(rechargeAccountRequest);

                if(rechargeTurnCourse1.getBackRestMoney().compareTo(rechargeAccount.getCardRestMoney())!=0){
                    throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余金额与当前剩余金额不相等");
                }
                if(rechargeTurnCourse1.getBackConsumeMoney().compareTo(rechargeAccount.getCardConsumeMoney())!=0){
                    throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用金额与当前可用金额不相等");
                }
                if(rechargeTurnCourse1.getBackGiftRestMoney().compareTo(rechargeAccount.getCardGiftMoney())!=0){
                    throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后赠送剩余金额与当前赠送剩余金额不相等");
                }
                if(rechargeTurnCourse1.getBackGiftConsumeMoney().compareTo(rechargeAccount.getGiftConsumeMoney())!=0){
                    throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后赠送可用金额与当前赠送可用金额不相等");
                }
                rechargeAccount.setCardRestMoney(rechargeTurnCourse1.getBeforeRestMoney());
                rechargeAccount.setCardConsumeMoney(rechargeTurnCourse1.getBeforeConsumeMoney());
                rechargeAccount.setCardGiftMoney(rechargeTurnCourse1.getBeforeGiftRestMoney());
                rechargeAccount.setGiftConsumeMoney(rechargeTurnCourse1.getBeforeGiftConsumeMoney());

//                rechargeAccount.setCardConsumeMoney(rechargeAccount.getCardConsumeMoney().subtract(rechargeTurnCourse1.getRechargeInMoney()));
//                rechargeAccount.setCardRestMoney(rechargeAccount.getCardRestMoney().subtract(rechargeTurnCourse1.getRechargeInMoney()));
                RechargeAccount rechargeAccount1 = ssmRechargeAccountService.addMoney(rechargeAccount);
                if(rechargeAccount.getCardRestMoney().compareTo(BigDecimalUtils.bigDecimal("0"))==-1){
                    throw new SmsException(ResponseCode.MSG,"充值卡余额不足退还疗程卡");
                }

                //添加充值卡交易记录
                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecordCancel(rechargeTurnCourse1, rechargeAccount1);
                ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                //扣减疗程卡余额和单价重算和剩余次数(转可用的，如果需要全部转，需要取消欠款)
                CoursePayList coursePayList = new CoursePayList();
                coursePayList.setDocNum(rechargeTurnCourse1.getDocNum());
                coursePayList.setRechargeTurnCourseId(rechargeTurnCourse1.getId());
                List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
                int account=0;
                for (CoursePayList payList : select) {
                    account++;
                    CourseAccount courseAccount = new CourseAccount();
                    courseAccount.setId(payList.getCourseAccountId());
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);

                    if(payList.getBackPrice().compareTo(courseAccount1.getPrice())!=0){
                        throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后单价与当前单价不相等");
                    }
                    if(payList.getBackRestMoney().compareTo(courseAccount1.getCourseRestPrice())!=0){
                        throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余金额与当前剩余金额不相等");
                    }
                    if(payList.getBackConsumeMoney().compareTo(courseAccount1.getCourseConsumePrice())!=0){
                        throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用金额与当前可用金额不相等");
                    }
                    if(payList.getBackRestCount() != courseAccount1.getCourseRestCount()){
                        throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余次数与当前剩余次数不相等");
                    }
                    if(payList.getBackConsumeCount() != courseAccount1.getCourseConsumeCount()){
                        throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用次数与当前可用次数不相等");
                    }
                    courseAccount1.setPrice(payList.getBeforePrice());
                    courseAccount1.setCourseRestPrice(payList.getBeforeRestMoney());
                    courseAccount1.setCourseConsumePrice(payList.getBeforeConsumeMoney());
                    courseAccount1.setCourseRestCount(payList.getBeforeRestCount());
                    courseAccount1.setCourseConsumeCount(payList.getBeforeConsumeCount());

//                    DebtList debtList = new DebtList();
//                    debtList.setAccountId(payList.getCourseAccountId());
//                    debtList.setState(0);
//                    debtList.setStoreId(rechargeTurnCourse1.getStoreId());
//                    List<DebtList> selects = ssmDebtService.selectLists(debtList);
//                    if(selects.size()>0){
//                        throw new SmsException(ResponseCode.MSG,"第"+account+"个转出的疗程账户【"+coursePayList.getCourseProjectName()+"】存在欠款，请先取消欠款或者还款");
//                    }
//                    //校验疗程账户是否足够可用金额可扣
//                    courseAccount1.setCourseRestPrice(courseAccount1.getCourseRestPrice().add(payList.getRollOffMoney()));
//                    courseAccount1.setCourseConsumePrice(courseAccount1.getCourseConsumePrice().add(payList.getRollOffMoney()));
//                    courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()+payList.getRollOffCount());
//                    courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumeCount()+payList.getRollOffCount());
//                    courseAccount1.setPrice(courseAccount1.getCourseRestPrice().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseRestCount().toString())));
                    courseAccount1.setExpenditureMoney(courseAccount1.getExpenditureMoney().subtract(payList.getRollOffMoney()));
                    CourseAccount courseAccount2 = ssmCourseAccountService.subCount(courseAccount1);
                    //添加疗程卡交易记录
                    AccountTradeRecord accountTradeRecord1 = this.setAccountTradeRecord(rechargeTurnCourse1, courseAccount1, payList);
                    ssmAccountTradeRecordService.addRecord(accountTradeRecord1);
                }
            }

            return update;
        }else{
            throw new SmsException(ResponseCode.MSG,"该单据当前状态不允许审核");
        }
    }

    private AccountTradeRecord setAccountTradeRecordCancel(RechargeTurnCourse rechargeTurnCourse, RechargeAccount rechargeAccount) {
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(rechargeTurnCourse.getStoreId());
        accountTradeRecord.setStoreName(rechargeTurnCourse.getStoreName());
        accountTradeRecord.setMemberId(rechargeTurnCourse.getMemberId());
        accountTradeRecord.setMemberName(rechargeTurnCourse.getMemberName());
        accountTradeRecord.setMemberIdentity(rechargeTurnCourse.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(rechargeTurnCourse.getMobilePhone());
        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        if(rechargeTurnCourse.getState() == 0) {//审核
            accountTradeRecord.setAccountType(Constant.recharge_turn_account);
            accountTradeRecord.setInputDate(rechargeTurnCourse.getInputDate());
            accountTradeRecord.setInputBy(rechargeTurnCourse.getInputBy());
            accountTradeRecord.setAccountInMoney(rechargeTurnCourse.getRechargeInMoney());
            accountTradeRecord.setOrderStatu(0);
        }else {//反审核
            accountTradeRecord.setAccountType(Constant.recharge_turn_account_counrse);
            accountTradeRecord.setInputDate(rechargeTurnCourse.getUpdateDate());
            accountTradeRecord.setInputBy(rechargeTurnCourse.getUpdateBy());
            accountTradeRecord.setAccountSpendMoney(rechargeTurnCourse.getRechargeInMoney());
            accountTradeRecord.setOrderStatu(1);
        }
        accountTradeRecord.setIsAddition(rechargeTurnCourse.getIsAddition());
        accountTradeRecord.setDocId(rechargeTurnCourse.getId());
        accountTradeRecord.setDocNum(rechargeTurnCourse.getDocNum());
        accountTradeRecord.setDocType(Constant.doc_type_recharge_turn_course);
        accountTradeRecord.setTradeId(rechargeTurnCourse.getRechargeTypeId());
        accountTradeRecord.setTradeName(rechargeTurnCourse.getRechargeTypeName());
        accountTradeRecord.setTradeMoney(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        accountTradeRecord.setAccountSpendMoney(rechargeTurnCourse.getCourseOutMoney());
        accountTradeRecord.setDebtMoney(BigDecimal.valueOf(0));
        accountTradeRecord.setPrice(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setCardMoney(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setCashMoney(BigDecimal.valueOf(0));
        return accountTradeRecord;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int delete(RechargeTurnCourseRequest rechargeTurnCourseRequest) throws SmsException {
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setId(rechargeTurnCourseRequest.getId());
        RechargeTurnCourse rechargeTurnCourse1 = dao.selectOne(rechargeTurnCourse);
        if(rechargeTurnCourse1.getState()==0){
            int delete = dao.delete(rechargeTurnCourse);
            return delete;
        }else{
            throw new SmsException(ResponseCode.MSG,"该单据当前状态不允许删除");
        }

    }

    @Override
    public PageInfo<RechargeTurnCourseRequest> selectPage(RechargeTurnCourseRequest rechargeTurnCourseRequest) {
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != rechargeTurnCourseRequest.getStoreId()){
            criteria.andEqualTo("storeId", rechargeTurnCourseRequest.getStoreId());
        }
        if (null != rechargeTurnCourseRequest.getMemberNum()){
            criteria.andLike("memberNum", "%"+rechargeTurnCourseRequest.getMemberNum()+"%");
        }
        if (null != rechargeTurnCourseRequest.getMemberName()){
            criteria.andLike("memberName", "%"+rechargeTurnCourseRequest.getMemberName()+"%");
        }
        if (null != rechargeTurnCourseRequest.getMobilePhone()){
            criteria.andLike("mobilePhone", "%"+rechargeTurnCourseRequest.getMobilePhone()+"%");
        }
        if (null != rechargeTurnCourseRequest.getDocNum()){
            criteria.andLike("docNum", "%"+rechargeTurnCourseRequest.getDocNum()+"%");
        }
        rechargeTurnCourseRequest.setSort("docDate");
        PageHelper.startPage(rechargeTurnCourseRequest.getPageNum(),rechargeTurnCourseRequest.getPageSize(),rechargeTurnCourseRequest.getOrderAndSort());
        List<RechargeTurnCourse> lists = dao.selectByExample(example);
        List<RechargeTurnCourseRequest> objects = new ArrayList<>();
        for (RechargeTurnCourse list : lists) {
            RechargeTurnCourseRequest rechargeTurnCourseRequest1 = new RechargeTurnCourseRequest();
            BeanUtils.copyProperties(list, rechargeTurnCourseRequest1);
            CoursePayList coursePayList = new CoursePayList();
            coursePayList.setRechargeTurnCourseId(list.getId());
            List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
            List<AchiCommEntity> achiCommList = this.getAchiCommList(list);
            rechargeTurnCourseRequest1.setEntities(achiCommList);
            rechargeTurnCourseRequest1.setCoursePayLists(select);

            objects.add(rechargeTurnCourseRequest1);
        }
        PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>(objects);
        return pageInfo;
    }

    @Override
    public PageInfo<RechargeTurnCourseRequest> selectPageNew(RechargeTurnCourseRequest rechargeTurnCourseRequest) {
        List<Long> lists = new ArrayList<>();
        if(rechargeTurnCourseRequest.getSelectType()==0){
            lists=rechargeTurnCourseRequest.getStoreList();
        }else{
            BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(rechargeTurnCourseRequest.getStoreId());
            if(null == baseStore1.getParentId()&&(rechargeTurnCourseRequest.getStoreList().size()<0||rechargeTurnCourseRequest.getStoreList()==null)){
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(rechargeTurnCourseRequest.getStoreId());
                List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
                List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
                lists=collect;
            }else{
                lists.add(rechargeTurnCourseRequest.getStoreId());
            }
        }
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("storeId", lists);
        if (null != rechargeTurnCourseRequest.getMemberNum()){
            criteria.andLike("memberNum", "%"+rechargeTurnCourseRequest.getMemberNum()+"%");
        }
        if (null != rechargeTurnCourseRequest.getMemberName()){
            criteria.andLike("memberName", "%"+rechargeTurnCourseRequest.getMemberName()+"%");
        }
        if (null != rechargeTurnCourseRequest.getMobilePhone()){
            criteria.andLike("mobilePhone", "%"+rechargeTurnCourseRequest.getMobilePhone()+"%");
        }
        if (null != rechargeTurnCourseRequest.getDocNum()){
            criteria.andLike("docNum", "%"+rechargeTurnCourseRequest.getDocNum()+"%");
        }
        if(rechargeTurnCourseRequest.getStartTime()!=null && rechargeTurnCourseRequest.getEndTime()!=null){
            criteria.andBetween("docDate",rechargeTurnCourseRequest.getStartTime(),rechargeTurnCourseRequest.getEndTime());
        }
        if(rechargeTurnCourseRequest.getState()!=null){
            criteria.andEqualTo("state",rechargeTurnCourseRequest.getState());
        }
        rechargeTurnCourseRequest.setSort("docDate");
        PageHelper.startPage(rechargeTurnCourseRequest.getPageNum(),rechargeTurnCourseRequest.getPageSize(),rechargeTurnCourseRequest.getOrderAndSort());
        List<RechargeTurnCourse> listsTurn = dao.selectByExample(example);
        List<RechargeTurnCourseRequest> objects = new ArrayList<>();
        for (RechargeTurnCourse list : listsTurn) {
            RechargeTurnCourseRequest rechargeTurnCourseRequest1 = new RechargeTurnCourseRequest();
            BeanUtils.copyProperties(list, rechargeTurnCourseRequest1);
            CoursePayList coursePayList = new CoursePayList();
            coursePayList.setRechargeTurnCourseId(list.getId());
            List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
            List<AchiCommEntity> achiCommList = this.getAchiCommList(list);
            //查询疗程信息和疗程销售人员信息

            rechargeTurnCourseRequest1.setEntities(achiCommList);
            rechargeTurnCourseRequest1.setCoursePayLists(select);

            for (CoursePayList payList : select) {
                List<AchiCommEntity> list1 = this.getAchiCommList(payList);
                payList.setAchiCommEntityList(list1);
            }
            objects.add(rechargeTurnCourseRequest1);
        }
        PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>(objects);
        return pageInfo;
    }
    public List<AchiCommEntity> getAchiCommList(CoursePayList courseList) {
        List<AchiCommEntity> entities = new ArrayList<>();
//        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");

//        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(courseList.getCourseProjectId());

        if (null != courseList.getSaleOneStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleOneNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleOneStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleOneStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleOneIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleOneRatio());


            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleTwoStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleTwoNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleTwoStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleTwoStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleTwoIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleTwoRatio());

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleThreeStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleThreeNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleThreeStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleThreeStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleThreeIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleThreeRatio());

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleFourStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
//            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleFourNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleFourStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleFourStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleFourIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleFourRatio());

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleFiveStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleFiveNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleFiveStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleFiveStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleFiveIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleFiveRatio());

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleSixStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleSixNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleSixStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleSixStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleSixIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleSixRatio());

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleSevenStaffId()) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleSevenNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleSevenStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleSevenStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleSevenIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleSevenRatio());

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleEightStaffId() && courseList.getSaleEightStaffId() != 0) {
            AchiCommEntity achiCommEntity = new AchiCommEntity();
//            achiCommEntity.setMoney(courseList.getCourseReatmentAmount());
//            achiCommEntity.setMemberNum(String.valueOf(courseList.getMemberId()));
//            achiCommEntity.setMemberName(courseList.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
//            achiCommEntity.setDocNum(courseList.getCourseReatmentNum());
//            achiCommEntity.setDocType(Constant.doc_type_card_use);
//            achiCommEntity.setDocDate(courseList.getDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleEightNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleEightStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleEightStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleEightIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleEightRatio());

            entities.add(achiCommEntity);
        }

        return entities;
    }

    @Override
    public PageInfo<RechargeTurnCourseRequest> pageDetail(RechargeTurnCourseRequest rechargeTurnCourseRequest) {
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", rechargeTurnCourseRequest.getId());
        PageHelper.startPage(rechargeTurnCourseRequest.getPageNum(),rechargeTurnCourseRequest.getPageSize(),rechargeTurnCourseRequest.getOrderAndSort());
        List<RechargeTurnCourse> lists = dao.selectByExample(example);
        List<RechargeTurnCourseRequest> objects = new ArrayList<>();
        for (RechargeTurnCourse list : lists) {
            //转入充值单查询剩余可用金额和折扣
            Long rechargeAccountId = list.getRechargeAccountId();
            RechargeAccount rechargeAccount = ssmRechargeAccountService.selectByPrimaryKey(rechargeAccountId);
            RechargeTurnCourseRequest rechargeTurnCourseRequest1 = new RechargeTurnCourseRequest();
            BeanUtils.copyProperties(list, rechargeTurnCourseRequest1);
            String s = rechargeAccount.getDiscount().toString();
            rechargeTurnCourseRequest1.setDiscount(Double.valueOf(s).intValue());
            rechargeTurnCourseRequest1.setCardConsumeMoney(rechargeAccount.getCardConsumeMoney());
            CoursePayList coursePayList = new CoursePayList();
            coursePayList.setRechargeTurnCourseId(list.getId());
            List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
            List<AchiCommEntity> achiCommList = this.getAchiCommList(list);
            rechargeTurnCourseRequest1.setEntities(achiCommList);
            //rechargeTurnCourseRequest1.setCoursePayLists(select);
            List<CoursePayListRequest> requests = new ArrayList<>();
            for (CoursePayList payList : select) {
                CoursePayListRequest payListRequest = new CoursePayListRequest();
                BeanUtils.copyProperties(payList, payListRequest);
                List<StaffRequest> staffRequests = this.setStaffValue(payList);
                payListRequest.setStaffLists(staffRequests);
                requests.add(payListRequest);
            }
            rechargeTurnCourseRequest1.setCoursePayListRequests(requests);


            List<CoursePayList> requests1 = new ArrayList<>();
            for (CoursePayList payList : select) {
                CoursePayList payListRequest = new CoursePayList();
                BeanUtils.copyProperties(payList, payListRequest);
                List<StaffRequest> staffRequests = this.setStaffValue(payList);
                List<AchiCommEntity> achiCommEntityList = new ArrayList<>();
                for (StaffRequest staffRequest : staffRequests) {
                    AchiCommEntity achiCommEntity = new AchiCommEntity();
                    BeanUtils.copyProperties(staffRequest,achiCommEntity);
                    achiCommEntity.setSaleRatio(staffRequest.getRatio());
                    achiCommEntity.setSaleStaffName(staffRequest.getStaffName());
                    achiCommEntityList.add(achiCommEntity);
                }
                payListRequest.setAchiCommEntityList(achiCommEntityList);
                requests1.add(payListRequest);
            }
            rechargeTurnCourseRequest1.setCoursePayLists(requests1);



            objects.add(rechargeTurnCourseRequest1);
        }
        if (objects.size() > 0){
            PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>(objects);
            return pageInfo;
        }else {
            Example example1 = new Example(CoursePayList.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("id", rechargeTurnCourseRequest.getId());
            PageHelper.startPage(rechargeTurnCourseRequest.getPageNum(),rechargeTurnCourseRequest.getPageSize(),rechargeTurnCourseRequest.getOrderAndSort());
            List<CoursePayList> coursePayLists = ssmCoursePayListService.selectByExample(example1);

            for (CoursePayList coursePayList : coursePayLists) {

                /*List<CoursePayList> list = new ArrayList<>();
                list.add(coursePayList);*/
                RechargeTurnCourseRequest rechargeTurnCourseRequest1 = new RechargeTurnCourseRequest();

                //rechargeTurnCourseRequest1.setCoursePayLists(list);
                List<CoursePayListRequest> listRequests = new ArrayList<>();
                CoursePayListRequest payListRequest = new CoursePayListRequest();
                BeanUtils.copyProperties(coursePayList, payListRequest);
                List<StaffRequest> staffRequests = this.setStaffValue(coursePayList);
                payListRequest.setStaffLists(staffRequests);
                listRequests.add(payListRequest);
                rechargeTurnCourseRequest1.setCoursePayListRequests(listRequests);

                Long id = coursePayList.getRechargeTurnCourseId();
                RechargeTurnCourse rechargeTurnCourse = dao.selectByPrimaryKey(id);

                Long accountId = rechargeTurnCourse.getRechargeAccountId();
                if (accountId != null){
                    RechargeAccount rechargeAccount = ssmRechargeAccountService.selectByPrimaryKey(accountId);
                    BigDecimal discount = rechargeAccount.getDiscount();
                    String s = discount.stripTrailingZeros().toPlainString();
                    rechargeTurnCourseRequest1.setDiscount(Integer.valueOf(s));
                    rechargeTurnCourseRequest1.setCardConsumeMoney(rechargeAccount.getCardConsumeMoney());
                }else {
                    RechargeAccount rechargeAccount = new RechargeAccount();
                    rechargeAccount.setMemberId(rechargeTurnCourse.getMemberId());
                    rechargeAccount.setRechargeTypeId(rechargeTurnCourse.getRechargeTypeId());
                    RechargeAccount rechargeAccount1 = ssmRechargeAccountService.selectOne(rechargeAccount);
                    rechargeTurnCourseRequest1.setDiscount(Integer.valueOf(rechargeAccount1.getDiscount().stripTrailingZeros().toPlainString()));
                    rechargeTurnCourseRequest1.setCardConsumeMoney(rechargeAccount1.getCardConsumeMoney());
                }
                //RechargeAccount rechargeAccount = ssmRechargeAccountService.selectByPrimaryKey(accountId);
                BeanUtils.copyProperties(rechargeTurnCourse, rechargeTurnCourseRequest1);
                /*rechargeTurnCourseRequest1.setDiscount(Integer.parseInt(rechargeAccount.getDiscount().toString()));
                rechargeTurnCourseRequest1.setCardConsumeMoney(rechargeAccount.getCardConsumeMoney());*/

                List<AchiCommEntity> achiCommList = this.getAchiCommList(rechargeTurnCourse);
                rechargeTurnCourseRequest1.setEntities(achiCommList);
                objects.add(rechargeTurnCourseRequest1);

                /*BeanUtils.copyProperties(list, rechargeTurnCourseRequest1);
                CoursePayList coursePayList = new CoursePayList();
                coursePayList.setRechargeTurnCourseId(list.getId());
                List<CoursePayList> select = ssmCoursePayListService.select(coursePayList);
                List<AchiCommEntity> achiCommList = this.getAchiCommList(list);
                rechargeTurnCourseRequest1.setEntities(achiCommList);
                rechargeTurnCourseRequest1.setCoursePayLists(select);

                objects.add(rechargeTurnCourseRequest1);*/
            }
            PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>(objects);
            return pageInfo;
        }

    }

    @Override
    public List<RechargeTurnCoursesResponse> selectDate(Long storeId) {
        List<RechargeTurnCoursesResponse> rechargeTurnCoursesResponses = new ArrayList<>();
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != storeId){
            criteria.andEqualTo("storeId", storeId);
        }

        criteria.andEqualTo("state",0);
        List<RechargeTurnCourse> lists = dao.selectByExample(example);
        for (RechargeTurnCourse list : lists) {
            RechargeTurnCoursesResponse rechargeTurnCoursesResponse = new RechargeTurnCoursesResponse();
            BeanUtils.copyProperties(list, rechargeTurnCoursesResponse);
            List<AchiCommEntity> achiCommList = this.getAchiCommList(list);
            rechargeTurnCoursesResponse.setEntities(achiCommList);
            BigDecimal bigDecimal = achiCommList.stream().map(AchiCommEntity::getTotalPerformance).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal courseOutMoney = rechargeTurnCoursesResponse.getCourseOutMoney();
            if(bigDecimal.compareTo(courseOutMoney)!=0){
                rechargeTurnCoursesResponse.setErrorMessage("请检查是否填写员工或者漏填员工，无误则检查项目是否有业绩提成");
                rechargeTurnCoursesResponses.add(rechargeTurnCoursesResponse);
            }
        }
        return rechargeTurnCoursesResponses;
    }

    @Override
    public List<RechargeTurnCoursesResponse> select(Long storeId,String startDate, String endDate) {
        List<RechargeTurnCoursesResponse> rechargeTurnCoursesResponses = new ArrayList<>();
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != storeId){
            criteria.andEqualTo("storeId", storeId);
        }
        criteria.andBetween("docDate", startDate,endDate);
        criteria.andEqualTo("state",0);
        List<RechargeTurnCourse> lists = dao.selectByExample(example);
        for (RechargeTurnCourse list : lists) {
            RechargeTurnCoursesResponse rechargeTurnCoursesResponse = new RechargeTurnCoursesResponse();
            BeanUtils.copyProperties(list, rechargeTurnCoursesResponse);
            List<AchiCommEntity> achiCommList = this.getAchiCommList(list);
            rechargeTurnCoursesResponse.setEntities(achiCommList);
            rechargeTurnCoursesResponses.add(rechargeTurnCoursesResponse);
        }
        return rechargeTurnCoursesResponses;
    }

    @Override
    public List<RechargeTurnCoursesResponse> selectDates(Long storeId, String date) {
        List<RechargeTurnCoursesResponse> rechargeTurnCoursesResponses = new ArrayList<>();
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != storeId){
            criteria.andEqualTo("storeId", storeId);
        }
        if (null != date){
            criteria.andLike("docDate", date+"%");
        }
        criteria.andEqualTo("state",0);
        List<RechargeTurnCourse> lists = dao.selectByExample(example);
        for (RechargeTurnCourse list : lists) {
            RechargeTurnCoursesResponse rechargeTurnCoursesResponse = new RechargeTurnCoursesResponse();
            BeanUtils.copyProperties(list, rechargeTurnCoursesResponse);
            List<AchiCommEntity> achiCommList = this.getAchiCommLists(list);
            for (AchiCommEntity entity : achiCommList) {
                entity.setDocId(list.getId());
            }
            rechargeTurnCoursesResponse.setEntities(achiCommList);
//            List<AchiCommEntity> list1 =  new ArrayList<>();
//            rechargeTurnCoursesResponse.setEntities(list1);
            rechargeTurnCoursesResponses.add(rechargeTurnCoursesResponse);
        }


        return rechargeTurnCoursesResponses;
    }


    @Override
    public List<RechargeTurnCoursesResponse> selectBystartAndEndTime(Long storeId, Date startTime,Date endTime) {
        List<RechargeTurnCoursesResponse> rechargeTurnCoursesResponses = new ArrayList<>();
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != storeId){
            criteria.andEqualTo("storeId", storeId);
        }
        criteria.andBetween("docDate", startTime,endTime);
        criteria.andEqualTo("state",0);
        List<RechargeTurnCourse> lists = dao.selectByExample(example);
        for (RechargeTurnCourse list : lists) {
            RechargeTurnCoursesResponse rechargeTurnCoursesResponse = new RechargeTurnCoursesResponse();
            BeanUtils.copyProperties(list, rechargeTurnCoursesResponse);
            List<AchiCommEntity> achiCommList = this.getAchiCommLists(list);
            for (AchiCommEntity entity : achiCommList) {
                entity.setDocId(list.getId());
            }
            rechargeTurnCoursesResponse.setEntities(achiCommList);
//            List<AchiCommEntity> list1 =  new ArrayList<>();
//            rechargeTurnCoursesResponse.setEntities(list1);
            rechargeTurnCoursesResponses.add(rechargeTurnCoursesResponse);
        }


        return rechargeTurnCoursesResponses;
    }

    @Override
    public List<RechargeTurnCoursesResponse> selectDateschia(Long storeId,Date startDate, Date endDate) {

        List<RechargeTurnCoursesResponse> rechargeTurnCoursesResponses = new ArrayList<>();
        Example example = new Example(RechargeTurnCourse.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", storeId);
        criteria.andBetween("docDate", startDate,endDate);
        criteria.andEqualTo("state",0);
        List<RechargeTurnCourse> lists = dao.selectByExample(example);
        for (RechargeTurnCourse list : lists) {
            RechargeTurnCoursesResponse rechargeTurnCoursesResponse = new RechargeTurnCoursesResponse();
            BeanUtils.copyProperties(list, rechargeTurnCoursesResponse);
            List<AchiCommEntity> achiCommList = this.getAchiCommLists(list);
            rechargeTurnCoursesResponse.setEntities(achiCommList);
            rechargeTurnCoursesResponses.add(rechargeTurnCoursesResponse);
        }
        return rechargeTurnCoursesResponses;

    }

    @Override
    public List<RechargeTurnCourse> selectFKJ(RechargeTurnCourse rechargeTurnCourse) {
        return dao.selectFKJ(rechargeTurnCourse);
    }
    @Autowired
    private RechargeTurnCourseMapper mapper;
    @Override
    public List<RechargeTurnCourse> selectFKJByDate(RechargeTurnCourse rechargeTurnCourse) {
        return mapper.selectFKJByDate(rechargeTurnCourse);
    }

    public List<AchiCommEntity> getAchiCommList(RechargeTurnCourse rechargeTurnCourse){
        List<AchiCommEntity> entities = new ArrayList<>();
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");
        if (null != rechargeTurnCourse.getSaleOneStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));

            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberOne());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleOneStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleOneStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleOneIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleOneRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.bigDecimal("0"));
            achiCommEntity.setCashAchi(BigDecimalUtils.bigDecimal("0"));
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        }
        if (null != rechargeTurnCourse.getSaleTwoStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberTwo());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleTwoStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleTwoStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleTwoIdentityCard());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleTwoRatio());
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        }
        if (null != rechargeTurnCourse.getSaleThreeStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberThree());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleThreeStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleThreeStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleThreeIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleThreeRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        }
        if (null != rechargeTurnCourse.getSaleFourStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberFour());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleFourStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleFourStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleFourIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleFourRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        } if (null != rechargeTurnCourse.getSaleFiveStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberFive());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleFiveStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleFiveStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleFiveIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleFiveRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFiveRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFiveRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFiveRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        } if (null != rechargeTurnCourse.getSaleSixStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberSix());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleSixStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleSixStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleSixIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleSixRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSixRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSixRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSixRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        } if (null != rechargeTurnCourse.getSaleSevenStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberSeven());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleSevenStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleSevenStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleSevenIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleSevenRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSevenRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSevenRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleSevenRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        } if (null != rechargeTurnCourse.getSaleEightStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberEight());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleEightStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleEightStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleEightIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleEightRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            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));
            }
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleEightRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setTotalPerformance(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleEightRatio(),hundredBD)));
                achiCommEntity.setTotalCommission(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleEightRatio(),hundredBD)));
            }
            entities.add(achiCommEntity);
        }


        return entities;
    }

    public List<AchiCommEntity> getAchiCommLists(RechargeTurnCourse rechargeTurnCourse){
        List<AchiCommEntity> entities = new ArrayList<>();
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");
        if (null != rechargeTurnCourse.getSaleOneStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));

            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberOne());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleOneStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleOneStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleOneIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleOneRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.bigDecimal("0"));
            achiCommEntity.setCashAchi(BigDecimalUtils.bigDecimal("0"));
            achiCommEntity.setCashComm(BigDecimal.valueOf(0));

            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardComm(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleOneRatio(),hundredBD)));
            }
            achiCommEntity.setTotalPerformance(achiCommEntity.getCardAchi());
            achiCommEntity.setTotalCommission(achiCommEntity.getCardComm());

            entities.add(achiCommEntity);

        }
        if (null != rechargeTurnCourse.getSaleTwoStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberTwo());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleTwoStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleTwoStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleTwoIdentityCard());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleTwoRatio());
            achiCommEntity.setCashAchi(BigDecimalUtils.bigDecimal("0"));
            achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardComm(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleTwoRatio(),hundredBD)));
            }
            achiCommEntity.setTotalPerformance(achiCommEntity.getCardAchi());
            achiCommEntity.setTotalCommission(achiCommEntity.getCardComm());

            entities.add(achiCommEntity);
        }
        if (null != rechargeTurnCourse.getSaleThreeStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberThree());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleThreeStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleThreeStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleThreeIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleThreeRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            achiCommEntity.setCashAchi(BigDecimalUtils.bigDecimal("0"));
            achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardComm(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleThreeRatio(),hundredBD)));
            }
            achiCommEntity.setTotalPerformance(achiCommEntity.getCardAchi());
            achiCommEntity.setTotalCommission(achiCommEntity.getCardComm());

            entities.add(achiCommEntity);
        }
        if (null != rechargeTurnCourse.getSaleFourStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(rechargeTurnCourse.getRechargeInMoney());
            achiCommEntity.setMemberNum(String.valueOf(rechargeTurnCourse.getMemberId()));
            achiCommEntity.setMemberName(rechargeTurnCourse.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(rechargeTurnCourse.getRechargeTypeId()));
            achiCommEntity.setDocNum(rechargeTurnCourse.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(rechargeTurnCourse.getDocDate());
            achiCommEntity.setTradeName(rechargeTurnCourse.getRechargeTypeName());
            achiCommEntity.setBrandNumber(rechargeTurnCourse.getBrandNumberFour());
            achiCommEntity.setSaleStaffId(rechargeTurnCourse.getSaleFourStaffId());
            achiCommEntity.setSaleStaffName(rechargeTurnCourse.getSaleFourStaffName());
            achiCommEntity.setSaleIdentityCard(rechargeTurnCourse.getSaleFourIdentityCard());
            achiCommEntity.setSaleRatio(rechargeTurnCourse.getSaleFourRatio());
            achiCommEntity.setProjectId(rechargeTurnCourse.getRechargeTypeNum());
            achiCommEntity.setCashAchi(BigDecimalUtils.bigDecimal("0"));
            achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            if(rechargeTurnCourse.getTypeRatio().compareTo(BigDecimalUtils.bigDecimal("1"))==1){
                //固定金额
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardComm(rechargeTurnCourse.getTypeRatio().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
            }else{
                //比例
                achiCommEntity.setCardAchi(rechargeTurnCourse.getRechargeInMoney().multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
                achiCommEntity.setCardComm(BigDecimalUtils.multiply(rechargeTurnCourse.getRechargeInMoney(),rechargeTurnCourse.getTypeRatio()).multiply(BigDecimalUtils.div(rechargeTurnCourse.getSaleFourRatio(),hundredBD)));
            }
            achiCommEntity.setTotalPerformance(achiCommEntity.getCardAchi());
            achiCommEntity.setTotalCommission(achiCommEntity.getCardComm());

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

    /*
     * 充值账户交易记录属性赋值
     * */
    public AccountTradeRecord setAccountTradeRecord(RechargeTurnCourse rechargeTurnCourse,RechargeAccount rechargeAccount){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(rechargeTurnCourse.getStoreId());
        accountTradeRecord.setStoreName(rechargeTurnCourse.getStoreName());
        accountTradeRecord.setMemberId(rechargeTurnCourse.getMemberId());
        accountTradeRecord.setMemberName(rechargeTurnCourse.getMemberName());
        accountTradeRecord.setMemberIdentity(rechargeTurnCourse.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(rechargeTurnCourse.getMobilePhone());
        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        if(rechargeTurnCourse.getState() == 0) {//审核
            accountTradeRecord.setAccountType(Constant.recharge_turn_account);
            accountTradeRecord.setInputDate(rechargeTurnCourse.getInputDate());
            accountTradeRecord.setInputBy(rechargeTurnCourse.getInputBy());
            accountTradeRecord.setAccountInMoney(rechargeTurnCourse.getRechargeInMoney());
        }else {//反审核
            accountTradeRecord.setAccountType(Constant.recharge_rollout_account);
            accountTradeRecord.setInputDate(rechargeTurnCourse.getUpdateDate());
            accountTradeRecord.setInputBy(rechargeTurnCourse.getUpdateBy());
            accountTradeRecord.setAccountSpendMoney(rechargeTurnCourse.getRechargeInMoney());
        }
        accountTradeRecord.setIsAddition(rechargeTurnCourse.getIsAddition());
        accountTradeRecord.setDocId(rechargeTurnCourse.getId());
        accountTradeRecord.setDocNum(rechargeTurnCourse.getDocNum());
        accountTradeRecord.setDocType(Constant.doc_type_recharge_turn_course);
        accountTradeRecord.setTradeId(rechargeTurnCourse.getRechargeTypeId());
        accountTradeRecord.setTradeName(rechargeTurnCourse.getRechargeTypeName());
        accountTradeRecord.setTradeMoney(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        accountTradeRecord.setAccountSpendMoney(rechargeTurnCourse.getCourseOutMoney());
        accountTradeRecord.setDebtMoney(BigDecimal.valueOf(0));
        accountTradeRecord.setPrice(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setCardMoney(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setCashMoney(BigDecimal.valueOf(0));
        return accountTradeRecord;
    }

    /*
     * 账户交易记录属性赋值 疗程
     * */
    public AccountTradeRecord setAccountTradeRecord(RechargeTurnCourse rechargeTurnCourse,CourseAccount courseAccount,CoursePayList payList){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(rechargeTurnCourse.getStoreId());
        accountTradeRecord.setStoreName(rechargeTurnCourse.getStoreName());
        accountTradeRecord.setMemberId(rechargeTurnCourse.getMemberId());
        accountTradeRecord.setMemberName(rechargeTurnCourse.getMemberName());
        accountTradeRecord.setMemberIdentity(rechargeTurnCourse.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(rechargeTurnCourse.getMobilePhone());
        accountTradeRecord.setAccountId(courseAccount.getId());
        accountTradeRecord.setAccountName(courseAccount.getCourseProjectName());

        if(rechargeTurnCourse.getState() == 0) { //审核
            accountTradeRecord.setAccountType(Constant.recharge_rollout_account_counrse);
            accountTradeRecord.setInputDate(rechargeTurnCourse.getInputDate());
            accountTradeRecord.setInputBy(rechargeTurnCourse.getInputBy());
            accountTradeRecord.setAccountSpendCount(payList.getRollOffCount());
            accountTradeRecord.setAccountSpendMoney(payList.getRollOffMoney());
            accountTradeRecord.setOrderStatu(0);
        }else { //反审核
            accountTradeRecord.setAccountType(Constant.recharge_turn_account_counrse);
            accountTradeRecord.setInputDate(rechargeTurnCourse.getUpdateDate());
            accountTradeRecord.setInputBy(rechargeTurnCourse.getUpdateBy());
            accountTradeRecord.setAccountInCount(payList.getRollOffCount());
            accountTradeRecord.setAccountInMoney(payList.getRollOffMoney());
            accountTradeRecord.setOrderStatu(1);
        }
        accountTradeRecord.setIsAddition(rechargeTurnCourse.getIsAddition());
        accountTradeRecord.setDocId(rechargeTurnCourse.getId());
        accountTradeRecord.setDocNum(rechargeTurnCourse.getDocNum());
        accountTradeRecord.setDocType(Constant.doc_type_recharge_turn_course);
        accountTradeRecord.setTradeId(courseAccount.getCourseProjectId());
        accountTradeRecord.setTradeName(courseAccount.getCourseProjectName());
        accountTradeRecord.setTradeMoney(payList.getRollOffMoney());
        accountTradeRecord.setTradeCount(payList.getRollOffCount());
        accountTradeRecord.setAccountRestCount(courseAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(courseAccount.getCourseConsumeCount());
        accountTradeRecord.setDebtMoney(BigDecimal.valueOf(0));
        accountTradeRecord.setPrice(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setAccountRestMoney(courseAccount.getCourseRestPrice());
        accountTradeRecord.setAccountConsumeMoney(courseAccount.getCourseConsumePrice());
        accountTradeRecord.setCardMoney(rechargeTurnCourse.getRechargeInMoney());
        accountTradeRecord.setCashMoney(BigDecimal.valueOf(0));
        return accountTradeRecord;
    }

    /*
     * 业务员赋值
     * */
    public void setStaffValue(List<StaffRequest> staffLists, RechargeTurnCourse rechargeList){
        for (int i = 0; i < staffLists.size(); i++) {
            StaffRequest staffRequest = staffLists.get(i);
            if (i == 0){
                rechargeList.setSaleOneStaffId(staffRequest.getId());
                rechargeList.setSaleOneStaffName(staffRequest.getStaffName());
                rechargeList.setSaleOneIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleOneRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberOne(staffRequest.getBrandNumber());
            }else if (i == 1){
                rechargeList.setSaleTwoStaffId(staffRequest.getId());
                rechargeList.setSaleTwoStaffName(staffRequest.getStaffName());
                rechargeList.setSaleTwoIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleTwoRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberTwo(staffRequest.getBrandNumber());
            }else if (i == 2){
                rechargeList.setSaleThreeStaffId(staffRequest.getId());
                rechargeList.setSaleThreeStaffName(staffRequest.getStaffName());
                rechargeList.setSaleThreeIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleThreeRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberThree(staffRequest.getBrandNumber());
            }else if (i == 3){
                rechargeList.setSaleFourStaffId(staffRequest.getId());
                rechargeList.setSaleFourStaffName(staffRequest.getStaffName());
                rechargeList.setSaleFourIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleFourRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberFour(staffRequest.getBrandNumber());
            }else if (i == 4){
                rechargeList.setSaleFiveStaffId(staffRequest.getId());
                rechargeList.setSaleFiveStaffName(staffRequest.getStaffName());
                rechargeList.setSaleFiveIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleFiveRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberFive(staffRequest.getBrandNumber());
            }
            else if (i == 5){
                rechargeList.setSaleSixStaffId(staffRequest.getId());
                rechargeList.setSaleSixStaffName(staffRequest.getStaffName());
                rechargeList.setSaleFourIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleSixRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberSix(staffRequest.getBrandNumber());
            }
            else if (i == 6){
                rechargeList.setSaleSevenStaffId(staffRequest.getId());
                rechargeList.setSaleSevenStaffName(staffRequest.getStaffName());
                rechargeList.setSaleSevenIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleSevenRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberSeven(staffRequest.getBrandNumber());
            }
            else if (i == 7){
                rechargeList.setSaleEightStaffId(staffRequest.getId());
                rechargeList.setSaleEightStaffName(staffRequest.getStaffName());
                rechargeList.setSaleEightIdentityCard(staffRequest.getIdentityCard());
                rechargeList.setSaleEightRatio(staffRequest.getRatio());
                rechargeList.setBrandNumberEight(staffRequest.getBrandNumber());
            }
        }
    }

    public String getNum(String date,String storeNum,Long storeId){
        Example example = new Example(RechargeTurnCourse.class);
        String num="ZCZ"+storeNum;
        Example.Criteria criteria = example.createCriteria();
        String replace = date.replace("-", "");
        criteria.andLike("docNum",num + replace + "%");
        criteria.andEqualTo("storeId",storeId);
        List<RechargeTurnCourse> list = dao.selectByExample(example);
        if(null != list){
            if(list.size()>0 ){
                if(list.size()>999){
                    num += date+(list.size()+1);
                }else  if(list.size()>99){
                    num += date+"0"+(list.size()+1);
                }else  if(list.size()>9){
                    num += date+"00"+(list.size()+1);
                }else{
                    num += date+"000"+(list.size()+1);
                }
            }else{
                num += date+"0001";
            }
        }else{
            num += date+"0001";
        }
        return num.replace("-","");
    }

    public String getStaffKey(String phone){
        return staffKey + phone;
    }


    private List<StaffRequest> setStaffValue(CoursePayList coursePayList) {
        List<StaffRequest> staffs = new ArrayList<>();
        if (coursePayList.getSaleOneStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleOneStaffId());
            staffRequest.setStaffName(coursePayList.getSaleOneStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleOneIdentity());
            staffRequest.setRatio(coursePayList.getSaleOneRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleOneNumber());
            staffs.add(staffRequest);
        }if (coursePayList.getSaleTwoStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleTwoStaffId());
            staffRequest.setStaffName(coursePayList.getSaleTwoStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleTwoIdentity());
            staffRequest.setRatio(coursePayList.getSaleTwoRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleTwoNumber());
            staffs.add(staffRequest);
        }
        if (coursePayList.getSaleThreeStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleThreeStaffId());
            staffRequest.setStaffName(coursePayList.getSaleThreeStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleThreeIdentity());
            staffRequest.setRatio(coursePayList.getSaleThreeRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleThreeNumber());
            staffs.add(staffRequest);
        }if (coursePayList.getSaleFourStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleFourStaffId());
            staffRequest.setStaffName(coursePayList.getSaleFourStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleFourIdentity());
            staffRequest.setRatio(coursePayList.getSaleFourRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleFourNumber());
            staffs.add(staffRequest);
        }if (coursePayList.getSaleFiveStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleFiveStaffId());
            staffRequest.setStaffName(coursePayList.getSaleFiveStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleFiveIdentity());
            staffRequest.setRatio(coursePayList.getSaleFiveRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleFiveNumber());
            staffs.add(staffRequest);
        }if (coursePayList.getSaleSixStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleSixStaffId());
            staffRequest.setStaffName(coursePayList.getSaleSixStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleSixIdentity());
            staffRequest.setRatio(coursePayList.getSaleSixRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleSixNumber());
            staffs.add(staffRequest);
        }if (coursePayList.getSaleSevenStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleSevenStaffId());
            staffRequest.setStaffName(coursePayList.getSaleSevenStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleSevenIdentity());
            staffRequest.setRatio(coursePayList.getSaleSevenRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleSevenNumber());
            staffs.add(staffRequest);
        }if (coursePayList.getSaleEightStaffId() != null){
            StaffRequest staffRequest = new StaffRequest();
            staffRequest.setId(coursePayList.getSaleEightStaffId());
            staffRequest.setStaffName(coursePayList.getSaleEightStaffName());
            staffRequest.setIdentityCard(coursePayList.getSaleEightIdentity());
            staffRequest.setRatio(coursePayList.getSaleEightRatio());
            staffRequest.setBrandNumber(coursePayList.getSaleEightNumber());
            staffs.add(staffRequest);
        }
        return staffs;
    }

}
