package com.ssm.task;

import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.entity.AccountTradeRecord;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.NegativeCardDayRecord;
import com.ssm.manage.query.AccountTradeRecordRequest;
import com.ssm.manage.service.SsmAccountTradeRecordService;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmNegativeCardService;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Component
@DisallowConcurrentExecution
@Slf4j
public class SsmNegativeCardTask extends QuartzJobBean {
    @Autowired
    private SsmNegativeCardService ssmNegativeCardService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //查询门店信息
//        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 = 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);
//                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);
//                ssmNegativeCardService.saveNotNull(negativeCardDayRecord);
//            }else {
//                //查询上一天的数据 获取结存金额 即为今天的期初金额
//                try {
//                    negativeCardDayRecord.setDate(DateUtils.ymdString2Date(DateUtils.ymdFormat(DateUtils.diffDay(date, -1))));
//                } catch (ParseException e) {
//                    log.error("SsmNegativeCardTask:","时间转换异常");
//                }
//                List<NegativeCardDayRecord> negativeCardDayRecords = ssmNegativeCardService.select(negativeCardDayRecord);
//                BigDecimal startMoney = BigDecimalUtils.bigDecimal("0");
//                BigDecimal endMoney = 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);
//                    ssmNegativeCardService.saveNotNull(newRecord);
//
//                }
//            }
//
//        }
//


    }

    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 = 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 = BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountInMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume)) {
                    //充值卡购买疗程 充值递减金额
                    endMoney = BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountSpendMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume_cancel)) {
                    //充值卡购买疗程作废 充值叠加金额
                    endMoney = 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 = 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 = BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountInMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume)) {
                    //充值卡购买疗程 疗程叠加金额
                    endMoney = BigDecimalUtils.add(endMoney, tradeRecord.getAccountSpendMoney());
                }else if (tradeRecord.getAccountType().equals(Constant.account_type_recharge_consume_cancel)) {
                    //充值卡购买疗程作废 疗程递减金额
                    endMoney = BigDecimalUtils.subtract(endMoney, tradeRecord.getAccountInMoney());
                }
            }

        }
        return endMoney;
    }
}
