<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zksy.system.basicData.mapper.CrmHighTechZoneEnterprisesMapper">

    <resultMap type="CrmHighTechZoneEnterprises" id="CrmHighTechZoneEnterprisesResult">
        <result property="id"    column="id"    />
        <result property="enterpriseName"    column="enterprise_name"    />
        <result property="unifiedSocialCreditCode"    column="unified_social_credit_code"    />
        <result property="enterpriseLeader"    column="enterprise_leader"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
    </resultMap>

    <select id="getAllTableNames" resultType="java.util.LinkedHashMap">
        SELECT table_name AS tableName, table_comment AS tableComment
        FROM information_schema.tables
        WHERE table_schema = 'credit_rating_management'
        <if test="tableNames != null and tableNames.size() > 0">
            AND table_name IN
            <foreach item="tableName" index="index" collection="tableNames" open="(" separator="," close=")">
                #{tableName}
            </foreach>
        </if>
    </select>

    <select id="getTableComment" resultType="java.lang.String">
        select table_comment
        FROM information_schema.tables
        WHERE table_schema = 'credit_rating_management' and table_name = #{tableName}
    </select>

    <select id="getColumnNames" resultType="string">
        select column_name
        from information_schema.columns
        where table_name = #{tableName} and table_schema = 'credit_rating_management'
        order by ordinal_position;
    </select>

    <select id="getColumnComment" resultType="java.lang.String">
        select COLUMN_COMMENT
        from information_schema.columns
        where table_name = #{tableName} and table_schema = 'credit_rating_management' and COLUMN_NAME = #{columnName}
    </select>

    <select id="getTableData" resultType="java.util.List">
        select *
        from ${tableName}
        where unified_social_credit_code LIKE CONCAT('%', #{unifiedSocialCreditCode}, '%');
    </select>

    <select id="getTableDataMap" resultType="java.util.LinkedHashMap">
        select *
        from ${tableName}
        where unified_social_credit_code LIKE CONCAT('%', #{unifiedSocialCreditCode}, '%');
    </select>

    <select id="referenceCreditRating" resultType="com.zksy.system.basicData.model.respone.CreditRatingEntityDto">

        SELECT
        e.unified_social_credit_code AS unifiedSocialCreditCode,
        e.enterprise_name AS enterpriseName,
        COALESCE(
        CASE
        WHEN cnpcei.evaluation_grade LIKE '%优%'  THEN 2
        WHEN cnpcei.evaluation_grade LIKE '%良%' THEN 1.6
        WHEN cnpcei.evaluation_grade LIKE '%中%' THEN 1.2
        WHEN cnpcei.evaluation_grade LIKE '%差%' THEN 0
        ELSE 2
        END, 2
        ) AS nationalPublicCreditScore,
        -- 添加税收评级得分
        COALESCE(
        CASE WHEN etc.tax_credit_rating = 'A' THEN 1
        WHEN etc.tax_credit_rating = 'B' THEN 0.8
        WHEN etc.tax_credit_rating ='M' THEN 0.6
        WHEN etc.tax_credit_rating ='C' THEN 0.4
        WHEN etc.tax_credit_rating = 'D' THEN 0
        END, 0
        ) AS taxRatingScore,
        -- 添加银行信用评级得分
        COALESCE(
        CASE WHEN bcr.bank_credit_rating = 'A' THEN 1
        WHEN bcr.bank_credit_rating = 'B' THEN 0.8
        WHEN bcr.bank_credit_rating = 'C' THEN 0.6
        WHEN bcr.bank_credit_rating = 'D' THEN 0
        END, 1 -- 默认值，无记录的企业
        ) AS bankCreditRatingScore,
        -- 市场监管信用评级得分
        COALESCE(
        CASE WHEN mcr.credit_rating = 'A' THEN 1
        WHEN mcr.credit_rating = 'B' THEN 0.8
        WHEN mcr.credit_rating = 'C' THEN 0.6
        WHEN mcr.credit_rating = 'D' THEN 0
        END, 1 -- 默认值，无记录的企业
        ) AS marketSupervisionRatingScore,
        COALESCE(
        CASE
        WHEN mcr.credit_rating = '环保诚信单位' THEN 1
        WHEN mcr.credit_rating = '环保合格单位' THEN 0.8
        END, 0.6
        ) AS environmentalCreditRatingScore,
        COALESCE(
        CASE
        WHEN cnpcei.evaluation_grade = 'AAA' THEN 2
        WHEN cnpcei.evaluation_grade = 'AA' THEN 1.6
        WHEN cnpcei.evaluation_grade = 'A' THEN 1.2
        ELSE 2
        END, 2
        ) AS otherFieldsCreditAppraisalScore,
        COALESCE(ecr.rating_score * 0.01, 0) AS licensedCreditAgencyScore, -- rating_score值的1%作为得分，无记录则默认0
        -- 评级机构信用评级得分
        COALESCE(
        CASE
        WHEN rai.credit_rating = 'AAA' THEN 1
        WHEN rai.credit_rating = 'AA' THEN 0.9
        WHEN rai.credit_rating = 'A' THEN 0.8
        ELSE 0.8
        END, 0.8
        ) AS ratingAgencyCreditRating,
        -- 计算总得分
        (
        COALESCE(
        CASE
        WHEN cnpcei.evaluation_grade = 'AAA' THEN 2
        WHEN cnpcei.evaluation_grade = 'AA' THEN 1.6
        WHEN cnpcei.evaluation_grade = 'A' THEN 1.2
        ELSE 2
        END, 2
        )
        + COALESCE(
        CASE WHEN etc.tax_credit_rating = 'A' THEN 1
        WHEN etc.tax_credit_rating = 'B' THEN 0.8
        WHEN etc.tax_credit_rating ='M' THEN 0.6
        WHEN etc.tax_credit_rating ='C' THEN 0.4
        WHEN etc.tax_credit_rating = 'D' THEN 0
        END, 0
        )+ COALESCE(
        CASE WHEN bcr.bank_credit_rating = 'A' THEN 1
        WHEN bcr.bank_credit_rating = 'B' THEN 0.8
        WHEN bcr.bank_credit_rating = 'C' THEN 0.6
        WHEN bcr.bank_credit_rating = 'D' THEN 0
        END, 1
        )+ COALESCE(
        CASE WHEN mcr.credit_rating = 'A' THEN 1
        WHEN mcr.credit_rating = 'B' THEN 0.8
        WHEN mcr.credit_rating = 'C' THEN 0.6
        WHEN mcr.credit_rating = 'D' THEN 0
        END, 1 -- 默认值，无记录的企业
        )+COALESCE(
        CASE
        WHEN mcr.credit_rating = '环保诚信单位' THEN 1
        WHEN mcr.credit_rating = '环保合格单位' THEN 0.8
        END, 0.6
        )
        +COALESCE(
        CASE
        WHEN cnpcei.evaluation_grade = 'AAA' THEN 2
        WHEN cnpcei.evaluation_grade = 'AA' THEN 1.6
        WHEN cnpcei.evaluation_grade = 'A' THEN 1.2
        ELSE 2
        END, 2
        )
        + COALESCE(ecr.rating_score * 0.01, 0)
        + COALESCE(
        CASE
        WHEN rai.credit_rating = 'AAA' THEN 1
        WHEN rai.credit_rating = 'AA' THEN 0.9
        WHEN rai.credit_rating = 'A' THEN 0.8
        ELSE 0.8
        END, 0.8
        )

        ) AS total
        FROM
        crm_high_tech_zone_enterprises AS e
        LEFT JOIN
        crm_national_public_credit_evaluation_information cnpcei ON e.unified_social_credit_code = cnpcei.unified_social_credit_code
        LEFT JOIN
        crm_business_license_information AS bl ON e.unified_social_credit_code = bl.unified_social_credit_code
        LEFT JOIN
        crm_enterprise_tax_classification_info AS etc ON e.unified_social_credit_code = etc.unified_social_credit_code -- 新增JOIN税收分类信息表
        LEFT JOIN
        crm_bank_credit_rating AS bcr ON e.unified_social_credit_code = bcr.unified_social_credit_code -- 新增JOIN银行信用评级表
        LEFT JOIN
        crm_market_regulatory_credit_rating AS mcr ON e.unified_social_credit_code = mcr.unified_social_credit_code -- 新增JOIN市场监管信用评级表
        LEFT JOIN
        crm_market_regulatory_credit_rating cmrcr ON e.unified_social_credit_code = cmrcr.unified_social_credit_code
        LEFT JOIN
        crm_enterprise_credit_rating AS ecr ON e.unified_social_credit_code = ecr.unified_social_credit_code -- 新增JOIN企业信用评分表
        LEFT JOIN
        crm_rating_agency_info AS rai ON e.unified_social_credit_code = rai.unified_social_credit_code -- 新增JOIN评级机构信息表
        <where>
            <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
                and e.unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
            </if>
            <if test="enterpriseName !=null and enterpriseName != ''">
                and e.enterprise_name like concat('%', #{enterpriseName}, '%')
            </if>
        </where>
        GROUP BY
        e.unified_social_credit_code -- 根据企业代码分组
        ORDER BY
        total DESC
    </select>
    <select id="regulatoryPenalty" resultType="com.zksy.system.basicData.model.respone.RegulatoryPenaltyDto">
        SELECT
        e.unified_social_credit_code AS unifiedSocialCreditCode,
        e.enterprise_name AS enterpriseName,-- 计算扣分
        CASE
        WHEN COUNT(cba.unified_social_credit_code) > 2 THEN -5
        ELSE -LEAST(COUNT(cba.unified_social_credit_code), 2) * 2
        END AS marketRegulatoryOperationExceptionDeduction,
        -LEAST(COUNT(cef.unified_social_credit_code), 2) * 1 AS marketRegulatoryEquityFreezeDeduction,
        -LEAST(COUNT(cpp.unified_social_credit_code), 2) * 1 AS marketRegulatoryPriceActionDeduction,
        -LEAST(COUNT(cii.unified_social_credit_code), 2) * 1 AS marketRegulatoryIntellectualPropertyInfringementDeduction,
        -LEAST(COUNT(cpr.unified_social_credit_code), 2) * 1 AS marketRegulatoryProductRecallDeduction,
        -LEAST(COUNT(cmpm.unified_social_credit_code), 2) * 1 AS marketRegulatoryMovablePropertyMortgageDeduction,
        -LEAST(COUNT(cepi.unified_social_credit_code), 2) * 1 AS marketRegulatoryEquityPledgeDeduction,
        -LEAST(COUNT(cmr.unified_social_credit_code), 2) * 1 AS marketRegulatoryOtherPenaltiesDeduction,
        -LEAST(COUNT(cta.unified_social_credit_code), 2) * 1 AS taxPenaltyTaxArrearsAnnouncementDeduction,
        -LEAST(COUNT(ctu.unified_social_credit_code), 2) * 1 AS taxPenaltyTaxUrgentNoticeDeduction,
        -LEAST(COUNT(cca.unified_social_credit_code), 2) * 1 AS taxPenaltyOrderForRemovalDeduction,
        -LEAST(COUNT(cop.unified_social_credit_code), 1) * 1 AS taxPenaltyOtherPenaltiesDeduction,
        CASE
        WHEN COUNT(ckp.unified_social_credit_code) > 2 THEN -5
        ELSE -LEAST(COUNT(ckp.unified_social_credit_code), 2) * 2
        END AS environmentalProtectionKeyPollutionDeduction,
        -LEAST(COUNT(cesv.unified_social_credit_code), 2) * 1 AS environmentalProtectionIllegalSupervisionDeduction,
        -LEAST(COUNT(ceop.unified_social_credit_code), 2) * 1 AS environmentalProtectionOtherPenaltiesDeduction,
        -LEAST(COUNT(eop.unified_social_credit_code), 2) * 1 AS emergencySafetySupervisionAdministrativePenaltyDeduction,
        -LEAST(COUNT(clsp.unified_social_credit_code), 2) * 1 AS listedCompanySupervisionAccountabilityDeduction,
        -LEAST(COUNT(canf.unified_social_credit_code), 2) * 1 AS debtReminderFinancialDebtDeduction,
        -LEAST(COUNT(ccop.unified_social_credit_code), 2) * 1 AS housingConstructionPenaltyBulletinPenaltyDeduction,
        -LEAST(COUNT(clrp.unified_social_credit_code), 2) * 1 AS landResourcesAdministrativePenaltiesDeduction,
        -LEAST(COUNT(lsp.unified_social_credit_code), 2) * 1 AS laborSecurityIllegalLaborSupervisionPenaltyDeduction,
        CASE
        WHEN (COUNT(DISTINCT ccp.unified_social_credit_code) + COUNT(DISTINCT ccmd.unified_social_credit_code)) >= 2 THEN
        -2
        WHEN (COUNT(DISTINCT ccp.unified_social_credit_code) + COUNT(DISTINCT ccmd.unified_social_credit_code)) = 1 THEN -1
        ELSE 0
        END AS civilAffairsManagementPenaltyDeduction,
        -LEAST(COUNT(cmpp.unified_social_credit_code), 2) * 1 AS industryAndInformationTelecomPenaltiesDeduction,
        -LEAST(COUNT(cafwp.unified_social_credit_code), 2) * 1 AS agricultureFisheriesWaterPenaltiesDeduction,
        -LEAST(COUNT(cfspp.unified_social_credit_code), 2) * 1 AS financialSupervisionPBOCPenaltyDeduction,
        -LEAST(COUNT(cfsfp.unified_social_credit_code), 2) * 1 AS financialSupervisionFinancePenaltyDeduction,
        -LEAST(COUNT(fsfp.unified_social_credit_code), 2) * 1 AS financialSupervisionFinancialBureauPenaltyDeduction,
        -LEAST(COUNT(cfslp.unified_social_credit_code), 2) * 1 AS financialSupervisionLocalFinancePenaltyDeduction,
        -LEAST(COUNT(chsp.unified_social_credit_code), 2) * 1 AS healthAndWellnessAdministrativePenaltiesDeduction,
        -LEAST(COUNT(ceep.unified_social_credit_code), 2) * 1 AS energyElectricityElectricalSectorPenaltyDeduction,
        -LEAST(COUNT(cp.unified_social_credit_code), 2) * 1 AS commercePenaltiesAdministrativePenaltyDeduction,
        -LEAST(COUNT(csecp.unified_social_credit_code), 2) * 1 AS educationCultureTourismPenaltiesDeduction,
        -LEAST(COUNT(cbcp.unified_social_credit_code), 2) * 1 AS radioTelevisionInformationPenaltiesDeduction,
        -LEAST(COUNT(cuap.unified_social_credit_code), 2) * 1 AS urbanManagementPenaltiesDeduction,
        -LEAST(COUNT(ciodp.unified_social_credit_code), 2) * 1 AS industryOrganizationSelfDisciplinePenaltyDeduction,
        -LEAST(COUNT(cusp.unified_social_credit_code), 2) * 1 AS badSupplierListGovernmentProcurementPenaltyDeduction,
        -LEAST(COUNT(usp.unified_social_credit_code), 2) * 1 AS badSupplierListMilitaryProcurementPenaltyDeduction,
        -LEAST(COUNT(cbsep.unified_social_credit_code), 2) * 1 AS badSupplierListCorporateProcurementPenaltyDeduction,
        -LEAST(COUNT(copa.unified_social_credit_code), 2) * 1 AS administrativePenaltiesOtherPenaltiesDeduction,-- 合并扣分并限制最大扣分不超过10分
        - LEAST( COUNT( cap.unified_social_credit_code ), 10 ) * 2 AS administrativePenalty,
        GREATEST(
        0,20 - (
        GREATEST(
        0,
        CASE
        WHEN COUNT(cba.unified_social_credit_code) > 2 THEN 5
        ELSE LEAST(COUNT(cba.unified_social_credit_code), 2) * 2
        END +
        LEAST( COUNT( cef.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cpp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cii.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cpr.unified_social_credit_code ), 2 ) * 1 +
        LEAST(COUNT(cmpm.unified_social_credit_code), 2) * 1 + LEAST( COUNT( cepi.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cmr.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cta.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( ctu.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cca.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cop.unified_social_credit_code ), 1 ) * 1 +
        CASE

        WHEN COUNT( ckp.unified_social_credit_code ) > 2 THEN
        5 ELSE LEAST( COUNT( ckp.unified_social_credit_code ), 2 ) * 2
        END + LEAST( COUNT( cesv.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( ceop.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( eop.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( clsp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( canf.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( ccop.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( clrp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( lsp.unified_social_credit_code ), 2 ) * 1 + GREATEST(
        0,
        CASE
        WHEN (COUNT(DISTINCT ccp.unified_social_credit_code) + COUNT(DISTINCT ccmd.unified_social_credit_code)) >= 2 THEN
        2
        WHEN (COUNT(DISTINCT ccp.unified_social_credit_code) + COUNT(DISTINCT ccmd.unified_social_credit_code)) = 1 THEN 1
        ELSE 0
        END
        ) + LEAST( COUNT( cmpp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cafwp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cfspp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cfsfp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( fsfp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cfslp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( chsp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( ceep.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( csecp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cbcp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cuap.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( ciodp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cusp.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( usp.unified_social_credit_code ), 2 ) * 1 + LEAST(COUNT(cbsep.unified_social_credit_code), 2) * 1 + LEAST( COUNT( copa.unified_social_credit_code ), 2 ) * 1 + LEAST( COUNT( cap.unified_social_credit_code ), 10) * 2
        )
        )) AS total
        FROM
        crm_high_tech_zone_enterprises AS e
        LEFT JOIN crm_business_abnormality AS cba ON e.unified_social_credit_code = cba.unified_social_credit_code
        LEFT JOIN crm_equity_freeze AS cef ON e.unified_social_credit_code = cef.unified_social_credit_code
        LEFT JOIN crm_price_penalty AS cpp ON e.unified_social_credit_code = cpp.unified_social_credit_code
        LEFT JOIN crm_ip_infringement AS cii ON e.unified_social_credit_code = cii.unified_social_credit_code
        LEFT JOIN crm_product_recall AS cpr ON e.unified_social_credit_code = cpr.unified_social_credit_code
        LEFT JOIN crm_movable_property_mortgage AS cmpm ON e.unified_social_credit_code = cmpm.unified_social_credit_code
        LEFT JOIN crm_equity_pledge_information AS cepi ON e.unified_social_credit_code = cepi.unified_social_credit_code
        LEFT JOIN crm_market_regulation AS cmr ON e.unified_social_credit_code = cmr.unified_social_credit_code
        LEFT JOIN crm_tax_arrears AS cta ON e.unified_social_credit_code = cta.unified_social_credit_code
        LEFT JOIN crm_tax_urge AS ctu ON e.unified_social_credit_code = ctu.unified_social_credit_code
        LEFT JOIN crm_corrective_action AS cca ON e.unified_social_credit_code = cca.unified_social_credit_code
        LEFT JOIN crm_other_punishments AS cop ON e.unified_social_credit_code = cop.unified_social_credit_code
        LEFT JOIN crm_key_polluters AS ckp ON e.unified_social_credit_code = ckp.unified_social_credit_code
        LEFT JOIN crm_env_supervision_violation_supervision AS cesv ON e.unified_social_credit_code = cesv.unified_social_credit_code
        LEFT JOIN crm_environmental_other_penalties AS ceop ON e.unified_social_credit_code = ceop.unified_social_credit_code
        LEFT JOIN crm_emergency_other_penalty AS eop ON e.unified_social_credit_code = eop.unified_social_credit_code
        LEFT JOIN crm_listing_supervision_penalty AS clsp ON e.unified_social_credit_code = clsp.unified_social_credit_code
        LEFT JOIN crm_arrears_notification_financial AS canf ON e.unified_social_credit_code = canf.unified_social_credit_code
        LEFT JOIN crm_construction_other_penalties AS ccop ON e.unified_social_credit_code = ccop.unified_social_credit_code
        LEFT JOIN crm_land_resources_penalty AS clrp ON e.unified_social_credit_code = clrp.unified_social_credit_code
        LEFT JOIN crm_labor_supervision_punishment AS lsp ON e.unified_social_credit_code = lsp.unified_social_credit_code
        LEFT JOIN crm_civil_penalties AS ccp ON e.unified_social_credit_code = ccp.unified_social_credit_code
        LEFT JOIN crm_civil_management_dishonesty AS ccmd ON e.unified_social_credit_code = ccmd.unified_social_credit_code
        LEFT JOIN crm_miit_post_penalty AS cmpp ON e.unified_social_credit_code = cmpp.unified_social_credit_code
        LEFT JOIN crm_agri_fishery_water_penalty AS cafwp ON e.unified_social_credit_code = cafwp.unified_social_credit_code
        LEFT JOIN crm_finance_supervision_pbc_penalty AS cfspp ON e.unified_social_credit_code = cfspp.unified_social_credit_code
        LEFT JOIN crm_finance_supervision_fiscal_penalty AS cfsfp ON e.unified_social_credit_code = cfsfp.unified_social_credit_code
        LEFT JOIN crm_finance_supervision_financial_penalty AS fsfp ON e.unified_social_credit_code = fsfp.unified_social_credit_code
        LEFT JOIN crm_finance_supervision_local_penalty AS cfslp ON e.unified_social_credit_code = cfslp.unified_social_credit_code
        LEFT JOIN crm_health_sanitation_penalty AS chsp ON e.unified_social_credit_code = chsp.unified_social_credit_code
        LEFT JOIN crm_energy_electricity_penalty AS ceep ON e.unified_social_credit_code = ceep.unified_social_credit_code
        LEFT JOIN crm_commerce_penalty AS cp ON e.unified_social_credit_code = cp.unified_social_credit_code
        LEFT JOIN crm_sci_edu_culture_penalty AS csecp ON e.unified_social_credit_code = csecp.unified_social_credit_code
        LEFT JOIN crm_broadcast_communication_penalty AS cbcp ON e.unified_social_credit_code = cbcp.unified_social_credit_code
        LEFT JOIN crm_urban_administration_penalty AS cuap ON e.unified_social_credit_code = cuap.unified_social_credit_code
        LEFT JOIN crm_industry_organization_discipline_penalty AS ciodp ON e.unified_social_credit_code = ciodp.unified_social_credit_code
        LEFT JOIN crm_unsatisfactory_supplier_penalty AS cusp ON e.unified_social_credit_code = cusp.unified_social_credit_code
        LEFT JOIN crm_unqualified_suppliers_punishment AS usp ON e.unified_social_credit_code = usp.unified_social_credit_code
        LEFT JOIN crm_bad_supplier_enterprise_penalty AS cbsep ON e.unified_social_credit_code = cbsep.unified_social_credit_code
        LEFT JOIN crm_other_penalty_administrative AS copa ON e.unified_social_credit_code = copa.unified_social_credit_code
        LEFT JOIN crm_administrative_penalty AS cap ON e.unified_social_credit_code = cap.unified_social_credit_code and  EXTRACT(YEAR FROM penalty_decision_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        <where>
            <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
                and e.unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
            </if>
            <if test="enterpriseName !=null and enterpriseName != ''">
                and e.enterprise_name like concat('%', #{enterpriseName}, '%')
            </if>
        </where>
        GROUP BY
        e.unified_social_credit_code -- 根据企业代码分组

        ORDER BY
        total DESC;
    </select>
    <select id="creditScoreDeductions"
            resultType="com.zksy.system.basicData.model.respone.CreditScoreDeductionsDto">
        SELECT
        e.unified_social_credit_code AS unifiedSocialCreditCode,
        e.enterprise_name AS enterpriseName,
        - LEAST( COUNT( caci.unified_social_credit_code ), 2 ) * 5 AS adminForcibleExecutionDeduction,
        - LEAST( COUNT( cas.unified_social_credit_code ), 2 ) * 5 AS adminSanctionDeduction,
        - LEAST( COUNT( crmas.unified_social_credit_code ), 5 ) * 1 AS adminSupervisionDeduction,
        - LEAST( COUNT( cci.unified_social_credit_code ), 2 ) * 1 AS complaDoubleReportDeduction,
        - LEAST( COUNT( cucci.unified_social_credit_code ), 3 ) * 1 AS creditPromiseUnfulfilledDeduction,
        - LEAST( COUNT( ccni.unified_social_credit_code ), 3 ) * 1 AS contractUnfulfilledDeduction,
        CASE

        WHEN COUNT( csiai.unified_social_credit_code ) > 2 THEN
        - 5 ELSE - LEAST( COUNT( csiai.unified_social_credit_code ), 2 ) * 2
        END AS socialSecurityArrearsDeduction,
        (
        SELECT
        - LEAST(
        5,
        COALESCE(
        (SELECT SUM(CASE WHEN is_currently_in_arrears = '是' THEN 1 ELSE 0 END) FROM crm_quarterly_water_usage WHERE unified_social_credit_code = e.unified_social_credit_code),
        0
        ) +
        COALESCE(
        (SELECT SUM(CASE WHEN arrear_exists = '是' THEN 1 ELSE 0 END) FROM crm_quarterly_electricity_bill WHERE unified_social_credit_code = e.unified_social_credit_code),
        0
        ) +
        COALESCE(
        (SELECT SUM(CASE WHEN is_overdue = '是' THEN 1 ELSE 0 END) FROM crm_gas_fee_information WHERE unified_social_credit_code = e.unified_social_credit_code),
        0
        )
        )
        )
        AS utilityArrearsDeduction,
        - LEAST( COUNT( cfm.unified_social_credit_code ), 2 ) * 1 AS falseDeclarationMaterialsProvided,
        10 - (
        GREATEST(
        0,
        LEAST( COUNT( caci.unified_social_credit_code ), 2 ) * 5 +
        LEAST( COUNT( cas.unified_social_credit_code ), 2 ) * 5 +
        LEAST( COUNT( crmas.unified_social_credit_code ), 5 ) * 1 +
        LEAST( COUNT( cci.unified_social_credit_code ), 2 ) * 1
        )
        )+10 - (GREATEST(0,
        LEAST( COUNT( cucci.unified_social_credit_code ), 3 ) * 1 +
        LEAST( COUNT( ccni.unified_social_credit_code ), 3 ) * 1 +
        CASE

        WHEN COUNT( csiai.unified_social_credit_code ) > 2 THEN
        5 ELSE  LEAST( COUNT( csiai.unified_social_credit_code ), 2 ) * 2
        END+
        (
        SELECT
        LEAST(
        5,
        COALESCE(
        (SELECT SUM(CASE WHEN is_currently_in_arrears = '是' THEN 1 ELSE 0 END) FROM crm_quarterly_water_usage WHERE unified_social_credit_code = e.unified_social_credit_code),
        0
        ) +
        COALESCE(
        (SELECT SUM(CASE WHEN arrear_exists = '是' THEN 1 ELSE 0 END) FROM crm_quarterly_electricity_bill WHERE unified_social_credit_code = e.unified_social_credit_code),
        0
        ) +
        COALESCE(
        (SELECT SUM(CASE WHEN is_overdue = '是' THEN 1 ELSE 0 END) FROM crm_gas_fee_information WHERE unified_social_credit_code = e.unified_social_credit_code),
        0
        )+LEAST( COUNT( cfm.unified_social_credit_code ), 2 ) * 1
        )
        ))) AS total

        FROM
        crm_high_tech_zone_enterprises AS e
        LEFT JOIN crm_administrative_compulsion_information AS caci ON e.unified_social_credit_code = caci.unified_social_credit_code and EXTRACT(YEAR FROM date_of_administrative_compulsion_decision) >= EXTRACT(YEAR FROM CURDATE()) - 1
        LEFT JOIN crm_administrative_sanction AS cas ON e.unified_social_credit_code = cas.unified_social_credit_code and EXTRACT(YEAR FROM sanction_decision_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        LEFT JOIN crm_administrative_supervision AS crmas ON e.unified_social_credit_code = crmas.unified_social_credit_code and EXTRACT(YEAR FROM inspection_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        LEFT JOIN crm_complaint_information AS cci ON e.unified_social_credit_code = cci.unified_social_credit_code and EXTRACT(YEAR FROM complaint_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        LEFT JOIN crm_unfulfilled_credit_commitment_information AS cucci ON e.unified_social_credit_code = cucci.unified_social_credit_code and EXTRACT(YEAR FROM unfulfilled_credit_item_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        LEFT JOIN crm_contract_nonperformance_info AS ccni ON e.unified_social_credit_code = ccni.unified_social_credit_code and EXTRACT(YEAR FROM scheduled_performance_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        LEFT JOIN crm_social_insurance_arrears_info AS csiai ON e.unified_social_credit_code = csiai.unified_social_credit_code
        AND csiai.is_currently_in_arrears = '是'
        LEFT JOIN crm_fraudulent_materials AS cfm ON e.unified_social_credit_code = cfm.unified_social_credit_code and EXTRACT(YEAR FROM submission_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        <where>
            <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
                and e.unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
            </if>
            <if test="enterpriseName !=null and enterpriseName != ''">
                and e.enterprise_name like concat('%', #{enterpriseName}, '%')
            </if>
        </where>
        GROUP BY
        e.unified_social_credit_code -- 根据企业代码分组
        ORDER BY
        total DESC;
    </select>
    <select id="enterpriseScore" resultType="com.zksy.system.basicData.model.respone.EnterpriseScoreDto">


        SELECT
        e.unified_social_credit_code AS unifiedSocialCreditCode,
        e.enterprise_name AS enterpriseName,
        CASE

        WHEN DATEDIFF( NOW(), bl.establishment_date ) >= 3650 THEN
        1
        WHEN DATEDIFF( NOW(), bl.establishment_date ) >= 1095
        AND DATEDIFF( NOW(), bl.establishment_date ) &lt; 3650 THEN
        0.8 ELSE 0.6
        END AS operationalYearsScore,-- 产值得分
        COALESCE (
        CASE

        WHEN SUM( ovi.first_quarter_amount + ovi.second_quarter_amount + ovi.third_quarter_amount + ovi.fourth_quarter_amount ) > 10000 THEN
        4
        WHEN SUM( ovi.first_quarter_amount + ovi.second_quarter_amount + ovi.third_quarter_amount + ovi.fourth_quarter_amount ) BETWEEN 2000
        AND 10000 THEN
        3.5 ELSE 3
        END,
        3
        ) AS revenueScore,-- 计算组织建设得分
        CASE
        WHEN ( pp.id IS NOT NULL AND u.id IS NOT NULL ) THEN
        1 -- 含有任意两项
        WHEN pp.id IS NOT NULL OR u.id IS NOT NULL THEN 0.5
        ELSE 0
        END AS organizationalStructureScore,-- 上市得分
        COALESCE (
        CASE
        WHEN ls.listing_board IN ('主板', '中小板', '创业板', '科创板', '%境外%') THEN 1
        WHEN ls.listing_board IN ('京交所', '新三板') THEN 0.8
        WHEN ls.listing_board = '上市后备' THEN 0.6
        ELSE NULL
        END,
        0 -- 非上市企业默认得分
        ) AS listingScore,-- 业务资质证书信息得分
        COALESCE (
        CASE

        WHEN ql.qualification_level LIKE '%高%'
        OR ql.qualification_level LIKE '%甲%' THEN
        2
        WHEN ql.qualification_level LIKE '%中%'
        OR ql.qualification_level LIKE '%乙%' THEN
        1.6
        WHEN ql.qualification_level LIKE '%低%'
        OR ql.qualification_level LIKE '%丙%' THEN
        1.2 ELSE NULL
        END,
        1.6
        ) AS qualificationCertificateScore,-- 计算体系认证得分
        COALESCE (
        (
        SELECT
        CASE
        WHEN
        COUNT( DISTINCT sc.certification_type ) >= 2 THEN
        1 ELSE SUM( CASE WHEN sc.certification_type IN ( '质量管理', '环境管理' ) THEN 0.5 ELSE 0 END )
        END
        FROM
        crm_system_certification_info sc
        WHERE
        sc.unified_social_credit_code = e.unified_social_credit_code
        AND sc.certification_type IN ( '质量管理', '环境管理' )),
        1
        ) AS systemCertificationScore,
        CASE

        WHEN cte.end_date > NOW() THEN
        2 -- 若记录存在且在有效期内
        ELSE 0 -- 否则

        END AS highTechEnterpriseScore,
        COALESCE (
        (
        SELECT
        MAX( score )
        FROM
        (
        SELECT
        CASE

        WHEN
        ned.giant_code = e.unified_social_credit_code THEN
        1 ELSE 0
        END AS score
        FROM
        crm_new_enterprise_recognition_details ned
        WHERE
        ned.giant_code = e.unified_social_credit_code UNION ALL
        SELECT
        CASE

        WHEN
        ned.unified_social_credit_code = e.unified_social_credit_code THEN
        1 ELSE 0
        END
        FROM
        crm_new_enterprise_recognition_details ned
        WHERE
        ned.unified_social_credit_code = e.unified_social_credit_code
        ) AS scores
        ),
        0
        ) AS specializedNewScore,
        CASE

        WHEN te.unified_social_credit_code IS NOT NULL THEN
        1 ELSE 0
        END AS scienceAndTechnologySME,
        (
        COALESCE (
        (
        SELECT
        1
        FROM
        crm_enterprise_technology_rnd_summary AS trs
        WHERE
        trs.unified_social_credit_code = e.unified_social_credit_code
        AND (
        ( tr.lab_construction IS NOT NULL AND TRIM( lab_construction ) &lt;&gt; '' )
        OR ( tr.government_funding_support IS NOT NULL AND TRIM( government_funding_support ) &lt;&gt; '' )
        OR ( tr.participation_in_standards IS NOT NULL AND TRIM( participation_in_standards ) &lt;&gt; '' )
        OR ( tr.talent_certification IS NOT NULL AND TRIM( talent_certification ) &lt;&gt; '' )
        OR (trs.other_related_activities IS NOT NULL AND TRIM(trs.other_related_activities) &lt;&gt; '')
        )
        LIMIT 1
        ),
        1
        )
        ) AS enterpriseTechnologyRnDScore,
        COALESCE (
        (
        SELECT
        LEAST(
        3,
        ( SELECT COUNT(*) FROM crm_enterprise_trademark_information WHERE unified_social_credit_code = e.unified_social_credit_code ) * 0.1 + ( SELECT COUNT(*) FROM crm_patent_information WHERE unified_social_credit_code = e.unified_social_credit_code ) * 0.1 + ( SELECT COUNT(*) FROM crm_software_copyright_information WHERE unified_social_credit_code = e.unified_social_credit_code ) * 0.1
        )
        ),
        0
        ) AS intellectualPropertyScore,
        COALESCE ( ( SELECT 1 FROM crm_credit_redlist_info cri WHERE cri.unified_social_credit_code = e.unified_social_credit_code LIMIT 1 ), 1 ) AS creditRedListBonus,
        COALESCE (
        (
        SELECT
        LEAST(
        1,
        SUM( CASE WHEN ( bhi.well_known_brand IS NOT NULL AND TRIM( well_known_brand ) &lt;&gt; '' ) THEN 0.5 ELSE 0 END ) +
        CASE

        WHEN ( bhi.famous_trademark IS NOT NULL AND TRIM( famous_trademark ) &lt;&gt; '' ) THEN
        0.5 ELSE 0
        END
        )
        FROM
        crm_brand_honor_info bhi
        WHERE
        bhi.unified_social_credit_code = e.unified_social_credit_code
        ),
        1
        ) AS brandHonorScore,
        COALESCE (
        (
        SELECT
        LEAST(
        3,
        SUM(
        CASE

        WHEN gh.honor_type LIKE '%国家%' THEN
        1
        WHEN gh.honor_type LIKE '%省市%' THEN
        0.8
        WHEN gh.honor_type LIKE '%区县%' THEN
        0.6 ELSE 0
        END
        )
        )
        FROM
        crm_government_awarded_honors gh
        WHERE
        gh.unified_social_credit_code = e.unified_social_credit_code
        AND gh.publish_year >= DATE_SUB( CURDATE(), INTERVAL 3 YEAR )
        ),
        3
        ) AS governmentHonorBonus,
        COALESCE (
        (
        SELECT
        LEAST( 1, SUM( 0.5 ) )
        FROM
        crm_association_honor_information ah
        WHERE
        ah.unified_social_credit_code = e.unified_social_credit_code
        AND ah.release_year >= DATE_SUB( NOW(), INTERVAL 3 YEAR )
        ),
        1
        ) AS associationHonorBonus,-- 纳税额得分
        COALESCE (
        CASE

        WHEN SUM( epi.first_quarter_amount + epi.second_quarter_amount + epi.third_quarter_amount + epi.fourth_quarter_amount ) > 1000 THEN
        4
        WHEN SUM( epi.first_quarter_amount + epi.second_quarter_amount + epi.third_quarter_amount + epi.fourth_quarter_amount ) BETWEEN 100
        AND 1000 THEN
        3.5 ELSE 3
        END,
        2
        ) AS taxPaymentScore,
        COALESCE (
        (
        SELECT
        CASE

        WHEN
        esi.unified_social_credit_code THEN
        1 ELSE 0.5
        END
        FROM
        crm_enterprise_social_security_info esi
        WHERE
        esi.unified_social_credit_code = e.unified_social_credit_code and esi.remarks LIKE CONCAT('%', EXTRACT(YEAR FROM CURDATE()), '%')
        ),
        0
        ) AS socialSecurityScore,
        COALESCE ( ( SELECT LEAST( 1, SUM( 0.5 )) FROM crm_charity_info ci WHERE ci.unified_social_credit_code = e.unified_social_credit_code ), 0 ) AS charityActivityScore,
        (
        CASE

        WHEN DATEDIFF( NOW(), bl.establishment_date ) >= 3650 THEN
        1
        WHEN DATEDIFF( NOW(), bl.establishment_date ) >= 1095
        AND DATEDIFF( NOW(), bl.establishment_date ) &lt; 3650 THEN
        0.8 ELSE 0.6
        END + -- 产值得分
        COALESCE (
        CASE

        WHEN SUM( ovi.first_quarter_amount + ovi.second_quarter_amount + ovi.third_quarter_amount + ovi.fourth_quarter_amount ) > 10000 THEN
        4
        WHEN SUM( ovi.first_quarter_amount + ovi.second_quarter_amount + ovi.third_quarter_amount + ovi.fourth_quarter_amount ) BETWEEN 1000
        AND 10000 THEN
        3.5 ELSE 3
        END,
        3
        ) +
        CASE

        WHEN ( pp.id IS NOT NULL AND u.id IS NOT NULL ) THEN
        1 -- 含有任意两项
        WHEN pp.id IS NOT NULL OR u.id IS NOT NULL THEN 0.5
        ELSE 0
        END + COALESCE (
        CASE
        WHEN ls.listing_board IN ('主板', '中小板', '创业板', '科创板', '%境外%') THEN 1
        WHEN ls.listing_board IN ('京交所', '新三板') THEN 0.8
        WHEN ls.listing_board = '上市后备' THEN 0.6
        ELSE NULL
        END,
        0 -- 非上市企业默认得分
        ) + COALESCE (
        CASE

        WHEN ql.qualification_level LIKE '%高%'
        OR ql.qualification_level LIKE '%甲%' THEN
        2
        WHEN ql.qualification_level LIKE '%中%'
        OR ql.qualification_level LIKE '%乙%' THEN
        1.6
        WHEN ql.qualification_level LIKE '%低%'
        OR ql.qualification_level LIKE '%丙%' THEN
        1.2 ELSE NULL
        END,
        1.6
        )+ COALESCE (
        (
        SELECT
        CASE

        WHEN
        COUNT( DISTINCT sc.certification_type ) >= 2 THEN
        1 ELSE SUM( CASE WHEN sc.certification_type IN ( '质量管理', '环境管理' ) THEN 0.5 ELSE 0 END )
        END
        FROM
        crm_system_certification_info sc
        WHERE
        sc.unified_social_credit_code = e.unified_social_credit_code
        AND sc.certification_type IN ( '质量管理', '环境管理' )),
        1
        )+
        CASE

        WHEN cte.end_date > NOW() THEN
        2 -- 若记录存在且在有效期内
        ELSE 0 -- 否则

        END + COALESCE (
        (
        SELECT
        MAX( score )
        FROM
        (
        SELECT
        CASE

        WHEN
        ned.giant_code = e.unified_social_credit_code THEN
        1 ELSE 0
        END AS score
        FROM
        crm_new_enterprise_recognition_details ned
        WHERE
        ned.giant_code = e.unified_social_credit_code UNION ALL
        SELECT
        CASE

        WHEN
        ned.unified_social_credit_code = e.unified_social_credit_code THEN
        1 ELSE 0
        END
        FROM
        crm_new_enterprise_recognition_details ned
        WHERE
        ned.unified_social_credit_code = e.unified_social_credit_code
        ) AS scores
        ),
        0
        )+CASE
        WHEN te.unified_social_credit_code IS NOT NULL THEN
        1 ELSE 0 END+ COALESCE (
        (
        SELECT
        1
        FROM
        crm_enterprise_technology_rnd_summary AS trs
        WHERE
        trs.unified_social_credit_code = e.unified_social_credit_code
        AND (
        ( tr.lab_construction IS NOT NULL AND TRIM( lab_construction ) &lt;&gt; '' )
        OR ( tr.government_funding_support IS NOT NULL AND TRIM( government_funding_support ) &lt;&gt; '' )
        OR ( tr.participation_in_standards IS NOT NULL AND TRIM( participation_in_standards ) &lt;&gt; '' )
        OR ( tr.talent_certification IS NOT NULL AND TRIM( talent_certification ) &lt;&gt; '' )
        OR (trs.other_related_activities IS NOT NULL AND TRIM(trs.other_related_activities) &lt;&gt; '')
        )
        LIMIT 1
        ),
        1
        ) + COALESCE (
        (
        SELECT
        LEAST(
        3,
        ( SELECT COUNT(*) FROM crm_enterprise_trademark_information WHERE unified_social_credit_code = e.unified_social_credit_code ) * 0.1 + ( SELECT COUNT(*) FROM crm_patent_information WHERE unified_social_credit_code = e.unified_social_credit_code ) * 0.1 + ( SELECT COUNT(*) FROM crm_software_copyright_information WHERE unified_social_credit_code = e.unified_social_credit_code ) * 0.1
        )
        ),
        0
        ) + COALESCE ( ( SELECT 1 FROM crm_credit_redlist_info cri WHERE cri.unified_social_credit_code = e.unified_social_credit_code LIMIT 1 ), 1 )+ COALESCE (
        (
        SELECT
        LEAST(
        1,
        SUM( CASE WHEN ( bhi.well_known_brand IS NOT NULL AND TRIM( well_known_brand ) &lt;&gt; '' ) THEN 0.5 ELSE 0 END ) +
        CASE

        WHEN ( bhi.famous_trademark IS NOT NULL AND TRIM( famous_trademark ) &lt;&gt; '' ) THEN
        0.5 ELSE 0
        END
        )
        FROM
        crm_brand_honor_info bhi
        WHERE
        bhi.unified_social_credit_code = e.unified_social_credit_code
        ),
        1
        ) + COALESCE (
        (
        SELECT
        LEAST(
        3,
        SUM(
        CASE

        WHEN gh.honor_type LIKE '%国家%' THEN
        1
        WHEN gh.honor_type LIKE '%省市%' THEN
        0.8
        WHEN gh.honor_type LIKE '%区县%' THEN
        0.6 ELSE 0
        END
        )
        )
        FROM
        crm_government_awarded_honors gh
        WHERE
        gh.unified_social_credit_code = e.unified_social_credit_code
        AND gh.publish_year >= DATE_SUB( CURDATE(), INTERVAL 3 YEAR )
        ),
        3
        ) + COALESCE (
        (
        SELECT
        LEAST( 1, SUM( 0.5 ) )
        FROM
        crm_association_honor_information ah
        WHERE
        ah.unified_social_credit_code = e.unified_social_credit_code
        AND ah.release_year >= DATE_SUB( NOW(), INTERVAL 3 YEAR )
        ),
        1
        )+ COALESCE (
        CASE

        WHEN SUM( epi.first_quarter_amount + epi.second_quarter_amount + epi.third_quarter_amount + epi.fourth_quarter_amount ) > 1000 THEN
        4
        WHEN SUM( epi.first_quarter_amount + epi.second_quarter_amount + epi.third_quarter_amount + epi.fourth_quarter_amount ) BETWEEN 100
        AND 1000 THEN
        3 ELSE 2
        END,
        2
        ) + COALESCE (
        (
        SELECT
        CASE

        WHEN
        esi.unified_social_credit_code THEN
        1 ELSE 0.5
        END
        FROM
        crm_enterprise_social_security_info esi
        WHERE
        esi.unified_social_credit_code = e.unified_social_credit_code and esi.remarks LIKE CONCAT('%', EXTRACT(YEAR FROM CURDATE()), '%')
        ),
        0
        ) + COALESCE ( ( SELECT LEAST( 1, SUM( 0.5 )) FROM crm_charity_info ci WHERE ci.unified_social_credit_code = e.unified_social_credit_code ), 0 )
        ) AS total
        FROM
        crm_high_tech_zone_enterprises AS e
        LEFT JOIN crm_business_license_information AS bl ON e.unified_social_credit_code = bl.unified_social_credit_code
        LEFT JOIN crm_output_value AS ovi ON e.unified_social_credit_code = ovi.unified_social_credit_code
        AND ovi.annual_amount = EXTRACT(YEAR FROM CURDATE()) - 1
        LEFT JOIN crm_party_branch_information AS pp ON e.unified_social_credit_code = pp.unified_social_credit_code
        LEFT JOIN crm_union_information AS u ON e.unified_social_credit_code = u.unified_social_credit_code
        LEFT JOIN crm_credit_management AS cm ON e.unified_social_credit_code = cm.unified_social_credit_code
        LEFT JOIN crm_qualification_certificate_info AS ql ON e.unified_social_credit_code = ql.unified_social_credit_code
        LEFT JOIN crm_listed_company AS ls ON e.unified_social_credit_code = ls.unified_social_credit_code
        LEFT JOIN crm_business_license_information AS bli ON e.unified_social_credit_code = bli.unified_social_credit_code
        LEFT JOIN crm_certified_technology_enterprise AS cte ON e.unified_social_credit_code = cte.unified_social_credit_code
        AND cte.end_date IS NOT NULL -- 确保只考虑那些有end_date记录的
        LEFT JOIN crm_technology_enterprises AS te ON e.unified_social_credit_code = te.unified_social_credit_code
        LEFT JOIN crm_enterprise_technology_rnd_summary AS tr ON e.unified_social_credit_code = tr.unified_social_credit_code
        LEFT JOIN crm_business_license_information AS cbl ON e.unified_social_credit_code = cbl.unified_social_credit_code
        LEFT JOIN crm_enterprise_tax_payment_info AS epi ON e.unified_social_credit_code = epi.unified_social_credit_code
        AND epi.annual_amount = EXTRACT(YEAR FROM CURDATE()) - 1
        <where>
            <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
                and e.unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
            </if>
            <if test="enterpriseName !=null and enterpriseName != ''">
                and e.enterprise_name like concat('%', #{enterpriseName}, '%')
            </if>
        </where>
        GROUP BY
        e.unified_social_credit_code -- 根据企业代码分组
        ORDER BY
        total DESC;
    </select>
    <select id="getCrmFinancialReportList" resultType="com.zksy.system.domain.bo.FinancialBo">
        SELECT
            unified_social_credit_code,
            enterprise_name,
            SUM(CASE WHEN project = '应收账款余额（万元）' THEN beginning_balance ELSE 0 END) AS beginning_accounts_receivable,
            SUM(CASE WHEN project = '应收账款余额（万元）' THEN ending_balance ELSE 0 END) AS ending_accounts_receivable,
            SUM(CASE WHEN project = '存货余额（万元）' THEN beginning_balance ELSE 0 END) AS beginning_inventory,
            SUM(CASE WHEN project = '存货余额（万元）' THEN ending_balance ELSE 0 END) AS ending_inventory,
            SUM(CASE WHEN project = '流动资产（万元）' THEN beginning_balance ELSE 0 END) AS beginning_current_assets,
            SUM(CASE WHEN project = '流动资产（万元）' THEN ending_balance ELSE 0 END) AS ending_current_assets,
            SUM(CASE WHEN project = '资产总额（万元）' THEN beginning_balance ELSE 0 END) AS beginning_total_assets,
            SUM(CASE WHEN project = '资产总额（万元）' THEN ending_balance ELSE 0 END) AS ending_total_assets,
            SUM(CASE WHEN project = '流动负债（万元）' THEN beginning_balance ELSE 0 END) AS beginning_current_liabilities,
            SUM(CASE WHEN project = '流动负债（万元）' THEN ending_balance ELSE 0 END) AS ending_current_liabilities,
            SUM(CASE WHEN project = '负债总额（万元）' THEN beginning_balance ELSE 0 END) AS beginning_total_liabilities,
            SUM(CASE WHEN project = '负债总额（万元）' THEN ending_balance ELSE 0 END) AS ending_total_liabilities,
            SUM(CASE WHEN project = '净资产（万元）' THEN beginning_balance ELSE 0 END) AS beginning_equity,
            SUM(CASE WHEN project = '净资产（万元）' THEN ending_balance ELSE 0 END) AS ending_equity,
            SUM(CASE WHEN project = '营业总收入（万元）' THEN beginning_balance ELSE 0 END) AS beginning_operating_revenue,
            SUM(CASE WHEN project = '营业总收入（万元）' THEN ending_balance ELSE 0 END) AS ending_operating_revenue,
            SUM(CASE WHEN project = '营业成本（万元）' THEN beginning_balance ELSE 0 END) AS beginning_operating_cost,
            SUM(CASE WHEN project = '营业成本（万元）' THEN ending_balance ELSE 0 END) AS ending_operating_cost,
            SUM(CASE WHEN project = '研发（R&amp;D）经费（万元）' THEN beginning_balance ELSE 0 END) AS beginning_r_and_d_expense,
            SUM(CASE WHEN project = '研发（R&amp;D）经费（万元）' THEN ending_balance ELSE 0 END) AS ending_r_and_d_expense,
            SUM(CASE WHEN project = '营业利润（万元）' THEN beginning_balance ELSE 0 END) AS beginning_operating_profit,
            SUM(CASE WHEN project = '营业利润（万元）' THEN ending_balance ELSE 0 END) AS ending_operating_profit,
            SUM(CASE WHEN project = '利润总额（万元）' THEN beginning_balance ELSE 0 END) AS beginning_total_profit,
            SUM(CASE WHEN project = '利润总额（万元）' THEN ending_balance ELSE 0 END) AS ending_total_profit,
            SUM(CASE WHEN project = '净利润（万元）' THEN beginning_balance ELSE 0 END) AS beginning_net_profit,
            SUM(CASE WHEN project = '净利润（万元）' THEN ending_balance ELSE 0 END) AS ending_net_profit,
            SUM(CASE WHEN project = '经营活动产生的现金流量净额（万元）' THEN beginning_balance ELSE 0 END) AS beginning_cash_flow_from_operations,
            SUM(CASE WHEN project = '经营活动产生的现金流量净额（万元）' THEN ending_balance ELSE 0 END) AS ending_cash_flow_from_operations,
            practitioners
        FROM
            crm_financial_report
        <where>
            <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
                and unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
            </if>
            <if test="enterpriseName !=null and enterpriseName != ''">
                and enterprise_name like concat('%', #{enterpriseName}, '%')
            </if>
        </where>
        GROUP BY
            unified_social_credit_code,
            enterprise_name,
            practitioners
    </select>
    <select id="legalDeduction" resultType="com.zksy.system.basicData.model.respone.LegalDeductionDto">
        SELECT
        e.unified_social_credit_code AS unifiedSocialCreditCode,
        e.enterprise_name AS enterpriseName,-- 计算诉讼扣分
        - LEAST( COUNT( cli.unified_social_credit_code ), 2 ) * 5 AS criminalCaseLitigationDeduction,-- 存在诉讼记录每条扣5分，最多扣10分
        CASE

        WHEN COUNT( cde.unified_social_credit_code ) > 3 THEN
        -10 ELSE - LEAST( COUNT( cde.unified_social_credit_code ), 3 ) * 3
        END AS judicialExecutionDeduction,
        - LEAST( COUNT( ja.unified_social_credit_code ), 5 ) * 1 AS auctionNoticeAuctionDeduction,-- 合并扣分并限制最大扣分不超过10分
        - LEAST( COUNT( cppsa.unified_social_credit_code ), 2 ) * 1 AS policeNoticePenaltyDeduction,
        - LEAST( COUNT( cla.unified_social_credit_code ), 2 ) * 1 AS arbitrationInfoLaborArbitrationDeduction, -- 劳动仲裁使用裁决表

        10 - (
        GREATEST(
        0,
        LEAST(COUNT(cli.unified_social_credit_code), 2) * 5 +
        CASE WHEN COUNT(cde.unified_social_credit_code) > 3 THEN 10 ELSE LEAST(COUNT(cde.unified_social_credit_code), 3) * 3 END +
        LEAST(COUNT(ja.unified_social_credit_code), 5) * 1 +
        LEAST(COUNT(cppsa.unified_social_credit_code), 2) * 1 +
        LEAST(COUNT(cla.unified_social_credit_code), 2) * 1
        )
        ) AS total
        FROM
        crm_high_tech_zone_enterprises AS e
        LEFT JOIN crm_criminal_litigation_information AS cli ON e.unified_social_credit_code = cli.unified_social_credit_code and release_date >= DATE_SUB( CURDATE(), INTERVAL 1 YEAR ) -- 新增JOIN诉讼信息表
        LEFT JOIN crm_subject_to_execution AS cde ON e.unified_social_credit_code = cde.unified_social_credit_code and  filing_time >= DATE_SUB( CURDATE(), INTERVAL 1 YEAR )
        LEFT JOIN crm_judicial_auction AS ja ON e.unified_social_credit_code = ja.unified_social_credit_code and  auction_time >= DATE_SUB( CURDATE(), INTERVAL 1 YEAR ) -- 新增JOIN司法拍卖表
        LEFT JOIN crm_punish_public_security_announcement AS cppsa ON e.unified_social_credit_code = cppsa.unified_social_credit_code and  punishment_date >= DATE_SUB( CURDATE(), INTERVAL 1 YEAR )
        LEFT JOIN crm_administrative_sanction_info AS cla ON e.unified_social_credit_code = cla.unified_social_credit_code and EXTRACT(YEAR FROM date_of_sanction) >= EXTRACT(YEAR FROM CURDATE()) - 1
        <where>
            <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
                and e.unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
            </if>
            <if test="enterpriseName !=null and enterpriseName != ''">
                and e.enterprise_name like concat('%', #{enterpriseName}, '%')
            </if>
        </where>
        GROUP BY
        e.unified_social_credit_code -- 根据企业代码分组
        ORDER BY
        total DESC;
    </select>
    <select id="ratedLevelC" resultType="com.zksy.system.basicData.domain.CrmHighTechZoneEnterprises">
        SELECT DISTINCT e.*
        FROM crm_high_tech_zone_enterprises e
        WHERE e.unified_social_credit_code IN (
        SELECT unified_social_credit_code FROM crm_business_abnormality WHERE (penalty_authority LIKE '%税务局%')
        UNION
        SELECT unified_social_credit_code FROM crm_major_work_safety_accidents  WHERE (notification_reason LIKE '%瞒报%' OR notification_reason LIKE '%谎报%' OR notification_reason LIKE '%漏报%'OR notification_reason LIKE '%挂牌督办%')
        UNION
        SELECT unified_social_credit_code FROM crm_administrative_penalty WHERE (illegal_facts LIKE '%暂扣许可证%'
        OR illegal_facts LIKE '%吊销许可证%'
        OR illegal_facts LIKE '%降低资质等级%'
        OR illegal_facts LIKE '%责令停产停业%'
        OR illegal_facts LIKE '%责令关闭%'
        OR illegal_facts LIKE '%限制开展生产经营活动%')
        UNION
        SELECT unified_social_credit_code FROM crm_criminal_litigation_information WHERE (judgment_content LIKE '%生产销售伪劣产品%' OR judgment_content LIKE '%非法吸收公众存款%' OR judgment_content LIKE '%集资诈骗%' OR judgment_content LIKE '%非法经营%')
        )
        <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
            AND e.unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
        </if>
    </select>
    <select id="ratedLevelD" resultType="com.zksy.system.basicData.domain.CrmHighTechZoneEnterprises">
        SELECT DISTINCT e.*
        FROM crm_high_tech_zone_enterprises e
        WHERE e.unified_social_credit_code IN (
            SELECT unified_social_credit_code FROM crm_dishonest_executor /*where EXTRACT(YEAR FROM filing_time) >= EXTRACT(YEAR FROM CURDATE()) - 1*/
            UNION
            SELECT unified_social_credit_code FROM crm_seriously_illegal_and_dishonest_list /*where EXTRACT(YEAR FROM listing_decision_date) >= EXTRACT(YEAR FROM CURDATE()) - 1*/
            UNION
            SELECT cba.unified_social_credit_code FROM crm_business_license_information cbli
            LEFT JOIN crm_bankruptcy_announcement cba on cba.unified_social_credit_code = cbli.unified_social_credit_code and cbli.status not in ('已成立','续存')
            UNION
            SELECT unified_social_credit_code FROM crm_suspended_business_info
            UNION
            SELECT unified_social_credit_code FROM crm_liquidation_info
        )
        <if test="unifiedSocialCreditCode !=null and unifiedSocialCreditCode != ''">
            AND e.unified_social_credit_code like concat('%', #{unifiedSocialCreditCode}, '%')
        </if>
    </select>
    <select id="getDeductionPoints" resultType="java.lang.String">
        <![CDATA[
        -- 行政处罚
        SELECT unified_social_credit_code FROM crm_business_abnormality AS cba WHERE EXTRACT(YEAR FROM listing_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_equity_freeze AS cef WHERE EXTRACT(YEAR FROM publicity_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_price_penalty AS cpp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_ip_infringement AS cii WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_product_recall AS cpr WHERE EXTRACT(YEAR FROM date_published) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_movable_property_mortgage AS chm WHERE EXTRACT(YEAR FROM mortgage_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_equity_pledge_information AS cepi WHERE EXTRACT(YEAR FROM pledge_registration_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_market_regulation AS cmr WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_tax_arrears AS cta WHERE EXTRACT(YEAR FROM date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_tax_urge AS ctu WHERE EXTRACT(YEAR FROM urge_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_corrective_action AS cca WHERE EXTRACT(YEAR FROM penalty_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_other_punishments AS cop WHERE EXTRACT(YEAR FROM punishment_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_key_polluters AS ckp WHERE EXTRACT(YEAR FROM disclosure_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_env_supervision_violation_supervision AS cesv WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_environmental_other_penalties AS ceop WHERE EXTRACT(YEAR FROM penalty_datetime) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_emergency_other_penalty AS eop WHERE EXTRACT(YEAR FROM penalty_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_listing_supervision_penalty AS clsp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_arrears_notification_financial AS canf WHERE EXTRACT(YEAR FROM arrears_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_construction_other_penalties AS ccop WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_land_resources_penalty AS clrp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_labor_supervision_punishment AS lsp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_civil_penalties AS ccp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_civil_management_dishonesty AS ccmd WHERE EXTRACT(YEAR FROM punishment_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_miit_post_penalty AS cmpp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_agri_fishery_water_penalty AS cafwp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_finance_supervision_pbc_penalty AS cfspp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_finance_supervision_fiscal_penalty AS cfsfp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_finance_supervision_financial_penalty AS fsfp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_finance_supervision_local_penalty AS cfslp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_health_sanitation_penalty AS chsp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_energy_electricity_penalty AS ceep WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_commerce_penalty AS cp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_sci_edu_culture_penalty AS csecp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_broadcast_communication_penalty AS cbcp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_urban_administration_penalty AS cuap WHERE EXTRACT(YEAR FROM punishment_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_industry_organization_discipline_penalty AS ciodp WHERE EXTRACT(YEAR FROM punishment_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_unsatisfactory_supplier_penalty AS cusp WHERE EXTRACT(YEAR FROM punishment_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_unqualified_suppliers_punishment AS usp WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_bad_supplier_enterprise_penalty AS cbsep WHERE EXTRACT(YEAR FROM punishment_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_other_penalty_administrative AS copa WHERE EXTRACT(YEAR FROM penalty_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL  SELECT unified_social_credit_code FROM crm_administrative_penalty AS cap WHERE EXTRACT(YEAR FROM penalty_decision_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
                                                                                                  -- 司法裁判
        UNION ALL SELECT unified_social_credit_code FROM crm_criminal_litigation_information AS cli WHERE EXTRACT(YEAR FROM release_date) >= EXTRACT(YEAR FROM CURDATE()) - 1 -- 新增JOIN诉讼信息表
        UNION ALL SELECT unified_social_credit_code FROM crm_subject_to_execution AS cde WHERE EXTRACT(YEAR FROM filing_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_judicial_auction AS ja WHERE EXTRACT(YEAR FROM auction_time) >= EXTRACT(YEAR FROM CURDATE()) - 1 -- 新增JOIN司法拍卖表
        UNION ALL SELECT unified_social_credit_code FROM crm_punish_public_security_announcement AS cppsa WHERE EXTRACT(YEAR FROM punishment_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_administrative_sanction_info AS cla WHERE EXTRACT(YEAR FROM date_of_sanction) >= EXTRACT(YEAR FROM CURDATE()) - 1
                                                                                                       -- 行政监管、践诺履约
        UNION ALL SELECT unified_social_credit_code FROM crm_administrative_compulsion_information AS caci WHERE EXTRACT(YEAR FROM date_of_administrative_compulsion_decision) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_administrative_sanction AS cas WHERE EXTRACT(YEAR FROM sanction_decision_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_administrative_supervision AS crmas WHERE EXTRACT(YEAR FROM inspection_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_complaint_information AS cci WHERE EXTRACT(YEAR FROM complaint_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_unfulfilled_credit_commitment_information AS cucci WHERE EXTRACT(YEAR FROM unfulfilled_credit_item_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_contract_nonperformance_info AS ccni WHERE EXTRACT(YEAR FROM scheduled_performance_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
#         UNION ALL SELECT unified_social_credit_code FROM crm_social_insurance_arrears_info AS csiai WHERE csiai.is_currently_in_arrears = '是'
        UNION ALL SELECT unified_social_credit_code FROM crm_fraudulent_materials AS cfm WHERE EXTRACT(YEAR FROM submission_date) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_quarterly_water_usage WHERE is_currently_in_arrears = '是' and EXTRACT(YEAR FROM annual) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_quarterly_electricity_bill WHERE arrear_exists = '是' and EXTRACT(YEAR FROM `year`) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_gas_fee_information WHERE is_overdue = '是'
        -- 直接评D的处罚
        UNION ALL SELECT unified_social_credit_code FROM crm_dishonest_executor -- where EXTRACT(YEAR FROM filing_time) >= EXTRACT(YEAR FROM CURDATE()) - 1
        UNION ALL SELECT unified_social_credit_code FROM crm_seriously_illegal_and_dishonest_list where unified_social_credit_code IS NOT NULL AND unified_social_credit_code <> ''
        UNION ALL SELECT unified_social_credit_code FROM crm_bankruptcy_announcement WHERE unified_social_credit_code IN
        (SELECT cba.unified_social_credit_code FROM crm_business_license_information cbli
        LEFT JOIN crm_bankruptcy_announcement cba on cba.unified_social_credit_code = cbli.unified_social_credit_code and cbli.status not in ('已成立','续存'))
        UNION ALL
        SELECT unified_social_credit_code FROM crm_suspended_business_info
        UNION ALL
        SELECT unified_social_credit_code FROM crm_liquidation_info
        ]]>
    </select>
</mapper>