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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        batch_id, inv_id, from_id, from_item_id, wh_id, sku_id, non_std_code,
        sys.f_remove_zero(inv_qty) as inv_qty, cost_price, cost_amt, acc_date, remarks, 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>

    <!-- 通用查询列Response -->
    <sql id="Base_Column_List_Response">
        tpib.batch_id, tpib.inv_id, tpib.from_id, tpib.from_item_id,
        tpib.wh_id, tpib.sku_id, tpib.non_std_code,  sys.f_remove_zero(tpib.inv_qty) as inv_qty,
        tpib.cost_price, tpib.cost_amt, tpib.acc_date, tpib.remarks,
        tpib.flg_valid, tpib.cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.InventoryBatch">
        <id column="batch_id" property="batchId"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
                <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
                <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="non_std_code" property="nonStdCode"/>
                <result column="inv_qty" property="invQty"/>
                <result column="cost_price" property="costPrice"/>
                <result column="cost_amt" property="costAmt"/>
            <result column="acc_date" property="accDate" typeHandler="TimestampTypeHandler"/>
                <result column="remarks" property="remarks"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
            <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
            <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
                <result column="op_app_code" property="opAppCode"/>
            <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
                <result column="op_db_user" property="opDbUser"/>
    </resultMap>
    <!-- 通用查询映射结果Response -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.InventoryBatchResponse">
        <id column="batch_id" property="batchId"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
                <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
                <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="non_std_code" property="nonStdCode"/>
                <result column="inv_qty" property="invQty"/>
                <result column="cost_price" property="costPrice"/>
                <result column="cost_amt" property="costAmt"/>
            <result column="acc_date" property="accDate" typeHandler="TimestampTypeHandler"/>
                <result column="remarks" property="remarks"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
                <result column="sku_code" property="skuCode"/>
                <result column="sku_name" property="skuName"/>
                <result column="sku_model" property="skuModel"/>
                <result column="brand_name" property="brandName"/>
                <result column="short_name" property="shortName"/>
                <result column="wh_name" property="whName"/>
                <result column="from_no" property="fromNo"/>
                <result column="from_item_no" property="fromItemNo"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="invId != null and invId != ''">
                AND inv_id = #{invId}::uuid
            </if>
            <if test="fromId != null and fromId != ''">
                AND from_id = #{fromId}
            </if>
            <if test="fromItemId != null and fromItemId != ''">
                AND tpib.from_item_id = #{fromItemId}
            </if>
            <if test="whId != null and whId != ''">
                AND tpib.wh_id = #{whId}
            </if>
            <if test="skuId != null and skuId != ''">
                AND tpib.sku_id = #{skuId}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND tpib.non_std_code = #{nonStdCode}
            </if>
            <if test="invQty != null">
                AND tpib.inv_qty = #{invQty}
            </if>
            <if test="costPrice != null">
                AND tpib.cost_price = #{costPrice}
            </if>
            <if test="costAmt != null">
                AND tpib.cost_amt = #{costAmt}
            </if>
            <if test="accDate != null">
                AND tpib.acc_date = #{accDate}
            </if>
            <if test="flgValid != null">
                AND tpib.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpib.cp_id = #{cpId}
            </if>
        </where>
    </sql>

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

    <!-- 查询表dkic_b.t_psi_inventory_batch，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tmgs.sku_code
        ,tmgs.sku_model
        ,tmgs.sku_name
        ,tmgs.sku_spec
        ,tmgb.brand_name
        ,tmgb.short_name
        ,tmw.wh_name
        FROM dkic_b.t_psi_inventory_batch tpib
        <include refid="Join_Table"/>
        <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_inventory_batch，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_inventory_batch tpib
        <include refid="Join_Table"/>
        <include refid="Condition"/>
    </select>

    <sql id="Join_Table">
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpib.sku_id
        left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpib.wh_id
        left join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
    </sql>

    <!-- 根据主键查询表dkic_b.t_psi_inventory_batch的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_inventory_batch tpib
        WHERE tpib.batch_id = #{id}::uuid
    </select>

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

    <!-- 根据主键锁定表dkic_b.t_psi_inventory_batch的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_inventory_batch
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into dkic_b.t_psi_inventory_batch
        (
        <trim suffixOverrides=",">
            inv_id,
            from_id,
            from_item_id,
            wh_id,
            sku_id,
            non_std_code,
            inv_qty,
            cost_price,
            cost_amt,
            acc_date,
            remarks,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.invId}::uuid,
                #{item.fromId}::uuid,
                #{item.fromItemId}::uuid,
                #{item.whId}::uuid,
                #{item.skuId}::uuid,
                #{item.nonStdCode},
                #{item.invQty},
                #{item.costPrice},
                #{item.costAmt},
                #{item.accDate},
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>


    <!-- 查询库存批次明细 时间正序 目前用于出库减库存 -->
    <select id="selectAmountAndQuantity" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_inventory_batch tpib
        <where>
            tpib.inv_qty > 0  and tpib.flg_valid
            <if test="invId != null and invId != ''">
                AND tpib.inv_id = #{invId,typeHandler=UuidTypeHandler}
            </if>
        </where>
        order by tpib.op_create_time asc
    </select>

    <!--修改库存数量金额-->
    <update id="updateAmountAndQuantity" parameterType="com.dk.mdm.model.vo.ivt.InventoryBatchVO">
        update dkic_b.t_psi_inventory_batch
        <set>
            <if test="zeroFlag">
                cost_amt = 0,
                inv_qty = 0,
            </if>
            <if test="!zeroFlag">
                cost_amt = cost_amt - #{costAmt,jdbcType=NUMERIC},
                inv_qty = inv_qty - #{invQty,jdbcType=NUMERIC},
            </if>
        </set>
        where batch_id =#{batchId,typeHandler=UuidTypeHandler}
        and inv_qty - #{invQty,jdbcType=NUMERIC}>=0
        and cost_amt - #{costAmt,jdbcType=NUMERIC}>=0
    </update>

    <!--删除数据-->
    <delete id="deleteData" parameterType="com.dk.mdm.model.vo.ivt.InventoryBatchVO">
        DELETE
        FROM dkic_b.t_psi_inventory_batch
        where from_id = #{fromId,typeHandler=UuidTypeHandler}
          and from_item_id = #{fromItemId,typeHandler=UuidTypeHandler}
    </delete>

    <!--根据来源信息查询-->
    <select id="selectByFromCondition" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_inventory_batch tpib
        WHERE tpib.from_id = #{fromId}::uuid
        and   tpib.from_item_id = #{fromItemId}::uuid
    </select>

    <!-- 修改数量金额直接赋值-->
    <update id="updateAmtAndQty" parameterType="com.dk.mdm.model.pojo.ivt.InventoryBatch">
        update dkic_b.t_psi_inventory_batch
        set cost_amt = #{costAmt,jdbcType=NUMERIC},
            inv_qty  = #{invQty,jdbcType=NUMERIC}
            where batch_id = #{batchId,typeHandler=UuidTypeHandler}
    </update>


    <!--入库成本核对查询存货批次明细-->
    <select id="selectCostCondition" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_inventory_batch tpib
        WHERE tpib.from_item_id = #{fromItemId}::uuid
        AND tpib.flg_valid
    </select>

    <!-- 修改存货批次明细(入库成本核对)-->
    <update id="updateInventoryBatchCost" parameterType="com.dk.mdm.model.pojo.ivt.InventoryBatch">
        update dkic_b.t_psi_inventory_batch
        set cost_amt = #{costAmt,jdbcType=NUMERIC},
            cost_price = #{costPrice,jdbcType=NUMERIC}
            where batch_id = #{batchId,typeHandler=UuidTypeHandler}
    </update>


</mapper>
