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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        batch_id, out_item_id, inv_id, sys.f_remove_zero(out_qty) as out_qty, 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
    </sql>
    <!-- 通用查询列Response -->
    <sql id="Base_Column_List_Response">
        tpoic.batch_id,tpoic.out_item_id, tpoic.inv_id, sys.f_remove_zero(tpoic.out_qty) as out_qty, tpoic.cost_price, tpoic.cost_amt,
        tpoic.flg_valid, tpoic.cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.OutboundItemCost">
        <id column="batch_id" property="batchId"/>
                <result column="out_item_id" property="outItemId" typeHandler="UuidTypeHandler"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="out_qty" property="outQty"/>
                <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"/>
    </resultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.OutboundItemCostResponse">
        <id column="batch_id" property="batchId"/>
                <result column="out_item_id" property="outItemId" typeHandler="UuidTypeHandler"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="out_qty" property="outQty"/>
                <result column="cost_price" property="costPrice"/>
                <result column="cost_amt" property="costAmt"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="outItemId != null and outItemId != ''">
                AND tpoic.out_item_id = #{outItemId}::uuid
            </if>
            <if test="invId != null and invId != ''">
                AND tpoic.inv_id = #{invId}
            </if>
            <if test="outQty != null">
                AND tpoic.out_qty = #{outQty}
            </if>
            <if test="costPrice != null">
                AND tpoic.cost_price = #{costPrice}
            </if>
            <if test="costAmt != null">
                AND tpoic.cost_amt = #{costAmt}
            </if>
            <if test="flgValid != null">
                AND tpoic.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpoic.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_outbound_item_cost，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_outbound_item_cost tpoic
        <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_outbound_item_cost，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_outbound_item_cost tpoic
        <include refid="Condition"/>
    </select>

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

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_psi_outbound_item_cost
        (
        <trim suffixOverrides=",">
            out_item_id,
            inv_id,
            out_qty,
            cost_price,
            cost_amt,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.outItemId}::uuid,
                #{item.invId}::uuid,
                #{item.outQty},
                #{item.costPrice},
                #{item.costAmt},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>


    <!--删除数据-->
    <delete id="deleteData" >
        DELETE
        FROM dkic_b.t_psi_outbound_item_cost
        where out_item_id = #{id,typeHandler=UuidTypeHandler}
    </delete>


    <!-- 入库成本核对查询出库成本 -->
    <select id="selectCostOutbound" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_outbound_item_cost tpoic
        where tpoic.batch_id = #{batchId}::uuid
        AND tpoic.flg_valid
    </select>

    <!-- 修改出库成本(入库成本核对)-->
    <update id="updateOutboundCost" parameterType="com.dk.mdm.model.pojo.ivt.OutboundItemCost">
        update dkic_b.t_psi_outbound_item_cost
        set cost_amt = #{costAmt,jdbcType=NUMERIC},
            cost_price = #{costPrice,jdbcType=NUMERIC}
            where batch_id = #{batchId,typeHandler=UuidTypeHandler}
    </update>

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


</mapper>
