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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, check_id, item_index, sku_id, inv_id, inv_qty, check_qty, balance_qty, staff_id, loss_reason, check_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, o_inv_qty
    </sql>

    <!-- 盘点一览通用查询列 -->
    <sql id="Base_Column_List_Response">
        item.item_id,item.check_id,item.inv_id
        ,item.inv_qty,item.o_inv_qty
        ,item.check_qty,item.balance_qty,item.staff_id
        ,item.loss_reason,item.check_date,item.remarks,item.flg_valid,item.cp_id
        ,sku.sku_code,sku.sku_name,sku.sku_model,brand.short_name,wh.wh_name
        ,dd.data_value AS "lossReasonName",staff.staff_name AS "dutyStaff"
        ,item.sku_id,inv.wh_id,inv.non_std_code,item.item_id AS "fromItemId"
        ,item.check_id AS "fromId",item.item_index,item.balance_qty AS "intoingQty"
        ,abs(item.balance_qty) AS "outingQty",item.price_into,item.intoing_amt
        ,0 AS "priceOut"
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.CheckItem">
        <id column="item_id" property="itemId"/>
                <result column="check_id" property="checkId" typeHandler="UuidTypeHandler"/>
                <result column="item_index" property="itemIndex"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
                <result column="inv_qty" property="invQty"/>
                <result column="check_qty" property="checkQty"/>
                <result column="balance_qty" property="balanceQty"/>
                <result column="price_into" property="priceInto"/>
                <result column="intoing_amt" property="intoingAmt"/>
                <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
                <result column="loss_reason" property="lossReason" typeHandler="UuidTypeHandler"/>
            <result column="check_date" property="checkDate" 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"/>
                <result column="o_inv_qty" property="oInvQty"/>
    </resultMap>

    <!-- 盘点一览查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.CheckItemResponse">
        <id column="item_id" property="itemId"/>
        <result column="check_id" property="checkId" typeHandler="UuidTypeHandler"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
        <result column="inv_qty" property="invQty"/>
        <result column="check_qty" property="checkQty"/>
        <result column="balance_qty" property="balanceQty"/>
        <result column="price_into" property="priceInto"/>
        <result column="intoing_amt" property="intoingAmt"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="loss_reason" property="lossReason" typeHandler="UuidTypeHandler"/>
        <result column="check_date" property="checkDate" 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="sku_images" property="skuImages" typeHandler="JsonTypeHandler"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
        <result column="o_inv_qty" property="oInvQty"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="checkId != null and checkId != ''">
                AND check_id = #{checkId}::uuid
            </if>
            <if test="itemIndex != null">
                AND item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND sku_id = #{skuId}
            </if>
            <if test="invId != null and invId != ''">
                AND inv_id = #{invId}
            </if>
            <if test="invQty != null">
                AND inv_qty = #{invQty}
            </if>
            <if test="checkQty != null">
                AND check_qty = #{checkQty}
            </if>
            <if test="balanceQty != null">
                AND balance_qty = #{balanceQty}
            </if>
            <if test="staffId != null and staffId != ''">
                AND staff_id = #{staffId}
            </if>
            <if test="lossReason != null and lossReason != ''">
                AND loss_reason = #{lossReason}
            </if>
            <if test="checkDate != null">
                AND check_date = #{checkDate}
            </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>
            <if test="opCreateTime != null">
                AND op_create_time = #{opCreateTime}
            </if>
            <if test="opCreateUserId != null and opCreateUserId != ''">
                AND op_create_user_id = #{opCreateUserId}
            </if>
            <if test="opUpdateTime != null">
                AND op_update_time = #{opUpdateTime}
            </if>
            <if test="opUpdateUserId != null and opUpdateUserId != ''">
                AND op_update_user_id = #{opUpdateUserId}
            </if>
            <if test="opAppCode != null and opAppCode != ''">
                AND op_app_code = #{opAppCode}
            </if>
            <if test="opTimestamp != null">
                AND op_timestamp = #{opTimestamp}
            </if>
            <if test="opDbUser != null and opDbUser != ''">
                AND op_db_user = #{opDbUser}
            </if>
            <if test="oInvQty != null">
                AND o_inv_qty = #{oInvQty}
            </if>
        </where>
    </sql>

    <!-- 盘点一览条件列 -->
    <sql id="Condition_Response">
        <where>
            <if test="checkId != null and checkId != ''">
                AND item.check_id = #{checkId}::uuid
            </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_check_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,sku.sku_images
        FROM dkic_b.t_psi_check_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_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
        LEFT JOIN dkic_b.t_mst_staff AS staff ON staff.staff_id=item.staff_id
        left join dkic_b.t_mst_dictionary_data AS dd ON dd.data_id=item.loss_reason
        <include refid="Condition_Response"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

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

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

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

    <insert id="insertBatch">
        insert into t_psi_check_item
        (
        <trim suffixOverrides=",">
            check_id,
            item_index,
            sku_id,
            inv_id,
            inv_qty,
            check_qty,
            balance_qty,
            staff_id,
            loss_reason,
            check_date,
            remarks,
            cp_id,
            op_app_code,
            o_inv_qty,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.checkId}::uuid,
                #{item.itemIndex},
                #{item.skuId}::uuid,
                #{item.invId}::uuid,
                #{item.invQty},
                #{item.checkQty},
                #{item.balanceQty},
                #{item.staffId}::uuid,
                #{item.lossReason}::uuid,
                #{item.checkDate},
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
                #{item.oInvQty},
            </trim>
            )
        </foreach>
    </insert>
</mapper>
