<?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.ProProductQrcodeRecordMapper">

    <resultMap type="ProProductQrcodeRecord" id="ProProductQrcodeRecordResult">
            <result property="recordId"    column="record_id"    />
            <result property="itemId"    column="item_id"    />
            <result property="itemCode"    column="item_code"    />
            <result property="workOrderId"    column="work_order_id"    />
            <result property="workOrderNo"    column="work_order_no"    />
            <result property="printTime"    column="print_time"    />
            <result property="qrcode"    column="qrcode"    />
            <result property="clientCode"    column="client_code"    />
            <result property="clientName"    column="client_name"    />
            <result property="itemDesc"    column="item_desc"    />
            <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>

    <resultMap type="com.ximai.mes.pro.dto.ProProductQrcodeRecordDto" id="ProProductQrcodeRecordResult2">
            <result property="recordId"    column="record_id"    />
            <result property="itemId"    column="item_id"    />
            <result property="itemCode"    column="item_code"    />
            <result property="workOrderId"    column="work_order_id"    />
            <result property="workOrderNo"    column="work_order_no"    />
            <result property="printTime"    column="print_time"    />
            <result property="qrcode"    column="qrcode"    />
            <result property="clientCode"    column="client_code"    />
            <result property="clientName"    column="client_name"    />
            <result property="itemDesc"    column="item_desc"    />
            <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="selectProProductQrcodeRecordVo">
        select record_id, item_id, item_code, work_order_id, work_order_no,
               print_time, qrcode, client_code, client_name, item_desc, create_by,
               create_time, update_by, update_time ,template_id,template_type,
               template_code,template_name,task_batch
        from pro_product_qrcode_record
    </sql>

    <select id="selectProProductQrcodeRecordList" parameterType="ProProductQrcodeRecord" resultMap="ProProductQrcodeRecordResult2">
        <include refid="selectProProductQrcodeRecordVo"/>
        <where>  
            <if test="itemId != null "> and item_id = #{itemId}</if>
            <if test="itemCode != null  and itemCode != ''"> and item_code = #{itemCode}</if>
            <if test="workOrderId != null "> and work_order_id = #{workOrderId}</if>
            <if test="workOrderNo != null  and workOrderNo != ''"> and work_order_no = #{workOrderNo}</if>
            <if test="printTime != null "> and print_time = #{printTime}</if>
            <if test="qrcode != null  and qrcode != ''"> and qrcode = #{qrcode}</if>
            <if test="clientCode != null  and clientCode != ''"> and client_code = #{clientCode}</if>
            <if test="clientName != null  and clientName != ''"> and client_name like concat('%', #{clientName}, '%')</if>
            <if test="itemDesc != null  and itemDesc != ''"> and item_desc = #{itemDesc}</if>
        </where>
    </select>
    
    <select id="selectProProductQrcodeRecordByRecordId" parameterType="Long" resultMap="ProProductQrcodeRecordResult2">
        <include refid="selectProProductQrcodeRecordVo"/>
        where record_id = #{recordId}
    </select>
        
    <insert id="insertProProductQrcodeRecord" parameterType="ProProductQrcodeRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into pro_product_qrcode_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,</if>
            <if test="itemCode != null">item_code,</if>
            <if test="workOrderId != null">work_order_id,</if>
            <if test="workOrderNo != null">work_order_no,</if>
            <if test="printTime != null">print_time,</if>
            <if test="qrcode != null">qrcode,</if>
            <if test="clientCode != null">client_code,</if>
            <if test="clientName != null">client_name,</if>
            <if test="itemDesc != null">item_desc,</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="templateId != null">
                template_id,
            </if>
            <if test="templateCode != null">
                template_code,
            </if>
            <if test="templateName != null">
                template_name,
            </if>
            <if test="templateType != null">
                template_type,
            </if>
            <if test="taskBatch != null">
                task_batch,
            </if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null">#{itemId},</if>
            <if test="itemCode != null">#{itemCode},</if>
            <if test="workOrderId != null">#{workOrderId},</if>
            <if test="workOrderNo != null">#{workOrderNo},</if>
            <if test="printTime != null">#{printTime},</if>
            <if test="qrcode != null">#{qrcode},</if>
            <if test="clientCode != null">#{clientCode},</if>
            <if test="clientName != null">#{clientName},</if>
            <if test="itemDesc != null">#{itemDesc},</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="templateId != null">
                #{templateId,jdbcType=BIGINT},
            </if>
            <if test="templateCode != null">
                #{templateCode,jdbcType=VARCHAR},
            </if>
            <if test="templateName != null">
                #{templateName,jdbcType=VARCHAR},
            </if>
            <if test="templateType != null">
                #{templateType,jdbcType=VARCHAR},
            </if>
            <if test="taskBatch != null">
                #{taskBatch,jdbcType=VARCHAR},
            </if>
         </trim>
    </insert>
    <update id="addPrintTime">
        update pro_product_qrcode_record set print_time = IFNULL(print_time,0) + 1,update_by = #{userName},update_time = now()
        where record_id in
        <foreach collection="ids" separator="," open="(" close=")" item="id">
            #{id}
        </foreach>
     </update>

    <update id="updateProProductQrcodeRecord" parameterType="ProProductQrcodeRecord">
        update pro_product_qrcode_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="itemCode != null">item_code = #{itemCode},</if>
            <if test="workOrderId != null">work_order_id = #{workOrderId},</if>
            <if test="workOrderNo != null">work_order_no = #{workOrderNo},</if>
            <if test="printTime != null">print_time = #{printTime},</if>
            <if test="qrcode != null">qrcode = #{qrcode},</if>
            <if test="clientCode != null">client_code = #{clientCode},</if>
            <if test="clientName != null">client_name = #{clientName},</if>
            <if test="itemDesc != null">item_desc = #{itemDesc},</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 record_id = #{recordId}
    </update>

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

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

    <delete id="deleteProProductQrcodeRecordByTaskBatch" parameterType="String">
        delete from pro_product_qrcode_record where task_batch =#{taskBatch}
    </delete>

</mapper>