package com.yunyan.business.document.templateEpStrategy;


import cn.hutool.json.JSONUtil;
import com.yunyan.business.constant.TemplateEpType;
import com.yunyan.business.document.Word2PdfUtils;
import com.yunyan.business.document.WordGeneratorUtils;
import com.yunyan.business.document.y22.GenerateFinalExcelY22Utils;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalExcelDataVo;
import com.yunyan.business.domain.FinalData;
import com.yunyan.business.pojo.admin.vo.FinalDataDownloadFileItemVo;
import com.yunyan.business.pojo.admin.vo.FinalDataDownloadFileVo;
import com.yunyan.business.service.IFinalDataService;
import com.yunyan.common.config.YunYanConfig;
import com.yunyan.common.utils.StringUtils;
import com.yunyan.common.utils.file.FileUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@TemplateEpTypeAnnotation(templateEpType = TemplateEpType.Y2022FT2)
public class TplStrategyY2022FT2 implements TplStrategy {

    @Override
    public void exportSave(IFinalDataService finalDataService, Long id) {

        log.debug("tpl TplStrategyY2022FT2");

        /*FinalData finalData = finalDataService.getById(id);
        FinalExcelDataVo dataVo = JSONUtil.toBean(finalData.getJsonTableContent(), FinalExcelDataVo.class);

        // 上传文件路径
        String filePath = YunYanConfig.getOfficePath();
        long start = System.currentTimeMillis();
        String targetWordFileName = "";
        String targetPdf = "";
        String targetExcel = "";

        String targetWordFileName2 = "";
        String targetPdf2 = "";

        Long deptId = finalData.getDeptId();

        //文件名
        String deptName = dataVo.getDeptName();
        String deptCode = dataVo.getDeptCode();
        String year = dataVo.getYear();
        String fileName = String.format("%s_%s_%s_%s", deptName, deptCode, year, id);

        String wordFileName = FileUploadUtils.extractFilename(fileName, "docx");
        String pdfFileName = FileUploadUtils.extractFilename(fileName, "pdf");
        String excelFileName = FileUploadUtils.extractFilename(fileName, "xls");

        //第二个模板的
        String wordFileName2 = FileUploadUtils.extractFilename(2 + "_" +fileName, "docx");
        String pdfFileName2 = FileUploadUtils.extractFilename(2 + "_" +fileName, "pdf");

        //全路径
        try {
            targetWordFileName = FileUploadUtils.getAbsoluteFile(filePath, wordFileName).getAbsolutePath();
            targetPdf = FileUploadUtils.getAbsoluteFile(filePath, pdfFileName).getAbsolutePath();
            targetExcel = FileUploadUtils.getAbsoluteFile(filePath, excelFileName).getAbsolutePath();

            targetWordFileName2 = FileUploadUtils.getAbsoluteFile(filePath, wordFileName2).getAbsolutePath();
            targetPdf2 = FileUploadUtils.getAbsoluteFile(filePath, pdfFileName2).getAbsolutePath();
        } catch (IOException e) {
            log.error("创建office目录文件异常, {}, {}", e.getMessage(), e);
            //return AjaxResultVo.error("创建office目录文件异常");
        }

        //生成word预览
        WordGeneratorUtils.generatorWord(dataVo, targetWordFileName);
        //word 转 pdf 预览
        Word2PdfUtils.word2pdf(targetWordFileName, targetPdf, true);

        GenerateFinalExcelY22Utils.generateExcel(dataVo, targetExcel);

        //生成word 已去掉 第二部分
        WordGeneratorUtils.generatorWord(dataVo, targetWordFileName2, WordGeneratorUtils.TEMPLATE_WORD_Y2022FT2);
        //word 转 pdf 预览
        Word2PdfUtils.word2pdf(targetWordFileName2, targetPdf2, false);


        long end = System.currentTimeMillis();
        log.debug("生成word, 转PDF，用时：" + (end - start) + "ms");

        String wordPathFileName2 = "";
        String pdfPathFileName = "";
        String excelPathFileName = "";

        //已去掉 第二部分的 表格
        String pdfPathFileName2 = "";

        try {
            wordPathFileName2 = FileUploadUtils.getPathFileName(filePath, wordFileName2);
            pdfPathFileName = FileUploadUtils.getPathFileName(filePath, pdfFileName);
            excelPathFileName = FileUploadUtils.getPathFileName(filePath, excelFileName);

            pdfPathFileName2 = FileUploadUtils.getPathFileName(filePath, pdfFileName2);
        } catch (IOException e) {
            log.error("获取office目录文件异常, {}, {}", e.getMessage(), e);
            //return AjaxResultVo.error("获取office目录文件异常");
        }

        //设置保存 文件的url
        FinalDataDownloadFileVo fileVo = new FinalDataDownloadFileVo();
        List<FinalDataDownloadFileItemVo> fileContentlist = new ArrayList<>();
        fileVo.setList(fileContentlist);

        //获取部门
        if(StringUtils.isNotBlank(dataVo.getDeptName())) {
            fileVo.setDeptName(dataVo.getDeptName());
        }

        //1.pdf 生成第二个模板的 word 与 pdf
        FinalDataDownloadFileItemVo itemVo = new FinalDataDownloadFileItemVo();
        itemVo.setName("PDF文件");
        itemVo.setUrl(pdfPathFileName2);
        itemVo.setFileExtension("pdf");
        //pdf文件不导出
        //fileContentlist.add(itemVo);

        //2.excel
        FinalDataDownloadFileItemVo item2Vo = new FinalDataDownloadFileItemVo();
        item2Vo.setName("excel文件");
        item2Vo.setUrl(excelPathFileName);
        item2Vo.setFileExtension("xls");
        fileContentlist.add(item2Vo);

        //3.word
        FinalDataDownloadFileItemVo item3Vo = new FinalDataDownloadFileItemVo();
        item3Vo.setName("word文件");
        item3Vo.setUrl(wordPathFileName2);
        item3Vo.setFileExtension("docx");
        fileContentlist.add(item3Vo);

        String jsonFileContent = JSONUtil.toJsonStr(fileVo);

        //更新 保存 pdf状态数据
        FinalData updateEntity = new FinalData();
        updateEntity.setId(finalData.getId());
        //预览的
        updateEntity.setPdfUrl(pdfPathFileName);
        updateEntity.setGenerateTime(new Date());
        //updateEntity.setGenerateBy(SecurityUtils.getUsername());
        updateEntity.setGenerateStatus(1);
        updateEntity.setJsonFileContent(jsonFileContent);
        finalDataService.updateById(updateEntity);*/

    }


}
