xuhy
1 天以前 e7a7d37268b369eeaf1958c1c8b9c192f73f0d02
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
package com.ruoyi.web.controller.task;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.CodeGenerateUtils;
import com.ruoyi.system.mapper.TCleanerMapper;
import com.ruoyi.system.model.*;
import com.ruoyi.system.service.*;
import javafx.concurrent.Task;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author xiaochen
 * @date 2025/6/11 18:39
 */
@Slf4j
@Component
public class TaskUtil {
 
    // 项目部权重标识
    private static final String PROJECT_DEPT_WEIGHT = ":PROJECT_DEPT_WEIGHT";
    // 保洁员权重标识
    private static final String CLEANER_WEIGHT = ":CLEANER_WEIGHT";
    // 重复点位标识
    private static final String REPEAT_LOCATION = ":REPEAT_LOCATION";
 
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TTemplateService templateService;
    @Autowired
    private TTemplateDetailService templateDetailService;
    @Autowired
    private TProjectDeptService projectDeptService;
    @Autowired
    private TCleanerService cleanerService;
    @Autowired
    private TLocationService locationService;
    @Autowired
    private TLocationTypeService locationTypeService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private TTaskCleanService taskCleanService;
    @Autowired
    private TTemplateCountService templateCountService;
    @Autowired
    private TEarlyWarningService earlyWarningService;
 
 
    // 每一个小时执行一次
//    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(fixedRate = 1500000000)
    public void dayOfCreateInspection() {
        try {
 
            // 查询任务模板
            List<TTemplate> list = templateService.list();
            if (CollectionUtils.isEmpty(list)) {
                return;
            }
            list.forEach(template -> {
                // 创建任务
                createInspection(template);
            });
 
            // 查询前一天为执行的所有任务,改成已超时状态
            List<TTask> taskCleanList = taskCleanService.list(Wrappers.lambdaQuery(TTask.class)
                    .like(TTask::getImplementTime, LocalDate.now().minusDays(1))
                    .eq(TTask::getStatus, 1));
            if (!CollectionUtils.isEmpty(taskCleanList)) {
                taskCleanList.forEach(task -> {
                    task.setStatus(2);
                });
                taskCleanService.updateBatchById(taskCleanList);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Scheduled(cron = "0 0 20 * * ?")
    public void dayOfEarlyWarning() {
        try {
            // 查询前一天为执行的所有任务,改成已超时状态
            List<TTask> taskCleanList = taskCleanService.list(Wrappers.lambdaQuery(TTask.class)
                    .like(TTask::getImplementTime, LocalDate.now().minusDays(1))
                    .eq(TTask::getStatus, 1));
            if (!CollectionUtils.isEmpty(taskCleanList)) {
                List<TEarlyWarning> earlyWarningList = new ArrayList<>();
                taskCleanList.forEach(task -> {
                    task.setStatus(2);
                    TEarlyWarning earlyWarning = new TEarlyWarning();
                    earlyWarning.setWarningType(1);
                    earlyWarning.setTaskId(task.getId());
                    earlyWarningList.add(earlyWarning);
                });
                earlyWarningService.saveBatch(earlyWarningList);
                taskCleanService.updateBatchById(taskCleanList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void createInspection(TTemplate template) {
        // 查询所有的模板详情
        List<TTemplateDetail> list = templateDetailService.list(Wrappers.lambdaQuery(TTemplateDetail.class)
                .eq(TTemplateDetail::getTemplateId, template.getId()));
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        // 通过模板id查询员工巡检员
        List<SysUser> sysUsers = sysUserService.selectUserByTempLateId(template.getId());
        // 创建任务
        for (TTemplateDetail detail : list) {
            // 计算周期
            int cycle = detail.getCycle();
            switch (detail.getCycleType()){
                case 2:
                    cycle = cycle * 7;
                    break;
                case 3:
                    cycle = cycle * 30;
                    break;
                case 4:
                    cycle = cycle * 90;
                    break;
                case 5:
                    cycle = cycle * 365;
                    break;
            }
 
            // 拿到保洁抽查次数
            int num1 = detail.getNum1();
            // 拿到项目部数
            int num2 = detail.getNum2();
            // 拿到每日重复点位
            int num3 = detail.getNum3();
            // 获取点位类型的占比
            String num4 = detail.getNum4();
 
            // 未绑定员工
            if(CollectionUtils.isEmpty(sysUsers)){
                continue;
            }
 
            int taskCount = 0;
            for (SysUser sysUser : sysUsers) {
                List<TProjectDept> projectDeptLists = new ArrayList<>();
                if(sysUser.getDeptType() == 1){
                    TProjectDept projectDept = projectDeptService.getById(sysUser.getDeptId());
                    if("0".equals(projectDept.getParentId())){
                        projectDeptLists = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
                                .eq(TProjectDept::getParentId, projectDept.getId()));
                    }else {
                        projectDeptLists.add(projectDept);
                    }
                }else {
                    projectDeptLists = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
                            .ne(TProjectDept::getParentId,0));
                }
                List<String> proDeptIds = projectDeptLists.stream().map(TProjectDept::getId).collect(Collectors.toList());
 
                // 获取项目部在该模板详情中的权重
                List<String> projectDeptIds = redisCache.getCacheList(detail.getId() + PROJECT_DEPT_WEIGHT);
                // 获取项目部列表
                List<TProjectDept> projectDeptList;
                if(CollectionUtils.isEmpty(projectDeptIds)){
                    projectDeptList = projectDeptLists;
                }else {
                    projectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
                            .ne(TProjectDept::getParentId,0)
                            .in(TProjectDept::getId, proDeptIds)
                            .notIn(TProjectDept::getId, projectDeptIds));
                    // 所过所有的项目部都被抽取了,则重新抽取,并且清空项目部权重
                    if(CollectionUtils.isEmpty(projectDeptList)){
                        projectDeptList = projectDeptLists;
                        redisCache.deleteObject(detail.getId() + PROJECT_DEPT_WEIGHT);
                    }
                }
                // 如果可抽取的项目部数不足,先抽取余下项目部后,再清空权重,重新抽取
                List<TProjectDept> projectDepts = randomSelection(projectDeptList, num2);
                if(projectDepts.size() < num2){
                    List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
                    List<TProjectDept> projectDeptList1 = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
                            .notIn(TProjectDept::getId, proIds)
                            .ne(TProjectDept::getParentId,0));
                    redisCache.deleteObject(detail.getId() + PROJECT_DEPT_WEIGHT);
                    List<TProjectDept> projectDepts1 = randomSelection(projectDeptList1, num2 - projectDepts.size());
                    List<String> proIds1 = projectDepts1.stream().map(TProjectDept::getId).collect(Collectors.toList());
                    // 将已抽取的项目部id保存到redis中
                    redisCache.setCacheList(detail.getId() + PROJECT_DEPT_WEIGHT, proIds1);
                    projectDepts.addAll(projectDepts1);
                }else {
                    List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
                    // 将已抽取的项目部id保存到redis中
                    redisCache.setCacheList(detail.getId() + PROJECT_DEPT_WEIGHT, proIds);
                }
                List<String> proIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
 
                // 拿到抽取的项目部下的所有保洁员
//            List<TProjectDept> tProjectDeptList = projectDeptService.list(Wrappers.lambdaQuery(TProjectDept.class)
//                    .in(TProjectDept::getId, proIds));
                // 获取片区id
                List<String> areaIds = projectDepts.stream().map(TProjectDept::getId).collect(Collectors.toList());
                if(CollectionUtils.isEmpty(areaIds)){
                    return;
                }
 
                // 计算每天需要抽取多少个保洁员
                long count = cleanerService.count(Wrappers.lambdaQuery(TCleaner.class)
                        .in(TCleaner::getProjectId, areaIds));
                int cleanerSums = num1 * Integer.parseInt(count + "");
                // 获取每天需要抽取的保洁员数,向上取整
                if (cleanerSums < cycle){
                    cleanerSums = cycle;
                }
                int dayCleanerCount = cleanerSums / cycle;
 
                if((cycle == detail.getCurrentValue()) && cleanerSums % cycle != 0){
                    dayCleanerCount++;
                }
 
                // 获取保洁员权重
                List<String> cleanerIds = redisCache.getCacheList(detail.getId() + CLEANER_WEIGHT);
                // 获取保洁员列表
                List<TCleaner> cleaners;
                if(CollectionUtils.isEmpty(cleanerIds)){
                    cleaners = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                            .in(TCleaner::getProjectId, areaIds));
                }else {
                    cleaners = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                            .in(TCleaner::getProjectId, areaIds)
                            .notIn(TCleaner::getId, cleanerIds));
                    if(CollectionUtils.isEmpty(cleaners)){
                        cleaners = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                                .in(TCleaner::getProjectId, areaIds));
                        redisCache.deleteObject(detail.getId() + CLEANER_WEIGHT);
                    }
                }
 
                // 抽取保洁员
                List<TCleaner> tCleaners = randomSelection(cleaners, dayCleanerCount);
                if(CollectionUtils.isEmpty(tCleaners)){
                    log.error("没有可抽取的保洁员,模板id为:{}",detail.getId());
                    continue;
                }
                if(tCleaners.size() < dayCleanerCount){
                    List<String> cleanIds = tCleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                    List<TCleaner> cleaners1 = cleanerService.list(Wrappers.lambdaQuery(TCleaner.class)
                            .in(TCleaner::getProjectId, areaIds)
                            .notIn(TCleaner::getId, cleanIds));
                    redisCache.deleteObject(detail.getId() + CLEANER_WEIGHT);
                    List<TCleaner> tCleaners1 = randomSelection(cleaners1, dayCleanerCount - tCleaners.size());
                    List<String> cleanIds1 = tCleaners1.stream().map(TCleaner::getId).collect(Collectors.toList());
                    // 将已抽取的保洁员id保存到redis中
                    cleanIds1.addAll(cleanIds);
                    redisCache.setCacheList(detail.getId() + CLEANER_WEIGHT, cleanIds1);
                    tCleaners.addAll(tCleaners1);
                }else {
                    List<String> cleanIds = tCleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                    redisCache.setCacheList(detail.getId() + CLEANER_WEIGHT, cleanIds);
                }
 
                // 通过保洁员id查询点位
                List<String> cleanersIds = tCleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                List<TLocation> locationList = locationService.list(Wrappers.lambdaQuery(TLocation.class)
                        .in(TLocation::getLocationCleaner, cleanersIds));
                // 查询点位类型
                JSONArray jsonArray = JSONObject.parseArray(num4);
                List<TLocation> tLocationList = new ArrayList<>();
                for (Object o : jsonArray) {
                    JSONObject jsonObject = JSONObject.parseObject(o.toString());
                    String id = jsonObject.getString("id");
                    BigDecimal value = jsonObject.getBigDecimal("value");
                    List<TLocation> locations = locationList.stream().filter(tLocation -> tLocation.getLocationType().equals(id)).collect(Collectors.toList());
                    if(!CollectionUtils.isEmpty(locations)){
                        BigDecimal bigDecimal = new BigDecimal(locations.size()).multiply(value.divide(new BigDecimal(100), 2, BigDecimal.ROUND_DOWN)).setScale(0, BigDecimal.ROUND_UP);
                        int locationCount = bigDecimal.intValue();
                        if(locationCount > 0){
                            List<TLocation> tLocations = randomSelection(locations, locationCount);
                            tLocationList.addAll(tLocations);
                        }
                    }
                }
                tLocationList = tLocationList.stream().distinct().collect(Collectors.toList());
                // 抽取重复点位
                Integer currentValue = detail.getCurrentValue();
                if(currentValue != cycle){
                    // 周期天数加一
                    detail.setCurrentValue(currentValue + 1);
                    // 获取重复点位
                    Set<TLocation> repeatLocation = redisCache.getCacheSet(detail.getId() + ":" + sysUser.getUserId() + REPEAT_LOCATION);
                    if(!CollectionUtils.isEmpty(repeatLocation)){
                        List<TLocation> locations = randomSelection(new ArrayList<>(tLocationList), num3);
                        tLocationList.addAll(locations);
                    }
                    Set<TLocation> locationSet = new HashSet<>(tLocationList);
                    redisCache.setCacheSet(detail.getId() + ":" + sysUser.getUserId() + REPEAT_LOCATION, locationSet);
 
                }else {
                    // 设置当前周期为0
                    detail.setCurrentValue(0);
                    // 将重复点位置空
                    redisCache.deleteObject(detail.getId() + ":" + sysUser.getUserId() + REPEAT_LOCATION);
                    // 将项目部权重置空
                    redisCache.deleteObject(detail.getId() + PROJECT_DEPT_WEIGHT);
                    // 将保洁员权重置空
                    redisCache.deleteObject(detail.getId() + CLEANER_WEIGHT);
                }
 
                // 创建任务
                List<TTask> tasks = new ArrayList<>();
                for (TLocation tLocation : tLocationList) {
                    TTask task = new TTask();
                    // 获取保洁员
                    tCleaners.stream().filter(tCleaner -> tCleaner.getId().equals(tLocation.getLocationCleaner())).findFirst().ifPresent(tCleaner -> {
                        task.setProjectId(tCleaner.getProjectId());
                        task.setCleanerId(tCleaner.getId());
                    });
                    // 获取巡检员
                    task.setPatrolInspector(sysUser.getUserId().toString());
                    task.setPatrolInspectorDept(sysUser.getDeptId());
                    task.setUserId(sysUser.getUserId());
                    task.setStatus(1);
                    task.setLocationId(tLocation.getId());
                    task.setImplementTime(LocalDateTime.now().plusDays(1));
                    task.setTaskType(1);
                    task.setTemplateId(detail.getTemplateId());
                    String nameAndCode = CodeGenerateUtils.generateVolumeSn();
                    task.setTaskName(nameAndCode);
                    task.setTaskCode(nameAndCode);
                    tasks.add(task);
                }
 
                // 添加应生成任务数量
                TTemplateCount templateCount = new TTemplateCount();
                templateCount.setTemplateId(detail.getTemplateId());
                // 查询所有的保洁员下面的点位
                List<String> cleanIds = cleaners.stream().map(TCleaner::getId).collect(Collectors.toList());
                List<TLocation> tLocations = locationService.list(Wrappers.lambdaQuery(TLocation.class)
                        .in(TLocation::getLocationCleaner, cleanIds));
                templateCount.setTaskCount(tLocations.size());
                templateCount.setUserId(sysUser.getUserId());
                templateCountService.save(templateCount);
                taskCleanService.saveBatch(tasks);
                taskCount = taskCount + tasks.size();
            }
            template.setTaskCount(taskCount);
            templateService.updateById(template);
            templateDetailService.updateById(detail);
        }
    }
 
    /**
     * 从集合中随机抽取数量
     * @param list
     * @param size
     * @return
     * @param <T>
     */
    private static <T> List<T> randomSelection(List<T> list, int size) {
        List<T> selected = new ArrayList<>();
        int length = list.size();
        for (int i = 0; i < size; i++) {
            Collections.shuffle(list);
            // 如果需求的大小超过了集合的长度,则只能取到集合的全部元素
            int subListSize = Math.min(size - selected.size(), length);
            selected.addAll(list.subList(0, subListSize));
        }
        return selected;
    }
 
}