package com.casic.missiles.service.listeners.register.data;

import com.casic.missiles.dto.business.certificate.ElectronicImage;
import com.casic.missiles.dto.business.file.FilePrintRegister;
import com.casic.missiles.dto.flowable.ApprovalLogResponse;
import com.casic.missiles.mapper.sys.PrintFileRegisterMapper;
import com.casic.missiles.service.flowable.ApprovalOperateService;
import com.casic.missiles.service.listeners.register.FileImageTemplateProvider;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * 文件构建数据 spi
 *
 * @author cz
 * @date 2023-03-10
 */
public class PrintFileDataSupport extends FileImageTemplateProvider {

    @Autowired
    protected ApprovalOperateService approvalOperateService;
    @Resource
    protected PrintFileRegisterMapper fileRegisterMapper;

    @Resource
    private FileParamDataProvider fileParamDataCreator;

    /**
     * 初始化文件数据注册信息
     * 1、获取证书模板
     * 2、设置临时处理空间路径、文件名称、文件资源路径
     * 3、设置文件id，作为操作空间文件名称，防止对其他同时处理的证书形成影响
     */
    protected final FilePrintRegister initialization(String templateFileDir,
                                                     Long id, String fileName) {
        FilePrintRegister filePrintRegister = new FilePrintRegister();
        templateFileDir = templateFileDir + id + File.separator;
        filePrintRegister.setFileName(fileName);
        filePrintRegister.setTemDir(templateFileDir);
        filePrintRegister.setTemplatePath(templateFileDir + fileName);
        filePrintRegister.setFileNameId(id);
        List<String> fileNames = new ArrayList<>();
        fileNames.add(fileName);
        filePrintRegister.setBeDownloadedFileNames(fileNames);
        return filePrintRegister;
    }

    public <T> String getTemplateName(T templateIdOrName) {
        String fileName = "";
        if (templateIdOrName instanceof Long) {
            fileName = fileRegisterMapper.getSignFileSealById((Long) templateIdOrName, "system_template");
        }
        if (templateIdOrName instanceof String) {
            fileName = fileRegisterMapper.getFileNameByName((String) templateIdOrName, "system_template");
        }
        return fileName;
    }


    /**
     * 电子签名创建器
     */
    protected final List<ElectronicImage> signNamesCreator(Map<String, List<ApprovalLogResponse>> approvalLogMap, FilePrintRegister filePrintRegister) {
        List<ElectronicImage> electronicImageList = new ArrayList<>();
        for (int i = 0; i < keyParamList.size(); i++) {
            //是不是当前的人员，不是就skip
            if (!approvalLogMap.containsKey(keyParamList.get(i))) {
                continue;
            }
            final String customizedSignName = customizedSignNames.get(i);
            List<ApprovalLogResponse> approvalLogList = approvalLogMap.get(keyParamList.get(i));
            if (CollectionUtils.isNotEmpty(approvalLogList)) {
                approvalLogList.forEach(
                        approvalLog -> {
                            ElectronicImage electronicImage = defaultElectronicSignName(customizedSignName);
                            String signFileName = fileRegisterMapper.getSignFileNameById(approvalLog.getAssigneeId());
                            if (StringUtils.isEmpty(signFileName)) {
                                return;
                            }
                            //设置集合
                            electronicImage.setElectronicSealImageUrl(filePrintRegister.getTemDir() + signFileName);
                            //放入待下载的文件集合
                            filePrintRegister.getBeDownloadedFileNames().add(signFileName);
                            electronicImageList.add(electronicImage);
                        }
                );
            }
        }
        return electronicImageList;
    }


    /**
     * 电子签章获取
     *
     * @param electronicSealId 证书报告文件数据
     * @param electronicImage  关键字
     * @return
     */
    protected final ElectronicImage doPopulateElectronicSignSeal(Long electronicSealId, ElectronicImage electronicImage, FilePrintRegister filePrintRegister) {
        String electronicSealName = fileRegisterMapper.getSignFileSealById(electronicSealId, "system_sign");
        //设置集合
        electronicImage.setElectronicSealImageUrl(filePrintRegister.getTemDir() + electronicSealName);
        //放入待下载的文件集合
        filePrintRegister.getBeDownloadedFileNames().add(electronicSealName);
        return electronicImage;
    }

    /**
     * 证书参数在模板中起替换作用
     * 基本所包含的参数替换
     *
     * @param orderId
     * @param sampleId
     * @return
     */
    protected Map<String, Object> certificateFileParamCreator(Long orderId, Long sampleId) {
        Map<String, Object> fileParamData = fileParamDataCreator
                .builder()
                .loadTime()
                .businessOrder(orderId)
                .customerSample(sampleId)
                .customerInfo()
                .build();
        return fileParamData;
    }

    /**
     * 外检环境参数在模板中起替换作用
     * 基本所包含的参数替换
     *
     * @param orderId
     * @return
     */
    protected Map<String, Object> siteFileParamCreator(Long orderId, Long siteId) {
        Map<String, Object> fileParamData = fileParamDataCreator
                .builder()
                .loadTime()
                .businessOrder(orderId)
                .customerInfo()
                .equipmentFixedAssets(siteId)
                .build();
        return fileParamData;
    }

}
