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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        record_id, inv_id, biznis_type, biznis_id, biznis_item_id, sku_id, non_std_code, price_into, into_qty, into_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>

    <sql id="Base_Column_List_Response">
        tpior.record_id, tpior.inv_id, tpior.biznis_type, tpior.biznis_id, tpior.biznis_item_id, tpior.sku_id,
        tpior.non_std_code, tpior.price_into, tpior.into_qty, tpior.into_amt, tpior.acc_date, tpior.remarks,
        tpior.flg_valid, tpior.cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.InOutRecord">
        <id column="record_id" property="recordId"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="biznis_type" property="biznisType"/>
                <result column="biznis_id" property="biznisId" typeHandler="UuidTypeHandler"/>
                <result column="biznis_item_id" property="biznisItemId" typeHandler="UuidTypeHandler"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="non_std_code" property="nonStdCode"/>
                <result column="price_into" property="priceInto"/>
                <result column="into_qty" property="intoQty"/>
                <result column="into_amt" property="intoAmt"/>
            <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.InOutRecordResponse">
        <id column="record_id" property="recordId"/>
        <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
        <result column="biznis_type" property="biznisType"/>
        <result column="biznis_id" property="biznisId" typeHandler="UuidTypeHandler"/>
        <result column="biznis_item_id" property="biznisItemId" typeHandler="UuidTypeHandler"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="price_into" property="priceInto"/>
        <result column="into_qty" property="intoQty"/>
        <result column="into_amt" property="intoAmt"/>
        <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="biznis_no" property="biznisNo"/>
        <result column="biznis_item_no" property="biznisItemNo"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="invId != null and invId != ''">
                AND tpior.inv_id = #{invId}
            </if>
            <if test="biznisType != null and biznisType != ''">
                AND tpior.biznis_type = #{biznisType}
            </if>
            <if test="biznisId != null and biznisId != ''">
                AND tpior.biznis_id = #{biznisId}
            </if>
            <if test="biznisItemId != null and biznisItemId != ''">
                AND tpior.biznis_item_id = #{biznisItemId}
            </if>
            <if test="skuId != null and skuId != ''">
                AND tpior.sku_id = #{skuId}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND tpior.non_std_code = #{nonStdCode}
            </if>
            <if test="priceInto != null">
                AND tpior.price_into = #{priceInto}
            </if>
            <if test="intoQty != null">
                AND tpior.into_qty = #{intoQty}
            </if>
            <if test="intoAmt != null">
                AND tpior.into_amt = #{intoAmt}
            </if>
            <if test="accDate != null">
                AND tpior.acc_date = #{accDate}
            </if>
            <if test="flgValid != null">
                AND tpior.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpior.cp_id = #{cpId}
            </if>
        </where>
    </sql>

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

    <!-- 查询表t_psi_in_out_record，（条件查询+分页）列表 -->
    <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
        FROM dkic_b.t_psi_in_out_record  tpior
        <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>

    <!-- 查询表t_psi_in_out_record，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_in_out_record tpior
        <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 = tpior.sku_id
    </sql>

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

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_psi_in_out_record
        (
        <trim suffixOverrides=",">
            inv_id,
            biznis_type,
            biznis_id,
            biznis_item_id,
            sku_id,
            non_std_code,
            price_into,
            into_qty,
            into_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.biznisType},
                #{item.biznisId}::uuid,
                #{item.biznisItemId}::uuid,
                #{item.skuId}::uuid,
                #{item.nonStdCode},
                #{item.priceInto},
                #{item.intoQty},
                #{item.intoAmt},
                #{item.accDate},
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>


    <!--删除数据-->
    <delete id="deleteData" parameterType="com.dk.mdm.model.pojo.ivt.InOutRecord">
        DELETE
        FROM dkic_b.t_psi_in_out_record
        where biznis_id = #{biznisId,typeHandler=UuidTypeHandler}
          and biznis_item_id = #{biznisItemId,typeHandler=UuidTypeHandler}
    </delete>
</mapper>
