<?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.sale.deliver.mapper.SaDeliverBarCodeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.sale.deliver.SaDeliverBarCode">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_DELIVER" property="pkDeliver"/>
        <result column="PK_DELIVER_ITEMS" property="pkDeliverItems"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="PK_BAR_CODE_ITEMS" property="pkBarCodeItems"/>
        <result column="BAR_CODE" property="barCode"/>
    </resultMap>

    <!-- 根据订单明细和产品查询条码 -->
    <select id="queryBarCodeList" resultMap="BaseResultMap">
        select *
        from SA_DELIVER_BAR_CODE sdbc
        where sdbc.del_flag = 0
        and sdbc.pk_country = #{pkCountry}
        and (
        <foreach collection="paramList" item="item" separator=" or ">
            (sdbc.pk_deliver_items = #{item.pkDeliverItems} and sdbc.pk_product = #{item.pkProduct})
        </foreach>
        )
    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch">
        INSERT ALL
        <foreach collection="list" item="item" separator=" ">
            into SA_DELIVER_BAR_CODE (PK_DELIVER, PK_DELIVER_ITEMS, PK_PRODUCT, PK_BAR_CODE_ITEMS, BAR_CODE,
            CREATION_TIME, PK_CREATOR, MODIFIED_TIME, PK_MODIFIED, PK_COUNTRY)
            VALUES (#{item.pkDeliver}, #{item.pkDeliverItems}, #{item.pkProduct}, #{item.pkBarCodeItems}, #{item.barCode},
            #{item.creationTime}, #{item.pkCreator}, #{item.modifiedTime}, #{item.pkModified}, #{item.pkCountry})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 根据发货明细ID查询对应绑定条码数据 -->
    <select id="queryListByDeliverItems" resultMap="BaseResultMap">
        select sdbc.pk_deliver_items,
        WM_CONCAT(sdbc.bar_code) bar_code,
        min(sdbc.creation_time) creation_time
        from SA_DELIVER_BAR_CODE sdbc
        where sdbc.del_flag = 0
        and sdbc.pk_country = #{pkCountry}
        and (sdbc.pk_deliver_items in (0
        <foreach collection="deliverItemsIdList" item="item" close=")" index="index">
            <choose>
                <when test="index % 500 == 0">) or sdbc.pk_deliver_items in (#{item}</when>
                <otherwise>, #{item}</otherwise>
            </choose>
        </foreach>
        )
        group by sdbc.pk_deliver_items
    </select>

</mapper>
