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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?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.thhy.secure.modules.biz.regionInspection.mapper.RegionInspectionRecordMapper">
 
    <insert id="insertIMG">
        insert into t_region_inspection_record_img
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="regionInspectionRecordId != null">
                regionInspectionRecordId,
            </if>
            <if test="imgName != null">
                imgName,
            </if>
            <if test="imgPath != null">
                imgPath,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=VARCHAR},
            </if>
            <if test="regionInspectionRecordId != null">
                #{regionInspectionRecordId,jdbcType=VARCHAR},
            </if>
            <if test="imgName != null">
                #{imgName,jdbcType=VARCHAR},
            </if>
            <if test="imgPath != null">
                #{imgPath,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <insert id="insert">
        insert into t_region_inspection_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="regionInspectionId != null">
                regionInspectionId,
            </if>
            <if test="userId != null">
                userId,
            </if>
            <if test="regionHazardInformId != null">
                regionHazardInformId,
            </if>
            <if test="status != null">
                `status`,
            </if>
            <if test="location != null">
                location,
            </if>
            <if test="inspectionTime != null">
                inspectionTime,
            </if>
            <if test="createUser != null">
                createUser,
            </if>
            <if test="createTime != null">
                createTime,
            </if>
            <if test="updateUser != null">
                updateUser,
            </if>
            <if test="updateTime != null">
                updateTime,
            </if>
            <if test="result != null">
                result,
            </if>
 
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=VARCHAR},
            </if>
            <if test="regionInspectionId != null">
                #{regionInspectionId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=VARCHAR},
            </if>
            <if test="regionHazardInformId != null">
                #{regionHazardInformId,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                #{status,jdbcType=INTEGER},
            </if>
            <if test="location != null">
                #{location,jdbcType=VARCHAR},
            </if>
            <if test="inspectionTime != null">
                #{inspectionTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createUser != null">
                #{createUser,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUser != null">
                #{updateUser,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="result != null">
                #{result,jdbcType=VARCHAR},
            </if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="regionInspectionId != null">
                regionInspectionId = #{regionInspectionId,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                userId = #{userId,jdbcType=VARCHAR},
            </if>
            <if test="regionHazardInformId != null">
                regionHazardInformId = #{regionHazardInformId,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                `status` = #{status,jdbcType=INTEGER},
            </if>
            <if test="location != null">
                location = #{location,jdbcType=VARCHAR},
            </if>
            <if test="inspectionTime != null">
                inspectionTime = #{inspectionTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createUser != null">
                createUser = #{createUser,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                createTime = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUser != null">
                updateUser = #{updateUser,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                updateTime = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="result != null">
                result = #{result,jdbcType=VARCHAR},
            </if>
        </trim>
  </insert>
 
    <delete id="delete">
        delete from t_region_inspection_record where  id=#{id,jdbcType=VARCHAR}
    </delete>
 
    <delete id="deleteByRegionInspectionId">
        delete from t_region_inspection_record where  regionInspectionId=#{regionInspectionId,jdbcType=VARCHAR}
    </delete>
 
    <select id="selectImgByRecordId" resultType="com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionRecordImgEntity">
        select * from t_region_inspection_record_img where regionInspectionRecordId = #{regionInspectionRecordId}
    </select>
    <select id="selectPageList" resultType="com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionRecordEntity">
        SELECT  a.userId,
                a.regionInspectionId,
                a.inspectionTime,
                a.task,
                a.region,
                a.realName,
                IF(a.completeRegionNum = a.needRegionNum,"已完成","待巡检") statusName,
                IF(a.completeRegionNum = a.needRegionNum,1,0) status
        FROM (
                     SELECT
                             t.userId ,
                             t.regionInspectionId,
                             MIN(t.inspectionTime) inspectionTime,
                             tri.task task,
                             GROUP_CONCAT(DISTINCT trhi.region) region,
                             spu.real_name realName,
                             SUM(t.`status`)/(SELECT count(regionInspectionId) FROM t_region_inspection_hazard_inform WHERE regionInspectionId =  t.regionInspectionId) completeRegionNum,
                             (SELECT count(regionInspectionId) FROM t_region_inspection_hazard_inform WHERE regionInspectionId =  t.regionInspectionId) needRegionNum
                     from
                             t_region_inspection_record t
                                     LEFT JOIN t_region_inspection tri on tri.id = t.regionInspectionId
                                     LEFT JOIN t_region_inspection_hazard_inform trihi on trihi.regionInspectionId = t.regionInspectionId
                                     LEFT JOIN t_region_hazard_inform trhi on trhi.id = trihi.regionHazardInformId
                                     LEFT JOIN sys_plat_user spu on spu.user_id = t.userId
                     WHERE 1=1
                        <if test="realName !=null and realName !=''">
                            and spu.real_name REGEXP #{realName}
                        </if>
                        <if test="companyId !=null and companyId !=''">
                            and tri.companyId = #{companyId}
                        </if>
                        <if test="regionInspectionId !=null and regionInspectionId !=''">
                            and t.regionInspectionId = #{regionInspectionId}
                        </if>
                        <if test="startTime != null and endTime != null and startTime !='' and endTime != ''">
                            and t.inspectionTime BETWEEN str_to_date(#{startTime}, "%Y-%m-%d %H:%i:%s") and str_to_date(#{endTime}, "%Y-%m-%d %H:%i:%s")
                        </if>
        GROUP BY t.regionInspectionId,t.userId
                     )a
        WHERE 1=1
        <if test="intStatus!=null and intStatus ==0">
            and a.completeRegionNum != a.needRegionNum
        </if>
        <if test="intStatus!=null and intStatus ==1">
            and a.completeRegionNum = a.needRegionNum
        </if>
        <if test="page != null and limit != null">
            limit #{page},#{limit}
        </if>
    </select>
    <select id="selectCount" resultType="java.lang.Integer">
        SELECT  count(0)
        FROM (
                SELECT
                    t.userId ,
                    t.regionInspectionId,
                    MIN(t.inspectionTime) inspectionTime,
                    tri.task task,
                    GROUP_CONCAT(DISTINCT trhi.region) region,
                    spu.real_name realName,
                    SUM(t.`status`)/(SELECT count(regionInspectionId) FROM t_region_inspection_hazard_inform WHERE regionInspectionId =  t.regionInspectionId) completeRegionNum,
                    (SELECT count(regionInspectionId) FROM t_region_inspection_hazard_inform WHERE regionInspectionId =  t.regionInspectionId) needRegionNum
                from
                t_region_inspection_record t
                LEFT JOIN t_region_inspection tri on tri.id = t.regionInspectionId
                LEFT JOIN t_region_inspection_hazard_inform trihi on trihi.regionInspectionId = t.regionInspectionId
                LEFT JOIN t_region_hazard_inform trhi on trhi.id = trihi.regionHazardInformId
                LEFT JOIN sys_plat_user spu on spu.user_id = t.userId
                WHERE 1=1
                <if test="realName !=null and realName !=''">
                    and spu.real_name REGEXP #{realName}
                </if>
                <if test="companyId !=null and companyId !=''">
                    and tri.companyId = #{companyId}
                </if>
                <if test="regionInspectionId !=null and regionInspectionId !=''">
                    and t.regionInspectionId = #{regionInspectionId}
                </if>
                <if test="startTime != null and endTime != null and startTime !='' and endTime != ''">
                    and t.inspectionTime BETWEEN str_to_date(#{startTime}, "%Y-%m-%d %H:%i:%s") and str_to_date(#{endTime}, "%Y-%m-%d %H:%i:%s")
                </if>
                GROUP BY t.regionInspectionId,t.userId
        )a
        WHERE 1=1
        <if test="intStatus!=null and intStatus ==0">
            and a.completeRegionNum != a.needRegionNum
        </if>
        <if test="intStatus!=null and intStatus ==1">
            and a.completeRegionNum = a.needRegionNum
        </if>
    </select>
    <select id="selectInfo" resultType="com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionEntity">
        SELECT
                t.id regionInspectionId,
                b.user_id userId,
                t.task,
                t.standard,
                b.real_name realName,
                GROUP_CONCAT(DISTINCT d.region ORDER BY c.sort SEPARATOR ',') regions,
                GROUP_CONCAT(DISTINCT d.id ORDER BY c.sort SEPARATOR ',') regionIds
        FROM t_region_inspection t
                     LEFT JOIN t_region_inspection_user a ON a.regionInspectionId = t.id
                     LEFT JOIN sys_plat_user b ON b.user_id = a.userId
                     LEFT JOIN t_region_inspection_hazard_inform c ON c.regionInspectionId = t.id
                     LEFT JOIN t_region_hazard_inform d ON d.id = c.regionHazardInformId
        WHERE t.id = #{regionInspectionId} and b.user_id = #{userId}
        GROUP BY t.id
    </select>
 
    <select id="regionInfo" resultType="com.thhy.secure.modules.biz.regionInspection.entity.RegionInspectionRecordEntity">
        SELECT
                t.id,
                t.userId,
                t.regionInspectionId,
                t.regionHazardInformId,
                t.inspectionTime inspectionTime,
                rhi.region region,
                t.result result,
                GROUP_CONCAT(timg.imgPath) imgPaths,
                t.location,
                spu.real_name realName
        FROM t_region_inspection_record t
                     LEFT JOIN t_region_inspection_record_img timg on t.id = timg.regionInspectionRecordId
                     LEFT JOIN t_region_hazard_inform rhi on rhi.id = t.regionHazardInformId
                     LEFT JOIN sys_plat_user spu on spu.user_id = t.userId
        WHERE t.userId = #{userId} and t.regionInspectionId = #{regionInspectionId} and t.regionHazardInformId = #{regionHazardInformId}
        GROUP BY t.id
    </select>
 
    <select id="selectAppList" resultType="java.util.Map">
        SELECT
                a.id,
                MIN( b.inspectionTime ) inspectionTime,
                a.`status`,
                a.task,
                GROUP_CONCAT(DISTINCT d.region) region
        FROM
                t_region_inspection a
                        LEFT JOIN t_region_inspection_record b ON b.regionInspectionId = a.id
                        LEFT JOIN t_region_inspection_hazard_inform c ON c.regionInspectionId = a.id
                        LEFT JOIN t_region_hazard_inform d ON c.regionHazardInformId = d.id
                        LEFT JOIN t_region_inspection_user e on e.regionInspectionId = a.id
        WHERE a.companyId = #{companyId} and  e.userId = #{userId}
        <if test="status != null and status != ''">
            and a.status = #{status}
        </if>
        <if test="startTime != null and endTime != null and startTime !='' and endTime != ''">
            and t.inspectionTime BETWEEN str_to_date(#{startTime}, "%Y-%m-%d %H:%i:%s") and str_to_date(#{endTime}, "%Y-%m-%d %H:%i:%s")
        </if>
        GROUP BY
                a.id
    </select>
 
    <select id="selectPlatUserId" resultType="java.lang.String">
        SELECT
            plat_id platId
        FROM sys_users
        WHERE user_id= #{userId,jdbcType=VARCHAR}
    </select>
    <select id="getPlatUser" resultType="java.util.Map">
        SELECT * FROM sys_plat_user where user_id = #{userId}
    </select>
 
</mapper>