<?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.ximai.mes.qc.mapper.QcAbnormalReportMapper">

    <resultMap type="QcAbnormalReport" id="QcAbnormalReportResult">
        <result property="abnormalReportId"    column="abnormal_report_id"    />
        <result property="batchNumber"    column="batch_number"    />
        <result property="processId"    column="process_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="taskWorkunitId"    column="task_workunit_id"    />
        <result property="processCode"    column="process_code"    />
        <result property="processName"    column="process_name"    />
        <result property="workstationId"    column="workstation_id"    />
        <result property="workstationCode"    column="workstation_code"    />
        <result property="workstationName"    column="workstation_name"    />
        <result property="abnormalType"    column="abnormal_type"    />
        <result property="abnormalReason"    column="abnormal_reason"    />
        <result property="abnormalTime"    column="abnormal_time"    />
        <result property="abnormalStatus"    column="abnormal_status"    />
        <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="selectQcAbnormalReportVo">
        select abnormal_report_id, batch_number, process_id, task_id, task_workunit_id, process_code, process_name, workstation_id, workstation_code, workstation_name, abnormal_type, abnormal_reason, abnormal_time, abnormal_status, create_by, create_time, update_by, update_time
        from qc_abnormal_report
    </sql>

    <select id="selectQcAbnormalReportList" parameterType="QcAbnormalReport" resultMap="QcAbnormalReportResult">
        <include refid="selectQcAbnormalReportVo"/>
        <where>
            <if test="batchNumber != null  and batchNumber != ''"> and batch_number = #{batchNumber}</if>
            <if test="processId != null "> and process_id = #{processId}</if>
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="taskWorkunitId != null "> and task_workunit_id = #{taskWorkunitId}</if>
            <if test="processCode != null  and processCode != ''"> and process_code = #{processCode}</if>
            <if test="processName != null  and processName != ''"> and process_name like concat('%', #{processName}, '%')</if>
            <if test="workstationId != null "> and workstation_id = #{workstationId}</if>
            <if test="workstationCode != null  and workstationCode != ''"> and workstation_code = #{workstationCode}</if>
            <if test="workstationName != null  and workstationName != ''"> and workstation_name like concat('%', #{workstationName}, '%')</if>
            <if test="abnormalType != null  and abnormalType != ''"> and abnormal_type = #{abnormalType}</if>
            <if test="abnormalReason != null  and abnormalReason != ''"> and abnormal_reason = #{abnormalReason}</if>
            <if test="abnormalTime != null "> and abnormal_time = #{abnormalTime}</if>
            <if test="beginPurchaseDate != null "> abnormal_time BETWEEN #{beginPurchaseDate} </if>
            <if test="endPurchaseDate != null "> and  #{endPurchaseDate} </if>
            <if test="abnormalStatus != null  and abnormalStatus != ''"> and abnormal_status = #{abnormalStatus}</if>
        </where>
    </select>

    <select id="selectQcAbnormalReportByAbnormalReportId" parameterType="Long" resultMap="QcAbnormalReportResult">
        <include refid="selectQcAbnormalReportVo"/>
        where abnormal_report_id = #{abnormalReportId}
    </select>

    <insert id="insertQcAbnormalReport" parameterType="QcAbnormalReport" useGeneratedKeys="true" keyProperty="abnormalReportId">
        insert into qc_abnormal_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchNumber != null">batch_number,</if>
            <if test="processId != null">process_id,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskWorkunitId != null">task_workunit_id,</if>
            <if test="processCode != null">process_code,</if>
            <if test="processName != null">process_name,</if>
            <if test="workstationId != null">workstation_id,</if>
            <if test="workstationCode != null">workstation_code,</if>
            <if test="workstationName != null">workstation_name,</if>
            <if test="abnormalType != null">abnormal_type,</if>
            <if test="abnormalReason != null">abnormal_reason,</if>
            <if test="abnormalTime != null">abnormal_time,</if>
            <if test="abnormalStatus != null">abnormal_status,</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="batchNumber != null">#{batchNumber},</if>
            <if test="processId != null">#{processId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskWorkunitId != null">#{taskWorkunitId},</if>
            <if test="processCode != null">#{processCode},</if>
            <if test="processName != null">#{processName},</if>
            <if test="workstationId != null">#{workstationId},</if>
            <if test="workstationCode != null">#{workstationCode},</if>
            <if test="workstationName != null">#{workstationName},</if>
            <if test="abnormalType != null">#{abnormalType},</if>
            <if test="abnormalReason != null">#{abnormalReason},</if>
            <if test="abnormalTime != null">#{abnormalTime},</if>
            <if test="abnormalStatus != null">#{abnormalStatus},</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="updateQcAbnormalReport" parameterType="QcAbnormalReport">
        update qc_abnormal_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="batchNumber != null">batch_number = #{batchNumber},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskWorkunitId != null">task_workunit_id = #{taskWorkunitId},</if>
            <if test="processCode != null">process_code = #{processCode},</if>
            <if test="processName != null">process_name = #{processName},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="workstationCode != null">workstation_code = #{workstationCode},</if>
            <if test="workstationName != null">workstation_name = #{workstationName},</if>
            <if test="abnormalType != null">abnormal_type = #{abnormalType},</if>
            <if test="abnormalReason != null">abnormal_reason = #{abnormalReason},</if>
            <if test="abnormalTime != null">abnormal_time = #{abnormalTime},</if>
            <if test="abnormalStatus != null">abnormal_status = #{abnormalStatus},</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 abnormal_report_id = #{abnormalReportId}
    </update>

    <delete id="deleteQcAbnormalReportByAbnormalReportId" parameterType="Long">
        delete from qc_abnormal_report where abnormal_report_id = #{abnormalReportId}
    </delete>

    <delete id="deleteQcAbnormalReportByAbnormalReportIds" parameterType="String">
        delete from qc_abnormal_report where abnormal_report_id in
        <foreach item="abnormalReportId" collection="array" open="(" separator="," close=")">
            #{abnormalReportId}
        </foreach>
    </delete>
</mapper>