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

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        mac_id, mac_code, mac_name, mac_type, balance, display_no, flg_default, mac_receipt_code, acc_date, flg_negative, remarks, flg_valid, cp_id
    </sql>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mst.MoneyAccount">
        <id column="mac_id" property="macId"/>
            <result column="mac_code" property="macCode"/>
            <result column="mac_name" property="macName"/>
            <result column="mac_type" property="macType" typeHandler="UuidTypeHandler"/>
            <result column="balance" property="balance"/>
            <result column="display_no" property="displayNo"/>
            <result column="flg_default" property="flgDefault"/>
            <result column="mac_receipt_code" property="macReceiptCode" typeHandler="JsonTypeHandler"/>
            <result column="acc_date" property="accDate"/>
            <result column="flg_negative" property="flgNegative"/>
            <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.mst.MoneyAccountResponse">
        <id column="mac_id" property="macId"/>
            <result column="mac_code" property="macCode"/>
            <result column="mac_name" property="macName"/>
            <result column="mac_type" property="macType" typeHandler="UuidTypeHandler"/>
            <result column="balance" property="balance"/>
            <result column="display_no" property="displayNo"/>
            <result column="flg_default" property="flgDefault"/>
            <result column="mac_receipt_code" property="macReceiptCode" typeHandler="JsonTypeHandler"/>
            <result column="acc_date" property="accDate"/>
            <result column="flg_negative" property="flgNegative"/>
            <result column="remarks" property="remarks"/>
            <result column="flg_valid" property="flgValid"/>
            <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="macCode != null and macCode != ''">
                AND t.mac_code LIKE concat('%',my_ex.likequery(#{macCode}),'%')
            </if>
            <if test="macName != null and macName != ''">
                AND t.mac_name LIKE concat('%',my_ex.likequery(#{macName}),'%')
            </if>
            <if test="remarks != null and remarks != ''">
                AND t.remarks = LIKE concat('%',my_ex.likequery(#{remarks}),'%')
            </if>
            <if test="cpId != null">
                AND t.cp_id = #{cpId}
            </if>
            <if test="flgValidList != null and flgValidList.size>0">
                AND t.flg_valid  =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="flgValid != null ">
                AND t.flg_valid  = #{flgValid}
            </if>
            <if test="macTypeList != null and macTypeList.size() > 0">
                AND t.mac_Type  =any(#{macTypeList, typeHandler=UuidListTypeHandler})
            </if>
        </where>
    </sql>

    <!-- 查询表t_mst_money_account，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT t.mac_id,
               t.mac_code,
               t.mac_name,
               t.mac_Type,
               dd.data_value             AS "macTypeName",
               t.balance,
               t.display_no,
               t.mac_receipt_code,
               t.acc_date,
               t.flg_negative,
               t.remarks,
               t.flg_valid,
               t.cp_id,
               coalesce(tmti.amt_trf, 0) as "initBalance"
        FROM dkic_b.t_mst_money_account as t
                 left join dkic_b.t_mst_dictionary_data as dd on t.mac_type = dd.data_id
                 left join(select tmti.trf_in_mac, tmti.amt_trf
                           from dkic_b.t_mac_transfer as tmt
                                    inner join dkic_b.t_mac_transfer_item as tmti
                                        on tmti.trf_id = tmt.trf_id and tmt.flg_valid
                                               and tmti.flg_valid and tmt.trf_type = 0) tmti
                          on tmti.trf_in_mac = t.mac_id
        <include refid="Condition"/>
        order by t.display_no
    </select>

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

    <!-- 根据主键查询表t_mst_money_account的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT t.mac_id,
               t.mac_code,
               t.mac_name,
               t.mac_Type,
               dd.data_value            AS "macTypeName",
               t.balance,
               t.display_no,
               t.mac_receipt_code,
               t.acc_date,
               t.flg_negative,
               t.remarks,
               t.flg_valid,
               t.cp_id,
               coalesce(tmti.amt_trf, 0) as "initBalance"
        FROM dkic_b.t_mst_money_account as t
                 left join dkic_b.t_mst_dictionary_data as dd on t.mac_type = dd.data_id
                 left join(select tmti.trf_in_mac, tmti.amt_trf
                           from dkic_b.t_mac_transfer as tmt
                                    inner join dkic_b.t_mac_transfer_item as tmti
                                               on tmti.trf_id = tmt.trf_id and tmt.flg_valid
                                                   and tmti.flg_valid and tmt.trf_type = 0) tmti
                          on tmti.trf_in_mac = t.mac_id
        WHERE t.mac_id = #{id}::uuid
    </select>

    <!-- 王英杰 2024年3月6日 微信小程序用 -->
    <select id="wxSelectByCond" resultType="java.util.Map">
        SELECT T
        .*
        FROM
        (
        SELECT
        tmdd.data_id AS "macId",
        null AS "macType",
        null AS  "macCode",
        tmdd.data_value AS  "macName",
        null	AS "Remarks",
        'true' AS "flgValid",
        null AS "macTypeName",
        null AS  "macId",
        null	AS "balance",
        null AS "displayNo",
        'true' AS "flgNegative",
        tmdd.cp_id
        FROM
        dkic_b.t_mst_dictionary_data tmdd
        WHERE
        tmdd.cp_id =  #{cpId} and
        tmdd.dict_code = '基础资料-账户' UNION ALL
        SELECT
        t.mac_id AS "macId",
        t.mac_Type as "macType",
        t.mac_code AS "macCode",
        t.mac_name AS "macName",
        t.remarks  AS "Remarks",
        t.flg_valid AS "flgValid",
        dd.data_value AS "macTypeName",
        t.mac_id AS  "macId",
        t.balance AS  "balance",
        t.display_no AS  "displayNo",
        t.flg_negative as "flgNegative",
        t.cp_id
        FROM
        dkic_b.t_mst_money_account as t
        LEFT JOIN dkic_b.t_mst_dictionary_data dd ON t.mac_type = dd.data_id
        <include refid="Condition"/>
        ) T
        <where>
        <if test="searchText !=null">
            AND  T."macName"  LIKE concat('%', #{searchText}, '%')
        </if>
        </where>
    </select>

    <!-- 王英杰 2024年3月6日 微信小程序用 -->
    <select id="wxCountByCond" resultType="Long">
        SELECT
        count(1)
        FROM
        (
        SELECT
        null AS "macType",
        tmdd.data_value AS  "macCode",
        null AS  "macName",
        null	AS "Remarks",
        null AS "flgValid",
        null AS "macTypeName",
        null AS  "macId",
        null	AS "balance",
        null AS "displayNo"
        FROM
        dkic_b.t_mst_dictionary_data tmdd
        WHERE
        tmdd.dict_code = '账户类别' UNION ALL
        SELECT
        t.mac_Type as "macType",
        t.mac_code AS "macCode",
        t.mac_name AS "macName",
        t.remarks  AS "Remarks",
        t.flg_valid AS "flgValid",
        dd.data_value AS "macTypeName",
        t.mac_id AS  "macId",
        t.balance AS  "balance",
        t.display_no AS  "displayNo"
        FROM
        dkic_b.t_mst_money_account as t
        LEFT JOIN dkic_b.t_mst_dictionary_data dd ON t.mac_type = dd.data_id
        <include refid="Condition"/>
        ) T
        <where>
            <if test="searchText !=null">
                AND  T."macName"  LIKE concat('%', #{searchText}, '%')
            </if>
        </where>
    </select>

    <select id="selectDisplayNo" resultType="Integer">
        SELECT MAX(display_no) + 1  FROM dkic_b.t_mst_money_account t
         WHERE t.cp_id = #{cpId}
    </select>

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