package com.sunxung.factoring.service.project.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.*;
import com.sunxung.factoring.component.util.onlineConverter.OfficeConverterUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.project.*;
import com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask;
import com.sunxung.factoring.entity.supplier.CEnterpriseHistory;
import com.sunxung.factoring.entity.sys.*;
import com.sunxung.factoring.mapper.project.ProjectCoreEnterpriseMapper;
import com.sunxung.factoring.mapper.supplier.SupplierInfoMapper;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.project.*;
import com.sunxung.factoring.service.supplier.ICEnterpriseHistoryService;
import com.sunxung.factoring.service.supplier.IDueDiligenceAllocationService;
import com.sunxung.factoring.service.sys.*;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.apache.commons.io.FileUtils;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.*;

/**
 * <p>
 * 项目核心企业 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2023-11-25
 */
@Service
public class ProjectCoreEnterpriseServiceImpl extends ServiceImpl<ProjectCoreEnterpriseMapper, ProjectCoreEnterprise> implements IProjectCoreEnterpriseService {

    @Autowired
    private ICProjectCoreEnterpriseHistoryService projectCoreEnterpriseHistoryService;

    @Autowired
    private IDueDiligenceAllocationService dueDiligenceAllocationService;

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    @Resource(name = "projectInformationService")
    private IProjectInformationService projectInformationService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private ICEnterpriseHistoryService enterpriseHistoryService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private UserService userService;

    @Autowired
    private ICProjectCoreEnterpriseFileHistoryService projectCoreEnterpriseFileHistoryService;

    @Autowired
    private FileService fileService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private IProjectCoreEnterpriseAuditService projectCoreEnterpriseAuditService;

    @Autowired
    private IProjectCoreEnterpriseScoreService projectCoreEnterpriseScoreService;

    @Autowired
    private IProjectCoreEnterpriseComprehensiveEvaluationService projectCoreEnterpriseComprehensiveEvaluationService;

    @Autowired
    private DictionaryService dictionaryService;

    @Override
    public void startProjectProcess(ProjectInformation projectInformation) {
        User admin = userService.getByLoginName("admin");
        ThreadLocalUtil.setUser(admin);
        ProjectCoreEnterprise projectCoreEnterprise = projectInformation.getProjectCoreEnterprise();
        Long historyId = projectCoreEnterprise.getHistoryId();
        CEnterpriseHistory coreEnterprise = projectCoreEnterprise.getEnterprise();
        CEnterpriseHistory enterprise = projectInformation.getEnterprise();
        //开启流程
        Map<String, Object> procVariables = new HashMap<>();
        procVariables.put(ConstantUtil.PROJECT_CORE_ENTERPRISE_ASSIGNEE, admin.getId());
        //查询条件：业务编号 项目名称 主企业名称 核心企业名称
        String queryBusiness = projectInformation.getBusinessNumber() + FlowableUtil.splitChar + projectInformation.getProjectName();
        if (enterprise != null) {
            queryBusiness = queryBusiness + FlowableUtil.splitChar + enterprise.getCorporateName();
        }
        if (coreEnterprise != null) {
            queryBusiness = queryBusiness + FlowableUtil.splitChar + coreEnterprise.getCorporateName();
        }
        ProcessInstance processInst = flowableService.startProcessInstance(projectCoreEnterprise, procVariables,
                FlowableUtil.generateBusinessKey(projectCoreEnterprise, ConstantUtil.PROJECT_CORE_ENTERPRISE, queryBusiness), null, ConstantUtil.PROJECT_CORE_ENTERPRISE);
        if (processInst == null) {
            throw new BusinessException(CodeUtil.FAIL, "流程启动失败");
        }
        //完成第一步流程
        //获取风控负责人
        Long riskControlOwnerId = projectInformation.getRiskManagerId();

        procVariables.put(ConstantUtil.PROJECT_CORE_ENTERPRISE_APPROVAL_ADD_ASSIGNEE, riskControlOwnerId);
        String processInstId = processInst.getId();
        Task task = flowableService.findRuntimeTaskByProcessInstId(processInstId).get(0);
        Map<String, Object> msgVariables = new HashMap<>();
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_TITLE, "核心企业名称：" + coreEnterprise.getCorporateName());
        msgVariables.put(ConstantUtil.FLOWABLE_MSG_CONTENT,
                "核心企业名称“" + coreEnterprise.getCorporateName() + "”的核心企业已进入“"
                        + ConstantConversionUtil.flowBusinessKeyNames.get(ConstantUtil.PROJECT_CORE_ENTERPRISE) + "—核心企业审核，"
                        + ConstantUtil.FLOWABLE_MSG_ASSIGNEE);
        flowableService.completeMyTask(task.getId(), projectCoreEnterprise, procVariables, null, historyId, msgVariables, ConstantUtil.PROJECT_CORE_ENTERPRISE);
    }

    @Override
    public ProjectCoreEnterprise get(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public CProjectCoreEnterpriseHistory getView(Long id) {
        CProjectCoreEnterpriseHistory projectCoreEnterpriseHistory = projectCoreEnterpriseHistoryService.getById(id);
        List<CProjectCoreEnterpriseFileHistory> projectCoreEnterpriseFileHistoryList = projectCoreEnterpriseFileHistoryService.lambdaQuery().eq(CProjectCoreEnterpriseFileHistory::getcProjectCoreEnterpriseId, projectCoreEnterpriseHistory.getId()).list();
        if (CollectionUtil.isNotEmpty(projectCoreEnterpriseFileHistoryList)) {
            for (CProjectCoreEnterpriseFileHistory cProjectCoreEnterpriseFileHistory : projectCoreEnterpriseFileHistoryList) {
                List<FileStorageDO> fileStorageDOS = fileService.findByBusinessId(cProjectCoreEnterpriseFileHistory.getId(), FileModuleDict.ChildEnum.PROJECT_CORE_ENTERPRISE_FILE_HISTORY.getCode());
                cProjectCoreEnterpriseFileHistory.setFileStorageDOS(fileStorageDOS);
            }
        }
        Enterprise enterprise = enterpriseService.getById(projectCoreEnterpriseHistory.getcEnterpriseId());
        projectCoreEnterpriseHistory.setEnterprise(enterprise);
        projectCoreEnterpriseHistory.setProjectCoreEnterpriseFileHistoryList(projectCoreEnterpriseFileHistoryList);
        projectCoreEnterpriseHistory.setHistoricalCooperationDuration(StringUtil.isNotEmpty(projectCoreEnterpriseHistory.getHistoricalCooperationDuration()) ? dictionaryService.getByCode(projectCoreEnterpriseHistory.getHistoricalCooperationDuration()).getName() : "");
        projectCoreEnterpriseHistory.setHistoricalCooperationInvoicingAmount(StringUtil.isNotEmpty(projectCoreEnterpriseHistory.getHistoricalCooperationInvoicingAmount()) ? dictionaryService.getByCode(projectCoreEnterpriseHistory.getHistoricalCooperationInvoicingAmount()).getName() : "");
        projectCoreEnterpriseHistory.setHistoricalCooperationMaximumAccountingPeriod(StringUtil.isNotEmpty(projectCoreEnterpriseHistory.getHistoricalCooperationMaximumAccountingPeriod()) ? dictionaryService.getByCode(projectCoreEnterpriseHistory.getHistoricalCooperationMaximumAccountingPeriod()).getName() : "");
        projectCoreEnterpriseHistory.setHistoricalCooperationScale(StringUtil.isNotEmpty(projectCoreEnterpriseHistory.getHistoricalCooperationScale()) ? dictionaryService.getByCode(projectCoreEnterpriseHistory.getHistoricalCooperationScale()).getName() : "");
        projectCoreEnterpriseHistory.setHistoricalCooperationTimes(StringUtil.isNotEmpty(projectCoreEnterpriseHistory.getHistoricalCooperationTimes()) ? dictionaryService.getByCode(projectCoreEnterpriseHistory.getHistoricalCooperationTimes()).getName() : "");
        return projectCoreEnterpriseHistory;
    }

    @Override
    public GridPage<ProjectCoreEnterprise> findAll(SearchBusinessProcessingTask search) {
        Long currentUserId = ThreadLocalUtil.getUserId();
        List<UserOrgRel> userOrgRelList = userService.findOrgRelByUserId(currentUserId);
        List<Long> userIdList;
        List<UserRoleRel> userRoleRels = roleService.getUserRoleByUserId(ThreadLocalUtil.getUserId());
        List<Long> pers = permissionService.getPermissionIdByUserId(ThreadLocalUtil.getUserId());
        Permission per = permissionService.getByEname(ConstantConversionUtil.VIEW_ALL_PROJECT_CORE_ENTERPRISE);
        if (per == null) {
            throw new ValidatorException(CodeUtil.FAIL, "数据可见-查看全员权限不能为空");
        }
        if (pers.contains(per.getId())
                || ThreadLocalUtil.getUser().getLoginName().equals(ConstantConversionUtil.ADMINISTORIES)) {
            userIdList = null;
        } else {
            userIdList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(userRoleRels)) {
                for (UserOrgRel userOrgRel : userOrgRelList) {
                    if (userOrgRel.isUserIdentity()) {
                        // 如果是部门负责人，获取部门下所有人员Id
                        List<Long> userIds = userService.findUserIdByOrgId(userOrgRel.getOrgId());
                        userIdList.addAll(userIds);
                    } else {
                        userIdList.add(currentUserId);
                    }
                }
            }
        }
        search.setUserIds(userIdList);
        Page page = PageHelper.startPage(search.getPage(), search.getRows());
        List<ProjectCoreEnterprise> list = baseMapper.findBySearch(search);
        return new GridPage<>(search.getRows(), search.getPage(), page.getTotal(), list);
    }

    @Override
    public ProjectCoreEnterpriseScore getEnterpriseReport(Long id) {
        ProjectCoreEnterprise projectCoreEnterprise = get(id);
        projectCoreEnterprise.setHistoricalCooperationDuration(StringUtil.isNotEmpty(projectCoreEnterprise.getHistoricalCooperationDuration()) ? dictionaryService.getByCode(projectCoreEnterprise.getHistoricalCooperationDuration()).getName() : "");
        projectCoreEnterprise.setHistoricalCooperationScale(StringUtil.isNotEmpty(projectCoreEnterprise.getHistoricalCooperationScale()) ? dictionaryService.getByCode(projectCoreEnterprise.getHistoricalCooperationScale()).getName() : "");
        projectCoreEnterprise.setHistoricalCooperationInvoicingAmount(StringUtil.isNotEmpty(projectCoreEnterprise.getHistoricalCooperationInvoicingAmount()) ? dictionaryService.getByCode(projectCoreEnterprise.getHistoricalCooperationInvoicingAmount()).getName() : "");
        projectCoreEnterprise.setHistoricalCooperationMaximumAccountingPeriod(StringUtil.isNotEmpty(projectCoreEnterprise.getHistoricalCooperationMaximumAccountingPeriod()) ? dictionaryService.getByCode(projectCoreEnterprise.getHistoricalCooperationMaximumAccountingPeriod()).getName() : "");
        projectCoreEnterprise.setHistoricalCooperationTimes(StringUtil.isNotEmpty(projectCoreEnterprise.getHistoricalCooperationTimes()) ? dictionaryService.getByCode(projectCoreEnterprise.getHistoricalCooperationTimes()).getName() : "");
        CEnterpriseHistory enterpriseHistory = enterpriseHistoryService.getById(projectCoreEnterprise.getcEnterpriseHistoryId());
        projectCoreEnterprise.setEnterprise(enterpriseHistory);
        ProjectCoreEnterpriseAudit projectCoreEnterpriseAuditInfo = projectCoreEnterpriseAuditService.lambdaQuery().eq(ProjectCoreEnterpriseAudit::getcProjectCoreEnterpriseId, id).one();
        projectCoreEnterpriseAuditInfo.setEnterpriseNature(StringUtil.isNotEmpty(projectCoreEnterpriseAuditInfo.getEnterpriseNature()) ? dictionaryService.getByCode(projectCoreEnterpriseAuditInfo.getEnterpriseNature()).getName() : "");
        projectCoreEnterpriseAuditInfo.setEnterpriseNatureDetail(StringUtil.isNotEmpty(projectCoreEnterpriseAuditInfo.getEnterpriseNatureDetail()) ? dictionaryService.getByCode(projectCoreEnterpriseAuditInfo.getEnterpriseNatureDetail()).getName() : "");
        projectCoreEnterpriseAuditInfo.setHoldingRatio(StringUtil.isNotEmpty(projectCoreEnterpriseAuditInfo.getHoldingRatio()) ? dictionaryService.getByCode(projectCoreEnterpriseAuditInfo.getHoldingRatio()).getName() : "");
        projectCoreEnterpriseAuditInfo.setEquityHierarchy(StringUtil.isNotEmpty(projectCoreEnterpriseAuditInfo.getEquityHierarchy()) ? dictionaryService.getByCode(projectCoreEnterpriseAuditInfo.getEquityHierarchy()).getName() : "");
        projectCoreEnterpriseAuditInfo.setEquityHierarchyDetail(StringUtil.isNotEmpty(projectCoreEnterpriseAuditInfo.getEquityHierarchyDetail()) ? dictionaryService.getByCode(projectCoreEnterpriseAuditInfo.getEquityHierarchyDetail()).getName() : "");
        ProjectCoreEnterpriseComprehensiveEvaluation projectCoreEnterpriseComprehensiveEvaluation = projectCoreEnterpriseComprehensiveEvaluationService.lambdaQuery().eq(ProjectCoreEnterpriseComprehensiveEvaluation::getProjectCoreEnterpriseAuditId, projectCoreEnterpriseAuditInfo.getId()).one();
        ProjectCoreEnterpriseScore projectCoreEnterpriseScore = projectCoreEnterpriseScoreService.lambdaQuery().eq(ProjectCoreEnterpriseScore::getProjectCoreEnterpriseAuditId, projectCoreEnterpriseAuditInfo.getId()).one();
        projectCoreEnterpriseScore.setProjectCoreEnterprise(projectCoreEnterprise);
        projectCoreEnterpriseScore.setProjectCoreEnterpriseAuditInfo(projectCoreEnterpriseAuditInfo);
        projectCoreEnterpriseScore.setProjectCoreEnterpriseComprehensiveEvaluation(projectCoreEnterpriseComprehensiveEvaluation);

        LocalDate date1 = LocalDate.of(Integer.valueOf(DateUtil.getYear(enterpriseHistory.getStartSate())), Integer.valueOf(DateUtil.getMonth(enterpriseHistory.getStartSate())), Integer.valueOf(DateUtil.getDay(enterpriseHistory.getStartSate()))); // 第一个日期
        LocalDate date2 = LocalDate.of(Integer.valueOf(DateUtil.getYear(projectCoreEnterpriseScore.getGmtCreate())), Integer.valueOf(DateUtil.getMonth(projectCoreEnterpriseScore.getGmtCreate())), Integer.valueOf(DateUtil.getDay(projectCoreEnterpriseScore.getGmtCreate()))); // 当前日期（或者其他指定日期）
        Period period = Period.between(date1, date2); // 获取两个日期之间的时间段
        int yearsDiff = period.getYears(); // 获取年份差
        enterpriseHistory.setYearsDiff(yearsDiff);
        return projectCoreEnterpriseScore;
    }

    @Override
    public ProjectCoreEnterprise getByProjectInfoId(Long cProjectInformationId) {
        return baseMapper.getByProjectInfoId(cProjectInformationId);
    }

    @Override
    public void download(Long id, HttpServletResponse res) throws IOException {
        ProjectCoreEnterpriseScore enterpriseReport = getEnterpriseReport(id);
        ProjectCoreEnterprise projectCoreEnterprise = enterpriseReport.getProjectCoreEnterprise();
        CEnterpriseHistory enterprise = projectCoreEnterprise.getEnterprise();
        ProjectCoreEnterpriseAudit projectCoreEnterpriseAuditInfo = enterpriseReport.getProjectCoreEnterpriseAuditInfo();
        ProjectCoreEnterpriseComprehensiveEvaluation projectCoreEnterpriseComprehensiveEvaluation = enterpriseReport.getProjectCoreEnterpriseComprehensiveEvaluation();
        DecimalFormat b = new DecimalFormat("0.00");
        Map params = new HashMap();
        params.put("projectCoreEnterprise", enterprise.getCorporateName());
        params.put("socialCreditCode", enterprise.getSocialCreditCode());
        params.put("centralizedProcurementPlatform", projectCoreEnterprise.getCentralizedProcurementPlatform());
        params.put("yearsDiff", enterprise.getYearsDiff());
        params.put("establishmentPeriodScore", b.format(enterpriseReport.getEstablishmentPeriodScore()));
        params.put("registeredCapital", enterprise.getRegisteredCapital());
        params.put("registeredCapitalScore", b.format(enterpriseReport.getRegisteredCapitalScore()));
        params.put("businessAddress", enterprise.getBusinessAddress());
        params.put("businessAddressScore", b.format(enterpriseReport.getBusinessAddressScore()));
        params.put("enterpriseNature", projectCoreEnterpriseAuditInfo.getEnterpriseNature());
        params.put("enterpriseNatureScore", b.format(enterpriseReport.getEnterpriseNatureScore()));
        params.put("holdingRatio", projectCoreEnterpriseAuditInfo.getHoldingRatio());
        params.put("holdingRatioScore", b.format(enterpriseReport.getHoldingRatioScore()));
        String equityHierarchy = projectCoreEnterpriseAuditInfo.getEquityHierarchy() + " " + projectCoreEnterpriseAuditInfo.getEquityHierarchyDetail();
        params.put("equityHierarchy", equityHierarchy);
        params.put("equityHierarchyScore", b.format(enterpriseReport.getEquityHierarchyScore()));
        params.put("creditRating", projectCoreEnterpriseAuditInfo.getCreditRating());
        params.put("creditRatingScore", b.format(enterpriseReport.getCreditRatingScore()));
        params.put("enterpriseChange", enterpriseReport.getEnterpriseChange() == 0 ? "不存在" : "存在");
        params.put("enterpriseChangeScore", b.format(enterpriseReport.getEnterpriseChangeScore()));
        params.put("shiXin", enterpriseReport.getShiXin() == 0 ? "不存在" : "存在");
        params.put("shiXinScore", b.format(enterpriseReport.getShiXinScore()));
        params.put("stCount", enterpriseReport.getStCount() == 0 ? "不存在" : "存在");
        params.put("stCountScore", b.format(enterpriseReport.getStCountScore()));
        params.put("stCountPerson", enterpriseReport.getStCountPerson() == 0 ? "不存在" : "存在");
        params.put("stCountPersonScore", b.format(enterpriseReport.getStCountPersonScore()));
        params.put("zhiXingAmount", enterpriseReport.getZhiXingAmount().setScale(2, RoundingMode.HALF_UP) + "元人民币");
        params.put("zhiXingAmountScore", b.format(enterpriseReport.getZhiXingAmountScore()));
        params.put("zhiXingFrequent", enterpriseReport.getZhiXingFrequent());
        params.put("zhiXingFrequentScore", b.format(enterpriseReport.getZhiXingFrequentScore()));
        params.put("historicalLitigationAmount", enterpriseReport.getHistoricalLitigationAmount().setScale(2, RoundingMode.HALF_UP) + "元人民币");
        params.put("historicalLitigationAmountScore", b.format(enterpriseReport.getHistoricalLitigationAmountScore()));
        params.put("historicalLitigationCount", enterpriseReport.getHistoricalLitigationCount());
        params.put("historicalLitigationCountScore", b.format(enterpriseReport.getHistoricalLitigationCountScore()));
        params.put("privateLending", enterpriseReport.getPrivateLending() == 0 ? "不存在" : "存在");
        params.put("privateLendingScore", b.format(enterpriseReport.getPrivateLendingScore()));
        params.put("financialInstitution", enterpriseReport.getFinancialInstitution() == 0 ? "不存在" : "存在");
        params.put("financialInstitutionScore", b.format(enterpriseReport.getFinancialInstitutionScore()));
        params.put("equityFreeze", enterpriseReport.getEquityFreeze() == 0 ? "不存在" : "存在");
        params.put("equityFreezeScore", b.format(enterpriseReport.getEquityFreezeScore()));
        params.put("majorAbnormalLitigation", enterpriseReport.getMajorAbnormalLitigation() == 0 ? "不存在" : "存在");
        params.put("majorAbnormalLitigationScore", b.format(enterpriseReport.getMajorAbnormalLitigationScore()));
        params.put("seriousIllegality", enterpriseReport.getSeriousIllegality() == 0 ? "不存在" : "存在");
        params.put("seriousIllegalityScore", b.format(enterpriseReport.getSeriousIllegalityScore()));
        params.put("taxOweNotice", enterpriseReport.getTaxOweNotice() == 0 ? "不存在" : "存在");
        params.put("taxOweNoticeScore", b.format(enterpriseReport.getTaxOweNoticeScore()));
        params.put("taxIllegal", enterpriseReport.getTaxIllegal() == 0 ? "不存在" : "存在");
        params.put("taxIllegalScore", b.format(enterpriseReport.getTaxIllegalScore()));
        params.put("blacklist", enterpriseReport.getBlacklist() == 0 ? "不存在" : "存在");
        params.put("blacklistScore", b.format(enterpriseReport.getBlacklistScore()));
        params.put("exception", enterpriseReport.getException() == 0 ? "不存在" : "存在");
        params.put("exceptionScore", b.format(enterpriseReport.getExceptionScore()));
        params.put("administrativePenaltyCount", enterpriseReport.getAdministrativePenaltyCount());
        params.put("administrativePenaltyCountScore", b.format(enterpriseReport.getAdministrativePenaltyCountScore()));
        params.put("administrativePenaltyAmount", enterpriseReport.getAdministrativePenaltyAmount().setScale(2, RoundingMode.HALF_UP) + "元人民币");
        params.put("administrativePenaltyAmountScore", b.format(enterpriseReport.getAdministrativePenaltyAmountScore()));
        params.put("environmentalPenaltiesAmount", enterpriseReport.getEnvironmentalPenaltiesAmount().setScale(2, RoundingMode.HALF_UP) + "元人民币");
        params.put("environmentalPenaltiesAmountScore", b.format(enterpriseReport.getEnvironmentalPenaltiesAmountScore()));
        params.put("news", enterpriseReport.getNews() == 0 ? "不存在" : "存在");
        params.put("newsScore", b.format(enterpriseReport.getNewsScore()));

        params.put("historicalCooperationDuration", projectCoreEnterprise.getHistoricalCooperationDuration());
        params.put("historicalCooperationDurationScore", enterpriseReport.getHistoricalCooperationDurationScore().setScale(2, RoundingMode.HALF_UP));
        params.put("historicalCooperationScale", projectCoreEnterprise.getHistoricalCooperationScale());
        params.put("historicalCooperationScaleScore", b.format(enterpriseReport.getHistoricalCooperationScaleScore()));
        params.put("historicalCooperationInvoicingAmount", projectCoreEnterprise.getHistoricalCooperationInvoicingAmount());
        params.put("historicalCooperationInvoicingAmountScore", b.format(enterpriseReport.getHistoricalCooperationInvoicingAmountScore()));
        params.put("historicalCooperationMaximumAccountingPeriod", projectCoreEnterprise.getHistoricalCooperationMaximumAccountingPeriod());
        params.put("historicalCooperationMaximumAccountingPeriodScore", b.format(enterpriseReport.getHistoricalCooperationMaximumAccountingPeriodScore()));
        params.put("historicalCooperationTimes", projectCoreEnterprise.getHistoricalCooperationTimes());
        params.put("historicalCooperationTimesScore", b.format(enterpriseReport.getHistoricalCooperationTimesScore()));
        params.put("evaluationOpinions", projectCoreEnterpriseAuditInfo.getEvaluationOpinions());
        params.put("admitted", projectCoreEnterpriseComprehensiveEvaluation.getAdmitted());
        params.put("fixedCreditScore", projectCoreEnterpriseComprehensiveEvaluation.getFixedCreditScore().setScale(2, RoundingMode.HALF_UP));
        params.put("specialQuotaScore", projectCoreEnterpriseComprehensiveEvaluation.getSpecialQuotaScore().setScale(2, RoundingMode.HALF_UP));
        params.put("fixedCredit", projectCoreEnterpriseComprehensiveEvaluation.getFixedCredit().setScale(2, RoundingMode.HALF_UP) + "万元人民币");
        params.put("specialQuota", projectCoreEnterpriseComprehensiveEvaluation.getSpecialQuota().setScale(2, RoundingMode.HALF_UP) + "万元人民币");
        params.put("creditLimit", projectCoreEnterpriseComprehensiveEvaluation.getCreditLimit().setScale(2, RoundingMode.HALF_UP) + "万元人民币");
        //模板地址
        ClassPathResource classPathResource = new ClassPathResource("ftl/project/projectCoreEnterpriseReport.docx");
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream()).render(params);
        String path = FileUtil.getAbsolutePath(UUID.randomUUID().toString(), ".docx", FileModuleDict.ChildEnum.PROJECT_CORE_ENTERPRISE_CREDIT_REPORT);
        File docxFile = new File(path);
        if (!docxFile.exists()) {
            FileUtils.createParentDirectories(docxFile);
        }
        template.writeToFile(path);
        //将docx转为pdf
        String suffix = FileUtil.getSuffix(docxFile.getName());
        String newPath = docxFile.getAbsolutePath().replace(suffix, ".pdf");
        File pdfFile = OfficeConverterUtil.office2Pdf(docxFile.getAbsolutePath(), newPath);
        docxFile.deleteOnExit();
        FileUtil.downloadPdfFile(res, pdfFile);
    }

    @Override
    public List<ProjectCoreEnterprise> getByEnterpriseId(Long enterpriseId) {
        return baseMapper.getByEnterpriseId(enterpriseId);
    }
}
