package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmNegativeCardDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.*;
import com.ssm.manage.mapper.CourseListMemberInitMapper;
import com.ssm.manage.query.AccountTradeRecordRequest;
import com.ssm.manage.query.DocPromotionRequest;
import com.ssm.manage.query.RepayTradeRequest;
import com.ssm.manage.query.StoreDailyRequest;
import com.ssm.manage.response.DocPromotionResponse;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.BigDecimalUtils;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

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

@Service
@Slf4j
public class SsmNegativeCardServiceImpl extends BaseServiceImpl<NegativeCardDayRecord> implements SsmNegativeCardService {
    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Autowired
    private CourseMoneyService courseMoneyService;

    @Autowired
    private RechargeMoneyService rechargeMoneyService;

    @Autowired
    private CourseListMemberInitMapper courseListMemberInitMapper;

    @Autowired
    private SsmNegativeCardService ssmNegativeCardService;

    @Autowired
    private SsmBaseZengkaService ssmBaseZengkaService;



    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;
    @Autowired
    private SsmBaseNegativeKazinService ssmBaseNegativeKazinService;

    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmRechargeService ssmRechargeService;
    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmRepayService ssmRepayService;
    @Autowired
    private SsmRepayAccountListService ssmRepayAccountListService;
    @Autowired
    private SsmRepayTradeService ssmRepayTradeService;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;
    @Autowired
    private SsmBaseGrouponCardService ssmBaseGrouponCardService;

    @Autowired
    private SsmRechargeTurnCourseService ssmRechargeTurnCourseService;
    @Autowired
    private SsmMemberInitService ssmMemberInitService;

    @Autowired
    private SsmStoreTurnTreatmentService ssmStoreTurnTreatmentService;

    @Autowired
    private SsmCardChangeService ssmCardChangeService;
    @Autowired
    private SsmCashService ssmCashService;

    @Autowired
    private SsmRefundOrderService ssmRefundOrderService;

    @Autowired
    private SsmRefundCashService ssmRefundCashService;
    @Autowired
    private SsmRefundRechargeService ssmRefundRechargeService;

    @Autowired
    private SsmRefundCourseService ssmRefundCourseService;

    @Autowired
    private SsmNegativeCardDao dao;

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

    @Override
    public List<NegativeDayStatistic> selectDayList(Long storeId, Date date) {
        //查询当天账户交易记录
        String dateStr = DateUtils.ymdFormat(date);
        AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
        accountTradeRecordRequest.setDate(dateStr);
        accountTradeRecordRequest.setInputDate(date);
        accountTradeRecordRequest.setStoreId(storeId);
        List<AccountTradeRecord> accountTradeRecords = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
        List<NegativeDayStatistic> list = new ArrayList<>();
        NegativeDayStatistic recharge = new NegativeDayStatistic();
        NegativeDayStatistic course = new NegativeDayStatistic();
        BigDecimal rechargeMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal courseMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal recRepayMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal courseRepayMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal recStoreCashMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal courseStoreCashMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal recRefundMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal courseRefundMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal cashRefundMoney = BigDecimalUtils.bigDecimal("0");
        BigDecimal cardToCourseMoney = BigDecimalUtils.bigDecimal("0");

        //遍历
        for (AccountTradeRecord accountTradeRecord : accountTradeRecords) {
//            String wwq = "555";
//            if(accountTradeRecord.getOrderStatu()==0){
                if (Constant.doc_type_recharge.equals(accountTradeRecord.getDocType()) || Constant.doc_type_recharge_upgrade_in.equals(accountTradeRecord.getDocType())){
                    if (Constant.account_type_recharge.equals(accountTradeRecord.getAccountType())) {
                        if(null != accountTradeRecord.getAccountInMoney()){
                            rechargeMoney = BigDecimalUtils.add(rechargeMoney, accountTradeRecord.getAccountInMoney());
                        }
                    }else if (Constant.account_type_recharge_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            rechargeMoney = BigDecimalUtils.subtract(rechargeMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }
                }else if (Constant.doc_type_card_use.equals(accountTradeRecord.getDocType())){
//                    if (Constant.account_type_project.equals(accountTradeRecord.getAccountType())) {
//                        if(null != accountTradeRecord.getAccountInMoney()){
//                            courseMoney = BigDecimalUtils.add(courseMoney, accountTradeRecord.getAccountInMoney());
//                        }
//                    }else if (Constant.account_type_project_cancel.equals(accountTradeRecord.getAccountType())){
//                        if(null != accountTradeRecord.getAccountInMoney()){
//                            courseMoney = BigDecimalUtils.subtract(courseMoney, accountTradeRecord.getAccountInMoney());
//                        }
//                    }else if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())){
//                        if(null != accountTradeRecord.getAccountSpendMoney()){
//                            cardToCourseMoney = BigDecimalUtils.add(cardToCourseMoney, accountTradeRecord.getAccountSpendMoney());
//                        }
//
//                    }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
//                        if(null != accountTradeRecord.getAccountInMoney()){
//                            cardToCourseMoney = BigDecimalUtils.subtract(cardToCourseMoney, accountTradeRecord.getAccountInMoney());
//                        }
//
//                    }


                    if (Constant.account_type_project.equals(accountTradeRecord.getAccountType())) {
                        if(null != accountTradeRecord.getAccountInMoney()){
                            courseMoney = BigDecimalUtils.add(courseMoney, accountTradeRecord.getAccountInMoney());
                        }
                    }else if (Constant.account_type_project_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            courseMoney = BigDecimalUtils.subtract(courseMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }else if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountInMoney()){
                            cardToCourseMoney = BigDecimalUtils.add(cardToCourseMoney, accountTradeRecord.getAccountInMoney());
                        }

                    }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            cardToCourseMoney = BigDecimalUtils.subtract(cardToCourseMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }

                }else if (Constant.doc_type_card_change.equals(accountTradeRecord.getDocType())){
//                    if (Constant.account_type_project.equals(accountTradeRecord.getAccountType())) {
//                        if(null != accountTradeRecord.getAccountInMoney()){
//                            courseMoney = BigDecimalUtils.add(courseMoney, accountTradeRecord.getAccountInMoney());
//                        }
//                    }else if (Constant.account_type_project_cancel.equals(accountTradeRecord.getAccountType())){
//                        if(null != accountTradeRecord.getAccountInMoney()){
//                            courseMoney = BigDecimalUtils.subtract(courseMoney, accountTradeRecord.getAccountInMoney());
//                        }
//                    }else if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())){
//                        if(null != accountTradeRecord.getAccountSpendMoney()){
//                            cardToCourseMoney = BigDecimalUtils.add(cardToCourseMoney, accountTradeRecord.getAccountSpendMoney());
//                        }
//
//                    }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
//                        if(null != accountTradeRecord.getAccountInMoney()){
//                            cardToCourseMoney = BigDecimalUtils.subtract(cardToCourseMoney, accountTradeRecord.getAccountInMoney());
//                        }
//
//                    }


                    if (Constant.account_type_project.equals(accountTradeRecord.getAccountType())) {
                        if(null != accountTradeRecord.getCashMoney()){
                            courseMoney = BigDecimalUtils.add(courseMoney, accountTradeRecord.getCashMoney());
                        }
                    }else if (Constant.account_type_project_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getCashMoney()){
                            courseMoney = BigDecimalUtils.subtract(courseMoney, accountTradeRecord.getCashMoney());
                        }
                    }else if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountInMoney()){
                            cardToCourseMoney = BigDecimalUtils.add(cardToCourseMoney, accountTradeRecord.getAccountInMoney());
                        }

                    }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            cardToCourseMoney = BigDecimalUtils.subtract(cardToCourseMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }

                }else if (Constant.doc_type_repay.equals(accountTradeRecord.getDocType()) || Constant.doc_type_repay_promotion.equals(accountTradeRecord.getDocType())){
                    BigDecimal s = courseRepayMoney;
                    if (Constant.account_type_rec_repay.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountInMoney()){
                            recRepayMoney = BigDecimalUtils.add(recRepayMoney, accountTradeRecord.getAccountInMoney());
                        }
                    }else if (Constant.account_type_rec_repay_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            recRepayMoney = BigDecimalUtils.subtract(recRepayMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }else if (Constant.account_type_course_repay.equals(accountTradeRecord.getAccountType()) || Constant.account_type_course_repay_promotion.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountInMoney()){
                            courseRepayMoney = BigDecimalUtils.add(courseRepayMoney, accountTradeRecord.getAccountInMoney());
                        }
                    }else if (Constant.account_type_course_repay_cancel.equals(accountTradeRecord.getAccountType())|| Constant.account_type_course_repay_cancel_promotion.equals(accountTradeRecord.getAccountType())){
                        if(null ==accountTradeRecord.getAccountSpendMoney()){
                            accountTradeRecord.setAccountInMoney(BigDecimal.valueOf(0));
                        }
                        courseRepayMoney = BigDecimalUtils.subtract(courseRepayMoney,accountTradeRecord.getAccountSpendMoney());
                    }


                } else if (Constant.doc_type_promotion.equals(accountTradeRecord.getDocType()) ){
                    if (Constant.account_type_project.equals(accountTradeRecord.getAccountType())) {
                        if(null != accountTradeRecord.getAccountRestMoney()){
                            courseMoney = BigDecimalUtils.add(courseMoney, accountTradeRecord.getAccountRestMoney());
                        }
                    }else if (Constant.account_type_project_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            courseMoney = BigDecimalUtils.subtract(courseMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }else if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountInMoney()){
                            cardToCourseMoney = BigDecimalUtils.add(cardToCourseMoney, accountTradeRecord.getAccountInMoney());
                        }

                    }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            cardToCourseMoney = BigDecimalUtils.subtract(cardToCourseMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }

                } else if (Constant.doc_type_store_cash.equals(accountTradeRecord.getDocType())){
                    if (Constant.account_type_recharge_consume.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            recStoreCashMoney = BigDecimalUtils.add(recStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }else if (Constant.account_type_recharge_consume_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            recStoreCashMoney = BigDecimalUtils.subtract(recStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }else if (Constant.account_type_project_consume.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            courseStoreCashMoney = BigDecimalUtils.add(courseStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                    }else if (Constant.account_type_project_consume_cancel.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            courseStoreCashMoney = BigDecimalUtils.subtract(courseStoreCashMoney, accountTradeRecord.getAccountSpendMoney());
                        }

                    }

                }else if (Constant.doc_type_refund_recharge.equals(accountTradeRecord.getDocType())){
                    if (Constant.account_type_refund_recharge_audit.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            recRefundMoney = BigDecimalUtils.add(recRefundMoney, accountTradeRecord.getAccountSpendMoney());
                        }

                    }else if (Constant.account_type_refund_recharge_unaudit.equals(accountTradeRecord.getAccountType())){
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            recRefundMoney = BigDecimalUtils.subtract(recRefundMoney, accountTradeRecord.getAccountSpendMoney());
                        }

                    }
                }else if (Constant.doc_type_refund_course.equals(accountTradeRecord.getDocType())){
                    if (Constant.account_type_refund_course_audit.equals(accountTradeRecord.getAccountType())){
                        //退款次数*项目金额/项目次数 = 退款金额
//                    courseRefundMoney = BigDecimalUtils.add(courseRefundMoney,  BigDecimalUtils.div(BigDecimalUtils.multiply(accountTradeRecord.getTradeMoney(),BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getAccountSpendCount()))),
//                            BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getTradeCount()))));
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            courseRefundMoney = BigDecimalUtils.add(courseRefundMoney, accountTradeRecord.getAccountSpendMoney());
                        }

                    }else if (Constant.account_type_refund_course_unaudit.equals(accountTradeRecord.getAccountType())){
//                    courseRefundMoney = BigDecimalUtils.subtract(courseRefundMoney, BigDecimalUtils.div(BigDecimalUtils.multiply(accountTradeRecord.getTradeMoney(),BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getAccountInCount()))),
//                            BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getTradeCount()))));
                        if(null != accountTradeRecord.getAccountSpendMoney()){
                            courseRefundMoney = BigDecimalUtils.subtract(courseRefundMoney, accountTradeRecord.getAccountSpendMoney());
                        }

                    }
                }else if (Constant.doc_type_refund_cash.equals(accountTradeRecord.getDocType())){
                    if (Constant.account_type_refund_cash_audit.equals(accountTradeRecord.getAccountType())){

                        //金额不为null 消费退充值卡付
                        if ( null !=accountTradeRecord.getAccountInMoney()) {
                            cashRefundMoney = BigDecimalUtils.add(cashRefundMoney, accountTradeRecord.getAccountInMoney());
                        }
                        //次数不为null 消费退疗程卡付
                        if (accountTradeRecord.getAccountInCount() != null && accountTradeRecord.getAccountGiftRestCount() == null){
                            //校验疗程账户来源 促销活动不计入统计金额
                            CourseAccount courseAccount = ssmCourseAccountService.selectByPrimaryKey(accountTradeRecord.getAccountId());
                            if ("0".equals(courseAccount.getCourseType())) {
                                cashRefundMoney = BigDecimalUtils.add(cashRefundMoney, BigDecimalUtils.div(BigDecimalUtils.multiply(accountTradeRecord.getTradeMoney(), BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getAccountInCount()))),
                                        BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getTradeCount()))));
                            }
                        }
                    }else if (Constant.account_type_refund_cash_unaudit.equals(accountTradeRecord.getAccountType())){
                        //金额不为null 消费退充值卡付
                        if ( null != accountTradeRecord.getAccountSpendMoney()) {
                            cashRefundMoney = BigDecimalUtils.add(cashRefundMoney, accountTradeRecord.getAccountSpendMoney());
                        }
                        //次数不为null 消费退疗程卡付
                        if (  null !=  accountTradeRecord.getAccountSpendCount()){
                            //校验疗程账户来源 促销活动不计入统计金额
                            CourseAccount courseAccount = ssmCourseAccountService.selectByPrimaryKey(accountTradeRecord.getAccountId());
                            if ("0".equals(courseAccount.getCourseType())) {
                                cashRefundMoney = BigDecimalUtils.add(cashRefundMoney, BigDecimalUtils.div(BigDecimalUtils.multiply(accountTradeRecord.getTradeMoney(), BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getAccountSpendCount()))),
                                        BigDecimalUtils.bigDecimal(String.valueOf(accountTradeRecord.getTradeCount()))));
                            }
                        }
                    }
                }
//            }
        }
        //查询负卡金记录表
        Example example = new Example(NegativeCardDayRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", storeId);
        criteria.andLike("date", DateUtils.ymdFormat(DateUtils.diffDay(date, -1)) + "%");
        List<NegativeCardDayRecord> negativeCardDayRecords = ssmNegativeCardService.selectByExample(example);
        if (null == negativeCardDayRecords || negativeCardDayRecords.size() == 0){
            recharge.setStartMoney(BigDecimalUtils.bigDecimal("0"));
            course.setStartMoney(BigDecimalUtils.bigDecimal("0"));
        }else {
            for (NegativeCardDayRecord negativeCardDayRecord : negativeCardDayRecords) {
                if (negativeCardDayRecord.getType() == 1){//充值
                    recharge.setStartMoney(negativeCardDayRecord.getEndMoney());
                }else {
                    course.setStartMoney(negativeCardDayRecord.getEndMoney());
                }
            }
        }

        if(recharge.getStartMoney()==null){
            recharge.setStartMoney(BigDecimalUtils.bigDecimal("0"));
        }else if(course.getStartMoney()==null){
            course.setStartMoney(BigDecimalUtils.bigDecimal("0"));
        }

        recharge.setRechargeMoney(rechargeMoney);
        recharge.setRecRepayMoney(recRepayMoney);
        recharge.setRecStoreCashMoney(recStoreCashMoney);
        recharge.setRecRefundMoney(recRefundMoney);
        recharge.setCardToCourseMoney(cardToCourseMoney);
        recharge.setCashRefundMoney(cashRefundMoney);
        recharge.setEndMoney(BigDecimalUtils.subtract(BigDecimalUtils.subtract(BigDecimalUtils.subtract(BigDecimalUtils.subtract((BigDecimalUtils.add(BigDecimalUtils
                        .add(recharge.getStartMoney(), rechargeMoney),recRepayMoney))
                ,recStoreCashMoney),recRefundMoney),cardToCourseMoney),cashRefundMoney));
        recharge.setType(1);
        course.setCourseMoney(courseMoney);
        course.setCourseRepayMoney(courseRepayMoney);
        course.setCourseStoreCashMoney(courseStoreCashMoney);
        course.setCourseRefundMoney(courseRefundMoney);
        course.setCashRefundMoney(cashRefundMoney);
        course.setEndMoney(BigDecimalUtils.subtract(BigDecimalUtils.subtract(BigDecimalUtils.subtract((BigDecimalUtils.add(BigDecimalUtils
                        .add(course.getStartMoney(), courseMoney),courseRepayMoney))
                ,courseStoreCashMoney),courseRefundMoney),cashRefundMoney));
        course.setType(2);
        list.add(recharge);
        list.add(course);
        return list;
    }

    @Override
    public RechargeNegative rechargeDayStatistic(Long storeId, Date date) {
        //查询当天账户交易记录
        List<NegativeKazin> rechargeArray = new ArrayList<>();
        String dateStr = DateUtils.ymdFormat(date);
        NegativeKazin negativeKazin = new  NegativeKazin();

        RechargeNegative rechargeNegative = new RechargeNegative();

//        List<NegativeDayStatistic> list = new ArrayList<>();
//
////        BigDecimal courseMoney = BigDecimalUtils.bigDecimal("0");
//
////        BigDecimal courseRepayMoney = BigDecimalUtils.bigDecimal("0");
//
////        BigDecimal courseRepayCashMoney = BigDecimalUtils.bigDecimal("0"); //疗程卡支付还款
//        BigDecimal recStoreCashMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal courseStoreCashMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal recRefundMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal courseRefundMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal cashRefundMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal cardToCourseMoney = BigDecimalUtils.bigDecimal("0");

        BigDecimal rechargeMoney = BigDecimalUtils.bigDecimal("0");//充值金额
        BigDecimal InitRechargeMoney = BigDecimalUtils.bigDecimal("0");//充值金额
        BigDecimal GrouprechargeMoney = BigDecimalUtils.bigDecimal("0");//充值金额
        BigDecimal recRepayMoney = BigDecimalUtils.bigDecimal("0");//充值还款金额
        BigDecimal cashMoney = BigDecimalUtils.bigDecimal("0"); //收银支付金额
        BigDecimal cashRefundMoney = BigDecimalUtils.bigDecimal("0"); //收银支付退金额
        BigDecimal rechargeCourse = BigDecimalUtils.bigDecimal("0"); //购疗程金额
        BigDecimal rechargePromCourse = BigDecimalUtils.bigDecimal("0"); //购促销疗程金额
        BigDecimal rechargeRefundMoney = BigDecimalUtils.bigDecimal("0"); //充值退


        StoreDailyRequest storeDailyRequest = new StoreDailyRequest();
        storeDailyRequest.setStoreId(storeId);
        storeDailyRequest.setDateStr(dateStr);
        storeDailyRequest.setIsCancel(0);
        storeDailyRequest.setDate(date);

        //充值
        StoreDaily storeDaily = ssmRechargeService.NegativeKazin(storeDailyRequest);
//        rechargeMoney=rechargeMoney.add(storeDaily.getRechargeMoney());
        if(null == storeDaily){
//            NegativeKazin negativeKazin1 = NegativeKazin.getNegativeKazin();
//            negativeKazin1.setType("充值开卡");
//            rechargeArray.add(negativeKazin1);
            rechargeNegative.setRechargeMoney(BigDecimal.valueOf(0));
        }else{
//            NegativeKazin negativeKazin1 = NegativeKazin.getNegativeKazin();
//            negativeKazin1.setType("充值开卡");
//            negativeKazin1.setEarnings(storeDaily.getRechargeMoney());
//            rechargeArray.add(negativeKazin1);
            rechargeNegative.setRechargeMoney(storeDaily.getRechargeMoney());
        }


        //购疗程
        StoreDaily storeDaily1 = ssmCourseService.NegativeKazin(storeDailyRequest);
//        rechargeCourse=rechargeCourse.add(storeDaily1.getRechargeMoney());
        if(null == storeDaily1){
//            NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//            negativeKazin2.setType("疗程开卡");
//            rechargeArray.add(negativeKazin2);
            rechargeNegative.setCourseMoney(BigDecimal.valueOf(0));
        }else{
//            NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//            negativeKazin2.setType("疗程开卡");
//            negativeKazin2.setExpenditure(storeDaily1.getRechargeMoney());
//            rechargeArray.add(negativeKazin2);
            rechargeNegative.setCourseMoney(storeDaily1.getRechargeMoney());
        }

//        course

        RepayTradeRequest repayTradeList = new RepayTradeRequest();
        repayTradeList.setRepayDate(date);
        repayTradeList.setStoreId(storeId);
        List<RepayTradeList> select = ssmRepayTradeService.selectFKJ(repayTradeList);
        for (RepayTradeList tradeList : select) {
            RepayList repayList = new RepayList();
            repayList.setRepayNum(tradeList.getRepayNum());
            repayList.setStoreId(tradeList.getStoreId());
            repayList.setIsCancel(0);
            List<RepayList> select1 = ssmRepayService.select(repayList);
            if(select1.size()>0){
                //存在正常单据
                //还款
                if("card_recharge".equals(tradeList.getDocType())){
                    recRepayMoney=recRepayMoney.add(tradeList.getRepayMoney());
                }
                RepayAccountList repayAccountList = new RepayAccountList();
                repayAccountList.setRepayTradeId(tradeList.getId());
                List<RepayAccountList> select2 = ssmRepayAccountListService.select(repayAccountList);
                for (RepayAccountList accountList : select2) {
                    if(accountList.getAccountType()==0){
                        //购疗程
                        rechargeCourse=rechargeCourse.add(accountList.getPayMoney());
                    }
                }
            }
        }

//        NegativeKazin negativeKazin3 = NegativeKazin.getNegativeKazin();
//        negativeKazin3.setType("充值还款");
//        negativeKazin3.setEarnings(recRepayMoney);
//        negativeKazin3.setExpenditure(rechargeCourse);
//        rechargeArray.add(negativeKazin3);
        rechargeNegative.setRechargeRepay(recRepayMoney);
        rechargeNegative.setRechargeRepayMoney(rechargeCourse);

        //收银
        try {
            List<StoreDaily> storeDailies = ssmCashService.selectDayRecordRec(storeDailyRequest);
            if(null == storeDailies){
//                NegativeKazin negativeKazin4 = NegativeKazin.getNegativeKazin();
//                negativeKazin4.setType("收银");
//                rechargeArray.add(negativeKazin4);
                rechargeNegative.setCashMoney(BigDecimal.valueOf(0));
            }else{
                for (StoreDaily daily : storeDailies) {
                    cashMoney=cashMoney.add(daily.getRechargeMoney());
                }
//                NegativeKazin negativeKazin4 = NegativeKazin.getNegativeKazin();
//                negativeKazin4.setType("收银");
//                negativeKazin4.setExpenditure(cashMoney);
//                rechargeArray.add(negativeKazin4);
                rechargeNegative.setCashMoney(cashMoney);
            }

        } catch (SmsException e) {
            e.printStackTrace();
        }

        //查询促销充值卡付
        DocPromotionRequest docPromotion = new DocPromotionRequest();
        docPromotion.setDocDate(date);
        docPromotion.setStoreId(storeId);
        List<DocPromotion> docPromotionResponses = ssmDocPromotionService.selectFKJ(docPromotion);
        for (DocPromotion docPromotionRespons : docPromotionResponses) {
            //购疗程
            rechargePromCourse=rechargePromCourse.add(docPromotionRespons.getCollectionRechargeAmount());
        }
//        NegativeKazin negativeKazin5 = NegativeKazin.getNegativeKazin();
//        negativeKazin5.setType("疗程促销");
//        negativeKazin5.setExpenditure(rechargePromCourse);
//        rechargeArray.add(negativeKazin5);
        rechargeNegative.setCoursePromotion(rechargePromCourse);

        //充值转疗程
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setDocDate(date);
        rechargeTurnCourse.setStoreId(storeId);
        List<RechargeTurnCourse> rechargeTurnCourses = ssmRechargeTurnCourseService.selectFKJ(rechargeTurnCourse);
        for (RechargeTurnCourse rechargeTurnCours : rechargeTurnCourses) {
            rechargeMoney=rechargeMoney.add(rechargeTurnCours.getRechargeInMoney());
        }
//        NegativeKazin negativeKazin6 = NegativeKazin.getNegativeKazin();
//        negativeKazin6.setType("疗程转充值");
//        negativeKazin6.setEarnings(rechargeMoney);
//        rechargeArray.add(negativeKazin6);
        rechargeNegative.setCourseTurnRecharge(rechargeMoney);

        //充值退@消费退
        RefundOrderList refundOrderList = new RefundOrderList();
        refundOrderList.setRefundDate(date);
        refundOrderList.setStoreId(storeId);
        List<RefundOrderList> refundOrderLists = ssmRefundOrderService.selectFKJ(refundOrderList);
        for (RefundOrderList orderList : refundOrderLists) {
            if(orderList.getType()==0){
                RefundRecharge refundRecharge = new RefundRecharge();
                refundRecharge.setStoreId(orderList.getStoreId());
                refundRecharge.setRefundNum(orderList.getRefundNum());
                List<RefundRecharge> select1 = ssmRefundRechargeService.select(refundRecharge);
                BigDecimal reduce = select1.stream().map(RefundRecharge::getRecRefundMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                rechargeRefundMoney=rechargeRefundMoney.add(reduce);
            }else if(orderList.getType()==2){
                RefundCash refundCash = new RefundCash();
                refundCash.setStoreId(orderList.getStoreId());
                refundCash.setRefundNum(orderList.getRefundNum());
                List<RefundCash> select1 = ssmRefundCashService.select(refundCash);
                for (RefundCash cash : select1) {
                    CashList cashList = new CashList();
                    cashList.setId(cash.getCashId());
                    cashList.setCashDate(cash.getCashDate());
                    List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                    BigDecimal reduce = cashLists.stream().map(CashList::getCollectionRechargeAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    cashRefundMoney=cashRefundMoney.add(reduce);
                }
            }
        }

//        NegativeKazin negativeKazin7 = NegativeKazin.getNegativeKazin();
//        negativeKazin7.setType("充值退");
//        negativeKazin7.setExpenditure(rechargeRefundMoney);
//        rechargeArray.add(negativeKazin7);

        rechargeNegative.setRechargeRefundMoney(rechargeRefundMoney);


//        NegativeKazin negativeKazin8 = NegativeKazin.getNegativeKazin();
//        negativeKazin8.setType("消费退");
//        negativeKazin8.setEarnings(cashRefundMoney);
//        rechargeArray.add(negativeKazin8);

        rechargeNegative.setCashRefund(cashRefundMoney);

        //团购卡
        BaseGrouponCard baseGrouponCard = new BaseGrouponCard();
        baseGrouponCard.setDocDate(date);
        baseGrouponCard.setStoreId(storeId);
        List<BaseGrouponCard> baseGrouponCards = ssmBaseGrouponCardService.selectFKJ(baseGrouponCard);
        for (BaseGrouponCard grouponCard : baseGrouponCards) {
            BigDecimal multiply = grouponCard.getRechargeMoney().multiply(BigDecimal.valueOf(grouponCard.getQuantity()));
            GrouprechargeMoney=GrouprechargeMoney.add(multiply);
        }

//        NegativeKazin negativeKazin9 = NegativeKazin.getNegativeKazin();
//        negativeKazin9.setType("团购卡");
//        negativeKazin9.setEarnings(GrouprechargeMoney);
//        rechargeArray.add(negativeKazin9);

        rechargeNegative.setGroupPurchase(GrouprechargeMoney);

        //初始化
        MemberInit memberInit = new MemberInit();
        memberInit.setStoreId(storeId);
        memberInit.setDocDate(date);
        List<MemberInit> memberInits = ssmMemberInitService.selectFKJ(memberInit);
        BigDecimal reduce = memberInits.stream().filter(memberInit1 -> memberInit1.getCardType() == 0).map(MemberInit::getRestMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//        InitRechargeMoney=InitRechargeMoney.add(reduce);

//        NegativeKazin negativeKazin10 = NegativeKazin.getNegativeKazin();
//        negativeKazin10.setType("初始化");
//        negativeKazin10.setEarnings(reduce);
//        rechargeArray.add(negativeKazin10);

        rechargeNegative.setInit(reduce);

        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);
        rechargeNegative.setStoreName(baseStore.getStoreName());

        return rechargeNegative;

    }

    @Override
    public RechargeNegative rechargeDayStatisticByDate(Long storeId, Date startTime,Date endTime) {
        //查询当天账户交易记录
        List<NegativeKazin> rechargeArray = new ArrayList<>();
        String dateStr = DateUtils.ymdFormat(startTime);
        NegativeKazin negativeKazin = new  NegativeKazin();

        RechargeNegative rechargeNegative = new RechargeNegative();
        rechargeNegative.setOpenning(BigDecimal.ZERO);
        rechargeNegative.setOpenningGift(BigDecimal.ZERO);
//        List<NegativeDayStatistic> list = new ArrayList<>();
//
////        BigDecimal courseMoney = BigDecimalUtils.bigDecimal("0");
//
////        BigDecimal courseRepayMoney = BigDecimalUtils.bigDecimal("0");
//
////        BigDecimal courseRepayCashMoney = BigDecimalUtils.bigDecimal("0"); //疗程卡支付还款
//        BigDecimal recStoreCashMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal courseStoreCashMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal recRefundMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal courseRefundMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal cashRefundMoney = BigDecimalUtils.bigDecimal("0");
//        BigDecimal cardToCourseMoney = BigDecimalUtils.bigDecimal("0");

        BigDecimal rechargeMoney = BigDecimalUtils.bigDecimal("0");//充值金额
        BigDecimal InitRechargeMoney = BigDecimalUtils.bigDecimal("0");//充值金额
        BigDecimal GrouprechargeMoney = BigDecimalUtils.bigDecimal("0");//充值金额
        BigDecimal recRepayMoney = BigDecimalUtils.bigDecimal("0");//充值还款金额
        BigDecimal cashMoney = BigDecimalUtils.bigDecimal("0"); //收银支付金额
        BigDecimal cashRechargeGifMoney = BigDecimalUtils.bigDecimal("0"); //收银支付金额
        BigDecimal repayRechargeGifMoney = BigDecimalUtils.bigDecimal("0"); //收银支付金额
        BigDecimal rechargeGifMoney = BigDecimalUtils.bigDecimal("0"); //收银支付金额
        BigDecimal cashRefundMoney = BigDecimalUtils.bigDecimal("0"); //收银支付退金额
        BigDecimal rechargeCourse = BigDecimalUtils.bigDecimal("0"); //购疗程金额
        BigDecimal rechargePromCourse = BigDecimalUtils.bigDecimal("0"); //购促销疗程金额
        BigDecimal rechargeRefundMoney = BigDecimalUtils.bigDecimal("0"); //充值退
        BigDecimal refundGifMoney;
        BigDecimal refundCashGifMoney = BigDecimal.ZERO;
        BigDecimal turnOutStoreGifMoney;
        BigDecimal turnInStoreGifMoney;


        StoreDailyRequest storeDailyRequest = new StoreDailyRequest();
        storeDailyRequest.setStoreId(storeId);
        storeDailyRequest.setDateStr(dateStr);
        storeDailyRequest.setIsCancel(0);
        storeDailyRequest.setDate(startTime);
        storeDailyRequest.setStartTime(startTime);
        storeDailyRequest.setEndTime(endTime);

        NegativeDayStatistic negativeDayStatistic = new NegativeDayStatistic();
        List<Long> storeIds = new ArrayList<>();
        storeIds.add(storeId);
        negativeDayStatistic.setStoreIdl(storeIds);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);//设置起时间
        calendar.add(Calendar.MONTH, -1);//减一个月
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DATE));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormata = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTimeStr = simpleDateFormat.format(calendar.getTime());
        endTimeStr = endTimeStr + " 23:59:59";
        Date time = null;
        try {
            time = simpleDateFormata.parse(endTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
//        Date time = calendar.getTime();
        negativeDayStatistic.setStartTime(time);
        List<RechargeMoney> rechargeMoneyList = rechargeMoneyService.fixedNegativeCardByRecharge(negativeDayStatistic);
        if(rechargeMoneyList!=null && rechargeMoneyList.size()>0){
            RechargeMoney rechargeMoney1 = rechargeMoneyList.get(0);
            rechargeNegative.setOpenning(rechargeMoney1.getBalance());
            rechargeNegative.setOpenningGift(rechargeMoney1.getBalanceGift());

        }
        else{
            calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DATE));
            Date kaiShi = calendar.getTime();
            String kaiShiTime = simpleDateFormat.format(kaiShi);
            kaiShiTime = kaiShiTime + " 00:00:00";
            String chushihua = "2023-04-01 00:00:00";
            Date parse;
            Date chuShiTime;
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                parse = simpleDateFormat.parse(kaiShiTime);
                chuShiTime = simpleDateFormat.parse(chushihua);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            if(parse.after(chuShiTime)) {
                RechargeNegative courseNegative = rechargeDayStatisticByDate(storeId, parse, time);
                rechargeNegative.setOpenning(courseNegative.getBalance());
            }
        }

        //查询赠卡金额
        BigDecimal zengkaRecharge = ssmBaseZengkaService.NegativeKazinByDate(storeDailyRequest);

        //充值
        StoreDaily storeDaily = ssmRechargeService.NegativeKazinByDate(storeDailyRequest);
//        rechargeMoney=rechargeMoney.add(storeDaily.getRechargeMoney());
        if(null == storeDaily){
//            NegativeKazin negativeKazin1 = NegativeKazin.getNegativeKazin();
//            negativeKazin1.setType("充值开卡");
//            rechargeArray.add(negativeKazin1);
            rechargeNegative.setRechargeMoney(BigDecimal.valueOf(0));
            rechargeNegative.setRechargeGifMoney(BigDecimal.valueOf(0));
        }else{
//            NegativeKazin negativeKazin1 = NegativeKazin.getNegativeKazin();
//            negativeKazin1.setType("充值开卡");
//            negativeKazin1.setEarnings(storeDaily.getRechargeMoney());
//            rechargeArray.add(negativeKazin1);
            rechargeNegative.setRechargeMoney(storeDaily.getRechargeMoney());
            rechargeNegative.setRechargeGifMoney(storeDaily.getRechargeGifMoney());
        }
        rechargeNegative.setZengka(BigDecimal.ZERO);
        if(zengkaRecharge!=null){
            rechargeNegative.setZengka(zengkaRecharge);
//            rechargeNegative.setRechargeGifMoney(rechargeNegative.getRechargeGifMoney().add(zengkaRecharge));
        }


        //购疗程
        StoreDaily storeDaily1 = ssmCourseService.NegativeKazinByDate(storeDailyRequest);
//        rechargeCourse=rechargeCourse.add(storeDaily1.getRechargeMoney());
        if(null == storeDaily1){
//            NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//            negativeKazin2.setType("疗程开卡");
//            rechargeArray.add(negativeKazin2);
            rechargeNegative.setCourseMoney(BigDecimal.valueOf(0));
        }else{
//            NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//            negativeKazin2.setType("疗程开卡");
//            negativeKazin2.setExpenditure(storeDaily1.getRechargeMoney());
//            rechargeArray.add(negativeKazin2);
            rechargeNegative.setCourseMoney(storeDaily1.getRechargeMoney());
        }

//        course

        RepayTradeRequest repayTradeList = new RepayTradeRequest();
        repayTradeList.setRepayDate(startTime);
        repayTradeList.setStartTime(startTime);
        repayTradeList.setEndTime(endTime);
        repayTradeList.setStoreId(storeId);
        List<RepayTradeList> select = ssmRepayTradeService.selectFKJByDate(repayTradeList);
        for (RepayTradeList tradeList : select) {
            RepayList repayList = new RepayList();
            repayList.setRepayNum(tradeList.getRepayNum());
            repayList.setStoreId(tradeList.getStoreId());
            repayList.setIsCancel(0);
            List<RepayList> select1 = ssmRepayService.select(repayList);
            if(select1.size()>0){
                //存在正常单据
                //还款
                if("card_recharge".equals(tradeList.getDocType())){
                    recRepayMoney=recRepayMoney.add(tradeList.getPayMoney());
                    if(tradeList.getBackGiftRestMoney()==null){
                        tradeList.setBackGiftRestMoney(BigDecimal.ZERO);
                    }
                    if(tradeList.getBeforeGiftRestMoney()==null){
                        tradeList.setBeforeGiftRestMoney(BigDecimal.ZERO);
                    }
                    repayRechargeGifMoney=repayRechargeGifMoney.add(tradeList.getBackGiftRestMoney().subtract(tradeList.getBeforeGiftRestMoney()));
                }
                RepayAccountList repayAccountList = new RepayAccountList();
                repayAccountList.setRepayTradeId(tradeList.getId());
                List<RepayAccountList> select2 = ssmRepayAccountListService.select(repayAccountList);
                for (RepayAccountList accountList : select2) {
                    if(accountList.getAccountType()==0){
                        //购疗程
                        rechargeCourse=rechargeCourse.add(accountList.getPayMoney());
                    }
                }
            }
        }

//        NegativeKazin negativeKazin3 = NegativeKazin.getNegativeKazin();
//        negativeKazin3.setType("充值还款");
//        negativeKazin3.setEarnings(recRepayMoney);
//        negativeKazin3.setExpenditure(rechargeCourse);
//        rechargeArray.add(negativeKazin3);
        rechargeNegative.setRechargeRepay(recRepayMoney);
        rechargeNegative.setRepayRechargeGifMoney(repayRechargeGifMoney);
        rechargeNegative.setRechargeRepayMoney(rechargeCourse);

        //收银
        try {
            List<StoreDaily> storeDailies = ssmCashService.selectDayRecordRecByDate(storeDailyRequest);
            if(null == storeDailies){
//                NegativeKazin negativeKazin4 = NegativeKazin.getNegativeKazin();
//                negativeKazin4.setType("收银");
//                rechargeArray.add(negativeKazin4);
                rechargeNegative.setCashMoney(BigDecimal.valueOf(0));
                rechargeNegative.setRechargeGifMoney(BigDecimal.ZERO);
            }else{
                for (StoreDaily daily : storeDailies) {
                    cashMoney=cashMoney.add(daily.getRechargeMoney());
                    cashRechargeGifMoney = cashRechargeGifMoney.add(daily.getRechargeGifMoney());
                }
//                NegativeKazin negativeKazin4 = NegativeKazin.getNegativeKazin();
//                negativeKazin4.setType("收银");
//                negativeKazin4.setExpenditure(cashMoney);
//                rechargeArray.add(negativeKazin4);
                rechargeNegative.setCashMoney(cashMoney);
                rechargeNegative.setCashRechargeGifMoney(cashRechargeGifMoney);
            }

        } catch (SmsException e) {
            e.printStackTrace();
        }

        //查询促销充值卡付
        DocPromotionRequest docPromotion = new DocPromotionRequest();
        docPromotion.setDocDate(startTime);
        docPromotion.setStartTime(startTime);
        docPromotion.setEndTime(endTime);
        docPromotion.setStoreId(storeId);
        List<DocPromotion> docPromotionResponses = ssmDocPromotionService.selectFKJByDate(docPromotion);
        for (DocPromotion docPromotionRespons : docPromotionResponses) {
            //购疗程
            rechargePromCourse=rechargePromCourse.add(docPromotionRespons.getCollectionRechargeAmount());
        }
//        NegativeKazin negativeKazin5 = NegativeKazin.getNegativeKazin();
//        negativeKazin5.setType("疗程促销");
//        negativeKazin5.setExpenditure(rechargePromCourse);
//        rechargeArray.add(negativeKazin5);
        rechargeNegative.setCoursePromotion(rechargePromCourse);

        //充值转疗程
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setDocDate(startTime);
        rechargeTurnCourse.setStartTime(startTime);
        rechargeTurnCourse.setEndTime(endTime);
        rechargeTurnCourse.setStoreId(storeId);
        List<RechargeTurnCourse> rechargeTurnCourses = ssmRechargeTurnCourseService.selectFKJByDate(rechargeTurnCourse);
        for (RechargeTurnCourse rechargeTurnCours : rechargeTurnCourses) {
            rechargeMoney=rechargeMoney.add(rechargeTurnCours.getRechargeInMoney());
        }
//        NegativeKazin negativeKazin6 = NegativeKazin.getNegativeKazin();
//        negativeKazin6.setType("疗程转充值");
//        negativeKazin6.setEarnings(rechargeMoney);
//        rechargeArray.add(negativeKazin6);
        rechargeNegative.setCourseTurnRecharge(rechargeMoney);
        //查询充值退款的退款
        refundGifMoney =  ssmRefundRechargeService.selectFKJByDate(storeDailyRequest);
        if(refundGifMoney==null){
            refundGifMoney = BigDecimal.ZERO;
        }
        rechargeNegative.setRefundGifMoney(refundGifMoney);
        //查询充值卡赠送转门店
        turnOutStoreGifMoney =  ssmRefundRechargeService.selectFKJByRechargeOut(storeDailyRequest);
        turnInStoreGifMoney =  ssmRefundRechargeService.selectFKJByRechargeIn(storeDailyRequest);
        if(turnOutStoreGifMoney==null){
            turnOutStoreGifMoney = BigDecimal.ZERO;
        }
        if(turnInStoreGifMoney==null){
            turnInStoreGifMoney = BigDecimal.ZERO;
        }
        rechargeNegative.setTurnOutStoreGifMoney(turnOutStoreGifMoney);
        rechargeNegative.setTurnInStoreGifMoney(turnInStoreGifMoney);
        //消费退赠送金额
        List<RefundCash> refundCashes = ssmRefundCashService.selectFKJByRefundCashGifMoney(storeDailyRequest);
        //查询原单据
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Map<String, List<RefundCash>> collect = refundCashes.stream().collect(Collectors.groupingBy(item -> dateFormat.format(item.getCashDate())));
        Iterator<Map.Entry<String, List<RefundCash>>> iterator = collect.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, List<RefundCash>> next = iterator.next();
            String date = next.getKey();
            List<RefundCash> value = next.getValue();
            List<Long> cashId = value.stream().map(item -> item.getCashId()).collect(Collectors.toList());
            Date parse;
            try {
                parse = dateFormat.parse(date);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            storeDailyRequest.setCashIds(cashId);
            storeDailyRequest.setDate(parse);
            BigDecimal augend = ssmCashService.selectFKJByRefundCashGifMoney(storeDailyRequest);
            refundCashGifMoney = refundCashGifMoney.add(augend==null?BigDecimal.ZERO:augend);
        }
        //设置消费退的赠送金额
        rechargeNegative.setRefundCashGifMoney(refundCashGifMoney);


        //充值退@消费退
        RefundOrderList refundOrderList = new RefundOrderList();
        refundOrderList.setRefundDate(startTime);
        refundOrderList.setStartTime(startTime);
        refundOrderList.setEndTime(endTime);
        refundOrderList.setStoreId(storeId);
        List<RefundOrderList> refundOrderLists = ssmRefundOrderService.selectFKJByDate(refundOrderList);
        for (RefundOrderList orderList : refundOrderLists) {
            if(orderList.getType()==0){
                RefundRecharge refundRecharge = new RefundRecharge();
                refundRecharge.setStoreId(orderList.getStoreId());
                refundRecharge.setRefundNum(orderList.getRefundNum());
                List<RefundRecharge> select1 = ssmRefundRechargeService.select(refundRecharge);
                BigDecimal reduce = select1.stream().map(RefundRecharge::getRecRefundMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                rechargeRefundMoney=rechargeRefundMoney.add(reduce);
            }else if(orderList.getType()==2){
                RefundCash refundCash = new RefundCash();
                refundCash.setStoreId(orderList.getStoreId());
                refundCash.setRefundNum(orderList.getRefundNum());
                List<RefundCash> select1 = ssmRefundCashService.select(refundCash);
                for (RefundCash cash : select1) {
                    CashList cashList = new CashList();
                    cashList.setId(cash.getCashId());
                    cashList.setCashDate(cash.getCashDate());
                    List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                    BigDecimal reduce = cashLists.stream().map(CashList::getCollectionRechargeAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    cashRefundMoney=cashRefundMoney.add(reduce);
                }
            }
        }
        cashRefundMoney = cashRefundMoney.subtract(refundCashGifMoney);

//        NegativeKazin negativeKazin7 = NegativeKazin.getNegativeKazin();
//        negativeKazin7.setType("充值退");
//        negativeKazin7.setExpenditure(rechargeRefundMoney);
//        rechargeArray.add(negativeKazin7);

        rechargeNegative.setRechargeRefundMoney(rechargeRefundMoney);


//        NegativeKazin negativeKazin8 = NegativeKazin.getNegativeKazin();
//        negativeKazin8.setType("消费退");
//        negativeKazin8.setEarnings(cashRefundMoney);
//        rechargeArray.add(negativeKazin8);

        rechargeNegative.setCashRefund(cashRefundMoney);

        //团购卡
        BaseGrouponCard baseGrouponCard = new BaseGrouponCard();
        baseGrouponCard.setDocDate(startTime);
        baseGrouponCard.setStartTime(startTime);
        baseGrouponCard.setEndTime(endTime);
        baseGrouponCard.setStoreId(storeId);
        List<BaseGrouponCard> baseGrouponCards = ssmBaseGrouponCardService.selectFKJByDate(baseGrouponCard);
        for (BaseGrouponCard grouponCard : baseGrouponCards) {
            BigDecimal multiply = grouponCard.getRechargeMoney().multiply(BigDecimal.valueOf(grouponCard.getQuantity()));
            GrouprechargeMoney=GrouprechargeMoney.add(multiply);
        }

//        NegativeKazin negativeKazin9 = NegativeKazin.getNegativeKazin();
//        negativeKazin9.setType("团购卡");
//        negativeKazin9.setEarnings(GrouprechargeMoney);
//        rechargeArray.add(negativeKazin9);

        rechargeNegative.setGroupPurchase(GrouprechargeMoney);

        //初始化
        MemberInit memberInit = new MemberInit();
        memberInit.setStoreId(storeId);
        memberInit.setDocDate(startTime);
        memberInit.setStartTime(startTime);
        memberInit.setEndTime(endTime);
        List<MemberInit> memberInits = ssmMemberInitService.selectFKJByDate(memberInit);
        BigDecimal reduce = memberInits.stream().filter(memberInit1 -> memberInit1.getCardType() == 0).map(MemberInit::getRestMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
//        InitRechargeMoney=InitRechargeMoney.add(reduce);

//        NegativeKazin negativeKazin10 = NegativeKazin.getNegativeKazin();
//        negativeKazin10.setType("初始化");
//        negativeKazin10.setEarnings(reduce);
//        rechargeArray.add(negativeKazin10);

        rechargeNegative.setInit(reduce);

        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);
        rechargeNegative.setStoreName(baseStore.getStoreNum() +"-"+ baseStore.getStoreName());
        rechargeNegative.setStoreId(storeId);

        BigDecimal shouru = rechargeNegative.getRechargeMoney().add(rechargeNegative.getRechargeRepay())
//                .add(rechargeNegative.getRechargeGifMoney()).add(rechargeNegative.getRepayRechargeGifMoney())
//                .add(rechargeNegative.getTurnInStoreGifMoney())
                .add(rechargeNegative.getCourseTurnRecharge()).add(rechargeNegative.getCashRefund())
//                .add(rechargeNegative.getRefundCashGifMoney())
                .add(rechargeNegative.getGroupPurchase()).add(rechargeNegative.getInit()).add(rechargeNegative.getZengka());


        BigDecimal zhichu = rechargeNegative.getCourseMoney().add(rechargeNegative.getRechargeRepayMoney()).add(rechargeNegative.getCashMoney()).add(rechargeNegative.getCoursePromotion())
                .add(rechargeNegative.getRechargeRefundMoney());
//                .add(rechargeNegative.getCashRechargeGifMoney()).add(rechargeNegative.getRefundGifMoney()).add(rechargeNegative.getTurnOutStoreGifMoney());

        BigDecimal shouruGif = rechargeNegative.getRechargeGifMoney().add(rechargeNegative.getRepayRechargeGifMoney())
                .add(rechargeNegative.getTurnInStoreGifMoney()).add(rechargeNegative.getRefundCashGifMoney());

        BigDecimal zhichuGif = rechargeNegative.getCashRechargeGifMoney().add(rechargeNegative.getRefundGifMoney()).add(rechargeNegative.getTurnOutStoreGifMoney());

        rechargeNegative.setBalance(rechargeNegative.getOpenning().add(shouru).subtract(zhichu));
        rechargeNegative.setBalanceGift(rechargeNegative.getOpenningGift().add(shouruGif).subtract(zhichuGif));


        return rechargeNegative;

    }

    @Override
    public CourseNegative CourseDayStatistic(Long storeId, Date date) {
        String dateStr = DateUtils.ymdFormat(date);

//        List<NegativeKazin> negativeKazins = new ArrayList<>();

        CourseNegative rechargeNegative = new CourseNegative();
        BigDecimal courseMoney = BigDecimalUtils.bigDecimal("0");//疗程金额
        BigDecimal courseRepayMoney = BigDecimalUtils.bigDecimal("0"); //疗程还款金额
        BigDecimal courseTurnRechargeMoney = BigDecimalUtils.bigDecimal("0"); //疗程还款疗程支付金额
        BigDecimal cashMoney = BigDecimalUtils.bigDecimal("0"); //收银支付金额
        BigDecimal cashRefundMoney = BigDecimalUtils.bigDecimal("0"); //收银支付退金额
        BigDecimal courseRefund = BigDecimalUtils.bigDecimal("0"); //疗程退金额
        BigDecimal coursePromotionRepay = BigDecimal.ZERO;


        StoreDailyRequest storeDailyRequest = new StoreDailyRequest();
        storeDailyRequest.setStoreId(storeId);
        storeDailyRequest.setDateStr(dateStr);
        storeDailyRequest.setIsCancel(0);
        storeDailyRequest.setDate(date);

        //疗程开卡
        StoreDaily storeDaily1 = ssmCourseService.NegativeKazin(storeDailyRequest);
//        courseMoney=courseMoney.add(storeDaily1.getCourseMoney());
        if(null == storeDaily1){
//            NegativeKazin negativeKazin = NegativeKazin.getNegativeKazin();
//            negativeKazin.setType("疗程开卡");
//            negativeKazins.add(negativeKazin);

            rechargeNegative.setCourseMoney(BigDecimal.valueOf(0));
        }else{
//            NegativeKazin negativeKazin = NegativeKazin.getNegativeKazin();
//            negativeKazin.setType("疗程开卡");
//            negativeKazin.setEarnings(storeDaily1.getCourseMoney());
//            negativeKazins.add(negativeKazin);
            rechargeNegative.setCourseMoney(storeDaily1.getCourseMoney());
        }

        //还款
        RepayTradeRequest repayTradeList = new RepayTradeRequest();
        repayTradeList.setRepayDate(date);
        repayTradeList.setStoreId(storeId);
        List<RepayTradeList> select = ssmRepayTradeService.selectFKJ(repayTradeList);
        for (RepayTradeList tradeList : select) {
            RepayList repayList = new RepayList();
            repayList.setRepayNum(tradeList.getRepayNum());
            repayList.setStoreId(tradeList.getStoreId());
            repayList.setIsCancel(0);
            List<RepayList> select1 = ssmRepayService.select(repayList);
            if(select1.size()>0){
                //存在正常单据
                if(!"card_recharge".equals(tradeList.getDocType())){
                    //还款入账
                    courseRepayMoney=courseRepayMoney.add(tradeList.getRepayMoney());
                }
                RepayAccountList repayAccountList = new RepayAccountList();
                repayAccountList.setRepayTradeId(tradeList.getId());
                List<RepayAccountList> select2 = ssmRepayAccountListService.select(repayAccountList);
                for (RepayAccountList accountList : select2) {
                    if(accountList.getAccountType()==1){
                        //还款支付
                        courseTurnRechargeMoney=courseTurnRechargeMoney.add(accountList.getPayMoney());
                    }
                }
            }
        }
//        NegativeKazin negativeKazin1 = NegativeKazin.getNegativeKazin();
//        negativeKazin1.setType("还款");
//        negativeKazin1.setEarnings(courseRepayMoney);
//        negativeKazin1.setExpenditure(courseTurnRechargeMoney);
//        negativeKazins.add(negativeKazin1);

        rechargeNegative.setCourseRepay(courseRepayMoney);
        rechargeNegative.setCourseRepayMoney(courseTurnRechargeMoney);

        //收银
        try {
            List<StoreDaily> storeDailies = ssmCashService.selectDayRecordCou(storeDailyRequest);
            if(null != storeDailies){
                for (StoreDaily daily : storeDailies) {
                    cashMoney=cashMoney.add(daily.getRechargeMoney());
                }
//
//                NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//                negativeKazin2.setType("收银");
//                negativeKazin2.setExpenditure(cashMoney);
//                negativeKazins.add(negativeKazin2);

                rechargeNegative.setCashMoney(cashMoney);
            }else{
//                NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//                negativeKazin2.setType("收银");
//                negativeKazins.add(negativeKazin2);

                rechargeNegative.setCashMoney(BigDecimal.valueOf(0));
            }
        } catch (SmsException e) {
            e.printStackTrace();
        }

        //充值退@消费退
        RefundOrderList refundOrderList = new RefundOrderList();
        refundOrderList.setRefundDate(date);
        refundOrderList.setStoreId(storeId);
        List<RefundOrderList> refundOrderLists = ssmRefundOrderService.selectFKJ(refundOrderList);
        for (RefundOrderList orderList : refundOrderLists) {
            if(orderList.getType()==1){
                RefundCourse refundRecharge = new RefundCourse();
                refundRecharge.setStoreId(orderList.getStoreId());
                refundRecharge.setRefundNum(orderList.getRefundNum());
                List<RefundCourse> select1 = ssmRefundCourseService.select(refundRecharge);
                BigDecimal reduce = select1.stream().map(RefundCourse::getCourseRefundMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                courseRefund=courseRefund.add(reduce);
            }else if(orderList.getType()==2){
                RefundCash refundCash = new RefundCash();
                refundCash.setStoreId(orderList.getStoreId());
                refundCash.setRefundNum(orderList.getRefundNum());
                List<RefundCash> select1 = ssmRefundCashService.select(refundCash);
                for (RefundCash cash : select1) {
                    CashList cashList = new CashList();
                    cashList.setId(cash.getCashId());
                    cashList.setCashDate(cash.getCashDate());
                    List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                    BigDecimal reduce = cashLists.stream().filter(cashList1 -> null != cashList1.getCourseAccountId()).map(CashList::getTransactionPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                    cashRefundMoney=cashRefundMoney.add(reduce);
                }
            }
        }

//        NegativeKazin negativeKazin3 = NegativeKazin.getNegativeKazin();
//        negativeKazin3.setType("疗程退");
//        negativeKazin3.setExpenditure(courseRefund);
//        negativeKazins.add(negativeKazin3);

        rechargeNegative.setCourseRefundMoney(courseRefund);
//
//        NegativeKazin negativeKazin4 = NegativeKazin.getNegativeKazin();
//        negativeKazin4.setType("消费退");
//        negativeKazin4.setEarnings(cashRefundMoney);
//        negativeKazins.add(negativeKazin4);

        rechargeNegative.setCashRefund(cashRefundMoney);


        //查询促销
        DocPromotionRequest docPromotion = new DocPromotionRequest();
        docPromotion.setDocDate(date);
        docPromotion.setStoreId(storeId);
        List<DocPromotion> docPromotionResponses = ssmDocPromotionService.selectFKJ(docPromotion);
        for (DocPromotion docPromotionRespons : docPromotionResponses) {
            courseMoney=courseMoney.add(docPromotionRespons.getCollectionRechargeAmount().add(docPromotionRespons.getCollectionCashAmount()));
        }
//
//        NegativeKazin negativeKazin5 = NegativeKazin.getNegativeKazin();
//        negativeKazin5.setType("疗程促销");
//        negativeKazin5.setEarnings(courseMoney);
//        negativeKazins.add(negativeKazin5);
        rechargeNegative.setCoursePromotion(courseMoney);



        //疗程转充值
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setDocDate(date);
        rechargeTurnCourse.setStoreId(storeId);
        List<RechargeTurnCourse> rechargeTurnCourses = ssmRechargeTurnCourseService.selectFKJ(rechargeTurnCourse);
        for (RechargeTurnCourse rechargeTurnCours : rechargeTurnCourses) {
            courseTurnRechargeMoney=courseTurnRechargeMoney.add(rechargeTurnCours.getRechargeInMoney());
        }


//        NegativeKazin negativeKazin6 = NegativeKazin.getNegativeKazin();
//        negativeKazin6.setType("疗程转充值");
//        negativeKazin6.setExpenditure(courseTurnRechargeMoney);
//        negativeKazins.add(negativeKazin6);

        rechargeNegative.setCourseTurnCharge(courseTurnRechargeMoney);


        //初始化
        MemberInit memberInit = new MemberInit();
        memberInit.setStoreId(storeId);
        memberInit.setDocDate(date);
        List<MemberInit> memberInits = ssmMemberInitService.selectFKJ(memberInit);
        BigDecimal reduce = memberInits.stream().filter(memberInit1 -> memberInit1.getCardType() == 1).map(MemberInit::getRestMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        courseMoney=courseMoney.add(reduce);



//        NegativeKazin negativeKazin7 = NegativeKazin.getNegativeKazin();
//        negativeKazin7.setType("初始化");
//        negativeKazin7.setEarnings(reduce);
//        negativeKazins.add(negativeKazin7);

        rechargeNegative.setInit(reduce);


        //疗程转门店
        //转出
        StoreTurnTreatment storeTurnTreatment = new StoreTurnTreatment();
        storeTurnTreatment.setStoreId(storeId);
        storeTurnTreatment.setProposerDate(date);
        List<StoreTurnTreatment> storeTurnTreatments = ssmStoreTurnTreatmentService.selectFKJ(storeTurnTreatment);
        BigDecimal reduce1 = storeTurnTreatments.stream().map(StoreTurnTreatment::getCourseRestPrice).reduce(BigDecimal.ZERO, BigDecimal::add);


        //转入
        List<StoreTurnTreatment> storeTurnTreatments1 = ssmStoreTurnTreatmentService.selectFKJOut(storeTurnTreatment);
        BigDecimal reduce2 = storeTurnTreatments1.stream().map(StoreTurnTreatment::getCourseRestPrice).reduce(BigDecimal.ZERO, BigDecimal::add);

//
//        NegativeKazin negativeKazin8 = NegativeKazin.getNegativeKazin();
//        negativeKazin8.setType("疗程转门店");
//        negativeKazin8.setEarnings(reduce2);
//        negativeKazin8.setExpenditure(reduce1);
//        negativeKazins.add(negativeKazin8);


        rechargeNegative.setCourseTurnStore(reduce2);
        rechargeNegative.setCourseTurnStoreMoney(reduce1);

        //加价转疗程的现付
        CardChangeList cardChangeList = new CardChangeList();
        cardChangeList.setStoreId(storeId);
        cardChangeList.setDocDate(date);
        List<CardChangeList> list = ssmCardChangeService.selectFKJ(cardChangeList);
        BigDecimal reduce3 = list.stream().map(CardChangeList::getChangeInPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);

//
//        NegativeKazin negativeKazin9 = NegativeKazin.getNegativeKazin();
//        negativeKazin9.setType("加价转疗程");
//        negativeKazin9.setEarnings(reduce3);
//        negativeKazins.add(negativeKazin9);
        rechargeNegative.setCardChange(reduce3);

        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);
        rechargeNegative.setStoreName(baseStore.getStoreName());

//
//
//        for (NegativeKazin negativeKazin : negativeKazins) {
//            negativeKazin.setBalance(negativeKazin.getBeginning().add(negativeKazin.getEarnings()).subtract(negativeKazin.getExpenditure()));
//        }
        return rechargeNegative;
    }

    @Override
    public CourseNegative CourseDayStatisticByDate(Long storeId, Date startTime,Date endTime) {
//        String dateStr = DateUtils.ymdFormat(date);

//        List<NegativeKazin> negativeKazins = new ArrayList<>();

        CourseNegative rechargeNegative = new CourseNegative();
        rechargeNegative.setOpenning(BigDecimal.ZERO);
        rechargeNegative.setOpenning(BigDecimal.ZERO);
        rechargeNegative.setBalance(BigDecimal.ZERO);
        BigDecimal courseMoney = BigDecimalUtils.bigDecimal("0");//疗程金额
        BigDecimal courseRepayMoney = BigDecimalUtils.bigDecimal("0"); //疗程还款金额
        BigDecimal courseTurnRechargeMoney = BigDecimalUtils.bigDecimal("0"); //疗程还款疗程支付金额
        BigDecimal cashMoney = BigDecimalUtils.bigDecimal("0"); //收银支付金额
        BigDecimal cashRefundMoney = BigDecimalUtils.bigDecimal("0"); //收银支付退金额
        BigDecimal courseRefund = BigDecimalUtils.bigDecimal("0"); //疗程退金额
        BigDecimal coursePromotionRepay = BigDecimal.ZERO;
        BigDecimal coursePayRepayMoney = BigDecimal.ZERO;//疗程卡付还款


        NegativeDayStatistic negativeDayStatistic = new NegativeDayStatistic();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endTime);//设置起时间
        calendar.add(Calendar.MONTH, -1);//减一个月
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DATE));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormata = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTimeStr = simpleDateFormat.format(calendar.getTime());
        endTimeStr = endTimeStr + " 23:59:59";
        Date time = null;
        try {
            time = simpleDateFormata.parse(endTimeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        negativeDayStatistic.setStartTime(time);

        List<Long> storeIds =  new ArrayList<>();
        storeIds.add(storeId);
        negativeDayStatistic.setStoreIdl(storeIds);
        List<CourseMoney> courseMoneyList = courseMoneyService.fixedNegativeCardByCourse(negativeDayStatistic);
        if(courseMoneyList!=null && courseMoneyList.size()>0) {
            CourseMoney courseMoneyKajin = courseMoneyList.get(0);
            rechargeNegative.setOpenning(courseMoneyKajin.getBalance());
        }
        else{
            calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DATE));
            Date kaiShi = calendar.getTime();
            String kaiShiTime = simpleDateFormat.format(kaiShi);
            kaiShiTime = kaiShiTime + " 00:00:00";
            String chushihua = "2023-04-01 00:00:00";
            Date parse;
            Date chushiTime;
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                parse = simpleDateFormat.parse(kaiShiTime);
                chushiTime = simpleDateFormat.parse(chushihua);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            if(parse.after(chushiTime)) {
                CourseNegative courseNegative = CourseDayStatisticByDate(storeId, parse, time);
                rechargeNegative.setOpenning(courseNegative.getBalance());
            }
        }
        StoreDailyRequest storeDailyRequest = new StoreDailyRequest();
        storeDailyRequest.setStoreId(storeId);
//        storeDailyRequest.setDateStr(dateStr);
        storeDailyRequest.setIsCancel(0);
        storeDailyRequest.setDate(startTime);
        storeDailyRequest.setStartTime(startTime);
        storeDailyRequest.setEndTime(endTime);

        //疗程开卡
        StoreDaily storeDaily1 = ssmCourseService.NegativeKazinByDate(storeDailyRequest);
//        courseMoney=courseMoney.add(storeDaily1.getCourseMoney());
        if(null == storeDaily1){
//            NegativeKazin negativeKazin = NegativeKazin.getNegativeKazin();
//            negativeKazin.setType("疗程开卡");
//            negativeKazins.add(negativeKazin);

            rechargeNegative.setCourseMoney(BigDecimal.valueOf(0));
        }else{
//            NegativeKazin negativeKazin = NegativeKazin.getNegativeKazin();
//            negativeKazin.setType("疗程开卡");
//            negativeKazin.setEarnings(storeDaily1.getCourseMoney());
//            negativeKazins.add(negativeKazin);
            rechargeNegative.setCourseMoney(storeDaily1.getCourseMoney());
        }

        //还款
        RepayTradeRequest repayTradeList = new RepayTradeRequest();
        repayTradeList.setRepayDate(startTime);
        repayTradeList.setStartTime(startTime);
        repayTradeList.setEndTime(endTime);
        repayTradeList.setStoreId(storeId);
        List<RepayTradeList> select = ssmRepayTradeService.selectFKJByDate(repayTradeList);
        for (RepayTradeList tradeList : select) {
            RepayList repayList = new RepayList();
            repayList.setRepayNum(tradeList.getRepayNum());
            repayList.setStoreId(tradeList.getStoreId());
            repayList.setIsCancel(0);
            List<RepayList> select1 = ssmRepayService.select(repayList);
            if(select1.size()>0){
                //存在正常单据
                if(!"card_recharge".equals(tradeList.getDocType()) && !Constant.promotion_card_use.equals(tradeList.getDocType())){
                    //还款入账
                    courseRepayMoney=courseRepayMoney.add(
//                            tradeList.getRepayMoney().add(
                            tradeList.getPayMoney()
//                    )
                    );
                } else if (Constant.promotion_card_use.equals(tradeList.getDocType())) {
                    coursePromotionRepay = coursePromotionRepay.add(tradeList.getPayMoney());
                }
                RepayAccountList repayAccountList = new RepayAccountList();
                repayAccountList.setRepayTradeId(tradeList.getId());
                List<RepayAccountList> select2 = ssmRepayAccountListService.select(repayAccountList);
                for (RepayAccountList accountList : select2) {
                    if(accountList.getAccountType()==1){
                        //还款支付
                        coursePayRepayMoney=coursePayRepayMoney.add(accountList.getPayMoney());
                    }
                }
            }
        }
//        NegativeKazin negativeKazin1 = NegativeKazin.getNegativeKazin();
//        negativeKazin1.setType("还款");
//        negativeKazin1.setEarnings(courseRepayMoney);
//        negativeKazin1.setExpenditure(courseTurnRechargeMoney);
//        negativeKazins.add(negativeKazin1);

        rechargeNegative.setCourseRepay(courseRepayMoney);
        rechargeNegative.setCoursePromotionRepay(coursePromotionRepay);
        rechargeNegative.setCourseRepayMoney(coursePayRepayMoney);

        //收银
        try {
            List<StoreDaily> storeDailies = ssmCashService.selectDayRecordCouByDate(storeDailyRequest);
            if(null != storeDailies){
                for (StoreDaily daily : storeDailies) {
                    cashMoney=cashMoney.add(daily.getRechargeMoney());
                }
//
//                NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//                negativeKazin2.setType("收银");
//                negativeKazin2.setExpenditure(cashMoney);
//                negativeKazins.add(negativeKazin2);

                rechargeNegative.setCashMoney(cashMoney);
            }else{
//                NegativeKazin negativeKazin2 = NegativeKazin.getNegativeKazin();
//                negativeKazin2.setType("收银");
//                negativeKazins.add(negativeKazin2);

                rechargeNegative.setCashMoney(BigDecimal.valueOf(0));
            }
        } catch (SmsException e) {
            e.printStackTrace();
        }

        //充值退@消费退
        RefundOrderList refundOrderList = new RefundOrderList();
        refundOrderList.setRefundDate(startTime);
        refundOrderList.setStartTime(startTime);
        refundOrderList.setEndTime(endTime);
        refundOrderList.setStoreId(storeId);
        List<RefundOrderList> refundOrderLists = ssmRefundOrderService.selectFKJByDate(refundOrderList);
        for (RefundOrderList orderList : refundOrderLists) {
            if(orderList.getType()==1){
                RefundCourse refundRecharge = new RefundCourse();
                refundRecharge.setStoreId(orderList.getStoreId());
                refundRecharge.setRefundNum(orderList.getRefundNum());
                List<RefundCourse> select1 = ssmRefundCourseService.select(refundRecharge);
                BigDecimal reduce = select1.stream().map(RefundCourse::getCourseRefundMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                courseRefund=courseRefund.add(reduce);
            }else if(orderList.getType()==2){
                RefundCash refundCash = new RefundCash();
                refundCash.setStoreId(orderList.getStoreId());
                refundCash.setRefundNum(orderList.getRefundNum());
                List<RefundCash> select1 = ssmRefundCashService.select(refundCash);
                for (RefundCash cash : select1) {
                    CashList cashList = new CashList();
                    cashList.setId(cash.getCashId());
                    cashList.setCashDate(cash.getCashDate());
                    List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                    BigDecimal reduce = cashLists.stream().filter(cashList1 -> null != cashList1.getCourseAccountId()).map(CashList::getTransactionPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                    cashRefundMoney=cashRefundMoney.add(reduce);
                }
            }
        }

//        NegativeKazin negativeKazin3 = NegativeKazin.getNegativeKazin();
//        negativeKazin3.setType("疗程退");
//        negativeKazin3.setExpenditure(courseRefund);
//        negativeKazins.add(negativeKazin3);

        rechargeNegative.setCourseRefundMoney(courseRefund);
//
//        NegativeKazin negativeKazin4 = NegativeKazin.getNegativeKazin();
//        negativeKazin4.setType("消费退");
//        negativeKazin4.setEarnings(cashRefundMoney);
//        negativeKazins.add(negativeKazin4);

        rechargeNegative.setCashRefund(cashRefundMoney);


        //查询促销
        DocPromotionRequest docPromotion = new DocPromotionRequest();
        docPromotion.setDocDate(startTime);
        docPromotion.setStartTime(startTime);
        docPromotion.setEndTime(endTime);
        docPromotion.setStoreId(storeId);
        List<DocPromotion> docPromotionResponses = ssmDocPromotionService.selectFKJByDate(docPromotion);
        for (DocPromotion docPromotionRespons : docPromotionResponses) {
            courseMoney=courseMoney.add(docPromotionRespons.getCollectionRechargeAmount().add(docPromotionRespons.getCollectionCashAmount()));
        }
//
//        NegativeKazin negativeKazin5 = NegativeKazin.getNegativeKazin();
//        negativeKazin5.setType("疗程促销");
//        negativeKazin5.setEarnings(courseMoney);
//        negativeKazins.add(negativeKazin5);
        rechargeNegative.setCoursePromotion(courseMoney);



        //疗程转充值
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setDocDate(startTime);
        rechargeTurnCourse.setStartTime(startTime);
        rechargeTurnCourse.setEndTime(endTime);
        rechargeTurnCourse.setStoreId(storeId);
        List<RechargeTurnCourse> rechargeTurnCourses = ssmRechargeTurnCourseService.selectFKJByDate(rechargeTurnCourse);

        for (RechargeTurnCourse rechargeTurnCours : rechargeTurnCourses) {
            courseTurnRechargeMoney=courseTurnRechargeMoney.add(rechargeTurnCours.getRechargeInMoney());
        }


//        NegativeKazin negativeKazin6 = NegativeKazin.getNegativeKazin();
//        negativeKazin6.setType("疗程转充值");
//        negativeKazin6.setExpenditure(courseTurnRechargeMoney);
//        negativeKazins.add(negativeKazin6);

        rechargeNegative.setCourseTurnCharge(courseTurnRechargeMoney);


        //初始化
        MemberInit memberInit = new MemberInit();
        memberInit.setStoreId(storeId);
        memberInit.setDocDate(startTime);
        memberInit.setStartTime(startTime);
        memberInit.setEndTime(endTime);
        List<MemberInit> memberInits = ssmMemberInitService.selectFKJByDate(memberInit);
        BigDecimal reduce = memberInits.stream().filter(memberInit1 -> memberInit1.getCardType() == 1).map(MemberInit::getCourseRestPrice).reduce(BigDecimal.ZERO, BigDecimal::add);

        StoreDaily storeDaily = courseListMemberInitMapper.NegativeKazinByDate(storeDailyRequest);
        if(storeDaily!=null && storeDaily.getCourseMoney()!=null && storeDaily.getCourseMoney().compareTo(BigDecimal.ZERO)==1){
            reduce = reduce.add(storeDaily.getCourseMoney());
        }

        //查询疗程初始化
        courseMoney=courseMoney.add(reduce);

//        NegativeKazin negativeKazin7 = NegativeKazin.getNegativeKazin();
//        negativeKazin7.setType("初始化");
//        negativeKazin7.setEarnings(reduce);
//        negativeKazins.add(negativeKazin7);

        rechargeNegative.setInit(reduce);


        //疗程转门店
        //转出
        StoreTurnTreatment storeTurnTreatment = new StoreTurnTreatment();
        storeTurnTreatment.setStoreId(storeId);
        storeTurnTreatment.setProposerDate(startTime);
        storeTurnTreatment.setStartTime(startTime);
        storeTurnTreatment.setEndTime(endTime);
        List<StoreTurnTreatment> storeTurnTreatments = ssmStoreTurnTreatmentService.selectFKJByDate(storeTurnTreatment);
        BigDecimal reduce1 = storeTurnTreatments.stream().map(StoreTurnTreatment::getCourseRestPrice).reduce(BigDecimal.ZERO, BigDecimal::add);


        //转入
        List<StoreTurnTreatment> storeTurnTreatments1 = ssmStoreTurnTreatmentService.selectFKJOutByDate(storeTurnTreatment);
        BigDecimal reduce2 = storeTurnTreatments1.stream().map(StoreTurnTreatment::getCourseRestPrice).reduce(BigDecimal.ZERO, BigDecimal::add);

//
//        NegativeKazin negativeKazin8 = NegativeKazin.getNegativeKazin();
//        negativeKazin8.setType("疗程转门店");
//        negativeKazin8.setEarnings(reduce2);
//        negativeKazin8.setExpenditure(reduce1);
//        negativeKazins.add(negativeKazin8);


        rechargeNegative.setCourseTurnStore(reduce2);
        rechargeNegative.setCourseTurnStoreMoney(reduce1);

        //加价转疗程的现付
        CardChangeList cardChangeList = new CardChangeList();
        cardChangeList.setStoreId(storeId);
        cardChangeList.setDocDate(startTime);
        cardChangeList.setStartTime(startTime);
        cardChangeList.setEndTime(endTime);
        List<CardChangeList> list = ssmCardChangeService.selectFKJByDate(cardChangeList);
        BigDecimal reduce3 = list.stream().map(CardChangeList::getChangeInPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);

//
//        NegativeKazin negativeKazin9 = NegativeKazin.getNegativeKazin();
//        negativeKazin9.setType("加价转疗程");
//        negativeKazin9.setEarnings(reduce3);
//        negativeKazins.add(negativeKazin9);
        rechargeNegative.setCardChange(reduce3);

        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);
        rechargeNegative.setStoreName(baseStore.getStoreNum() +"-"+ baseStore.getStoreName());
        rechargeNegative.setStoreId(storeId);

//
//
//        for (NegativeKazin negativeKazin : negativeKazins) {
//            negativeKazin.setBalance(negativeKazin.getBeginning().add(negativeKazin.getEarnings()).subtract(negativeKazin.getExpenditure()));
//        }
        BigDecimal shouru = rechargeNegative.getCourseMoney()
                .add(rechargeNegative.getCourseRepay())
//                .add(rechargeNegative.getCourseRefundMoney())
                .add(rechargeNegative.getCoursePromotion())
                .add(rechargeNegative.getInit())
                .add(rechargeNegative.getCourseTurnStore())
                .add(rechargeNegative.getCardChange())
                .add(rechargeNegative.getCoursePromotionRepay())
                .add(rechargeNegative.getCashRefund());
        BigDecimal zhichu = rechargeNegative.getCourseRepayMoney().add(rechargeNegative.getCashMoney()).add(rechargeNegative.getCourseRefundMoney()).add(rechargeNegative.getCourseTurnCharge())
                .add(rechargeNegative.getCourseTurnStoreMoney());



        rechargeNegative.setBalance(rechargeNegative.getOpenning().add(shouru).subtract(zhichu));
        return rechargeNegative;
    }



//    @Override
//    public List<NegativeDayStatistic> selectNowList(Long storeId) {
//        Date date = new Date();
//
//        return null;
//    }


    @Override
    public int saveNegativeRecord(Date date) {
        int i = 0;
        //查询门店信息
        BaseStore store = new BaseStore();
        store.setState(1);
        List<BaseStore> storeList = ssmBaseStoreService.select(store);
//        Date date = DateUtils.diffDay(new Date(), -1);
        for (BaseStore baseStore : storeList) {
            //根据门店 查询负卡金记录表
            NegativeCardDayRecord negativeCardDayRecord = new NegativeCardDayRecord();
            negativeCardDayRecord.setStoreId(baseStore.getId());
            negativeCardDayRecord.setStoreName(baseStore.getStoreName());
            int count = ssmNegativeCardService.selectCount(negativeCardDayRecord);
            //查询当前门店当天账户充值交易记录
            AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
            accountTradeRecordRequest.setDate(DateUtils.ymdFormat(date));
            accountTradeRecordRequest.setStoreId(baseStore.getId());
            accountTradeRecordRequest.setInputDate(date);
            List<String> docTypes = new ArrayList<>();
            docTypes.add(Constant.doc_type_recharge);
            docTypes.add(Constant.doc_type_repay);
            docTypes.add(Constant.doc_type_card_use);



            accountTradeRecordRequest.setDocTypes(docTypes);
            List<AccountTradeRecord> accountTradeRecords = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
            //无值时 第一次添加负卡金记录
            if (count == 0){
                negativeCardDayRecord.setDate(date);
                //期初置0
                BigDecimal startMoney = com.ssm.core.utils.BigDecimalUtils.bigDecimal("0");
                negativeCardDayRecord.setStartMoney(startMoney);
                //充值负卡金记录添加
                negativeCardDayRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                negativeCardDayRecord.setType(1);
                //结存计算
                BigDecimal recEndMoney = this.getMoney(accountTradeRecords, startMoney, negativeCardDayRecord.getType());
                negativeCardDayRecord.setEndMoney(recEndMoney);
                i = ssmNegativeCardService.saveNotNull(negativeCardDayRecord);

                //疗程负卡金记录添加
                negativeCardDayRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                negativeCardDayRecord.setType(2);
                //结存计算
                BigDecimal courseEndMoney = this.getMoney(accountTradeRecords, startMoney, negativeCardDayRecord.getType());
                negativeCardDayRecord.setEndMoney(courseEndMoney);
                i = ssmNegativeCardService.saveNotNull(negativeCardDayRecord);
            }else {
                //查询上一天的数据 获取结存金额 即为今天的期初金额
                try {
                    negativeCardDayRecord.setDate(DateUtils.ymdString2Date(DateUtils.ymdFormat(DateUtils.diffDay(date, -1))));
                } catch (ParseException e) {
                    log.error("SsmNegativeCardServiceImpl:","时间转换异常");
                }
                List<NegativeCardDayRecord> negativeCardDayRecords = ssmNegativeCardService.select(negativeCardDayRecord);
                BigDecimal startMoney = com.ssm.core.utils.BigDecimalUtils.bigDecimal("0");
                BigDecimal endMoney = com.ssm.core.utils.BigDecimalUtils.bigDecimal("0");
                for (NegativeCardDayRecord record : negativeCardDayRecords) {

                    startMoney = record.getEndMoney();
                    //计算当天结存金额
                    endMoney = this.getMoney(accountTradeRecords, startMoney, record.getType());
                    NegativeCardDayRecord newRecord = new NegativeCardDayRecord();
                    BeanUtils.copyProperties(record, newRecord);
                    newRecord.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                    newRecord.setDate(date);
                    newRecord.setStartMoney(startMoney);
                    newRecord.setEndMoney(endMoney);
                    i = ssmNegativeCardService.saveNotNull(newRecord);

                }
            }

        }
        return i;
    }

    @Override
    public void saveNegativeTask() {
        System.out.println("执行定时任务");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        BaseStore baseStore = new BaseStore();
        baseStore.setState(1);
        List<BaseStore> select = ssmBaseStoreService.select(baseStore);
        for (BaseStore store : select) {
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);//设置起时间
            calendar.add(Calendar.MONTH, -1);//减一个月
            Date time = calendar.getTime();

            BaseNegativeKazin baseNegativeKazin = new BaseNegativeKazin();
            baseNegativeKazin.setDate(time);
            baseNegativeKazin.setStoreId(store.getId());
            baseNegativeKazin.setStoreNum(store.getStoreNum());
            baseNegativeKazin.setStoreName(store.getStoreName());
            baseNegativeKazin.setRechargeInitial(BigDecimal.valueOf(0));
            baseNegativeKazin.setRechargeIncome(BigDecimal.valueOf(0));
            baseNegativeKazin.setRechargeRepayment(BigDecimal.valueOf(0));
            baseNegativeKazin.setRechargeService(BigDecimal.valueOf(0));
            baseNegativeKazin.setRechargeRefund(BigDecimal.valueOf(0));
            baseNegativeKazin.setRechargeCourse(BigDecimal.valueOf(0));
            baseNegativeKazin.setRechargeBalance(BigDecimal.valueOf(0));
            baseNegativeKazin.setCourseInitial(BigDecimal.valueOf(0));
            baseNegativeKazin.setCourseIncome(BigDecimal.valueOf(0));
            baseNegativeKazin.setCourseRepayment(BigDecimal.valueOf(0));
            baseNegativeKazin.setCourseService(BigDecimal.valueOf(0));
            baseNegativeKazin.setCourseRefund(BigDecimal.valueOf(0));
            baseNegativeKazin.setCourseBalance(BigDecimal.valueOf(0));
            while (time.before(date)){
                List<NegativeDayStatistic> negativeDayStatistics = this.selectDayList(store.getId(), time);
                for (NegativeDayStatistic negativeDayStatistic : negativeDayStatistics) {
                    if(negativeDayStatistic.getType()==1){ //充值
                        baseNegativeKazin.setRechargeInitial(baseNegativeKazin.getRechargeInitial().add(negativeDayStatistic.getStartMoney()));
                        baseNegativeKazin.setRechargeIncome(baseNegativeKazin.getRechargeIncome().add(negativeDayStatistic.getRechargeMoney()));
                        baseNegativeKazin.setRechargeRepayment(baseNegativeKazin.getRechargeRepayment().add(negativeDayStatistic.getRecRepayMoney()));
                        baseNegativeKazin.setRechargeService(baseNegativeKazin.getRechargeService().add(negativeDayStatistic.getRecStoreCashMoney()));
                        baseNegativeKazin.setRechargeRefund(baseNegativeKazin.getRechargeRefund().add(negativeDayStatistic.getRecRefundMoney()));
                        baseNegativeKazin.setRechargeCourse(baseNegativeKazin.getRechargeCourse().add(negativeDayStatistic.getCardToCourseMoney()));
                        baseNegativeKazin.setRechargeBalance(baseNegativeKazin.getRechargeBalance().add(negativeDayStatistic.getEndMoney()));
                    }else{
                        baseNegativeKazin.setCourseInitial(baseNegativeKazin.getCourseInitial().add(negativeDayStatistic.getStartMoney()));
                        baseNegativeKazin.setCourseIncome(baseNegativeKazin.getCourseIncome().add(negativeDayStatistic.getCourseMoney()));
                        baseNegativeKazin.setCourseRepayment(baseNegativeKazin.getCourseRepayment().add(negativeDayStatistic.getCourseRepayMoney()));
                        baseNegativeKazin.setCourseService(baseNegativeKazin.getCourseService().add(negativeDayStatistic.getCourseStoreCashMoney()));
                        baseNegativeKazin.setCourseRefund(baseNegativeKazin.getCourseRefund().add(negativeDayStatistic.getCourseRefundMoney()));
                        baseNegativeKazin.setCourseBalance(baseNegativeKazin.getCourseBalance().add(negativeDayStatistic.getEndMoney()));
                    }
                }
                calendar.setTime(time);//设置起时间
                calendar.add(Calendar.DATE, 1);//减一个月
                time = calendar.getTime();
            }
            int save = ssmBaseNegativeKazinService.save(baseNegativeKazin);
        }
    }

    @Override
    public List<RechargeMoney> fixedNegativeCardByRecharge(NegativeDayStatistic negativeDayStatistic) {
        return rechargeMoneyService.fixedNegativeCardByRecharge(negativeDayStatistic);
    }

    @Override
    public List<CourseMoney> fixedNegativeCardByCourse(NegativeDayStatistic negativeDayStatistic) {
        return courseMoneyService.fixedNegativeCardByCourse(negativeDayStatistic);
    }

    public BigDecimal getMoney(List<AccountTradeRecord> list, BigDecimal startMonty, int type){
        //结存计算
        BigDecimal endMoney = startMonty;
        for (AccountTradeRecord tradeRecord : list) {
            if (type == 1) {//充值
                //充值单和充值还款单 叠加金额
                if (tradeRecord.getAccountType().equals(Constant.account_type_recharge) || tradeRecord.getAccountType().equals(Constant.account_type_rec_repay)) {
                    endMoney = com.ssm.core.utils.BigDecimalUtils.add(endMoney, tradeRecord.getAccountInMoney());
                } else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_cancel) || tradeRecord.getAccountType().equals(Constant.account_type_rec_repay_cancel)) {
                    //充值单和充值还款单作废 递减金额
                    endMoney = com.ssm.core.utils.BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountInMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume)) {
                    //充值卡购买疗程 充值递减金额
                    endMoney = com.ssm.core.utils.BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountSpendMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume_cancel)) {
                    //充值卡购买疗程作废 充值叠加金额
                    endMoney = com.ssm.core.utils.BigDecimalUtils.add(endMoney, tradeRecord.getAccountInMoney());
                }
            }else {//疗程
                if (tradeRecord.getAccountType().equals(Constant.account_type_project) || tradeRecord.getAccountType().equals(Constant.account_type_course_repay)) {
                    //accountInCount/tradeCount * tradeMoney  比例*项目金额 即单据入账(防止还款金额不到一次 使用accountInMoney字段)
                    endMoney = com.ssm.core.utils.BigDecimalUtils.add(endMoney, tradeRecord.getAccountInMoney());
                } else if (tradeRecord.getAccountType().equals(Constant.account_type_project_cancel) || tradeRecord.getAccountType().equals(Constant.account_type_course_repay_cancel)) {
                    //充值单和充值还款单作废 递减金额
                    endMoney = com.ssm.core.utils.BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountInMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume)) {
                    //充值卡购买疗程 疗程叠加金额
                    endMoney = com.ssm.core.utils.BigDecimalUtils.add(endMoney, tradeRecord.getAccountSpendMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume_cancel)) {
                    //充值卡购买疗程作废 疗程递减金额
                    endMoney = com.ssm.core.utils.BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountInMoney());
                }
            }

        }
        return endMoney;
    }
}
