package cn.superdesk.uniorder.service.accounting.score.customer.impl;

import cn.hutool.core.io.FileUtil;
import cn.superdesk.uniorder.common.enums.thirdpart.jd.SysQuartzScheduleTaskStatementOfAccountRunStateEnum;
import cn.superdesk.uniorder.common.excel.ExcelResponse;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto._constant.ResourceConstant;
import cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerDetailVo;
import cn.superdesk.uniorder.dto.vo.accounting.score.customer.AccountingOrderScoreCustomerListItemVo;
import cn.superdesk.uniorder.notify.NotifyUtils;
import cn.superdesk.uniorder.service.accounting.score.customer.AccountingScoreCustomerExportExcelService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @Description: 侨城汇 客户结算 Export Excel
 * @Author: linjinyuØ
 * @CreateDate: 2021/4/15 下午4:05
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/4/15 下午4:05
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingScoreCustomerExportExcelServiceImpl implements AccountingScoreCustomerExportExcelService {

    @Autowired
    private ResourceConstant resourceConstant;

//	exportExcelAccountingCustomerNotYetDimCredit

    private static final String TEMPLATE_EXCEL_EXPORT_ACCOUNTING_ORDER_SCORE_CUSTOMER_LIST = "exportExcelAccountingOrderScoreCustomerList.xlsx";

    public SysNotify exportExcelAccountingOrderScoreCustomerList(List<AccountingOrderScoreCustomerListItemVo> exportData) {

        /******************************************  start ******************************************/

        String excelExportPathRoot             = resourceConstant.getPathRoot();
        String excelExportRelativePathTemplate = resourceConstant.getPathTemplate();
        String excelExportRelativePathStorage  = resourceConstant.getPathStorage();

        String relativePath = File.separator + "excel" + File.separator + "accounting" + File.separator + "score" + File.separator + "customer" + File.separator;

        String templateRelativePath = excelExportPathRoot + excelExportRelativePathTemplate + relativePath;
        String templateFullPath     = templateRelativePath + TEMPLATE_EXCEL_EXPORT_ACCOUNTING_ORDER_SCORE_CUSTOMER_LIST;

        String targetFullPath          = excelExportPathRoot + excelExportRelativePathStorage;
        String targetFileName          = "积分结算_" + "对账单-概要_" + "导出" + ExcelResponse.addTail() + ".xlsx";
        String targetWriteFileFullPath = targetFullPath + relativePath + targetFileName;
        FileUtil.touch(targetWriteFileFullPath);

        /******************************************  start ******************************************/


//		ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(targetWriteFileFullPath);
//		ExcelWriter        excelWriter        = excelWriterBuilder.withTemplate(templateFullPath).build();

        ExcelWriter excelWriter = EasyExcel.write(targetWriteFileFullPath).withTemplate(templateFullPath).build();

        WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "对账单-概要").build();


        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build();

        excelWriter.fill(new FillWrapper("detail", exportData), fillConfig, writeSheet0);


        // 别忘记关闭流
        excelWriter.finish();

        /******************************************  start ******************************************/


        return NotifyUtils.createSysNotify(
                targetFileName,
                relativePath + targetFileName,
                SysQuartzScheduleTaskStatementOfAccountRunStateEnum.成功
        );
    }


    private static final String TEMPLATE_EXCEL_EXPORT_STATEMENTOFACCOUNT_DETAIL_DIM_CREDIT             = "exportExcelStatementOfAccountDetailDimCredit.xlsx";
    private static final String TEMPLATE_EXCEL_EXPORT_STATEMENTOFACCOUNT_DETAIL_DIM_CREDIT_WITH_WECHAT = "exportExcelStatementOfAccountDetailDimCreditWithWechat.xlsx";

    /**
     * 侨城汇 积分结算
     * 积分结算 > 对账单 > 详情 > op导出账单
     *
     * @param exportData
     * @return
     * @link UnifiedOrderExportExcelServiceImpl.exportCustomerStatementOfAccountDetailCompatible
     */
    public SysNotify exportExcelAccountingOrderScoreCustomerDetailDimCredit(AccountingOrderScoreCustomerDetailVo exportData) {

        /****************************************** 初始 start ******************************************/

        String excelExportPathRoot             = resourceConstant.getPathRoot();
        String excelExportRelativePathTemplate = resourceConstant.getPathTemplate();
        String excelExportRelativePathStorage  = resourceConstant.getPathStorage();

        String relativePath = File.separator + "excel" + File.separator + "accounting" + File.separator + "score" + File.separator + "customer" + File.separator;

        String templateRelativePath = excelExportPathRoot + excelExportRelativePathTemplate + relativePath;
        String templateFullPath;

        String targetFullPath = excelExportPathRoot + excelExportRelativePathStorage;

        String targetFileName          = "积分结算" + "-" + "对账单" + "-" + "电子账单-op导出报表" + ExcelResponse.addTail() + ".xlsx";
        String targetWriteFileFullPath = targetFullPath + relativePath + targetFileName;

//		if (!FileUtil.exist(targetWriteFileFullPath)) {
//			FileUtil.mkdir(targetWriteFileFullPath);
//		}
        FileUtil.touch(targetWriteFileFullPath);

        /****************************************** 初始   end ******************************************/

        ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(targetWriteFileFullPath);
        ExcelWriter        excelWriter        = null;

        WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "面单").build();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "明细").build();
        FillConfig fillConfig  = FillConfig.builder().forceNewRow(Boolean.FALSE).build();

        templateFullPath = templateRelativePath + TEMPLATE_EXCEL_EXPORT_STATEMENTOFACCOUNT_DETAIL_DIM_CREDIT;

        excelWriter = excelWriterBuilder.withTemplate(templateFullPath).build();

        excelWriter.fill(new FillWrapper("detail", exportData.getCreditList()), fillConfig, writeSheet1);

        Map<String, Object> exportMapAccounting = AnBeanUtils.beanToMap(exportData.getAccounting(), "accounting");
        Map<String, Object> exportMapBankInfo   = AnBeanUtils.beanToMap(exportData.getBankInfo(), "bankInfo");

        excelWriter.fill(exportMapAccounting, writeSheet0).fill(exportMapBankInfo, writeSheet0);


        // 别忘记关闭流
        excelWriter.finish();

        return NotifyUtils.createSysNotify(targetFileName, relativePath + targetFileName, SysQuartzScheduleTaskStatementOfAccountRunStateEnum.成功);
    }


    /**
     * 侨城汇 积分结算
     * 积分结算 > 对账单 > 详情 > op导出账单（含微信支付）
     *
     * @param exportData
     * @return
     */
    public SysNotify exportExcelAccountingOrderScoreCustomerDetailDimCreditWithWechat(AccountingOrderScoreCustomerDetailVo exportData) {

        /****************************************** 初始 start ******************************************/

        String excelExportPathRoot             = resourceConstant.getPathRoot();
        String excelExportRelativePathTemplate = resourceConstant.getPathTemplate();
        String excelExportRelativePathStorage  = resourceConstant.getPathStorage();

        String relativePath = File.separator + "excel" + File.separator + "accounting" + File.separator + "score" + File.separator + "customer" + File.separator;

        String templateRelativePath = excelExportPathRoot + excelExportRelativePathTemplate + relativePath;
        String templateFullPath;

        String targetFullPath = excelExportPathRoot + excelExportRelativePathStorage;

        String targetFileName          = "积分结算" + "-" + "对账单" + "-" + "电子账单-op导出报表(含微信支付)" + ExcelResponse.addTail() + ".xlsx";
        String targetWriteFileFullPath = targetFullPath + relativePath + targetFileName;

//		if (!FileUtil.exist(targetWriteFileFullPath)) {
//			FileUtil.mkdir(targetWriteFileFullPath);
//		}
        FileUtil.touch(targetWriteFileFullPath);

        /****************************************** 初始   end ******************************************/

        ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(targetWriteFileFullPath);
        ExcelWriter        excelWriter        = null;

        WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "面单").build();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "明细").build();
        FillConfig fillConfig  = FillConfig.builder().forceNewRow(Boolean.FALSE).build();

        templateFullPath = templateRelativePath + TEMPLATE_EXCEL_EXPORT_STATEMENTOFACCOUNT_DETAIL_DIM_CREDIT_WITH_WECHAT;

        excelWriter = excelWriterBuilder.withTemplate(templateFullPath).build();

        excelWriter.fill(new FillWrapper("detail", exportData.getCreditList()), fillConfig, writeSheet1);

        Map<String, Object> exportMapAccounting = AnBeanUtils.beanToMap(exportData.getAccounting(), "accounting");
        Map<String, Object> exportMapBankInfo   = AnBeanUtils.beanToMap(exportData.getBankInfo(), "bankInfo");

        excelWriter.fill(exportMapAccounting, writeSheet0).fill(exportMapBankInfo, writeSheet0);


        // 别忘记关闭流
        excelWriter.finish();

        return NotifyUtils.createSysNotify(targetFileName, relativePath + targetFileName, SysQuartzScheduleTaskStatementOfAccountRunStateEnum.成功);
    }

}
