gongchunyi
4 天以前 b7080587bc00e5451aa8435f093f0d8cc6f80320
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
package com.ruoyi.inspectiontask.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.inspectiontask.mapper.InspectionItemMapper;
import com.ruoyi.inspectiontask.pojo.InspectionItem;
import com.ruoyi.inspectiontask.service.InspectionItemService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Service
@RequiredArgsConstructor
public class InspectionItemServiceImpl implements InspectionItemService {
 
    private static final List<String> VALID_FREQUENCIES = List.of(
            "DAILY", "WEEKLY", "MONTHLY", "QUARTERLY", "ALL"
    );
 
    private final InspectionItemMapper inspectionItemMapper;
 
    @Override
    public List<InspectionItem> list(InspectionItem query, boolean includeCommon) {
        return inspectionItemMapper.selectList(buildQuery(query, includeCommon)
                .orderByAsc(InspectionItem::getSortNum)
                .orderByAsc(InspectionItem::getId));
    }
 
    @Override
    public Page<InspectionItem> page(long pageNum, long pageSize, boolean includeCommon, InspectionItem query) {
        long safePageNum = Math.max(pageNum, 1);
        long safePageSize = Math.min(Math.max(pageSize, 1), 100);
        return inspectionItemMapper.selectPage(new Page<>(safePageNum, safePageSize),
                buildQuery(query, includeCommon)
                        .orderByAsc(InspectionItem::getSortNum)
                        .orderByAsc(InspectionItem::getId));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(InspectionItem item) {
        validate(item);
        normalize(item);
        item.setDeleted(0);
        if (existsDuplicate(item, null)) {
            throw new ServiceException("该设备和频率下已存在同名巡检内容");
        }
        if (inspectionItemMapper.insert(item) <= 0) {
            throw new ServiceException("新增巡检内容失败");
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InspectionItem item) {
        if (item == null || item.getId() == null) {
            throw new ServiceException("巡检内容编号不能为空");
        }
        validate(item);
        InspectionItem existing = inspectionItemMapper.selectById(item.getId());
        if (existing == null || Integer.valueOf(1).equals(existing.getDeleted())) {
            throw new ServiceException("巡检内容不存在");
        }
        normalize(item);
        if (existsDuplicate(item, item.getId())) {
            throw new ServiceException("该设备和频率下已存在同名巡检内容");
        }
 
        existing.setItemProject(item.getItemProject());
        existing.setItemContent(item.getItemContent());
        existing.setDeviceLedgerId(item.getDeviceLedgerId());
        existing.setFrequencyType(item.getFrequencyType());
        existing.setSortNum(item.getSortNum());
        existing.setIsActive(item.getIsActive());
        existing.setRemarks(item.getRemarks());
        if (inspectionItemMapper.updateById(existing) <= 0) {
            throw new ServiceException("修改巡检内容失败");
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        if (id == null) {
            throw new ServiceException("巡检内容编号不能为空");
        }
        InspectionItem item = inspectionItemMapper.selectById(id);
        if (item == null || Integer.valueOf(1).equals(item.getDeleted())) {
            throw new ServiceException("巡检内容不存在");
        }
        item.setDeleted(1);
        item.setIsActive(0);
        if (inspectionItemMapper.updateById(item) <= 0) {
            throw new ServiceException("删除巡检内容失败");
        }
    }
 
    private LambdaQueryWrapper<InspectionItem> buildQuery(InspectionItem query, boolean includeCommon) {
        InspectionItem filter = query == null ? new InspectionItem() : query;
        LambdaQueryWrapper<InspectionItem> wrapper = new LambdaQueryWrapper<InspectionItem>()
                .eq(InspectionItem::getDeleted, 0);
        if (StringUtils.isNotBlank(filter.getItemProject())) {
            wrapper.like(InspectionItem::getItemProject, filter.getItemProject().trim());
        }
        if (StringUtils.isNotBlank(filter.getItemContent())) {
            wrapper.like(InspectionItem::getItemContent, filter.getItemContent().trim());
        }
        if (StringUtils.isNotBlank(filter.getItemKeyword())) {
            String keyword = filter.getItemKeyword().trim();
            wrapper.and(condition -> condition
                    .like(InspectionItem::getItemProject, keyword)
                    .or()
                    .like(InspectionItem::getItemContent, keyword));
        }
        if (filter.getIsActive() != null) {
            wrapper.eq(InspectionItem::getIsActive, filter.getIsActive());
        }
        if (filter.getDeviceLedgerId() != null) {
            if (includeCommon) {
                wrapper.and(condition -> condition
                        .eq(InspectionItem::getDeviceLedgerId, filter.getDeviceLedgerId())
                        .or()
                        .isNull(InspectionItem::getDeviceLedgerId));
            } else {
                wrapper.eq(InspectionItem::getDeviceLedgerId, filter.getDeviceLedgerId());
            }
        }
        if (StringUtils.isNotBlank(filter.getFrequencyType())) {
            if (includeCommon) {
                wrapper.and(condition -> condition
                        .eq(InspectionItem::getFrequencyType, filter.getFrequencyType())
                        .or()
                        .eq(InspectionItem::getFrequencyType, "ALL"));
            } else {
                wrapper.eq(InspectionItem::getFrequencyType, filter.getFrequencyType());
            }
        }
        return wrapper;
    }
 
    private void validate(InspectionItem item) {
        if (item == null || StringUtils.isBlank(item.getItemProject())
                || StringUtils.isBlank(item.getItemContent())
                || StringUtils.isBlank(item.getFrequencyType())
                || !VALID_FREQUENCIES.contains(item.getFrequencyType())) {
            throw new ServiceException("巡检项目、巡检内容和频率不能为空或无效");
        }
    }
 
    private void normalize(InspectionItem item) {
        item.setItemProject(item.getItemProject().trim());
        item.setItemContent(item.getItemContent().trim());
        item.setSortNum(item.getSortNum() == null ? 0 : item.getSortNum());
        item.setIsActive(item.getIsActive() == null ? 1 : item.getIsActive());
    }
 
    private boolean existsDuplicate(InspectionItem item, Long excludedId) {
        LambdaQueryWrapper<InspectionItem> wrapper = new LambdaQueryWrapper<InspectionItem>()
                .eq(InspectionItem::getDeleted, 0)
                .eq(InspectionItem::getItemProject, item.getItemProject())
                .eq(InspectionItem::getItemContent, item.getItemContent())
                .eq(InspectionItem::getFrequencyType, item.getFrequencyType());
        if (item.getDeviceLedgerId() == null) {
            wrapper.isNull(InspectionItem::getDeviceLedgerId);
        } else {
            wrapper.eq(InspectionItem::getDeviceLedgerId, item.getDeviceLedgerId());
        }
        if (excludedId != null) {
            wrapper.ne(InspectionItem::getId, excludedId);
        }
        return inspectionItemMapper.selectCount(wrapper) > 0;
    }
}