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

    <resultMap type="QmsCheckRule" id="QmsCheckRuleResult">
        <result property="checkRuleId"    column="check_rule_id"    />
        <result property="checkRuleName"    column="check_rule_name"    />
        <result property="checkRuleType"    column="check_rule_type"    />
        <result property="checkMode"    column="check_mode"    />
        <result property="checkSample"    column="check_sample"    />
        <result property="checkRuleStatus"    column="check_rule_status"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap id="QmsCheckRuleQmsCheckRuleProjectResult" type="QmsCheckRule" extends="QmsCheckRuleResult">
        <collection property="qmsCheckRuleProjectList" notNullColumn="sub_check_rule_id" javaType="java.util.List" resultMap="QmsCheckRuleProjectResult" />
    </resultMap>

    <resultMap type="QmsCheckRuleProject" id="QmsCheckRuleProjectResult">
        <result property="checkRuleId"    column="sub_check_rule_id"    />
        <result property="checkProjectId"    column="sub_check_project_id"    />
        <result property="projectStepOrder"    column="sub_project_step_order"    />
    </resultMap>



    <sql id="selectQmsCheckRuleVo">
        select check_rule_id, check_rule_name, check_rule_type, check_mode, check_sample, check_rule_status, remark, create_by, create_time, update_by, update_time from qms_check_rule
    </sql>

    <select id="selectQmsCheckRuleList" parameterType="QmsCheckRule" resultMap="QmsCheckRuleResult">
        <include refid="selectQmsCheckRuleVo"/>
        <where>
            <if test="checkRuleName != null  and checkRuleName != ''"> and check_rule_name like concat('%', #{checkRuleName}, '%')</if>
            <if test="checkRuleType != null  and checkRuleType != ''"> and check_rule_type = #{checkRuleType}</if>
            <if test="checkMode != null  and checkMode != ''"> and check_mode = #{checkMode}</if>
            <if test="checkSample != null "> and check_sample = #{checkSample}</if>
            <if test="checkRuleStatus != null  and checkRuleStatus != ''"> and check_rule_status = #{checkRuleStatus}</if>
        </where>
    </select>

    <select id="selectQmsCheckRuleByCheckRuleId" parameterType="Long" resultMap="QmsCheckRuleQmsCheckRuleProjectResult">
        select a.check_rule_id, a.check_rule_name, a.check_rule_type, a.check_mode, a.check_sample, a.check_rule_status, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,
               b.check_rule_id as sub_check_rule_id, b.check_project_id as sub_check_project_id, b.project_step_order as sub_project_step_order
        from qms_check_rule a
                 left join qms_check_rule_project b on b.check_rule_id = a.check_rule_id
        where a.check_rule_id = #{checkRuleId} order by b.project_step_order
    </select>

    <insert id="insertQmsCheckRule" parameterType="QmsCheckRule" useGeneratedKeys="true" keyProperty="checkRuleId">
        insert into qms_check_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="checkRuleName != null and checkRuleName != ''">check_rule_name,</if>
            <if test="checkRuleType != null and checkRuleType != ''">check_rule_type,</if>
            <if test="checkMode != null">check_mode,</if>
            <if test="checkSample != null">check_sample,</if>
            <if test="checkRuleStatus != null and checkRuleStatus != ''">check_rule_status,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="checkRuleName != null and checkRuleName != ''">#{checkRuleName},</if>
            <if test="checkRuleType != null and checkRuleType != ''">#{checkRuleType},</if>
            <if test="checkMode != null">#{checkMode},</if>
            <if test="checkSample != null">#{checkSample},</if>
            <if test="checkRuleStatus != null and checkRuleStatus != ''">#{checkRuleStatus},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateQmsCheckRule" parameterType="QmsCheckRule">
        update qms_check_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="checkRuleName != null and checkRuleName != ''">check_rule_name = #{checkRuleName},</if>
            <if test="checkRuleType != null and checkRuleType != ''">check_rule_type = #{checkRuleType},</if>
            <if test="checkMode != null">check_mode = #{checkMode},</if>
            check_sample = #{checkSample},
            <if test="checkRuleStatus != null and checkRuleStatus != ''">check_rule_status = #{checkRuleStatus},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where check_rule_id = #{checkRuleId}
    </update>

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

    <delete id="deleteQmsCheckRuleByCheckRuleIds" parameterType="String">
        delete from qms_check_rule where check_rule_id in
        <foreach item="checkRuleId" collection="array" open="(" separator="," close=")">
            #{checkRuleId}
        </foreach>
    </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>

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

    <insert id="batchQmsCheckRuleDetail">
        insert into qms_check_rule_detail( check_rule_id, target_type, target_id) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.checkRuleId}, #{item.targetType}, #{item.targetId})
        </foreach>
    </insert>




    <insert id="batchQmsCheckRuleProject">
        insert into qms_check_rule_project( check_rule_id, check_project_id, project_step_order) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.checkRuleId}, #{item.checkProjectId}, #{item.projectStepOrder})
        </foreach>
    </insert>

</mapper>
