<?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.lutao.cloud.oil.mapper.ProblemRectificationDetailMapper">

    <select id="selectProblemRectifyStatus" resultType="com.lutao.cloud.oil.pojo.vo.ProblemRectifyStatusVO">
        SELECT
        pi.platform_name,
        prd.facility_name,
        CASE
        WHEN sra.score &lt;= 700 THEN '001'
        WHEN sra.score > 700 AND sra.score&lt;= 850 THEN '002'
        WHEN sra.score > 850 AND sra.score&lt;= 900 THEN '003'
        ELSE '004'
        END AS riskRank,
        prd.deduction AS score,
        ai.app_name AS appName,
        prd.hidden_problems,
        prd.hidden_problems_state,
        sra.evaluation_code AS evaluationCode
        FROM problem_rectification_detail prd
        LEFT JOIN app_info ai ON prd.app_id = ai.id
        LEFT JOIN platform_info pi ON prd.platform_code = pi.platform_code
        LEFT JOIN security_risk_assessment_summary sra ON sra.origin_id = prd.sra_id
        <where>
            pi.delete_flag = '0'
            <if test="platformId != null and platformId != ''">
               and prd.platform_code = #{platformId}
            </if>
            <if test="appId != null and appId != ''">
                and prd.app_id = #{appId}
            </if>
            <if test="operatingCompany != null and operatingCompany != ''">
                and pi.operating_company = #{operatingCompany}
            </if>
            <if test="oilGasName != null and oilGasName != ''">
                and pi.oil_gas_name = #{oilGasName}
            </if>
            <if test="evaluationType != null and  evaluationType != ''">
                and sra.evaluation_type = #{evaluationType}
            </if>
            <if test="year != null and year != ''">
                and evaluation_year = #{year}
            </if>
            <choose>
                <when test="status == 1">
                    and prd.hidden_problems_state = '001'
                </when>
                <when test="status == 2">
                    and prd.hidden_problems_state = '002'
                </when>
            </choose>
        </where>
        ORDER BY prd.hidden_problems_state ASC, evaluationCode DESC, pi.platform_name IS NULL ASC
    </select>
    <select id="selectProblemRectifyStatusCheck"
            resultType="com.lutao.cloud.oil.pojo.vo.ProblemRectifyStatusCheckVO">
        SELECT
        -- prd.platform_name AS platformName,
        pi.platform_name AS platformName,
--         CASE
--         WHEN prd.score &lt;= 700 THEN '001'
--         WHEN prd.score > 700 AND prd.score&lt;= 850 THEN '002'
--         WHEN prd.score > 850 AND prd.score&lt;= 900 THEN '003'
--         ELSE '004'
--         END AS remediationRequirements,
--         prd.score,
        -- prd.remediation_requirements AS remediationRequirements,
        prd.hidden_problems AS hiddenProblems,
        prd.hidden_problems_state AS hiddenProblemsState,
        prd.treatment_measures AS treatmentMeasures
        FROM problem_rectification_detail prd
        LEFT JOIN security_risk_assessment sra ON sra.origin_id = prd.sra_id
        LEFT JOIN platform_info pi ON prd.platform_code = pi.platform_code

        <where>
            pi.delete_flag = '0'
            <if test="platformId != null and platformId != ''">
               and prd.platform_code = #{platformId}
            </if>
            <if test="appId != null and appId != ''">
                and prd.app_id = #{appId}
            </if>
            <if test="year != null and year != ''">
                and LEFT(sra.evaluation_code, 4) LIKE CONCAT('%',#{year},'%')
            </if>
            <if test="operatingCompany != null and operatingCompany != ''">
                and pi.operating_company = #{operatingCompany}
            </if>
            <if test="oilGasName != null and oilGasName != ''">
                and pi.oil_gas_name = #{oilGasName}
            </if>
        </where>
        ORDER BY platformName IS NULL ASC
    </select>
    <select id="deepEvaluation" resultType="com.lutao.cloud.oil.pojo.vo.FourProblemCheckVO">
        SELECT
            hidden_problems_state AS rectifyStatus,
            COUNT(*) AS number
        FROM
            problem_rectification_detail prd
                LEFT JOIN security_risk_assessment_summary sra ON prd.sra_id = sra.origin_id
        LEFT JOIN platform_info pi ON prd.platform_code = pi.platform_code
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                prd.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and prd.app_id = #{param.appId}
            </if>
            <if test="param.year != null and param.year != ''">
                     and sra.evaluation_year = #{param.year}
            </if>
            <if test="true">
                and sra.evaluation_type = '1'
            </if>
        </where>
        GROUP BY prd.hidden_problems_state
    </select>
    <select id="selfEvaluation" resultType="com.lutao.cloud.oil.pojo.vo.FourProblemCheckVO">
        SELECT
        hidden_problems_state AS rectifyStatus,
        COUNT(*) AS number
        FROM
        problem_rectification_detail prd
        LEFT JOIN security_risk_assessment_summary sra ON prd.sra_id = sra.origin_id
        LEFT JOIN platform_info pi ON prd.platform_code = pi.platform_code
        <where>
           pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
               and prd.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and prd.app_id = #{param.appId}
            </if>
            <if test="param.year != null and param.year != ''">
                and sra.evaluation_year = #{param.year}
            </if>
            <if test="true">
                and sra.evaluation_type = '2'
            </if>
        </where>
        GROUP BY prd.hidden_problems_state
    </select>
    <select id="divisionEvaluation" resultType="com.lutao.cloud.oil.pojo.vo.FourProblemCheckVO">
        SELECT
            hidden_problems_state AS rectifyStatus,
            COUNT(*) as number
        FROM
            division_inspection_detail did
            LEFT JOIN platform_info pi ON did.platform_code = pi.platform_code
            <where>
                pi.delete_flag = '0'
                <if test="param.platformId != null and param.platformId != ''">
                    and did.platform_code = #{param.platformId}
                </if>
                <if test="param.appId != null and param.appId != ''">
                    and did.app_id = #{param.appId}
                </if>
                <if test="param.year != null and param.year != ''">
                    and YEAR(did.input_time) = #{param.year}
                </if>
        </where>
        GROUP BY did.hidden_problems_state

    </select>
    <select id="ministerEvaluation" resultType="com.lutao.cloud.oil.pojo.vo.FourProblemCheckVO">
        SELECT
        hidden_problems_state AS rectifyStatus,
        COUNT(*) as number
        FROM
        ministerial_supervision_detail msd
        LEFT JOIN platform_info pi ON msd.platform_code = pi.platform_code
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and msd.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and msd.app_id = #{param.appId}
            </if>
            <if test="param.year != null and param.year != ''">
                and YEAR(msd.input_time) = #{param.year}
            </if>
        </where>
        GROUP BY msd.hidden_problems_state
    </select>
    <select id="queryFourQuartersRectification"
            resultType="com.lutao.cloud.oil.pojo.vo.FourQuartersRectificationBO">
        SELECT
            YEAR(prd.scoring_time) as year,
            QUARTER(prd.scoring_time) as quarter,
            SUM(CASE WHEN prd.hidden_problems_state = '002' THEN 1 ELSE 0 END) as rectifiedCount,
            COUNT(1) as total_count,
            SUM(CASE WHEN prd.hidden_problems_state = '002' THEN 1 ELSE 0 END) / COUNT(1)  as rectifyRate
        FROM
            `problem_rectification_detail` prd
        LEFT JOIN platform_info pi ON prd.platform_code = pi.platform_code
        LEFT JOIN security_risk_assessment_summary sra ON prd.sra_id = sra.origin_id
        <where>
            pi.delete_flag = '0'
            <if test="param.year != null and param.year != ''">
                and sra.evaluation_year = #{param.year}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                and prd.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and prd.app_id = #{param.appId}
            </if>
        </where>

        GROUP BY
            year,
            quarter
        ORDER BY
            year,
            quarter;

    </select>
</mapper>
