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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?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.sales.mapper.SalesLedgerMapper">
 
    <select id="selectSequencesByDate" resultType="java.lang.Integer">
        SELECT CAST(SUBSTR(sales_contract_no,LENGTH(#{datePart})+1 , 3) AS SIGNED)
        FROM sales_ledger
        WHERE sales_contract_no LIKE CONCAT('%',#{datePart},'%')
    </select>
    <select id="getSalesNo" resultType="com.ruoyi.sales.pojo.SalesLedger">
        
    </select>
 
    <select id="selectSalesLedgerList" resultType="com.ruoyi.sales.pojo.SalesLedger">
        SELECT
        T1.id,
        T1.sales_contract_no,
        T1.customer_contract_no,
        T1.project_name,
        T1.entry_date,
        T1.salesman,
        T1.customer_id,
        T1.customer_name,
        T1.entry_person,
        T1.remarks,
        T1.attachment_materials,
        T1.tenant_id,
        T1.contract_amount,
        T1.execution_date,
        T2.nick_name AS entry_person_name,
        T1.payment_method,
        T1.is_produce AS produce,
        DATEDIFF(T1.delivery_date, CURDATE()) AS delivery_days_diff
        FROM
        sales_ledger T1
        LEFT JOIN sys_user T2 ON T1.entry_person = T2.user_id
        <where>
            <if test="salesLedgerDto.customerName != null and salesLedgerDto.customerName != '' ">
                T1.customer_name LIKE CONCAT('%',#{salesLedgerDto.customerName},'%')
            </if>
        </where>
    </select>
 
    <select id="selectSalesLedgerListPage" resultType="com.ruoyi.sales.dto.SalesLedgerDto">
        SELECT T1.id,
        T1.sales_contract_no,
        T1.customer_contract_no,
        T1.project_name,
        T1.entry_date,
        T1.salesman,
        T1.customer_id,
        T1.customer_name,
        T3.company_address,
        T3.contact_phone,
        T3.contact_person,
        T3.company_phone,
        T1.entry_person,
        T1.remarks,
        T1.attachment_materials,
        T1.tenant_id,
        T1.contract_amount,
        T1.contract_amount                    as noInvoiceAmountTotal,
        T1.execution_date,
        T2.nick_name                          AS entry_person_name,
        T1.payment_method,
        T1.is_produce AS produce,
        T1.delivery_date,
        DATEDIFF(T1.delivery_date, CURDATE()) AS delivery_days_diff,
        product_summary.model,
        '' AS voltage,
        product_summary.qty,
        CASE
        WHEN shipping_status_counts.total_count = 0 THEN false
        WHEN shipping_status_counts.unshipped_count = 0 THEN true
        ELSE false
        END AS is_fh
        FROM sales_ledger T1
        LEFT JOIN sys_user T2 ON T1.entry_person = T2.user_id
        LEFT JOIN customer T3 ON T1.customer_id = T3.id
        LEFT JOIN (
        SELECT sales_ledger_id,
        COUNT(*) as total_count,
        SUM(CASE WHEN status != '已发货' THEN 1 ELSE 0 END) as unshipped_count
        FROM shipping_info
        GROUP BY sales_ledger_id
        ) shipping_status_counts ON T1.id = shipping_status_counts.sales_ledger_id
        LEFT JOIN (
        SELECT
        sales_ledger_id,
        GROUP_CONCAT(IFNULL(specification_model, '') ORDER BY id SEPARATOR ',') AS model,
        GROUP_CONCAT(IFNULL(CAST(quantity AS CHAR), '') ORDER BY id SEPARATOR ',') AS qty
        FROM sales_ledger_product
        WHERE type = 1
        GROUP BY sales_ledger_id
        ) product_summary ON T1.id = product_summary.sales_ledger_id
        <where>
            <if test="salesLedgerDto.customerName != null and salesLedgerDto.customerName != '' ">
                AND  T1.customer_name LIKE CONCAT('%',#{salesLedgerDto.customerName},'%')
            </if>
            <if test="salesLedgerDto.customerContractNo != null and salesLedgerDto.customerContractNo !='' ">
                AND  T1.customer_contract_no LIKE CONCAT('%',#{salesLedgerDto.customerContractNo},'%')
            </if>
            <if test="salesLedgerDto.salesContractNo != null and salesLedgerDto.salesContractNo != '' ">
                AND  T1.sales_contract_no LIKE CONCAT('%',#{salesLedgerDto.salesContractNo},'%')
            </if>
            <if test="salesLedgerDto.projectName != null and salesLedgerDto.projectName != '' ">
                AND T1.project_name LIKE CONCAT('%',#{salesLedgerDto.projectName},'%')
            </if>
            <if test="salesLedgerDto.entryDateStart != null and salesLedgerDto.entryDateStart != '' ">
               AND T1.entry_date &gt;= DATE_FORMAT(#{salesLedgerDto.entryDateStart},'%Y-%m-%d')
            </if>
            <if test="salesLedgerDto.entryDateEnd != null and salesLedgerDto.entryDateEnd != '' ">
                AND  T1.entry_date &lt;= DATE_FORMAT(#{salesLedgerDto.entryDateEnd},'%Y-%m-%d')
            </if>
        </where>
    order by T1.entry_date desc
    </select>
 
    <select id="selectIncomeStats" resultType="com.ruoyi.home.dto.IncomeExpenseAnalysisDto">
        SELECT DATE_FORMAT(entry_date, #{dateFormat}) as dateStr, IFNULL(SUM(contract_amount), 0) as amount
        FROM sales_ledger
        WHERE entry_date BETWEEN #{startDate} AND #{endDate}
        GROUP BY dateStr
    </select>
 
    <select id="selectCustomerSalesComposition" resultType="com.ruoyi.dto.MapDto">
        SELECT customer_name as name, CAST(IFNULL(SUM(contract_amount), 0) AS CHAR) as value
        FROM sales_ledger
        GROUP BY customer_name
    </select>
</mapper>