<?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.mst.MoneyAccountItemMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id, mac_id, flow_type, invoice_id, amt_inflow, acc_date, remarks, make_staff, make_time, flg_valid, cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mst.MoneyAccountItem">
        <id column="item_id" property="itemId"/>
                <result column="mac_id" property="macId" typeHandler="UuidTypeHandler"/>
                <result column="flow_type" property="flowType"/>
                <result column="invoice_id" property="invoiceId" typeHandler="UuidTypeHandler"/>
                <result column="amt_inflow" property="amtInflow"/>
                <result column="acc_date" property="accDate" typeHandler="TimestampTypeHandler"/>
                <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="macId != null and macId != ''">
                AND mac_id = #{macId}
            </if>
            <if test="flowType != null and flowType != ''">
                AND flow_type = #{flowType}
            </if>
            <if test="invoiceId != null and invoiceId != ''">
                AND invoice_id = #{invoiceId}
            </if>
            <if test="amtInflow != null">
                AND amt_inflow = #{amtInflow}
            </if>
            <if test="accDate != null">
                AND acc_date = #{accDate}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND make_staff = #{makeStaff}
            </if>
            <if test="makeTime != null">
                AND make_time = #{makeTime}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND 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_mst_money_account_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_money_account_item
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

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

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

    <!-- 根据来源Id锁定表t_mst_money_account_item的一行数据 -->
    <select id="selectByInvoiceIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_money_account_item
        WHERE invoice_id = #{id}::uuid
        for update
    </select>

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

    <insert id="insertBatch">
        insert into dkic_b.t_mst_money_account_item
        (
        <trim suffixOverrides=",">
            mac_id,
            flow_type,
            invoice_id,
            amt_inflow,
            acc_date,
            remarks,
            make_staff,
            make_time,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.macId}::uuid,
                #{item.flowType},
                #{item.invoiceId}::uuid,
                #{item.amtInflow},
                #{item.accDate},
                #{item.remarks},
                #{item.makeStaff}::uuid,
                #{item.makeTime},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

    <delete id="deleteByInvoiceId">
        DELETE FROM dkic_b.t_mst_money_account_item
        WHERE invoice_id = #{id}::uuid;
    </delete>

    <!-- 查询收款总额 -->
    <select id="getSumAmtInflow" resultType="java.util.Map">
        SELECT COALESCE(sum(tmmai.amt_inflow), 0) as "sumAmtInflow"
        FROM dkic_b.t_mst_money_account_item as tmmai
        where tmmai.flg_valid
          and tmmai.mac_id = #{id}::uuid
    </select>
</mapper>
