Administrator
2023-08-12 ee89d67599e85222b1df11df6601ab5658475052
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package com.ruoyi.app.patrolplan.service.impl;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWell;
import com.ruoyi.alarmpoints.well.mapper.ArdAlarmpointsWellMapper;
import com.ruoyi.app.patrolplan.domain.ArdAppPatrolplan;
import com.ruoyi.app.patrolplan.domain.ArdAppPatrolpointRecordImg;
import com.ruoyi.app.patrolplan.domain.ArdAppPatroluser;
import com.ruoyi.app.patrolplan.domain.param.ArdAppPatrolpointRecordParam;
import com.ruoyi.app.patrolplan.mapper.ArdAppPatrolplanMapper;
import com.ruoyi.app.patrolplan.mapper.ArdAppPatrolpointRecordImgMapper;
import com.ruoyi.app.patrolplan.mapper.ArdAppPatroluserMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.utils.result.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.app.patrolplan.mapper.ArdAppPatrolpointRecordMapper;
import com.ruoyi.app.patrolplan.domain.ArdAppPatrolpointRecord;
import com.ruoyi.app.patrolplan.service.IArdAppPatrolpointRecordService;
 
import javax.annotation.Resource;
import javax.xml.crypto.Data;
 
/**
 * app巡检计划记录Service业务层处理
 *
 * @author ard
 * @date 2023-08-02
 */
@Service
public class ArdAppPatrolpointRecordServiceImpl implements IArdAppPatrolpointRecordService {
    @Resource
    private ArdAppPatrolpointRecordMapper ardAppPatrolpointRecordMapper;
 
    @Resource
    private ArdAppPatrolpointRecordImgMapper recordImgMapper;
 
    @Resource
    private ArdAppPatrolplanMapper patrolplanMapper;
 
    @Resource
    private ArdAppPatroluserMapper userMapper;
 
    @Resource
    private SysUserMapper sysUserMapper;
 
    @Resource
    private ArdAlarmpointsWellMapper wellMapper;
 
    /**
     * 查询app巡检计划记录
     *
     * @param id app巡检计划记录主键
     * @return app巡检计划记录
     */
    @Override
    public ArdAppPatrolpointRecord selectArdAppPatrolpointRecordById(String id) {
        return ardAppPatrolpointRecordMapper.selectArdAppPatrolpointRecordById(id);
    }
 
    /**
     * 查询app巡检计划记录列表
     *
     * @param ardAppPatrolpointRecord app巡检计划记录
     * @return app巡检计划记录
     */
    @Override
    public List<ArdAppPatrolpointRecord> selectArdAppPatrolpointRecordList(ArdAppPatrolpointRecord ardAppPatrolpointRecord) {
        return ardAppPatrolpointRecordMapper.selectArdAppPatrolpointRecordList(ardAppPatrolpointRecord);
    }
 
    /**
     * 新增app巡检计划记录
     *
     * @param ardAppPatrolpointRecord app巡检计划记录
     * @return 结果
     */
    @Override
    public int insertArdAppPatrolpointRecord(ArdAppPatrolpointRecord ardAppPatrolpointRecord) {
                ardAppPatrolpointRecord.setUserId(SecurityUtils.getUserId());
            return ardAppPatrolpointRecordMapper.insertArdAppPatrolpointRecord(ardAppPatrolpointRecord);
    }
 
    /**
     * 修改app巡检计划记录
     *
     * @param ardAppPatrolpointRecord app巡检计划记录
     * @return 结果
     */
    @Override
    public int updateArdAppPatrolpointRecord(ArdAppPatrolpointRecord ardAppPatrolpointRecord) {
        return ardAppPatrolpointRecordMapper.updateArdAppPatrolpointRecord(ardAppPatrolpointRecord);
    }
 
    /**
     * 批量删除app巡检计划记录
     *
     * @param ids 需要删除的app巡检计划记录主键
     * @return 结果
     */
    @Override
    public int deleteArdAppPatrolpointRecordByIds(String[] ids) {
        return ardAppPatrolpointRecordMapper.deleteArdAppPatrolpointRecordByIds(ids);
    }
 
    /**
     * 删除app巡检计划记录信息
     *
     * @param id app巡检计划记录主键
     * @return 结果
     */
    @Override
    public int deleteArdAppPatrolpointRecordById(String id) {
        return ardAppPatrolpointRecordMapper.deleteArdAppPatrolpointRecordById(id);
    }
 
    @Override
    public Results record(ArdAppPatrolpointRecord ardAppPatrolpointRecord) {
        String id = IdUtils.simpleUUID();
        ardAppPatrolpointRecord.setId(id);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //查询该用户今天的打卡数据
        QueryWrapper<ArdAppPatrolpointRecord> recordQueryWrapper = new QueryWrapper<>();
        recordQueryWrapper.eq("plan_id", ardAppPatrolpointRecord.getPlanId())
                .like("record_time", dateFormat.format(new Date()))
                .eq("user_id",ardAppPatrolpointRecord.getUserId());
        List<ArdAppPatrolpointRecord> recordList = ardAppPatrolpointRecordMapper.selectList(recordQueryWrapper);
        ardAppPatrolpointRecord.setPointsNum(recordList.size()+1);
        int num = ardAppPatrolpointRecordMapper.insert(ardAppPatrolpointRecord);
        List<String> list = ardAppPatrolpointRecord.getImg();
        for (int i = 0; i < list.size(); i++) {
            ArdAppPatrolpointRecordImg recordImg = new ArdAppPatrolpointRecordImg();
            String imgId = IdUtils.simpleUUID();
            recordImg.setId(imgId);
            recordImg.setAppPatrolponitRecordId(id);
            recordImg.setImg(list.get(i));
            recordImgMapper.insert(recordImg);
        }
        if(num>0){
            return Results.succeed("打卡成功!");
        }else {
            return Results.error("打开失败!");
        }
    }
 
    @Override
    public Results recordMonth(ArdAppPatrolpointRecordParam ardAppPatrolpointRecordParam) throws ParseException {
        //获取前端参数
        String begin = ardAppPatrolpointRecordParam.getBegin();
        String end = ardAppPatrolpointRecordParam.getEnd();
        String planId = ardAppPatrolpointRecordParam.getPlanId();
        //查询计划表
        ArdAppPatrolplan ardAppPatrolplan = patrolplanMapper.selectById(planId);
        //查询出有效打卡数据
        QueryWrapper<ArdAppPatrolpointRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("record_time",begin,end).eq("plan_id",planId);
        List<ArdAppPatrolpointRecord> list = ardAppPatrolpointRecordMapper.selectList(queryWrapper);
        //时间拆分
        String[] parts = end.split("[-\\s:]");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]);
        int day = Integer.parseInt(parts[2]);
        String monthZero;
        if(month<10){
            monthZero = String.format("%02d", month);
        }else {
            monthZero = String.valueOf(month);
        }
        JSONArray jsonArray = new JSONArray();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 1; i <= day; i++) {
            String di;
            if(i<10){
                di = String.format("%02d", i);
            }else {
                di = String.valueOf(i);
            }
            JSONObject jsonObject = new JSONObject();
            Boolean planBoolean = false;
            //循环每一天
            String ymd = year+"-"+monthZero+"-"+di;
//            Date ymdDate = dateFormat.parse(ymd);
            //匹配计划时间
            String cycle = ardAppPatrolplan.getCycle();
            if(cycle.equals("day")){
                String createTime = ardAppPatrolplan.getCreateTime();
//                Date beginCreateDate = dateFormat.parse(createTime);
                int d = ymd.compareTo(createTime);
                if(d>=0){
                    planBoolean = true;
                }
            }else if(cycle.equals("once")){
                String onceBegin = ardAppPatrolplan.getPatroBeginTime();
//                Date onceBeginDate = dateFormat.parse(onceBegin);
                String onceEnd = ardAppPatrolplan.getPatroEndTime();
//                Date onceEndDate = dateFormat.parse(onceEnd);
                int bd = ymd.compareTo(onceBegin);
                int ed = ymd.compareTo(onceEnd);
                if(bd>=0 && ed<=0){
                    planBoolean = true;
                }
            }
            //匹配打卡时间
            Boolean or = false;
            if(planBoolean){
                for (int j = 0; j < list.size(); j++) {
                    //打卡时间拆分
                    String recordTime = list.get(j).getRecordTime();
                    String[] part = recordTime.split("[-\\s:]");
                    int recordYear = Integer.parseInt(part[0]);
                    int recordMonth = Integer.parseInt(part[1]);
                    String recordMonthZero;
                    if(recordMonth<10){
                        recordMonthZero = String.format("%02d", recordMonth);
                    }else {
                        recordMonthZero = String.valueOf(recordMonth);
                    }
                    int recordDay = Integer.parseInt(part[2]);
                    String recordDayZero;
                    if(recordDay<10){
                        recordDayZero = String.format("%02d", recordDay);
                    }else {
                        recordDayZero = String.valueOf(recordDay);
                    }
                    String recordDate = recordYear+"-"+recordMonthZero+"-"+recordDayZero;
                    //如果相等说明这天有打卡记录记录
                    if(ymd.equals(recordDate)){
                        or = true;
                        break;
                    }
                }
            }
            jsonObject.put("toDay",ymd );
            jsonObject.put("plan",planBoolean);
            jsonObject.put("record",or);
            jsonArray.add(jsonObject);
        }
        return Results.succeed(jsonArray);
    }
 
    @Override
    public Results recordDetails(ArdAppPatrolpointRecordParam ardAppPatrolpointRecordParam) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormatTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //获取参数
        String planId = ardAppPatrolpointRecordParam.getPlanId();
        String toDay = ardAppPatrolpointRecordParam.getToDay();
        //查询计划表
        ArdAppPatrolplan ardAppPatrolplan = patrolplanMapper.selectById(planId);
        String cycle = ardAppPatrolplan.getCycle();
        String begin = ardAppPatrolplan.getPatroBeginTime();
        String end = ardAppPatrolplan.getPatroEndTime();
        String create = ardAppPatrolplan.getCreateTime();
        JSONArray jsonArray = new JSONArray();
        int c = 0;
        if(cycle.equals("day")){
            //每天计划取创建时间
            c = toDay.compareTo(create.substring(0,10));
        }else if(cycle.equals("once")){
            //单次计划取开始时间
            c = toDay.compareTo(begin.substring(0,10));
        }
        if(cycle.equals("once") && c>0){
            //单次计划大于开始时间==跨天,在计划时间外
            return Results.succeed("[]");
        }
        if(c<0){
            //每日计划小于创建时间或单次计划小于开始时间,在计划时间外
            return Results.succeed("[]");
        }else{
            //在每日计划的时间范围内
            String date = dateFormat.format(new Date());
            //比较选中的日期和今天日期
            int d = toDay.compareTo(date);
            //查询该计划的所有人员
            List<SysUser> users = sysUserMapper.userByPlanId(planId);
            //大于0代表查询以后的记录
            if(d>0){
                //查询计划内的每日未来数据
                for (int i = 0; i < users.size(); i++) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("userId",users.get(i).getUserId());
                    jsonObject.put("userName",users.get(i).getNickName());
                    jsonObject.put("type",false);
                    jsonArray.add(jsonObject);
                }
                return Results.succeed(jsonArray);
            }else{
                //查询计划内的实际历史打卡数据
                QueryWrapper<ArdAppPatrolpointRecord> recordQueryWrapper = new QueryWrapper<>();
                if(d<0){
                    //小于0是历史
                    recordQueryWrapper.select("max(points_num) pointsNum","user_id","user_name").eq("plan_id", planId).like("record_time", toDay).groupBy("user_id","user_name");
                }else{
                    //等于0选中的日期就是今天,需要进一步判断小于当前时分秒的记录
                    recordQueryWrapper.select("max(points_num) pointsNum","user_id","user_name").eq("plan_id", planId).le("record_time", dateFormatTime.format(new Date())).groupBy("user_id","user_name");;
                }
                List<ArdAppPatrolpointRecord> list = ardAppPatrolpointRecordMapper.selectList(recordQueryWrapper);
                //选中这天该计划下没有任何打卡记录
                if(list.size()==0){
                    for (int i = 0; i < users.size(); i++) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("userId",users.get(i).getUserId());
                        jsonObject.put("userName",users.get(i).getNickName());
                        jsonObject.put("type",false);
                        jsonArray.add(jsonObject);
                    }
                    return Results.succeed(jsonArray);
                }
                //查询该计划的所有兴趣点
                List<ArdAlarmpointsWell> wellList = wellMapper.wellByPlanId(planId);
                Integer wellNum = wellList.size();
                //取出所有打了卡的userId
                List<String> userIdList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    userIdList.add(list.get(i).getUserId());
                }
                //有打卡记录时的打卡情况查出一次都没打卡的人员
                for (int i = 0; i < users.size(); i++) {
                    String userId = users.get(i).getUserId();
                    String userName = users.get(i).getNickName();
                    //如果该计划的所有人员userId不在所有打了卡的userId中
                    if (!userIdList.contains(userId)) {
                        //记录该人员未完成打卡
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("userId", userId);
                        jsonObject.put("userName", userName);
                        jsonObject.put("type", false);
                        jsonArray.add(jsonObject);
                        continue;
                    }
                }
                //全部打完卡或打了部分卡的人员
                for (ArdAppPatrolpointRecord ardAppPatrolpointRecord : list) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("userId", ardAppPatrolpointRecord.getUserId());
                    jsonObject.put("userName", ardAppPatrolpointRecord.getUserName());
                    if (ardAppPatrolpointRecord.getPointsNum().equals(wellNum)) {
                        jsonObject.put("type", true);
                    } else {
                        jsonObject.put("type", false);
                    }
                    jsonArray.add(jsonObject);
                }
                return Results.succeed(jsonArray);
            }
        }
    }
 
    @Override
    public Results recordSolo(ArdAppPatrolpointRecordParam ardAppPatrolpointRecordParam) {
        String toDay = ardAppPatrolpointRecordParam.getToDay();
        String planId = ardAppPatrolpointRecordParam.getPlanId();
        String userId = ardAppPatrolpointRecordParam.getUserId();
        QueryWrapper<ArdAppPatrolpointRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", planId).eq("user_id",userId).like("record_time", toDay);
        //当前计划选中时间里此用户的打卡记录
        List<ArdAppPatrolpointRecord> list = ardAppPatrolpointRecordMapper.selectList(queryWrapper);
        for (ArdAppPatrolpointRecord appPatrolpointRecord : list) {
            QueryWrapper<ArdAppPatrolpointRecordImg> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("app_patrolponit_record_id", appPatrolpointRecord.getId());
            List<ArdAppPatrolpointRecordImg> list1 = recordImgMapper.selectList(queryWrapper1);
            List<String> strings = new ArrayList<>();
            for (ArdAppPatrolpointRecordImg ardAppPatrolpointRecordImg : list1) {
                strings.add(ardAppPatrolpointRecordImg.getImg());
            }
            appPatrolpointRecord.setImg(strings);
        }
        //打过卡的地点
        List<String> pointList = new ArrayList<>();
        for (ArdAppPatrolpointRecord appPatrolpointRecord : list) {
            pointList.add(appPatrolpointRecord.getAppPatrolpointsId());
        }
        //查询该计划的所有兴趣点
        List<ArdAlarmpointsWell> wellList = wellMapper.wellByPlanId(planId);
        for (ArdAlarmpointsWell ardAlarmpointsWell : wellList) {
            //所有应该打卡的地点ID
            String well = ardAlarmpointsWell.getId();
            //如果打开地点不包含应打卡地点,那说名这块儿没打卡
            if (!pointList.contains(well)) {
                ArdAppPatrolpointRecord ardAppPatrolpointRecord = new ArdAppPatrolpointRecord();
                ardAppPatrolpointRecord.setAppPatrolpointsId(well);
                ardAppPatrolpointRecord.setAppPatrolpointsName(ardAlarmpointsWell.getWellId());
                ardAppPatrolpointRecord.setPlanId(planId);
                ardAppPatrolpointRecord.setPlanName(list.get(0).getPlanName());
                ardAppPatrolpointRecord.setUserId(userId);
                ardAppPatrolpointRecord.setUserName(list.get(0).getUserName());
                ardAppPatrolpointRecord.setLatitude(ardAlarmpointsWell.getLatitude().toString());
                ardAppPatrolpointRecord.setLongitude(ardAlarmpointsWell.getLongitude().toString());
                list.add(ardAppPatrolpointRecord);
            }
        }
        return Results.succeed(list);
    }
 
}