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

    <resultMap type="QmsCheckRuleDetail" id="QmsCheckRuleDetailResult">
        <result property="checkRuleId"    column="check_rule_id"    />
        <result property="targetType"    column="target_type"    />
        <result property="targetId"    column="target_id"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <sql id="selectQmsCheckRuleDetailVo">
        select check_rule_id, target_type, target_id from qms_check_rule_detail
    </sql>

    <select id="selectQmsCheckRuleDetailList" parameterType="QmsCheckRuleDetail" resultMap="QmsCheckRuleDetailResult">
        <include refid="selectQmsCheckRuleDetailVo"/>
        <where>
            <if test="checkRuleId != null "> and check_rule_id = #{checkRuleId}</if>
            <if test="targetType != null  and targetType != ''"> and target_type = #{targetType}</if>
            <if test="targetId != null "> and target_id = #{targetId}</if>
        </where>
    </select>

    <select id="selectQmsCheckRuleDetailByCheckRuleId" parameterType="Long" resultMap="QmsCheckRuleDetailResult">
        <include refid="selectQmsCheckRuleDetailVo"/>
        where check_rule_id = #{checkRuleId}
    </select>

    <insert id="insertQmsCheckRuleDetail" parameterType="QmsCheckRuleDetail">
        insert into qms_check_rule_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="checkRuleId != null">check_rule_id,</if>
            <if test="targetType != null and targetType != ''">target_type,</if>
            <if test="targetId != null">target_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="checkRuleId != null">#{checkRuleId},</if>
            <if test="targetType != null and targetType != ''">#{targetType},</if>
            <if test="targetId != null">#{targetId},</if>
         </trim>
    </insert>

    <update id="updateQmsCheckRuleDetail" parameterType="QmsCheckRuleDetail">
        update qms_check_rule_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="targetType != null and targetType != ''">target_type = #{targetType},</if>
            <if test="targetId != null">target_id = #{targetId},</if>
        </trim>
        where check_rule_id = #{checkRuleId}
    </update>

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

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
















    <select id="selectQmsCheckRuleDetailJoinList" parameterType="QmsCheckRuleDetail" resultMap="QmsCheckRuleDetailResult">
        select qcrd.check_rule_id, qcrd.target_type, qcrd.target_id,
        mbmi.material_code,mbmi.material_name
        from qms_check_rule_detail qcrd
        left join mes_base_material_info mbmi on qcrd.target_id=mbmi.material_id and qcrd.target_type='1'
        <where>
            <if test="checkRuleId != null "> and check_rule_id = #{checkRuleId}</if>
            <if test="targetType != null  and targetType != ''"> and target_type = #{targetType}</if>
            <if test="targetId != null "> and target_id = #{targetId}</if>
            <if test="materialCode != null  and materialCode != ''"> and material_code = #{materialCode}</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
        </where>
    </select>


    <delete id="deleteQmsCheckRuleDetails">
        delete from qms_check_rule_detail where
        <foreach item="item" index="index" collection="list" separator="or">
            ( check_rule_id = #{item.checkRuleId} and target_id = #{item.targetId} and target_type=#{item.targetType})
        </foreach>
    </delete>


</mapper>
