<?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.mac.OtherPayableItemMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, payable_id, payable_type, qty_payable, amt_payable, remarks, flg_valid, cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mac.OtherPayableItem">
        <id column="item_id" property="itemId"/>
            <result column="payable_id" property="payableId" typeHandler="UuidTypeHandler"/>
            <result column="payable_type" property="payableType" typeHandler="UuidTypeHandler"/>
            <result column="qty_payable" property="qtyPayable"/>
            <result column="amt_payable" property="amtPayable"/>
            <result column="remarks" property="remarks"/>
            <result column="flg_valid" property="flgValid"/>
            <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.mac.OtherPayableItemResponse">
        <id column="item_id" property="itemId"/>
            <result column="payable_id" property="payableId" typeHandler="UuidTypeHandler"/>
            <result column="payable_type" property="payableType" typeHandler="UuidTypeHandler"/>
            <result column="qty_payable" property="qtyPayable"/>
            <result column="amt_payable" property="amtPayable"/>
            <result column="remarks" property="remarks"/>
            <result column="flg_valid" property="flgValid"/>
            <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="payableId != null and payableId != ''">
                AND t.payable_id = #{payableId}::uuid
            </if>
            <if test="payableType != null and payableType != ''">
                AND t.payable_type = #{payableType}
            </if>
            <if test="qtyPayable != null">
                AND t.qty_payable = #{qtyPayable}
            </if>
            <if test="amtPayable != null">
                AND t.amt_payable = #{amtPayable}
            </if>
            <if test="remarks != null and remarks != ''">
                AND t.remarks = #{remarks}
            </if>
            <if test="flgValid != null">
                AND t.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND t.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_mac_other_payable_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.payable_id,
               t.payable_type,
               tmda.data_value as "payableTypeName",
               t.qty_payable,
               t.amt_payable,
               t.remarks,
               t.flg_valid,
               t.cp_id
        FROM dkic_b.t_mac_other_payable_item as t
                 LEFT JOIN dkic_b.t_mst_dictionary_data as tmda ON tmda.data_id = t.payable_type
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_mac_other_payable_item，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT count(1)
        FROM dkic_b.t_mac_other_payable_item as t
        LEFT JOIN dkic_b.t_mst_dictionary_data as tmda ON tmda.data_id = t.payable_type
        <include refid="Condition"/>
    </select>

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

    <!-- 根据主表主键锁定表t_psi_outbound_item的多行数据 -->
    <select id="selectByZIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mac_other_payable_item
        WHERE payable_id = #{id}::uuid
        for update
    </select>

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_mac_other_payable_item
        (
        <trim suffixOverrides=",">
            payable_id,
            payable_type,
            qty_payable,
            amt_payable,
            remarks,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.payableId}::uuid,
                #{item.payableType}::uuid,
                #{item.qtyPayable},
                #{item.amtPayable},
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

    <delete id="deleteById">
        DELETE
        FROM dkic_b.t_mac_other_payable_item
        WHERE item_id = #{id}::uuid;
    </delete>
</mapper>
