<?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.dk.mdm.mapper.ivt.TransferItemMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, transfer_id, transfer_date, item_index, sku_id, o_inv_id, inv_id, o_wh_id, transfer_qty, wh_id, o_non_std_code, non_std_code, price_into, intoing_amt, flg_valid, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
    </sql>

    <!-- 一览查询列 -->
    <sql id="Base_Column_List_Response">
        item.item_id, item.transfer_id, item.transfer_date, item.item_index, item.sku_id, item.o_inv_id, item.inv_id
        , item.o_wh_id, item.transfer_qty, item.wh_id, item.o_non_std_code, item.non_std_code, item.price_into, item.intoing_amt
        ,inv.non_std_code,oinv.non_std_code AS "oNonStdCode",wh.wh_Name AS "whName",owh.wh_Name AS "oWhName",item.flg_valid
        ,sku.sku_code,sku.sku_name,sku.sku_model,brand.short_name
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.TransferItem">
        <id column="item_id" property="itemId"/>
                <result column="transfer_id" property="transferId" typeHandler="UuidTypeHandler"/>
            <result column="transfer_date" property="transferDate" typeHandler="TimestampTypeHandler"/>
                <result column="item_index" property="itemIndex"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="o_inv_id" property="oInvId" typeHandler="UuidTypeHandler"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="o_wh_id" property="oWhId" typeHandler="UuidTypeHandler"/>
                <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
                <result column="o_non_std_code" property="oNonStdCode"/>
                <result column="non_std_code" property="nonStdCode"/>
                <result column="price_into" property="priceInto"/>
                <result column="intoing_amt" property="intoingAmt"/>
                <result column="transfer_qty" property="transferQty"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
            <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
            <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
                <result column="op_app_code" property="opAppCode"/>
            <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
                <result column="op_db_user" property="opDbUser"/>
    </resultMap>

    <!-- 一览查询映射结果 -->
    <resultMap id="BaseResultMap_Response" type="com.dk.mdm.model.response.ivt.TransferItemResponse">
        <id column="item_id" property="itemId"/>
        <result column="transfer_id" property="transferId" typeHandler="UuidTypeHandler"/>
        <result column="transfer_date" property="transferDate" typeHandler="TimestampTypeHandler"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="o_inv_id" property="oInvId" typeHandler="UuidTypeHandler"/>
        <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
        <result column="o_wh_id" property="oWhId" typeHandler="UuidTypeHandler"/>
        <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
        <result column="o_non_std_code" property="oNonStdCode"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="price_into" property="priceInto"/>
        <result column="intoing_amt" property="intoingAmt"/>
        <result column="transfer_qty" property="transferQty"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
    </resultMap>
    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="transferId != null and transferId != ''">
                AND transfer_id = #{transferId}::uuid
            </if>
            <if test="transferDate != null">
                AND transfer_date = #{transferDate}
            </if>
            <if test="itemIndex != null">
                AND item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND sku_id = #{skuId}
            </if>
            <if test="oInvId != null and oInvId != ''">
                AND o_inv_id = #{oInvId}
            </if>
            <if test="invId != null and invId != ''">
                AND inv_id = #{invId}
            </if>
            <if test="oWhId != null and oWhId != ''">
                AND o_wh_id = #{oWhId}
            </if>
            <if test="whId != null and whId != ''">
                AND wh_id = #{whId}
            </if>
            <if test="oNonStdCode != null and oNonStdCode != ''">
                AND o_non_std_code = #{oNonStdCode}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND non_std_code = #{nonStdCode}
            </if>
            <if test="priceInto != null">
                AND price_into = #{priceInto}
            </if>
            <if test="intoingAmt != null">
                AND intoing_amt = #{intoingAmt}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            <if test="opCreateTime != null">
                AND op_create_time = #{opCreateTime}
            </if>
            <if test="opCreateUserId != null and opCreateUserId != ''">
                AND op_create_user_id = #{opCreateUserId}
            </if>
            <if test="opUpdateTime != null">
                AND op_update_time = #{opUpdateTime}
            </if>
            <if test="opUpdateUserId != null and opUpdateUserId != ''">
                AND op_update_user_id = #{opUpdateUserId}
            </if>
            <if test="opAppCode != null and opAppCode != ''">
                AND op_app_code = #{opAppCode}
            </if>
            <if test="opTimestamp != null">
                AND op_timestamp = #{opTimestamp}
            </if>
            <if test="opDbUser != null and opDbUser != ''">
                AND op_db_user = #{opDbUser}
            </if>
        </where>
    </sql>

    <!-- 一览条件列 -->
    <sql id="Condition_Response">
        <where>
            <if test="transferId != null and transferId != ''">
                AND item.transfer_id = #{transferId}::uuid
            </if>
        </where>
    </sql>

    <!-- 查询表t_psi_transfer_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap_Response">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_transfer_item AS item
        LEFT JOIN dkic_b.t_mst_goods_sku AS sku ON item.sku_id=sku.sku_id
        LEFT JOIN dkic_b.t_psi_inventory AS inv ON inv.inv_id=item.inv_id
        LEFT JOIN dkic_b.t_mst_warehouse AS wh ON wh.wh_id=inv.wh_id
        LEFT JOIN dkic_b.t_psi_inventory AS oinv ON oinv.inv_id=item.o_inv_id
        LEFT JOIN dkic_b.t_mst_warehouse AS owh ON owh.wh_id=item.o_wh_id
        LEFT JOIN dkic_b.t_mst_goods_brand AS brand ON brand.brand_id=sku.brand_id
        <include refid="Condition_Response"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_psi_transfer_item，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_transfer_item
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表t_psi_transfer_item的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_transfer_item
        WHERE item_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_psi_transfer_item的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_transfer_item
        WHERE item_id = #{itemId}
        for update
    </select>

</mapper>
