<?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.hw.qms.mapper.QmsCheckRuleProjectMapper">

    <resultMap type="QmsCheckRuleProject" id="QmsCheckRuleProjectResult">
        <result property="checkRuleId"    column="check_rule_id"    />
        <result property="checkProjectId"    column="check_project_id"    />
        <result property="projectStepOrder"    column="project_step_order"    />
        <association property="CheckProject"    javaType="QmsCheckProject"         resultMap="CheckProjectResult" />
    </resultMap>

    <resultMap id="CheckProjectResult" type="QmsCheckProject">
        <id property="checkProjectId"    column="check_project_id"    />
        <result property="checkProjectName"    column="check_project_name"    />
        <result property="checkProjectStatus"    column="check_project_status"    />
        <result property="checkProjectProperty"    column="check_project_property"    />
        <result property="checkMode"    column="check_mode"    />
        <result property="checkSample"    column="check_sample"    />
        <result property="checkTool"    column="check_tool"    />
        <result property="unitCode"    column="unit_code"    />
        <result property="standardValue"    column="standard_value"    />
        <result property="upperDiff"    column="upper_diff"    />
        <result property="downDiff"    column="down_diff"    />
        <result property="defectSeverity"    column="defect_severity"    />
    </resultMap>


    <sql id="selectQmsCheckRuleProjectVo">
        select check_rule_id, check_project_id, project_step_order from qms_check_rule_project
    </sql>

    <select id="selectQmsCheckRuleProjectList" parameterType="QmsCheckRuleProject" resultMap="QmsCheckRuleProjectResult">
        <include refid="selectQmsCheckRuleProjectVo"/>
        <where>
            <if test="projectStepOrder != null "> and project_step_order = #{projectStepOrder}</if>
        </where>
    </select>

    <select id="selectQmsCheckRuleProjectByCheckRuleId" parameterType="Long" resultMap="QmsCheckRuleProjectResult">
        <include refid="selectQmsCheckRuleProjectVo"/>
        where check_rule_id = #{checkRuleId}
    </select>

    <insert id="insertQmsCheckRuleProject" parameterType="QmsCheckRuleProject">
        insert into qms_check_rule_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="checkRuleId != null">check_rule_id,</if>
            <if test="checkProjectId != null">check_project_id,</if>
            <if test="projectStepOrder != null">project_step_order,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="checkRuleId != null">#{checkRuleId},</if>
            <if test="checkProjectId != null">#{checkProjectId},</if>
            <if test="projectStepOrder != null">#{projectStepOrder},</if>
         </trim>
    </insert>

    <update id="updateQmsCheckRuleProject" parameterType="QmsCheckRuleProject">
        update qms_check_rule_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="checkProjectId != null">check_project_id = #{checkProjectId},</if>
            <if test="projectStepOrder != null">project_step_order = #{projectStepOrder},</if>
        </trim>
        where check_rule_id = #{checkRuleId}
    </update>

    <delete id="deleteQmsCheckRuleProjectByCheckRuleId" parameterType="Long">
        delete from qms_check_rule_project where check_rule_id = #{checkRuleId}
    </delete>

    <delete id="deleteQmsCheckRuleProjectByCheckRuleIds" parameterType="String">
        delete from qms_check_rule_project where check_rule_id in
        <foreach item="checkRuleId" collection="array" open="(" separator="," close=")">
            #{checkRuleId}
        </foreach>
    </delete>






    <select id="selectQmsCheckRuleProjectListByTarget" parameterType="QmsCheckRuleProject" resultMap="QmsCheckRuleProjectResult">
        select qcrp.check_rule_id, qcrp.check_project_id, qcrp.project_step_order,
               qcp.check_project_name,qcp.check_project_property,qcp.standard_value,qcp.upper_diff,qcp.down_diff
        from qms_check_rule_project qcrp left join qms_check_project qcp on qcrp.check_project_id = qcp.check_project_id
        <where>
            <if test="projectStepOrder != null "> and project_step_order = #{projectStepOrder}</if>
            <if test="targetId != null and targetType!=null and targetType!=''"> and exists (select 1 from qms_check_rule_detail qcrd where
                                                              qcrd.check_rule_id=qcrd.check_rule_id and qcrd.target_type=#{targetType} and qcrd.target_id=#{targetId})</if>
        </where>
    </select>


    <delete id="deleteQmsCheckRuleProjects">
        delete from qms_check_rule_project where
        <foreach item="item" index="index" collection="list" separator="or">
            ( check_rule_id = #{item.checkRuleId} and  check_project_id = #{item.checkProjectId})
        </foreach>
    </delete>


    <select id="selectQmsCheckRuleProjectJoinList" parameterType="QmsCheckRuleProject" resultMap="QmsCheckRuleProjectResult">
        select qcrp.check_rule_id, qcrp.check_project_id, qcrp.project_step_order,qcp.check_project_name
        from qms_check_rule_project qcrp left join qms_check_project qcp on qcrp.check_project_id = qcp.check_project_id

        <where>
            <if test="projectStepOrder != null "> and project_step_order = #{projectStepOrder}</if>
            <if test="checkProjectName != null "> and check_project_name like concat('%', #{checkProjectName},
                '%')</if>
        </where>
    </select>

</mapper>
