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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id
        , out_id, out_type, from_id, from_item_id, item_index, sku_id, price_out, non_std_code,
        out_status, sys.f_remove_zero(outing_qty) as outing_qty, outing_amt,
sys.f_remove_zero(out_qty) as out_qty, out_amt, sys.f_remove_zero(return_qty) as return_qty, return_amt, remarks,
        inv_id, cost_price, cost_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,
        s_out_id,s_out_item_id,s_order_id,s_order_item_id,outing_box,outing_piece,out_box,out_piece,
        unit_id,sub_unit_id, pack_box,sku_name,sku_model,sup_id
    </sql>

    <sql id="Base_Column_List_Response">
        tpoi.item_id, tpoi.out_id, tpoi.out_type, tpoi.from_id, tpoi.from_item_id, tpoi.item_index,
        tpoi.sku_id, tpoi.price_out, tpoi.non_std_code, tpoi.out_status, sys.f_remove_zero(tpoi.outing_qty) as outing_qty, tpoi.outing_amt,
        sys.f_remove_zero(tpoi.out_qty) as out_qty, tpoi.out_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, tpoi.flg_valid, tpoi.cp_id,
        tpoi.s_out_id,tpoi.s_out_item_id,tpoi.s_order_id,tpoi.s_order_item_id,
        tpoi.outing_box,tpoi.outing_piece,tpoi.out_box,tpoi.out_piece,
        tpoi.unit_id,tpoi.sub_unit_id,tpoi.pack_box,sku_name,sku_model,sup_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.OutboundItem">
        <id column="item_id" property="itemId"/>
        <result column="out_id" property="outId" typeHandler="UuidTypeHandler"/>
        <result column="out_type" property="outType"/>
        <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_out" property="priceOut"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="out_status" property="outStatus"/>
        <result column="outing_qty" property="outingQty"/>
        <result column="outing_amt" property="outingAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="out_amt" property="outAmt"/>
        <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="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_out_id" property="sOutId" typeHandler="UuidTypeHandler"/>
        <result column="s_out_item_id" property="sOutItemId" typeHandler="UuidTypeHandler"/>
        <result column="s_order_id" property="sOrderId" typeHandler="UuidTypeHandler"/>
        <result column="s_order_item_id" property="sOrderItemId" typeHandler="UuidTypeHandler"/>
        <result column="outing_box" property="outingBox"/>
        <result column="outing_piece" property="outingPiece"/>
        <result column="out_box" property="outBox"/>
        <result column="out_piece" property="outPiece"/>
        <result column="unit_id" property="unitId"/>
        <result column="sub_unit_id" property="subUnitId"/>
        <result column="pack_box" property="packBox"/>
        <result column="sku_name" property="skuName"/>
        <result column="sku_model" property="skuModel"/>
        <result column="decimalPlaces" property="decimalPlaces"/>
        <result column="sup_id" property="supId"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.OutboundItemResponse">
        <id column="item_id" property="itemId"/>
        <result column="out_id" property="outId" typeHandler="UuidTypeHandler"/>
        <result column="out_type" property="outType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="fromNo" property="fromNo" />
        <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_out" property="priceOut"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="out_status" property="outStatus"/>
        <result column="outing_qty" property="outingQty"/>
        <result column="outing_amt" property="outingAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="out_amt" property="outAmt"/>
        <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="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="s_out_id" property="sOutId" typeHandler="UuidTypeHandler"/>
        <result column="s_out_item_id" property="sOutItemId" typeHandler="UuidTypeHandler"/>
        <result column="s_order_id" property="sOrderId" typeHandler="UuidTypeHandler"/>
        <result column="s_order_item_id" property="sOrderItemId" typeHandler="UuidTypeHandler"/>
        <result column="skuCode" property="skuCode"/>
        <result column="sku_images" property="skuImages" typeHandler="JsonTypeHandler"/>
        <result column="skuModel" property="skuModel"/>
        <result column="skuName" property="skuName"/>
        <result column="priceStandard" property="priceStandard"/>
        <result column="brandName" property="brandName"/>
        <result column="brand_name" property="brandName"/>
        <result column="shortName" property="shortName"/>
        <result column="short_name" property="shortName"/>
        <result column="orderQty" property="orderQty"/>
        <result column="canOutingQty" property="canOutingQty"/>
        <result column="whId" property="whId"/>
        <result column="wh_id" property="whId"/>
        <result column="invQty" property="invQty"/>
        <result column="usableQty" property="usableQty"/>
        <result column="whName" property="whName"/>
        <result column="outing_box" property="outingBox"/>
        <result column="outing_piece" property="outingPiece"/>
        <result column="out_box" property="outBox"/>
        <result column="out_piece" property="outPiece"/>
        <result column="unit_id" property="unitId"/>
        <result column="unit_code" property="unitCode"/>
        <result column="sub_unit_id" property="subUnitId"/>
        <result column="sub_unit_code" property="subUnitCode"/>
        <result column="pack_box" property="packBox"/>
        <result column="flgSubUnit" property="flgSubUnit"/>
        <result column="subUnitName" property="subUnitName"/>
        <result column="unitName" property="unitName"/>
        <result column="use_place" property="usePlace"/>
        <result column="decimalPlaces" property="decimalPlaces"/>
        <result column="sup_id" property="supId"/>
        <result column="sup_name" property="supName"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="outId != null and outId != ''">
                AND tpoi.out_id = #{outId}::uuid
            </if>
            <if test="outType != null and outType != ''">
                AND tpoi.out_type = #{outType}
            </if>
            <if test="fromId != null and fromId != ''">
                AND tpoi.from_id = #{fromId}
            </if>
            <if test="fromItemId != null and fromItemId != ''">
                AND tpoi.from_item_id = #{fromItemId}
            </if>
            <if test="itemIndex != null">
                AND tpoi.item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND tpoi.sku_id = #{skuId}
            </if>
            <if test="priceOut != null">
                AND tpoi.price_out = #{priceOut}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND tpoi.non_std_code = #{nonStdCode}
            </if>
            <if test="outStatus != null and outStatus != ''">
                AND tpoi.out_status = #{outStatus}
            </if>
            <if test="outingQty != null">
                AND tpoi.outing_qty = #{outingQty}
            </if>
            <if test="outingAmt != null">
                AND tpoi.outing_amt = #{outingAmt}
            </if>
            <if test="outQty != null">
                AND tpoi.out_qty = #{outQty}
            </if>
            <if test="outAmt != null">
                AND tpoi.out_amt = #{outAmt}
            </if>
            <if test="returnQty != null">
                AND tpoi.return_qty = #{returnQty}
            </if>
            <if test="returnAmt != null">
                AND tpoi.return_amt = #{returnAmt}
            </if>
            <if test="remarks != null and remarks != ''">
                AND tpoi.remarks = #{remarks}
            </if>
            <if test="invId != null and invId != ''">
                AND tpoi.inv_id = #{invId}
            </if>
            <if test="costPrice != null">
                AND tpoi.cost_price = #{costPrice}
            </if>
            <if test="costAmt != null">
                AND tpoi.cost_amt = #{costAmt}
            </if>
            <if test="flgValid != null">
                AND tpoi.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpoi.cp_id = #{cpId}
            </if>
        </where>
    </sql>

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

    <!-- 查询表t_psi_outbound_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT tpoi.item_id,
        tpoi.out_id,
        tpoi.out_type,
        tpoi.from_id,
        tpoi.from_item_id,
        tpoi.item_index,
        tpoi.sku_id,
        tmgs.sku_code as "skuCode",
        case when tpoi.sku_id is null then tpoi.sku_model  else  tmgs.sku_model end   as "skuModel",
        case when tpoi.sku_id is null then tpoi.sku_name  else  tmgs.sku_name end   as "skuName",
        tpoi.price_out,
        tpoi.non_std_code,
        tpoi.out_status,
        sys.f_remove_zero(tpoi.outing_qty) as outing_qty,
        tpoi.outing_box,
        tpoi.outing_piece,
        dkic_b.f_box_piece(u2.unit_name,ul.unit_name,tpoi.outing_box,tpoi.outing_piece) AS "outingBoxPiece",
        tpoi.outing_amt,
        sys.f_remove_zero(tpoi.out_qty) as out_qty,
        tpoi.out_box,
        tpoi.out_piece,
        dkic_b.f_box_piece(u2.unit_name,ul.unit_name,tpoi.out_box,tpoi.out_piece) AS "outBoxPiece",
        tpoi.out_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,
        tpoi.flg_valid,
        tpoi.cp_id,
        tpi.wh_id as "whId",
        sys.f_remove_zero(tpi.inv_qty) as    "invQty",
        sys.f_remove_zero(tpi.usable_qty) as    "usableQty",
        tmw.wh_name as "whName",
        tmgb.brand_name  as "brandName",
        tmgb.short_name  as "shortName",
        tmgs.sku_spec  as "skuSpec",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) as "outStatusName",
        ul.decimal_places    as "decimalPlaces",
        tpoi.sup_id,
        tms.sup_name,
        tpoi.unit_id,
        tpoi.sub_unit_id,
        ul.unit_name as "unitName",
        u2.unit_name as "subUnitName"
        FROM dkic_b.t_psi_outbound_item as tpoi
        LEFT JOIN dkic_b.t_mst_supplier as tms ON tpoi.sup_id = tms.sup_id
        LEFT JOIN dkic_b.t_mst_unit AS ul ON tpoi.unit_id = ul.unit_id
        LEFT JOIN dkic_b.t_mst_unit AS u2 ON tpoi.sub_unit_id = u2.unit_id
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpoi.sku_id
        left join dkic_b.t_psi_inventory as tpi on tpi.inv_id = tpoi.inv_id
        left join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id
        left join sys.t_data_kind as tdk on tdk.kind_code = tpoi.out_status
        left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

    <!-- 根据主键查询表t_psi_outbound_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 tpoi.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_outbound_item tpoi
        WHERE tpoi.item_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_psi_outbound_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_outbound_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_outbound_item
        WHERE item_id = #{id}::uuid
        for update
    </select>

    <!-- 根据主键锁定表t_psi_outbound_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_outbound_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_outbound_item
        <include refid="idsForeach"/>
        for update
    </select>

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

    <insert id="insertBatch">
        insert into dkic_b.t_psi_outbound_item
        (
        <trim suffixOverrides=",">
            out_id,
            out_type,
            from_id,
            from_item_id,
            item_index,
            sku_id,
            sku_name,
            sku_model,
            price_out,
            non_std_code,
            out_status,
            outing_qty,
            outing_amt,
            out_qty,
            out_amt,
            return_qty,
            return_amt,
            remarks,
            inv_id,
            cost_price,
            cost_amt,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.outId}::uuid,
                #{item.outType},
                #{item.fromId}::uuid,
                #{item.fromItemId}::uuid,
                #{item.itemIndex},
                #{item.skuId}::uuid,
                #{item.skuName},
                #{item.skuModel},
                #{item.priceOut},
                #{item.nonStdCode},
                #{item.outStatus},
                #{item.outingQty},
                #{item.outingAmt},
                #{item.outQty},
                #{item.outAmt},
                #{item.returnQty},
                #{item.returnAmt},
                #{item.remarks},
                #{item.invId}::uuid,
                #{item.costPrice},
                #{item.costAmt},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

    <!-- 根据条件进行查询（出库编辑用） -->
    <select id="selectByCondForOutEdit" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.out_id,
               t.from_item_id,
               t.from_id,
               tpo.order_no AS  "fromNo",
               t.item_index,
               t.sku_id,
               tmgs.sku_images,
               tmgs.flg_sub_unit AS "flgSubUnit",
               t.outing_box ,
               t.outing_piece ,
               tmgs.price_standard AS "priceStandard",
               t.pack_box  ,
                tmgus.unit_name         as "subUnitName",
                tmgu.unit_name         as "unitName",
               case when t.sku_id is null then t.sku_model  else  tmgs.sku_model end   as "skuModel",
               case when t.sku_id is null then t.sku_name  else  tmgs.sku_name end   as "skuName",
               tmgs.sku_code            as "skuCode",
               sys.f_remove_zero(tpoi.item_qty)    as "orderQty",
               sys.f_remove_zero(tpoi.item_qty - tpoi.outing_qty - tpoi.out_qty + t.outing_qty + t.out_qty) as "canOutingQty",
               tmgb.brand_name AS "brandName",
               tmgb.short_name AS "shortName",
               t.price_out,
               sys.f_remove_zero(t.outing_qty + t.out_qty)                                                  as outing_qty,
               t.outing_amt + t.out_amt                                                  as outing_amt,
               sys.f_remove_zero(t.out_qty) as out_qty,
               t.out_amt,
               t.non_std_code,
               t.remarks,
               t.inv_id,
               tpi.wh_id                                                                 as "whId",
               sys.f_remove_zero(tpi.inv_qty)                                                               as "invQty",
               sys.f_remove_zero(tpi.usable_qty)                                                            as "usableQty",
               tmw.wh_name                                                               as "whName",
               t.use_place,
              tmgu.decimal_places    as "decimalPlaces",
               t.sup_id,
               tms.sup_name
        FROM dkic_b.t_psi_outbound_item as t
                 left join dkic_b.t_mst_supplier as tms on t.sup_id = tms.sup_id
                 left join dkic_b.t_psi_order_item as tpoi on tpoi.item_id = t.from_item_id
                 left join dkic_b.t_psi_order as tpo on tpo.order_id = t.from_id
                 left join dkic_b.t_mst_goods_sku tmgs on t.sku_id = tmgs.sku_id
                 left join dkic_b.t_mst_unit as tmgu on t.unit_id = tmgu.unit_id
                 left join dkic_b.t_mst_unit as tmgus on t.sub_unit_id = tmgus.unit_id
                 left join dkic_b.t_psi_inventory as tpi on tpi.inv_id = t.inv_id
                 left join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id
                 left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
        where t.flg_valid
          and t.out_id = #{id}::uuid
        order by t.item_index
    </select>

    <!-- 根据条件进行查询（编辑用，适用于直接新建的出库单） -->
    <select id="selectByCondForOutEditTogether" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.out_id,
               t.from_item_id,
               t.from_id,
               t.item_index,
               t.sku_id,
               tmgs.sku_code            as "skuCode",
               case when t.sku_id is null then t.sku_model  else  tmgs.sku_model end   as "skuModel",
               case when t.sku_id is null then t.sku_name  else  tmgs.sku_name end   as "skuName",
               t.pack_box,
               t.price_out,
               sys.f_remove_zero(t.outing_qty) as outing_qty,
               t.outing_box,
               t.outing_piece,
               t.outing_amt,
               sys.f_remove_zero(t.out_qty) as out_qty,
               t.out_amt,
               t.non_std_code,
               t.remarks,
               t.inv_id,
               tpi.wh_id                as "whId",
               sys.f_remove_zero(tpi.inv_qty)             as "invQty",
               sys.f_remove_zero(tpi.usable_qty)           as "usableQty",
               tmw.wh_name              as "whName"
                ,tmgu.decimal_places    as "decimalPlaces"
               ,t.unit_id
               ,t.sub_unit_id
               ,ul.unit_name as "unitName"
               ,u2.unit_name as "subUnitName"
               ,dkic_b.f_box_piece(u2.unit_name,ul.unit_name,t.outing_box,t.outing_piece) AS "outingBoxPiece"
               ,dkic_b.f_box_piece(u2.unit_name,ul.unit_name,t.out_box,t.out_piece) AS "outBoxPiece"
                ,sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) as "outStatusName"
                ,t.out_status
        FROM dkic_b.t_psi_outbound_item as t
                 left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = t.sku_id
                 left join dkic_b.t_mst_unit as tmgu on t.unit_id = tmgu.unit_id
                 left join dkic_b.t_psi_inventory as tpi on tpi.inv_id = t.inv_id
                 left join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id
                 LEFT JOIN dkic_b.t_mst_unit AS ul ON t.unit_id = ul.unit_id
                 LEFT JOIN dkic_b.t_mst_unit AS u2 ON t.sub_unit_id = u2.unit_id
                 left join sys.t_data_kind as tdk on tdk.kind_code = t.out_status
        where t.flg_valid
          and t.out_id = #{id}::uuid
        order by t.item_index
    </select>

    <!-- 获取出库信息（新建退货用） -->
    <select id="selectByCondForReturn" resultMap="BaseResultMapResponse">
        SELECT t.out_id                                   as s_out_id,
               t.item_id                                  as s_out_item_id,
               t.sku_id,
               tmgs.sku_code                              as "skuCode",
               case when t.sku_id is null then t.sku_model  else  tmgs.sku_model end   as "skuModel",
               case when t.sku_id is null then t.sku_name  else  tmgs.sku_name end   as "skuName",
               sys.f_remove_zero(t.out_qty)                                  as "outboundOutQty",
               sys.f_remove_zero(t.out_qty - t.return_qty)                   as "canReturnQty",
               - sys.f_remove_zero(t.out_qty - t.return_qty)               as outing_qty,
               t.price_out,
               - t.price_out * (t.out_qty - t.return_qty) as outing_amt,
               0                                          as out_qty,
               0                                          as out_amt,
               t.inv_id,
               t.non_std_code,
               t.remarks
                ,tmgu.decimal_places    as "decimalPlaces"
        FROM dkic_b.t_psi_outbound_item as t
                 inner join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = t.sku_id
                 left join dkic_b.t_mst_unit as tmgu on t.unit_id = tmgu.unit_id
        where t.flg_valid
          and t.out_type = '出库类型-销售出库'
          and t.out_qty > t.return_qty
          and t.out_id = #{outId}::uuid
        order by t.item_index
    </select>

    <!-- 根据条件进行查询（退货编辑用） -->
    <select id="selectByCondForReturnEdit" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.out_id,
               t.s_out_id,
               t.s_out_item_id,
               t.s_order_id,
               t.s_order_item_id,
               t.item_index,
               t.sku_id,
               tmgs.sku_images,
               tmgb.brand_name,
               tmgb.short_name,
               tmgs.sku_code                                                                         as "skuCode",
               case when t.sku_id is null then t.sku_model  else  tmgs.sku_model end                 as "skuModel",
               case when t.sku_id is null then t.sku_name  else  tmgs.sku_name end                   as "skuName",
               sys.f_remove_zero(COALESCE(tpobi.out_qty, 0))                                                            as "outboundOutQty",
               sys.f_remove_zero(COALESCE(tpobi.out_qty, 0) - COALESCE(tpobi.return_qty, 0) + t.outing_qty + t.out_qty) as "canReturnQty",
               t.inv_id,
               tpi.wh_id,
               sys.f_remove_zero(tpi.inv_qty)                                                                           as "invQty",
               sys.f_remove_zero(tpi.usable_qty)                                                                        as "usableQty",
               tmw.wh_name                                                                           as "whName",
               t.price_out,
               sys.f_remove_zero(t.outing_qty + t.out_qty)                                                              as outing_qty,
               t.outing_amt + t.out_amt                                                              as outing_amt,
               t.non_std_code,
               t.remarks
                ,tmgu.decimal_places    as "decimalPlaces"
                ,tmgus.unit_name         as "subUnitName"
                ,tmgu.unit_name         as "unitName"
                ,t.pack_box
                ,tmgs.flg_sub_unit       as "flgSubUnit"
                ,t.outing_box
                ,t.outing_piece
                ,t.out_box
                ,t.out_piece
                ,t.out_status
        FROM dkic_b.t_psi_outbound_item as t
                 left join dkic_b.t_mst_unit as tmgu on t.unit_id = tmgu.unit_id
                 left join dkic_b.t_mst_unit as tmgus on t.sub_unit_id = tmgus.unit_id
                 left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = t.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 = t.inv_id
                 left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
                 left join dkic_b.t_psi_outbound_item as tpobi on tpobi.item_id = t.s_out_item_id
        where t.flg_valid
          and t.out_id = #{id}::uuid
        order by t.item_index
    </select>

    <!-- 根据条件进行查询（退货编辑用） -->
    <select id="selectByCondForReturnEditWx" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.out_id,
               t.s_out_id,
               t.s_out_item_id,
               t.s_order_id,
               t.s_order_item_id,
               t.item_index,
               t.sku_id,
               tmgs.sku_images,
               tmgb.brand_name,
               tmgb.short_name,
               tmgs.sku_code                                                                         as "skuCode",
               case when t.sku_id is null then t.sku_model  else  tmgs.sku_model end                 as "skuModel",
               case when t.sku_id is null then t.sku_name  else  tmgs.sku_name end                   as "skuName",
               sys.f_remove_zero(COALESCE(tpobi.out_qty, 0))                                                            as "outboundOutQty",
               sys.f_remove_zero(t.out_qty - t.return_qty)                   as "canReturnQty",
               t.inv_id,
               tpi.wh_id,
               sys.f_remove_zero(tpi.inv_qty)                                                                           as "invQty",
               sys.f_remove_zero(tpi.usable_qty)                                                                        as "usableQty",
               tmw.wh_name                                                                           as "whName",
               t.price_out,
               sys.f_remove_zero(t.outing_qty + t.out_qty)                                                              as outing_qty,
               t.outing_amt + t.out_amt                                                              as outing_amt,

               sys.f_remove_zero(t.out_qty) as out_qty,
               t.out_amt,
            t.non_std_code,
               t.remarks
                ,tmgu.decimal_places    as "decimalPlaces"
                ,tmgus.unit_name         as "subUnitName"
                ,tmgu.unit_name         as "unitName"
                ,t.pack_box
                ,tmgs.flg_sub_unit       as "flgSubUnit"
                ,t.outing_box
                ,t.outing_piece
                ,t.out_box
                ,t.out_piece
        ,t.out_status
        FROM dkic_b.t_psi_outbound_item as t
                 left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = t.sku_id
                 left join dkic_b.t_mst_unit as tmgu on t.unit_id = tmgu.unit_id
                 left join dkic_b.t_mst_unit as tmgus on t.sub_unit_id = tmgus.unit_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 = t.inv_id
                 left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
                 left join dkic_b.t_psi_outbound_item as tpobi on tpobi.item_id = t.s_out_item_id
        where t.flg_valid
          and t.out_id = #{id}::uuid
        order by t.item_index
    </select>

    <!--修改退货信息 -->
    <update id="updateReturnMessage" parameterType="com.dk.mdm.model.pojo.ivt.OutboundItem">
        update dkic_b.t_psi_outbound_item
        <set>
            <if test=" returnQty != null">
                return_qty = return_qty + #{returnQty,jdbcType=NUMERIC},
            </if>
            <if test=" returnAmt != null">
                return_amt = return_amt + #{returnAmt,jdbcType=NUMERIC},
            </if>
        </set>
        where item_id = #{itemId,typeHandler=UuidTypeHandler}
        and out_qty >= return_qty
    </update>


    <!-- 出库办理显示字段 -->
    <sql id="Base_Column_List_Response_Out">
        tpoi.item_id ,
        tpoi.out_id ,
        tpoi.out_type ,
        tpoi.from_id,
        tpoi.from_item_id,
        tpoi.item_index,
        tpoi.sku_id,
        tpoi.price_out,
        tpoi.non_std_code,
        tpoi.out_status ,
        sys.f_remove_zero(tpoi.outing_qty) as  outing_qty,
        tpoi.outing_amt ,
        sys.f_remove_zero(tpoi.out_qty) as out_qty ,
        tpoi.out_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,
        tpoi.outing_box,
        tpoi.outing_piece,
        tpoi.out_box,
        tpoi.out_piece,
        tpoi.pack_box
    </sql>
    <sql id="Base_Column_List_Response_Into">
        tpii.item_id ,
        tpii.into_id as out_id,
        tpii.into_type as out_type,
        tpii.from_id,
        tpii.from_item_id,
        tpii.item_index,
        tpii.sku_id,
        tpii.price_into as price_out,
        tpii.non_std_code,
        tpii.into_status,
        sys.f_remove_zero(tpii.intoing_qty) as outing_qty ,
        tpii.intoing_amt as outing_amt ,
        sys.f_remove_zero(tpii.into_qty) as out_qty ,
        tpii.into_amt as out_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.intoing_box as outing_box,
        tpii.intoing_piece as outing_piece,
        tpii.into_box as out_box,
        tpii.into_piece as out_piece,
        tpii.pack_box
    </sql>
    <!--条件查询 web端出库办理用-->
    <select id="selectOutboundItem" resultMap="BaseResultMapResponse">
        select
        <include refid="Base_Column_List_Response_Out"/>
        ,tmgs.sku_code as "skuCode"
        ,case when tpoi.sku_id is null then tpoi.sku_model  else  tmgs.sku_model end   as "skuModel"
        ,case when tpoi.sku_id is null then tpoi.sku_name  else  tmgs.sku_name end   as "skuName"
        ,tmgs.sku_spec as "skuSpec"
        ,tmgb.brand_name as "brandName"
        ,tmgb.short_name as "shortName"
        ,tmu.unit_code as "unitCode"
        ,tmu.unit_name as "unitName"
        ,tmu2.unit_code as "subUnitCode"
        ,tmu2.unit_name as "subUnitName"
        ,dkic_b.f_box_piece(tmu2.unit_name,tmu.unit_name,tpoi.outing_box,tpoi.outing_piece) as "outingBoxPiece"
        ,tmu.decimal_places    as "decimalPlaces"
        from dkic_b.t_psi_outbound_item tpoi
        <include refid="Select_Inbound_Join_Table_Out"/>
        <where>
            tpoi.flg_valid
            <if test="outId != null and outId != ''">
                AND tpoi.out_id = #{outId}::uuid
            </if>
        </where>
        union all
        SELECT
        <include refid="Base_Column_List_Response_Into"/>
        ,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"
        ,tmu.unit_code as "unitCode"
        ,tmu.unit_name as "unitName"
        ,tmu2.unit_code as "subUnitCode"
        ,tmu2.unit_name as "subUnitName"
        ,dkic_b.f_box_piece(tmu2.unit_name,tmu.unit_name,tpoi.out_box,tpoi.out_piece) as "outBoxPiece"
        ,tmu.decimal_places    as "decimalPlaces"
        FROM dkic_b.t_psi_inbound_item tpii
        <include refid="Select_Inbound_Join_Table_Into"/>
        <where>
            tpii.flg_valid
            <if test="outId != null and outId != ''">
                AND tpii.into_id = #{outId}::uuid
            </if>
        </where>
    </select>
    <!--出库办理显示出库关联表-->
    <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>
    <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_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>

    <!--修改出库明细成本单价 金额 -->
    <update id="updateOutCost" parameterType="com.dk.mdm.model.pojo.ivt.OutboundItem">
        update dkic_b.t_psi_outbound_item
        <set>
            <if test=" costPrice != null">
                cost_price = #{costPrice,jdbcType=NUMERIC},
            </if>
            <if test=" costAmt != null">
                cost_amt = #{costAmt,jdbcType=NUMERIC},
            </if>
        </set>
        where item_id = #{itemId,typeHandler=UuidTypeHandler}
    </update>

    <!-- 修改最后一条出库成本(入库成本核对)-->
    <update id="updateLastCost" parameterType="com.dk.mdm.model.pojo.ivt.OutboundItem">
        update dkic_b.t_psi_outbound_item
        set cost_amt += #{costAmt,jdbcType=NUMERIC}
        where item_id = #{itemId,typeHandler=UuidTypeHandler}
    </update>


    <!-- 根据商品顺序查询表t_psi_outbound_item的一行数据 -->
    <select id="selectItemIndex" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_outbound_item tpoi
        WHERE tpoi.item_index = #{itemIndex}
             and tpoi.out_id=#{id}::uuid
    </select>



</mapper>
