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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        t.item_id, t.trf_id, t.trf_in_mac, t.trf_out_mac, t.amt_trf, t.remarks, t.make_staff, t.make_time, t.flg_valid, t.cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mac.MacTransferItem">
        <id column="item_id" property="itemId"/>
            <result column="trf_id" property="trfId" typeHandler="UuidTypeHandler"/>
            <result column="trf_in_mac" property="trfInMac" typeHandler="UuidTypeHandler"/>
            <result column="trf_out_mac" property="trfOutMac" typeHandler="UuidTypeHandler"/>
            <result column="amt_trf" property="amtTrf"/>
            <result column="remarks" property="remarks"/>
            <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
            <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
            <result column="flg_valid" property="flgValid"/>
            <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.mac.MacTransferItemResponse">
        <id column="item_id" property="itemId"/>
            <result column="trf_id" property="trfId" typeHandler="UuidTypeHandler"/>
            <result column="trf_in_mac" property="trfInMac" typeHandler="UuidTypeHandler"/>
            <result column="trf_out_mac" property="trfOutMac" typeHandler="UuidTypeHandler"/>
            <result column="amt_trf" property="amtTrf"/>
            <result column="remarks" property="remarks"/>
            <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
            <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
            <result column="flg_valid" property="flgValid"/>
            <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="trfId != null and trfId != ''">
                AND t.trf_id = #{trfId}::uuid
            </if>
            <if test="trfInMac != null and trfInMac != ''">
                AND t.trf_in_mac = #{trfInMac}
            </if>
            <if test="trfOutMac != null and trfOutMac != ''">
                AND t.trf_out_mac = #{trfOutMac}
            </if>
            <if test="amtTrf != null">
                AND t.amt_trf = #{amtTrf}
            </if>
            <if test="remarks != null and remarks != ''">
                AND t.remarks = #{remarks}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND t.make_staff = #{makeStaff}
            </if>
            <if test="makeTime != null">
                AND t.make_time = #{makeTime}
            </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 t.item_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_mac_transfer_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.trf_id,
               t.trf_in_mac,
               t.trf_out_mac,
               t.amt_trf,
               t.remarks,
               t.make_staff,
               t.make_time,
               t.flg_valid,
               t.cp_id,
               main.mac_name  as "trfInMacName",
               maout.mac_name as "trfOutMacName"
        FROM dkic_b.t_mac_transfer_item as t
                 left join dkic_b.t_mst_money_account as main on main.mac_id = t.trf_in_mac
                 left join dkic_b.t_mst_money_account as maout on maout.mac_id = t.trf_out_mac
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_mac_transfer_item，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT count(1)
        FROM dkic_b.t_mac_transfer_item as t
        left join dkic_b.t_mst_money_account as main on main.mac_id = t.trf_in_mac
        left join dkic_b.t_mst_money_account as maout on maout.mac_id = t.trf_out_mac
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表t_mac_transfer_item的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.trf_id,
               t.trf_in_mac,
               t.trf_out_mac,
               t.amt_trf,
               t.remarks,
               t.make_staff,
               t.make_time,
               t.flg_valid,
               t.cp_id,
               main.mac_name  as "trfInMacName",
               maout.mac_name as "trfOutMacName"
        FROM dkic_b.t_mac_transfer_item as t
                 left join dkic_b.t_mst_money_account as main on main.mac_id = t.trf_in_mac
                 left join dkic_b.t_mst_money_account as maout on maout.mac_id = t.trf_out_mac
        WHERE t.item_id = #{id}::uuid
    </select>

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

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_mac_transfer_item
        (
        <trim suffixOverrides=",">
            trf_id,
            trf_in_mac,
            trf_out_mac,
            amt_trf,
            remarks,
            make_staff,
            make_time,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.trfId}::uuid,
                #{item.trfInMac}::uuid,
                #{item.trfOutMac}::uuid,
                #{item.amtTrf},
                #{item.remarks},
                #{item.makeStaff}::uuid,
                #{item.makeTime},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

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

    <!-- 根据主键锁定表t_mac_transfer的一行数据 -->
    <select id="selectByInMacIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mac_transfer_item as t
         inner join dkic_b.t_mac_transfer as tmt
             on tmt.trf_id = t.trf_id and tmt.trf_type = 0
        where t.trf_in_mac = #{id}::uuid
        for update
    </select>
</mapper>
