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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, freeze_id, item_index, sku_id,sub_unit_id, unit_id,pack_box, box, piece,
          sys.f_remove_zero(freeze_qty) as freeze_qty,  sys.f_remove_zero(unfreeze_qty) as unfreeze_qty,  sys.f_remove_zero(surplus_qty) as surplus_qty, remarks, inv_id, 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>
    <!-- 通用Respons查询列 -->
    <sql id="Base_Column_List_Respons">
        item.item_id, item.freeze_id, item.item_index, item.sku_id,item.box,item.piece,
        sys.f_remove_zero(item.freeze_qty) as freeze_qty, sys.f_remove_zero(item.unfreeze_qty) as unfreeze_qty,
        sys.f_remove_zero(item.surplus_qty) as surplus_qty, item.remarks, item.inv_id, item.flg_valid
        ,item.cp_id, sku.sku_code, sku.sku_name, sku.sku_model,brand.brand_name,brand.short_name,wh.wh_name
        ,item.sub_unit_id,item.unit_id,item.pack_box,dkic_b.f_box_piece(u2.unit_name,ul.unit_name,item.box,item.piece) AS "boxPiece"
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.FreezeItemResponse">
        <id column="item_id" property="itemId"/>
                <result column="freeze_id" property="freezeId" typeHandler="UuidTypeHandler"/>
                <result column="item_index" property="itemIndex"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="unit_id" property="unitId" typeHandler="UuidTypeHandler"/>
                <result column="sub_unit_id" property="subUnitId" typeHandler="UuidTypeHandler"/>
                <result column="pack_box" property="packBox"/>
                <result column="box" property="box"/>
                <result column="piece" property="piece"/>
                <result column="freeze_qty" property="freezeQty"/>
                <result column="unfreeze_qty" property="unfreezeQty"/>
                <result column="surplus_qty" property="surplusQty"/>
                <result column="remarks" property="remarks"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <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="sku_images" property="skuImages" typeHandler="JsonTypeHandler"/>
        <result column="sku_spec" property="skuSpec"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="pack_box" property="packBox"/>
        <result column="decimalPlaces" property="decimalPlaces"/>
        <result column="brand_name" property="brandName"/>
        <result column="boxPiece" property="boxPiece"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.FreezeItem">
        <id column="item_id" property="itemId"/>
        <result column="freeze_id" property="freezeId" typeHandler="UuidTypeHandler"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="box" property="box"/>
        <result column="piece" property="piece"/>
        <result column="unit_id" property="unitId" typeHandler="UuidTypeHandler"/>
        <result column="sub_unit_id" property="subUnitId" typeHandler="UuidTypeHandler"/>
        <result column="pack_box" property="packBox"/>
        <result column="freeze_qty" property="freezeQty"/>
        <result column="unfreeze_qty" property="unfreezeQty"/>
        <result column="surplus_qty" property="surplusQty"/>
        <result column="remarks" property="remarks"/>
        <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
        <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="decimalPlaces" property="decimalPlaces"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="freezeId != null and freezeId != ''">
                AND item.freeze_id = #{freezeId}::uuid
            </if>
            <if test="itemIndex != null">
                AND item.item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND item.sku_id = #{skuId}
            </if>
            <if test="freezeQty != null">
                AND item.freeze_qty = #{freezeQty}
            </if>
            <if test="unfreezeQty != null">
                AND item.unfreeze_qty = #{unfreezeQty}
            </if>
            <if test="surplusQty != null">
                AND item.surplus_qty = #{surplusQty}
            </if>
            <if test="remarks != null and remarks != ''">
                AND item.remarks = #{remarks}
            </if>
            <if test="invId != null and invId != ''">
                AND item.inv_id = #{invId}
            </if>
            <if test="flgValid != null">
                AND item.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND item.cp_id = #{cpId}
            </if>
        </where>
    </sql>

    <!-- 查询表t_psi_freeze_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Respons"/>
        ,sku.sku_images
        ,sku.sku_spec
        ,inv.non_std_code
        ,sku.pack_box
        ,ul.decimal_places    as "decimalPlaces"
        FROM dkic_b.t_psi_freeze_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_mst_unit AS ul ON item.unit_id = ul.unit_id
        LEFT JOIN dkic_b.t_mst_unit AS u2 ON item.sub_unit_id = u2.unit_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_mst_goods_brand AS brand ON brand.brand_id=sku.brand_id
        <include refid="Condition"/>
    </select>

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

   <!-- 根据主键查询表t_psi_freeze_item的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
            item.item_id, item.freeze_id, item.item_index, item.sku_id, item.freeze_qty, item.unfreeze_qty
             , item.surplus_qty, item.remarks, item.inv_id, item.flg_valid,item.cp_id
           ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_freeze_item AS item
        WHERE item.item_id = #{id}::uuid
    </select>


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


</mapper>
