<?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.hzs.system.config.mapper.BdBarCodeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.config.BdBarCode">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="PREFIX" property="prefix"/>
        <result column="IS_YEAR" property="isYear"/>
        <result column="IS_MONTH" property="isMonth"/>
        <result column="TAIL_NUMBER" property="tailNumber"/>
        <result column="IS_INITIALIZE" property="isInitialize"/>
        <result column="END_NUMBER" property="endNumber"/>
        <result column="CURRENT_NUMBER" property="currentNumber"/>
        <result column="TOTAL_BAR_CODE" property="totalBarCode"/>
        <result column="USED_BAR_CODE" property="usedBarCode"/>
        <result column="ENABLE_STATE" property="enableState"/>
        <result column="BATCH" property="batch"/>

    </resultMap>

    <!-- 查询条形码列表-->
    <select id="queryBarCodeList" parameterType="com.hzs.common.domain.system.config.ext.BdBarCodeExt"
            resultType="com.hzs.common.domain.system.config.ext.BdBarCodeExt">
        select a.PK_ID,
        a.PK_PRODUCT,
        a.PREFIX,
        a.IS_YEAR,
        a.IS_MONTH,
        a.TAIL_NUMBER,
        a.IS_INITIALIZE,
        a.END_NUMBER,
        a.CURRENT_NUMBER,
        a.TOTAL_BAR_CODE,
        a.USED_BAR_CODE,
        a.ENABLE_STATE,
        a.DEL_FLAG,
        a.PK_COUNTRY,
        a.CREATION_TIME,
        a.MODIFIED_TIME,
        a.PK_CREATOR,
        a.PK_MODIFIED,
        a.BATCH,
        b.PRODUCT_CODE as productCode ,b.PRODUCT_NAME as productName
        from bd_bar_code a INNER JOIN bd_product b on a.pk_product=b.pk_ID
        where a.DEL_FLAG=0 and b.DEL_FLAG=0 and a.PK_COUNTRY=#{pkCountry}
        <if test="productName != null and productName !='' ">
            AND b.PRODUCT_NAME = #{productName}
        </if>
        <if test="productCode != null and productCode !='' ">
            AND b.PRODUCT_CODE = #{productCode}
        </if>
        <if test="prefix != null and prefix !='' ">
            AND a.PREFIX = #{prefix}
        </if>
        <if test="isInitialize != null ">
            AND a.IS_INITIALIZE = #{isInitialize}
        </if>
        <if test="batch != null ">
            AND a.BATCH = #{batch}
        </if>
        <if test="isYear != null ">
            AND a.IS_YEAR = #{isYear}
        </if>
        <if test="isMonth != null ">
            AND a.IS_MONTH = #{isMonth}
        </if>

        <if test="tailNumber != null ">
            AND a.TAIL_NUMBER = #{tailNumber}
        </if>
        <if test="endNumber != null ">
            AND a.END_NUMBER = #{endNumber}
        </if>
        <if test="totalBarCode != null ">
            AND a.TOTAL_BAR_CODE = #{totalBarCode}
        </if>
        <if test="usedBarCode != null ">
            AND a.USED_BAR_CODE = #{usedBarCode}
        </if>

        <if test="startDate != null ">
            and a.EFFECTIVE_DATE &gt;= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null ">
            and a.EFFECTIVE_DATE &lt; #{endDate, jdbcType=DATE}
        </if>


        order by a.PK_ID desc
    </select>

<!--     追溯码查询特殊产品-->
    <select id="querySpecialProduct" parameterType="com.hzs.common.domain.system.config.BdBarCode"
            resultType="com.hzs.common.domain.system.config.ext.BdBarCodeExt">
        select a.PK_ID,
        a.PRODUCT_CODE,
        a.PRODUCT_NAME
        from BD_PRODUCT a
        where a.DEL_FLAG=0 and a.PK_COUNTRY=#{pkCountry}
        <if test="pkProduct !=null ">
            AND a.PK_ID = #{pkProduct}
        </if>

    </select>

    <select id="getOneBarCode" parameterType="Long"
            resultType="com.hzs.common.domain.system.config.BdBarCode">
        select a.PK_ID,
        a.PK_PRODUCT,
        a.PREFIX,
        a.IS_YEAR,
        a.IS_MONTH,
        a.TAIL_NUMBER,
        a.IS_INITIALIZE,
        a.END_NUMBER,
        a.CURRENT_NUMBER,
        a.TOTAL_BAR_CODE,
        a.USED_BAR_CODE,
        a.ENABLE_STATE,
        a.DEL_FLAG,
        a.PK_COUNTRY,
        a.CREATION_TIME,
        a.MODIFIED_TIME,
        a.PK_CREATOR,
        a.PK_MODIFIED,
        a.BATCH,
        a.EFFECTIVE_DATE,
        b.PRODUCT_CODE,b.PRODUCT_NAME
        from bd_bar_code a INNER JOIN bd_product b on a.pk_product=b.pk_ID
        where a.DEL_FLAG=0 and b.DEL_FLAG=0
        <if test="pkId != null and pkId !=0 ">
            AND a.PK_ID = #{pkId}
        </if>
    </select>

    <!-- 更新条码使用数量 -->
    <update id="updateUsedBarCode">
        merge into BD_BAR_CODE bbc
        using (select bbci.pk_bar_code, count(bbci.pk_bar_code) upNum
        from BD_BAR_CODE_ITEMS bbci
        where bbci.del_flag = 0
        and bbci.pk_country = #{pkCountry}
        and bbci.pk_id in
        <foreach collection="idList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by bbci.pk_bar_code) tmp
        on (bbc.pk_id = tmp.pk_bar_code)
        when matched then
        update
        set bbc.USED_BAR_CODE = bbc.USED_BAR_CODE + ${type} * nvl(tmp.upnum, 0),
        bbc.PK_MODIFIED = #{userId},
        bbc.MODIFIED_TIME = sysdate
    </update>



</mapper>
