<?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.dayou.mapper.FinanceRealFundMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.FinanceRealFund">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="modified" property="modified" />
        <result column="created" property="created" />
        <result column="payer" property="payer" />
        <result column="payer_bank" property="payerBank" />
        <result column="payer_account" property="payerAccount" />
        <result column="amount" property="amount" />
        <result column="pay_datetime" property="payDatetime" />
        <result column="remark" property="remark" />
        <result column="creator_id" property="creatorId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        modified,
        created,
        payer, payer_bank, payer_account, amount, pay_datetime, remark, creator_id
    </sql>

    <select id="getPage" parameterType="com.dayou.entity.FinanceRealFund" resultType="com.dayou.vo.FinanceRealFundVO">
        SELECT
            frf.id,
            frf.created,
            frf.payer,
            frf.payer_bank,
            frf.payer_account,
            frf.amount,
            frf.pay_datetime,
            frf.remark,
            frf.creator_id,
            u.name AS creator,
            ifnull(t.ca,0) AS claimAmount,
            ( frf.amount - ifnull(t.ca,0) ) AS notClaimAmount
        FROM
            finance_real_fund frf
                LEFT JOIN user u ON u.id = frf.creator_id
                LEFT JOIN ( SELECT real_fund_id, sum(ifnull(claim_amount,0)) ca FROM finance_claim WHERE deleted = 0 GROUP BY real_fund_id ) t ON t.real_fund_id = frf.id
        WHERE
            frf.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            frf.payer like concat ('%',#{keyword},'%')
            or frf.payer_bank like concat('%',#{keyword},'%')
            or u.name like concat('%',#{keyword},'%')
            or frf.payer_account like concat('%',#{keyword},'%')
            or frf.amount like concat('%',#{keyword},'%')
            or frf.remark like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.startDate!=null and vo.startDate!=''">
            and frf.pay_datetime &gt;= #{vo.startDate}
        </if>
        <if test="vo!=null and vo.endDate!=null and vo.endDate!=''">
            and frf.pay_datetime &lt;= #{vo.endDate}
        </if>
        <if test="vo!=null and vo.xStartDate!=null and vo.xStartDate!=''">
            and frf.created &gt;= #{vo.xStartDate}
        </if>
        <if test="vo!=null and vo.xEndDate!=null and vo.xEndDate!=''">
            and frf.created &lt;= #{vo.xEndDate}
        </if>
        ORDER BY
            frf.created DESC
    </select>

    <select id="getNotClaimAmount" parameterType="java.lang.Long" resultType="java.math.BigDecimal">
        SELECT

            ( frf.amount - ifnull(t.ca,0) ) AS notClaimAmount
        FROM
            finance_real_fund frf
                INNER JOIN ( SELECT real_fund_id,ifnull(sum(claim_amount),0) as ca  FROM finance_claim WHERE deleted = 0 and real_fund_id = #{realFundId} ) t ON t.real_fund_id = frf.id
        WHERE
            frf.deleted = 0
    </select>
</mapper>
