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.AESUtils;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.*;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.TurnMembershipCardMxRequest;
import com.ssm.manage.query.TurnMembershipCardRequest;
import com.ssm.manage.response.BaseZengkaResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.response.TurnMembershipCardResponse;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.util.RedisClient;
import org.apache.commons.lang.StringUtils;
import org.reflections.Store;
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.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class SsmTurnMembershipCardServiceImpl extends BaseServiceImpl<TurnMembershipCard> implements SsmTurnMembershipCardService {

    @Autowired
    private SsmTurnMembershipCardDao dao;
    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;
    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;
    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;
    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private SsmRechargeTypeSevice ssmRechargeTypeSevice;
    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Autowired
    private SsmRechargeAccountGiveDao ssmRechargeAccountGiveDao;


    @Autowired
    private SsmTurnMembershipCardMxService ssmTurnMembershipCardMxService;

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

    @Autowired
    private RedisClient<StaffResponse> templateRedisClient;

    @Override
    public PageInfo<TurnMembershipCardResponse> selectPage(TurnMembershipCardRequest turnMembershipCardRequest) {
        Example example = new Example(TurnMembershipCard.class);
        Example.Criteria criteria = example.createCriteria();

        //重点
//        criteria.andEqualTo("storeId",turnMembershipCardRequest.getStoreId());
        if (null != turnMembershipCardRequest.getStoreIds() &&turnMembershipCardRequest.getStoreIds().size()>0){
            criteria.andIn("storeId",turnMembershipCardRequest.getStoreIds());
        }
//        if (null != turnMembershipCardRequest.getRollInMemberName()){
//            criteria.andLike("rollInMemberName","%" + turnMembershipCardRequest.getRollInMemberName() + "%");
//        }
        if (null != turnMembershipCardRequest.getRollInMemberNum()){
            criteria.andLike("rollInMemberNum","%" + turnMembershipCardRequest.getRollInMemberNum() + "%");
        }
        //重点
        if (null != turnMembershipCardRequest.getRollOutMemberName() && turnMembershipCardRequest.getRollOutMemberName().length()>0){
            criteria.andLike("rollOutMemberName","%" + turnMembershipCardRequest.getRollOutMemberName() + "%")
                    .orLike("rollInMemberName","%" + turnMembershipCardRequest.getRollOutMemberName() + "%");
        }
        if (null != turnMembershipCardRequest.getRollOutMemberNum()){
            criteria.andLike("rollOutMemberNum","%" + turnMembershipCardRequest.getRollOutMemberNum() + "%");
        }
        if (null != turnMembershipCardRequest.getDocDate()){
            criteria.andLike("docDate","%" + turnMembershipCardRequest.getDocDate() + "%");
        }
        //重点
        if(null != turnMembershipCardRequest.getRollInMobilePhone() && turnMembershipCardRequest.getRollInMobilePhone().length()>0){
            criteria.andLike("rollOutMobilePhone","%" + turnMembershipCardRequest.getRollInMobilePhone() + "%")
                    .orLike("rollInMobilePhone","%" + turnMembershipCardRequest.getRollInMobilePhone() + "%");
        }
        //重点
        if (null != turnMembershipCardRequest.getState()){
            criteria.andEqualTo("state",turnMembershipCardRequest.getState());
        }
        if(turnMembershipCardRequest.getDocNum()!=null){
            criteria.andEqualTo("docNum",turnMembershipCardRequest.getDocNum());
        }
//        turnMembershipCardRequest.setSort("state");
        PageHelper.startPage(turnMembershipCardRequest.getPageNum(),turnMembershipCardRequest.getPageSize(),turnMembershipCardRequest.getOrderAndSort());
        List<TurnMembershipCard> list = dao.selectByExample(example);


        Example examples = new Example(TurnMembershipCard.class);
        Example.Criteria criterias = examples.createCriteria();

        criterias.andEqualTo("storeId",turnMembershipCardRequest.getStoreId());
        if (null != turnMembershipCardRequest.getRollInMemberName()){
            criterias.andLike("rollInMemberName","%" + turnMembershipCardRequest.getRollInMemberName() + "%");
        }
        if (null != turnMembershipCardRequest.getRollInMemberNum()){
            criterias.andLike("rollInMemberNum","%" + turnMembershipCardRequest.getRollInMemberNum() + "%");
        }
        if (null != turnMembershipCardRequest.getRollOutMemberName()){
            criterias.andLike("rollOutMemberName","%" + turnMembershipCardRequest.getRollOutMemberName() + "%");
        }
        if (null != turnMembershipCardRequest.getRollOutMemberNum()){
            criterias.andLike("rollOutMemberNum","%" + turnMembershipCardRequest.getRollOutMemberNum() + "%");
        }
        if (null != turnMembershipCardRequest.getDocDate()){
            criterias.andLike("docDate","%" + turnMembershipCardRequest.getDocDate() + "%");
        }
        if (null != turnMembershipCardRequest.getState()){
            criterias.andEqualTo("state",turnMembershipCardRequest.getState());
        }
        if(turnMembershipCardRequest.getDocNum()!=null){
            criteria.andEqualTo("docNum",turnMembershipCardRequest.getDocNum());
        }
        int size = dao.selectByExample(examples).size();
        List<TurnMembershipCardResponse> responses= new ArrayList<>();
        for (TurnMembershipCard turnMembershipCard : list) {
            TurnMembershipCardResponse turnMembershipCardResponse = new TurnMembershipCardResponse();
            BeanUtils.copyProperties(turnMembershipCard,turnMembershipCardResponse);
            TurnMembershipCardMx turnMembershipCardMx = new TurnMembershipCardMx();
            turnMembershipCardMx.setTurnMembershipCardId(turnMembershipCard.getId());
            List<TurnMembershipCardMx> select = ssmTurnMembershipCardMxService.select(turnMembershipCardMx);
            turnMembershipCardResponse.setTurnMembershipCardMxList(select);
            responses.add(turnMembershipCardResponse);
        }
        PageInfo<TurnMembershipCardResponse> pageInfo = new PageInfo<>(responses);

        pageInfo.setTotal(size);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized int add(TurnMembershipCardRequest turnMembershipCardRequest,String key) throws SmsException {
        TurnMembershipCard turnMembershipCard = new TurnMembershipCard();
        BeanUtils.copyProperties(turnMembershipCardRequest,turnMembershipCard);
        if (templateRedisClient.hasKey(getKey(key))){
            StaffResponse staffResponse = templateRedisClient.get(getKey(key));
            turnMembershipCard.setInputBy(staffResponse.getStaffName());
            turnMembershipCard.setInputDate(new Date());
        }
        turnMembershipCard.setDocDate(new Date());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String num = this.getNum(format, turnMembershipCardRequest.getStoreId());
        turnMembershipCard.setDocNum(num);
        turnMembershipCard.setState(0);
        turnMembershipCard.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());

        return dao.insert(turnMembershipCard);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized int addNew(TurnMembershipCardRequest turnMembershipCardRequest,String key) throws SmsException {
        TurnMembershipCard turnMembershipCard = new TurnMembershipCard();
        BeanUtils.copyProperties(turnMembershipCardRequest,turnMembershipCard);
        if (templateRedisClient.hasKey(getKey(key))){
            StaffResponse staffResponse = templateRedisClient.get(getKey(key));
            turnMembershipCard.setInputBy(staffResponse.getStaffName());
            turnMembershipCard.setInputDate(new Date());
        }
        turnMembershipCard.setDocDate(new Date());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String num = this.getNum(format, turnMembershipCardRequest.getStoreId());
        turnMembershipCard.setDocNum(num);
        turnMembershipCard.setState(0);
        turnMembershipCard.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        List<TurnMembershipCardMxRequest> turnMembershipCardMxRequestList = turnMembershipCardRequest.getTurnMembershipCardMxRequestList();


        for (TurnMembershipCardMxRequest turnMembershipCardMxRequest : turnMembershipCardMxRequestList) {

            if(turnMembershipCardMxRequest.getOutCardRestMoney()!=null)
            turnMembershipCard.setCardRestMoney(turnMembershipCardMxRequest.getOutCardRestMoney());
            if(turnMembershipCardMxRequest.getOutCourseRestCount()!=null)
            turnMembershipCard.setCourseRestCount(turnMembershipCardMxRequest.getOutCourseRestCount());



            TurnMembershipCardMx turnMembershipCardMx = new TurnMembershipCardMx();
            BeanUtils.copyProperties(turnMembershipCardMxRequest,turnMembershipCardMx);
            turnMembershipCardMx.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            turnMembershipCardMx.setTurnMembershipCardId(turnMembershipCard.getId());
            int save = ssmTurnMembershipCardMxService.save(turnMembershipCardMx);
            if(save==0){
                throw new SmsException(ResponseCode.MSG,"保存转出卡包异常");
            }
        }



        return dao.insert(turnMembershipCard);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(TurnMembershipCardRequest turnMembershipCardRequest, String key) throws SmsException {
        TurnMembershipCard turnMembershipCard = new TurnMembershipCard();
        turnMembershipCard.setId(turnMembershipCardRequest.getId());
        TurnMembershipCard turnMembershipCard1 = dao.selectByPrimaryKey(turnMembershipCard.getId());
        turnMembershipCard1.setRollInMemberId(turnMembershipCardRequest.getRollInMemberId());
        turnMembershipCard1.setRollInMemberName(turnMembershipCardRequest.getRollInMemberName());
        turnMembershipCard1.setRollInMemberNum(turnMembershipCardRequest.getRollInMemberNum());
        if (templateRedisClient.hasKey(getKey(key))){
            StaffResponse staffResponse = templateRedisClient.get(getKey(key));
            turnMembershipCard1.setUpdateBy(null);
            turnMembershipCard1.setUpdateDate(new Date());
        }
        int update = dao.updateByPrimaryKeySelective(turnMembershipCard1);

        return update;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNew(TurnMembershipCardRequest turnMembershipCardRequest, String key) throws SmsException {
        TurnMembershipCard turnMembershipCard = new TurnMembershipCard();
        turnMembershipCard.setId(turnMembershipCardRequest.getId());
        TurnMembershipCard turnMembershipCard1 = dao.selectByPrimaryKey(turnMembershipCard.getId());
        turnMembershipCard1.setRollInMemberId(turnMembershipCardRequest.getRollInMemberId());
        turnMembershipCard1.setRollInMemberName(turnMembershipCardRequest.getRollInMemberName());
        turnMembershipCard1.setRollInMemberNum(turnMembershipCardRequest.getRollInMemberNum());
        if (templateRedisClient.hasKey(getKey(key))){
            StaffResponse staffResponse = templateRedisClient.get(getKey(key));
            turnMembershipCard1.setUpdateBy(staffResponse.getStaffName());
            turnMembershipCard1.setUpdateDate(new Date());
        }
        int update = dao.updateByPrimaryKeySelective(turnMembershipCard1);
        if(update>0){
            TurnMembershipCardMx turnMembershipCardMxs = new TurnMembershipCardMx();
            turnMembershipCardMxs.setTurnMembershipCardId(turnMembershipCard.getId());
            int delete = ssmTurnMembershipCardMxService.delete(turnMembershipCardMxs);
            if(delete>0){
                List<TurnMembershipCardMxRequest> turnMembershipCardMxRequestList = turnMembershipCardRequest.getTurnMembershipCardMxRequestList();
                for (TurnMembershipCardMxRequest turnMembershipCardMxRequest : turnMembershipCardMxRequestList) {
                    TurnMembershipCardMx turnMembershipCardMx = new TurnMembershipCardMx();
                    BeanUtils.copyProperties(turnMembershipCardMxRequest,turnMembershipCardMx);
                    turnMembershipCardMx.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                    turnMembershipCardMx.setTurnMembershipCardId(turnMembershipCard.getId());
                    int save = ssmTurnMembershipCardMxService.save(turnMembershipCardMx);
                    if(save==0){
                        throw new SmsException(ResponseCode.MSG,"保存转出卡包异常");
                    }
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"保存转出卡包异常");
            }


        }

        return update;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized int updateState(TurnMembershipCardRequest turnMembershipCardRequest, String key) throws SmsException {
        int state=0;
        TurnMembershipCard turnMembershipCard = new TurnMembershipCard();
        turnMembershipCard.setId(turnMembershipCardRequest.getId());
        TurnMembershipCard turnMembershipCard1 = dao.selectByPrimaryKey(turnMembershipCard.getId());
        if(turnMembershipCardRequest.getState()==1){//送审
            if(turnMembershipCard1.getState()==0 || turnMembershipCard1.getState() == 2 || turnMembershipCard1.getState()==4){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setUpdateBy(null);
                    turnMembershipCard1.setUpdateDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
            }else{
                throw new SmsException(ResponseCode.MSG,"当前状态不允许送审");
            }
        }else if(turnMembershipCardRequest.getState()==2){//回收
            if(turnMembershipCard1.getState()==1){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setUpdateBy(null);
                    turnMembershipCard1.setUpdateDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
            }else{
                throw new SmsException(ResponseCode.MSG,"当前状态不允许回收");
            }
        }else if(turnMembershipCardRequest.getState()==3){//审核
            if(turnMembershipCard1.getState()==1){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setAuditBy(null);
                    turnMembershipCard1.setAuditDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
                if(state>0){
                    //审核通过
                    if(turnMembershipCard1.getType()==0){
                        //充值
                        //扣除用户账户
                        RechargeAccount rechargeAccount = new RechargeAccount();
                        rechargeAccount.setId(turnMembershipCard1.getAccountId());
                        RechargeAccount rechargeAccount1 = ssmRechargeAccountService.selectByPrimaryKey(rechargeAccount.getId());
                        //转可用
                        if(rechargeAccount1.getCardRestMoney().compareTo(turnMembershipCard1.getCardRestMoney()) != -1){//剩余可用金额足够
                            if(rechargeAccount1.getGiftConsumeMoney().compareTo(turnMembershipCard1.getGiftConsumeMoney())!=-1){ //赠送金额足够
                                //剩余金额
                                BigDecimal subtract = rechargeAccount1.getCardRestMoney().subtract(turnMembershipCard1.getCardRestMoney());
                                //剩余可用金额
                                BigDecimal subtract1 = rechargeAccount1.getCardConsumeMoney().subtract(turnMembershipCard1.getCardRestMoney());
                                //剩余赠送金额
                                BigDecimal subtract2 = rechargeAccount1.getCardGiftMoney().subtract(turnMembershipCard1.getGiftConsumeMoney());
                                BigDecimal subtract3 = rechargeAccount1.getGiftConsumeMoney().subtract(turnMembershipCard1.getGiftConsumeMoney());
                                rechargeAccount1.setCardRestMoney(subtract);
                                rechargeAccount1.setCardConsumeMoney(subtract1);
                                rechargeAccount1.setCardGiftMoney(subtract2);
                                rechargeAccount1.setGiftConsumeMoney(subtract3);
                                RechargeAccount rechargeAccount3 = ssmRechargeAccountService.addMoney(rechargeAccount1);

                                StoreMemberList storeMemberLists = new StoreMemberList();
                                storeMemberLists.setId(turnMembershipCard1.getRollOutMemberId());
                                StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberLists.getId());
                                AccountTradeRecord accountTradeRecords = this.setOutAccountTradeRecord(storeMemberList3, rechargeAccount1, turnMembershipCard1);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecords);


                                //添加金额
                                //判断转入用户是否已经存在卡包
                                rechargeAccount.setId(null);
                                rechargeAccount.setMemberId(turnMembershipCard1.getRollInMemberId());
                                rechargeAccount.setRechargeTypeId(turnMembershipCard1.getRechargeOrProjectId());
                                List<RechargeAccount> select = ssmRechargeAccountService.select(rechargeAccount);
                                if(select.size()>0){//卡包存在
                                    RechargeAccount rechargeAccount2 = select.get(0);
                                    rechargeAccount2.setCardRestMoney(rechargeAccount2.getCardRestMoney().add(turnMembershipCard1.getCardRestMoney()));
                                    rechargeAccount2.setCardConsumeMoney(rechargeAccount2.getCardConsumeMoney().add(turnMembershipCard1.getCardRestMoney()));
                                    rechargeAccount2.setGiftConsumeMoney(rechargeAccount2.getGiftConsumeMoney().add(turnMembershipCard1.getGiftConsumeMoney()));
                                    rechargeAccount2.setCardGiftMoney(rechargeAccount2.getCardGiftMoney().add(turnMembershipCard1.getGiftConsumeMoney()));
                                    ssmRechargeAccountService.addMoney(rechargeAccount2);

                                    StoreMemberList storeMemberList = new StoreMemberList();
                                    storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                                    StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                                    AccountTradeRecord accountTradeRecord = this.setOutAccountTradeRecord(storeMemberList1, rechargeAccount2, turnMembershipCard1);
                                    ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                                    turnMembershipCard1.setRollInAcoountId(rechargeAccount2.getId());
                                    state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
                                    if(state==0){
                                        throw new SmsException(ResponseCode.MSG,"审核失败");
                                    }
                                }else{//卡包不存在
                                    //创建卡包
                                    //获取会员的完整信息
                                    StoreMemberList storeMemberList = new StoreMemberList();
                                    storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                                    StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                                    RechargeType rechargeType = new RechargeType();
                                    rechargeType.setId(turnMembershipCard1.getRechargeOrProjectId());
                                    RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);

                                    RechargeAccount rechargeAccount2 = new RechargeAccount();
                                    rechargeAccount2.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                                    rechargeAccount2.setMemberId(storeMemberList1.getId());
                                    rechargeAccount2.setMemberName(storeMemberList1.getMemberName());
                                    rechargeAccount2.setMemberName(storeMemberList1.getMemberName());
                                    rechargeAccount2.setCardRestMoney(turnMembershipCard1.getCardRestMoney());
                                    rechargeAccount2.setCardConsumeMoney(turnMembershipCard1.getCardRestMoney());
                                    rechargeAccount2.setGiftConsumeMoney(turnMembershipCard1.getGiftConsumeMoney());
                                    rechargeAccount2.setCardGiftMoney(turnMembershipCard1.getGiftConsumeMoney());
                                    rechargeAccount2.setRechargeTypeId(turnMembershipCard1.getRechargeOrProjectId());
                                    rechargeAccount2.setRechargeTypeNum(turnMembershipCard1.getRechargeOrProjectNum());
                                    rechargeAccount2.setRechargeTypeName(turnMembershipCard1.getRechargeOrProjectName());
                                    rechargeAccount2.setType(rechargeType1.getType());
                                    rechargeAccount2.setDiscount(rechargeType1.getDiscount());
                                    rechargeAccount2.setDiscount(rechargeType1.getDiscount());
                                    rechargeAccount2.setStoreId(rechargeAccount1.getStoreId());
                                    rechargeAccount2.setStoreName(rechargeAccount1.getStoreName());
                                    rechargeAccount2.setState(1);
                                    if(null != rechargeAccount1.getEndTime()){
                                        rechargeAccount2.setEndTime(rechargeAccount1.getEndTime());
                                    }
                                    rechargeAccount2.setFirstCharge(0L);
                                    rechargeAccount2.setCardNum(0);
                                    int save = ssmRechargeAccountService.save(rechargeAccount2);
                                    if(save==0){
                                        throw new SmsException(ResponseCode.MSG,"添加转入会员卡失败");
                                    }
                                    AccountTradeRecord accountTradeRecord = this.setInAccountTradeRecord(storeMemberList1, rechargeAccount2, turnMembershipCard1);
                                    ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                                    turnMembershipCard1.setRollInAcoountId(rechargeAccount2.getId());
                                    state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
                                    if(state==0){
                                        throw new SmsException(ResponseCode.MSG,"审核失败");
                                    }
                                }

                            }else{
                                throw new SmsException(ResponseCode.MSG,"转出账户赠送可用金额不足");
                            }
                        }else{//不够
                            throw new SmsException(ResponseCode.MSG,"转出账户可用金额不足");
                        }

                    }
                    else{//疗程
                        CourseAccount courseAccount = new CourseAccount();
                        courseAccount.setId(turnMembershipCard1.getAccountId());
                        CourseAccount courseAccount1 = ssmCourseAccountService.selectByPrimaryKey(courseAccount.getId());
                        if(courseAccount1.getCourseConsumePrice().compareTo(turnMembershipCard1.getCardRestMoney())!= -1){
                            StoreMemberList storeMemberList = new StoreMemberList();
                            storeMemberList.setId(turnMembershipCard1.getRollOutMemberId());
                            StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                            courseAccount1.setCourseRestPrice(courseAccount1.getCourseRestPrice().subtract(turnMembershipCard1.getCardRestMoney()));
                            courseAccount1.setCourseConsumePrice(courseAccount1.getCourseConsumePrice().subtract(turnMembershipCard1.getCardRestMoney()));
                            courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()- turnMembershipCard1.getCourseConsumeCount());
                            courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumeCount()- turnMembershipCard1.getCourseConsumeCount());
                            courseAccount1.setPrice(courseAccount1.getCourseRestPrice().divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
                            courseAccount1.setExpenditureMoney(courseAccount1.getExpenditureMoney().add(turnMembershipCard1.getCardRestMoney()));
                            ssmCourseAccountService.subCount(courseAccount1);
                            AccountTradeRecord accountTradeRecord = this.setOutAccountTradeRecord(storeMemberList1, courseAccount1, turnMembershipCard1);
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                            //转入的会员
                            CourseAccount courseAccount3 = new CourseAccount();
                            courseAccount3.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                            courseAccount3.setMemberId(storeMemberList1.getId());
                            courseAccount3.setMemberName(storeMemberList1.getMemberName());
                            courseAccount3.setMemberName(storeMemberList1.getMemberName());
                            courseAccount3.setStoreId(courseAccount1.getStoreId());
                            courseAccount3.setStoreName(courseAccount1.getStoreName());
                            courseAccount3.setState(1);
                            if(null != courseAccount1.getEndTime()){
                                courseAccount3.setEndTime(courseAccount1.getEndTime());
                            }
                            courseAccount3.setCourseProjectId(turnMembershipCard1.getRechargeOrProjectId());
                            courseAccount3.setCourseProjectNum(turnMembershipCard1.getRechargeOrProjectNum());
                            courseAccount3.setCourseProjectName(turnMembershipCard1.getRechargeOrProjectName());
                            courseAccount3.setCourseRestCount(turnMembershipCard1.getCourseRestCount());
                            courseAccount3.setCourseConsumeCount(turnMembershipCard1.getCourseRestCount());
                            courseAccount3.setCourseRestPrice(turnMembershipCard1.getCardRestMoney());
                            courseAccount3.setCourseConsumePrice(turnMembershipCard1.getCardRestMoney());
                            courseAccount3.setPrice(courseAccount3.getCourseRestPrice().divide(BigDecimalUtils.bigDecimal(courseAccount3.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
                            ssmCourseAccountService.subCount(courseAccount1);

                            StoreMemberList storeMemberLists = new StoreMemberList();
                            storeMemberLists.setId(turnMembershipCard1.getRollOutMemberId());
                            StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberLists.getId());
                            AccountTradeRecord accountTradeRecord2 = this.setOutAccountTradeRecord(storeMemberList3, courseAccount3, turnMembershipCard1);
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord2);

                            turnMembershipCard1.setRollInAcoountId(courseAccount1.getId());
                            state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
                            if(state==0){
                                throw new SmsException(ResponseCode.MSG,"审核失败");
                            }
                        }else{
                            throw new SmsException(ResponseCode.MSG,"转出账户可用金额不足");
                        }
                    }
                }else{
                    throw new SmsException(ResponseCode.MSG,"审核失败");
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"当前状态不允许审核");
            }
        }else if(turnMembershipCardRequest.getState()==4) {//反审核
            if(turnMembershipCard1.getState()==3){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setAuditBy(null);
                    turnMembershipCard1.setAuditDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
                if(state>0){
                    //判断转入的账户是否还有足够的金额
                    if(turnMembershipCard1.getType() == 0){ //充值
                        RechargeAccount rechargeAccount = new RechargeAccount();
                        rechargeAccount.setId(turnMembershipCard1.getRollInAcoountId());
                        RechargeAccount rechargeAccount1 = ssmRechargeAccountService.selectOne(rechargeAccount);
                        if(rechargeAccount1.getCardConsumeMoney().compareTo(turnMembershipCard1.getCardRestMoney())!= -1){
                            if(rechargeAccount1.getGiftConsumeMoney().compareTo(turnMembershipCard1.getGiftConsumeMoney())!= -1){
                                //扣减转入
                                rechargeAccount1.setCardRestMoney(rechargeAccount1.getCardRestMoney().subtract(turnMembershipCard1.getCardRestMoney()));
                                rechargeAccount1.setCardConsumeMoney(rechargeAccount1.getCardConsumeMoney().subtract(turnMembershipCard1.getCardRestMoney()));
                                rechargeAccount1.setGiftConsumeMoney(rechargeAccount1.getGiftConsumeMoney().subtract(turnMembershipCard1.getGiftConsumeMoney()));
                                rechargeAccount1.setCardGiftMoney(rechargeAccount1.getCardGiftMoney().subtract(turnMembershipCard1.getGiftConsumeMoney()));
                                ssmRechargeAccountService.addMoney(rechargeAccount1);

                                StoreMemberList storeMemberList = new StoreMemberList();
                                storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                                AccountTradeRecord accountTradeRecord = this.setOutAccountTradeRecord(storeMemberList1, rechargeAccount1, turnMembershipCard1);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                                //添加转出卡的金额
                                RechargeAccount rechargeAccount2 = new RechargeAccount();
                                rechargeAccount2.setId(turnMembershipCard1.getAccountId());
                                RechargeAccount rechargeAccount3 = ssmRechargeAccountService.selectOne(rechargeAccount2);
                                rechargeAccount3.setCardRestMoney(rechargeAccount3.getCardRestMoney().add(turnMembershipCard1.getCardRestMoney()));
                                rechargeAccount3.setCardConsumeMoney(rechargeAccount3.getCardConsumeMoney().add(turnMembershipCard1.getCardRestMoney()));
                                rechargeAccount3.setGiftConsumeMoney(rechargeAccount3.getGiftConsumeMoney().add(turnMembershipCard1.getGiftConsumeMoney()));
                                rechargeAccount3.setCardGiftMoney(rechargeAccount3.getCardGiftMoney().add(turnMembershipCard1.getGiftConsumeMoney()));
                                ssmRechargeAccountService.addMoney(rechargeAccount3);

                                StoreMemberList storeMemberList2 = new StoreMemberList();
                                storeMemberList2.setId(turnMembershipCard1.getRollOutMemberId());
                                StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList2.getId());

                                AccountTradeRecord accountTradeRecord1 = this.setOutAccountTradeRecord(storeMemberList3, rechargeAccount3, turnMembershipCard1);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord1);


                            }else{
                                throw new SmsException(ResponseCode.MSG,"转入账户的赠送金额不足");
                            }
                        }else{
                            throw new SmsException(ResponseCode.MSG,"转入账户的可用金额不足");
                        }

                    }else{//疗程
                        CourseAccount courseAccount = new CourseAccount();
                        courseAccount.setId(turnMembershipCard1.getRollInAcoountId());
                        CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                        if(courseAccount1.getCourseConsumePrice().compareTo(turnMembershipCard1.getCardRestMoney())!= -1){
                            if (courseAccount1.getCourseRestCount()>= turnMembershipCard1.getCourseRestCount()) {
                                courseAccount1.setCourseRestPrice(courseAccount1.getCourseRestPrice().subtract(turnMembershipCard1.getCardRestMoney()));
                                courseAccount1.setCourseConsumePrice(courseAccount1.getCourseConsumePrice().subtract(turnMembershipCard1.getCardRestMoney()));
                                courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()- turnMembershipCard1.getCourseConsumeCount());
                                courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumeCount()- turnMembershipCard1.getCourseConsumeCount());
                                ssmCourseAccountService.subCount(courseAccount1);
                                StoreMemberList storeMemberList = new StoreMemberList();
                                storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                                AccountTradeRecord accountTradeRecord = this.setOutAccountTradeRecord(storeMemberList1, courseAccount1, turnMembershipCard1);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                                //添加转出的金额
                                CourseAccount courseAccount2 = new CourseAccount();
                                courseAccount2.setId(turnMembershipCard1.getAccountId());
                                CourseAccount courseAccount3 = ssmCourseAccountService.selectOne(courseAccount2);
                                courseAccount3.setCourseRestPrice(courseAccount3.getCourseRestPrice().add(turnMembershipCard1.getCardRestMoney()));
                                courseAccount3.setCourseConsumePrice(courseAccount3.getCourseConsumePrice().add(turnMembershipCard1.getCardRestMoney()));
                                courseAccount3.setCourseRestCount(courseAccount3.getCourseRestCount()+ turnMembershipCard1.getCourseConsumeCount());
                                courseAccount3.setCourseConsumeCount(courseAccount3.getCourseConsumeCount()+ turnMembershipCard1.getCourseConsumeCount());
                                courseAccount3.setPrice(courseAccount3.getCourseRestPrice().divide(BigDecimalUtils.bigDecimal(courseAccount3.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
                                courseAccount3.setExpenditureMoney(courseAccount3.getExpenditureMoney().subtract(turnMembershipCard1.getCardRestMoney()));
                                ssmCourseAccountService.subCount(courseAccount3);
                                StoreMemberList storeMemberList2 = new StoreMemberList();
                                storeMemberList2.setId(turnMembershipCard1.getRollOutMemberId());
                                StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList2.getId());

                                AccountTradeRecord accountTradeRecord1 = this.setOutAccountTradeRecord(storeMemberList3, courseAccount3, turnMembershipCard1);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord1);
                            }else{
                                throw new SmsException(ResponseCode.MSG,"转入账户的可用次数不足");
                            }
                        }else{
                            throw new SmsException(ResponseCode.MSG,"转入账户的可用金额不足");
                        }
                    }

                }
            }


        }

        return state;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized int updateStateNew(TurnMembershipCardRequest turnMembershipCardRequest, String key) throws SmsException {
        int state=0;
        TurnMembershipCard turnMembershipCard = new TurnMembershipCard();
        turnMembershipCard.setId(turnMembershipCardRequest.getId());
        TurnMembershipCard turnMembershipCard1 = dao.selectByPrimaryKey(turnMembershipCard.getId());
        if(turnMembershipCardRequest.getState()==1){//送审
            if(turnMembershipCard1.getState()==0 || turnMembershipCard1.getState() == 2 || turnMembershipCard1.getState()==4){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setUpdateBy(null);
                    turnMembershipCard1.setUpdateDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
            }else{
                throw new SmsException(ResponseCode.MSG,"当前状态不允许送审");
            }
        }
        else if(turnMembershipCardRequest.getState()==2){//回收
            if(turnMembershipCard1.getState()==1){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setUpdateBy(null);
                    turnMembershipCard1.setUpdateDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
            }else{
                throw new SmsException(ResponseCode.MSG,"当前状态不允许回收");
            }
        }
        else if(turnMembershipCardRequest.getState()==3){//审核
            if(turnMembershipCard1.getState()==1){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setAuditBy(staffResponse.getStaffName());
                    turnMembershipCard1.setAuditDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
                if(state>0){
                    //审核通过
                    TurnMembershipCardMx turnMembershipCardMx = new TurnMembershipCardMx();
                    turnMembershipCardMx.setTurnMembershipCardId(turnMembershipCard1.getId());
                    List<TurnMembershipCardMx> select1 = ssmTurnMembershipCardMxService.select(turnMembershipCardMx);
                    for (TurnMembershipCardMx membershipCardMx : select1) {
                        if(membershipCardMx.getType()==0){//充值
                            BigDecimal giftConsumeMoneyGive=new BigDecimal("0");
                            if(membershipCardMx.getOutGiftConsumeMoney()!=null){
                                giftConsumeMoneyGive=membershipCardMx.getOutGiftConsumeMoney();
                            }
                            //扣除用户账户
                            RechargeAccount rechargeAccount = new RechargeAccount();
                            rechargeAccount.setId(membershipCardMx.getAccountId());
                            RechargeAccount rechargeAccount1 = ssmRechargeAccountService.selectByPrimaryKey(rechargeAccount.getId());
                            membershipCardMx.setBeforeRestMoney(rechargeAccount1.getCardRestMoney());
                            membershipCardMx.setBeforeConsumeMoney(rechargeAccount1.getCardConsumeMoney());
                            membershipCardMx.setBeforeGiftRestMoney(rechargeAccount1.getCardGiftMoney());
                            membershipCardMx.setBeforeGiftConsumeMoney(rechargeAccount1.getGiftConsumeMoney());
                            //转可用
                            if(rechargeAccount1.getCardRestMoney().compareTo(membershipCardMx.getOutCardRestMoney()) != -1){//剩余可用金额足够
                                if(rechargeAccount1.getGiftConsumeMoney().compareTo(membershipCardMx.getOutGiftConsumeMoney())!=-1){ //赠送金额足够
                                    //剩余金额
                                    BigDecimal subtract = rechargeAccount1.getCardRestMoney().subtract(membershipCardMx.getOutCardRestMoney());
                                    //剩余可用金额
                                    BigDecimal subtract1 = rechargeAccount1.getCardConsumeMoney().subtract(membershipCardMx.getOutCardRestMoney());
                                    //剩余赠送金额
                                    BigDecimal subtract2 = rechargeAccount1.getCardGiftMoney().subtract(membershipCardMx.getOutGiftConsumeMoney());
                                    BigDecimal subtract3 = rechargeAccount1.getGiftConsumeMoney().subtract(membershipCardMx.getOutGiftConsumeMoney());
                                    rechargeAccount1.setCardRestMoney(subtract);
                                    rechargeAccount1.setCardConsumeMoney(subtract1);
                                    rechargeAccount1.setCardGiftMoney(subtract2);
                                    rechargeAccount1.setGiftConsumeMoney(subtract3);

                                    membershipCardMx.setBackRestMoney(rechargeAccount1.getCardRestMoney());
                                    membershipCardMx.setBackConsumeMoney(rechargeAccount1.getCardConsumeMoney());
                                    membershipCardMx.setBackGiftRestMoney(rechargeAccount1.getCardGiftMoney());
                                    membershipCardMx.setBackGiftConsumeMoney(rechargeAccount1.getGiftConsumeMoney());
                                    //updateType==1为修改  asType==1为减0为加
                                    this.rechargeAccountGive(rechargeAccount1,turnMembershipCard1,giftConsumeMoneyGive,1,1);
                                    RechargeAccount rechargeAccount3 = ssmRechargeAccountService.addMoney(rechargeAccount1);

                                    StoreMemberList storeMemberLists = new StoreMemberList();
                                    storeMemberLists.setId(turnMembershipCard1.getRollOutMemberId());
                                    StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberLists.getId());
                                    AccountTradeRecord accountTradeRecords = this.setOutAccountTradeRecordByRecharge(storeMemberList3, rechargeAccount1, turnMembershipCard1,membershipCardMx);
                                    ssmAccountTradeRecordService.addRecord(accountTradeRecords);


                                    //添加金额
                                    //判断转入用户是否已经存在卡包
                                    rechargeAccount.setId(null);
                                    rechargeAccount.setMemberId(turnMembershipCard1.getRollInMemberId());
                                    rechargeAccount.setRechargeTypeId(membershipCardMx.getRechargeOrProjectId());
                                    List<RechargeAccount> select = ssmRechargeAccountService.select(rechargeAccount);
                                    if(select.size()>0){//卡包存在
                                        RechargeAccount rechargeAccount2 = select.get(0);
                                        membershipCardMx.setRollInBeforeRestMoney(rechargeAccount2.getCardRestMoney());
                                        membershipCardMx.setRollInBeforeConsumeMoney(rechargeAccount2.getCardConsumeMoney());
                                        membershipCardMx.setRollInBeforeGiftRestMoney(rechargeAccount2.getCardGiftMoney());
                                        membershipCardMx.setRollInBeforeGiftConsumeMoney(rechargeAccount2.getGiftConsumeMoney());

                                        rechargeAccount2.setCardRestMoney(rechargeAccount2.getCardRestMoney().add(membershipCardMx.getOutCardRestMoney()));
                                        rechargeAccount2.setCardConsumeMoney(rechargeAccount2.getCardConsumeMoney().add(membershipCardMx.getOutCardRestMoney()));
                                        rechargeAccount2.setGiftConsumeMoney(rechargeAccount2.getGiftConsumeMoney().add(membershipCardMx.getOutGiftConsumeMoney()));
                                        rechargeAccount2.setCardGiftMoney(rechargeAccount2.getCardGiftMoney().add(membershipCardMx.getOutGiftConsumeMoney()));

                                        membershipCardMx.setRollInBackRestMoney(rechargeAccount2.getCardRestMoney());
                                        membershipCardMx.setRollInBackConsumeMoney(rechargeAccount2.getCardConsumeMoney());
                                        membershipCardMx.setRollInBackGiftRestMoney(rechargeAccount2.getCardGiftMoney());
                                        membershipCardMx.setRollInBackGiftConsumeMoney(rechargeAccount2.getGiftConsumeMoney());
                                        //updateType==1为修改  asType==1为减0为加
                                        this.rechargeAccountGive(rechargeAccount2,turnMembershipCard1,giftConsumeMoneyGive,1,0);
                                        ssmRechargeAccountService.addMoney(rechargeAccount2);

                                        StoreMemberList storeMemberList = new StoreMemberList();
                                        storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                                        StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                                        AccountTradeRecord accountTradeRecord = this.setInAccountTradeRecord(storeMemberList1, rechargeAccount2, turnMembershipCard1,membershipCardMx);
                                        ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                                        membershipCardMx.setRollInAcoountId(rechargeAccount2.getId());
                                        state = ssmTurnMembershipCardMxService.updateNotNull(membershipCardMx);
                                        if(state==0){
                                            throw new SmsException(ResponseCode.MSG,"审核失败");
                                        }
                                    }else{//卡包不存在
                                        //创建卡包
                                        //获取会员的完整信息
                                        StoreMemberList storeMemberList = new StoreMemberList();
                                        storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                                        StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                                        RechargeType rechargeType = new RechargeType();
                                        rechargeType.setId(membershipCardMx.getRechargeOrProjectId());
                                        RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);

                                        RechargeAccount rechargeAccount2 = new RechargeAccount();
                                        rechargeAccount2.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                                        rechargeAccount2.setMemberId(storeMemberList1.getId());
                                        rechargeAccount2.setMemberName(storeMemberList1.getMemberName());
                                        rechargeAccount2.setMemberName(storeMemberList1.getMemberName());
                                        rechargeAccount2.setCardRestMoney(membershipCardMx.getOutCardRestMoney());
                                        rechargeAccount2.setCardConsumeMoney(membershipCardMx.getOutCardRestMoney());
                                        rechargeAccount2.setGiftConsumeMoney(membershipCardMx.getOutGiftConsumeMoney());
                                        rechargeAccount2.setCardGiftMoney(membershipCardMx.getOutGiftConsumeMoney());
                                        rechargeAccount2.setRechargeTypeId(membershipCardMx.getRechargeOrProjectId());
                                        rechargeAccount2.setRechargeTypeNum(membershipCardMx.getRechargeOrProjectNum());
                                        rechargeAccount2.setRechargeTypeName(membershipCardMx.getRechargeOrProjectName());
                                        rechargeAccount2.setType(rechargeType1.getType());
                                        rechargeAccount2.setDiscount(rechargeType1.getDiscount());
                                        rechargeAccount2.setDiscount(rechargeType1.getDiscount());
                                        rechargeAccount2.setStoreId(rechargeAccount1.getStoreId());
                                        rechargeAccount2.setStoreName(rechargeAccount1.getStoreName());
                                        rechargeAccount2.setState(1);
                                        if(null != rechargeAccount1.getEndTime()){
                                            rechargeAccount2.setEndTime(rechargeAccount1.getEndTime());
                                        }
                                        rechargeAccount2.setFirstCharge(0L);
                                        rechargeAccount2.setCardNum(0);

                                        membershipCardMx.setRollInBeforeRestMoney(BigDecimal.valueOf(0));
                                        membershipCardMx.setRollInBeforeConsumeMoney(BigDecimal.valueOf(0));
                                        membershipCardMx.setRollInBeforeGiftRestMoney(BigDecimal.valueOf(0));
                                        membershipCardMx.setRollInBeforeGiftConsumeMoney(BigDecimal.valueOf(0));
                                        membershipCardMx.setRollInBackRestMoney(rechargeAccount2.getCardRestMoney());
                                        membershipCardMx.setRollInBackConsumeMoney(rechargeAccount2.getCardConsumeMoney());
                                        membershipCardMx.setRollInBackGiftRestMoney(rechargeAccount2.getCardGiftMoney());
                                        membershipCardMx.setRollInBackGiftConsumeMoney(rechargeAccount2.getGiftConsumeMoney());
                                        //updateType==1为修改  asType==1为减0为加
                                        this.rechargeAccountGive(rechargeAccount2,turnMembershipCard1,giftConsumeMoneyGive,0,1);
                                        int save = ssmRechargeAccountService.save(rechargeAccount2);
                                        if(save==0){
                                            throw new SmsException(ResponseCode.MSG,"添加转入会员卡失败");
                                        }
                                        AccountTradeRecord accountTradeRecord = this.setInAccountTradeRecord(storeMemberList1, rechargeAccount2, turnMembershipCard1,membershipCardMx);
                                        ssmAccountTradeRecordService.addRecord(accountTradeRecord);
                                        membershipCardMx.setRollInAcoountId(rechargeAccount2.getId());
//                                        state = ssmTurnMembershipCardMxService.updateNotNull(membershipCardMx);
//                                        if(state==0){
//                                            throw new SmsException(ResponseCode.MSG,"审核失败");
//                                        }
                                    }
                                }else{
                                    throw new SmsException(ResponseCode.MSG,"转出账户赠送可用金额不足");
                                }
                            }else{//不够
                                throw new SmsException(ResponseCode.MSG,"转出账户可用金额不足");
                            }

                        }else{//疗程
                            CourseAccount courseAccount = new CourseAccount();
                            courseAccount.setId(membershipCardMx.getAccountId());
                            CourseAccount courseAccount1 = ssmCourseAccountService.selectByPrimaryKey(courseAccount.getId());
                            membershipCardMx.setBeforePrice(courseAccount1.getPrice());
                            membershipCardMx.setBeforeRestMoney(courseAccount1.getCourseRestPrice());
                            membershipCardMx.setBeforeConsumeMoney(courseAccount1.getCourseConsumePrice());
                            membershipCardMx.setBeforeRestCount(courseAccount1.getCourseRestCount());
                            membershipCardMx.setBeforeConsumeCount(courseAccount1.getCourseConsumeCount());
                            if(courseAccount1.getCourseConsumePrice().compareTo(membershipCardMx.getOutCardRestMoney())!= -1){
                                StoreMemberList storeMemberList = new StoreMemberList();
                                storeMemberList.setId(turnMembershipCard1.getRollOutMemberId());
                                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                                courseAccount1.setCourseRestPrice(courseAccount1.getCourseRestPrice().subtract(membershipCardMx.getOutCardRestMoney()));
                                courseAccount1.setCourseConsumePrice(courseAccount1.getCourseConsumePrice().subtract(membershipCardMx.getOutCardRestMoney()));
                                courseAccount1.setCourseRestCount(courseAccount1.getCourseRestCount()- membershipCardMx.getOutCourseRestCount());
                                courseAccount1.setCourseConsumeCount(courseAccount1.getCourseConsumeCount()- membershipCardMx.getOutCourseRestCount());
                                if(courseAccount1.getCourseProjectMoney().subtract(membershipCardMx.getOutCardRestMoney()).compareTo(BigDecimal.valueOf(0))!=0){
                                    if(courseAccount1.getCourseRestCount()!=0){
                                        courseAccount1.setPrice(courseAccount1.getCourseProjectMoney().subtract(membershipCardMx.getOutCardRestMoney()).divide(BigDecimalUtils.bigDecimal(courseAccount1.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
                                    }
                                }
                                courseAccount1.setExpenditureMoney(courseAccount1.getExpenditureMoney().add(membershipCardMx.getOutCardRestMoney()));

                                membershipCardMx.setBackPrice(courseAccount1.getPrice());
                                membershipCardMx.setBackRestMoney(courseAccount1.getCourseRestPrice());
                                membershipCardMx.setBackConsumeMoney(courseAccount1.getCourseConsumePrice());
                                membershipCardMx.setBackRestCount(courseAccount1.getCourseRestCount());
                                membershipCardMx.setBackConsumeCount(courseAccount1.getCourseConsumeCount());

                                ssmCourseAccountService.subCount(courseAccount1);
                                AccountTradeRecord accountTradeRecord = this.setOutAccountTradeRecord(storeMemberList1, courseAccount1, turnMembershipCard1,membershipCardMx);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord);



                                storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                                storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());
                                //转入的会员
                                CourseAccount courseAccount3 = new CourseAccount();
                                courseAccount3.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                                courseAccount3.setMemberId(storeMemberList1.getId());
                                courseAccount3.setMemberName(storeMemberList1.getMemberName());
                                courseAccount3.setStoreId(courseAccount1.getStoreId());
                                courseAccount3.setStoreName(courseAccount1.getStoreName());
                                courseAccount3.setState(1);
                                if(null != courseAccount1.getEndTime()){
                                    courseAccount3.setEndTime(courseAccount1.getEndTime());
                                }
                                courseAccount3.setCourseProjectId(membershipCardMx.getRechargeOrProjectId());
                                courseAccount3.setCourseProjectNum(membershipCardMx.getRechargeOrProjectNum());
                                courseAccount3.setCourseProjectName(membershipCardMx.getRechargeOrProjectName());
                                courseAccount3.setCourseRestCount(membershipCardMx.getOutCourseRestCount());
                                courseAccount3.setCourseConsumeCount(membershipCardMx.getOutCourseRestCount());
                                courseAccount3.setCourseRestPrice(membershipCardMx.getOutCardRestMoney());
                                courseAccount3.setCourseConsumePrice(membershipCardMx.getOutCardRestMoney());
                                courseAccount3.setPrice(courseAccount3.getCourseRestPrice().divide(BigDecimalUtils.bigDecimal(courseAccount3.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
                                membershipCardMx.setRollInBeforePrice(BigDecimal.valueOf(0));
                                membershipCardMx.setRollInBeforeRestMoney(BigDecimal.valueOf(0));
                                membershipCardMx.setRollInBeforeConsumeMoney(BigDecimal.valueOf(0));
                                membershipCardMx.setRollInBeforeRestCount(0);
                                membershipCardMx.setRollInBeforeConsumeCount(0);
                                membershipCardMx.setRollInBackPrice(courseAccount3.getPrice());
                                membershipCardMx.setRollInBackRestMoney(courseAccount3.getCourseRestPrice());
                                membershipCardMx.setRollInBackConsumeMoney(courseAccount3.getCourseConsumePrice());
                                membershipCardMx.setRollInBackRestCount(courseAccount3.getCourseRestCount());
                                membershipCardMx.setRollInBackConsumeCount(courseAccount3.getCourseConsumeCount());
                                ssmCourseAccountService.save(courseAccount3);

//                                StoreMemberList storeMemberLists = new StoreMemberList();
//                                storeMemberLists.setId(turnMembershipCard1.getRollOutMemberId());
//                                StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberLists.getId());
                                AccountTradeRecord accountTradeRecord2 = this.setInAccountTradeRecord(storeMemberList1, courseAccount3, turnMembershipCard1,membershipCardMx);
                                ssmAccountTradeRecordService.addRecord(accountTradeRecord2);
                                membershipCardMx.setRollInAcoountId(courseAccount3.getId());

                            }else{
                                throw new SmsException(ResponseCode.MSG,"转出账户可用金额不足");
                            }
                        }
                        state = ssmTurnMembershipCardMxService.updateNotNull(membershipCardMx);
                        if(state==0){
                            throw new SmsException(ResponseCode.MSG,"审核失败");
                        }
                    }
                }else{
                    throw new SmsException(ResponseCode.MSG,"审核失败");
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"当前状态不允许审核");
            }
        }
        else if(turnMembershipCardRequest.getState()==4) {//反审核
            if(turnMembershipCard1.getState()==3){
                turnMembershipCard1.setState(turnMembershipCardRequest.getState());
                if (templateRedisClient.hasKey(getKey(key))){
                    StaffResponse staffResponse = templateRedisClient.get(getKey(key));
                    turnMembershipCard1.setAuditBy(staffResponse.getStaffName());
                    turnMembershipCard1.setAuditDate(new Date());
                }
                state = dao.updateByPrimaryKeySelective(turnMembershipCard1);
                if(state>0){
                    TurnMembershipCardMx turnMembershipCardMx = new TurnMembershipCardMx();
                    turnMembershipCardMx.setTurnMembershipCardId(turnMembershipCard1.getId());
                    List<TurnMembershipCardMx> select1 = ssmTurnMembershipCardMxService.select(turnMembershipCardMx);
                    for (TurnMembershipCardMx membershipCardMx : select1) {
                        //判断转入的账户是否还有足够的金额
                        if(membershipCardMx.getType() == 0){ //充值
                            BigDecimal giftConsumeMoneyGive=new BigDecimal("0");
                            if(membershipCardMx.getOutGiftConsumeMoney()!=null){
                                giftConsumeMoneyGive=membershipCardMx.getOutGiftConsumeMoney();
                            }
                            RechargeAccount rechargeAccount = new RechargeAccount();
                            rechargeAccount.setId(membershipCardMx.getRollInAcoountId());
                            RechargeAccount rechargeAccount1 = ssmRechargeAccountService.selectOne(rechargeAccount);

                            if(membershipCardMx.getRollInBackRestMoney().compareTo(rechargeAccount1.getCardRestMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount1.getRechargeTypeName()+"] 转入卡的剩余金额与当前剩余金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getRollInBackConsumeMoney().compareTo(rechargeAccount1.getCardConsumeMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount1.getRechargeTypeName()+"] 转入卡的可用金额与当前可用金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getRollInBackGiftRestMoney().compareTo(rechargeAccount1.getCardGiftMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount1.getRechargeTypeName()+"] 转入卡的剩余赠送金额与当前剩余赠送金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getRollInBackGiftConsumeMoney().compareTo(rechargeAccount1.getGiftConsumeMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount1.getRechargeTypeName()+"] 转入卡的可用赠送金额与当前可用赠送金额不相等，已经进行其他交易，不能反审核");
                            }

                            rechargeAccount1.setCardRestMoney(membershipCardMx.getRollInBeforeRestMoney());
                            rechargeAccount1.setCardConsumeMoney(membershipCardMx.getRollInBeforeConsumeMoney());
                            rechargeAccount1.setCardGiftMoney(membershipCardMx.getRollInBeforeGiftRestMoney());
                            rechargeAccount1.setGiftConsumeMoney(membershipCardMx.getRollInBeforeGiftConsumeMoney());
                            this.rechargeAccountGive(rechargeAccount1,turnMembershipCard1,giftConsumeMoneyGive,1,1);
                            ssmRechargeAccountService.addMoney(rechargeAccount1);

                            StoreMemberList storeMemberList = new StoreMemberList();
                            storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                            StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());

                            AccountTradeRecord accountTradeRecord = this.setOutAccountTradeRecord(storeMemberList1, rechargeAccount1, turnMembershipCard1,membershipCardMx);
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                            //添加转出卡的金额
                            RechargeAccount rechargeAccount2 = new RechargeAccount();
                            rechargeAccount2.setId(membershipCardMx.getAccountId());
                            RechargeAccount rechargeAccount3 = ssmRechargeAccountService.selectOne(rechargeAccount2);

                            if(membershipCardMx.getBackRestMoney().compareTo(rechargeAccount3.getCardRestMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount3.getRechargeTypeName()+"] 转出卡的剩余金额与当前剩余金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getBackConsumeMoney().compareTo(rechargeAccount3.getCardConsumeMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount3.getRechargeTypeName()+"] 转出卡的可用金额与当前可用金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getBackGiftRestMoney().compareTo(rechargeAccount3.getCardGiftMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount3.getRechargeTypeName()+"] 转出卡的剩余赠送金额与当前剩余赠送金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getBackGiftConsumeMoney().compareTo(rechargeAccount3.getGiftConsumeMoney())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+rechargeAccount3.getRechargeTypeName()+"] 转出卡的可用赠送金额与当前可用赠送金额不相等，已经进行其他交易，不能反审核");
                            }

                            rechargeAccount3.setCardRestMoney(rechargeAccount3.getCardRestMoney().add(membershipCardMx.getOutCardRestMoney()));
                            rechargeAccount3.setCardConsumeMoney(rechargeAccount3.getCardConsumeMoney().add(membershipCardMx.getOutCardRestMoney()));
                            rechargeAccount3.setGiftConsumeMoney(rechargeAccount3.getGiftConsumeMoney().add(membershipCardMx.getOutGiftConsumeMoney()));
                            rechargeAccount3.setCardGiftMoney(rechargeAccount3.getCardGiftMoney().add(membershipCardMx.getOutGiftConsumeMoney()));
                            rechargeAccount3.setCardRestMoney(membershipCardMx.getBeforeRestMoney());
                            rechargeAccount3.setCardConsumeMoney(membershipCardMx.getBeforeConsumeMoney());
                            rechargeAccount3.setCardGiftMoney(membershipCardMx.getBeforeGiftRestMoney());
                            rechargeAccount3.setGiftConsumeMoney(membershipCardMx.getBeforeGiftConsumeMoney());
                            //updateType==1为修改  asType==1为减0为加
                            this.rechargeAccountGive(rechargeAccount3,turnMembershipCard1,giftConsumeMoneyGive,1,0);
                            ssmRechargeAccountService.addMoney(rechargeAccount3);

                            StoreMemberList storeMemberList2 = new StoreMemberList();
                            storeMemberList2.setId(turnMembershipCard1.getRollOutMemberId());
                            StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList2.getId());

                            AccountTradeRecord accountTradeRecord1 = this.setRechargeOutAccountTradeRecord(storeMemberList3, rechargeAccount3, turnMembershipCard1,membershipCardMx);
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord1);

                        }else{//疗程
                            CourseAccount courseAccount = new CourseAccount();
                            courseAccount.setId(membershipCardMx.getRollInAcoountId());
                            CourseAccount courseAccount1 = ssmCourseAccountService.selectOne(courseAccount);
                            if(membershipCardMx.getRollInBackPrice().compareTo(courseAccount1.getPrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount1.getCourseProjectName()+"] 转入卡的单价与当前单价不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getRollInBackRestMoney().compareTo(courseAccount1.getCourseRestPrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount1.getCourseProjectName()+"] 转入卡的剩余金额与当前剩余金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getRollInBackConsumeMoney().compareTo(courseAccount1.getCourseConsumePrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount1.getCourseProjectName()+"] 转入卡的可用金额与当前可用金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getRollInBackRestCount() != courseAccount1.getCourseRestCount()){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount1.getCourseProjectName()+"] 转入卡的剩余次数与当前剩余次数不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getRollInBackConsumeCount() != courseAccount1.getCourseConsumeCount()){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount1.getCourseProjectName()+"] 转入卡的可用次数与当前可用次数不相等，已经进行其他交易，不能反审核");
                            }
                            courseAccount1.setPrice(membershipCardMx.getRollInBeforePrice());
                            courseAccount1.setCourseRestPrice(membershipCardMx.getRollInBeforeRestMoney());
                            courseAccount1.setCourseConsumePrice(membershipCardMx.getRollInBeforeConsumeMoney());
                            courseAccount1.setCourseRestCount(membershipCardMx.getRollInBeforeRestCount());
                            courseAccount1.setCourseConsumeCount(membershipCardMx.getRollInBeforeConsumeCount());
                            ssmCourseAccountService.subCount(courseAccount1);
                            StoreMemberList storeMemberList = new StoreMemberList();
                            storeMemberList.setId(turnMembershipCard1.getRollInMemberId());
                            StoreMemberList storeMemberList1 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList.getId());
                            //转入的作废
                            AccountTradeRecord accountTradeRecord = this.setProjectInAccountTradeRecord(storeMemberList1, courseAccount1, turnMembershipCard1,membershipCardMx);
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord);

                            //添加转出的金额
                            CourseAccount courseAccount2 = new CourseAccount();
                            courseAccount2.setId(membershipCardMx.getAccountId());
                            CourseAccount courseAccount3 = ssmCourseAccountService.selectOne(courseAccount2);
                            if(membershipCardMx.getBackPrice().compareTo(courseAccount3.getPrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount3.getCourseProjectName()+"] 转出卡的单价与当前单价不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getBackRestMoney().compareTo(courseAccount3.getCourseRestPrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount3.getCourseProjectName()+"] 转出卡的剩余金额与当前剩余金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getBackConsumeMoney().compareTo(courseAccount3.getCourseConsumePrice())!=0){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount3.getCourseProjectName()+"] 转出卡的可用金额与当前可用金额不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getBackRestCount() != courseAccount3.getCourseRestCount()){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount3.getCourseProjectName()+"] 转出卡的剩余次数与当前剩余次数不相等，已经进行其他交易，不能反审核");
                            }
                            if(membershipCardMx.getBackConsumeCount() != courseAccount3.getCourseConsumeCount()){
                                throw new SmsException(ResponseCode.MSG,"该转卡审核后 ["+courseAccount3.getCourseProjectName()+"] 转出卡的可用次数与当前可用次数不相等，已经进行其他交易，不能反审核");
                            }
                            courseAccount3.setPrice(membershipCardMx.getBeforePrice());
                            courseAccount3.setCourseRestPrice(membershipCardMx.getBeforeRestMoney());
                            courseAccount3.setCourseConsumePrice(membershipCardMx.getBeforeConsumeMoney());
                            courseAccount3.setCourseRestCount(membershipCardMx.getBeforeRestCount());
                            courseAccount3.setCourseConsumeCount(membershipCardMx.getBeforeConsumeCount());
                            ssmCourseAccountService.subCount(courseAccount3);
                            StoreMemberList storeMemberList2 = new StoreMemberList();
                            storeMemberList2.setId(turnMembershipCard1.getRollOutMemberId());
                            StoreMemberList storeMemberList3 = ssmStoreMemberService.selectByPrimaryKey(storeMemberList2.getId());
                            //转出的作废
                            AccountTradeRecord accountTradeRecord1 = this.setOutAccountTradeRecord(storeMemberList3, courseAccount3, turnMembershipCard1,membershipCardMx);
                            ssmAccountTradeRecordService.addRecord(accountTradeRecord1);
                        }
                    }

                }
            }
        }
        return state;
    }

    /*
     * 会员转卡疗程转入
     * */
    public AccountTradeRecord setInAccountTradeRecord(StoreMemberList storeMemberList,CourseAccount courseAccount,TurnMembershipCard turnMembershipCard){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(storeMemberList.getId());
        accountTradeRecord.setMemberName(storeMemberList.getMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(courseAccount.getId());
        accountTradeRecord.setAccountName(courseAccount.getCourseProjectName());
        if(turnMembershipCard.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.account_type_project_out);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInCount(turnMembershipCard.getCourseRestCount());
            accountTradeRecord.setAccountInMoney(turnMembershipCard.getCardRestMoney());
        }else {
            accountTradeRecord.setAccountType(Constant.account_type_project_in_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendCount(turnMembershipCard.getCourseRestCount());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCard.getCardRestMoney());
        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setPrice(turnMembershipCard.getCardRestMoney().divide(BigDecimalUtils.bigDecimal(turnMembershipCard.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.turn_doc_type_card_use);
        accountTradeRecord.setTradeId(courseAccount.getCourseProjectId());
        accountTradeRecord.setTradeName(courseAccount.getCourseProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCard.getCardRestMoney());
        accountTradeRecord.setTradeCount(courseAccount.getCourseCount());
        accountTradeRecord.setAccountRestCount(courseAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(courseAccount.getCourseConsumeCount());
        return accountTradeRecord;
    }

    /*
     * 会员转卡疗程转出
     * */
    public AccountTradeRecord setOutAccountTradeRecord(StoreMemberList storeMemberList,CourseAccount courseAccount,TurnMembershipCard turnMembershipCard){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(storeMemberList.getId());
        accountTradeRecord.setMemberName(storeMemberList.getMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(courseAccount.getId());
        accountTradeRecord.setAccountName(courseAccount.getCourseProjectName());
        if(turnMembershipCard.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.account_type_project_in);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendCount(turnMembershipCard.getCourseRestCount());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCard.getCardRestMoney());
            accountTradeRecord.setOrderStatu(0);
        }else {
            accountTradeRecord.setAccountType(Constant.account_type_project_out_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInCount(turnMembershipCard.getCourseRestCount());
            accountTradeRecord.setAccountInMoney(turnMembershipCard.getCardRestMoney());
            accountTradeRecord.setOrderStatu(2);
        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setPrice(turnMembershipCard.getCardRestMoney().divide(BigDecimalUtils.bigDecimal(turnMembershipCard.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.turn_doc_type_card_use);
        accountTradeRecord.setTradeId(courseAccount.getCourseProjectId());
        accountTradeRecord.setTradeName(courseAccount.getCourseProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCard.getCardRestMoney());
        accountTradeRecord.setTradeCount(courseAccount.getCourseCount());
        accountTradeRecord.setAccountRestCount(courseAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(courseAccount.getCourseConsumeCount());
        return accountTradeRecord;
    }

    /*
     * 会员转卡疗程转入
     * */
    public AccountTradeRecord setInAccountTradeRecord(StoreMemberList storeMemberList,CourseAccount courseAccount,TurnMembershipCard turnMembershipCard,TurnMembershipCardMx turnMembershipCardMx){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(storeMemberList.getId());
        accountTradeRecord.setMemberName(storeMemberList.getMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(courseAccount.getId());
        accountTradeRecord.setAccountName(courseAccount.getCourseProjectName());
        if(turnMembershipCard.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.account_type_project_in);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInCount(turnMembershipCard.getCourseRestCount());
            accountTradeRecord.setAccountInMoney(turnMembershipCard.getCardRestMoney());
        }else {
            accountTradeRecord.setAccountType(Constant.account_type_project_out_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendCount(turnMembershipCard.getCourseRestCount());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCard.getCardRestMoney());
        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setPrice(turnMembershipCard.getCardRestMoney().divide(BigDecimalUtils.bigDecimal(turnMembershipCard.getCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.turn_doc_type_card_use);
        accountTradeRecord.setTradeId(courseAccount.getCourseProjectId());
        accountTradeRecord.setTradeName(courseAccount.getCourseProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCard.getCardRestMoney());
        accountTradeRecord.setTradeCount(courseAccount.getCourseCount());
        accountTradeRecord.setAccountRestCount(courseAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(courseAccount.getCourseConsumeCount());
        return accountTradeRecord;
    }
    /*
     * 会员转卡疗程转出
     * */
    public AccountTradeRecord setOutAccountTradeRecord(StoreMemberList storeMemberList,CourseAccount courseAccount,TurnMembershipCard turnMembershipCard,TurnMembershipCardMx turnMembershipCardMx){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(storeMemberList.getId());
        accountTradeRecord.setMemberName(storeMemberList.getMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(courseAccount.getId());
        accountTradeRecord.setAccountName(courseAccount.getCourseProjectName());
        if(turnMembershipCard.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.account_type_project_out);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendCount(turnMembershipCardMx.getOutCourseRestCount());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setOrderStatu(0);
        }else {
            accountTradeRecord.setAccountType(Constant.account_type_project_out_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInCount(turnMembershipCardMx.getOutCourseRestCount());
            accountTradeRecord.setAccountInMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setOrderStatu(2);
        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney().divide(BigDecimalUtils.bigDecimal(turnMembershipCardMx.getOutCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.turn_doc_type_card_use);
        accountTradeRecord.setTradeId(courseAccount.getCourseProjectId());
        accountTradeRecord.setTradeName(courseAccount.getCourseProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setTradeCount(courseAccount.getCourseCount());
        accountTradeRecord.setAccountRestCount(courseAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(courseAccount.getCourseConsumeCount());
        return accountTradeRecord;
    }

    public AccountTradeRecord setProjectInAccountTradeRecord(StoreMemberList storeMemberList,CourseAccount courseAccount,TurnMembershipCard turnMembershipCard,TurnMembershipCardMx turnMembershipCardMx){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(storeMemberList.getId());
        accountTradeRecord.setMemberName(storeMemberList.getMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(courseAccount.getId());
        accountTradeRecord.setAccountName(courseAccount.getCourseProjectName());
        if(turnMembershipCard.getState() == 3) {
            accountTradeRecord.setAccountType(Constant.account_type_project_in);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendCount(turnMembershipCardMx.getOutCourseRestCount());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setOrderStatu(0);
        }else {
            accountTradeRecord.setAccountType(Constant.account_type_project_in_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getAuditDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInCount(turnMembershipCardMx.getOutCourseRestCount());
            accountTradeRecord.setAccountInMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setOrderStatu(2);
        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney().divide(BigDecimalUtils.bigDecimal(turnMembershipCardMx.getOutCourseRestCount().toString()),2,BigDecimal.ROUND_HALF_UP));
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.turn_doc_type_card_use);
        accountTradeRecord.setTradeId(courseAccount.getCourseProjectId());
        accountTradeRecord.setTradeName(courseAccount.getCourseProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setTradeCount(courseAccount.getCourseCount());
        accountTradeRecord.setAccountRestCount(courseAccount.getCourseRestCount());
        accountTradeRecord.setAccountConsumeCount(courseAccount.getCourseConsumeCount());
        return accountTradeRecord;
    }



    /*
     * 添加转入会员转卡交易记录
     * */
    public AccountTradeRecord setInAccountTradeRecord(StoreMemberList storeMemberList,RechargeAccount rechargeAccount,TurnMembershipCard turnMembershipCard){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(turnMembershipCard.getRollInMemberId());
        accountTradeRecord.setMemberName(turnMembershipCard.getRollInMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        accountTradeRecord.setPrice(turnMembershipCard.getCardRestMoney());
        if(turnMembershipCard.getState() == 3) {//转入审核==添加
//            if(rechargeList.getLiftType()==1){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_upgrade_in);
//            }else if(rechargeList.getLiftType()==2){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_demotion_in);
//            }else{
//                accountTradeRecord.setDocType(Constant.doc_type_recharge);
//            }
            accountTradeRecord.setAccountType(Constant.account_type_recharge_in);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInMoney(turnMembershipCard.getCardRestMoney());
            accountTradeRecord.setGiftInMenoy(turnMembershipCard.getGiftConsumeMoney());
        }else {//转入反审核==扣减
            accountTradeRecord.setAccountType(Constant.account_type_recharge_out_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCard.getCardRestMoney());
            accountTradeRecord.setAccountGiftSpendMoney(turnMembershipCard.getGiftConsumeMoney());
        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.trun_doc_type_recharge);


        accountTradeRecord.setTradeId(turnMembershipCard.getRechargeOrProjectId());
        accountTradeRecord.setPrice(turnMembershipCard.getCardRestMoney());
        accountTradeRecord.setTradeName(turnMembershipCard.getRechargeOrProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCard.getCardRestMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        return accountTradeRecord;
    }

    /*
     * 添加转入会员转卡交易记录
     * */
    public AccountTradeRecord setInAccountTradeRecord(StoreMemberList storeMemberList,RechargeAccount rechargeAccount,TurnMembershipCard turnMembershipCard,TurnMembershipCardMx turnMembershipCardMx){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(turnMembershipCard.getRollInMemberId());
        accountTradeRecord.setMemberName(turnMembershipCard.getRollInMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        if(turnMembershipCard.getState() == 3) {//转入审核==添加
//            if(rechargeList.getLiftType()==1){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_upgrade_in);
//            }else if(rechargeList.getLiftType()==2){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_demotion_in);
//            }else{
//                accountTradeRecord.setDocType(Constant.doc_type_recharge);
//            }
            accountTradeRecord.setAccountType(Constant.account_type_recharge_in);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setGiftInMenoy(turnMembershipCardMx.getOutGiftConsumeMoney());
        }else {//转入反审核==扣减
            accountTradeRecord.setAccountType(Constant.account_type_recharge_out_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setAccountGiftSpendMoney(turnMembershipCardMx.getOutGiftConsumeMoney());
        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.trun_doc_type_recharge);


        accountTradeRecord.setTradeId(turnMembershipCardMx.getRechargeOrProjectId());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setTradeName(turnMembershipCardMx.getRechargeOrProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        return accountTradeRecord;
    }

    /*
     * 添加转出会员转卡交易记录
     * */
    public AccountTradeRecord setOutAccountTradeRecord(StoreMemberList storeMemberList,RechargeAccount rechargeAccount,TurnMembershipCard turnMembershipCard){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(turnMembershipCard.getRollInMemberId());
        accountTradeRecord.setMemberName(turnMembershipCard.getRollInMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        accountTradeRecord.setPrice(turnMembershipCard.getCardRestMoney());
        if(turnMembershipCard.getState() == 3) {//转入审核==添加
//            if(rechargeList.getLiftType()==1){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_upgrade_in);
//            }else if(rechargeList.getLiftType()==2){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_demotion_in);
//            }else{
//                accountTradeRecord.setDocType(Constant.doc_type_recharge);
//            }
            accountTradeRecord.setAccountType(Constant.account_type_recharge_out);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCard.getCardRestMoney());
            accountTradeRecord.setAccountGiftSpendMoney(turnMembershipCard.getGiftConsumeMoney());
        }else {//转入反审核==扣减
            accountTradeRecord.setAccountType(Constant.account_type_recharge_in_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInMoney(turnMembershipCard.getCardRestMoney());
            accountTradeRecord.setGiftInMenoy(turnMembershipCard.getGiftConsumeMoney());

        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.trun_doc_type_recharge);


        accountTradeRecord.setTradeId(turnMembershipCard.getRechargeOrProjectId());
        accountTradeRecord.setPrice(turnMembershipCard.getCardRestMoney());
        accountTradeRecord.setTradeName(turnMembershipCard.getRechargeOrProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCard.getCardRestMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        return accountTradeRecord;
    }

    /*
     * 添加转出会员转卡交易记录
     * */
    public AccountTradeRecord setOutAccountTradeRecord(StoreMemberList storeMemberList,RechargeAccount rechargeAccount,TurnMembershipCard turnMembershipCard,TurnMembershipCardMx turnMembershipCardMx){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(storeMemberList.getId());
        accountTradeRecord.setMemberName(storeMemberList.getMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        if(turnMembershipCard.getState() == 3) {//转入审核==添加
//            if(rechargeList.getLiftType()==1){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_upgrade_in);
//            }else if(rechargeList.getLiftType()==2){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_demotion_in);
//            }else{
//                accountTradeRecord.setDocType(Constant.doc_type_recharge);
//            }
            accountTradeRecord.setAccountType(Constant.account_type_recharge_out);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setAccountGiftSpendMoney(turnMembershipCardMx.getOutGiftConsumeMoney());
        }else {//转入反审核==扣减
            accountTradeRecord.setAccountType(Constant.account_type_recharge_in_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setGiftInMenoy(turnMembershipCardMx.getOutGiftConsumeMoney());

        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.trun_doc_type_recharge);


        accountTradeRecord.setTradeId(turnMembershipCardMx.getRechargeOrProjectId());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setTradeName(turnMembershipCardMx.getRechargeOrProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        return accountTradeRecord;
    }

    public AccountTradeRecord setRechargeOutAccountTradeRecord(StoreMemberList storeMemberList,RechargeAccount rechargeAccount,TurnMembershipCard turnMembershipCard,TurnMembershipCardMx turnMembershipCardMx){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(storeMemberList.getId());
        accountTradeRecord.setMemberName(storeMemberList.getMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        if(turnMembershipCard.getState() == 3) {//转入审核==添加
//            if(rechargeList.getLiftType()==1){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_upgrade_in);
//            }else if(rechargeList.getLiftType()==2){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_demotion_in);
//            }else{
//                accountTradeRecord.setDocType(Constant.doc_type_recharge);
//            }
            accountTradeRecord.setAccountType(Constant.account_type_recharge_out);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setAccountGiftSpendMoney(turnMembershipCardMx.getOutGiftConsumeMoney());
        }else {//转入反审核==扣减
            accountTradeRecord.setAccountType(Constant.account_type_recharge_out_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setGiftInMenoy(turnMembershipCardMx.getOutGiftConsumeMoney());

        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.trun_doc_type_recharge);


        accountTradeRecord.setTradeId(turnMembershipCardMx.getRechargeOrProjectId());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setTradeName(turnMembershipCardMx.getRechargeOrProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        return accountTradeRecord;
    }

    public AccountTradeRecord setOutAccountTradeRecordByRecharge(StoreMemberList storeMemberList,RechargeAccount rechargeAccount,TurnMembershipCard turnMembershipCard,TurnMembershipCardMx turnMembershipCardMx){
        AccountTradeRecord accountTradeRecord = new AccountTradeRecord();
        accountTradeRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        accountTradeRecord.setStoreId(turnMembershipCard.getStoreId());
        accountTradeRecord.setStoreName(turnMembershipCard.getStoreName());
        accountTradeRecord.setMemberId(turnMembershipCard.getRollOutMemberId());
        accountTradeRecord.setMemberName(turnMembershipCard.getRollOutMemberName());
        try {
            accountTradeRecord.setMemberPhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        accountTradeRecord.setAccountId(rechargeAccount.getId());
        accountTradeRecord.setAccountName(rechargeAccount.getRechargeTypeName());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        if(turnMembershipCard.getState() == 3) {//转入审核==添加
//            if(rechargeList.getLiftType()==1){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_upgrade_in);
//            }else if(rechargeList.getLiftType()==2){
//                accountTradeRecord.setDocType(Constant.doc_type_recharge_demotion_in);
//            }else{
//                accountTradeRecord.setDocType(Constant.doc_type_recharge);
//            }
            accountTradeRecord.setAccountType(Constant.account_type_recharge_out);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountSpendMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setAccountGiftSpendMoney(turnMembershipCardMx.getOutGiftConsumeMoney());
        }else {//转入反审核==扣减
            accountTradeRecord.setAccountType(Constant.account_type_recharge_in_cancel);
            accountTradeRecord.setInputDate(turnMembershipCard.getDocDate());
            accountTradeRecord.setInputBy(turnMembershipCard.getAuditBy());
            accountTradeRecord.setAccountInMoney(turnMembershipCardMx.getOutCardRestMoney());
            accountTradeRecord.setGiftInMenoy(turnMembershipCardMx.getOutGiftConsumeMoney());

        }
        accountTradeRecord.setDocId(turnMembershipCard.getId());
        accountTradeRecord.setDocNum(turnMembershipCard.getDocNum());
        accountTradeRecord.setDocType(Constant.trun_doc_type_recharge);


        accountTradeRecord.setTradeId(turnMembershipCardMx.getRechargeOrProjectId());
        accountTradeRecord.setPrice(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setTradeName(turnMembershipCardMx.getRechargeOrProjectName());
        accountTradeRecord.setTradeMoney(turnMembershipCardMx.getOutCardRestMoney());
        accountTradeRecord.setAccountRestMoney(rechargeAccount.getCardRestMoney());
        accountTradeRecord.setAccountConsumeMoney(rechargeAccount.getCardConsumeMoney());
        accountTradeRecord.setAccountGiftRestMoney(rechargeAccount.getCardGiftMoney());
        accountTradeRecord.setAccountGiftConsumeMoney(rechargeAccount.getGiftConsumeMoney());
        return accountTradeRecord;
    }

    public String getNum(String date,Long storeId){
        Example example = new Example(TurnMembershipCard.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("docDate","%" + date + "%");
        criteria.andEqualTo("storeId",storeId);
        List<TurnMembershipCard> list = dao.selectByExample(example);
        String num="ZK";
        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 getKey(String phone){
        String key = "";
        if (StringUtils.isNotBlank(phone)){
            key = Constant.staffKey + phone;
        }
        return key;
    }

    public void rechargeAccountGive(RechargeAccount rechargeAccount1,TurnMembershipCard turnMembershipCard1,BigDecimal giftConsumeMoneyGive,Integer updateType,Integer asType){
        RechargeType rechargeType = ssmRechargeTypeSevice.selectByPrimaryKey(rechargeAccount1.getRechargeTypeId());

        if(rechargeType.getType()==2&&rechargeType.getGiveType()==1){
            //1为修改，0为添加
            RechargeAccountGive rechargeAccountGive=new RechargeAccountGive();
            BeanUtils.copyProperties(rechargeAccount1,rechargeAccountGive);
            rechargeAccountGive.setAccountId(rechargeAccount1.getId());
//            rechargeAccountGive.setStoreId(rechargeAccount1.getStoreId());
            //type=1修改，查询仅门店使用账户赠送卡金额修改
            if(updateType==1){
                Example example = new Example(RechargeAccountGive.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("accountId",rechargeAccountGive.getAccountId());
//                criteria.andEqualTo("storeId",rechargeAccountGive.getStoreId());
                criteria.andEqualTo("memberId",rechargeAccountGive.getMemberId());
                //查询原账户的赠送金额
                List<RechargeAccountGive> List=ssmRechargeAccountGiveDao.selectByExample(example);
                if(List.size()>0){
                    rechargeAccountGive=List.get(0);
                    if(asType==0){//（加）
                        //原账户赠送金额+本单充值赠送金额
                        rechargeAccountGive.setGiftConsumeMoney(rechargeAccountGive.getGiftConsumeMoney().add(giftConsumeMoneyGive));
                        ssmRechargeAccountGiveDao.updateByGifMoney(rechargeAccountGive);
                    }else if(asType==1){//（减）
                        //原账户赠送金额-本单充值赠送金额
                        BigDecimal reduce = List.stream().map(RechargeAccountGive::getGiftConsumeMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                        //如果扣减赠送等于账户所有赠送
                        if(reduce.compareTo(giftConsumeMoneyGive)==0){
                            for (RechargeAccountGive accountGive : List) {

                                accountGive.setGiftConsumeMoney(BigDecimal.ZERO);
                                ssmRechargeAccountGiveDao.updateByGifMoney(accountGive);
//                                ssmRechargeAccountGiveDao.updateByExampleSelective(accountGive,example);
                            }
                        }else if(reduce.compareTo(giftConsumeMoneyGive)==1){
                            //这是一个接收需要扣除的金额
                            BigDecimal sub = giftConsumeMoneyGive;
                            //进行遍历
                            for (RechargeAccountGive accountGive : List) {
                                //当前账户的前不足以支付扣除的赠送金额，则将当前账户的钱进行清零
                                if(accountGive.getGiftConsumeMoney().compareTo(sub)==-1){
                                    //赠送金额减账户余额，计算扣除当前账户还需要扣除剩余的金额
                                    sub = sub.subtract(accountGive.getGiftConsumeMoney());
                                    accountGive.setGiftConsumeMoney(BigDecimal.ZERO);
                                    ssmRechargeAccountGiveDao.updateByGifMoney(accountGive);
//                                    ssmRechargeAccountGiveDao.updateByExampleSelective(accountGive,example);
                                    //如果扣减的赠送金额小于或者等于账号的金额，则将当前账户的金额减去扣除的赠送金额
                                }else if(accountGive.getGiftConsumeMoney().compareTo(sub)==1 || accountGive.getGiftConsumeMoney().compareTo(sub)==0){
                                    sub = accountGive.getGiftConsumeMoney().subtract(sub);
                                    accountGive.setGiftConsumeMoney(sub);
                                    ssmRechargeAccountGiveDao.updateByGifMoney(accountGive);
//                                    ssmRechargeAccountGiveDao.updateByExampleSelective(accountGive,example);
                                }
                            }
                        }
                    }
                }
            }else{//添加一个新仅门店使用的赠卡
                ssmRechargeAccountGiveDao.insert(rechargeAccountGive);
            }

        }
    }

}
