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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, rp_id, acc_item_id, amt_receivable_handle, amt_payable_handle, amt_waive, acc_date, remarks, flg_valid, cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mac.RecPayHandleItem">
        <id column="item_id" property="itemId"/>
                <result column="rp_id" property="rpId" typeHandler="UuidTypeHandler"/>
                <result column="acc_item_id" property="accItemId" typeHandler="UuidTypeHandler"/>
                <result column="amt_receivable_handle" property="amtReceivableHandle"/>
                <result column="amt_payable_handle" property="amtPayableHandle"/>
                <result column="amt_waive" property="amtWaive"/>
            <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"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.mac.RecPayHandleItemResponse">
        <id column="item_id" property="itemId"/>
                <result column="rp_id" property="rpId" typeHandler="UuidTypeHandler"/>
                <result column="acc_item_id" property="accItemId" typeHandler="UuidTypeHandler"/>
                <result column="amt_receivable_handle" property="amtReceivableHandle"/>
                <result column="amt_payable_handle" property="amtPayableHandle"/>
                <result column="amt_waive" property="amtWaive"/>
            <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"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <if test="rpId != null and rpId != ''">
            AND t.rp_id = #{rpId}::uuid
        </if>
        <if test="accItemId != null and accItemId != ''">
            AND t.acc_item_id = #{accItemId}::uuid
        </if>
        <if test="amtReceivableHandle != null">
            AND t.amt_receivable_handle = #{amtReceivableHandle}
        </if>
        <if test="amtPayableHandle != null">
            AND t.amt_payable_handle = #{amtPayableHandle}
        </if>
        <if test="amtWaive != null">
            AND t.amt_waive = #{amtWaive}
        </if>
        <if test="accDate != null">
            AND t.acc_date = #{accDate}
        </if>
        <if test="remarks != null and remarks != ''">
            AND t.remarks = #{remarks}
        </if>
        <if test="cpId != null">
            AND t.cp_id = #{cpId}
        </if>
    </sql>

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

    <!-- 查询表t_mac_rec_pay_handle_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.rp_id,
               t.acc_item_id,
               t.amt_receivable_handle,
               t.amt_payable_handle,
               t.amt_waive,
               t.acc_date,
               t.remarks,
               t.flg_valid,
               t.cp_id,
               tmai.biznis_no  as "biznisNo",
               tmai.amt_should as "amtShould"
        FROM dkic_b.t_mac_rec_pay_handle_item as t
                 left join dkic_b.t_mac_account_item tmai on tmai.item_id = t.acc_item_id
        where t.flg_valid
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

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

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

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_mac_rec_pay_handle_item
        (
        <trim suffixOverrides=",">
            rp_id,
            acc_item_id,
            amt_receivable_handle,
            amt_payable_handle,
            amt_waive,
            acc_date,
            remarks,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.rpId}::uuid,
                #{item.accItemId}::uuid,
                #{item.amtReceivableHandle},
                #{item.amtPayableHandle},
                #{item.amtWaive},
                #{item.accDate},
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

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