<?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="workunitId"    column="workunit_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"    />
        <result column="abnormal_number" jdbcType="INTEGER" property="abnormalNumber" />
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
        <result column="start_by" jdbcType="VARCHAR" property="startBy" />
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime" />
        <result column="end_by" jdbcType="VARCHAR" property="endBy" />
        <result column="time_consuming" jdbcType="BIGINT" property="timeConsuming" />
    </resultMap>

    <sql id="selectQcAbnormalReportVo">
        select abnormal_report_id, batch_number, process_id, task_id, task_workunit_id, 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, abnormal_number, start_time, start_by, end_time, end_by, time_consuming,remarks
        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   and endPurchaseDate != null  ">
                and  abnormal_time between #{beginPurchaseDate} and #{endPurchaseDate}
            </if>
            <if test="abnormalStatus != null  and abnormalStatus != ''"> and abnormal_status = #{abnormalStatus}</if>
            <if test="abnormalNumber != null">and abnormal_number = #{abnormalNumber}</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="workunitId != null">workunit_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>
            <if test="abnormalNumber != null">abnormal_number,</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="workunitId != null">#{workunitId},</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>
            <if test="abnormalNumber != null">#{abnormalNumber},</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="workunitId != null">workunit_id = #{workunitId},</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>
            <if test="startTime != null">
                start_time = #{startTime,jdbcType=TIMESTAMP},
            </if>
            <if test="startBy != null">
                start_by = #{startBy,jdbcType=VARCHAR},
            </if>
            <if test="endTime != null">
                end_time = #{endTime,jdbcType=TIMESTAMP},
            </if>
            <if test="endBy != null">
                end_by = #{endBy,jdbcType=VARCHAR},
            </if>
            <if test="timeConsuming != null">
                time_consuming = #{timeConsuming,jdbcType=BIGINT},
            </if>

            <if test="remarks != null">
                remarks = #{remarks},
            </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>