//package com.yaoyicloud.render;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//
//import com.deepoove.poi.config.Configure;
//import com.deepoove.poi.config.ConfigureBuilder;
//import com.deepoove.poi.policy.RenderPolicy;
//
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.google.protobuf.util.JsonFormat;
//import com.yaoyicloud.config.CommonDataCache;
//import com.yaoyicloud.config.FilerepoProperties;
//
//import com.yaoyicloud.factory.FXYAbstractRender;
//import com.yaoyicloud.message.FxyProtos;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.collections4.MapUtils;
//
///**
// * PublicRecord渲染器
// *
// */
//@Slf4j
//public final class PublicRecordRender extends FXYAbstractRender {
//    private final FilerepoProperties filerepoProperties;
//private final CommonDataCache commonDataCache;
//    public PublicRecordRender(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 indicatorsRenderPolicy = this.indicatorsRenderPolicy();
//
//        builder.bind("dishonestPersons", indicatorsRenderPolicy);
//        builder.bind("businessAbnormals", indicatorsRenderPolicy);
//        builder.bind("penaltyRecords", indicatorsRenderPolicy);
//        builder.bind("taxPenalties", indicatorsRenderPolicy);
//        builder.bind("severeViolations", indicatorsRenderPolicy);
//
//        FxyProtos.PublicRecord.Builder basicInfoBuilder = FxyProtos.PublicRecord.newBuilder();
//        JsonFormat.parser().merge(info, basicInfoBuilder);
//        FxyProtos.PublicRecord defaultInstance = FxyProtos.PublicRecord.getDefaultInstance();
//        FxyProtos.PublicRecord 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);
//
//        fillBasicDefaultValues(data);
//        Map<String, Object> commonDataCacheData = commonDataCache.getData(relationId);
//        if (MapUtils.isNotEmpty(commonDataCacheData)) {
//            data.putAll(commonDataCacheData);
//        }
//
//        try {
//            // 渲染文档
//            String resultPath = this.renderDocx(data, inputStream, builder, relationId, "publicRecord");
//            log.info("渲染公共模块成功，文件路径: {}", resultPath);
//            return resultPath;
//        } catch (Exception e) {
//            log.error("渲染公共模块失败，relationId: {}", relationId, e);
//            throw new IOException("文档渲染失败", e);
//        }
//    }
//
//    /**
//     * 填充默认值，确保所有必要字段都存在
//     */
//    private void fillBasicDefaultValues(Map<String, Object> data) {
//        Map<String, Object> basicInfoSummary = (Map<String, Object>) data.get("publicRecordSummary");
//        basicInfoSummary.replaceAll((k, v) -> v.equals("") ? "-" : v);
//
//        // 处理失信人数据
//        ArrayList<Map<String, Object>> dishonestPersons = (ArrayList<Map<String, Object>>) data.get("dishonestPersons");
//        if (CollectionUtils.isNotEmpty(dishonestPersons)) {
//            dishonestPersons.forEach(person -> {
//                person.replaceAll((k, v) -> v.equals("") ? "-" : v);
//            });
//            data.put("dishonestPersonsCount", dishonestPersons.size());
//        } else {
//            Map<String, Object> defaultDishonestPerson = new HashMap<>();
//            defaultDishonestPerson.put("id", "1");
//            defaultDishonestPerson.put("inDate", "-");
//            defaultDishonestPerson.put("caseNumber", "-");
//            defaultDishonestPerson.put("amount", "-");
//            defaultDishonestPerson.put("executionDesc", "-");
//            defaultDishonestPerson.put("executionStatus", "-");
//            defaultDishonestPerson.put("court", "-");
//            defaultDishonestPerson.put("disabled", "-");
//            List<Map<String, Object>> defaultDishonestPersonList = Collections.singletonList(defaultDishonestPerson);
//            data.put("dishonestPersons", defaultDishonestPersonList);
//            data.put("dishonestPersonsCount", 0);
//        }
//
//        // 处理经营异常数据
//        ArrayList<Map<String, Object>> businessAbnormals =
//            (ArrayList<Map<String, Object>>) data.get("businessAbnormals");
//        if (CollectionUtils.isNotEmpty(businessAbnormals)) {
//            businessAbnormals.forEach(item -> {
//                item.replaceAll((k, v) -> v.equals("") ? "-" : v);
//            });
//            data.put("businessAbnormalsCount", businessAbnormals.size());
//        } else {
//            Map<String, Object> defaultBusinessAbnormal = new HashMap<>();
//            defaultBusinessAbnormal.put("id", "1");
//            defaultBusinessAbnormal.put("inDate", "-");
//            defaultBusinessAbnormal.put("department", "-");
//            defaultBusinessAbnormal.put("inReason", "-");
//            defaultBusinessAbnormal.put("disabled", "-");
//            List<Map<String, Object>> defaultBusinessAbnormalList = Collections.singletonList(defaultBusinessAbnormal);
//            data.put("businessAbnormals", defaultBusinessAbnormalList);
//            data.put("businessAbnormalsCount", 0);
//        }
//
//        // 处理行政处罚记录
//        ArrayList<Map<String, Object>> penaltyRecords = (ArrayList<Map<String, Object>>) data.get("penaltyRecords");
//        if (CollectionUtils.isNotEmpty(penaltyRecords)) {
//            penaltyRecords.forEach(item -> {
//                item.replaceAll((k, v) -> v.equals("") ? "-" : v);
//            });
//            data.put("penaltyRecordsCount", penaltyRecords.size());
//        } else {
//            Map<String, Object> defaultPenaltyRecord = new HashMap<>();
//            defaultPenaltyRecord.put("id", "1");
//            defaultPenaltyRecord.put("seqNo", "-");
//            defaultPenaltyRecord.put("number", "-");
//            defaultPenaltyRecord.put("penaltyData", "-");
//            defaultPenaltyRecord.put("content", "-");
//            defaultPenaltyRecord.put("punishAmnt", "-");
//            defaultPenaltyRecord.put("department", "-");
//            defaultPenaltyRecord.put("description", "-");
//            List<Map<String, Object>> defaultPenaltyRecordList = Collections.singletonList(defaultPenaltyRecord);
//            data.put("penaltyRecords", defaultPenaltyRecordList);
//            data.put("penaltyRecordsCount", 0);
//        }
//
//        // 处理税务处罚记录
//        ArrayList<Map<String, Object>> taxPenalties = (ArrayList<Map<String, Object>>) data.get("taxPenalties");
//        if (CollectionUtils.isNotEmpty(taxPenalties)) {
//            taxPenalties.forEach(item -> {
//                item.replaceAll((k, v) -> v.equals("") ? "-" : v);
//            });
//            data.put("taxPenaltiesCount", taxPenalties.size());
//        } else {
//            Map<String, Object> defaultTaxPenalty = new HashMap<>();
//            defaultTaxPenalty.put("id", "1");
//            defaultTaxPenalty.put("time", "-");
//            defaultTaxPenalty.put("type", "-");
//            defaultTaxPenalty.put("result", "-");
//            List<Map<String, Object>> defaultTaxPenaltyList = Collections.singletonList(defaultTaxPenalty);
//            data.put("taxPenalties", defaultTaxPenaltyList);
//            data.put("taxPenaltiesCount", 0);
//        }
//
//        // 处理严重违法记录
//        ArrayList<Map<String, Object>> severeViolations = (ArrayList<Map<String, Object>>) data.get("severeViolations");
//        if (CollectionUtils.isNotEmpty(severeViolations)) {
//            severeViolations.forEach(item -> {
//                item.replaceAll((k, v) -> v.equals("") ? "-" : v);
//            });
//            data.put("severeViolationsCount", severeViolations.size());
//        } else {
//            Map<String, Object> defaultSevereViolation = new HashMap<>();
//            defaultSevereViolation.put("id", "1");
//            defaultSevereViolation.put("time", "-");
//            defaultSevereViolation.put("decisionDept", "-");
//            defaultSevereViolation.put("inReason", "-");
//            defaultSevereViolation.put("isHistory", "-");
//            List<Map<String, Object>> defaultSevereViolationList = Collections.singletonList(defaultSevereViolation);
//            data.put("severeViolations", defaultSevereViolationList);
//            data.put("severeViolationsCount", 0);
//        }
//    }
//
//
//}
