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

    <resultMap type="QmsCheckProject" id="QmsCheckProjectResult">
        <result 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"    />
        <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>

    <sql id="selectQmsCheckProjectVo">
        select check_project_id, check_project_name, check_project_status, check_project_property, check_mode, check_sample, check_tool, unit_code, standard_value, upper_diff, down_diff, defect_severity, remark, create_by, create_time, update_by, update_time from qms_check_project
    </sql>

    <select id="selectQmsCheckProjectList" parameterType="QmsCheckProject" resultMap="QmsCheckProjectResult">
        <include refid="selectQmsCheckProjectVo"/>
        <where>
            <if test="checkProjectName != null  and checkProjectName != ''"> and check_project_name like concat('%', #{checkProjectName}, '%')</if>
            <if test="checkProjectStatus != null  and checkProjectStatus != ''"> and check_project_status = #{checkProjectStatus}</if>
            <if test="checkProjectProperty != null  and checkProjectProperty != ''"> and check_project_property = #{checkProjectProperty}</if>
            <if test="checkMode != null  and checkMode != ''"> and check_mode = #{checkMode}</if>
            <if test="checkSample != null "> and check_sample = #{checkSample}</if>
            <if test="checkTool != null  and checkTool != ''"> and check_tool = #{checkTool}</if>
            <if test="unitCode != null  and unitCode != ''"> and unit_code = #{unitCode}</if>
            <if test="standardValue != null "> and standard_value = #{standardValue}</if>
            <if test="upperDiff != null "> and upper_diff = #{upperDiff}</if>
            <if test="downDiff != null "> and down_diff = #{downDiff}</if>
            <if test="defectSeverity != null  and defectSeverity != ''"> and defect_severity = #{defectSeverity}</if>
        </where>
    </select>

    <select id="selectQmsCheckProjectByCheckProjectId" parameterType="Long" resultMap="QmsCheckProjectResult">
        <include refid="selectQmsCheckProjectVo"/>
        where check_project_id = #{checkProjectId}
    </select>

    <insert id="insertQmsCheckProject" parameterType="QmsCheckProject" useGeneratedKeys="true" keyProperty="checkProjectId">
        insert into qms_check_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="checkProjectName != null and checkProjectName != ''">check_project_name,</if>
            <if test="checkProjectStatus != null and checkProjectStatus != ''">check_project_status,</if>
            <if test="checkProjectProperty != null and checkProjectProperty != ''">check_project_property,</if>
            <if test="checkMode != null and checkMode != ''">check_mode,</if>
            <if test="checkSample != null">check_sample,</if>
            <if test="checkTool != null">check_tool,</if>
            <if test="unitCode != null">unit_code,</if>
            <if test="standardValue != null">standard_value,</if>
            <if test="upperDiff != null">upper_diff,</if>
            <if test="downDiff != null">down_diff,</if>
            <if test="defectSeverity != null">defect_severity,</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="checkProjectName != null and checkProjectName != ''">#{checkProjectName},</if>
            <if test="checkProjectStatus != null and checkProjectStatus != ''">#{checkProjectStatus},</if>
            <if test="checkProjectProperty != null and checkProjectProperty != ''">#{checkProjectProperty},</if>
            <if test="checkMode != null and checkMode != ''">#{checkMode},</if>
            <if test="checkSample != null">#{checkSample},</if>
            <if test="checkTool != null">#{checkTool},</if>
            <if test="unitCode != null">#{unitCode},</if>
            <if test="standardValue != null">#{standardValue},</if>
            <if test="upperDiff != null">#{upperDiff},</if>
            <if test="downDiff != null">#{downDiff},</if>
            <if test="defectSeverity != null">#{defectSeverity},</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="updateQmsCheckProject" parameterType="QmsCheckProject">
        update qms_check_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="checkProjectName != null and checkProjectName != ''">check_project_name = #{checkProjectName},</if>
            <if test="checkProjectStatus != null and checkProjectStatus != ''">check_project_status = #{checkProjectStatus},</if>
            <if test="checkProjectProperty != null and checkProjectProperty != ''">check_project_property = #{checkProjectProperty},</if>
            <if test="checkMode != null and checkMode != ''">check_mode = #{checkMode},</if>
            <if test="checkSample != null">check_sample = #{checkSample},</if>
            <if test="checkTool != null">check_tool = #{checkTool},</if>
            <if test="unitCode != null">unit_code = #{unitCode},</if>
            standard_value = #{standardValue},
            upper_diff = #{upperDiff},
            down_diff = #{downDiff},
            <if test="defectSeverity != null">defect_severity = #{defectSeverity},</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_project_id = #{checkProjectId}
    </update>

    <delete id="deleteQmsCheckProjectByCheckProjectId" parameterType="Long">
        delete from qms_check_project where check_project_id = #{checkProjectId}
    </delete>

    <delete id="deleteQmsCheckProjectByCheckProjectIds" parameterType="String">
        delete from qms_check_project where check_project_id in
        <foreach item="checkProjectId" collection="array" open="(" separator="," close=")">
            #{checkProjectId}
        </foreach>
    </delete>










    <select id="selectQmsCheckProjects4AllocateCheckRule" parameterType="QmsCheckProject" resultMap="QmsCheckProjectResult">
        select qcp.check_project_id, qcp.check_project_name from qms_check_project qcp
        <where>
            and not exists (select 1 from qms_check_rule_project qcrp where qcrp.check_project_id = qcp.check_project_id and qcrp.check_rule_id=#{checkRuleId})

            <if test="checkProjectName != null  and checkProjectName != ''"> and check_project_name like concat('%', #{checkProjectName}, '%')</if>
            <if test="checkProjectStatus != null  and checkProjectStatus != ''"> and check_project_status = #{checkProjectStatus}</if>
            <if test="checkProjectProperty != null  and checkProjectProperty != ''"> and check_project_property = #{checkProjectProperty}</if>
            <if test="checkMode != null  and checkMode != ''"> and check_mode = #{checkMode}</if>
            <if test="checkSample != null "> and check_sample = #{checkSample}</if>
            <if test="checkTool != null  and checkTool != ''"> and check_tool = #{checkTool}</if>
            <if test="unitCode != null  and unitCode != ''"> and unit_code = #{unitCode}</if>
            <if test="standardValue != null "> and standard_value = #{standardValue}</if>
            <if test="upperDiff != null "> and upper_diff = #{upperDiff}</if>
            <if test="downDiff != null "> and down_diff = #{downDiff}</if>
            <if test="defectSeverity != null  and defectSeverity != ''"> and defect_severity = #{defectSeverity}</if>
        </where>
    </select>
</mapper>
