package cn.superdesk.uniorder.service.thirdpart.szhtxx.electronicInvoice.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
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.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxDetailVo;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxListItemVo;
import cn.superdesk.uniorder.notify.NotifyUtils;
import cn.superdesk.uniorder.service.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExportExcelService;
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: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/3/11 下午12:42
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/3/11 下午12:42
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class SzhtxxElectronicInvoiceFptxxExportExcelServiceImpl implements SzhtxxElectronicInvoiceFptxxExportExcelService {


    @Autowired
    private ResourceConstant resourceConstant;

//	exportExcelAccountingCustomerNotYetDimCredit

    private static final String TEMPLATE_EXCEL_EXPORT_SZHTXX_ELECTRONIC_INVOICE_FPTXX_LIST = "exportExcelSzhtxxElectronicInvoiceFptxxList.xlsx";

    public SysNotify exportExcelSzhtxxElectronicInvoiceFptxxList(List<SzhtxxElectronicInvoiceFptxxListItemVo> exportData) {

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

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

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

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

        String targetFullPath          = excelExportPathRoot + excelExportRelativePathStorage;
        String targetFileName          = "积分结算_" + "金税盘发票_" + "op导出" + 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_SZHTXX_ELECTRONIC_INVOICE_FPTXX_DETAIL = "exportExcelSzhtxxElectronicInvoiceDetail.xlsx";

    public SysNotify exportExcelSzhtxxElectronicInvoiceFptxxDetail(SzhtxxElectronicInvoiceFptxxDetailVo exportData) {

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

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

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

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

        String targetFullPath = excelExportPathRoot + excelExportRelativePathStorage;
        String targetFileName = StrUtil.format("积分结算_" + "金税盘发票_" + "op导出发票数据_{}_{}_{}_{}" + ExcelResponse.addTail() + ".xlsx",
                exportData.getFptxx().getFpdm(),
                exportData.getFptxx().getFphm(),
                exportData.getFptxx().getFpqqlsh(),
                exportData.getFptxx().getAccountingOrderSn());
        String targetWriteFileFullPath = targetFullPath + relativePath + targetFileName;
        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("xmxx", exportData.getXmxx()), fillConfig, writeSheet1);

//		Map<String, Object> exportMapAccounting = AnBeanUtils.beanToMap(exportData.getAccounting(), "accounting");
        Map<String, Object> exportMapFptxx = AnBeanUtils.beanToMap(exportData.getFptxx(), "fptxx");

        excelWriter
//				.fill(exportMapAccounting, writeSheet0)
                .fill(exportMapFptxx, writeSheet0);


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


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