package com.jeequan.jeepay.thirdparty.channel;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.jeequan.jeepay.components.oss.ctrl.OssFileController;
import com.jeequan.jeepay.components.oss.model.MockMultipartFile;
import com.jeequan.jeepay.components.oss.model.OssFileConfig;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.CheckBatch;
import com.jeequan.jeepay.core.interfaces.paychannel.IBillDownloadService;
import com.jeequan.jeepay.service.impl.SysConfigService;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import com.jeequan.jeepay.thirdparty.util.ChannelCertConfigKitBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;

/*
* 渠道对账单下载 抽象类
*
* @author zx
*
* @date 2021/6/8 17:18
*/
public abstract class AbstractBillDownloadService implements IBillDownloadService {

    @Autowired protected SysConfigService sysConfigService;
    @Autowired protected ChannelCertConfigKitBean channelCertConfigKitBean;
    @Autowired protected ConfigContextQueryService configContextQueryService;
    @Autowired private OssFileController ossFileController;


    // 上传文件至oss
    public String upload2Oss(String ifCode, String channelMchNo, Date billDate, String suffix, InputStream inputStream){
        return ossFileController.singleFileUpload(new MockMultipartFile(genOssFileName(ifCode, channelMchNo, billDate, suffix), 1L, inputStream), OssFileConfig.BIZ_TYPE.CHANNEL_BILL);
    }

    // 生成上传oss  账单文件名称、后缀
    public String genOssFileName(String ifCode, String channelMchNo, Date billDate, String suffix){
        return String.format("%s_%s_%s_%s", ifCode, channelMchNo, DateUtil.format(billDate, DatePattern.PURE_DATE_PATTERN), suffix);
    }

    /**  生成渠道账单  解析成功  的对账批次 */
    public CheckBatch genReleaseSuccessCheckBatch(String ifCode, Date billDate, String channelMchNo, String path) {
        return initCheckBatch(ifCode, billDate, channelMchNo, path, CS.YES, null);
    }

    /**  生成渠道账单  解析失败  的对账批次 */
    public CheckBatch genReleaseFailCheckBatch(String ifCode, Date billDate, String channelMchNo, String releaseErrMsg) {
        return initCheckBatch(ifCode, billDate, channelMchNo, null, CS.NO, releaseErrMsg);
    }

    /** 初始化对账批次 */
    public CheckBatch initCheckBatch(String ifCode, Date billDate, String channelMchNo, String path, Byte releaseState, String releaseErrMsg) {
        CheckBatch checkBatch = new CheckBatch();
        checkBatch.setBatchNo(getBatchNo(ifCode, channelMchNo, billDate));
        checkBatch.setIfCode(ifCode);
        checkBatch.setBillDate(billDate);
        checkBatch.setChannelMchNo(channelMchNo);
        checkBatch.setState(CS.NO); //未处理状态
        checkBatch.setOrgBillFilePath(path);
        checkBatch.setReleaseState(releaseState);
        checkBatch.setReleaseErrMsg(releaseErrMsg);
        checkBatch.setChannelTotalAmount(BigDecimal.ZERO.longValue());
        return checkBatch;
    }

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

}
