package com.dsk.jsk.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dsk.acc.openapi.client.util.CommonUtils;
import com.dsk.common.constant.CacheConstants;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.utils.EncodeIdUtil;
import com.dsk.common.utils.StringUtils;
import com.dsk.common.utils.redis.RedisUtils;
import com.dsk.cscec.service.ICustomerInfoService;
import com.dsk.jsk.domain.*;
import com.dsk.jsk.domain.vo.QualificationVo;
import com.dsk.system.utils.DskOpenApiUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.*;

/**
 * @ClassName enterpriseService
 * @Description TODO
 * @Author zhangyi
 * @Date 2023/5/15 12:00
 * @Version 1.0
 */
@Service
public class EnterpriseService {

    private static final Logger log = LoggerFactory.getLogger(EnterpriseService.class);

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    @Autowired
    private ICustomerInfoService iCustomerInfoService;

    @Autowired
    private CompanyRelationTableV1Service relationTableV1Service;

    //开放平台返回的企业资质资格接口字段
    private static final Map<String, String> qualificationMap = new HashMap<>();

    static {
        qualificationMap.put("construction", "建筑业企业资质");
        //接口文档写的"工程设计资质",正确的是"工程设计"
        qualificationMap.put("engineeringDesign", "工程设计");
        //接口文档写的"工程勘察资质",正确的是"工程勘察"
        qualificationMap.put("engineeringInvestigation", "工程勘察");
        qualificationMap.put("engineeringSupervision", "工程监理");
        qualificationMap.put("costConsultation", "造价咨询企业资质");
        qualificationMap.put("designConstruction", "设计施工一体化企业资质");
        qualificationMap.put("biddingAgency", "招标代理机构资质");
        qualificationMap.put("constructionLabor", "施工劳务资质");
        qualificationMap.put("powerFacilityLicense", "承装（修、试）电力设施许可证");
        //接口文档写的"房地产估价资质",正确的是"房地产估价机构资质"
        qualificationMap.put("realEstateAppraisal", "房地产估价资质");
        qualificationMap.put("cqHighwayMaintenance", "重庆公路养护资质");
        qualificationMap.put("blastingUnit", "爆破从业单位资质");
        qualificationMap.put("exhibitionEngineering", "展览工程");
        qualificationMap.put("culturalRelicsProtection", "文物保护资质");
        qualificationMap.put("geologicalDisasterControl", "地质灾害治理工程");
        qualificationMap.put("securityEngineering", "安防工程资质");
        qualificationMap.put("realEstateDevelopment", "房地产开发资质");
        qualificationMap.put("engineeringConsultingUnit", "工程咨询资信单位资质");
        qualificationMap.put("mapping", "测绘资质");
        qualificationMap.put("urbanPlanning", "城乡规划编制资质");
        qualificationMap.put("waterResourcesSurvey", "水文、水资源调查评价资质");
        qualificationMap.put("systemIntegration", "信息系统集成及服务");
        qualificationMap.put("fireFightingTechnology", "消防技术服务机构");
        qualificationMap.put("waterInspection", "公路水运工程试验检测机构");
        qualificationMap.put("transportationSafety", "交通运输企业安全生产标准化");
        qualificationMap.put("waterSafety", "水利安全生产标准化企业");
        qualificationMap.put("itss", "itss信息技术服务标准");
        qualificationMap.put("powerLicense", "电力业务许可证");
        qualificationMap.put("specialEquipment", "特种设备安装改造维修许可");
        qualificationMap.put("placeOfFiling", "备案地");
        qualificationMap.put("geology", "地质勘查资质");
        qualificationMap.put("roadCuring", "公路养护从业资格");
        qualificationMap.put("websiteOfFiling", "备案网站");
        //接口文档写的"博物馆资质",正确的是"博物馆陈列展览资质"
        qualificationMap.put("museum", "博物馆陈列展览资质");
        qualificationMap.put("safeCert", "安全生产许可证");
        qualificationMap.put("mediumLess", "缺人资质");
        qualificationMap.put("certificateChange", "资质变更");
    }

    public R label(EnterpriseInfoLabelBody body) throws Exception {
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (body.isVaildCid()) {
            return R.ok(mapList);
        }
        Map<String, Object> companyMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/label", BeanUtil.beanToMap(body, false, false));
        Integer companyCode = MapUtils.getInteger(companyMap, "code", 300);
        if (200 != companyCode) {
            return R.ok(mapList);
        }
        // list
        List<Map<String, Object>> data = (List<Map<String, Object>>) companyMap.get("data");
        if (ObjectUtil.isNotEmpty(data)) {
            for (Map<String, Object> labelsMap : data) {
                String labelName = MapUtils.getString(labelsMap, "labelName", "");
                if (labelName.equals("央企企业") || labelName.equals("国企") || labelName.equals("央企") ||
                        labelName.equals("央企子公司") || labelName.equals("国有企业") || labelName.equals("省属国企")) {
                    mapList.add(labelsMap);
                }
            }
        }
        return R.ok(mapList);
    }

    public R infoHeader(EnterpriseInfoHeaderBody body) throws Exception {
        if (body.isVaildCid()) {
            return R.ok();
        }
        Map<String, Object> companyMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/infoHeader", BeanUtil.beanToMap(body, false, false));
        Integer companyCode = MapUtils.getInteger(companyMap, "code", 300);
        if (200 != companyCode) {
            return R.ok();
        }

        Map companyData = MapUtils.getMap(companyMap, "data", null);
        if (ObjectUtil.isEmpty(companyData)) {
            return R.ok();
        }
        companyData.put("uipId", null);
        companyData.put("claimStatus", 0);
        companyData.put("bratingSubjectLevel", null);
        String companyName = MapUtils.getString(companyData, "companyName", "");

        Map<String, Object> map = BeanUtil.beanToMap(body, false, false);
        map.put("cid", body.getCompanyId());

        Map<String, Object> uipMap = dskOpenApiUtil.requestBody("/operate/enterprise/financial", map);
        Integer uipCode = MapUtils.getInteger(uipMap, "code", 300);
        if (200 != uipCode) return R.ok(companyData);

        Map<String, Object> bondCreditRatingMap = dskOpenApiUtil.requestBody("/operate/enterprise/bondCreditRating", map);
        Integer bondCreditRatingCode = MapUtils.getInteger(bondCreditRatingMap, "code", 300);
        if (200 != bondCreditRatingCode) return R.ok(companyData);

        List<Map<String, Object>> bondCreditRatingList = (List<Map<String, Object>>) MapUtils.getObject(bondCreditRatingMap, "data", new ArrayList<>());
        if (CollectionUtils.isEmpty(bondCreditRatingList)) return R.ok(companyData);

        companyData.put("bratingSubjectLevel", MapUtils.getString(bondCreditRatingList.get(0), "bratingSubjectLevel", null));

        return R.ok(companyData);
    }

    public R statistic(EnterpriseStatisticBody body) throws Exception {
        if (body.isVaildCid()) {
            return R.ok();
        }
        Map<String, Object> statisticMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/statistic", BeanUtil.beanToMap(body, false, false));

        Integer statisticMapCode = MapUtils.getInteger(statisticMap, "code", 300);
        if (200 != statisticMapCode) {
            return R.ok();
        }

        Map statisticMapData = MapUtils.getMap(statisticMap, "data", null);
        if (MapUtils.isEmpty(statisticMapData)) {
            return R.ok();
        }

        //处理资质资格
        Map qualification = MapUtils.getMap(statisticMapData, "qualification");
        ArrayList<QualificationVo> qualificationVoList = new ArrayList<>();
        Set<Map.Entry<String, Long>> entries = qualification.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            String paramName = entry.getKey();
            if (
                //企业没有的资质不做展示
                    entry.getValue() == 0
                            //qualification字段表示资质资格数量,无需返回
                            || paramName.contains("qualification")
                            || paramName.contains("companyId")
                            //如果返回字段中包含V1版本,则跳过该字段取该字段的V1版本
                            //eg:xxx,xxxV1
                            || qualification.containsKey(paramName + "V1")
                            || qualification.containsKey(paramName + "V2")) {
                continue;
            }
            //字段对应中文名
            String paramNameCn = qualificationMap.get(paramName.contains("V1") ? paramName.replace("V1", "") : paramName.contains("V2") ? paramName.replace("V2", "") : paramName);
            //给字段填充中文名
            if (StringUtils.isNoneBlank(paramNameCn)) {
                qualificationVoList.add(new QualificationVo(paramName, paramNameCn, entry.getValue()));
            } else {
                //映射不上的是接口文档中没写,只有拿着paramName找张毅问
                qualificationVoList.add(new QualificationVo(paramName, null, entry.getValue()));
            }
        }
        statisticMapData.put("newQualification", qualificationVoList);

        //内部合作
        Map<String, Object> cooperation = iCustomerInfoService.cooperationStatistic(body.getCompanyName());
        statisticMapData.put("cooperation", cooperation);

        Map performance = MapUtils.getMap(statisticMapData, "performance");
        Map business = MapUtils.getMap(statisticMapData, "business");

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("cid", body.getCompanyId());
        Map<String, Object> projectStatisticMap = dskOpenApiUtil.requestBody("/operate/enterpriseProject/statistic", paramMap);
        Integer projectStatisticMapCode = MapUtils.getInteger(projectStatisticMap, "code", 300);
        if (200 != projectStatisticMapCode) {
            return R.ok(statisticMapData);
        }

        Map projectStatisticMapData = MapUtils.getMap(projectStatisticMap, "data", null);

        performance.put("specialDebt", MapUtils.getInteger(projectStatisticMapData, "specialDebt", 0));
        performance.put("bidPlan", MapUtils.getInteger(projectStatisticMapData, "bidPlan", 0));
        business.put("historySend", MapUtils.getInteger(projectStatisticMapData, "historySend", 0));
        business.put("importantBusiness", MapUtils.getInteger(projectStatisticMapData, "importantBusiness", 0));

        statisticMapData.put("performance", performance);
        statisticMapData.put("business", business);

        Map<String, Object> combineMemberMap = dskOpenApiUtil.requestBody("/operate/enterprise/combineMember", paramMap);
        Integer code = MapUtils.getInteger(combineMemberMap, "code", 300);
        statisticMapData.put("combineMember", null);
        if (code.equals(HttpStatus.OK.value())) {
            statisticMapData.put("combineMember", MapUtils.getMap(combineMemberMap, "data", null));
        }
        // 咨询业务往来
        JskCompanyRelationTableV1Dto v1Dto = new JskCompanyRelationTableV1Dto();
        Map<String, Object> businessTransactionsMap = new HashMap<>();
        v1Dto.setCompanyId(body.getCompanyId());
        v1Dto.setCompanyType(1);
        TableDataInfo page1 = relationTableV1Service.page(v1Dto);
        long total1 = page1.getTotal();
        businessTransactionsMap.put("owner", total1);
        v1Dto.setCompanyType(2);
        TableDataInfo page2 = relationTableV1Service.page(v1Dto);
        long total2 = page2.getTotal();
        businessTransactionsMap.put("construction", total2);
        v1Dto.setCompanyType(3);
        TableDataInfo page3 = relationTableV1Service.page(v1Dto);
        long total3 = page3.getTotal();
        businessTransactionsMap.put("combine", total3);
        if (total1 + total2 + total3 > 0) {
            statisticMapData.put("businessTransactions", businessTransactionsMap);
        } else {
            statisticMapData.put("businessTransactions", null);
        }
        return R.ok(statisticMapData);
    }

    public R bidDataGroup(EnterpriseBidDataGroupBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/bidDataGroup", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R projectTenderDataGroup(EnterpriseProjectTenderDataGroupBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/projectTenderDataGroup", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    //todo 新数据，待入库
    public TableDataInfo partners(EnterprisePartnersBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/partners", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo investment(EnterpriseInvestmentBody body) throws Exception {
        if (body.isVaildCid()) {
            return new TableDataInfo(new ArrayList<>(), 0);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/investment", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);

//        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/investment", BeanUtil.beanToMap(body, false, false));
//        Integer code = MapUtils.getInteger(map, "code", 300);
//        if (!code.equals(HttpStatus.OK.value())) {
//            throw new RuntimeException();
//        }
//        Map data = MapUtils.getMap(map, "data", null);
//        List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(data, "list", ""));
//        if (CollectionUtils.isEmpty(list)) {
//            return new TableDataInfo(list, 0);
//        }
//        List<Object> newList = new ArrayList<>(20);
//        for (Object companyObj : list) {
//            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
//            String businessStatus = MapUtils.getString(companyMap, "businessStatus");
//            if (!"注销".equals(businessStatus)) {
//                newList.add(companyMap);
//            }
//        }
//        for (Object companyObj : list) {
//            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
//            String businessStatus = MapUtils.getString(companyMap, "businessStatus");
//            if ("注销".equals(businessStatus)) {
//                newList.add(companyMap);
//            }
//        }
//        return new TableDataInfo(newList, MapUtils.getInteger(data, "totalCount", 0));
    }

    public TableDataInfo affiliates(EnterpriseAffiliatesBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/affiliates", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo dynamicPage(EnterpriseDynamicPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/api/jsk/enterprise/dynamic", BeanUtil.beanToMap(body, false, false));
        //底层接口写死了pageSize为10，这里前端只要5条
//        return dskOpenApiUtil.responsePage(map);

        Integer code = MapUtils.getInteger(map, "code", 300);
        Map data = MapUtils.getMap(map, "data", null);
        if (200 != code) throw new RuntimeException();
        Integer count = MapUtils.getInteger(data, "totalCount", 0);
        List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(data, "list", ""));

        return new TableDataInfo(body.getPageSize() <= list.size() ? list.subList(0, body.getPageSize()) : list, count);
    }

    public R icInfo(EnterpriseIcInfoBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/icInfo", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo changeInfo(EnterpriseChangeInfoBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/changeInfo", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo keymembers(EnterpriseKeymembersBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/keymembers", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo bestStockPage(EnterpriseBestStockPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/bestStockPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public R financial(EnterpriseFinancialBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/financial", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R getUipId(String companyName) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("companyName", companyName);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/getUipId", params);
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo uipSerach(EnterpriseUipSearchBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/uipSerach", BeanUtil.beanToMap(body, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (200 != code) throw new RuntimeException();

        Map data = MapUtils.getMap(map, "data", null);
        List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(data, "list", ""));

        if (CollectionUtils.isEmpty(list)) {
            return new TableDataInfo(list, 0);
        }

        ArrayList<String> companyNames = new ArrayList<>();
        ArrayList<Long> cIds = new ArrayList<>();
        for (Object dataMap : list) {
            String name = MapUtils.getString(CommonUtils.assertAsMap(dataMap), "companyName");
            if (ObjectUtil.isNotEmpty(name) && ObjectUtil.isNotEmpty(body.getKeys())) {
                name = name.replaceAll("<font color='red'>", "");
                name = name.replaceAll("</font>", "");
                companyNames.add(name);
            } else {
                companyNames.add(name);
            }
            cIds.add(MapUtils.getLong(CommonUtils.assertAsMap(dataMap), "companyId"));
        }

        //按照城投企业id合并两个list
        for (Object companyObj : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
//            companyMap.put("bratingSubjectLevel", null);
            String uipId = MapUtils.getString(companyMap, "uipId");
            String companyName = MapUtils.getString(companyMap, "companyName", "");
            if (ObjectUtil.isNotEmpty(companyName) && ObjectUtil.isNotEmpty(body.getKeys())) {
                companyName = companyName.replace("<font color='red'>", "");
                companyName = companyName.replace("</font>", "");
            }
            Long companyId = MapUtils.getLong(companyMap, "companyId");
            companyMap.put("bondBalance", MapUtils.getDouble(companyMap, "bondBalance") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "bondBalance")));
            companyMap.put("totalAssets", MapUtils.getDouble(companyMap, "totalAssets") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "totalAssets")));
            companyMap.put("belongNetAssets", MapUtils.getDouble(companyMap, "belongNetAssets") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "belongNetAssets")));
            companyMap.put("monetaryFunds", MapUtils.getDouble(companyMap, "monetaryFunds") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "monetaryFunds")));
            companyMap.put("landAssets", MapUtils.getDouble(companyMap, "landAssets") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "landAssets")));
            companyMap.put("restrictedAssets", MapUtils.getDouble(companyMap, "restrictedAssets") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "restrictedAssets")));
            companyMap.put("accountsReceivable", MapUtils.getDouble(companyMap, "accountsReceivable") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "accountsReceivable")));
            companyMap.put("otherReceivable", MapUtils.getDouble(companyMap, "otherReceivable") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "otherReceivable")));
            companyMap.put("econData_001", MapUtils.getDouble(companyMap, "econData_001") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "econData_001")));
            companyMap.put("govSubsidy", MapUtils.getDouble(companyMap, "govSubsidy") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "govSubsidy")));
            companyMap.put("specialPayable", MapUtils.getDouble(companyMap, "specialPayable") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "specialPayable")));
            companyMap.put("operatingIncome", MapUtils.getDouble(companyMap, "operatingIncome") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "operatingIncome")));
            companyMap.put("belongNetProfit", MapUtils.getDouble(companyMap, "belongNetProfit") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "belongNetProfit")));
            companyMap.put("roe", MapUtils.getDouble(companyMap, "roe") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "roe")));
            companyMap.put("netOperatingCashFlow", MapUtils.getDouble(companyMap, "netOperatingCashFlow") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "netOperatingCashFlow")));
            companyMap.put("netFinancingCashFlow", MapUtils.getDouble(companyMap, "netFinancingCashFlow") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "netFinancingCashFlow")));
            companyMap.put("netInvestmentCashFlow", MapUtils.getDouble(companyMap, "netInvestmentCashFlow") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "netInvestmentCashFlow")));
            companyMap.put("totalLiabilities", MapUtils.getDouble(companyMap, "totalLiabilities") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "totalLiabilities")));
            companyMap.put("uipInterestBearingDebt", MapUtils.getDouble(companyMap, "uipInterestBearingDebt") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "uipInterestBearingDebt")));
            companyMap.put("econData_002", MapUtils.getDouble(companyMap, "econData_002") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "econData_002")));
            companyMap.put("ofcb", MapUtils.getDouble(companyMap, "ofcb") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "ofcb")));
            companyMap.put("cashRatio", MapUtils.getDouble(companyMap, "cashRatio") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "cashRatio")));
            companyMap.put("cashFlowRatio", MapUtils.getDouble(companyMap, "cashFlowRatio") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "cashFlowRatio")));
            companyMap.put("cashDebtRatio", MapUtils.getDouble(companyMap, "cashDebtRatio") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "cashDebtRatio")));
            companyMap.put("creditBalance", MapUtils.getDouble(companyMap, "creditBalance") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "creditBalance")));
            companyMap.put("econData_003", MapUtils.getDouble(companyMap, "econData_003") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "econData_003")));
            companyMap.put("ebitdaIcr", MapUtils.getDouble(companyMap, "ebitdaIcr") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "ebitdaIcr")));
            companyMap.put("ppnBalanceProp", MapUtils.getDouble(companyMap, "ppnBalanceProp") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "ppnBalanceProp")));
            companyMap.put("econData_004", MapUtils.getDouble(companyMap, "econData_004") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "econData_004")));
            companyMap.put("econData_005", MapUtils.getDouble(companyMap, "econData_005") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "econData_005")));
            companyMap.put("econData_006", MapUtils.getDouble(companyMap, "econData_006") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "econData_006")));
            companyMap.put("nonStandardBalance", MapUtils.getDouble(companyMap, "nonStandardBalance") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "nonStandardBalance")));
            companyMap.put("nonStandardRatio", MapUtils.getDouble(companyMap, "nonStandardRatio") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "nonStandardRatio")));
            companyMap.put("guaranteeAmount", MapUtils.getDouble(companyMap, "guaranteeAmount") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "guaranteeAmount")));
            companyMap.put("guaranteeRatio", MapUtils.getDouble(companyMap, "guaranteeRatio") == null ? null : String.format("%.2f", MapUtils.getDouble(companyMap, "guaranteeRatio")));


//            for (Map<String, Object> m : objArrayList) {
//                Long companyIdCredit = MapUtils.getLong(m, "companyId", 0L);
//                if (companyIdCredit.equals(companyId)) {
//                    companyMap.put("bratingSubjectLevel", m.get("bratingSubjectLevel"));
//                }
//            }
        }

        return new TableDataInfo(list, MapUtils.getInteger(data, "totalCount", 0));
    }

    public static void main(String[] args) {
        String str = String.format("%1$.2f", 0.0456);
        System.out.println(str);

        String format = String.format("%.2f", null);
        System.out.println(format);
    }

    public R uipGroupData() throws Exception {
        String redisKey = CacheConstants.DATA_UIPGROUPDATA;
        Map<String, Object> cacheMap = RedisUtils.getCacheMap(redisKey);
        if (MapUtils.isNotEmpty(cacheMap)) {
            return R.ok(cacheMap);
        }

        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/uipGroupData", new HashMap<>());
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (200 != code) throw new RuntimeException();

        Map data = MapUtils.getMap(map, "data", null);
        RedisUtils.setCacheMap(redisKey, data);
        RedisUtils.expire(redisKey, Duration.ofHours(24));

        return BeanUtil.toBean(map, R.class);
    }

    public R remark(EnterpriseRemarkBody vo) throws Exception {
        return R.ok("操作成功", EncodeIdUtil.avDecode(EncodeIdUtil.binaryToUnicode(vo.mark)));
    }

    public R bondCreditRating(EnterpriseBondCreditRatingBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/bondCreditRating", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R getUipIdByCid(List<Integer> body) throws Exception {
        if (CollectionUtils.isEmpty(body)) return R.ok();
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("ids", body);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/getUipIdByCid", paramMap);
        return BeanUtil.toBean(map, R.class);
    }


    public R financialData(EnterpriseFinancialDataBody body) throws Exception {
        if (body.isValidateCid()) {
            return R.ok();
        }
        // TODO 缓存需要
//        String redisKey = CacheConstants.DATA_FINANCIAL + body.getCid();
//        List cacheMap = RedisUtils.getCacheList(redisKey);
//        if (ObjectUtil.isNotEmpty(cacheMap)) {
//            return R.ok(cacheMap);
//        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/financialData", BeanUtil.beanToMap(body, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (!code.equals(HttpStatus.OK.value())) {
            throw new RuntimeException();
        }

//        Object data = map.get("data");
//        if (ObjectUtil.isNotEmpty(data)) {
//            RedisUtils.setCacheList(redisKey, (List) data);
//            RedisUtils.expire(redisKey,  Duration.ofHours(24));
//        }

        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo certPage(EnterpriseInfoCertPageBody body) throws Exception {
        body.setPageSize(1000);
        if (body.isVaildCid()) {
            return new TableDataInfo(new ArrayList<>(), 0);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCert/certPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo certChangePage(EnterpriseCertBody body) throws Exception {
        body.setPageSize(1000);
        if (body.isVaildCid()) {
            return new TableDataInfo(new ArrayList<>(), 0);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCert/certChangePage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public R liceList(EnterpriseCertBody body) throws Exception {
        if (body.isVaildCid()) {
            return R.ok();
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCert/liceList", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R recordWebSiteList(EnterpriseCertBody body) throws Exception {
        if (body.isVaildCid()) {
            return R.ok();
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCert/recordWebSiteList", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R recordAreaList(EnterpriseCertBody body) throws Exception {
        if (body.isVaildCid()) {
            return R.ok();
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseCert/recordAreaList", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

}
