<?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="testResult" column="test_result"/>
        <result property="testValue" column="test_value"/>
        <result property="referenceValue" column="reference_value"/>
        <result property="testingTime" column="testing_time"/>
        <result property="fileName" column="file_name"/>
        <result property="destinationInfo" column="destination_info"/>
        <result property="libraryFlag" column="library_flag"/>
        <result property="intendedUse" column="intended_use"/>
        <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,
               test_result,
               test_value,
               reference_value,
               testing_time,
               file_name,
               destination_info,
               library_flag,
               intended_use,
               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="destinationInfo != null  and destinationInfo != ''">and destination_info = #{destinationInfo}</if>
            <if test="libraryFlag != null  and libraryFlag != ''">and library_flag = #{libraryFlag}</if>
            <if test="intendedUse != null  and intendedUse != ''">and intended_use = #{intendedUse}</if>
            <if test="remark != null  and remark != ''">and remark like concat('%', #{remark}, '%')</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>
            <if test="batchNumberList != null and batchNumberList.size() > 0">
                AND batch_number IN
                <foreach collection="batchNumberList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="tagSequenceList != null and tagSequenceList.size() > 0">
                AND tag_sequence IN
                <foreach collection="tagSequenceList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </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>

    <select id="selectExistingTids" resultType="string">
        SELECT t_id FROM hw_tag_record WHERE t_id IN
        <foreach collection="list" item="tid" open="(" separator="," close=")">
            #{tid}
        </foreach>
    </select>

    <select id="selectExistingEpcs" resultMap="HwTagRecordResult">
        SELECT r_id, t_id, epc FROM hw_tag_record WHERE epc IN
        <foreach collection="list" item="epc" open="(" separator="," close=")">
            #{epc}
        </foreach>
    </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="testResult != null">test_result,</if>
            <if test="testValue != null">test_value,</if>
            <if test="referenceValue != null">reference_value,</if>
            <if test="testingTime != null">testing_time,</if>
            <if test="fileName != null">file_name,</if>
            <if test="destinationInfo != null">destination_info,</if>
            <if test="libraryFlag != null">library_flag,</if>
            <if test="intendedUse != null">intended_use,</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="testResult != null">#{testResult},</if>
            <if test="testValue != null">#{testValue},</if>
            <if test="referenceValue != null">#{referenceValue},</if>
            <if test="testingTime != null">#{testingTime},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="destinationInfo != null">#{destinationInfo},</if>
            <if test="libraryFlag != null">#{libraryFlag},</if>
            <if test="intendedUse != null">#{intendedUse},</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="testResult != null">test_result = #{testResult},</if>
            <if test="testValue != null">test_value = #{testValue},</if>
            <if test="referenceValue != null">reference_value = #{referenceValue},</if>
            <if test="testingTime != null">testing_time = #{testingTime},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="destinationInfo != null">destination_info = #{destinationInfo},</if>
            <if test="libraryFlag != null">library_flag = #{libraryFlag},</if>
            <if test="intendedUse != null">intended_use = #{intendedUse},</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="tId != null and tId != ''">t_id = #{tId},</if>
            <if test="epc != null and epc != ''">epc = #{epc},</if>
            <if test="password != null and password != '' and password != '!clear!'">password = #{password},</if>
            <if test="password == '!clear!'">password = NULL,</if>
            <if test="orderCode != null and orderCode != '' and orderCode != '!clear!'">order_code = #{orderCode},</if>
            <if test="orderCode == '!clear!'">order_code = NULL,</if>
            <if test="batchNumber != null and batchNumber != '' and batchNumber != '!clear!'">batch_number =
                #{batchNumber},
            </if>
            <if test="batchNumber == '!clear!'">batch_number = NULL,</if>
            <if test="tagSequence != null and tagSequence != '' and tagSequence != '!clear!'">tag_sequence =
                #{tagSequence},
            </if>
            <if test="tagSequence == '!clear!'">tag_sequence = NULL,</if>
            <if test="totalQuantity != null">total_quantity = #{totalQuantity},</if>
            <if test="tagBatch != null and tagBatch != '' and tagBatch != '!clear!'">tag_batch = #{tagBatch},</if>
            <if test="tagBatch == '!clear!'">tag_batch = NULL,</if>
            <if test="modelCode != null and modelCode != ''">model_code = #{modelCode},</if>
            <if test="processingTime != null">processing_time = #{processingTime},</if>
            <if test="operatorId != null and modelCode != '' and operatorId != '!clear!'">operator_id = #{operatorId},
            </if>
            <if test="operatorId == '!clear!'">operator_id = NULL,</if>
            <if test="testResult != null and testResult != '' and testResult != '!clear!'">test_result =
                #{testResult},
            </if>
            <if test="testResult == '!clear!'">test_result = NULL,</if>
            <if test="testValue != null and testValue != '' and testValue != '!clear!'">test_value = #{testValue},</if>
            <if test="testValue == '!clear!'">test_value = NULL,</if>
            <if test="referenceValue != null and referenceValue != '' and referenceValue != '!clear!'">reference_value =
                #{referenceValue},
            </if>
            <if test="referenceValue == '!clear!'">reference_value = NULL,</if>
            <if test="testingTime != null">testing_time = #{testingTime},</if>
            <if test="fileName != null and fileName != '' and fileName != '!clear!'">file_name = #{fileName},</if>
            <if test="fileName == '!clear!'">file_name = NULL,</if>
            <if test="destinationInfo != null and destinationInfo != '' and destinationInfo != '!clear!'">
                destination_info = #{destinationInfo},
            </if>
            <if test="destinationInfo == '!clear!'">destination_info = NULL,</if>
            <if test="libraryFlag != null and libraryFlag != '' and libraryFlag != '!clear!'">library_flag =
                #{libraryFlag},
            </if>
            <if test="libraryFlag == '!clear!'">library_flag = NULL,</if>
            <!--            <if test="intendedUse != null">intended_use = #{intendedUse},</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 and remark != '' and remark != '!clear!'">remark = #{remark},</if>
            <if test="remark == '!clear!'">remark = NULL,</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,
        test_result,test_value,reference_value,testing_time,file_name,
        destination_info,library_flag,intended_use
        ) 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},
            #{item.testResult}, #{item.testValue},#{item.referenceValue}, #{item.testingTime},#{item.fileName}
            ,#{item.destinationInfo}, #{item.libraryFlag},#{item.intendedUse}
            )
        </foreach>
    </insert>
</mapper>