<?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.ruoyi.project.reliability.mapper.TQualitativeAnalysisMapper">

    <resultMap type="TQualitativeAnalysis" id="TQualitativeAnalysisResult">
        <result property="id" column="id"/>
        <result property="location" column="location"/>
        <result property="tagNo" column="tag_no"/>
        <result property="description" column="description"/>
        <result property="objectType" column="object_type"/>
        <result property="catalogProfile" column="catalog_profile"/>
        <result property="relevant" column="relevant"/>
        <result property="frequencyFailure" column="frequency_failure"/>
        <result property="maintenanceCost" column="maintenance_cost"/>
        <result property="productionLoss" column="production_loss"/>
        <result property="maintenanceRisk" column="maintenance_risk"/>
        <result property="productionRisk" column="production_risk"/>
        <result property="equipmentCategory" column="equipment_category"/>
        <result property="riskCriticality" column="risk_criticality"/>
        <result property="quantitativeAnalysis" column="quantitative_analysis"/>
        <result property="maintenanceStrategy" column="maintenance_strategy"/>
        <result property="msDescription" column="ms_description"/>
        <result property="sparepartStrategy" column="sparepart_strategy"/>
        <result property="ssDescription" column="ss_description"/>
        <result property="needClarification" column="need_clarification"/>
        <result property="remarks" column="remarks"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createrCode" column="creater_code"/>
        <result property="createdate" column="createdate"/>
        <result property="updaterCode" column="updater_code"/>
        <result property="updatedate" column="updatedate"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="materialSpecification" column="material_specification"/>
        <result property="specificationMode" column="specification_mode"/>
    </resultMap>

    <resultMap type="TRiskMatrixVo" id="TRiskMatrixVoResult">
        <result property="mrhb2" column="mrhb2"/>
        <result property="mrha1" column="mrha1"/>
        <result property="mrha0" column="mrha0"/>
        <result property="mrmc2" column="mrmc2"/>
        <result property="mrmb1" column="mrmb1"/>
        <result property="mrma0" column="mrma0"/>
        <result property="mrlc2" column="mrlc2"/>
        <result property="mrlc1" column="mrlc1"/>
        <result property="mrlb0" column="mrlb0"/>
        <result property="prhb2" column="prhb2"/>
        <result property="prha1" column="prha1"/>
        <result property="prha0" column="prha0"/>
        <result property="prmc2" column="prmc2"/>
        <result property="prmb1" column="prmb1"/>
        <result property="prma0" column="prma0"/>
        <result property="prlc2" column="prlc2"/>
        <result property="prlc1" column="prlc1"/>
        <result property="prlb0" column="prlb0"/>
        <result property="echc" column="echc"/>
        <result property="echb" column="echb"/>
        <result property="echa" column="echa"/>
        <result property="ecmc" column="ecmc"/>
        <result property="ecmmb" column="ecmmb"/>
        <result property="ecmhb" column="ecmhb"/>
        <result property="ecld" column="ecld"/>
        <result property="eclc" column="eclc"/>
        <result property="eclb" column="eclb"/>
        <result property="nr" column="nr"/>
        <result property="omr" column="omr"/>
        <result property="opr" column="opr"/>
        <result property="countA" column="countA"/>
        <result property="countB" column="countB"/>
        <result property="countC" column="countC"/>
        <result property="countD" column="countD"/>
        <result property="totalA" column="totalA"/>
        <result property="totalB" column="totalB"/>
        <result property="totalC" column="totalC"/>
        <result property="totalD" column="totalD"/>
    </resultMap>

    <select id="selectTRiskMatrixVoData" resultMap="TRiskMatrixVoResult">
        select
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'B2' and maintenance_cost = 'low' and frequency_failure = 'high' ) mrhb2,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'A1' and maintenance_cost = 'medium' and frequency_failure = 'high' ) mrha1,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'A0' and maintenance_cost = 'high' and frequency_failure = 'high' ) mrha0,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'C2' and maintenance_cost = 'low' and frequency_failure = 'medium' ) mrmc2,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'B1' and maintenance_cost = 'medium' and frequency_failure = 'medium' ) mrmb1,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'A0' and maintenance_cost = 'high' and frequency_failure = 'medium' ) mrma0,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'C2' and maintenance_cost = 'low' and frequency_failure = 'low' ) mrlc2,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'C1' and maintenance_cost = 'medium' and frequency_failure = 'low' ) mrlc1,
            ( select count( 1 ) from t_qualitative_analysis where maintenance_risk = 'B0' and maintenance_cost = 'high' and frequency_failure = 'low' ) mrlb0,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'B2' and production_loss = 'low' and frequency_failure = 'high' ) prhb2,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'A1' and production_loss = 'medium' and frequency_failure = 'high' ) prha1,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'A0' and production_loss = 'high' and frequency_failure = 'high' ) prha0,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'C2' and production_loss = 'low' and frequency_failure = 'medium' ) prmc2,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'B1' and production_loss = 'medium' and frequency_failure = 'medium' ) prmb1,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'A0' and production_loss = 'high' and frequency_failure = 'medium' ) prma0,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'C2' and production_loss = 'low' and frequency_failure = 'low' ) prlc2,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'C1' and production_loss = 'medium' and frequency_failure = 'low' ) prlc1,
            ( select count( 1 ) from t_qualitative_analysis where production_risk = 'B0' and production_loss = 'high' and frequency_failure = 'low' ) prlb0,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'C' and production_loss = 'low' and maintenance_cost = 'high' ) echc,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'B' and production_loss = 'medium' and maintenance_cost = 'high' ) echb,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'A' and production_loss = 'high' and maintenance_cost = 'high' ) echa,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'C' and production_loss = 'low' and maintenance_cost = 'medium' ) ecmc,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'B' and production_loss = 'medium' and maintenance_cost = 'medium' ) ecmmb,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'B' and production_loss = 'high' and maintenance_cost = 'medium' ) ecmhb,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'D' and production_loss = 'low' and maintenance_cost = 'low' ) ecld,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'C' and production_loss = 'medium' and maintenance_cost = 'low' ) eclc,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'B' and production_loss = 'high' and maintenance_cost = 'low' ) eclb,
            ( select count( 1 ) from t_qualitative_analysis where RELEVANT = 'n') nr,
            ( select count( 1 ) from t_qualitative_analysis where MAINTENANCE_RISK is null) omr,
            ( select count( 1 ) from t_qualitative_analysis where PRODUCTION_RISK is null) opr,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'A') countA,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'B') countB,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'C') countC,
            ( select count( 1 ) from t_qualitative_analysis where equipment_category = 'D') countD
        from
            dual
    </select>

    <sql id="selectTQualitativeAnalysisVo">
        select d.id,
               d.location,
               d.tag_no,
               d.description,
               d.object_type,
               d.catalog_profile,
               d.relevant,
               d.frequency_failure,
               d.maintenance_cost,
               d.production_loss,
               d.maintenance_risk,
               d.production_risk,
               d.equipment_category,
               d.risk_criticality,
               d.quantitative_analysis,
               d.maintenance_strategy,
               d.ms_description,
               d.sparepart_strategy,
               d.ss_description,
               d.need_clarification,
               d.remarks,
               d.del_flag,
               d.creater_code,
               d.createdate,
               d.updater_code,
               d.updatedate,
               d.dept_id,
               d.material_specification,
               d.specification_mode,
               s.dept_name
        from t_qualitative_analysis d
                 left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTQualitativeAnalysisList" parameterType="TQualitativeAnalysis"
            resultMap="TQualitativeAnalysisResult">
        <include refid="selectTQualitativeAnalysisVo"/>
        <where>
            <if test="location != null  and location != ''">and location = #{location}</if>
            <if test="tagNo != null  and tagNo != ''">and tag_no = #{tagNo}</if>
            <if test="description != null  and description != ''">and description = #{description}</if>
            <if test="objectType != null  and objectType != ''">and object_type = #{objectType}</if>
            <if test="catalogProfile != null  and catalogProfile != ''">and catalog_profile = #{catalogProfile}</if>
            <if test="relevant != null  and relevant != ''">and relevant = #{relevant}</if>
            <if test="frequencyFailure != null  and frequencyFailure != ''">and frequency_failure =
                #{frequencyFailure}
            </if>
            <if test="maintenanceCost != null  and maintenanceCost != ''">and maintenance_cost = #{maintenanceCost}</if>
            <if test="productionLoss != null  and productionLoss != ''">and production_loss = #{productionLoss}</if>
            <if test="maintenanceRisk != null  and maintenanceRisk != ''">and maintenance_risk = #{maintenanceRisk}</if>
            <if test="productionRisk != null  and productionRisk != ''">and production_risk = #{productionRisk}</if>
            <if test="equipmentCategory != null  and equipmentCategory != ''">and equipment_category =
                #{equipmentCategory}
            </if>
            <if test="riskCriticality != null  and riskCriticality != ''">and risk_criticality = #{riskCriticality}</if>
            <if test="quantitativeAnalysis != null  and quantitativeAnalysis != ''">and quantitative_analysis =
                #{quantitativeAnalysis}
            </if>
            <if test="maintenanceStrategy != null  and maintenanceStrategy != ''">and maintenance_strategy =
                #{maintenanceStrategy}
            </if>
            <if test="msDescription != null  and msDescription != ''">and ms_description = #{msDescription}</if>
            <if test="sparepartStrategy != null  and sparepartStrategy != ''">and sparepart_strategy =
                #{sparepartStrategy}
            </if>
            <if test="ssDescription != null  and ssDescription != ''">and ss_description = #{ssDescription}</if>
            <if test="needClarification != null  and needClarification != ''">and need_clarification =
                #{needClarification}
            </if>
            <if test="remarks != null  and remarks != ''">and remarks = #{remarks}</if>
            <if test="createrCode != null  and createrCode != ''">and creater_code = #{createrCode}</if>
            <if test="createdate != null ">and createdate = #{createdate}</if>
            <if test="updaterCode != null  and updaterCode != ''">and updater_code = #{updaterCode}</if>
            <if test="updatedate != null ">and updatedate = #{updatedate}</if>
            <if test="deptId != null ">and dept_id = #{deptId}</if>
            <if test="materialSpecification != null ">and material_specification = #{materialSpecification}</if>
            <if test="specificationMode != null ">and specification_mode = #{specificationMode}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTQualitativeAnalysisById" parameterType="Long" resultMap="TQualitativeAnalysisResult">
        <include refid="selectTQualitativeAnalysisVo"/>
        where id = #{id}
    </select>

    <insert id="insertTQualitativeAnalysis" parameterType="TQualitativeAnalysis">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_qualitative_analysis.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_qualitative_analysis
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="location != null">location,</if>
            <if test="tagNo != null">tag_no,</if>
            <if test="description != null">description,</if>
            <if test="objectType != null">object_type,</if>
            <if test="catalogProfile != null">catalog_profile,</if>
            <if test="relevant != null">relevant,</if>
            <if test="frequencyFailure != null">frequency_failure,</if>
            <if test="maintenanceCost != null">maintenance_cost,</if>
            <if test="productionLoss != null">production_loss,</if>
            <if test="maintenanceRisk != null">maintenance_risk,</if>
            <if test="productionRisk != null">production_risk,</if>
            <if test="equipmentCategory != null">equipment_category,</if>
            <if test="riskCriticality != null">risk_criticality,</if>
            <if test="quantitativeAnalysis != null">quantitative_analysis,</if>
            <if test="maintenanceStrategy != null">maintenance_strategy,</if>
            <if test="msDescription != null">ms_description,</if>
            <if test="sparepartStrategy != null">sparepart_strategy,</if>
            <if test="ssDescription != null">ss_description,</if>
            <if test="needClarification != null">need_clarification,</if>
            <if test="remarks != null">remarks,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="materialSpecification != null">material_specification,</if>
            <if test="specificationMode != null">specification_mode,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="location != null">#{location},</if>
            <if test="tagNo != null">#{tagNo},</if>
            <if test="description != null">#{description},</if>
            <if test="objectType != null">#{objectType},</if>
            <if test="catalogProfile != null">#{catalogProfile},</if>
            <if test="relevant != null">#{relevant},</if>
            <if test="frequencyFailure != null">#{frequencyFailure},</if>
            <if test="maintenanceCost != null">#{maintenanceCost},</if>
            <if test="productionLoss != null">#{productionLoss},</if>
            <if test="maintenanceRisk != null">#{maintenanceRisk},</if>
            <if test="productionRisk != null">#{productionRisk},</if>
            <if test="equipmentCategory != null">#{equipmentCategory},</if>
            <if test="riskCriticality != null">#{riskCriticality},</if>
            <if test="quantitativeAnalysis != null">#{quantitativeAnalysis},</if>
            <if test="maintenanceStrategy != null">#{maintenanceStrategy},</if>
            <if test="msDescription != null">#{msDescription},</if>
            <if test="sparepartStrategy != null">#{sparepartStrategy},</if>
            <if test="ssDescription != null">#{ssDescription},</if>
            <if test="needClarification != null">#{needClarification},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="materialSpecification != null">#{materialSpecification},</if>
            <if test="specificationMode != null">#{specificationMode},</if>
        </trim>
    </insert>

    <update id="updateTQualitativeAnalysis" parameterType="TQualitativeAnalysis">
        update t_qualitative_analysis
        <trim prefix="SET" suffixOverrides=",">
            <if test="location != null">location = #{location},</if>
            <if test="tagNo != null">tag_no = #{tagNo},</if>
            <if test="description != null">description = #{description},</if>
            <if test="objectType != null">object_type = #{objectType},</if>
            <if test="catalogProfile != null">catalog_profile = #{catalogProfile},</if>
            <if test="relevant != null">relevant = #{relevant},</if>
            <if test="frequencyFailure != null">frequency_failure = #{frequencyFailure},</if>
            <if test="maintenanceCost != null">maintenance_cost = #{maintenanceCost},</if>
            <if test="productionLoss != null">production_loss = #{productionLoss},</if>
            <if test="maintenanceRisk != null">maintenance_risk = #{maintenanceRisk},</if>
            <if test="productionRisk != null">production_risk = #{productionRisk},</if>
            <if test="equipmentCategory != null">equipment_category = #{equipmentCategory},</if>
            <if test="riskCriticality != null">risk_criticality = #{riskCriticality},</if>
            <if test="quantitativeAnalysis != null">quantitative_analysis = #{quantitativeAnalysis},</if>
            <if test="maintenanceStrategy != null">maintenance_strategy = #{maintenanceStrategy},</if>
            <if test="msDescription != null">ms_description = #{msDescription},</if>
            <if test="sparepartStrategy != null">sparepart_strategy = #{sparepartStrategy},</if>
            <if test="ssDescription != null">ss_description = #{ssDescription},</if>
            <if test="needClarification != null">need_clarification = #{needClarification},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTQualitativeAnalysisById" parameterType="Long">
        update t_qualitative_analysis
        set del_flag = 2
        where id = #{id}
    </update>

    <update id="deleteTQualitativeAnalysisByIds" parameterType="String">
        update t_qualitative_analysis set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
