fuliqi
2025-01-16 edc7172b312e0aec94362b651e2f7145e0c357fe
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?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.ycl.platform.mapper.CalculateReportMapper">
 
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ycl.platform.domain.vo.CalculateReportVO">
        <result column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="unit_name" property="unitName"/>
        <result column="name" property="contractName"/>
        <result column="contract_id" property="contractId"/>
        <result column="calculate_time" property="calculateTime"/>
        <result column="deduct_money" property="deductMoney"/>
        <result column="latest_time" property="latestTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="can_publish" property="canPublish"/>
        <result column="status" property="status"/>
        <result column="latestDeductMoney" property="latestDeductMoney"/>
    </resultMap>
 
<!--    <select id="page" resultMap="BaseResultMap">-->
<!--        SELECT-->
<!--            tcr.contract_id,-->
<!--            tyu.unit_name,-->
<!--            tc.name,-->
<!--            tcr.id,-->
<!--            tcr.status,-->
<!--            tcr.deduct_money,-->
<!--            tcr.create_time,-->
<!--            tcr.update_time,-->
<!--            tcr.calculate_time,-->
<!--            tcr.latest_time,-->
<!--            (SELECT a.deduct_money FROM (SELECT deduct_money,which_year, which_month FROM t_calculate_record WHERE contract_id = tcr.contract_id ORDER BY which_year, which_month DESC limit 1) a) as latestDeductMoney-->
<!--        FROM-->
<!--        t_calculate_report tcr-->
<!--        INNER JOIN t_contract tc ON tcr.contract_id = tc.id-->
<!--        INNER JOIN t_yw_unit tyu ON tyu.id = tc.unit_id-->
<!--        <where>-->
<!--            AND tcr.deleted = 0 AND tc.deleted = 0 AND tyu.deleted = 0-->
<!--            <if test="query.unitName != null and query.unitName != '' ">-->
<!--                AND tyu.unit_name like concat('%', #{query.unitName}, '%')-->
<!--            </if>-->
<!--            <if test="query.unitId != null">-->
<!--                AND tyu.id = #{query.unitId}-->
<!--            </if>-->
<!--        </where>-->
<!--    </select>-->
    <select id="page" resultType="com.ycl.platform.domain.vo.CalculateReportVO">
        SELECT
        tcr.id,
        tcr.contract_id,
        tyu.unit_name,
        tc.name as contractName,
        tcr.which_year,
        tcr.which_month,
        SUM(tcr.deduct_money) AS deductMoney,
        MAX(tcr.which_month) AS latest_month,
        (
        SELECT deduct_money
        FROM t_calculate_record
        WHERE contract_id = tcr.contract_id
        AND which_year = tcr.which_year
        ORDER BY which_month DESC
        LIMIT 1
        ) AS latestDeductMoney
        FROM
        t_calculate_record tcr
        INNER JOIN t_contract tc ON tcr.contract_id = tc.id
        INNER JOIN t_yw_unit tyu ON tyu.id = tc.unit_id
        <where>
            AND tcr.deleted = 0 AND tc.deleted = 0 AND tyu.deleted = 0
            <if test="query.status!=null and query.status !=''">
                AND tcr.status = #{query.status}
            </if>
        </where>
        group by contract_id,which_year
    </select>
    <resultMap id="DetailResultMap" type="com.ycl.platform.domain.vo.CalculateReportDetailVO">
        <result column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="unit_name" property="unitName"/>
        <result column="unit_id" property="unitId"/>
        <result column="name" property="contractName"/>
        <result column="contract_id" property="contractId"/>
        <result column="which_year" property="whichYear"/>
        <result column="status" property="status"/>
        <collection property="ruleList" column="contract_id" ofType="com.ycl.platform.domain.vo.CalculateMoneyRuleVO"
                    select="selectRuleByContractId"/>
        <collection property="recordList" column="{contractId=contract_id, whichYear=which_year,status=status}" ofType="com.ycl.platform.domain.vo.CalculateRecordVO"
                    select="selectRecordByContractId"/>
    </resultMap>
 
    <select id="selectRuleByContractId" resultType="com.ycl.platform.domain.vo.CalculateMoneyRuleVO">
        SELECT id,
               description,
               score_condition as scoreCondition
        FROM t_calculate_money_rule
        WHERE contract_id = #{contractId}
          AND deleted = 0
    </select>
 
    <select id="selectRecordByContractId" resultType="com.ycl.platform.domain.vo.CalculateRecordVO">
        SELECT
        tcr.id,
        tcr.create_time as createTime,
        (IFNULL(-SUM(tcs.score), 0) + 100) AS score,
        tcr.deduct_money as deductMoney,
        tcr.which_year as whichYear,
        tcr.which_month as whichMonth,
        tcr.status
        FROM
        t_calculate_record tcr
        LEFT JOIN t_contract_score tcs
            ON tcr.contract_id =tcs.contract_id
                   AND tcs.deleted=0
                   AND YEAR(tcs.create_time) = #{whichYear}
                   AND MONTH(tcs.create_time) = tcr.which_month
                   AND tcs.auditing_status = 'PASS'
        <where>
            tcr.contract_id = #{contractId} AND tcr.which_year = #{whichYear} AND tcr.deleted = 0
            <if test="status!=null">
                AND tcr.status=#{status}
            </if>
        </where>
        group by tcr.id
        ORDER BY
        tcr.create_time
    </select>
 
    <select id="getById" resultMap="DetailResultMap">
        SELECT tyu.unit_name,
        tyu.id as unit_id,
        tc.name,
        tcr.id,
        tcr.contract_id,
        tcr.create_time,
        tcr.update_time,
        tcr.which_year,
        #{query.status} as status
        FROM t_calculate_record tcr
        INNER JOIN t_contract tc ON tcr.contract_id = tc.id
        INNER JOIN t_yw_unit tyu ON tyu.id = tc.unit_id
        <where>
            tcr.contract_id = #{query.contractId} AND tcr.which_year = #{query.whichYear}
        </where>
        limit 1
    </select>
 
    <select id="exportData" resultType="com.ycl.platform.domain.excel.CalculateExport">
        SELECT
        a.rule_name as ruleName,
        a.rule_condition as detailName,
        COUNT(b.id) AS num,
        IFNULL(-SUM(b.score), 0) AS score
        FROM t_calculate_rule a
        LEFT JOIN t_calculate_record tcr ON a.contract_id = tcr.contract_id
        LEFT JOIN t_contract_score b ON b.rule_id = a.id AND auditing_status = 'PASS' AND b.deleted = 0
            AND MONTH(b.create_time) = tcr.which_month
            AND YEAR(b.create_time) = tcr.which_year
        <where>
            a.deleted = 0 and a.contract_id = #{contractId}
            <if test="whichMonth!=null">
                and tcr.which_month = #{whichMonth}
            </if>
            <if test="whichYear!=null">
                and tcr.which_year = #{whichYear}
            </if>
        </where>
        GROUP BY a.id
    </select>
 
</mapper>