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.SsmCancelDebtListDao;
import com.ssm.manage.dao.SsmCourseDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.*;
import com.ssm.manage.response.CancelDebtListReponse;
import com.ssm.manage.response.CashListResponse;
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 tk.mybatis.mapper.entity.Example;

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


@Service
@Slf4j
public class SsmCancelDebtListServiceImpl  extends BaseServiceImpl<CancelDebtList> implements SsmCancelDebtListService {

    private static String staffKey = "staffKey";

    @Autowired
    private SsmCancelDebtListDao dao;

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

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmDebtService ssmDebtService;

    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;
    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;
    @Autowired
    private SsmCancelDebtMxService ssmCancelDebtMxService;

    @Autowired
    private SsmCancelDebtListService ssmCancelDebtListService;

    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;
    @Autowired
    private SsmRepayTradeService ssmRepayTradeService;

    @Autowired
    private SsmRepayService ssmRepayService;


    @Autowired
    private RedisClient<StaffResponse> redisClient;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmCourseDao ssmCourseDao;

    @Override
    public PageInfo<CancelDebtListReponse> selectPage(CancelDebtListRequest cancelDebtListRequest){
        PageInfo<CancelDebtListReponse> pageInfo = new PageInfo<>();
        Long total = 0L;
        List<CancelDebtListReponse> cancelDebtListRequestsReq = new ArrayList<>();
        //总店
        BaseStores baseStores = new BaseStores();
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(cancelDebtListRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                baseStores=pagesorganization;
            }
        }


        if(baseStores.getId().equals(cancelDebtListRequest.getStoreId())){ //总店登录
            List<CancelDebtListReponse> cancelDebtListRequestsZ = new ArrayList<>();
            for (BaseStores pagesorganization : pagesorganizations) {
                //总店查询全部
                if(pagesorganization.getId().equals(cancelDebtListRequest.getStoreId())){
                    List<Long> collect = pagesorganizations.stream().filter(item -> item.getParentId()!=null &&( item.getParentId() == cancelDebtListRequest.getStoreId() || item.getParentId().equals(cancelDebtListRequest.getStoreId()))).map(BaseStores::getId).collect(Collectors.toList());
                    collect.add(pagesorganization.getId());
                    Example example = new Example(CancelDebtList.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andIn("storeId",collect);
                    if (null != cancelDebtListRequest.getMemberName()){
                        criteria.andLike("memberName","%" + cancelDebtListRequest.getMemberName() + "%");
                    }
                    if (null != cancelDebtListRequest.getDocNum()){
                        criteria.andLike("docNum","%" + cancelDebtListRequest.getDocNum() + "%");
                    }if (null != cancelDebtListRequest.getMobilePhone()){
                        criteria.andLike("mobilePhone","%" + cancelDebtListRequest.getMobilePhone() + "%");
                    }
                    if (null != cancelDebtListRequest.getOrderCode()){
                        criteria.andLike("orderCode","%" + cancelDebtListRequest.getOrderCode() + "%");
                    }
                    if (null != cancelDebtListRequest.getState()){
                        criteria.andEqualTo("state",cancelDebtListRequest.getState());
                    }
                    cancelDebtListRequest.setSort("docDate");
                    List<CancelDebtList> list1 = selectByExample(example);
                    total = Long.valueOf(list1.size());
                    PageHelper.startPage(cancelDebtListRequest.getPageNum(),cancelDebtListRequest.getPageSize(),cancelDebtListRequest.getOrderAndSort());
                    List<CancelDebtList> list = selectByExample(example);
                    List<CancelDebtListReponse> cancelDebtListRequests = new ArrayList<>();
                    for (CancelDebtList cancelDebtList : list) {
                        CancelDebtListReponse cancelDebtListRequest1 = new CancelDebtListReponse();
                        BeanUtils.copyProperties(cancelDebtList,cancelDebtListRequest1);
                        if(cancelDebtList.getDocType().equals("promotion_card_use")){
                            CancelDebtMx cancelDebtMx = new CancelDebtMx();
                            cancelDebtMx.setCancelDebtId(cancelDebtList.getId());
                            List<CancelDebtMx> select = ssmCancelDebtMxService.select(cancelDebtMx);
                            cancelDebtListRequest1.setCancelDebtMxes(select);
                        }
                        cancelDebtListRequests.add(cancelDebtListRequest1);
                    }
                    cancelDebtListRequestsZ.addAll(cancelDebtListRequests);
                }
//                else{ //分店查询送审和审核和反审核
//                    Example example = new Example(CancelDebtList.class);
//                    Example.Criteria criteria = example.createCriteria();
//
//                    criteria.andEqualTo("storeId",pagesorganization.getId());
//                    if (null != cancelDebtListRequest.getMemberName()){
//                        criteria.andLike("memberName","%" + cancelDebtListRequest.getMemberName() + "%");
//                    }
//                    if (null != cancelDebtListRequest.getDocNum()){
//                        criteria.andLike("docNum","%" + cancelDebtListRequest.getDocNum() + "%");
//                    }
//                    if (null != cancelDebtListRequest.getOrderCode()){
//                        criteria.andLike("orderCode","%" + cancelDebtListRequest.getOrderCode() + "%");
//                    }
//                    criteria.andIn("state",Arrays.asList(1,3,4));
//                    PageHelper.startPage(cancelDebtListRequest.getPageNum(),cancelDebtListRequest.getPageSize(),cancelDebtListRequest.getOrderAndSort());
//                    List<CancelDebtList> list = selectByExample(example);
//                    List<CancelDebtListReponse> cancelDebtListRequests = new ArrayList<>();
//                    for (CancelDebtList cancelDebtList : list) {
//                        CancelDebtListReponse cancelDebtListRequest1 = new CancelDebtListReponse();
//                        BeanUtils.copyProperties(cancelDebtList,cancelDebtListRequest1);
//                        if(cancelDebtList.getDocType().equals("promotion_card_use")){
//                            CancelDebtMx cancelDebtMx = new CancelDebtMx();
//                            cancelDebtMx.setCancelDebtId(cancelDebtList.getId());
//                            List<CancelDebtMx> select = ssmCancelDebtMxService.select(cancelDebtMx);
//                            cancelDebtListRequest1.setCancelDebtMxes(select);
//                        }
//                        cancelDebtListRequests.add(cancelDebtListRequest1);
//                    }
//                    cancelDebtListRequestsZ.addAll(cancelDebtListRequests);
//                }
            }
            if(cancelDebtListRequestsZ!=null&&cancelDebtListRequestsZ.size()>0){
//                cancelDebtListRequestsZ.sort(Comparator.comparing(CancelDebtListReponse::getDocDate));
                cancelDebtListRequestsZ.sort(Comparator.comparing(CancelDebtListReponse::getInsertDate));
            }
            cancelDebtListRequestsReq.addAll(cancelDebtListRequestsZ);
        }
        else {
            Example example = new Example(CancelDebtList.class);
            Example.Criteria criteria = example.createCriteria();

            criteria.andEqualTo("storeId",cancelDebtListRequest.getStoreId());
            if (null != cancelDebtListRequest.getMemberName()){
                criteria.andLike("memberName","%" + cancelDebtListRequest.getMemberName() + "%");
            }
            if (null != cancelDebtListRequest.getMobilePhone()){
                criteria.andLike("mobilePhone","%" + cancelDebtListRequest.getMobilePhone() + "%");
            }
            if (null != cancelDebtListRequest.getDocNum()){
                criteria.andLike("docNum","%" + cancelDebtListRequest.getDocNum() + "%");
            }
            if (null != cancelDebtListRequest.getOrderCode()){
                criteria.andLike("orderCode","%" + cancelDebtListRequest.getOrderCode() + "%");
            }
            if (null != cancelDebtListRequest.getState()){
                criteria.andEqualTo("state",cancelDebtListRequest.getState());
            }
            cancelDebtListRequest.setSort("docDate");
            List<CancelDebtList> list1 = selectByExample(example);
            total = Long.valueOf(list1.size());
            PageHelper.startPage(cancelDebtListRequest.getPageNum(),cancelDebtListRequest.getPageSize(),cancelDebtListRequest.getOrderAndSort());
            List<CancelDebtList> list = selectByExample(example);
            List<CancelDebtListReponse> cancelDebtListRequests = new ArrayList<>();
            for (CancelDebtList cancelDebtList : list) {
                CancelDebtListReponse cancelDebtListRequest1 = new CancelDebtListReponse();
                BeanUtils.copyProperties(cancelDebtList,cancelDebtListRequest1);
                if(cancelDebtList.getDocType().equals("promotion_card_use")){
                    CancelDebtMx cancelDebtMx = new CancelDebtMx();
                    cancelDebtMx.setCancelDebtId(cancelDebtList.getId());
                    List<CancelDebtMx> select = ssmCancelDebtMxService.select(cancelDebtMx);
                    cancelDebtListRequest1.setCancelDebtMxes(select);
                }
                cancelDebtListRequests.add(cancelDebtListRequest1);
            }
            cancelDebtListRequestsReq.addAll(cancelDebtListRequests);
        }
        for (CancelDebtListReponse cancelDebtListReponse : cancelDebtListRequestsReq) {
            BaseStore baseStore = new BaseStore();
            baseStore.setId(cancelDebtListReponse.getStoreId());
            BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(baseStore.getId());
            cancelDebtListReponse.setStoreNum(baseStore1.getStoreNum());
        }

//        cancelDebtListRequestsReq.sort(Comparator.comparing(CancelDebtListReponse::getDocDate).reversed());
        if(cancelDebtListRequestsReq!=null && cancelDebtListRequestsReq.size()>0) {
            cancelDebtListRequestsReq.sort(Comparator.comparing(CancelDebtListReponse::getInsertDate).reversed());
        }
        pageInfo.setList(cancelDebtListRequestsReq);
        pageInfo.setTotal(total);
        return pageInfo;
    }

    @Override
    public PageInfo<CancelDebtListReponse> selectById(CancelDebtListRequest cancelDebtListRequest){
        PageInfo<CancelDebtListReponse> pageInfo = new PageInfo<>();
        Long total = 0L;
        List<CancelDebtListReponse> cancelDebtListRequestsReq = new ArrayList<>();
        //总店


            Example example = new Example(CancelDebtList.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",cancelDebtListRequest.getId());
            List<CancelDebtList> list1 = selectByExample(example);
            total = Long.valueOf(list1.size());
            PageHelper.startPage(cancelDebtListRequest.getPageNum(),cancelDebtListRequest.getPageSize(),cancelDebtListRequest.getOrderAndSort());
            List<CancelDebtList> list = selectByExample(example);
            List<CancelDebtListReponse> cancelDebtListRequests = new ArrayList<>();
            for (CancelDebtList cancelDebtList : list) {
                CancelDebtListReponse cancelDebtListRequest1 = new CancelDebtListReponse();
                BeanUtils.copyProperties(cancelDebtList,cancelDebtListRequest1);
                if(cancelDebtList.getDocType().equals("promotion_card_use")){
                    CancelDebtMx cancelDebtMx = new CancelDebtMx();
                    cancelDebtMx.setCancelDebtId(cancelDebtList.getId());
                    List<CancelDebtMx> select = ssmCancelDebtMxService.select(cancelDebtMx);
                    cancelDebtListRequest1.setCancelDebtMxes(select);
                }
                cancelDebtListRequests.add(cancelDebtListRequest1);
            }
            cancelDebtListRequestsReq.addAll(cancelDebtListRequests);
        for (CancelDebtListReponse cancelDebtListReponse : cancelDebtListRequestsReq) {
            BaseStore baseStore = new BaseStore();
            baseStore.setId(cancelDebtListReponse.getStoreId());
            BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(baseStore.getId());
            cancelDebtListReponse.setStoreNum(baseStore1.getStoreNum());
        }

//        cancelDebtListRequestsReq.sort(Comparator.comparing(CancelDebtListReponse::getDocDate).reversed());
        if(cancelDebtListRequestsReq!=null && cancelDebtListRequestsReq.size()>0) {
            cancelDebtListRequestsReq.sort(Comparator.comparing(CancelDebtListReponse::getInsertDate).reversed());
        }
        pageInfo.setList(cancelDebtListRequestsReq);
        pageInfo.setTotal(total);
        return pageInfo;
    }

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

        DebtList debtList = new DebtList();
        debtList.setId(cancelDebtListRequest.getDebtId());
        DebtList debtList1 = ssmDebtService.selectOne(debtList);
        CancelDebtList cancelDebtList1 = new CancelDebtList();
        cancelDebtList1.setDebtId(debtList1.getId());
        List<CancelDebtList> select = ssmCancelDebtListService.select(cancelDebtList1);
        if(select.size()>0){
            for (CancelDebtList cancelDebtList : select) {
                if(cancelDebtList.getState()!=3){
                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"该欠款单已经存在未审核的取消欠款单,不允许再次申请取消欠款");
                }
            }

        }
        if(debtList1.getDebtMoney().compareTo(cancelDebtListRequest.getCancelMoney())>-1){
            if("card_recharge".equals(debtList1.getDocType())){
                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);
                }
                CancelDebtList cancelDebtList = new CancelDebtList();
                BeanUtils.copyProperties(cancelDebtListRequest,cancelDebtList);
                cancelDebtList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                //创建SimpleDateFormat对象实例并定义好转换格式
                SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
                String formats = sdfs.format(new Date());
                String num = this.getNum(formats, cancelDebtListRequest.getStoreId());
                cancelDebtList.setOrderCode(num);
                cancelDebtList.setDocDate(new Date());
                cancelDebtList.setInsertId(staffResponse.getId());
                cancelDebtList.setInsertName(staffResponse.getStaffName());
                cancelDebtList.setInsertDate(new Date());
                cancelDebtList.setState(0);
                int insert = dao.insert(cancelDebtList);
                return insert;
            }
            else if("card_use".equals(debtList1.getDocType())||Constant.doc_type_card_change.equals(debtList1.getDocType())){
                CourseAccount courseAccount = new CourseAccount();
                courseAccount.setId(debtList1.getAccountId());
                CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                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);
                }
                CancelDebtList cancelDebtList = new CancelDebtList();
                BeanUtils.copyProperties(cancelDebtListRequest,cancelDebtList);
                cancelDebtList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                //创建SimpleDateFormat对象实例并定义好转换格式
                SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
                String formats = sdfs.format(new Date());
                String num = this.getNum(formats, cancelDebtListRequest.getStoreId());
                cancelDebtList.setOrderCode(num);
                cancelDebtList.setDocDate(new Date());
                cancelDebtList.setInsertId(staffResponse.getId());
                cancelDebtList.setInsertName(staffResponse.getStaffName());
                cancelDebtList.setInsertDate(new Date());
                cancelDebtList.setState(0);
                if(null == cancelDebtListRequest.getCancelNum()){
                    cancelDebtList.setCancelNum(0);
                }
                //对疗程单进行修改
//                CourseList courseList = new CourseList();
//                courseList.setCourseReatmentNum(cancelDebtList.getDocNum());
//                CourseList courseList1 = ssmCourseService.selectOne(courseList);
//                courseList1.setCollectionCashAmount(courseList1.getCollectionCashAmount().add(cancelDebtList.getCancelMoney()));
//                courseList1.setCourseReatmentAmount(courseList1.getCourseReatmentAmount().add(cancelDebtList.getCancelMoney()));
//                if(courseList1.getIsCooperation()==1){
//                    //疗程业绩
//                    courseList1.setAchievementAmount(courseList1.getAchievementAmount().add(cancelDebtList.getCancelMoney().subtract(cancelDebtList.getCancelMoney().multiply(courseList1.getBrokerage()))));
//                }
//                if(courseList1.getIsCooperation()==0){
//                    courseList1.setAchievementAmount(courseList1.getAchievementAmount().add(cancelDebtList.getCancelMoney()));
//                }
//                if(courseList1.getIsCooperation()==2){
//                    courseList1.setAchievementAmount(courseList1.getAchievementAmount().add(cancelDebtList.getCancelMoney().subtract(courseList1.getBrokerage().divide(courseList.getPriceMember()).multiply(cancelDebtList.getCancelMoney()))));
//                }
//                //欠款金额
//                courseList1.setDebtMoney(courseList1.getDebtMoney().subtract(cancelDebtList.getCancelMoney()));
//                ssmCourseService.updateOne(courseList1);
                int insert = dao.insert(cancelDebtList);
                return insert;
//                String s = String.valueOf(cancelDebtListRequest.getCancelMoney().divide(courseAccount1.getPrice(), 2, BigDecimal.ROUND_HALF_UP));
//                int nums =Double.valueOf(s).intValue();
//                if(nums==cancelDebtListRequest.getCancelNum()||++nums==cancelDebtListRequest.getCancelNum()){
//                    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);
//                    }
//                    CancelDebtList cancelDebtList = new CancelDebtList();
//                    BeanUtils.copyProperties(cancelDebtListRequest,cancelDebtList);
//                    cancelDebtList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
//                    //创建SimpleDateFormat对象实例并定义好转换格式
//                    SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
//                    String formats = sdfs.format(new Date());
//                    String num = this.getNum(formats, cancelDebtListRequest.getStoreId());
//                    cancelDebtList.setOrderCode(num);
//                    cancelDebtList.setDocDate(new Date());
//                    cancelDebtList.setInsertId(staffResponse.getId());
//                    cancelDebtList.setInsertName(staffResponse.getStaffName());
//                    cancelDebtList.setInsertDate(new Date());
//                    cancelDebtList.setState(0);
//                    int insert = dao.insert(cancelDebtList);
//                    return insert;
//                }else{
//                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"取消欠款次数范围不能大于欠款次数的大小范围一次");
//                }
            }
            else{
                List<CancelDebtMx> cancelDebtMxes = cancelDebtListRequest.getCancelDebtMxes();
                for (CancelDebtMx cancelDebtMx : cancelDebtMxes) {
                    if(null != cancelDebtMx.getCancelNum() &&cancelDebtMx.getCancelNum()>0){
                        if(cancelDebtMx.getCancelMoney().compareTo(cancelDebtMx.getDebtMoney())==1){
                            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"疗程项目"+cancelDebtMx.getCourseProjectNum()+"-"+cancelDebtMx.getCourseProjectName()+"取消欠款金额不能大于欠款金额");
                        }
                        if(cancelDebtMx.getCancelNum()>cancelDebtMx.getDebtCount()){
                            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"疗程项目"+cancelDebtMx.getCourseProjectNum()+"-"+cancelDebtMx.getCourseProjectName()+"取消欠款次数不能大于欠款次数");
                        }
                    }
                }

                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);
                }
                CancelDebtList cancelDebtList = new CancelDebtList();
                BeanUtils.copyProperties(cancelDebtListRequest,cancelDebtList);
                cancelDebtList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                //创建SimpleDateFormat对象实例并定义好转换格式
                SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
                String formats = sdfs.format(new Date());
                String num = this.getNum(formats, cancelDebtListRequest.getStoreId());
                cancelDebtList.setOrderCode(num);
                cancelDebtList.setDocDate(new Date());
                cancelDebtList.setInsertId(staffResponse.getId());
                cancelDebtList.setInsertName(staffResponse.getStaffName());
                cancelDebtList.setInsertDate(new Date());
                cancelDebtList.setState(0);
                int insert = dao.insert(cancelDebtList);
                if(insert>0){
                    for (CancelDebtMx cancelDebtMx : cancelDebtMxes) {
//                        if(null != cancelDebtMx.getCancelNum() &&cancelDebtMx.getCancelNum()>0){
                            cancelDebtMx.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                            cancelDebtMx.setCancelDebtId(cancelDebtList.getId());
                            cancelDebtMx.setDocPromotionId(debtList1.getDocId());
                            if(null == cancelDebtMx.getCancelNum()){
                                cancelDebtMx.setCancelNum(0);
                            }
                            ssmCancelDebtMxService.save(cancelDebtMx);
//                        }
                    }
                }
                return insert;
            }
        }else{
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"取消欠款金额不能大于欠款金额");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int update(CancelDebtListRequest cancelDebtListRequest, String staffPhone) throws SmsException {

        CancelDebtList cancelDebtList1 = new CancelDebtList();
        cancelDebtList1.setId(cancelDebtListRequest.getId());
        CancelDebtList cancelDebtList2 = dao.selectOne(cancelDebtList1);


        DebtList debtList = new DebtList();
        debtList.setId(cancelDebtList2.getDebtId());
        DebtList debtList1 = ssmDebtService.selectOne(debtList);
        if(debtList1.getDebtMoney().compareTo(cancelDebtListRequest.getCancelMoney())>-1){
            if("card_recharge".equals(debtList1.getDocType())){
                if(cancelDebtList2.getState()==0||cancelDebtList2.getState()==2||cancelDebtList2.getState()==4){

                    if(cancelDebtList2.getState()==cancelDebtListRequest.getState()){
                        StoreParament storeParament = StoreParaUtils.getStorePara(cancelDebtListRequest.getStoreId(), Constant.editOrderNum);
                        int days = (int) DateUtils.getDayDiff(cancelDebtListRequest.getDocDate(), new Date());
                        if(!"0".equals(storeParament.getParamentValue())) {
                            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
                            }
                        }
                    }
                    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);
                    }
                    cancelDebtList2.setCancelMoney(cancelDebtListRequest.getCancelMoney());
                    cancelDebtList2.setCancelNum(cancelDebtListRequest.getCancelNum());
                    cancelDebtList2.setUpdateId(staffResponse.getId());
                    cancelDebtList2.setUpdateName(staffResponse.getStaffName());
                    cancelDebtList2.setUpdateDate(new Date());
                    int insert = dao.updateByPrimaryKeySelective(cancelDebtList2);
                    return insert;
                }else{
                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATE,"该单据当前状态");
                }
            }else if("card_use".equals(debtList1.getDocType())){
                CourseAccount courseAccount = new CourseAccount();
                courseAccount.setId(debtList1.getAccountId());
                CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                if(cancelDebtList2.getState()==0||cancelDebtList2.getState()==2||cancelDebtList2.getState()==4){

                    if(cancelDebtList2.getState()==cancelDebtListRequest.getState()){
                        StoreParament storeParament = StoreParaUtils.getStorePara(cancelDebtListRequest.getStoreId(), Constant.editOrderNum);
                        int days = (int) DateUtils.getDayDiff(cancelDebtListRequest.getDocDate(), new Date());
                        if(!"0".equals(storeParament.getParamentValue())) {
                            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
                            }
                        }
                    }
                    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);
                    }
                    cancelDebtList2.setCancelMoney(cancelDebtListRequest.getCancelMoney());
                    cancelDebtList2.setCancelNum(cancelDebtListRequest.getCancelNum());
                    cancelDebtList2.setUpdateId(staffResponse.getId());
                    cancelDebtList2.setUpdateName(staffResponse.getStaffName());
                    cancelDebtList2.setUpdateDate(new Date());
                    int insert = dao.updateByPrimaryKeySelective(cancelDebtList2);
                    return insert;
                }else{
                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATE,"该单据当前状态");
                }
//                String s = String.valueOf(cancelDebtListRequest.getCancelMoney().divide(courseAccount1.getPrice(), 2, BigDecimal.ROUND_HALF_UP));
//                int nums =Double.valueOf(s).intValue();
//                if(nums==cancelDebtListRequest.getCancelNum()||++nums==cancelDebtListRequest.getCancelNum()){
//                    if(cancelDebtList2.getState()==0||cancelDebtList2.getState()==2||cancelDebtList2.getState()==4){
//
//                        if(cancelDebtList2.getState()==cancelDebtListRequest.getState()){
//                            StoreParament storeParament = StoreParaUtils.getStorePara(cancelDebtListRequest.getStoreId(), Constant.editOrderNum);
//                            int days = (int) DateUtils.getDayDiff(cancelDebtListRequest.getDocDate(), new Date());
//                            if(!"0".equals(storeParament.getParamentValue())) {
//                                if (Integer.parseInt(storeParament.getParamentValue()) < days) {
//                                    throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
//                                }
//                            }
//                        }
//                        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);
//                        }
//                        cancelDebtList2.setCancelMoney(cancelDebtListRequest.getCancelMoney());
//                        cancelDebtList2.setCancelNum(cancelDebtListRequest.getCancelNum());
//                        cancelDebtList2.setUpdateId(staffResponse.getId());
//                        cancelDebtList2.setUpdateName(staffResponse.getStaffName());
//                        cancelDebtList2.setUpdateDate(new Date());
//                        int insert = dao.updateByPrimaryKeySelective(cancelDebtList2);
//                        return insert;
//                    }else{
//                        throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATE,"该单据当前状态");
//                    }
//                }else{
//                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"取消欠款次数范围不能大于欠款次数的大小范围一次");
//                }
            }else{
                List<CancelDebtMx> cancelDebtMxes = cancelDebtListRequest.getCancelDebtMxes();
                for (CancelDebtMx cancelDebtMx : cancelDebtMxes) {
                    CourseAccount courseAccount = new CourseAccount();
                    courseAccount.setId(cancelDebtMx.getCourseId());
                    CourseAccount courseAccount1 = ssmCourseAccountService.selectByPrimaryKey(courseAccount.getId());
                    if(cancelDebtMx.getCancelMoney().compareTo(courseAccount1.getUnrecordedAmount())==1){
                        throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"疗程项目"+cancelDebtMx.getCourseProjectNum()+"-"+cancelDebtMx.getCourseProjectName()+"取消欠款金额不能大于欠款金额");
                    }
                    if(cancelDebtMx.getCancelNum()>courseAccount1.getUnrecordedNumber()){
                        throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"疗程项目"+cancelDebtMx.getCourseProjectNum()+"-"+cancelDebtMx.getCourseProjectName()+"取消欠款次数不能大于欠款次数");
                    }
                }
                if(cancelDebtList2.getState()==0||cancelDebtList2.getState()==2||cancelDebtList2.getState()==4){

                    if(cancelDebtList2.getState()==cancelDebtListRequest.getState()){
                        StoreParament storeParament = StoreParaUtils.getStorePara(cancelDebtListRequest.getStoreId(), Constant.editOrderNum);
                        int days = (int) DateUtils.getDayDiff(cancelDebtListRequest.getDocDate(), new Date());
                        if(!"0".equals(storeParament.getParamentValue())) {
                            if (Integer.parseInt(storeParament.getParamentValue()) < days) {
                                throw new SmsException(ResponseCode.CANCEL_NOT_PERMISSIONS, "单据超过允许修改时间");
                            }
                        }
                    }
                    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);
                    }
                    cancelDebtList2.setCancelMoney(cancelDebtListRequest.getCancelMoney());
                    cancelDebtList2.setCancelNum(cancelDebtListRequest.getCancelNum());
                    cancelDebtList2.setUpdateId(staffResponse.getId());
                    cancelDebtList2.setUpdateName(staffResponse.getStaffName());
                    cancelDebtList2.setUpdateDate(new Date());
                    int insert = dao.updateByPrimaryKeySelective(cancelDebtList2);
                    if(insert>0){
                        for (CancelDebtMx cancelDebtMx : cancelDebtMxes) {
                            ssmCancelDebtMxService.updateNotNull(cancelDebtMx);
                        }
                    }
                    return insert;
                }else{
                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATE,"该单据当前状态");
                }
            }
        }else{
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"取消欠款金额不能大于欠款金额");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int audit(CancelDebtList cancelDebtListRequest, String staffPhone) throws SmsException {
        Long TradId=null;
        List<Long> TradIds=new ArrayList<>();
        CancelDebtList cancelDebtList1 = new CancelDebtList();
        cancelDebtList1.setId(cancelDebtListRequest.getId());
        CancelDebtList cancelDebtList2 =  dao.selectOne(cancelDebtList1);
        DebtList debtList = new DebtList();
        debtList.setId(cancelDebtList2.getDebtId());
        DebtList debtList1 = ssmDebtService.selectOne(debtList);
        if(debtList1.getState()==0){
            if(cancelDebtList2.getState()==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);
                }
                cancelDebtList2.setUpdateId(staffResponse.getId());
                cancelDebtList2.setUpdateName(staffResponse.getStaffName());
                cancelDebtList2.setUpdateDate(new Date());
                cancelDebtList2.setState(3);
                int insert = dao.updateByPrimaryKeySelective(cancelDebtList2);
                if(insert>0){
                    if("card_recharge".equals(debtList1.getDocType())){
                        if(debtList1.getDebtMoney().compareTo(cancelDebtList2.getCancelMoney())==-1){
                            throw new SmsException(ResponseCode.MSG,"取消欠款金额不能比欠款金额大");
                        }
                        RechargeAccount rechargeAccount = new RechargeAccount();
                        rechargeAccount.setId(debtList1.getAccountId());
                        RechargeAccount rechargeAccount1 = ssmRechargeAccountService.selectOne(rechargeAccount);
                        cancelDebtList2.setBeforeRestMoney(rechargeAccount1.getCardRestMoney());
                        cancelDebtList2.setBeforeConsumeMoney(rechargeAccount1.getCardConsumeMoney());
                        cancelDebtList2.setBeforeGiftRestMoney(rechargeAccount1.getCardGiftMoney());
                        cancelDebtList2.setBeforeGiftConsumeMoney(rechargeAccount1.getGiftConsumeMoney());
                        if(debtList1.getDebtMoney().compareTo(cancelDebtList2.getCancelMoney())==0) {//全部取消
                            debtList1.setDebtMoney(debtList1.getDebtMoney().subtract(cancelDebtList2.getCancelMoney()));
                            rechargeAccount1.setCardConsumeMoney(rechargeAccount1.getCardRestMoney());
                            debtList1.setState(2);
//                            debtList1.setState(1);
                            ssmRechargeAccountService.updateNotNull(rechargeAccount1);
                        }else{
                            debtList1.setDebtMoney(debtList1.getDebtMoney().subtract(cancelDebtList2.getCancelMoney()));
                        }
                        cancelDebtList2.setBackRestMoney(rechargeAccount1.getCardRestMoney());
                        cancelDebtList2.setBackConsumeMoney(rechargeAccount1.getCardConsumeMoney());
                        cancelDebtList2.setBackGiftRestMoney(rechargeAccount1.getCardGiftMoney());
                        cancelDebtList2.setBackGiftConsumeMoney(rechargeAccount1.getGiftConsumeMoney());
//                        cancelDebtList2.setState(2);
                        int update = dao.updateByPrimaryKeySelective(cancelDebtList2);
                        if(update>0){
                            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeAccount1, cancelDebtList2,debtList1.getDebtMoney());
                            TradId=accountTradeRecord.getId();
                            TradIds.add(TradId);
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                        }
                    }
                    else if("card_use".equals(debtList1.getDocType())||Constant.doc_type_card_change.equals(debtList1.getDocType())){
                        if(debtList1.getDebtMoney().compareTo(cancelDebtList2.getCancelMoney())==-1){
                            throw new SmsException(ResponseCode.MSG,"取消欠款金额不能比欠款金额大");
                        }
                        if(debtList1.getDebtMoney().compareTo(cancelDebtList2.getCancelMoney())==0){

                            debtList1.setDebtMoney(debtList1.getDebtMoney().subtract(cancelDebtList2.getCancelMoney()));
                            CourseAccount courseAccount = new CourseAccount();
                            courseAccount.setId(debtList1.getAccountId());
                            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                            //全部取消
                            if(cancelDebtList2.getCancelNum()>courseAccount1.getCourseRestCount()-courseAccount1.getCourseConsumeCount()){
                                throw new SmsException(ResponseCode.MSG,"当前疗程不能取消大于"+(courseAccount1.getCourseRestCount()-courseAccount1.getCourseConsumeCount())+"次");
                            }
                            cancelDebtList2.setBeforePrice(courseAccount1.getPrice());
                            cancelDebtList2.setBeforeRestMoney(courseAccount1.getCourseRestPrice());
                            cancelDebtList2.setBeforeConsumeMoney(courseAccount1.getCourseConsumePrice());
                            cancelDebtList2.setBeforeRestCount(courseAccount1.getCourseRestCount());
                            cancelDebtList2.setBeforeConsumeCount(courseAccount1.getCourseConsumeCount());

                            BigDecimal divide = courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()), 2, BigDecimal.ROUND_HALF_UP);
                            courseAccount1.setPrice(divide);
                            courseAccount1.setCourseCount(courseAccount1.getCourseCount()-cancelDebtList2.getCancelNum());
                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-cancelDebtList2.getCancelNum());
                            courseAccount1.setCourseConsumeCount(courseAccount1.getCourseRestCount());
                            courseAccount1.setCourseConsumePrice(courseAccount1.getCourseRestPrice());

                            courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().subtract(cancelDebtList2.getCancelMoney()));
//                            courseAccount1.setCourseCount(courseAccount1.getCourseCount()-cancelDebtList2.getCancelNum());

                            if(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
                                if(courseAccount1.getCourseRestCount()!= 0){
                                    courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_HALF_UP));
                                }
                            }

                            cancelDebtList2.setBackPrice(courseAccount1.getPrice());
                            cancelDebtList2.setBackRestMoney(courseAccount1.getCourseRestPrice());
                            cancelDebtList2.setBackConsumeMoney(courseAccount1.getCourseConsumePrice());
                            cancelDebtList2.setBackRestCount(courseAccount1.getCourseRestCount());
                            cancelDebtList2.setBackConsumeCount(courseAccount1.getCourseConsumeCount());
////                            //获取已经还款的金额
////                            BigDecimal repayMoney=BigDecimal.valueOf(0);
////                            RepayTradeList repayTradeList = new RepayTradeList();
////                            repayTradeList.setDebtId(debtList1.getId());
////                            List<RepayTradeList> select = ssmRepayTradeService.select(repayTradeList);
////                            for (RepayTradeList tradeList : select) {
////                                RepayList repayList = new RepayList();
////                                repayList.setRepayNum(tradeList.getRepayNum());
////                                repayList.setStoreId(tradeList.getStoreId());
////                                RepayList repayList1 = ssmRepayService.selectOne(repayList);
////                                if(repayList1.getIsCancel()==0){
////                                    repayMoney=repayMoney.add(tradeList.getRepayMoney().add(tradeList.getPayRechargeMoney()));
////                                }
////                            }
//
////                            courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseCount().toString()),2,BigDecimal.ROUND_HALF_UP));
//                            courseAccount1.setCourseConsumePrice(courseAccount1.getCourseRestPrice());
//                            courseAccount1.setCourseConsumeCount(courseAccount1.getCourseRestCount());
                            debtList1.setState(2);
//                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-cancelDebtList2.getCancelNum());
                            ssmCourseAccountService.updateNotNull(courseAccount1);
                            int update = dao.updateByPrimaryKeySelective(cancelDebtList2);
                            if(update>0){
                                //查询对应疗程账户
//                                CourseListRequest courseListRequest = new CourseListRequest();
//                                courseListRequest.setDocId(cancelDebtList2.getDocNum());
//                                List<CourseList> courseLists = ssmCourseDao.selectCourseOne(courseListRequest);
//

                                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount1, cancelDebtList2,debtList1.getDebtMoney());
                                TradId=accountTradeRecord.getId();
                                TradIds.add(TradId);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                            }
                        }else{
                            debtList1.setDebtMoney(debtList1.getDebtMoney().subtract(cancelDebtList2.getCancelMoney()));
                            CourseAccount courseAccount = new CourseAccount();
                            courseAccount.setId(debtList1.getAccountId());
                            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                            cancelDebtList2.setBeforePrice(courseAccount1.getPrice());
                            cancelDebtList2.setBeforeRestMoney(courseAccount1.getCourseRestPrice());
                            cancelDebtList2.setBeforeConsumeMoney(courseAccount1.getCourseConsumePrice());
                            cancelDebtList2.setBeforeRestCount(courseAccount1.getCourseRestCount());
                            cancelDebtList2.setBeforeConsumeCount(courseAccount1.getCourseConsumeCount());

                            //全部取消
                            if(cancelDebtList2.getCancelNum()>courseAccount1.getCourseRestCount()-courseAccount1.getCourseConsumeCount()){
                                throw new SmsException(ResponseCode.MSG,"当前疗程不能取消大于"+(courseAccount1.getCourseRestCount()-courseAccount1.getCourseConsumeCount())+"次");
                            }

                            courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().subtract(cancelDebtList2.getCancelMoney()));
                            courseAccount1.setCourseCount(courseAccount1.getCourseCount()-cancelDebtList2.getCancelNum());
                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-cancelDebtList2.getCancelNum());
                            if(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
                                if(courseAccount1.getCourseRestCount()!= 0){
                                    courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_HALF_UP));
                                    courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumePrice().divide(courseAccount1.getPrice(),0,BigDecimal.ROUND_DOWN).intValue());
                                }
                            }


                            //获取已经还款的金额
//                            BigDecimal repayMoney=BigDecimal.valueOf(0);
//                            RepayTradeList repayTradeList = new RepayTradeList();
//                            repayTradeList.setDebtId(debtList1.getId());
//                            List<RepayTradeList> select = ssmRepayTradeService.select(repayTradeList);
//                            for (RepayTradeList tradeList : select) {
//                                RepayList repayList = new RepayList();
//                                repayList.setRepayNum(tradeList.getRepayNum());
//                                repayList.setStoreId(tradeList.getStoreId());
//                                RepayList repayList1 = ssmRepayService.selectOne(repayList);
//                                if(repayList1.getIsCancel()==0){
//                                    repayMoney=repayMoney.add(tradeList.getRepayMoney().add(tradeList.getPayRechargeMoney()));
//                                }
//                            }
                            //单据总金额
//                            BigDecimal add = debtList1.getRepayMoney().add(repayMoney);
                            cancelDebtList2.setBackPrice(courseAccount1.getPrice());
                            cancelDebtList2.setBackRestMoney(courseAccount1.getCourseRestPrice());
                            cancelDebtList2.setBackConsumeMoney(courseAccount1.getCourseConsumePrice());
                            cancelDebtList2.setBackRestCount(courseAccount1.getCourseRestCount());
                            cancelDebtList2.setBackConsumeCount(courseAccount1.getCourseConsumeCount());
//                            if(courseAccount1.getCourseRestCount()>0){
//                                if(courseAccount1.getCourseRestPrice().compareTo(BigDecimal.valueOf(0))==1){
//                                    BigDecimal multiply = courseAccount1.getCourseRestPrice().multiply(BigDecimal.valueOf(courseAccount1.getCourseRestCount())).divide(courseAccount1.getCourseProjectMoney(),8,BigDecimal.ROUND_HALF_UP).multiply(debtList1.getRepayConsumeMoney());
//                                    courseAccount1.setCourseConsumePrice(multiply);
//                                }
//                            }
//                            courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseCount().toString()),2,BigDecimal.ROUND_HALF_UP));
//                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-cancelDebtList2.getCancelNum());
                            ssmCourseAccountService.updateNotNull(courseAccount1);
                            int update = dao.updateByPrimaryKeySelective(cancelDebtList2);
                            if(update>0){
                                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount1, cancelDebtList2,debtList1.getDebtMoney());
                                TradId=accountTradeRecord.getId();
                                TradIds.add(TradId);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                            }
                        }
                    }
                    else{
                        //促销
                        //进行审核
                        CancelDebtMx cancelDebtMx = new CancelDebtMx();
                        cancelDebtMx.setCancelDebtId(cancelDebtList2.getId());
                        List<CancelDebtMx> select = ssmCancelDebtMxService.select(cancelDebtMx);
                        debtList1.setDebtMoney(debtList1.getDebtMoney().subtract(cancelDebtList2.getCancelMoney()));
                        for (CancelDebtMx debtMx : select) {
                            CourseAccount courseAccount = new CourseAccount();
                            courseAccount.setId(debtMx.getCourseId());
                            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                            if(null == courseAccount1.getUnrecordedAmount()){
                                courseAccount1.setUnrecordedAmount(BigDecimal.valueOf(0));
                            }
                            if(null == courseAccount1.getCancelMoney()){
                                courseAccount1.setCancelMoney(BigDecimal.valueOf(0));
                            }
                            if(null == courseAccount1.getUnrecordedNumber()){
                                courseAccount1.setUnrecordedNumber(0);
                            }
                            if(null == courseAccount1.getCancelNum()){
                                courseAccount1.setCancelNum(0);
                            }
                            BigDecimal subtract = courseAccount1.getUnrecordedAmount().subtract(courseAccount1.getCancelMoney());
                            int i = courseAccount1.getUnrecordedNumber() - courseAccount1.getCancelNum();
                            if(subtract.compareTo(debtMx.getCancelMoney())==-1){
                                throw new SmsException(ResponseCode.MSG,"取消欠款金额不能比欠款金额大");
                            }
                            if(i<debtMx.getCancelNum()){
                                throw new SmsException(ResponseCode.MSG,"取消欠款次数不能比欠款次数大");
                            }

                            debtMx.setBeforePrice(courseAccount1.getPrice());
                            debtMx.setBeforeRestMoney(courseAccount1.getCourseRestPrice());
                            debtMx.setBeforeConsumeMoney(courseAccount1.getCourseConsumePrice());
                            debtMx.setBeforeRestCount(courseAccount1.getCourseRestCount());
                            debtMx.setBeforeConsumeCount(courseAccount1.getCourseConsumeCount());
                            if(debtMx.getCancelMoney().compareTo(courseAccount1.getUnrecordedAmount().subtract(courseAccount1.getCancelMoney()))==0){ //全部取消
                                if(debtMx.getCancelNum()<=(courseAccount1.getUnrecordedNumber()-courseAccount1.getCancelNum())){
//                                    courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().subtract(debtMx.getCancelMoney()));
                                    courseAccount1.setCourseCount(courseAccount1.getCourseCount()-debtMx.getCancelNum());
                                    courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-debtMx.getCancelNum());
//                                  courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-debtMx.getCancelNum());
                                    if(courseAccount1.getCourseRestPrice().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
                                        if(courseAccount1.getCourseRestCount()!= 0){
                                            courseAccount1.setPrice(courseAccount1.getCourseRestPrice().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_HALF_UP));
                                        }
                                    }

                                    //courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseCount().toString()),2,BigDecimal.ROUND_HALF_UP));
                                    courseAccount1.setCourseConsumePrice(courseAccount1.getCourseRestPrice());
                                    courseAccount1.setCourseConsumeCount(courseAccount1.getCourseRestCount());
                                    courseAccount1.setCancelMoney(courseAccount1.getCancelMoney().add(debtMx.getCancelMoney()));
                                    courseAccount1.setCancelNum(courseAccount1.getCancelNum()+debtMx.getCancelNum());

                                    debtList1.setState(2);
                                    debtMx.setBackPrice(courseAccount1.getPrice());
                                    debtMx.setBackRestMoney(courseAccount1.getCourseRestPrice());
                                    debtMx.setBackConsumeMoney(courseAccount1.getCourseConsumePrice());
                                    debtMx.setBackRestCount(courseAccount1.getCourseRestCount());
                                    debtMx.setBackConsumeCount(courseAccount1.getCourseConsumeCount());
                                    int update = ssmCourseAccountService.updateNotNull(courseAccount1);
                                    if(update>0){
                                        AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount1, cancelDebtList2,debtMx,debtList1.getDebtMoney());
                                        TradId=accountTradeRecord.getId();
                                        TradIds.add(TradId);
                                        ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                                    }
                                }else{
                                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"取消次数不能大于欠款次数");
                                }
                            }else if(debtMx.getCancelMoney().compareTo(courseAccount1.getUnrecordedAmount().subtract(courseAccount1.getCancelMoney()))==-1){
                                if(debtMx.getCancelNum()<=(courseAccount1.getUnrecordedNumber()-courseAccount1.getCancelNum())){

                                    //总的取消欠款金额
                                    BigDecimal canceltotalMoney = BigDecimal.valueOf(0);
                                    Integer cancelTotalNum=0;
                                    CancelDebtList cancelDebtList = new CancelDebtList();
                                    cancelDebtList.setDebtId(debtList.getId());
                                    cancelDebtList.setState(3);
                                    List<CancelDebtList> select1 = ssmCancelDebtListService.select(cancelDebtList);
                                    for (CancelDebtList list : select1) {
                                        CancelDebtMx cancelDebtMx1 = new CancelDebtMx();
                                        cancelDebtMx1.setCancelDebtId(list.getId());
                                        cancelDebtMx1.setCourseId(courseAccount1.getId());
                                        List<CancelDebtMx> select2 = ssmCancelDebtMxService.select(cancelDebtMx1);
                                        for (CancelDebtMx mx : select2) {
                                            canceltotalMoney=canceltotalMoney.add(mx.getCancelMoney());
                                            cancelTotalNum=cancelTotalNum+mx.getCancelNum();
                                        }
                                    }


                                    courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().subtract(debtMx.getCancelMoney()));
                                    courseAccount1.setCourseCount(courseAccount1.getCourseCount()-debtMx.getCancelNum());
                                    courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-debtMx.getCancelNum());
//                                  courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()-debtMx.getCancelNum());
                                    BigDecimal price = courseAccount1.getPrice();
//                                    if(courseAccount1.getCourseCount()==courseAccount1.getCourseRestCount()){ //没有消费
//                                        courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseCount().toString()),2,BigDecimal.ROUND_HALF_UP));
//                                    }else{
//                                        Integer li = courseAccount1.getCourseCount()-courseAccount1.getCourseRestCount();
////                                        courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(price.multiply(BigDecimal.valueOf(li))).divide(BigDecimal.valueOf(courseAccount1.getCourseCount()-li),2,BigDecimal.ROUND_HALF_UP));
//                                    }
                                    if(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
                                        if(courseAccount1.getCourseRestCount()!= 0){
                                            courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_HALF_UP));
                                        }
                                    }
//                                    courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_HALF_UP));


                                    //取消的次数
                                    courseAccount1.setCancelMoney(courseAccount1.getCancelMoney().add(debtMx.getCancelMoney()));
                                    courseAccount1.setCancelNum(courseAccount1.getCancelNum()+debtMx.getCancelNum());

                                    debtMx.setBackPrice(courseAccount1.getPrice());
                                    debtMx.setBackRestMoney(courseAccount1.getCourseRestPrice());
                                    debtMx.setBackConsumeMoney(courseAccount1.getCourseConsumePrice());
                                    debtMx.setBackRestCount(courseAccount1.getCourseRestCount());
                                    debtMx.setBackConsumeCount(courseAccount1.getCourseConsumeCount());
                                    int update = ssmCourseAccountService.updateNotNull(courseAccount1);

                                    if(update>0){
                                        AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount1, cancelDebtList2,debtMx,debtList1.getDebtMoney());
                                        TradId=accountTradeRecord.getId();
                                        TradIds.add(TradId);
                                        ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                                    }
                                }else{
                                    throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"取消次数不能大于欠款次数");
                                }
                                ssmCancelDebtMxService.updateNotNull(debtMx);

                            }else{
                                throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"取消金额不能大于欠款金额");
                            }

                        }

                    }
                    int update = ssmDebtService.updateNotNull(debtList1);
//                    if(update>0){
//                        AccountTradeRecordRequest accountTradeRecord = new AccountTradeRecordRequest();
//                        accountTradeRecord.setId(TradId);
//                        accountTradeRecord.setInputDate(new Date());
//                        List<AccountTradeRecord> accountTradeRecords = ssmAccountTradeRecordService.selectById(accountTradeRecord);
//                        //添加交易记录Id集合
////                        if(){
////
////                        }
//                    }

                    return update;
                }else{
                    return 0;
                }

            }else{
                throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"该单据当前状态不允许");
            }
        }else{
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"该单据的欠款单已经还款，不允许操作");
        }

    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int submit(CancelDebtList cancelDebtListRequest, String staffPhone) throws SmsException {
        CancelDebtList cancelDebtList1 = new CancelDebtList();
        cancelDebtList1.setId(cancelDebtListRequest.getId());
        CancelDebtList cancelDebtList2 =  dao.selectOne(cancelDebtList1);

        DebtList debtList = new DebtList();
        debtList.setId(cancelDebtList2.getDebtId());
        DebtList debtList1 = ssmDebtService.selectOne(debtList);
        if(debtList1.getState()==0){
            if(cancelDebtList2.getState()==0 || cancelDebtList2.getState()==2 || cancelDebtList2.getState()==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);
                }
                cancelDebtList2.setUpdateId(staffResponse.getId());
                cancelDebtList2.setUpdateName(staffResponse.getStaffName());
                cancelDebtList2.setUpdateDate(new Date());
                cancelDebtList2.setState(1);
                int update = dao.updateByPrimaryKeySelective(cancelDebtList2);
                return update;
            }else{
                throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"该单据当前状态");
            }
        }else{
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"该单据的欠款单已经还款，不允许操作");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int recycle(CancelDebtList cancelDebtListRequest, String staffPhone) throws SmsException {
        CancelDebtList cancelDebtList1 = new CancelDebtList();
        cancelDebtList1.setId(cancelDebtListRequest.getId());
        CancelDebtList cancelDebtList2 =  dao.selectOne(cancelDebtList1);

        DebtList debtList = new DebtList();
        debtList.setId(cancelDebtList2.getDebtId());
        DebtList debtList1 = ssmDebtService.selectOne(debtList);
        if(debtList1.getState()==0){
            if(cancelDebtList2.getState()==0 || cancelDebtList2.getState()==1 || cancelDebtList2.getState()==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);
                }
                cancelDebtList2.setUpdateId(staffResponse.getId());
                cancelDebtList2.setUpdateName(staffResponse.getStaffName());
                cancelDebtList2.setUpdateDate(new Date());
                cancelDebtList2.setState(2);
                int update = dao.updateByPrimaryKeySelective(cancelDebtList2);
                return update;
            }else{
                throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"该单据当前状态，不允许操作");
            }
        }else{
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATES,"该单据的欠款单已经还款，不允许操作");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int theaudit(CancelDebtList cancelDebtListRequest, String staffPhone) throws SmsException {
        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        CancelDebtList cancelDebtList1 = new CancelDebtList();
        cancelDebtList1.setId(cancelDebtListRequest.getId());
        CancelDebtList cancelDebtList2 =  dao.selectOne(cancelDebtList1);
        Date updateDate = cancelDebtList2.getUpdateDate();
        DebtList debtList = new DebtList();
        debtList.setId(cancelDebtList2.getDebtId());
        DebtList debtList1 = ssmDebtService.selectOne(debtList);

        //校验是否存在最新单据
        CancelDebtList cancelDebtList = new CancelDebtList();
        cancelDebtList.setDebtId(debtList1.getId());
        cancelDebtList.setState(3);
        List<CancelDebtList> select1 = dao.select(cancelDebtList);
        for (CancelDebtList list : select1) {
            if(list.getInsertDate().after(cancelDebtList2.getInsertDate())){
                throw new SmsException(ResponseCode.MSG,"该取消欠款单不是关联欠款单的最新审核取消欠款单，不允许反审核");
            }
        }


        if(debtList1.getState()!=1){
            if(cancelDebtList2.getState()==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);
                }
                cancelDebtList2.setUpdateId(staffResponse.getId());
                cancelDebtList2.setUpdateName(staffResponse.getStaffName());
                cancelDebtList2.setUpdateDate(new Date());
                cancelDebtList2.setState(cancelDebtListRequest.getState());
                int insert = dao.updateByPrimaryKeySelective(cancelDebtList2);
                if(insert>0){
                    if("card_recharge".equals(debtList1.getDocType())){
                        RechargeAccount rechargeAccount = new RechargeAccount();
                        rechargeAccount.setId(debtList1.getAccountId());
                        RechargeAccount rechargeAccount1 = ssmRechargeAccountService.selectOne(rechargeAccount);
                        if(cancelDebtList2.getBackRestMoney().compareTo(rechargeAccount1.getCardRestMoney())!=0){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余金额与当前剩余金额不相等");
                        }
                        if(cancelDebtList2.getBackConsumeMoney().compareTo(rechargeAccount1.getCardConsumeMoney())!=0){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用金额与当前可用金额不相等");
                        }
                        if(cancelDebtList2.getBackGiftRestMoney().compareTo(rechargeAccount1.getCardGiftMoney())!=0){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后赠送剩余金额与当前赠送剩余金额不相等");
                        }
                        if(cancelDebtList2.getBackGiftConsumeMoney().compareTo(rechargeAccount1.getGiftConsumeMoney())!=0){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后赠送可用金额与当前赠送可用金额不相等");
                        }
                        rechargeAccount1.setCardRestMoney(cancelDebtList2.getBeforeRestMoney());
                        rechargeAccount1.setCardConsumeMoney(cancelDebtList2.getBeforeConsumeMoney());
                        rechargeAccount1.setCardGiftMoney(cancelDebtList2.getBeforeGiftRestMoney());
                        rechargeAccount1.setGiftConsumeMoney(cancelDebtList2.getBeforeGiftConsumeMoney());
                        if(debtList1.getDebtMoney().compareTo(BigDecimal.valueOf(0))==0) {
                            debtList1.setDebtMoney(debtList1.getDebtMoney().add(cancelDebtList2.getCancelMoney()));
                            debtList1.setState(0);
                        }else{
                            debtList1.setDebtMoney(debtList1.getDebtMoney().add(cancelDebtList2.getCancelMoney()));
                        }
                        int i = ssmRechargeAccountService.updateNotNull(rechargeAccount1);
                        if(i>0){
                            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(rechargeAccount1, cancelDebtList2,debtList1.getDebtMoney());
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                        }
                    }else if("card_use".equals(debtList1.getDocType())||Constant.doc_type_card_change.equals(debtList1.getDocType())){
                        CourseAccount courseAccount = new CourseAccount();
                        courseAccount.setId(debtList1.getAccountId());
                        CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                        if(cancelDebtList2.getBackPrice().compareTo(courseAccount1.getPrice())!=0){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后单价与当前单价不相等");
                        }
                        if(cancelDebtList2.getBackRestMoney().compareTo(courseAccount1.getCourseRestPrice())!=0){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余金额与当前剩余金额不相等");
                        }
                        if(cancelDebtList2.getBackConsumeMoney().compareTo(courseAccount1.getCourseConsumePrice())!=0){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用金额与当前可用金额不相等");
                        }
                        if(cancelDebtList2.getBackRestCount() != courseAccount1.getCourseRestCount()){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余次数与当前剩余次数不相等");
                        }
                        if(cancelDebtList2.getBackConsumeCount() != courseAccount1.getCourseConsumeCount()){
                            throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用次数与当前可用次数不相等");
                        }
                        courseAccount1.setPrice(cancelDebtList2.getBeforePrice());
                        courseAccount1.setCourseRestPrice(cancelDebtList2.getBeforeRestMoney());
                        courseAccount1.setCourseConsumePrice(cancelDebtList2.getBeforeConsumeMoney());
                        courseAccount1.setCourseRestCount(cancelDebtList2.getBeforeRestCount());
                        courseAccount1.setCourseConsumeCount(cancelDebtList2.getBeforeConsumeCount());
                        courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().add(cancelDebtList2.getCancelMoney()));
                        if(null != cancelDebtList2.getCancelNum()){
                            courseAccount1.setCourseCount(courseAccount1.getCourseCount()+cancelDebtList2.getCancelNum());
                        }

                        int i = ssmCourseAccountService.updateNotNull(courseAccount1);
                        debtList1.setDebtMoney(debtList1.getDebtMoney().add(cancelDebtList2.getCancelMoney()));
                        if(i>0){
                            AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount1, cancelDebtList2,debtList1.getDebtMoney());
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                        }

//
//                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
//                            String[] split = simpleDateFormat.format(updateDate).split("-");
//                            //开始日期
//                            Integer year =Integer.valueOf(split[0]);
//                            Integer month = 4;
//                            String months=split[1];
//                            Date startDate =null;
//                            Date startDates =null;
//                            //今天
//                            Date date = new Date();
//                            try {
//                                startDate= simpleDateFormat.parse(year+"-"+months);
//                            } catch (ParseException e) {
//                                e.printStackTrace();
//                            }
//                            while (startDate.before(date)){
//                                AccountTradeRecordRequest accountTradeRecord = new AccountTradeRecordRequest();
//                                accountTradeRecord.setInputDate(startDate);
//                                accountTradeRecord.setAccountId(cancelDebtList2.getAccountId());
//                                accountTradeRecord.setOrderStatu(0);
//                                List<AccountTradeRecord> accountTradeRecords = ssmAccountTradeRecordService.selectList(accountTradeRecord);
//                                if(null != accountTradeRecords && accountTradeRecords.size()>0){
//                                    Integer years =Integer.valueOf(split[0]);
//                                    Integer monthss = 4;
//                                    String months1=split[1];
//                                    try {
//                                        startDates= simpleDateFormat.parse(year+"-"+months);
//                                    } catch (ParseException e) {
//                                        e.printStackTrace();
//                                    }
//                                    List<AccountTradeRecord> accountTradeRecordRequests = new ArrayList<>();
//                                    for (AccountTradeRecord tradeRecord : accountTradeRecords) {
//                                        if(startDates.before(date)){
//                                            AccountTradeRecordRequest accountTradeRecord1 = new AccountTradeRecordRequest();
//                                            Example example = new Example(CourseAccount.class);
//                                            accountTradeRecord1.setDocId(tradeRecord.getAccountId());
//                                            accountTradeRecord1.setInputDate(startDates);
//                                            List<AccountTradeRecord> accountTradeRecords1 = ssmAccountTradeRecordService.selectList(accountTradeRecord1);
//                                            if(accountTradeRecords1.size()>0){
//                                                for (AccountTradeRecord record : accountTradeRecords1) {
//                                                    if(cancelDebtList2.getInsertDate().before(record.getInputDate())){
//                                                        accountTradeRecordRequests.add(record);
//                                                    }
//
//                                                }
//                                            }
//
//                                        }
//                                        monthss++;
//                                        if(monthss>12){
//                                            months1="01";
//                                            monthss=1;
//                                            years++;
//                                        }else{
//                                            if(monthss<10){
//                                                months1="0"+monthss;
//                                            }else{
//                                                months1 =String.valueOf(monthss);
//                                            }
//                                        }
//                                        try {
//                                            startDates= simpleDateFormat.parse(years+"-"+months1);
//                                        } catch (ParseException e) {
//                                            e.printStackTrace();
//                                        }
//
//                                    }
//                                    if(accountTradeRecordRequests.size()>0){
//                                        if(accountTradeRecordRequests.size()%2!=0){
//                                            throw new SmsException(ResponseCode.MSG,"当前相关卡包在还款之后已经进行过交易，无法作废");
//                                        }
//                                    }
////                                    else{
////                                        throw new SmsException(ResponseCode.MSG,"当前相关卡包在还款之后已经进行过交易，无法作废");
////                                    }
//
//                                }
//                                month++;
//                                if(month>12){
//                                    months="01";
//                                    month=1;
//                                    year++;
//                                }else{
//                                    if(month<10){
//                                        months="0"+month;
//                                    }else{
//                                        months =String.valueOf(month);
//                                    }
//                                }
//                                try {
//                                    startDate= simpleDateFormat.parse(year+"-"+months);
//                                } catch (ParseException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//
//
//
//                            debtList1.setDebtMoney(debtList1.getDebtMoney().add(cancelDebtList2.getCancelMoney()));
//                            CourseAccount courseAccount = new CourseAccount();
//                            courseAccount.setId(debtList1.getAccountId());
//                            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
////                        debtList1.setRepayCount(debtList1.getRepayCount()-cancelDebtList2.getCancelNum());
////                        debtList1.setRepayConsumeCount(debtList1.getRepayConsumeCount()-cancelDebtList2.getCancelNum());
//                            courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().add(cancelDebtList2.getCancelMoney()));
//                            courseAccount1.setCourseCount(courseAccount1.getCourseCount() + cancelDebtList2.getCancelNum());
//                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount() + cancelDebtList2.getCancelNum());
//                            if(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
//                                if(courseAccount1.getCourseRestCount()!= 0){
//                                    courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_HALF_UP));
//                                }
//                            }
////                            courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseCount().toString()), 2, BigDecimal.ROUND_HALF_UP));
//                            //可用金额
//                            BigDecimal CourseConsumePrice = courseAccount1.getCourseProjectMoneys().multiply(debtList1.getRepayConsumeMoney());
//
//                            courseAccount1.setCourseConsumePrice(CourseConsumePrice);
//                            courseAccount1.setCourseConsumeCount(Double.valueOf(CourseConsumePrice.divide(courseAccount1.getPrice(),2,BigDecimal.ROUND_HALF_UP).toString()).intValue());
//                            debtList1.setState(0);
////                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()+cancelDebtList2.getCancelNum());
//                            ssmCourseAccountService.updateNotNull(courseAccount1);
//                        }else {
//
//                            CourseAccount courseAccount = new CourseAccount();
//                            courseAccount.setId(debtList1.getAccountId());
//                            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
////                        debtList1.setRepayCount(debtList1.getRepayCount()-cancelDebtList2.getCancelNum());
////                        debtList1.setRepayConsumeCount(debtList1.getRepayConsumeCount()-cancelDebtList2.getCancelNum());
//                            courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().add(cancelDebtList2.getCancelMoney()));
//                            courseAccount1.setCourseCount(courseAccount1.getCourseCount() + cancelDebtList2.getCancelNum());
//                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount() + cancelDebtList2.getCancelNum());
//                            if(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
//                                if(courseAccount1.getCourseRestCount()!= 0){
//                                    courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_HALF_UP));
//                                }
//                            }
////                            courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseCount().toString()), 2, BigDecimal.ROUND_HALF_UP));
//
//                            ssmCourseAccountService.updateNotNull(courseAccount1);
//                        }
                    }else{
//                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
//                        String[] split = simpleDateFormat.format(updateDate).split("-");
//                        //开始日期
//                        Integer year =Integer.valueOf(split[0]);
//                        Integer month = 4;
//                        String months=split[1];
//                        Date startDate =null;
//                        Date startDates =null;
//                        //今天
//                        Date date = new Date();
//                        try {
//                            startDate= simpleDateFormat.parse(year+"-"+months);
//                        } catch (ParseException e) {
//                            e.printStackTrace();
//                        }
//                        while (startDate.before(date)){
//
//                            CourseAccount courseAccount = new CourseAccount();
//                            courseAccount.setPromotionDocId(debtList.getDocId());
//                            List<CourseAccount> select = ssmCourseAccountService.select(courseAccount);
//                            for (CourseAccount account : select) {
//                                AccountTradeRecordRequest accountTradeRecord = new AccountTradeRecordRequest();
//                                accountTradeRecord.setInputDate(startDate);
//                                accountTradeRecord.setAccountId(account.getId());
//                                accountTradeRecord.setOrderStatu(0);
//                                List<AccountTradeRecord> accountTradeRecords = ssmAccountTradeRecordService.selectList(accountTradeRecord);
//                                if(null != accountTradeRecords && accountTradeRecords.size()>0){
//                                    Integer years =Integer.valueOf(split[0]);
//                                    Integer monthss = 4;
//                                    String months1=split[1];
//                                    try {
//                                        startDates= simpleDateFormat.parse(year+"-"+months);
//                                    } catch (ParseException e) {
//                                        e.printStackTrace();
//                                    }
//                                    List<AccountTradeRecord> accountTradeRecordRequests = new ArrayList<>();
//                                    for (AccountTradeRecord tradeRecord : accountTradeRecords) {
//                                        if(startDates.before(date)){
//                                            AccountTradeRecordRequest accountTradeRecord1 = new AccountTradeRecordRequest();
//                                            accountTradeRecord1.setDocId(tradeRecord.getAccountId());
//                                            accountTradeRecord1.setInputDate(startDates);
//                                            List<AccountTradeRecord> accountTradeRecords1 = ssmAccountTradeRecordService.selectList(accountTradeRecord1);
//                                            if(accountTradeRecords1.size()>0){
//                                                for (AccountTradeRecord record : accountTradeRecords1) {
//                                                    if(cancelDebtList2.getInsertDate().before(record.getInputDate())){
//                                                        accountTradeRecordRequests.add(record);
//                                                    }
//                                                }
//                                            }
//                                        }
//                                        monthss++;
//                                        if(monthss>12){
//                                            months1="01";
//                                            monthss=1;
//                                            years++;
//                                        }else{
//                                            if(monthss<10){
//                                                months1="0"+monthss;
//                                            }else{
//                                                months1 =String.valueOf(monthss);
//                                            }
//                                        }
//                                        try {
//                                            startDates= simpleDateFormat.parse(years+"-"+months1);
//                                        } catch (ParseException e) {
//                                            e.printStackTrace();
//                                        }
//
//                                    }
//                                    if(accountTradeRecordRequests.size()>0) {
//                                        if (accountTradeRecordRequests.size() % 2 != 0) {
//                                            throw new SmsException(ResponseCode.MSG, "当前相关卡包在还款之后已经进行过交易，无法作废");
//                                        }
//                                    }
////                                    }else{
////                                        throw new SmsException(ResponseCode.MSG,"当前相关卡包在还款之后已经进行过交易，无法作废");
////                                    }
//                                }
//                            }
//                            month++;
//                            if(month>12){
//                                months="01";
//                                month=1;
//                                year++;
//                            }else{
//                                if(month<10){
//                                    months="0"+month;
//                                }else{
//                                    months =String.valueOf(month);
//                                }
//                            }
//                            try {
//                                startDate= simpleDateFormat.parse(year+"-"+months);
//                            } catch (ParseException e) {
//                                e.printStackTrace();
//                            }
//                        }

                        CancelDebtMx cancelDebtMx = new CancelDebtMx();
                        cancelDebtMx.setCancelDebtId(cancelDebtListRequest.getId());
                        List<CancelDebtMx> select = ssmCancelDebtMxService.select(cancelDebtMx);

                        for (CancelDebtMx debtMx : select) {
                            CourseAccount courseAccount = new CourseAccount();
                            courseAccount.setId(debtMx.getCourseId());
                            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
//                            if(debtList1.getDebtMoney().compareTo(BigDecimal.valueOf(0))==0){
//                                courseAccount1.setCourseProjectMoney(courseAccount1.getCourseProjectMoney().add(debtMx.getCancelMoney()));
//                                courseAccount1.setCourseCount(courseAccount1.getCourseCount() + debtMx.getCancelNum());
//                                courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount() + debtMx.getCancelNum());
//                                if(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
//                                    if(courseAccount1.getCourseRestCount()!= 0){
//                                        courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_DOWN));
//                                    }
//                                }
////                                courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseCount().toString()), 2, BigDecimal.ROUND_HALF_UP));
//                                //可用金额
//                                BigDecimal CourseConsumePrice = courseAccount1.getCourseRestPrice().multiply(debtList1.getRepayConsumeMoney());
//
//                                //花掉
//                                BigDecimal subtract = courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getCourseRestPrice().add(courseAccount1.getCancelMoney()));
//                                int count = courseAccount1.getCourseCount()-courseAccount1.getCourseRestCount();
//
//                                courseAccount1.setCourseConsumePrice(CourseConsumePrice);
//                                courseAccount1.setCourseConsumeCount(Double.valueOf(CourseConsumePrice.divide(courseAccount1.getPrice(),2,BigDecimal.ROUND_DOWN).toString()).intValue());
//                                debtList1.setState(0);
//                                courseAccount1.setCancelMoney(courseAccount1.getCancelMoney().subtract(debtMx.getCancelMoney()));
//                                courseAccount1.setCancelNum(courseAccount1.getCancelNum()-debtMx.getCancelNum());
//                                ssmCourseAccountService.updateNotNull(courseAccount1);
//                            }else{
////                                debtList1.setDebtMoney(debtList1.getDebtMoney().add(cancelDebtList2.getCancelMoney()));
//                                CourseAccount courseAccounta = new CourseAccount();
//                                courseAccounta.setId(debtMx.getCourseId());
//                                CourseAccount courseAccountas = ssmCourseAccountService.selectOne(courseAccounta);
////                              debtList1.setRepayCount(debtList1.getRepayCount()-cancelDebtList2.getCancelNum());
////                              debtList1.setRepayConsumeCount(debtList1.getRepayConsumeCount()-cancelDebtList2.getCancelNum());
//                                courseAccountas.setCourseProjectMoney(courseAccountas.getCourseProjectMoney().add(debtMx.getCancelMoney()));
//                                courseAccountas.setCourseCount(courseAccountas.getCourseCount() + debtMx.getCancelNum());
//                                courseAccountas.setCourseRestCount(courseAccountas.getCourseRestCount() + debtMx.getCancelNum());
//                                if(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).compareTo(BigDecimal.valueOf(0))==1){
//                                    if(courseAccount1.getCourseRestCount()!= 0){
//                                        courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(courseAccount1.getExpenditureMoney()).divide(BigDecimal.valueOf(courseAccount1.getCourseRestCount()),2,BigDecimal.ROUND_DOWN));
//                                    }
//                                }
////                                courseAccountas.setPrice(courseAccountas.getCourseProjectMoney().divide(BigDecimalUtils.bigDecimal(courseAccountas.getCourseCount().toString()), 2, BigDecimal.ROUND_HALF_UP));
//                                courseAccount1.setCancelMoney(courseAccount1.getCancelMoney().subtract(debtMx.getCancelMoney()));
//                                courseAccount1.setCancelNum(courseAccount1.getCancelNum()-debtMx.getCancelNum());
//                                ssmCourseAccountService.updateNotNull(courseAccountas);
//                            }
//                            debtList1.setRepayCount(debtList1.getRepayCount()-debtMx.getCancelNum());
//                            debtList1.setRepayConsumeCount(debtList1.getRepayConsumeCount()-debtMx.getCancelNum());
                            courseAccount1.setCancelMoney(courseAccount1.getCancelMoney().subtract(debtMx.getCancelMoney()));
                            courseAccount1.setCancelNum(courseAccount1.getCancelNum()-debtMx.getCancelNum());
                            if(!cancelDebtList2.getDocType().equals(Constant.promotion_card_use)){


                            if(debtMx.getBackPrice().compareTo(courseAccount1.getPrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后单价与当前单价不相等");
                            }
                            if(debtMx.getBackRestMoney().compareTo(courseAccount1.getCourseRestPrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余金额与当前剩余金额不相等");
                            }
                            if(debtMx.getBackConsumeMoney().compareTo(courseAccount1.getCourseConsumePrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用金额与当前可用金额不相等");
                            }
                            if(debtMx.getBackRestCount() != courseAccount1.getCourseRestCount()){
                                throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后剩余次数与当前剩余次数不相等");
                            }
                            if(debtMx.getBackConsumeCount() != courseAccount1.getCourseConsumeCount()){
                                throw new SmsException(ResponseCode.MSG,"该取消欠款单审核后可用次数与当前可用次数不相等");
                            }
                            }
                            courseAccount1.setPrice(debtMx.getBeforePrice());
                            courseAccount1.setCourseRestPrice(debtMx.getBeforeRestMoney());
                            courseAccount1.setCourseConsumePrice(debtMx.getBeforeConsumeMoney());
                            courseAccount1.setCourseRestCount(debtMx.getBeforeRestCount());
                            courseAccount1.setCourseConsumeCount(debtMx.getBeforeConsumeCount());
                            int i = ssmCourseAccountService.updateNotNull(courseAccount1);
                            debtList1.setDebtMoney(debtList1.getDebtMoney().add(debtMx.getCancelMoney()));
                            if(i>0){
                                AccountTradeRecord accountTradeRecord = this.setAccountTradeRecord(courseAccount1, cancelDebtList2,debtMx,debtList1.getDebtMoney());
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                            }

                        }

                    }
                    debtList1.setState(0);
                    int update = ssmDebtService.updateNotNull(debtList1);
                    return update;
                }else{
                    return 0;
                }

            }else{
                throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESS,"该单据当前状态");
            }
        }else{
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSSS,"该取消还款的欠款单已经还款，不允许反审核");
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int delete(CancelDebtList cancelDebtListRequest) throws SmsException {
        CancelDebtList cancelDebtList1 = new CancelDebtList();
        cancelDebtList1.setId(cancelDebtListRequest.getId());
        CancelDebtList cancelDebtList2 =  dao.selectOne(cancelDebtList1);
        if(cancelDebtList2.getState()==0){
            return dao.deleteByPrimaryKey(cancelDebtList1.getId());
        }else{
            throw new SmsException(ResponseCode.DATA_NOT_ALLOW_UPDATESSS,"该单据当前状态不允许");
        }
    }

    @Override
    public void insert(CancelDebtList cancelDebtList) {
        dao.insert(cancelDebtList);
    }

    @Override
    public void deleteByDocNum(CancelDebtList cancelDebtList) {
        dao.deleteByDocNum(cancelDebtList);
    }

    public String getNum(String date,Long storeId){
        Example example = new Example(CancelDebtList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("orderCode","%" + date + "%");
        criteria.andEqualTo("storeId",storeId);
        List<CancelDebtList> list = dao.selectByExample(example);
        String num=null;
        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;
    }

    public String getKey(Long storeId, String key){
        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);
        key = baseStore.getStoreNum() + key;
        return key;
    }

    //添加取消欠款交易记录
    public AccountTradeRecord setAccountTradeRecord(RechargeAccount rechargeAccount, CancelDebtList cancelDebtList,BigDecimal debtMoney){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(cancelDebtList.getStoreId());
        accountTradeRecord.setStoreName(cancelDebtList.getStoreName());
        accountTradeRecord.setMemberId(cancelDebtList.getMemberId());
        accountTradeRecord.setMemberName(cancelDebtList.getMemberName());
        accountTradeRecord.setMemberIdentity(cancelDebtList.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(cancelDebtList.getMobilePhone());
        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        if(cancelDebtList.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.cancel_Debt);
            accountTradeRecord.setAccountSpendMoney(cancelDebtList.getCancelMoney());
//            accountTradeRecord.setAccountSpendCount(cancelDebtList.getCancelNum());
            accountTradeRecord.setInputDate(new Date());
            accountTradeRecord.setInputBy(cancelDebtList.getInsertName());
            accountTradeRecord.setDebtMoney(debtMoney);
            accountTradeRecord.setOrderStatu(0);
        }else {
            accountTradeRecord.setAccountType(Constant.cancel_Debt_cancel);
            accountTradeRecord.setAccountInMoney(cancelDebtList.getCancelMoney());
//            accountTradeRecord.setAccountInCount(cancelDebtList.getCancelNum());
            accountTradeRecord.setInputDate(cancelDebtList.getUpdateDate());
            accountTradeRecord.setInputBy(cancelDebtList.getUpdateName());
            accountTradeRecord.setDebtMoney(debtMoney);
//            accountTradeRecord.setAccountGiftSpendMoney(BigDecimal.valueOf(0));
//            accountTradeRecord.setAccountInCount(repayAccountListRequest.getPayNumber());
            accountTradeRecord.setOrderStatu(1);
        }
        accountTradeRecord.setDocId(cancelDebtList.getId());
        accountTradeRecord.setDocNum(cancelDebtList.getDocNum());
        accountTradeRecord.setDocType(Constant.cancel_Debt);
        accountTradeRecord.setTradeId(cancelDebtList.getTradeId());
        accountTradeRecord.setTradeName(cancelDebtList.getTradeName());
        accountTradeRecord.setTradeMoney(cancelDebtList.getCancelMoney());
//        accountTradeRecord.setAccountRestCount(rechargeAccount.getCourseRestCount());
//        accountTradeRecord.setAccountConsumeCount(rechargeAccount.getCourseConsumeCount());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setPrice(cancelDebtList.getCancelMoney());

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

    //添加取消欠款交易记录
    public AccountTradeRecord setAccountTradeRecord(CourseAccount rechargeAccount, CancelDebtList cancelDebtList,BigDecimal debtMoney){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(cancelDebtList.getStoreId());
        accountTradeRecord.setStoreName(cancelDebtList.getStoreName());
        accountTradeRecord.setMemberId(cancelDebtList.getMemberId());
        accountTradeRecord.setMemberName(cancelDebtList.getMemberName());
        accountTradeRecord.setMemberIdentity(cancelDebtList.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(cancelDebtList.getMobilePhone());
        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getCourseProjectName());
        if(cancelDebtList.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.cancel_Debt);
            accountTradeRecord.setAccountSpendMoney(cancelDebtList.getCancelMoney());
            accountTradeRecord.setAccountSpendCount(cancelDebtList.getCancelNum());
            accountTradeRecord.setInputDate(new Date());
            accountTradeRecord.setInputBy(cancelDebtList.getInsertName());
            accountTradeRecord.setDebtMoney(debtMoney);
            accountTradeRecord.setOrderStatu(0);
        }else {
            accountTradeRecord.setAccountType(Constant.cancel_Debt_cancel);
            accountTradeRecord.setAccountInMoney(cancelDebtList.getCancelMoney());
            accountTradeRecord.setAccountInCount(cancelDebtList.getCancelNum());
            accountTradeRecord.setInputDate(cancelDebtList.getUpdateDate());
            accountTradeRecord.setInputBy(cancelDebtList.getUpdateName());
//            accountTradeRecord.setAccountGiftSpendMoney(BigDecimal.valueOf(0));
//            accountTradeRecord.setAccountInCount(repayAccountListRequest.getPayNumber());
            accountTradeRecord.setDebtMoney(debtMoney);
            accountTradeRecord.setOrderStatu(1);
        }
        accountTradeRecord.setDocId(cancelDebtList.getId());
        accountTradeRecord.setDocNum(cancelDebtList.getDocNum());
        accountTradeRecord.setDocType(Constant.cancel_Debt);
        accountTradeRecord.setTradeId(cancelDebtList.getTradeId());
        accountTradeRecord.setTradeName(cancelDebtList.getTradeName());
        accountTradeRecord.setTradeMoney(cancelDebtList.getCancelMoney());
        accountTradeRecord.setAccountRestCount(rechargeAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(rechargeAccount.getCourseConsumeCount());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCourseRestPrice());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCourseConsumePrice());
//        accountTradeRecord.setDebtMoney(cancelDebtList.getDebtMoney());
        accountTradeRecord.setPrice(rechargeAccount.getPrice());

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

    //添加取消欠款交易记录
    public AccountTradeRecord setAccountTradeRecord(CourseAccount rechargeAccount, CancelDebtList cancelDebtList,CancelDebtMx cancelDebtMx,BigDecimal debtMoney){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(cancelDebtList.getStoreId());
        accountTradeRecord.setStoreName(cancelDebtList.getStoreName());
        accountTradeRecord.setMemberId(cancelDebtList.getMemberId());
        accountTradeRecord.setMemberName(cancelDebtList.getMemberName());
        accountTradeRecord.setMemberIdentity(cancelDebtList.getMemberIdentityCard());
        accountTradeRecord.setMemberPhone(cancelDebtList.getMobilePhone());
        accountTradeRecord.setAccountId(cancelDebtMx.getId());
        accountTradeRecord.setAccountName(cancelDebtMx.getCourseProjectName());
        if(cancelDebtList.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.cancel_Debt);
            accountTradeRecord.setAccountSpendMoney(cancelDebtMx.getCancelMoney());
            accountTradeRecord.setAccountSpendCount(cancelDebtMx.getCancelNum());
            accountTradeRecord.setInputDate(new Date());
            accountTradeRecord.setDebtMoney(debtMoney);
            accountTradeRecord.setInputBy(cancelDebtList.getInsertName());
            accountTradeRecord.setOrderStatu(0);
        }else {
            accountTradeRecord.setAccountType(Constant.cancel_Debt_cancel);
            accountTradeRecord.setAccountInMoney(cancelDebtMx.getCancelMoney());
            accountTradeRecord.setAccountInCount(cancelDebtMx.getCancelNum());
            accountTradeRecord.setInputDate(cancelDebtList.getUpdateDate());
            accountTradeRecord.setInputBy(cancelDebtList.getUpdateName());
            accountTradeRecord.setDebtMoney(debtMoney);
            accountTradeRecord.setOrderStatu(1);
//            accountTradeRecord.setAccountGiftSpendMoney(BigDecimal.valueOf(0));
//            accountTradeRecord.setAccountInCount(repayAccountListRequest.getPayNumber());
        }
        accountTradeRecord.setDocId(cancelDebtList.getId());
        accountTradeRecord.setDocNum(cancelDebtList.getDocNum());
        accountTradeRecord.setDocType(Constant.cancel_Debt);
        accountTradeRecord.setTradeId(cancelDebtList.getTradeId());
        accountTradeRecord.setTradeName(cancelDebtList.getTradeName());
        accountTradeRecord.setTradeMoney(cancelDebtList.getCancelMoney());
        accountTradeRecord.setAccountRestCount(rechargeAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(rechargeAccount.getCourseConsumeCount());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCourseRestPrice());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCourseConsumePrice());
//        accountTradeRecord.setDebtMoney(cancelDebtMx.getDebtMoney());
        accountTradeRecord.setPrice(rechargeAccount.getPrice());

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

}
