package cn.superdesk.uniorder.service.invoice.customer.invoiced.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.json.AnJsonUtils;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto._constant.ResourceConstant;
import cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedExcelExportVo;
import cn.superdesk.uniorder.notify.NotifyUtils;
import cn.superdesk.uniorder.service.invoice.customer.invoiced.InvoiceInvoicedExportExcelService;
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;

@Slf4j
@Service
public class InvoiceInvoicedExportExcelServiceImpl implements InvoiceInvoicedExportExcelService {

	@Autowired
	private ResourceConstant resourceConstant;

	private static final String TEMPLATE_EXPORT_EXCEL_INVOICE_INVOICEDS = "exportExcelInvoiceInvoiceds_v20210806.xlsx";

	public SysNotify exportExcelInvoiceInvoiceds(List<InvoiceInvoicedExcelExportVo> exportData) {

		log.info("{}", AnJsonUtils.toJson(exportData));

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

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

		String relativePath = File.separator + "excel" + File.separator + "invoice" + File.separator + "invoiced" + File.separator;

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

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