//package com.yaoyicloud.render.foundation;
//
//import java.io.IOException;
//
//import java.io.InputStream;
//import java.util.Map;
//import java.util.Objects;
//
//import com.deepoove.poi.config.Configure;
//import com.deepoove.poi.config.ConfigureBuilder;
//import com.deepoove.poi.policy.RenderPolicy;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.google.protobuf.InvalidProtocolBufferException;
//import com.google.protobuf.util.JsonFormat;
//import com.yaoyicloud.config.CommonDataCache;
//import com.yaoyicloud.config.FilerepoProperties;
//import com.yaoyicloud.message.FxyProtos;
//import com.yaoyicloud.factory.FXYAbstractRender;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.MapUtils;
//
///**
// * BasicInfo渲染器
// *
// */
//@Slf4j
//public final class FoundationBasicInfoRender extends FXYAbstractRender {
//    private final FilerepoProperties filerepoProperties;
//    private final CommonDataCache commonDataCache;
//
//    public FoundationBasicInfoRender(String cwd, FilerepoProperties filerepoProperties,
//        CommonDataCache commonDataCache) {
//        super(cwd);
//        this.filerepoProperties = filerepoProperties;
//        this.commonDataCache = commonDataCache;
//    }
//
//    @Override
//    protected String getBasicPath() throws IOException {
//        return filerepoProperties.getBasePath();
//    }
//
//    @Override
//    protected String getReportImagePath() {
//        return filerepoProperties.getReportImagePath();
//    }
//
//    /**
//     * Docx 渲染
//     *
//     * @param info 数据
//     * @param inputStream 模板内容
//     * @return 本地文件目录
//     * @throws IOException
//     */
//    public String renderDocx(String info, InputStream inputStream,
//        String relationId) throws IOException {
//        log.info("开始渲染基金会基础信息报告模块，relationId: {}", relationId);
//
//        // 配置POI-TL渲染器
//        ConfigureBuilder builder = Configure.builder();
//        RenderPolicy indicatorsRenderPolicyToProtobuf = this.indicatorsRenderPolicyToProtobuf();
//        builder.bind("basicInfoChecks", indicatorsRenderPolicyToProtobuf).bind("superiorAuthority",
//            indicatorsRenderPolicyToProtobuf);
//        builder.addPlugin('^', this.pictureRenderPolicy());
//        builder.useSpringEL();
//        // 通过默认protobuf实例来填充不存在的key
//        FxyProtos.BasicInfo.Builder basicInfoBuilder = FxyProtos.BasicInfo.newBuilder();
//        JsonFormat.parser().merge(info, basicInfoBuilder);
//        FxyProtos.BasicInfo defaultInstance = FxyProtos.BasicInfo.getDefaultInstance();
//        FxyProtos.BasicInfo mergedProto = defaultInstance.toBuilder()
//            .mergeFrom(basicInfoBuilder.build())
//            .build();
//
//        String completeJson = JsonFormat.printer()
//            .includingDefaultValueFields()
//            .print(mergedProto);
//
//        ObjectMapper objectMapper = new ObjectMapper();
//        Map<String, Object> data = objectMapper.readValue(completeJson, new TypeReference<Map<String, Object>>() {});
//        data.replaceAll((k, v) -> v.equals("") ? "-" : v);
//
//        Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
//        if (MapUtils.isNotEmpty(commonDataCacheData)) {
//            data.putAll(commonDataCacheData);
//        }
//
//        fillBasicDefaultValues(data);
//        try {
//            // 渲染文档
//            String resultPath = this.renderDocx(data, inputStream, builder, relationId, "basicInfo");
//            log.info("渲染基金会基础信息报告模块成功，文件路径: {}", resultPath);
//            return resultPath;
//        } catch (Exception e) {
//            log.error("渲染基金会基础信息报告模块失败，relationId: {}", relationId, e);
//            throw new IOException("文档渲染失败", e);
//        }
//    }
//
//    /**
//     * 填充默认值，确保所有必要字段都存在
//     */
//    private void fillBasicDefaultValues(Map<String, Object> data)
//        throws InvalidProtocolBufferException, JsonProcessingException {
//        Map<String, Object> basicInfoSummary = (Map<String, Object>) data.get("basicInfoSummary");
//        basicInfoSummary.replaceAll((k, v) -> v.equals("") ? "-" : v);
//        Object netAsset = data.get("netAsset");
//        data.put("netAsset", netAsset + "元人民币");
//
//        Map<String, Object> foundationExt = (Map<String, Object>) data.get("foundationExt");
//        if (foundationExt == null) {
//            ObjectMapper objectMapper = new ObjectMapper();
//            FxyProtos.FoundationBasicInfoExt foundationBasicInfoExt =
//                FxyProtos.FoundationBasicInfoExt.getDefaultInstance();
//            String print = JsonFormat.printer()
//                .includingDefaultValueFields()
//                .print(foundationBasicInfoExt);
//            foundationExt = objectMapper.readValue(print, new TypeReference<Map<String, Object>>() {});
//            foundationExt.replaceAll((k, v) -> v.equals("") ? "-" : v);
//            data.put("foundationExt", foundationExt);
//        }
//
//        foundationExt.replaceAll((k, v) -> v.equals("") ? "-" : v);
//
//        Object lastYearSponsorshipDonationRatio = foundationExt.get("lastYearDonationIncome");
//        foundationExt.put("lastYearDonationIncome", lastYearSponsorshipDonationRatio + "万元人民币");
//        Object initialFund = foundationExt.get("initialFund");
//        foundationExt.put("initialFund", initialFund + "万人民币");
//
//        Object numberOfDirectors = foundationExt.get("numberOfDirectors");
//        if (numberOfDirectors.equals("-")) {
//            foundationExt.put("numberOfDirectors", 0);
//        }
//
//        Object certReceived = data.get("certReceived");
//        if (Objects.equals(certReceived, "-")) {
//            data.put("certReceived", "否");
//        }
//
//        Object bankLicense = data.get("bankLicense");
//        if (Objects.equals(bankLicense, "-")) {
//            data.put("bankLicense", "否");
//        }
//
//        Object annualInspectionResult = data.get("annualInspectionResult");
//        if (Objects.equals(annualInspectionResult, "-")) {
//            data.put("annualInspectionResult", "无");
//        }
//    }
//
//}
