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

    <resultMap type="ProProductQrcodeRule" id="ProProductQrcodeRuleResult">
                    <result property="ruleId"    column="rule_id"    />
                    <result property="itemId"    column="item_id"    />
                    <result property="itemCode"    column="item_code"    />
                    <result property="prefix"    column="prefix"    />
                    <result property="datePattern"    column="date_pattern"    />
                    <result property="serialLen"    column="serial_len"    />
                    <result property="itemDesc"    column="item_desc"    />
                    <result property="clientCode"    column="client_code"    />
                    <result property="clientName"    column="client_name"    />
                    <result property="currentSerial"    column="current_serial"    />
                    <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.ProProductQrcodeRuleDto" id="ProProductQrcodeRuleResult2">
                    <result property="ruleId"    column="rule_id"    />
                    <result property="itemId"    column="item_id"    />
                    <result property="itemCode"    column="item_code"    />
                    <result property="prefix"    column="prefix"    />
                    <result property="datePattern"    column="date_pattern"    />
                    <result property="serialLen"    column="serial_len"    />
                    <result property="itemDesc"    column="item_desc"    />
                    <result property="clientCode"    column="client_code"    />
                    <result property="clientName"    column="client_name"    />
                    <result property="currentSerial"    column="current_serial"    />
                    <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="selectProProductQrcodeRuleVo">
        select rule_id, item_id, item_code, prefix, date_pattern, serial_len, item_desc, client_code, client_name, current_serial, create_by, create_time, update_by, update_time from pro_product_qrcode_rule
    </sql>

    <select id="selectProProductQrcodeRuleList" parameterType="ProProductQrcodeRule" resultMap="ProProductQrcodeRuleResult2">
        <include refid="selectProProductQrcodeRuleVo"/>
        <where>  
            <if test="itemId != null "> and item_id = #{itemId}</if>
            <if test="itemCode != null  and itemCode != ''"> and item_code = #{itemCode}</if>
            <if test="prefix != null  and prefix != ''"> and prefix = #{prefix}</if>
            <if test="datePattern != null  and datePattern != ''"> and date_pattern = #{datePattern}</if>
            <if test="serialLen != null "> and serial_len = #{serialLen}</if>
            <if test="itemDesc != null  and itemDesc != ''"> and item_desc = #{itemDesc}</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="currentSerial != null "> and current_serial = #{currentSerial}</if>
        </where>
    </select>
    
    <select id="selectProProductQrcodeRuleByRuleId" parameterType="Long" resultMap="ProProductQrcodeRuleResult2">
        <include refid="selectProProductQrcodeRuleVo"/>
        where rule_id = #{ruleId}
    </select>
        
    <insert id="insertProProductQrcodeRule" parameterType="ProProductQrcodeRule" useGeneratedKeys="true" keyProperty="ruleId">
        insert into pro_product_qrcode_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,</if>
            <if test="itemCode != null and itemCode != ''">item_code,</if>
            <if test="prefix != null">prefix,</if>
            <if test="datePattern != null">date_pattern,</if>
            <if test="serialLen != null">serial_len,</if>
            <if test="itemDesc != null">item_desc,</if>
            <if test="clientCode != null">client_code,</if>
            <if test="clientName != null">client_name,</if>
            <if test="currentSerial != null">current_serial,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null">#{itemId},</if>
            <if test="itemCode != null and itemCode != ''">#{itemCode},</if>
            <if test="prefix != null">#{prefix},</if>
            <if test="datePattern != null">#{datePattern},</if>
            <if test="serialLen != null">#{serialLen},</if>
            <if test="itemDesc != null">#{itemDesc},</if>
            <if test="clientCode != null">#{clientCode},</if>
            <if test="clientName != null">#{clientName},</if>
            <if test="currentSerial != null">#{currentSerial},</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>
         </trim>
    </insert>

    <update id="updateProProductQrcodeRule" parameterType="ProProductQrcodeRule">
        update pro_product_qrcode_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="itemCode != null and itemCode != ''">item_code = #{itemCode},</if>
            <if test="prefix != null">prefix = #{prefix},</if>
            <if test="datePattern != null">date_pattern = #{datePattern},</if>
            <if test="serialLen != null">serial_len = #{serialLen},</if>
            <if test="itemDesc != null">item_desc = #{itemDesc},</if>
            <if test="clientCode != null">client_code = #{clientCode},</if>
            <if test="clientName != null">client_name = #{clientName},</if>
            <if test="currentSerial != null">current_serial = #{currentSerial},</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 rule_id = #{ruleId}
    </update>

    <delete id="deleteProProductQrcodeRuleByRuleId" parameterType="Long">
        delete from pro_product_qrcode_rule where rule_id = #{ruleId}
    </delete>

    <delete id="deleteProProductQrcodeRuleByRuleIds" parameterType="String">
        delete from pro_product_qrcode_rule where rule_id in 
        <foreach item="ruleId" collection="array" open="(" separator="," close=")">
            #{ruleId}
        </foreach>
    </delete>
</mapper>