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

    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        t.object_id, t.object_type, t.receipt, t.receipt_lock, t.contract_assets, t.receivable,
        t.receivable_handle, t.receivable_waive, t.receivable_residue, t.receipt_residue, t.payment,
        t.pur_estimate, t.payable, t.payable_handle, t.payable_waive, t.payable_residue,
        t.payment_residue, t.remarks, t.flg_valid, t.cp_id
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mac.Account">
        <id column="object_id" property="objectId"/>
            <result column="object_type" property="objectType"/>
            <result column="receipt" property="receipt"/>
            <result column="receipt_lock" property="receiptLock"/>
            <result column="contract_assets" property="contractAssets"/>
            <result column="receivable" property="receivable"/>
            <result column="receivable_handle" property="receivableHandle"/>
            <result column="receivable_waive" property="receivableWaive"/>
            <result column="receivable_residue" property="receivableResidue"/>
            <result column="receipt_residue" property="receiptResidue"/>
            <result column="payment" property="payment"/>
            <result column="pur_estimate" property="purEstimate"/>
            <result column="payable" property="payable"/>
            <result column="payable_handle" property="payableHandle"/>
            <result column="payable_waive" property="payableWaive"/>
            <result column="payable_residue" property="payableResidue"/>
            <result column="payment_residue" property="paymentResidue"/>
            <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.AccountResponse">
        <id column="object_id" property="objectId"/>
            <result column="object_type" property="objectType"/>
            <result column="receipt" property="receipt"/>
            <result column="receipt_lock" property="receiptLock"/>
            <result column="contract_assets" property="contractAssets"/>
            <result column="receivable" property="receivable"/>
            <result column="receivable_handle" property="receivableHandle"/>
            <result column="receivable_waive" property="receivableWaive"/>
            <result column="receivable_residue" property="receivableResidue"/>
            <result column="receipt_residue" property="receiptResidue"/>
            <result column="payment" property="payment"/>
            <result column="pur_estimate" property="purEstimate"/>
            <result column="payable" property="payable"/>
            <result column="payable_handle" property="payableHandle"/>
            <result column="payable_waive" property="payableWaive"/>
            <result column="payable_residue" property="payableResidue"/>
            <result column="payment_residue" property="paymentResidue"/>
            <result column="remarks" property="remarks"/>
            <result column="flg_valid" property="flgValid"/>
            <result column="cp_id" property="cpId"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="objectId != null and objectId != ''">
                AND t.object_id = #{objectId}::uuid
            </if>
            <if test="objectType != null and objectType != ''">
                AND t.object_type = #{objectType}
            </if>
            <if test="receipt != null">
                AND t.receipt = #{receipt}
            </if>
            <if test="receiptLock != null">
                AND t.receipt_lock = #{receiptLock}
            </if>
            <if test="contractAssets != null">
                AND t.contract_assets = #{contractAssets}
            </if>
            <if test="receivable != null">
                AND t.receivable = #{receivable}
            </if>
            <if test="receivableHandle != null">
                AND t.receivable_handle = #{receivableHandle}
            </if>
            <if test="receivableWaive != null">
                AND t.receivable_waive = #{receivableWaive}
            </if>
            <if test="receivableResidue != null">
                AND t.receivable_residue = #{receivableResidue}
            </if>
            <if test="receiptResidue != null">
                AND t.receipt_residue = #{receiptResidue}
            </if>
            <if test="payment != null">
                AND t.payment = #{payment}
            </if>
            <if test="purEstimate != null">
                AND t.pur_estimate = #{purEstimate}
            </if>
            <if test="payable != null">
                AND t.payable = #{payable}
            </if>
            <if test="payableHandle != null">
                AND t.payable_handle = #{payableHandle}
            </if>
            <if test="payableWaive != null">
                AND t.payable_waive = #{payableWaive}
            </if>
            <if test="payableResidue != null">
                AND t.payable_residue = #{payableResidue}
            </if>
            <if test="paymentResidue != null">
                AND t.payment_residue = #{paymentResidue}
            </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">
        <!-- 根据主键objectId批量操作 -->
        WHERE  t.object_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_mac_account，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT t.object_id,
               t.object_type,
               t.receipt,
               t.receipt_lock,
               t.contract_assets,
               t.receivable,
               t.receivable_handle,
               t.receivable_residue,
               t.receivable_waive,
               t.receipt_residue,
               t.payment,
               t.pur_estimate,
               t.payable,
               t.payable_handle,
               t.payable_residue,
               t.payable_waive,
               t.payment_residue,
               t.remarks,
               t.flg_valid,
               t.cp_id,
               t.receivable - t.receipt as "receivableBalance"
        FROM dkic_b.t_mac_account as t
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

    <!-- 根据主键查询表t_mac_account的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT t.object_id,
               t.object_type,
               t.receipt,
               t.receipt_lock,
               t.contract_assets,
               t.receivable,
               t.receivable_handle,
               t.receivable_waive,
               t.receivable_residue,
               t.receipt_residue,
               t.payment,
               t.pur_estimate,
               t.payable,
               t.payable_handle,
               t.payable_waive,
               t.payable_residue,
               t.payment_residue,
               t.remarks,
               t.flg_valid,
               t.cp_id,
               sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS "objectTypeName",
               tmp.sup_name  AS "supName",
               tmc.cus_name  as "cusName",
               tmc.cus_phone as "cusPhone"
        FROM dkic_b.t_mac_account t
                 LEFT JOIN sys.t_data_kind tdk ON tdk.kind_code = t.object_type
                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.object_id
                 left join dkic_b.t_mst_customer tmc on t.object_id = tmc.cus_id
        WHERE t.object_id = #{id}::uuid
    </select>

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_mac_account
        (
        <trim suffixOverrides=",">
            object_type,
            receipt,
            receipt_lock,
            contract_assets,
            receivable,
            receivable_handle,
            receivable_waive,
            receivable_residue,
            receipt_residue,
            payment,
            pur_estimate,
            payable,
            payable_handle,
            payable_waive,
            payable_residue,
            payment_residue,
            remarks,
            cp_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.objectType},
                #{item.receipt},
                #{item.receiptLock},
                #{item.contractAssets},
                #{item.receivable},
                #{item.receivableHandle},
                #{item.receivableWaive},
                #{item.receivableResidue},
                #{item.receiptResidue},
                #{item.payment},
                #{item.purEstimate},
                #{item.payable},
                #{item.payableHandle},
                #{item.payableWaive},
                #{item.payableResidue},
                #{item.paymentResidue},
                #{item.remarks},
                #{item.cpId},
            </trim>
            )
        </foreach>
    </insert>
</mapper>
