package cn.superdesk.uniorder.service.order.base.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.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto._constant.ResourceConstant;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.notyet.CustomerUnAccountingDimGoodsVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.customer.statement.CustomerStatementOfAccountDetailVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.notyet.UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo;
import cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimCreditItemVo;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderGoodsVo;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderJoinGoodsDetailVo;
import cn.superdesk.uniorder.notify.NotifyUtils;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderExportExcelService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
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;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/3/5 上午11:03
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/3/5 上午11:03
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class UnifiedOrderExportExcelServiceImpl implements UnifiedOrderExportExcelService {

    @Autowired
    private ResourceConstant resourceConstant;

    private static final String TEMPLATE_EXPORT_EXCEL_UNIFIED_ORDER_MANAGER_GOODS_DATA       = "exportExcelOrderManagerGoodsData.xlsx";
    private static final String TEMPLATE_EXPORT_EXCEL_UNIFIED_ORDER_MANAGER_GOODS_COST_PRICE = "exportExcelOrderManagerGoodsCostPrice.xlsx";


    private static final String TEMPLATE_EXPORT_EXCEL_ACCOUNTING_CUSTOMER_NOT_YET_DIM_ORDER = "exportExcelAccountingCustomerNotYetDimOrder.xlsx";
    private static final String TEMPLATE_EXPORT_EXCEL_ACCOUNTING_CUSTOMER_NOT_YET_DIM_GOODS = "exportExcelAccountingCustomerNotYetDimGoods.xlsx";

    /****************************************** 订单管理 export excel start ******************************************/
    @Override
    public SysNotify exportExcelOrderManagerGoodsData(List<UnifiedOrderGoodsVo> exportData) {

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

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

        String relativePath = File.separator + "excel" +
                File.separator + "order" +
                File.separator + "manager" +
                File.separator;

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

        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.成功);

    }

    @Override
    public SysNotify exportExcelOrderManagerGoodsCostPrice(List<UnifiedOrderGoodsVo> exportData) {


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

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

        String relativePath = File.separator + "excel" +
                File.separator + "order" +
                File.separator + "manager" +
                File.separator;

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

        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.成功);

    }
    /****************************************** 订单管理 export excel   end ******************************************/


    /****************************************** 客户结算对账 未对账 start ******************************************/
    /**
     * 客户结算对账-未对账商品
     *
     * @param exportData
     * @return
     */
    @Override
    public SysNotify exportExcelAccountingCustomerNotYetDimOrder(List<UnifiedOrderJoinGoodsDetailVo> exportData) {

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

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

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

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

        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.成功);

    }

    @Override
    public SysNotify exportExcelAccountingCustomerNotYetDimGoods(List<CustomerUnAccountingDimGoodsVo> exportData) {
        /******************************************  start ******************************************/

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

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

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

        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.成功);
    }

    /****************************************** 客户结算对账 未对账   end ******************************************/


    /****************************************** 积分结算对账 未对账 start ******************************************/

    private static final String TEMPLATE_EXPORT_EXCEL_ACCOUNTING_CUSTOMER_NOT_YET_DIM_CREDIT = "exportExcelAccountingCustomerNotYetDimCredit.xlsx";

    @Override
    public SysNotify exportExcelScoreOrderReconciliationNotYet(List<InvoiceApplyDetailDimCreditItemVo> 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_EXPORT_EXCEL_ACCOUNTING_CUSTOMER_NOT_YET_DIM_CREDIT;

        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.成功);

    }
    /****************************************** 积分结算对账 未对账   end ******************************************/


    /****************************************** 供应商结算对账 未对账 start ******************************************/

    private static final String TEMPLATE_EXPORT_EXCEL_ACCOUNTING_SUPPLIER_NOT_YET_DIM_GOODS = "exportExcelAccountingSupplierNotYetDimGoods.xlsx";

    @Override
    public SysNotify exportExcelAccountingSupplierNotYetDimGoods(List<UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo> exportData) {

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

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

        String relativePath = File.separator + "excel" +
                File.separator + "accounting" +
                File.separator + "unified" +
                File.separator + "supplier" +
                File.separator;

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

        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)
                .inMemory(Boolean.TRUE)
//				.registerWriteHandler(SugarUpBigDecimalCellWriteHandler
//						.builder()
//						.startRow(1)
//						.matchCell(Arrays.asList(13, 14, 15, 16))
//						.build()
//				)
                .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.成功);
    }
    /****************************************** 供应商结算对账 未对账   end ******************************************/


    /****************************************** 客户结算 对账单 start ******************************************/

    private static final String TEMPLATE_EXPORT_CUSTOMER_STATEMENTOFACCOUNTDETAIL_COMPATIBLE = "exportCustomerStatementOfAccountDetailCompatible.xlsx";

    @Override
    public SysNotify exportCustomerStatementOfAccountDetailCompatible(String accountingOrderSn, CustomerStatementOfAccountDetailVo exportData) {

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

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

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

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

        String targetFullPath          = excelExportPathRoot + excelExportRelativePathStorage;
        String targetFileName          = "客户结算_" + "电子对账单_" + accountingOrderSn + 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();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "对账单明细").build();

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

        excelWriter.fill(exportData, writeSheet0);
//		excelWriter.fill(exportData.getBankInfo(), writeSheet0);
        excelWriter.fill(new FillWrapper("detail", exportData.getOrders()), fillConfig, writeSheet1);


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

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


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

    }

    /****************************************** 客户结算 对账单   end ******************************************/


    /****************************************** 供应商结算对账 对账单 详情 start ******************************************/


    private static final String TEMPLATE_EXPORT_SUPPLIER_STATEMENTOFACCOUNTDETAIL_COMPATIBLE = "exportSupplierStatementOfAccountDetailCompatible.xlsx";

    @Override
    public SysNotify exportSupplierStatementOfAccountDetailCompatible(String accountingOrderSn, List<UnifiedOrderJoinGoodsOfSupplierStatementOfAccountVo> exportData) {

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

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

        String relativePath = File.separator + "excel" +
                File.separator + "accounting" +
                File.separator + "unified" +
                File.separator + "supplier" +
                File.separator;

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

        String targetFullPath          = excelExportPathRoot + excelExportRelativePathStorage;
        String targetFileName          = "供应商结算_" + "电子对账单_" + accountingOrderSn + 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)
                .inMemory(Boolean.TRUE)
//				.registerWriteHandler(SugarUpBigDecimalCellWriteHandler
//						.builder()
//						.startRow(1)
//						.matchCell(Arrays.asList(12, 13, 14, 15))
//						.build()
//				)
                .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.成功);
    }

    /****************************************** 供应商结算对账 对账单 详情 start ******************************************/

}
