<?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="hw.tagApi.service.mapper.HwTagRecordMapper">
    
    <resultMap type="HwTagRecord" id="HwTagRecordResult">
        <result property="rId"    column="r_id"    />
        <result property="tId"    column="t_id"    />
        <result property="epc"    column="epc"    />
        <result property="password"    column="password"    />
        <result property="orderCode"    column="order_code"    />
        <result property="batchNumber"    column="batch_number"    />
        <result property="tagSequence"    column="tag_sequence"    />
        <result property="totalQuantity"    column="total_quantity"    />
        <result property="tagBatch"    column="tag_batch"    />
        <result property="modelCode"    column="model_code"    />
        <result property="processingTime"    column="processing_time"    />
        <result property="operatorId"    column="operator_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="fields1"    column="fields_1"    />
        <result property="fields2"    column="fields_2"    />
        <result property="fields3"    column="fields_3"    />
        <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 property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectHwTagRecordVo">
        select r_id, t_id, epc, password, order_code, batch_number, tag_sequence, total_quantity, tag_batch, model_code, processing_time, operator_id, del_flag, fields_1, fields_2, fields_3, create_by, create_time, update_by, update_time, remark from hw_tag_record
    </sql>

    <select id="selectHwTagRecordList" parameterType="HwTagRecord" resultMap="HwTagRecordResult">
        <include refid="selectHwTagRecordVo"/>
        <where>  
            <if test="tId != null  and tId != ''"> and t_id = #{tId}</if>
            <if test="epc != null  and epc != ''"> and epc = #{epc}</if>
            <if test="selectTID != null  and selectTID != ''"> and t_id like concat('%', #{selectTID}, '%')</if>
            <if test="selectEPC != null  and selectEPC != ''"> and epc like concat('%', #{selectEPC}, '%')</if>
            <if test="password != null  and password != ''"> and password = #{password}</if>
            <if test="orderCode != null  and orderCode != ''"> and order_code = #{orderCode}</if>
            <if test="batchNumber != null  and batchNumber != ''"> and batch_number = #{batchNumber}</if>
            <if test="tagSequence != null  and tagSequence != ''"> and tag_sequence = #{tagSequence}</if>
            <if test="totalQuantity != null "> and total_quantity = #{totalQuantity}</if>
            <if test="tagBatch != null  and tagBatch != ''"> and tag_batch = #{tagBatch}</if>
            <if test="modelCode != null  and modelCode != ''"> and model_code = #{modelCode}</if>
            <if test="params.beginProcessingTime != null and params.beginProcessingTime != '' and params.endProcessingTime != null and params.endProcessingTime != ''"> and processing_time between #{params.beginProcessingTime} and #{params.endProcessingTime}</if>
            <if test="operatorId != null  and operatorId != ''"> and operator_id = #{operatorId}</if>
            <if test="fields1 != null  and fields1 != ''"> and fields_1 = #{fields1}</if>
            <if test="fields2 != null  and fields2 != ''"> and fields_2 = #{fields2}</if>
            <if test="fields3 != null  and fields3 != ''"> and fields_3 = #{fields3}</if>
            <if test="startDate != null  and startDate != ''"> and DATE_FORMAT(processing_time, '%Y/%m/%d') >= #{startDate}</if>
            <if test="endDate != null  and endDate != ''"> and #{endDate}  >= DATE_FORMAT(processing_time, '%Y/%m/%d')</if>
        </where>
    </select>
    
    <select id="selectHwTagRecordByRId" parameterType="Long" resultMap="HwTagRecordResult">
        <include refid="selectHwTagRecordVo"/>
        where r_id = #{rId}
    </select>

    <select id="selectHwTagRecordByTxId" parameterType="String" resultMap="HwTagRecordResult">
        select r_id, t_id, epc from hw_tag_record
        where t_id = #{tId}
    </select>

    <insert id="insertHwTagRecord" parameterType="HwTagRecord">
        insert into hw_tag_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="rId != null">r_id,</if>
            <if test="tId != null and tId != ''">t_id,</if>
            <if test="epc != null and epc != ''">epc,</if>
            <if test="password != null">password,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="batchNumber != null">batch_number,</if>
            <if test="tagSequence != null">tag_sequence,</if>
            <if test="totalQuantity != null">total_quantity,</if>
            <if test="tagBatch != null">tag_batch,</if>
            <if test="modelCode != null">model_code,</if>
            <if test="processingTime != null">processing_time,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="fields1 != null">fields_1,</if>
            <if test="fields2 != null">fields_2,</if>
            <if test="fields3 != null">fields_3,</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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="rId != null">#{rId},</if>
            <if test="tId != null and tId != ''">#{tId},</if>
            <if test="epc != null and epc != ''">#{epc},</if>
            <if test="password != null">#{password},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="batchNumber != null">#{batchNumber},</if>
            <if test="tagSequence != null">#{tagSequence},</if>
            <if test="totalQuantity != null">#{totalQuantity},</if>
            <if test="tagBatch != null">#{tagBatch},</if>
            <if test="modelCode != null">#{modelCode},</if>
            <if test="processingTime != null">#{processingTime},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="fields1 != null">#{fields1},</if>
            <if test="fields2 != null">#{fields2},</if>
            <if test="fields3 != null">#{fields3},</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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateHwTagRecord" parameterType="HwTagRecord">
        update hw_tag_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="tId != null and tId != ''">t_id = #{tId},</if>
            <if test="epc != null and epc != ''">epc = #{epc},</if>
            <if test="password != null">password = #{password},</if>
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="batchNumber != null">batch_number = #{batchNumber},</if>
            <if test="tagSequence != null">tag_sequence = #{tagSequence},</if>
            <if test="totalQuantity != null">total_quantity = #{totalQuantity},</if>
            <if test="tagBatch != null">tag_batch = #{tagBatch},</if>
            <if test="modelCode != null">model_code = #{modelCode},</if>
            <if test="processingTime != null">processing_time = #{processingTime},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="fields1 != null">fields_1 = #{fields1},</if>
            <if test="fields2 != null">fields_2 = #{fields2},</if>
            <if test="fields3 != null">fields_3 = #{fields3},</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="remark != null">remark = #{remark},</if>
        </trim>
        where r_id = #{rId}
    </update>

    <update id="updateHwTagRecordByTID" parameterType="HwTagRecord">
        update hw_tag_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="epc != null and epc != ''">epc = #{epc},</if>
            <if test="password != null">password = #{password},</if>
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="batchNumber != null">batch_number = #{batchNumber},</if>
            <if test="tagSequence != null">tag_sequence = #{tagSequence},</if>
            <if test="totalQuantity != null">total_quantity = #{totalQuantity},</if>
            <if test="tagBatch != null">tag_batch = #{tagBatch},</if>
            <if test="modelCode != null">model_code = #{modelCode},</if>
            <if test="processingTime != null">processing_time = #{processingTime},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="fields1 != null">fields_1 = #{fields1},</if>
            <if test="fields2 != null">fields_2 = #{fields2},</if>
            <if test="fields3 != null">fields_3 = #{fields3},</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="remark != null">remark = #{remark},</if>
        </trim>
        where t_id = #{tId}
    </update>

    <delete id="deleteHwTagRecordByRId" parameterType="Long">
        delete from hw_tag_record where r_id = #{rId}
    </delete>

    <delete id="deleteHwTagRecordByRIds" parameterType="String">
        delete from hw_tag_record where r_id in 
        <foreach item="rId" collection="array" open="(" separator="," close=")">
            #{rId}
        </foreach>
    </delete>

    <!-- 批量新增标签记录 -->
    <insert id="batchInsertHwTagRecord" parameterType="java.util.List">
        insert into hw_tag_record (
            r_id, t_id, epc, password, order_code, batch_number, 
            tag_sequence, total_quantity, tag_batch, model_code, 
            processing_time, operator_id,  fields_1, fields_2, fields_3,
            create_by, create_time, update_by, update_time, remark
        ) values 
        <foreach collection="list" item="item" separator=",">
            (
                #{item.rId}, #{item.tId}, #{item.epc}, #{item.password}, #{item.orderCode}, #{item.batchNumber},
                #{item.tagSequence}, #{item.totalQuantity}, #{item.tagBatch}, #{item.modelCode},
                #{item.processingTime}, #{item.operatorId},  #{item.fields1}, #{item.fields2}, #{item.fields3},
                #{item.createBy}, sysdate(), #{item.updateBy}, sysdate(), #{item.remark}
            )
        </foreach>
    </insert>
</mapper>