package com.yunyan.business.document.templateEpStrategy;


import cn.hutool.json.JSONUtil;
import com.yunyan.business.document.Word2PdfUtils;
import com.yunyan.business.document.WordGeneratorUtils;
import com.yunyan.business.document.common.constant.TemplateFilePathConstants;
import com.yunyan.business.document.utils.FileDataUtil;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalExcelDataVo;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalP1Vo;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalP4T1TermVo;
import com.yunyan.business.domain.FinalData;
import com.yunyan.business.domain.ReportExplain;
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.business.service.IReportDeptService;
import com.yunyan.business.service.IReportExplainService;
import com.yunyan.business.service.IReportTermService;
import com.yunyan.common.config.YunYanConfig;
import com.yunyan.common.core.domain.entity.SysDept;
import com.yunyan.common.exception.ServiceException;
import com.yunyan.common.utils.StringUtils;
import com.yunyan.common.utils.file.FileUploadUtils;
import com.yunyan.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class TplStrategyY2023FTService {

    @Autowired
    private IReportTermService reportTermService;

    @Autowired
    private IReportDeptService reportDeptService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IReportExplainService reportExplainService;

    public void exportSave(IFinalDataService finalDataService, Long id, String templatePath) {
        FinalData finalData = finalDataService.getById(id);

        ReportExplain reportExplain = reportExplainService.getReportExplainByReportId(id);
        String jsonContent = reportExplain.getJsonContent();
        if(null == reportExplain || StringUtils.isBlank(jsonContent)) {
            throw new ServiceException("还未有读取的报告数据");
        }

        FinalExcelDataVo dataVo = JSONUtil.toBean(jsonContent, FinalExcelDataVo.class);

        SysDept sysDept = sysDeptService.getById(finalData.getDeptId());
        if(null != sysDept) {
            //设置 名词解释
            List<FinalP4T1TermVo> termVoList = reportTermService.getTermInfo(sysDept.getArea(), finalData.getYear(), finalData.getType());
            dataVo.setTermList(termVoList);
        }

        //设置部门的
        FinalP1Vo finalP1Vo = reportDeptService.getDeptInfo(finalData.getDeptId(), finalData.getYear(), finalData.getType());
        dataVo.setP1Vo(finalP1Vo);

        // 上传文件路径
        String localOfficePath = YunYanConfig.getOfficePath();
        String localPath = YunYanConfig.getProfile();
        String targetWordFileName = "";
        String targetPdf = "";

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

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

        try {
            targetWordFileName = FileUploadUtils.getAbsoluteFile(localOfficePath, wordFileName).getAbsolutePath();
            targetPdf = FileUploadUtils.getAbsoluteFile(localOfficePath, pdfFileName).getAbsolutePath();
        } catch (IOException e) {
            log.error("创建office目录文件异常, {}, {}", e.getMessage(), e);
            //return AjaxResultVo.error("创建office目录文件异常");
        }

        //生成word
        WordGeneratorUtils.generatorWord(dataVo, targetWordFileName, templatePath);
        //word 转 pdf
        Word2PdfUtils.word2pdf(targetWordFileName, targetPdf, false);

        String wordPathFileName = "";
        String pdfPathFileName = "";

        try {
            wordPathFileName = FileUploadUtils.getPathFileName(localOfficePath, wordFileName);
            pdfPathFileName = FileUploadUtils.getPathFileName(localOfficePath, pdfFileName);
        } 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());
        }
        if(StringUtils.isNotBlank(dataVo.getDeptCode())) {
            fileVo.setDeptCode(dataVo.getDeptCode());
        }
        fileVo.setGenerateTime(new Date());

        FinalDataDownloadFileItemVo itemVo = new FinalDataDownloadFileItemVo();
        itemVo.setName(fileName);
        itemVo.setUrl(pdfPathFileName);
        itemVo.setFileExtension("pdf");
        //只有百色的pdf文件导出, 其他的不导出
        if(TemplateFilePathConstants.TEMPLATE_WORD_Y2023FT2BS.equals(templatePath) || TemplateFilePathConstants.TEMPLATE_WORD_Y2023FT9LC.equals(templatePath)
        ) {
            fileContentlist.add(itemVo);
        }


        //3.word
        FinalDataDownloadFileItemVo item3Vo = new FinalDataDownloadFileItemVo();
        item3Vo.setName(fileName);
        item3Vo.setUrl(wordPathFileName);
        item3Vo.setFileExtension("docx");
        item3Vo.setType(1);
        fileContentlist.add(item3Vo);

        String jsonFileContent = JSONUtil.toJsonStr(fileVo);

        //记录之前的旧文件路径, 判断是否存在, 存在就删除 todo 目前先不删
        FileDataUtil.deleteUrlFile(localPath, finalData.getJsonFileContent(), finalData.getPdfUrl());

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

    }
}
