<?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.pro.mapper.ProMaterialUsageRecordMapper">

    <resultMap type="ProMaterialUsageRecord" id="ProMaterialUsageRecordResult">
        <result property="recordId"    column="record_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="materialBatchNumber"    column="material_batch_number"    />
        <result property="quantity"    column="quantity"    />
        <result property="operatorId"    column="operator_id"    />
        <result property="operatorName"    column="operator_name"    />
        <result property="operatorTime"    column="operator_time"    />
        <result property="workstationId"    column="workstation_id"    />
        <result property="depletionMethod"    column="depletion_method"    />
        <result property="remark"    column="remark"    />
        <result property="attr1"    column="attr1"    />
        <result property="attr2"    column="attr2"    />
        <result property="attr3"    column="attr3"    />
        <result property="attr4"    column="attr4"    />
    </resultMap>

    <sql id="selectProMaterialUsageRecordVo">
        select record_id, task_id, material_batch_number, quantity, operator_id, operator_name, operator_time, workstation_id, depletion_method, remark, attr1, attr2, attr3, attr4 from pro_material_usage_record
    </sql>

    <select id="selectProMaterialUsageRecordList" parameterType="ProMaterialUsageRecord" resultMap="ProMaterialUsageRecordResult">
        <include refid="selectProMaterialUsageRecordVo"/>
        <where>
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="materialBatchNumber != null  and materialBatchNumber != ''"> and material_batch_number = #{materialBatchNumber}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="operatorId != null "> and operator_id = #{operatorId}</if>
            <if test="operatorName != null  and operatorName != ''"> and operator_name like concat('%', #{operatorName}, '%')</if>
            <if test="operatorTime != null "> and operator_time = #{operatorTime}</if>
            <if test="workstationId != null "> and workstation_id = #{workstationId}</if>
            <if test="depletionMethod != null "> and depletion_method = #{depletionMethod}</if>
            <if test="attr1 != null  and attr1 != ''"> and attr1 = #{attr1}</if>
            <if test="attr2 != null  and attr2 != ''"> and attr2 = #{attr2}</if>
            <if test="attr3 != null "> and attr3 = #{attr3}</if>
            <if test="attr4 != null "> and attr4 = #{attr4}</if>
        </where>
    </select>

    <select id="selectProMaterialUsageRecordByRecordId" parameterType="Long" resultMap="ProMaterialUsageRecordResult">
        <include refid="selectProMaterialUsageRecordVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertProMaterialUsageRecord" parameterType="ProMaterialUsageRecord">
        insert into pro_material_usage_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recordId != null">record_id,</if>
            <if test="taskId != null">task_id,</if>
            <if test="materialBatchNumber != null">material_batch_number,</if>
            <if test="quantity != null">quantity,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="operatorName != null">operator_name,</if>
            <if test="operatorTime != null">operator_time,</if>
            <if test="workstationId != null">workstation_id,</if>
            <if test="depletionMethod != null">depletion_method,</if>
            <if test="remark != null">remark,</if>
            <if test="attr1 != null">attr1,</if>
            <if test="attr2 != null">attr2,</if>
            <if test="attr3 != null">attr3,</if>
            <if test="attr4 != null">attr4,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recordId != null">#{recordId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="materialBatchNumber != null">#{materialBatchNumber},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="operatorName != null">#{operatorName},</if>
            <if test="operatorTime != null">#{operatorTime},</if>
            <if test="workstationId != null">#{workstationId},</if>
            <if test="depletionMethod != null">#{depletionMethod},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attr1 != null">#{attr1},</if>
            <if test="attr2 != null">#{attr2},</if>
            <if test="attr3 != null">#{attr3},</if>
            <if test="attr4 != null">#{attr4},</if>
         </trim>
    </insert>

    <update id="updateProMaterialUsageRecord" parameterType="ProMaterialUsageRecord">
        update pro_material_usage_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="materialBatchNumber != null">material_batch_number = #{materialBatchNumber},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="operatorName != null">operator_name = #{operatorName},</if>
            <if test="operatorTime != null">operator_time = #{operatorTime},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="depletionMethod != null">depletion_method = #{depletionMethod},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attr1 != null">attr1 = #{attr1},</if>
            <if test="attr2 != null">attr2 = #{attr2},</if>
            <if test="attr3 != null">attr3 = #{attr3},</if>
            <if test="attr4 != null">attr4 = #{attr4},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteProMaterialUsageRecordByRecordId" parameterType="Long">
        delete from pro_material_usage_record where record_id = #{recordId}
    </delete>

    <delete id="deleteProMaterialUsageRecordByRecordIds" parameterType="String">
        delete from pro_material_usage_record where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
</mapper>
