浏览代码

19.提取sql片段

sunpanhu 4 周之前
父节点
当前提交
309fd64693

+ 39 - 44
airport-ledger/src/main/resources/mapper/ledger/LedgerSeizureStatsMapper.xml

@@ -96,15 +96,9 @@
96 96
             </choose>,
97 97
             IFNULL(SUM(item_quantity), 0) AS seizeNum
98 98
         FROM ledger_seizure_stats
99
-        WHERE
100
-            del_flag = '0'
101
-            <!-- 近30天自然日 -->
102
-            AND record_date >= DATE_SUB(CURDATE(), INTERVAL 29 DAY)
103
-            AND record_date &lt;= CURDATE()
104
-            <!-- 三级动态筛选 -->
105
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
106
-            <if test="teamId != null">AND team_id = #{teamId}</if>
107
-            <if test="groupId != null">AND group_id = #{groupId}</if>
99
+        <where>
100
+            <include refid="FILTER_FIXED_DATE_CRITERIA_SQL"/>
101
+        </where>
108 102
         <!-- 动态分组:重点!全为空时仅按日期分组 -->
109 103
         GROUP BY
110 104
             <choose>
@@ -129,15 +123,9 @@
129 123
             </choose>,
130 124
             IFNULL(SUM(item_quantity), 0) AS seizeNum
131 125
         FROM ledger_seizure_stats
132
-        WHERE
133
-            del_flag = '0'
134
-            <!-- 近30天自然日 -->
135
-            AND record_date >= DATE_SUB(CURDATE(), INTERVAL 29 DAY)
136
-            AND record_date &lt;= CURDATE()
137
-            <!-- 三级筛选条件 -->
138
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
139
-            <if test="teamId != null">AND team_id = #{teamId}</if>
140
-            <if test="groupId != null">AND group_id = #{groupId}</if>
126
+        <where>
127
+            <include refid="FILTER_FIXED_DATE_CRITERIA_SQL"/>
128
+        </where>
141 129
         <!-- 动态分组,严格层级下钻 -->
142 130
         GROUP BY
143 131
             <choose>
@@ -159,12 +147,9 @@
159 147
             </choose>,
160 148
             IFNULL(SUM(item_quantity), 0) AS seizeNum
161 149
         FROM ledger_seizure_stats
162
-        WHERE
163
-            del_flag = '0'
164
-            AND record_date BETWEEN #{startDate} AND #{endDate}
165
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
166
-            <if test="teamId != null">AND team_id = #{teamId}</if>
167
-            <if test="groupId != null">AND group_id = #{groupId}</if>
150
+        <where>
151
+            <include refid="FILTER_FIXED_CRITERIA_SQL"/>
152
+        </where>
168 153
         GROUP BY
169 154
             <choose>
170 155
                 <when test="groupId != null">inspector_id</when>
@@ -179,27 +164,20 @@
179 164
     <select id="statsTimeRangeTotal" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="java.lang.Integer">
180 165
         SELECT IFNULL(SUM(item_quantity), 0)
181 166
         FROM ledger_seizure_stats
182
-        WHERE
183
-            del_flag = '0'
184
-            AND record_date BETWEEN #{startDate} AND #{endDate}
185
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
186
-            <if test="teamId != null">AND team_id = #{teamId}</if>
187
-            <if test="groupId != null">AND group_id = #{groupId}</if>
167
+        <where>
168
+            <include refid="FILTER_FIXED_CRITERIA_SQL"/>
169
+        </where>
188 170
     </select>
189 171
 
190 172
     <select id="countSeizeSubjectCategoryQuantity" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.SeizeCategoryQuantityVO">
191 173
         SELECT item_category AS categoryName, IFNULL(SUM(item_quantity), 0) AS categoryNum
192 174
         FROM ledger_seizure_stats
193
-        WHERE
194
-            del_flag = '0'
195
-            AND record_date BETWEEN #{startDate} AND #{endDate}
196
-            <!-- 三级联动筛选 -->
197
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
198
-            <if test="teamId != null">AND team_id = #{teamId}</if>
199
-            <if test="groupId != null">AND group_id = #{groupId}</if>
175
+        <where>
176
+            <include refid="FILTER_FIXED_CRITERIA_SQL"/>
200 177
             <!-- 过滤空类别,避免脏数据 -->
201 178
             AND item_category IS NOT NULL
202 179
             AND item_category != ''
180
+        </where>
203 181
         GROUP BY item_category
204 182
         ORDER BY categoryNum DESC
205 183
     </select>
@@ -207,16 +185,33 @@
207 185
     <select id="countSeizeAreaQuantity" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.SeizureAreaVO">
208 186
         SELECT area_id AS areaId, work_area AS workArea, IFNULL(SUM(item_quantity), 0) AS areaSeizeNum
209 187
         FROM ledger_seizure_stats
210
-        WHERE
211
-            del_flag = '0'
212
-            AND record_date BETWEEN #{startDate} AND #{endDate}
213
-            <!-- 三级联动筛选 -->
214
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
215
-            <if test="teamId != null">AND team_id = #{teamId}</if>
216
-            <if test="groupId != null">AND group_id = #{groupId}</if>
188
+        <where>
189
+            <include refid="FILTER_FIXED_CRITERIA_SQL"/>
217 190
             <!-- 过滤空区域脏数据 -->
218 191
             AND work_area IS NOT NULL AND work_area != ''
192
+        </where>
219 193
         GROUP BY area_id, work_area
220 194
         ORDER BY areaSeizeNum DESC
221 195
     </select>
196
+
197
+    <sql id="FILTER_FIXED_CRITERIA_SQL">
198
+        del_flag = '0'
199
+        AND record_date BETWEEN #{startDate} AND #{endDate}
200
+        <include refid="FILTER_CRITERIA_SQL"/>
201
+    </sql>
202
+
203
+    <sql id="FILTER_FIXED_DATE_CRITERIA_SQL">
204
+        del_flag = '0'
205
+        <!-- 近30天自然日 -->
206
+        AND record_date >= DATE_SUB(CURDATE(), INTERVAL 29 DAY)
207
+        AND record_date &lt;= CURDATE()
208
+        <include refid="FILTER_CRITERIA_SQL"/>
209
+    </sql>
210
+
211
+    <sql id="FILTER_CRITERIA_SQL">
212
+        <!-- 三级联动筛选 -->
213
+        <if test="deptId != null">AND dept_id = #{deptId}</if>
214
+        <if test="teamId != null">AND team_id = #{teamId}</if>
215
+        <if test="groupId != null">AND group_id = #{groupId}</if>
216
+    </sql>
222 217
 </mapper>

+ 18 - 21
airport-ledger/src/main/resources/mapper/ledger/LedgerUnsafeEventMapper.xml

@@ -59,15 +59,11 @@
59 59
     <select id="countSeizureStatsItem" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.UnsafeItemVO">
60 60
         SELECT involved_items AS itemName, COUNT(*) AS itemNum
61 61
         FROM ledger_unsafe_event
62
-        WHERE
63
-            del_flag = '0'
64
-            AND record_date BETWEEN #{startDate} AND #{endDate}
65
-            <!-- 三级联动筛选 -->
66
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
67
-            <if test="teamId != null">AND team_id = #{teamId}</if>
68
-            <if test="groupId != null">AND group_id = #{groupId}</if>
62
+        <where>
63
+            <include refid="FILTER_CRITERIA_SQL"/>
69 64
             <!-- 过滤空物品脏数据 -->
70 65
             AND involved_items IS NOT NULL AND involved_items != ''
66
+        </where>
71 67
         GROUP BY involved_items
72 68
         ORDER BY itemNum DESC
73 69
     </select>
@@ -75,15 +71,11 @@
75 71
     <select id="countSeizureStatsType" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.UnsafeTypeVO">
76 72
         SELECT event_type AS eventType, COUNT(*) AS eventTypeNum
77 73
         FROM ledger_unsafe_event
78
-        WHERE
79
-            del_flag = '0'
80
-            AND record_date BETWEEN #{startDate} AND #{endDate}
81
-            <!-- 三级联动筛选 -->
82
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
83
-            <if test="teamId != null">AND team_id = #{teamId}</if>
84
-            <if test="groupId != null">AND group_id = #{groupId}</if>
74
+        <where>
75
+            <include refid="FILTER_CRITERIA_SQL"/>
85 76
             <!-- 过滤空类型脏数据 -->
86 77
             AND event_type IS NOT NULL AND event_type != ''
78
+        </where>
87 79
         GROUP BY event_type
88 80
         ORDER BY typeNum DESC
89 81
     </select>
@@ -91,16 +83,21 @@
91 83
     <select id="countSeizureStatsPost" parameterType="com.sundot.airport.ledger.domain.vo.CountQueryReqVO" resultType="com.sundot.airport.ledger.domain.vo.UnsafePositionVO">
92 84
         SELECT position_id AS positionId, position AS positionName, COUNT(*) AS positionNum
93 85
         FROM ledger_unsafe_event
94
-        WHERE
95
-            del_flag = '0'
96
-            AND record_date BETWEEN #{startDate} AND #{endDate}
97
-            <!-- 三级联动筛选 -->
98
-            <if test="deptId != null">AND dept_id = #{deptId}</if>
99
-            <if test="teamId != null">AND team_id = #{teamId}</if>
100
-            <if test="groupId != null">AND group_id = #{groupId}</if>
86
+        <where>
87
+            <include refid="FILTER_CRITERIA_SQL"/>
101 88
             <!-- 过滤空岗位脏数据 -->
102 89
             AND position IS NOT NULL AND position != ''
90
+        </where>
103 91
         GROUP BY position_id, position
104 92
         ORDER BY positionNum DESC
105 93
     </select>
94
+    
95
+    <sql id="FILTER_CRITERIA_SQL">
96
+        del_flag = '0'
97
+        AND record_date BETWEEN #{startDate} AND #{endDate}
98
+        <!-- 三级联动筛选 -->
99
+        <if test="deptId != null">AND dept_id = #{deptId}</if>
100
+        <if test="teamId != null">AND team_id = #{teamId}</if>
101
+        <if test="groupId != null">AND group_id = #{groupId}</if>
102
+    </sql>
106 103
 </mapper>