liding
5 天以前 dce840653b78fc05a732c5c89773c74e15dbed09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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.ruoyi.account.mapper.FinanceAccountMapper">
 
    <select id="selectAccountPageWithBalance" resultType="com.ruoyi.account.pojo.FinanceAccount">
        SELECT fa.*,
               (fa.opening_balance + IFNULL(
                    (SELECT SUM(a.direction * a.amount) FROM account a WHERE a.account_id = fa.id), 0)
               ) AS currentBalance
        FROM finance_account fa
        WHERE 1=1
        <if test="c.accountName != null and c.accountName != ''">
            AND fa.account_name LIKE concat('%', #{c.accountName}, '%')
        </if>
        <if test="c.accountType != null and c.accountType != ''">
            AND fa.account_type = #{c.accountType}
        </if>
        <if test="c.status != null and c.status != ''">
            AND fa.status = #{c.status}
        </if>
        ORDER BY fa.id DESC
    </select>
 
    <select id="selectBalance" resultType="java.math.BigDecimal">
        SELECT IFNULL(
            (SELECT opening_balance FROM finance_account WHERE id = #{accountId}), 0)
            + IFNULL(
            (SELECT SUM(a.direction * a.amount) FROM account a WHERE a.account_id = #{accountId}), 0)
    </select>
 
    <select id="selectTotalBalance" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(fa.opening_balance), 0) + IFNULL(
            (SELECT SUM(a.direction * a.amount)
             FROM account a INNER JOIN finance_account x ON x.id = a.account_id
             WHERE x.status = '0'), 0)
        FROM finance_account fa
        WHERE fa.status = '0'
    </select>
 
    <select id="selectTotalSafetyLine" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(safety_line), 0) FROM finance_account WHERE status = '0'
    </select>
 
    <select id="trend" resultType="java.util.Map">
        SELECT
            DATE_FORMAT(a.account_date, '%Y-%m-%d') AS `date`,
            SUM(CASE WHEN a.direction &gt; 0 THEN a.amount ELSE 0 END) AS income,
            SUM(CASE WHEN a.direction &lt; 0 THEN a.amount ELSE 0 END) AS expense,
            SUM(a.direction * a.amount) AS net
        FROM account a
        WHERE 1=1
        <if test="accountId != null">
            AND a.account_id = #{accountId}
        </if>
        <if test="start != null and start != ''">
            AND a.account_date &gt;= DATE_FORMAT(#{start},'%Y-%m-%d')
        </if>
        <if test="end != null and end != ''">
            AND a.account_date &lt;= DATE_FORMAT(#{end},'%Y-%m-%d')
        </if>
        GROUP BY DATE_FORMAT(a.account_date, '%Y-%m-%d')
        ORDER BY `date` ASC
    </select>
 
</mapper>