package com.jeequan.jeepay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.db.entity.CheckBatch;
import com.jeequan.jeepay.db.entity.CheckDiff;
import com.jeequan.jeepay.service.mapper.CheckBatchMapper;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 对账批次表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2022-09-09
 */
@Service
public class CheckBatchService extends ServiceImpl<CheckBatchMapper, CheckBatch> {

    public CheckBatch saveOrUpdateCheckBatch(com.jeequan.jeepay.core.entity.CheckBatch channelCheckBatch) {

        CheckBatch checkBatch = covertCheckBatch(channelCheckBatch);
        boolean result = this.saveOrUpdate(checkBatch);
        if (!result) {
            throw new BizException("保存对账批次失败");
        }

        return checkBatch;
    }

    // 对账批次号   支付接口代码ifCode_渠道商户号_对账日期
    public String getBatchNo(String ifCode, String channelMchNo, Date billDate) {
        return ifCode + "_" + channelMchNo + "_" + DateUtil.format(billDate, DatePattern.PURE_DATE_PATTERN);
    }

    private CheckBatch covertCheckBatch(com.jeequan.jeepay.core.entity.CheckBatch checkBatch) {
        CheckBatch dbCheckBatch = new CheckBatch();
        BeanUtil.copyProperties(checkBatch, dbCheckBatch);
        return dbCheckBatch;
    }

    /** 根据已有的批次信息  初始化本地数据 */
    public CheckBatch initLocalCheckBatchData(CheckBatch checkBatch) {
        CheckBatch updateRecord = new CheckBatch();
        updateRecord.setBatchNo(checkBatch.getBatchNo());
        updateRecord.setIfCode(checkBatch.getIfCode());
        updateRecord.setChannelMchNo(checkBatch.getChannelMchNo());
        updateRecord.setBillDate(checkBatch.getBillDate());
        updateRecord.setReleaseState(checkBatch.getReleaseState());
        updateRecord.setTotalCount(0);
        updateRecord.setTotalRefundCount(0);
        updateRecord.setTotalAmount(0L);
        updateRecord.setTotalRefundAmount(0L);
        updateRecord.setTotalFee(0L);
        return updateRecord;
    }

    /** 更新批次统计数据 */
    @Transactional
    public void updateUnHandleCountAndState4CheckDiff(List<CheckDiff> checkSuccessList) {

        MutablePair<String, Integer> mutablePair = new MutablePair<>();

        List<MutablePair<String, Integer>> mutablePairList = checkSuccessList.stream().map(checkDiff -> {

            if (checkDiff.getBatchNo().equals(mutablePair.left)) {
                mutablePair.right += 1;
            }else {
                mutablePair.left = checkDiff.getBatchNo();
                mutablePair.right = 1;
            }

            return mutablePair;
        }).collect(Collectors.toList());

        List<CheckBatch> checkBatchList = mutablePairList.stream().map(pair -> {
            CheckBatch checkBatch = new CheckBatch();
            checkBatch.setBatchNo(pair.left);
            checkBatch.setUnHandleDiffCount(pair.right);
            return checkBatch;
        }).collect(Collectors.toList());

        this.updateUnHandleCountAndState4CheckBatch(checkBatchList);
    }

    @Transactional
    public void updateUnHandleCountAndState4CheckBatch(List<CheckBatch> checkBatchList) {
        for (CheckBatch checkBatch : checkBatchList) {
            baseMapper.updateUnHandleCountAndState(checkBatch);
        }
    }
}
