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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, return_id, into_id, into_item_id, pur_id, pur_item_id, item_index,
        sku_id, item_qty, price_std, amt_std, price_return, item_amt, non_std_code,
        out_status, outoing_qty, outoing_amt, out_qty, out_amt, 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,wh_id
    </sql>


    <sql id="Base_Column_List_Join">
            tpiri.item_id, tpiri.return_id, tpiri.into_id, tpiri.into_item_id, tpiri.pur_id, tpiri.pur_item_id, tpiri.item_index, tpiri.sku_id, tpiri.item_qty,
                     tpiri.price_std,tpiri.amt_std, tpiri.price_return,
               tpiri.item_amt, tpiri.non_std_code, tpiri.out_status, tpiri.outing_qty, tpiri.outing_amt, tpiri.out_qty, tpiri.out_amt,
                     tpiri.remarks, tpiri.flg_valid, tpiri.cp_id
    </sql>



    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.IntoReturnItem">
        <id column="item_id" property="itemId"/>
                <result column="return_id" property="returnId" typeHandler="UuidTypeHandler"/>
                <result column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
                <result column="into_item_id" property="intoItemId" typeHandler="UuidTypeHandler"/>
                <result column="pur_id" property="purId" typeHandler="UuidTypeHandler"/>
                <result column="pur_item_id" property="purItemId" typeHandler="UuidTypeHandler"/>
                <result column="item_index" property="itemIndex"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="item_qty" property="itemQty"/>
                <result column="price_std" property="priceStd"/>
                <result column="amt_std" property="amtStd"/>
                <result column="price_return" property="priceReturn"/>
                <result column="item_amt" property="itemAmt"/>
                <result column="non_std_code" property="nonStdCode"/>
                <result column="out_status" property="outStatus"/>
                <result column="outoing_qty" property="outingQty"/>
                <result column="outoing_amt" property="outingAmt"/>
                <result column="out_qty" property="outQty"/>
                <result column="out_amt" property="outAmt"/>

                <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"/>
        <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
    </resultMap>

    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.IntoReturnItemResponse">
        <id column="item_id" property="itemId"/>
        <result column="return_id" property="returnId" typeHandler="UuidTypeHandler"/>
        <result column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_item_id" property="intoItemId" typeHandler="UuidTypeHandler"/>
        <result column="pur_id" property="purId" typeHandler="UuidTypeHandler"/>
        <result column="pur_item_id" property="purItemId" typeHandler="UuidTypeHandler"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="item_qty" property="itemQty"/>
        <result column="price_std" property="priceStd"/>
        <result column="amt_std" property="amtStd"/>
        <result column="price_return" property="priceReturn"/>
        <result column="item_amt" property="itemAmt"/>
        <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="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="sku_images" property="skuImages"/>
        <result column="reject_qty" property="rejectQty"/>
        <result column="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
    </resultMap>


    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="returnId != null and returnId != ''">
                AND return_id = #{returnId}
            </if>
            <if test="intoId != null and intoId != ''">
                AND into_id = #{intoId}
            </if>
            <if test="intoItemId != null and intoItemId != ''">
                AND into_item_id = #{intoItemId}
            </if>
            <if test="purId != null and purId != ''">
                AND pur_id = #{purId}
            </if>
            <if test="purItemId != null and purItemId != ''">
                AND pur_item_id = #{purItemId}
            </if>
            <if test="itemIndex != null">
                AND item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND sku_id = #{skuId}
            </if>
            <if test="itemQty != null">
                AND item_qty = #{itemQty}
            </if>
            <if test="priceStd != null">
                AND price_std = #{priceStd}
            </if>
            <if test="amtStd != null">
                AND amt_std = #{amtStd}
            </if>
            <if test="priceReturn != null">
                AND price_return = #{priceReturn}
            </if>
            <if test="itemAmt != null">
                AND item_amt = #{itemAmt}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND non_std_code = #{nonStdCode}
            </if>
            <if test="outStatus != null and outStatus != ''">
                AND out_status = #{outStatus}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
    </sql>

    <sql id="Condition_Join">
        <where>
            <if test="returnId != null and returnId != ''">
                AND tpiri.return_id = #{returnId}::uuid
            </if>
            <if test="intoId != null and intoId != ''">
                AND tpiri.into_id = #{intoId}::uuid
            </if>
            <if test="intoItemId != null and intoItemId != ''">
                AND tpiri.into_item_id = #{intoItemId}::uuid
            </if>
            <if test="purId != null and purId != ''">
                AND tpiri.pur_id = #{purId}::uuid
            </if>
            <if test="purItemId != null and purItemId != ''">
                AND tpiri.pur_item_id = #{purItemId}::uuid
            </if>
            <if test="itemIndex != null">
                AND tpiri.item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND tpiri.sku_id = #{skuId}
            </if>
            <if test="itemQty != null">
                AND tpiri.item_qty = #{itemQty}
            </if>
            <if test="priceStd != null">
                AND tpiri.price_std = #{priceStd}
            </if>
            <if test="amtStd != null">
                AND tpiri.amt_std = #{amtStd}
            </if>
            <if test="priceReturn != null">
                AND tpiri.price_return = #{priceReturn}
            </if>
            <if test="itemAmt != null">
                AND tpiri.item_amt = #{itemAmt}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND tpiri.non_std_code = #{nonStdCode}
            </if>
            <if test="outStatus != null and outStatus != ''">
                AND tpiri.out_status = #{outStatus}
            </if>
            <if test="remarks != null and remarks != ''">
                AND tpiri.remarks = #{remarks}
            </if>
            <if test="flgValid != null">
                AND tpiri.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpiri.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_into_return_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Join"/>,
        tmgs.sku_code AS sku_code,
        tmgs.sku_name AS sku_name,
        tmgs.sku_model AS sku_model,
        tmgs.sku_images AS sku_images,
        tpii.into_qty as "intoQty",
        tpii.into_amt as "intoAmt",
        tpii.return_qty as "returnQty",
        tpii.return_amt as "returnAmt",
        (tpii.into_qty - tpii.return_qty) AS reject_qty
        FROM dkic_b.t_psi_into_return_item  tpiri
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpiri.sku_id
        left join dkic_b.t_psi_inbound_item tpii on tpiri.into_item_id = tpii.item_id
        <include refid="Condition_Join"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_psi_into_return_item，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM t_psi_into_return_item tpiri
        <include refid="Condition_Join"/>
    </select>

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

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

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

    <insert id="insertBatch">
        insert into t_psi_into_return_item
        (
        <trim suffixOverrides=",">
            return_id,
            into_id,
            into_item_id,
            pur_id,
            pur_item_id,
            item_index,
            sku_id,
            item_qty,
            price_std,
            amt_std,
            price_return,
            item_amt,
            non_std_code,
            out_status,
            outoing_qty,
            outoing_amt,
            out_qty,
            out_amt,

            remarks,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.returnId}::uuid,
                #{item.intoId}::uuid,
                #{item.intoItemId}::uuid,
                #{item.purId}::uuid,
                #{item.purItemId}::uuid,
                #{item.itemIndex},
                #{item.skuId}::uuid,
                #{item.itemQty},
                #{item.priceStd},
                #{item.amtStd},
                #{item.priceReturn},
                #{item.itemAmt},
                #{item.nonStdCode},
                #{item.outStatus},
                #{item.outingQty},
                #{item.outingAmt},
                #{item.outQty},
                #{item.outAmt},

                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

    <!-- 根据id修改-->
    <update id="updateById" parameterType="com.dk.mdm.model.pojo.ivt.IntoReturnItem">
        update dkic_b.t_psi_into_return_item
        <set>
            <if test=" et!=null and et.itemIndex != null">
                item_index = #{et.itemIndex,jdbcType=INTEGER},
            </if>
            <if test=" et!=null and et.skuId != null">
                sku_id = #{et.skuId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.itemQty != null">
                item_qty = #{et.itemQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.priceStd != null">
                price_std = #{et.priceStd,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtStd != null">
                amt_std = #{et.amtStd,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.priceReturn != null">
                price_return = #{et.priceReturn,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.itemAmt != null">
                item_amt = #{et.itemAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.nonStdCode != null">
                non_std_code = #{et.nonStdCode,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.outStatus != null">
                out_status = #{et.outStatus,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.outingQty != null">
                outing_qty = outing_qty + #{et.outingQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outingAmt != null">
                outing_amt = outing_amt + #{et.outingAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outQty != null">
                out_qty = out_qty + #{et.outQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outAmt != null">
                out_amt = out_amt + #{et.outAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.remarks != null">
                remarks = #{et.remarks,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.flgValid != null">
                flg_valid = #{et.flgValid,jdbcType=BOOLEAN},
            </if>
            <if test=" et!=null and et.cpId != null">
                cp_id = #{et.cpId,jdbcType=INTEGER},
            </if>
        </set>
        where item_id = #{et.itemId,typeHandler=UuidTypeHandler}
        and  item_qty >= (out_qty + outing_qty)
    </update>

</mapper>
