liujie
2026-01-20 b506a0a10cdf3ac34cfdae2b82da4dae49b41567
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
package com.estate.expert.admin.service.impl;
 
import com.estate.expert.admin.domain.common.request.PageReq;
import com.estate.expert.admin.domain.common.response.PageResp;
import com.estate.expert.admin.domain.exam.task.check.CheckTaskRepository;
import com.estate.expert.admin.domain.exam.task.check.CheckTaskState;
import com.estate.expert.admin.domain.plan.*;
import com.estate.expert.admin.domain.user.User;
import com.estate.expert.admin.enums.BizResultCode;
import com.estate.expert.admin.service.PlanService;
import com.estate.expert.admin.service.common.CrudServiceBaseImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * 计划服务实现
 */
@Slf4j
@Service
public class PlanServiceImpl extends CrudServiceBaseImpl<Plan, PlanDto> implements PlanService {
 
    @Resource
    private PlanRepository planRepository;
 
    @Resource
    private CheckTaskRepository checkTaskRepository;
 
    @Override
    public void updateState(Integer id) {
        var plan = get(id);
        if (plan == null) {
            return;
        }
        var checkTasks = checkTaskRepository.findAllByPlanIdAndDeleted(id, false);
        // 新建计划,无任务,无需更新状态
        if (CollectionUtils.isEmpty(checkTasks) && plan.getState() == PlanState.PUBLISHED) {
            return;
        } else {
            // 有任务未完成
            var executing = checkTasks.stream().anyMatch(task ->
                    task.getState() != CheckTaskState.Finished && task.getState() != CheckTaskState.Reviewed);
            if (executing) {
                plan.setState(PlanState.EXECUTING);
            } else {
                plan.setState(PlanState.COMPLETE);
                plan.setCompletedTime(LocalDateTime.now());
            }
            planRepository.save(plan);
        }
    }
 
    public PageResp<PlanDto> list(String keyword, List<PlanType> planTypes, List<PlanState> planStates,
                                  List<Integer> clientIds, Integer creatorId,
                                  PageReq pageReq) {
        var page = planRepository.findPlans(keyword,
                CollectionUtils.isEmpty(planTypes) ? null : planTypes, 
                CollectionUtils.isEmpty(planStates) ? null : planStates,
                CollectionUtils.isEmpty(clientIds) ? null : clientIds,
                creatorId,
                null,
                pageReq.toPageRequest());
        return PageResp.of(page, toDto(page.getContent()));
    }
 
    public List<Plan> list(Integer clientId) {
        return planRepository.findAllByClientId(clientId);
    }
 
    @Override
    public List<Plan> list(String keyword, List<PlanType> planType, List<PlanState> planStates, List<Integer> clientIds, Integer creatorId) {
        return planRepository.findPlans(keyword,
                CollectionUtils.isEmpty(planType) ? null : planType,
                CollectionUtils.isEmpty(planStates) ? null : planStates,
                CollectionUtils.isEmpty(clientIds) ? null : clientIds,
                creatorId,
                null);
    }
 
    @Override
    public PageResp<PlanDto> myPlans(Integer userId, String keyword, List<PlanType> planTypes, List<PlanState> planStates, List<Integer> clientIds,  PageReq pageReq) {
        var myPlanIds = checkTaskRepository.listMyTaskPlans(userId).stream().distinct().toList();
 
        if(CollectionUtils.isEmpty(myPlanIds)) {
            return PageResp.empty();
        }
        var page = planRepository.myPlans(keyword,
                CollectionUtils.isEmpty(planTypes) ? null : planTypes,
                CollectionUtils.isEmpty(planStates) ? null : planStates,
                CollectionUtils.isEmpty(clientIds) ? null : clientIds,
                userId,
                myPlanIds,
                pageReq.toPageRequest());
        return PageResp.of(page, toDto(page.getContent()));
    }
 
    @Override
    public Plan add(User creator, Plan entity) {
        entity.setState(PlanState.PUBLISHED);
        BizResultCode.PLAN_SYSTEM_MODEL_NOT_EXIST.check(entity.getSystemModel() == null);
        BizResultCode.PLAN_DEVICE_MODEL_NOT_EXIST.check(entity.getDeviceModel() == null);
        BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getSystemModel().equals(entity.getSystemFuzzyExpert().getModel()));
        BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getSystemModel().equals(entity.getSystemScoreExpert().getModel()));
        BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getDeviceModel().equals(entity.getDeviceFuzzyExpert().getModel()));
        BizResultCode.PlAN_MODEL_EXPERT_SHOULD_PAIRED.check(!entity.getDeviceModel().equals(entity.getDeviceScoreExpert().getModel()));
        return super.add(creator, entity);
    }
}