<?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.SecurityRiskAssessmentSummaryMapper">


    <select id="querySecurityRiskAssessmentSummary"
            resultType="com.lutao.cloud.oil.pojo.vo.SecurityRiskAssessmentSummaryVO">
        SELECT
        sras.*,
        pi.oil_gas_name,
        pi.operating_company,
        CASE
        WHEN sras.risk_rank = '004' THEN '低风险'
        WHEN sras.risk_rank = '003' THEN '中风险'
        WHEN sras.risk_rank = '002' THEN '较高风险'
        WHEN sras.risk_rank = '001' THEN '高风险'
        ELSE
        '暂未定级'
        END as riskLevel
        FROM
        security_risk_assessment_summary sras
        LEFT JOIN platform_info pi ON pi.platform_code = sras.platform_code
        <where>
            pi.delete_flag = '0'
            <if test="param.appId != null and param.appId != ''">
                AND sras.app_id = #{param.appId}
            </if>
            <if test="param.platformId != null and param.platformId != ''">
                AND sras.platform_code = #{param.platformId}
            </if>
            <if test="param.year != null and param.year != ''">
                AND sras.evaluation_year = #{param.year}
            </if>
            <if test="param.oilGasName != null and param.oilGasName != ''">
                AND pi.oil_gas_name = #{param.oilGasName}
            </if>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                AND pi.operating_company = #{param.operatingCompany}
            </if>
            <choose>
                <when test="param.assessmentObjectType != null and param.assessmentObjectType != ''">
                    AND sras.evaluation_type = #{param.assessmentObjectType}
                </when>
                <otherwise>
                    AND sras.evaluation_type = 2
                </otherwise>
            </choose>
        </where>
    </select>
    <select id="querySecurityRiskAssessmentSummaryPage"
            resultType="com.lutao.cloud.oil.pojo.vo.SecurityRiskAssessmentSummaryPageVO">
        SELECT

            sras.platform_code,
            sras.evaluation_year,
            sras.evaluation_object_type,
            sras.evaluation_code,
            sras.total_deduction,
            sras.evaluation_type,
            sras.score,
            sras.risk_rank,
            sras.remediation_requirements,
            sras.platform_type,
            CASE
            WHEN sras.`evaluation_object_type` = 1 THEN '固定平台'
            WHEN sras.`evaluation_object_type` = 2 THEN '钻井平台'
            WHEN sras.`evaluation_object_type` = 3 THEN '浮式生产储油装置'
            END AS riskAssessmentName,
            sras.app_id,
            pi.oil_gas_name,
            pi.operating_company,
            pi.platform_name as platform,
            ai.app_name as appName,
            CASE
                WHEN sras.risk_rank = '004' THEN '低风险'
                WHEN sras.risk_rank = '003' THEN '中风险'
                WHEN sras.risk_rank = '002' THEN '较高风险'
                WHEN sras.risk_rank = '001' THEN '高风险'
                ELSE
                    '暂未定级'
                END as riskLevel
        FROM
            security_risk_assessment_summary sras
                LEFT JOIN platform_info pi ON pi.platform_code = sras.platform_code
                LEFT JOIN app_info ai ON ai.id = sras.app_id
        <where>
            pi.delete_flag = '0'
            <if test="param.platformId != null and param.platformId != ''">
                and sras.platform_code = #{param.platformId}
            </if>
            <if test="param.appId != null and param.appId != ''">
                and sras.app_id = #{param.appId}
            </if>
            <if test="param.operatingCompany != null and param.operatingCompany != ''">
                and pi.operating_company = #{param.operatingCompany}
            </if>
            <if test="param.oilGasName != null and param.oilGasName != ''">
                and pi.oil_gas_name = #{param.oilGasName}
            </if>
            <if test="param.riskLevel != null">
                <choose>
                    <when test="param.riskLevel == 1">
                        and sras.risk_rank = '001'
                    </when>
                    <when test="param.riskLevel == 2">
                        and sras.risk_rank = '002'
                    </when>
                    <when test="param.riskLevel == 3">
                        and sras.risk_rank = '003'
                    </when>
                    <when test="param.riskLevel == 4">
                        and sras.risk_rank = '004'
                    </when>
                    <when test="param.riskLevel == 5">
                        and (sras.risk_rank is null or sras.risk_rank = '')
                    </when>
                </choose>
            </if>
            <if test="param.year != null and param.year != ''">
                AND sras.evaluation_year = #{param.year}
            </if>
            <if test="param.assessmentObjectType != null">
                <choose>
                    <when test="param.assessmentObjectType == 1">
                        AND evaluation_type = 1
                    </when>
                    <when test="param.assessmentObjectType == 2">
                        AND evaluation_type = 2
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY sras.evaluation_code DESC
    </select>
</mapper>