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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, into_id, into_type, from_id, from_item_id, item_index, sku_id, price_into, non_std_code,
        into_status, sys.f_remove_zero(intoing_qty) as intoing_qty, intoing_amt, sys.f_remove_zero(into_qty) as into_qty,
        into_amt, sys.f_remove_zero(return_qty) as return_qty, return_amt,
        remarks, inv_id, cost_price, cost_amt, sys.f_remove_zero(out_qty) as out_qty, sys.f_remove_zero(inv_qty) as inv_qty, 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,
        s_into_id,s_into_item_id,s_pur_id,s_pur_item_id,intoing_box,intoing_piece,into_box,into_piece,
        unit_id,sub_unit_id, pack_box
    </sql>

    <!-- response通用查询列 -->
    <sql id="Base_Column_List_Response">
        tpii.item_id, tpii.into_id, tpii.into_type, tpii.from_id, tpii.from_item_id, tpii.item_index, tpii.sku_id, tpii.price_into,
        tpii.non_std_code, tpii.into_status, sys.f_remove_zero(tpii.intoing_qty) as intoing_qty, tpii.intoing_amt,
        sys.f_remove_zero(tpii.into_qty) as into_qty, tpii.into_amt, sys.f_remove_zero(tpii.return_qty) as return_qty,
        tpii.return_amt, tpii.remarks, tpii.inv_id, tpii.cost_price, tpii.cost_amt,
        sys.f_remove_zero(tpii.out_qty) as out_qty, sys.f_remove_zero(tpii.inv_qty) as inv_qty, tpii.flg_valid,
        tpii.cp_id,tpii.s_into_id,tpii.s_into_item_id,tpii.s_pur_id,tpii.s_pur_item_id,
        tpii.intoing_box,tpii.intoing_piece,tpii.into_box,tpii.into_piece,
        tpii.unit_id,tpii.sub_unit_id,tpii.pack_box,tpii.flg_cost_check
    </sql>


    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.InboundItem">
        <id column="item_id" property="itemId"/>
                <result column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
                <result column="into_type" property="intoType"/>
                <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
                <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
                <result column="item_index" property="itemIndex"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="price_into" property="priceInto"/>
                <result column="non_std_code" property="nonStdCode"/>
                <result column="into_status" property="intoStatus"/>
                <result column="intoing_qty" property="intoingQty"/>
                <result column="intoing_amt" property="intoingAmt"/>
                <result column="into_qty" property="intoQty"/>
                <result column="into_amt" property="intoAmt"/>
                <result column="return_qty" property="returnQty"/>
                <result column="return_amt" property="returnAmt"/>
                <result column="remarks" property="remarks"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="cost_price" property="costPrice"/>
                <result column="cost_amt" property="costAmt"/>
                <result column="out_qty" property="outQty"/>
                <result column="inv_qty" property="invQty"/>
                <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"/>
        <result column="s_into_id" property="sIntoId" typeHandler="UuidTypeHandler"/>
        <result column="s_into_item_id" property="sIntoItemId" typeHandler="UuidTypeHandler"/>
        <result column="s_pur_id" property="sPurId" typeHandler="UuidTypeHandler"/>
        <result column="s_pur_item_id" property="sPurItemId" typeHandler="UuidTypeHandler"/>
        <result column="intoing_box" property="intoingBox" />
        <result column="intoing_piece" property="intoingPiece" />
        <result column="into_box" property="intoBox" />
        <result column="into_piece" property="intoPiece" />
    </resultMap>


    <!-- response查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.InboundItemResponse">
        <id column="item_id" property="itemId"/>
        <result column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_type" property="intoType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="sku_model" property="skuModel"/>
        <result column="sku_name" property="skuName"/>
        <result column="price_into" property="priceInto"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="into_status" property="intoStatus"/>
        <result column="intoing_qty" property="intoingQty"/>
        <result column="intoing_amt" property="intoingAmt"/>
        <result column="into_qty" property="intoQty"/>
        <result column="into_amt" property="intoAmt"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="remarks" property="remarks"/>
        <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
        <result column="cost_price" property="costPrice"/>
        <result column="cost_amt" property="costAmt"/>
        <result column="brandName" property="brandName"/>
        <result column="out_qty" property="outQty"/>
        <result column="inv_qty" property="invQty"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="price_purchase" property="pricePurchase"/>
        <result column="sku_images" property="skuImages" typeHandler="JsonTypeHandler"/>
        <result column="s_into_id" property="sIntoId" typeHandler="UuidTypeHandler"/>
        <result column="s_into_item_id" property="sIntoItemId" typeHandler="UuidTypeHandler"/>
        <result column="s_pur_id" property="sPurId" typeHandler="UuidTypeHandler"/>
        <result column="s_pur_item_id" property="sPurItemId" typeHandler="UuidTypeHandler"/>
        <result column="intoing_box" property="intoingBox" />
        <result column="intoing_piece" property="intoingPiece" />
        <result column="into_box" property="intoBox" />
        <result column="into_piece" property="intoPiece" />
        <result column="unit_id" property="unitId"/>
        <result column="sub_unit_id" property="subUnitId"/>
        <result column="pack_box" property="packBox"/>
        <result column="subUnitName" property="subUnitName"/>
        <result column="unitName" property="unitName"/>
        <result column="whName" property="whName"/>
        <result column="intoDate" property="intoDate"  typeHandler="TimestampTypeHandler"/>
        <result column="seriesName" property="seriesName"/>
        <result column="intoStatusName" property="intoStatusName"/>
        <result column="flg_sub_unit" property="flgSubUnit"/>
        <result column="intoTypeName" property="intoTypeName"/>
        <result column="flg_cost_check" property="flgCostCheck"/>
        <result column="flgCostCheckName" property="flgCostCheckName"/>
        <result column="ingBoxPiece" property="ingBoxPiece"/>
        <result column="boxPiece" property="boxPiece"/>
        <result column="skuModel" property="skuModel"/>
        <result column="skuName" property="skuName"/>
        <result column="supName" property="supName"/>
    </resultMap>
<!--    退货用-->
    <resultMap id="BaseResultMapReturnResponse" type="com.dk.mdm.model.response.ivt.InboundItemResponse">
        <id column="item_id" property="itemId"/>
        <result column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_type" property="intoType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="price_into" property="priceInto"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="into_status" property="intoStatus"/>
        <result column="intoing_qty" property="intoingQty"/>
        <result column="intoing_amt" property="intoingAmt"/>
        <result column="can_return_qty" property="canReturnQty"/>
        <result column="into_qty" property="intoQty"/>
        <result column="into_amt" property="intoAmt"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="remarks" property="remarks"/>
        <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
        <result column="cost_price" property="costPrice"/>
        <result column="cost_amt" property="costAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="inv_qty" property="invQty"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="s_into_id" property="sIntoId" typeHandler="UuidTypeHandler"/>
        <result column="s_into_item_id" property="sIntoItemId" typeHandler="UuidTypeHandler"/>
        <result column="s_pur_id" property="sPurId" typeHandler="UuidTypeHandler"/>
        <result column="s_pur_item_id" property="sPurItemId" typeHandler="UuidTypeHandler"/>
        <result column="sku_images" property="skuImages" typeHandler="JsonTypeHandler"/>
        <result column="sIntoItemIntoingQty" property="sIntoItemIntoingQty"/>
        <result column="sIntoItemIntoingAmt" property="sIntoItemIntoingAmt"/>
        <result column="sIntoItemIntoQty" property="sIntoItemIntoQty"/>
        <result column="sIntoItemIntoAmt" property="sIntoItemIntoAmt"/>
        <result column="sIntoItemReturnQty" property="sIntoItemReturnQty"/>
        <result column="sIntoItemReturnAmt" property="sIntoItemReturnAmt"/>


        <result column="sPurItemIntoingQty" property="sPurItemIntoingQty"/>
        <result column="sPurItemIntoingAmt" property="sPurItemIntoingAmt"/>
        <result column="sPurItemIntoQty" property="sPurItemIntoQty"/>
        <result column="sPurItemIntoAmt" property="sPurItemIntoAmt"/>
        <result column="sPurItemReturnQty" property="sPurItemReturnQty"/>
        <result column="sPurItemReturnAmt" property="sPurItemReturnAmt"/>
        <result column="usableQty" property="usableQty"/>
        <result column="nonStdCode" property="nonStdCode"/>
        <result column="intoing_box" property="intoingBox" />
        <result column="intoing_piece" property="intoingPiece" />
        <result column="into_box" property="intoBox" />
        <result column="into_piece" property="intoPiece" />
        <result column="unit_id" property="unitId"/>
        <result column="unit_code" property="unitCode"/>
        <result column="unit_name" property="unitName"/>
        <result column="sub_unit_id" property="subUnitId"/>
        <result column="sub_unit_code" property="subUnitCode"/>
        <result column="sub_unit_name" property="subUnitName"/>
        <result column="pack_box" property="packBox"/>
        <result column="sItemAmt" property="sItemAmt"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            tpii.flg_valid
            <if test="intoId != null and intoId != ''">
                AND tpii.into_id = #{intoId}::uuid
            </if>
            <if test="intoType != null and intoType != ''">
                AND tpii.into_type = #{intoType}
            </if>
            <if test="fromId != null and fromId != ''">
                AND tpii.from_id = #{fromId}::uuid
            </if>
            <if test="fromItemId != null and fromItemId != ''">
                AND tpii.from_item_id = #{fromItemId}::uuid
            </if>
            <if test="itemIndex != null">
                AND tpii.item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND tpii.sku_id = #{skuId}
            </if>
            <if test="priceInto != null">
                AND tpii.price_into = #{priceInto}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND tpii.non_std_code = #{nonStdCode}
            </if>
            <if test="invId != null and invId != ''">
                AND tpii.inv_id = #{invId}
            </if>
            <if test="cpId != null">
                AND tpii.cp_id = #{cpId}
            </if>
            <if test="sPurId != null">
                AND tpii.s_pur_id = #{sPurId,typeHandler=UuidTypeHandler}
            </if>
            <if test="sPurItemId != null">
                AND tpii.s_pur_item_id = #{sPurItemId,typeHandler=UuidTypeHandler}
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键itemId批量操作 -->
        WHERE item_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表dkic_b.t_psi_inbound_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tpii.wh_id as "whId"
        ,tmw.wh_name as "whName"
        ,tmgs.sku_code
        ,tmgs.sku_model
        ,tmgs.sku_name
        ,tmgs.sku_spec
        ,tmgs.price_purchase
        ,tmgs.sku_images
        ,tmgb.brand_name  as "brandName"
        ,tmgb.short_name  as "shortName"
        ,tpp.pur_id as "purId"
        ,tpp.pur_no as "purNo"
        ,sys.f_remove_zero(tppi.item_qty)  as "purItemQty"
        ,sys.f_remove_zero(tppi.intoing_qty) as "purItemIntoingQty"
        ,sys.f_remove_zero(tppi.into_qty) as "purItemIntoQty"
        ,sys.f_remove_zero(tpiv.inv_qty) as inv_qty
        ,tmgs.pack_box
        ,tmgus.unit_name         as "subUnitName"
        ,tmgu.unit_name         as "unitName"
        ,tmgs.flg_sub_unit
        ,dkic_b.f_box_piece(tmgus.unit_name,tmgu.unit_name,tpii.intoing_box,tpii.intoing_piece) as "ingBoxPiece"
        ,dkic_b.f_box_piece(tmgus.unit_name,tmgu.unit_name,tpii.into_box,tpii.into_piece) as "boxPiece"
        ,tmgu.decimal_places    as "decimalPlaces"
        FROM dkic_b.t_psi_inbound_item tpii
        left join dkic_b.t_mst_unit as tmgu on tpii.unit_id = tmgu.unit_id
        left join dkic_b.t_mst_unit as tmgus on tpii.sub_unit_id = tmgus.unit_id
        left join dkic_b.t_psi_inventory tpiv on tpiv.inv_id = tpii.inv_id
        left join dkic_b.t_psi_purchase_item tppi on tppi.item_id = tpii.from_item_id
        left join dkic_b.t_psi_purchase tpp on tpp.pur_id = tpii.from_id
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
        left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
        left join dkic_b.t_psi_inventory tpi on tpi.inv_id = tpii.inv_id
        left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpii.wh_id
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表dkic_b.t_psi_inbound_item，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_inbound_item tpii
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
        left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
        left join dkic_b.t_psi_inventory tpi on tpi.inv_id = tpii.inv_id
        left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpii.wh_id
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表dkic_b.t_psi_inbound_item的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where tpii.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_inbound_item tpii
        WHERE tpii.item_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表dkic_b.t_psi_inbound_item的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_inbound_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_inbound_item
        WHERE item_id = #{itemId}
        for update
    </select>

    <!-- 根据主键锁定表dkic_b.t_psi_inbound_item的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_inbound_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_inbound_item
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into dkic_b.t_psi_inbound_item
        (
        <trim suffixOverrides=",">
            into_id,
            into_type,
            from_id,
            from_item_id,
            item_index,
            sku_id,
            price_into,
            non_std_code,
            into_status,
            intoing_qty,
            intoing_amt,
            into_qty,
            into_amt,
            return_qty,
            return_amt,
            remarks,
            inv_id,
            cost_price,
            cost_amt,
            out_qty,
            inv_qty,
            cp_id,
            op_app_code,
            s_into_id,
            s_into_item_id,
            s_pur_id,
            s_pur_item_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.intoId}::uuid,
                #{item.intoType},
                #{item.fromId}::uuid,
                #{item.fromItemId}::uuid,
                #{item.itemIndex},
                #{item.skuId}::uuid,
                #{item.priceInto},
                #{item.nonStdCode},
                #{item.intoStatus},
                #{item.intoingQty},
                #{item.intoingAmt},
                #{item.intoQty},
                #{item.intoAmt},
                #{item.returnQty},
                #{item.returnAmt},
                #{item.remarks},
                #{item.invId}::uuid,
                #{item.costPrice},
                #{item.costAmt},
                #{item.outQty},
                #{item.invQty},
                #{item.cpId},
                #{item.opAppCode},
                #{item.sIntoId}::uuid,
                #{item.sIntoItemId}::uuid,
                #{item.sPurId}::uuid,
                #{item.sPurItemId}::uuid,
            </trim>
            )
        </foreach>
    </insert>

    <update id="updateAmount" parameterType="com.dk.mdm.model.pojo.ivt.InboundItem">
        update dkic_b.t_psi_inbound_item
        <set>
            <if test="returnQty!= null">
                return_qty= #{returnQty},
            </if>
            <if test="returnAmt!= null">
                return_amt = #{returnAmt},
            </if>
        </set>
        where item_id = #{itemId}::uuid
    </update>


    <!-- 入库办理显示字段 -->
    <sql id="Base_Column_List_Response_Into">
        tpii.item_id,
        tpii.into_id,
        tpii.into_type,
        tpii.from_id,
        tpii.from_item_id,
        tpii.item_index,
        tpii.sku_id,
        tpii.price_into,
        tpii.non_std_code,
        tpii.into_status,
        sys.f_remove_zero(tpii.intoing_qty) as intoing_qty,
        tpii.intoing_amt,
        sys.f_remove_zero(tpii.into_qty) as into_qty,
        tpii.into_amt,
        sys.f_remove_zero(tpii.return_qty) as return_qty,
        tpii.return_amt,
        tpii.remarks,
        tpii.inv_id,
        tpii.cost_price,
        tpii.cost_amt,
        tpii.wh_id,
        sys.f_remove_zero(tpii.inv_qty) as inv_qty,
        tpii.intoing_box,
        tpii.intoing_piece,
        tpii.into_box,
        tpii.into_piece,
        tpii.pack_box
    </sql>
    <sql id="Base_Column_List_Response_Out">
        tpoi.item_id,
        tpoi.out_id as into_id,
        tpoi.out_type as into_type,
        tpoi.from_id,
        tpoi.from_item_id,
        tpoi.item_index,
        tpoi.sku_id,
        tpoi.price_out as price_into,
        tpoi.non_std_code,
        tpoi.out_status as into_status,
        tpoi.outing_qty as intoing_amt ,
        sys.f_remove_zero(tpoi.outing_amt) as intoing_qty ,
        sys.f_remove_zero(tpoi.out_qty) as into_qty ,
        tpoi.out_amt as into_amt ,
        sys.f_remove_zero(tpoi.return_qty) as return_qty,
        tpoi.return_amt,
        tpoi.remarks,
        tpoi.inv_id,
        tpoi.cost_price,
        tpoi.cost_amt,
        null as  wh_id,
        null  as inv_qty,
        tpoi.outing_box as intoing_box,
        tpoi.outing_piece as intoing_piece,
        tpoi.out_box as into_box,
        tpoi.out_piece as into_piece,
        tpoi.pack_box
    </sql>
    <!--条件查询 web端入库办理用-->
    <select id="selectInboundItem" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response_Into"/>
        ,tmgs.sku_code  as "skuCode"
        ,tmgs.sku_model
        ,tmgs.sku_name
        ,tmgs.sku_spec  as "skuSpec"
        ,tmgb.brand_name  as "brandName"
        ,tmgb.short_name  as "shortName"
        ,tmw.wh_name as "whName"
        ,tmu.unit_code as "unitCode"
        ,tmu.unit_name as "unitName"
        ,tmu2.unit_code as "subUnitCode"
        ,tmu2.unit_name as "subUnitName"
        ,tmu.decimal_places    as "decimalPlaces"
        ,dkic_b.f_box_piece(tmu2.unit_name,tmu.unit_name,tpii.intoing_box,tpii.intoing_piece) as "ingBoxPiece"
        ,dkic_b.f_box_piece(tmu2.unit_name,tmu.unit_name,tpii.into_box,tpii.into_piece) as "boxPiece"
        FROM dkic_b.t_psi_inbound_item tpii
        <include refid="Select_Inbound_Join_Table_Into"/>
        <where>
            tpii.flg_valid
            <if test="intoId != null and intoId != ''">
                AND tpii.into_id = #{intoId}::uuid
            </if>
        </where>
        union all
        select
        <include refid="Base_Column_List_Response_Out"/>
        ,tmgs.sku_code  as "skuCode"
        ,tmgs.sku_model as "skuModel"
        ,tmgs.sku_name  as "skuName"
        ,tmgs.sku_spec  as "skuSpec"
        ,tmgb.brand_name  as "brandName"
        ,tmgb.short_name  as "shortName"
        ,null as "whName"
        ,tmu.unit_code as "unitCode"
        ,tmu.unit_name as "unitName"
        ,tmu2.unit_code as "subUnitCode"
        ,tmu2.unit_name as "subUnitName"
        ,tmu.decimal_places    as "decimalPlaces"
        ,dkic_b.f_box_piece(tmu2.unit_name,tmu.unit_name,tpoi.outing_box,tpoi.outing_piece) as "ingBoxPiece"
        ,dkic_b.f_box_piece(tmu2.unit_name,tmu.unit_name,tpoi.out_box,tpoi.out_piece) as "boxPiece"
        from dkic_b.t_psi_outbound_item tpoi
        <include refid="Select_Inbound_Join_Table_Out"/>
        <where>
            tpoi.flg_valid
            <if test="intoId != null and intoId != ''">
                AND tpoi.out_id = #{intoId}::uuid
            </if>
        </where>
    </select>
    <!--入库办理显示入库关联表-->
    <sql id="Select_Inbound_Join_Table_Into">
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
        left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
        left join dkic_b.t_psi_inventory tpi on tpi.inv_id = tpii.inv_id
        left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpii.wh_id
        left join dkic_b.t_mst_unit tmu on tmu.unit_id = tpii.unit_id
        left join dkic_b.t_mst_unit tmu2 on tmu2.unit_id = tpii.unit_id
    </sql>
    <sql id="Select_Inbound_Join_Table_Out">
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpoi.sku_id
        left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
        left join dkic_b.t_psi_inventory tpi on tpi.inv_id = tpoi.inv_id
        left join dkic_b.t_mst_unit tmu on tmu.unit_id = tpoi.unit_id
        left join dkic_b.t_mst_unit tmu2 on tmu2.unit_id = tpoi.sub_unit_id
    </sql>


    <delete id="deleteByFromId" parameterType="com.dk.mdm.model.pojo.ivt.InboundItem">

        DELETE FROM dkic_b.t_psi_inbound_item WHERE from_item_id =  #{fromItemId}::uuid
    </delete>



    <update id="updateIntoStatus" parameterType="com.dk.mdm.model.pojo.ivt.InboundItem">
        update dkic_b.t_psi_inbound_item
        <set>
            <if test="intoStatus!= null">
                into_status= #{intoStatus},
            </if>

        </set>
        where item_id = #{itemId}::uuid
    </update>

    <select id="selectByInboundReturnCond" resultMap="BaseResultMapReturnResponse">
        SELECT
        tpii.item_id,
        tpii.into_id,
        tpii.into_type,
        tpii.from_id,
        tpii.from_item_id,
        tpii.item_index,
        tpii.sku_id,
        tpii.price_into,
        tpii.non_std_code,
        tpii.into_status,
      --   sys.f_remove_zero(tpii.into_qty - tpii.return_qty)  AS can_return_qty,
        -sys.f_remove_zero ( COALESCE ( tpsii.into_qty, 0 )  + tpii.intoing_qty) AS can_return_qty,
        sys.f_remove_zero(tpii.intoing_qty + tpii.into_qty)               as intoing_qty,
        sys.f_remove_zero(tpii.intoing_amt + tpii.into_amt)   as intoing_amt,
        sys.f_remove_zero ( tpii.into_qty ) AS into_qty,
        tpii.into_amt,
        sys.f_remove_zero ( tpii.return_qty ) AS return_qty,
        tpii.return_amt,
        tpii.remarks,
        tpii.inv_id,
        tpii.cost_price,
        tpii.cost_amt,
        sys.f_remove_zero ( tpii.out_qty ) AS out_qty,
        sys.f_remove_zero ( tpi.inv_qty ) AS inv_qty,
        tpii.flg_valid,
        tpii.cp_id,
        tpii.s_into_id,
        tpii.s_into_item_id,
        tpii.s_pur_id,
        tpii.s_pur_item_id,
        tpii.intoing_box,
        tpii.intoing_piece,
        tpii.into_box,
        tpii.into_piece,
        tpii.unit_id,
        tpii.sub_unit_id,
        tpii.pack_box,
        tpii.flg_cost_check,
        tmgs.sku_code AS "skuCode",
        tmgs.sku_model AS "skuModel",
        tmgs.sku_name AS "skuName",
        tmgs.sku_spec AS "skuSpec",
        tmgs.sku_images,
        tmgb.brand_name AS "brandName",
        tmgb.short_name AS "shortName",
        tmw.wh_id AS "whId",
        tmw.wh_code AS "whCode",
        tmw.wh_name AS "whName",
        sys.f_remove_zero ( tpsii.intoing_qty ) AS "sIntoItemIntoingQty",
        tpsii.intoing_amt AS "sIntoItemIntoingAmt",
        sys.f_remove_zero ( tpsii.into_qty ) AS "sIntoItemIntoQty",
        tpsii.into_amt AS "sIntoItemIntoAmt",
        sys.f_remove_zero ( tpsii.return_qty ) AS "sIntoItemReturnQty",
        tpsii.return_amt AS "sIntoItemReturnAmt",
        sys.f_remove_zero ( tppi.intoing_qty ) AS "sPurItemIntoingQty",
        tppi.intoing_amt AS "sPurItemIntoingAmt",
        sys.f_remove_zero ( tppi.into_qty ) AS "sPurItemIntoQty",
        tppi.into_amt AS "sPurItemIntoAmt",
        sys.f_remove_zero ( tppi.return_qty ) AS "sPurItemReturnQty",
        tppi.return_amt AS "sPurItemReturnAmt",
        sys.f_remove_zero ( tpi.usable_qty ) AS "usableQty",
        tpi.non_std_code AS "non_std_code",
        ( SELECT ul.decimal_places FROM dkic_b.t_mst_unit ul WHERE tpii.unit_id = ul.unit_id ) AS "decimalPlaces",
        tppi.item_amt AS "sItemAmt"
        FROM
        dkic_b.t_psi_inbound_item tpii
        LEFT JOIN dkic_b.t_mst_goods_sku tmgs ON tmgs.sku_id = tpii.sku_id
        LEFT JOIN dkic_b.t_mst_goods_brand tmgb ON tmgb.brand_id = tmgs.brand_id
        LEFT JOIN dkic_b.t_psi_inventory tpi ON tpi.inv_id = tpii.inv_id
        LEFT JOIN dkic_b.t_mst_warehouse tmw ON tmw.wh_id = tpii.wh_id
        LEFT JOIN dkic_b.t_psi_inbound_item tpsii ON tpsii.item_id = tpii.s_into_item_id
        LEFT JOIN dkic_b.t_psi_purchase_item tppi ON tppi.item_id = tpii.s_pur_item_id
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>



    <!-- 条件查询 (入库成本核对用) -->
    <select id="selectCostCheck" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tpid.into_no as "intoNo"
        ,tmgs.sku_code  as "skuCode"

        ,case when tmgs.sku_model is null then tpii.sku_model
        when tmgs.sku_model is not null then tmgs.sku_model
        else null end as "skuModel"
        ,case when tmgs.sku_name is null then tpii.sku_name
        when tmgs.sku_name is not null then tmgs.sku_name
        else null end as "skuName"


--         ,tmgs.sku_model as "skuModel"
--         ,tmgs.sku_name  as "skuName"
--         ,tpii.sku_model as "skuModel"
--         ,tpii.sku_name  as "skuName"
        ,tmgs.sku_spec  as "skuSpec"
        ,tmgb.brand_name  as "brandName"
        ,tmgb.short_name  as "shortName"
        ,tpp.pur_id as "purId"
        ,tpp.pur_no as "purNo"
        ,sys.f_remove_zero(tppi.item_qty) as "purItemQty"
        ,sys.f_remove_zero(tppi.intoing_qty) as "purItemIntoingQty"
        ,sys.f_remove_zero(tppi.into_qty) as "purItemIntoQty"
        ,tmp.sup_code as "supCode"
        ,tmp.sup_name as "supName"
        ,tmw.wh_name AS "whName"
        ,tpid.into_date AS "intoDate"
        ,sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "intoStatusName"
        ,sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) AS  "intoTypeName"
        ,tmgss.series_name AS "seriesName"
        ,case when tpii.flg_cost_check is null then '未核对'
        when tpii.flg_cost_check = true then '已核对'
        else '未核对' end as "flgCostCheckName"
        ,sys.f_remove_zero(tpiv.inv_qty)  as inv_qty
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where tpii.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_inbound_item tpii
        left join dkic_b.t_psi_inbound tpid on tpii.into_id = tpid.into_id
        left join dkic_b.t_psi_inventory tpiv on tpiv.inv_id = tpii.inv_id
        left join dkic_b.t_psi_purchase_item tppi on tppi.item_id = tpii.from_item_id
        left join dkic_b.t_psi_purchase tpp on tpp.pur_id = tpii.from_id
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
        left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
        left join dkic_b.t_mst_goods_series tmgss on tmgss.series_id = tmgs.series_id
        left join dkic_b.t_psi_inventory tpi on tpi.inv_id = tpii.inv_id
        left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpii.wh_id
        Left join dkic_b.t_mst_supplier tmp on tpid.sup_id = tmp.sup_id
        LEFT JOIN sys.t_data_kind tdk   ON tdk.kind_code = tpid.into_status
        LEFT JOIN sys.t_data_kind tdk1   ON tdk1.kind_code = tpii.into_type
        LEFT JOIN dkic_b.t_mst_staff tmss   ON tmss.staff_id = tpid.staff_id
        LEFT JOIN dkic_b.t_mst_org tmo   ON tmo.org_id = tpid.org_id
        <where>
            tpii.flg_valid
            and tpii.into_qty &lt;&gt; 0
            <if test="intoId != null and intoId != ''">
                AND tpii.into_id = #{intoId}::uuid
            </if>
            <if test="intoNo != null and intoNo != ''">
                AND tpid.into_no LIKE concat('%',my_ex.likequery(#{intoNo}),'%')
            </if>
            <if test="supId != null and supId != ''">
                AND tpid.sup_id = #{supId}::uuid
            </if>
            <if test="intoDateStart != null">
                AND tpid.into_date &gt;= #{intoDateStart}::timestamp with time zone
            </if>
            <if test="intoDateEnd != null">
                AND tpid.into_date &lt; #{intoDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="skuName != null and skuName!='' ">
                AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
            </if>
            <if test="cpId != null">
                AND tpii.cp_id = #{cpId}
            </if>
            <if test="intoTypeName != null">
                AND sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) LIKE concat('%',my_ex.likequery(#{intoTypeName}),'%')
            </if>
            <if test="supName != null">
                AND tmp.sup_name LIKE concat('%',my_ex.likequery(#{supName}),'%')
            </if>
            <if test="viewNoCostDocuments !=null and viewNoCostDocuments == true">
                AND tpii.flg_cost_check = false
            </if>
            <if test="staffName !=null">
                AND tmss.staff_name  LIKE concat('%',my_ex.likequery(#{staffName}),'%')
            </if>
            <if test="orgName !=null">
                AND tmo.org_name  LIKE concat('%',my_ex.likequery(#{orgName}),'%')
            </if>
            <if test="intoStatusList != null and intoStatusList.size()>0">
                AND tpii.into_status =
                any(#{intoStatusList, typeHandler= StringListTypeHandler})
            </if>
            <if test="skuModel != null and skuModel != ''">
                AND tmgs.sku_model LIKE concat('%',my_ex.likequery(#{skuModel}),'%')
            </if>
            <if test="searchText !=null">
                AND  (tpid.into_no LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                OR   tmgs.sku_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                OR   tmgs.sku_model LIKE concat('%',my_ex.likequery(#{searchText}),'%'))
            </if>
        </where>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!--入库成本核对-->
    <update id="updateCost" parameterType="com.dk.mdm.model.pojo.ivt.InboundItem">
        update dkic_b.t_psi_inbound_item
        <set>
            flg_cost_check = true,
            <if test="costPrice!= null">
                cost_price= #{costPrice},
            </if>
            <if test="costAmt!= null">
                cost_amt = #{costAmt},
            </if>
        </set>
        where item_id = #{itemId}::uuid
    </update>

    <!--根据来源id删除单据-->
    <update id="deleteItemByFromId">
        update dkic_b.t_psi_inbound_item
        <set>
            flg_valid = false
        </set>
        WHERE from_id = #{fromId}::uuid
    </update>

    <!--查询入库价-->
    <select id="selectPriceInto"  resultMap="BaseResultMapResponse">
        SELECT
        ti.item_id,ti.price_into,ti.cost_price,ti.cost_amt
        FROM dkic_b.t_psi_inbound_item ti
        left join dkic_b.t_psi_inbound t ON ti.into_id = t.into_id
        where  t.flg_valid
        and ti.flg_valid
        and ti.price_into > 0
        and t.into_type != '外协入库'
        and ti.wh_id = #{whId}::uuid
        and ti.sku_id =#{skuId}::uuid
        and ti.non_std_code = #{nonStdCode}
        order by make_time desc
    </select>




    <select id="selectCostCheckList" resultType="map">
        SELECT
        A."skuModel",
        A."skuName",
        A."skuId",
        A."costPrice",
        A."costAmt",
        A."skuSpec",
        A."skuCode",
        A."brandId",
        A."skuImages",
        A."brandName",
        A."shortName",
        json_agg (
        json_build_object (
        'itemId', A."itemId",
        'intoId', A."intoId",
        'outId', A."outId",
        'invId', A."invId",
        'intoType', A."intoType",
        'outType', A."outType",
        'fromId', A."fromId",
        'fromItemId', A."fromItemId",
        'skuModel', A."skuModel",
        'skuName', A."skuName",
        'skuId', A."skuId",
        'intoQty', A."intoQty",
        'outQty', A."outQty",
        'costPrice', A."costPrice",
        'costAmt', A."costAmt",
        'brandId', A."brandId",
        'skuImages', A."skuImages",
        'brandName', A."brandName",
        'shortName', A."shortName"
        )
        ) AS "itemList"
        FROM
        (
        SELECT
        tpii.sku_model AS "skuModel",
        tpii.sku_name AS "skuName",
        tpii.sku_id AS "skuId",
        tpii.cost_price AS "costPrice",
        tpii.cost_amt AS "costAmt",
        tmgs.sku_spec AS "skuSpec",
        tmgs.sku_code AS "skuCode",
        tmgs.brand_id AS "brandId",
        tmgs.sku_images AS "skuImages",
        tmgb.brand_name AS "brandName",
        tmgb.short_name AS "shortName",
        tpii.item_id AS "itemId",
        tpii.into_id AS "intoId",
        NULL AS "outId",
        tpii.inv_id AS "invId",
        null AS "outType",
        tpii.into_type AS "intoType",
        tpii.from_id AS "fromId",
        tpii.from_item_id AS "fromItemId",
        tpii.into_qty AS "intoQty",
        null  AS "outQty"
        FROM
        dkic_b.t_psi_inbound_item tpii
        LEFT JOIN dkic_b.t_psi_inbound tpi ON tpi.into_id = tpii.into_id
        LEFT JOIN dkic_b.t_mst_goods_sku tmgs ON tmgs.sku_id = tpii.sku_id
        LEFT JOIN dkic_b.t_mst_goods_brand tmgb ON tmgb.brand_id = tmgs.brand_id
        <where>
            tpii.cost_price = 0
            AND tpii.into_qty != 0
            AND tpii.sku_model IS NOT NULL
            AND tpii.into_type != '入库类型-采退入库'
            <if test="intoId != null and intoId != ''">
                AND tpii.into_id = #{intoId}::uuid
            </if>
            <if test="cpId != null">
                AND tpii.cp_id = #{cpId}
            </if>
            <if test="brandId != null">
                AND tmgs.brand_id = #{brandId}::uuid
            </if>
            <if test="intoDateStart != null">
                AND tpi.into_date &gt;= #{intoDateStart}::timestamp with time zone
            </if>
            <if test="intoDateEnd != null">
                AND tpi.into_date &lt; #{intoDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="viewNoCostDocuments !=null and viewNoCostDocuments == true">
                AND tpii.flg_cost_check = false
            </if>
            <if test="staffName !=null">
                AND tmss.staff_name  LIKE concat('%',my_ex.likequery(#{staffName}),'%')
            </if>
            <if test="intoStatusList != null and intoStatusList.size()>0">
                AND tpii.into_status =
                any(#{intoStatusList, typeHandler= StringListTypeHandler})
            </if>
            <if test="brandId != null and brandId != ''">
                AND tmgb.brand_id = #{brandId}::uuid
            </if>
            <if test="skuIdFlag !=null and skuIdFlag == true">
                AND tpii.sku_id is null
            </if>


            <if test="searchText !=null">
                AND  (tpii.sku_model LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                OR   tpii.sku_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>

        UNION ALL

        SELECT
        tpoi.sku_model AS "skuModel",
        tpoi.sku_name AS "skuName",
        tpoi.sku_id AS "skuId",
        tpoi.cost_price AS "costPrice",
        tpoi.cost_amt AS "costAmt",
        tmgs.sku_spec AS "skuSpec",
        tmgs.sku_code AS "skuCode",
        tmgs.brand_id AS "brandId",
        tmgs.sku_images AS "skuImages",
        tmgb.brand_name AS "brandName",
        tmgb.short_name AS "shortName",
        tpoi.item_id AS "itemId",
        NULL AS "intoId",
        tpoi.out_id AS "outId",
        tpoi.inv_id AS "invId",
        tpoi.out_type AS "outType",
        null AS "intoType",
        tpoi.from_id AS "fromId",
        tpoi.from_item_id AS "fromItemId",
        null AS "intoQty",
        tpoi.out_qty AS "outQty"
        FROM
        dkic_b.t_psi_outbound_item tpoi
        LEFT JOIN dkic_b.t_psi_outbound tpo ON tpo.out_id = tpoi.out_id
        LEFT JOIN dkic_b.t_mst_goods_sku tmgs ON tmgs.sku_id = tpoi.sku_id
        LEFT JOIN dkic_b.t_mst_goods_brand tmgb ON tmgb.brand_id = tmgs.brand_id
        <where>
            tpoi.cost_price = 0
            AND tpoi.out_qty != 0
            AND tpoi.sku_model IS NOT NULL
            <if test="outId != null and outId != ''">
                AND tpoi.out_id = #{outId}::uuid
            </if>
            <if test="cpId != null">
                AND tpoi.cp_id = #{cpId}
            </if>
            <if test="intoDateStart != null">
                AND tpo.out_date &gt;= #{intoDateStart}::timestamp with time zone
            </if>
            <if test="intoDateEnd != null">
                AND tpo.out_date &lt; #{intoDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="brandId != null and brandId != ''">
                AND tmgb.brand_id = #{brandId}::uuid
            </if>
            <if test="skuIdFlag !=null and skuIdFlag == true">
                AND tpoi.sku_id is null
            </if>
            <if test="searchText !=null">
                AND  (tpoi.sku_model LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                OR   tpoi.sku_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>
        ) AS A
        GROUP BY
        A."skuModel",
        A."skuName",
        A."skuId",
        A."costPrice",
        A."costAmt",
        A."skuSpec",
        A."skuCode",
        A."brandId",
        A."skuImages",
        A."brandName",
        A."shortName"
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>

    </select>

    <select id="countByCondList" resultType="Long">
        SELECT COUNT(1) from
        (
        SELECT
        A."skuModel",
        A."skuName",
        A."skuId",
        A."costPrice",
        A."costAmt",
        A."skuSpec",
        A."skuCode",
        A."brandId",
        A."skuImages",
        A."brandName",
        A."shortName",
        json_agg (
        json_build_object (
        'itemId', A."itemId",
        'intoId', A."intoId",
        'outId', A."outId",
        'invId', A."invId",
        'fromId', A."fromId",
        'fromItemId', A."fromItemId",
        'skuModel', A."skuModel",
        'skuName', A."skuName",
        'skuId', A."skuId",
        'intoQty', A."intoQty",
        'costPrice', A."costPrice",
        'costAmt', A."costAmt",
        'brandId', A."brandId",
        'skuImages', A."skuImages",
        'brandName', A."brandName",
        'shortName', A."shortName"
        )
        ) AS "itemList"
        FROM
        (
        SELECT
        tpii.sku_model AS "skuModel",
        tpii.sku_name AS "skuName",
        tpii.sku_id AS "skuId",
        tpii.cost_price AS "costPrice",
        tpii.cost_amt AS "costAmt",
        tmgs.sku_spec AS "skuSpec",
        tmgs.sku_code AS "skuCode",
        tmgs.brand_id AS "brandId",
        tmgs.sku_images AS "skuImages",
        tmgb.brand_name AS "brandName",
        tmgb.short_name AS "shortName",
        tpii.item_id AS "itemId",
        tpii.into_id AS "intoId",
        NULL AS "outId",
        tpii.inv_id AS "invId",
        tpii.from_id AS "fromId",
        tpii.from_item_id AS "fromItemId",
        tpii.into_qty AS "intoQty"
        FROM
        dkic_b.t_psi_inbound_item tpii
        LEFT JOIN dkic_b.t_psi_inbound tpi ON tpi.into_id = tpii.into_id
        LEFT JOIN dkic_b.t_mst_goods_sku tmgs ON tmgs.sku_id = tpii.sku_id
        LEFT JOIN dkic_b.t_mst_goods_brand tmgb ON tmgb.brand_id = tmgs.brand_id
        <where>
            tpii.cost_price = 0
            AND tpii.into_qty != 0
            AND tpii.sku_model IS NOT NULL
            AND tpii.into_type != '入库类型-采退入库'
            <if test="intoId != null and intoId != ''">
                AND tpii.into_id = #{intoId}::uuid
            </if>
            <if test="cpId != null">
                AND tpii.cp_id = #{cpId}
            </if>
            <if test="brandId != null">
                AND tmgs.brand_id = #{brandId}::uuid
            </if>
            <if test="intoDateStart != null">
                AND tpi.into_date &gt;= #{intoDateStart}::timestamp with time zone
            </if>
            <if test="intoDateEnd != null">
                AND tpi.into_date &lt; #{intoDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="viewNoCostDocuments !=null and viewNoCostDocuments == true">
                AND tpii.flg_cost_check = false
            </if>
            <if test="staffName !=null">
                AND tmss.staff_name  LIKE concat('%',my_ex.likequery(#{staffName}),'%')
            </if>
            <if test="intoStatusList != null and intoStatusList.size()>0">
                AND tpii.into_status =
                any(#{intoStatusList, typeHandler= StringListTypeHandler})
            </if>
            <if test="brandId != null and brandId != ''">
                AND tmgb.brand_id = #{brandId}::uuid
            </if>
            <if test="skuIdFlag !=null and skuIdFlag == true">
                AND tpii.sku_id is null
            </if>

            <if test="searchText !=null">
                AND  (tpii.sku_model LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                OR   tpii.sku_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>

        UNION ALL

        SELECT
        tpoi.sku_model AS "skuModel",
        tpoi.sku_name AS "skuName",
        tpoi.sku_id AS "skuId",
        tpoi.cost_price AS "costPrice",
        tpoi.cost_amt AS "costAmt",
        tmgs.sku_spec AS "skuSpec",
        tmgs.sku_code AS "skuCode",
        tmgs.brand_id AS "brandId",
        tmgs.sku_images AS "skuImages",
        tmgb.brand_name AS "brandName",
        tmgb.short_name AS "shortName",
        tpoi.item_id AS "itemId",
        NULL AS "intoId",
        tpoi.out_id AS "outId",
        tpoi.inv_id AS "invId",
        tpoi.from_id AS "fromId",
        tpoi.from_item_id AS "fromItemId",
        tpoi.out_qty AS "intoQty"
        FROM
        dkic_b.t_psi_outbound_item tpoi
        LEFT JOIN dkic_b.t_psi_outbound tpo ON tpo.out_id = tpoi.out_id
        LEFT JOIN dkic_b.t_mst_goods_sku tmgs ON tmgs.sku_id = tpoi.sku_id
        LEFT JOIN dkic_b.t_mst_goods_brand tmgb ON tmgb.brand_id = tmgs.brand_id
        <where>
            tpoi.cost_price = 0
            AND tpoi.out_qty != 0
            AND tpoi.sku_model IS NOT NULL
            <if test="outId != null and outId != ''">
                AND tpoi.out_id = #{outId}::uuid
            </if>
            <if test="cpId != null">
                AND tpoi.cp_id = #{cpId}
            </if>
            <if test="intoDateStart != null">
                AND tpo.out_date &gt;= #{intoDateStart}::timestamp with time zone
            </if>
            <if test="intoDateEnd != null">
                AND tpo.out_date &lt; #{intoDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="brandId != null and brandId != ''">
                AND tmgb.brand_id = #{brandId}::uuid
            </if>
            <if test="skuIdFlag !=null and skuIdFlag == true">
                AND tpoi.sku_id is null
            </if>
            <if test="searchText !=null">
                AND  (tpoi.sku_model LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                OR   tpoi.sku_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>
        ) AS A
        GROUP BY
        A."skuModel",
        A."skuName",
        A."skuId",
        A."costPrice",
        A."costAmt",
        A."skuSpec",
        A."skuCode",
        A."brandId",
        A."skuImages",
        A."brandName",
        A."shortName") AS B
    </select>

    <!--查询当前订单下是否存在退货单-->
    <select id="selectHasReturn" resultType="Long">
        SELECT count(1)
        FROM dkic_b.t_psi_inbound_item ti
                 left join dkic_b.t_psi_inbound t on t.into_id = ti.into_id
        where t.flg_valid = #{flgValid}
          AND ti.s_pur_id = #{sPurId,typeHandler=UuidTypeHandler}
          AND ti.into_type = #{intoType}
    </select>

    <!--根据条件进行查询-->
    <select id="selectMessageByOtherCondition" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_inbound_item tpii
        <where>
            <if test="intoId != null and intoId != ''">
                AND tpii.into_id = #{intoId}::uuid
            </if>
            <if test="fromId != null and fromId != ''">
                AND tpii.from_id = #{fromId}::uuid
            </if>
        </where>
    </select>

</mapper>
