heheng
昨天 75960d6e223f8cab9ceb489f6b89f5f08c6db62a
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
package com.gkhy.exam.system.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.*;
import com.gkhy.exam.system.mapper.AnnualMaintenanceServiceContentMapper;
import com.gkhy.exam.system.mapper.AnnualMaintenanceServiceMapper;
import com.gkhy.exam.system.mapper.AnnualMaintenanceServiceUserMapper;
import com.gkhy.exam.system.service.AnnualMaintenanceServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
import static net.sf.jsqlparser.parser.feature.Feature.update;
 
/**
 * <p>
 * 年度基础设施维护保养表     服务实现类
 * </p>
 *
 * @author hh
 * @since 2025-07-31 16:12:02
 */
@Service
public class AnnualMaintenanceServiceServiceImpl extends ServiceImpl<AnnualMaintenanceServiceMapper, AnnualMaintenanceService> implements AnnualMaintenanceServiceService {
 
    @Autowired
    private AnnualMaintenanceServiceMapper annualMaintenanceServiceMapper;
 
    @Autowired
    private AnnualMaintenanceServiceUserMapper annualMaintenanceServiceUserMapper;
 
    @Autowired
    private AnnualMaintenanceServiceContentMapper annualMaintenanceServiceContentMapper;
 
    @Override
    public CommonPage selectAnnualMaintenanceServiceList(AnnualMaintenanceService annualMaintenanceService) {
        PageUtils.startPage();
        List<AnnualMaintenanceService> annualMaintenanceServices = annualMaintenanceServiceMapper.selectAnnualMaintenanceServiceList(annualMaintenanceService);
        return CommonPage.restPage(annualMaintenanceServices);
    }
 
    @Override
    public CommonResult saveAnnualMaintenanceService(AnnualMaintenanceService annualMaintenanceService) {
        List<AnnualMaintenanceServiceContent> annualMaintenanceServiceContentList = annualMaintenanceService.getAnnualMaintenanceServiceContentList();
        List<AnnualMaintenanceServiceUser> annualMaintenanceServiceUserList = annualMaintenanceService.getAnnualMaintenanceServiceUserList();
        if (ObjectUtil.isEmpty(annualMaintenanceServiceContentList) || ObjectUtil.isEmpty(annualMaintenanceServiceUserList)) {
            return CommonResult.failed("请填写保养内容及保养人员数据");
        }
        checkUser(annualMaintenanceServiceUserList);
        boolean isAdd = annualMaintenanceService.getId() == null;
        int i = 0;
        if (isAdd) {
            annualMaintenanceService.setCreateBy(SecurityUtils.getUsername());
            annualMaintenanceService.setCreateTime(LocalDateTime.now());
            i = annualMaintenanceServiceMapper.insert(annualMaintenanceService);
        } else {
            annualMaintenanceService.setUpdateBy(SecurityUtils.getUsername());
            annualMaintenanceService.setUpdateTime(LocalDateTime.now());
            i = annualMaintenanceServiceMapper.updateById(annualMaintenanceService);
        }
        if (i > 0) {
            batchSaveContent(isAdd, annualMaintenanceService.getId(), annualMaintenanceServiceContentList);
            batchSaveUser(annualMaintenanceService.getId(), annualMaintenanceServiceUserList, annualMaintenanceService.getDelServiceUserIds());
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
 
    private void batchSaveContent(boolean isAdd, Long serviceId, List<AnnualMaintenanceServiceContent> annualMaintenanceServiceContentList) {
        if (isAdd) {
            annualMaintenanceServiceContentList.forEach(content -> {
                content.setAnnualMaintenanceServiceId(serviceId);
                content.setCreateBy(SecurityUtils.getUsername());
                content.setCreateTime(LocalDateTime.now());
            });
            int insert = annualMaintenanceServiceContentMapper.batchInsert(annualMaintenanceServiceContentList);
            if (insert <= 0) {
                throw new RuntimeException("保存保养内容失败");
            }
        } else {
            annualMaintenanceServiceContentList.forEach(content -> {
                content.setUpdateBy(SecurityUtils.getUsername());
                content.setUpdateTime(LocalDateTime.now());
            });
            int update = annualMaintenanceServiceContentMapper.batchUpdate(annualMaintenanceServiceContentList);
            if (update <= 0) {
                throw new RuntimeException("保存保养内容失败");
            }
        }
    }
 
    private void batchSaveUser(Long serviceId, List<AnnualMaintenanceServiceUser> annualMaintenanceServiceUserList, List<Long> delServiceUserIds) {
        if (ObjectUtil.isNotEmpty(delServiceUserIds)) {
            int update1 = annualMaintenanceServiceUserMapper.update(new AnnualMaintenanceServiceUser(),
                    new LambdaUpdateWrapper<AnnualMaintenanceServiceUser>().set(AnnualMaintenanceServiceUser::getDelFlag, UserConstant.DEPT_DISABLE)
                            .set(AnnualMaintenanceServiceUser::getUpdateTime, LocalDateTime.now()).set(AnnualMaintenanceServiceUser::getUpdateBy, SecurityUtils.getUsername())
                            .in(AnnualMaintenanceServiceUser::getId, delServiceUserIds)
            );
            if (update1 <= 0) {
                throw new RuntimeException("保存失败");
            }
        }
 
        List<AnnualMaintenanceServiceUser> addUser = annualMaintenanceServiceUserList.stream()
                .filter(user -> null == user.getId())
                .collect(Collectors.toList());
        addUser.forEach(user -> {
            user.setCreateBy(SecurityUtils.getUsername());
            user.setCreateTime(LocalDateTime.now());
            user.setAnnualMaintenanceServiceId(serviceId);
        });
        int i = annualMaintenanceServiceUserMapper.batchInsert(addUser);
        if (i <= 0) {
            throw new RuntimeException("保存失败");
        }
 
        List<AnnualMaintenanceServiceUser> updateUser = annualMaintenanceServiceUserList.stream()
                .filter(user -> null != user.getId())
                .collect(Collectors.toList());
        updateUser.forEach(user -> {
            user.setUpdateBy(SecurityUtils.getUsername());
            user.setUpdateTime(LocalDateTime.now());
        });
        int update = annualMaintenanceServiceUserMapper.batchUpdate(updateUser);
        if (update <= 0) {
            throw new RuntimeException("保存失败");
        }
    }
 
    private void checkUser(List<AnnualMaintenanceServiceUser> annualMaintenanceServiceUserList) {
 
        List<AnnualMaintenanceServiceUser> filteredUsers = annualMaintenanceServiceUserList.stream()
                .filter(user -> 1 == user.getUserType())
                .collect(Collectors.toList());
        if (ObjectUtil.isEmpty(filteredUsers)) {
            throw new RuntimeException("请填写一级保养操作人员");
        }
        Set<Long> userIdSet = filteredUsers.stream()
                .map(AnnualMaintenanceServiceUser::getUserId)
                .collect(Collectors.toSet());
        if (userIdSet.size() != filteredUsers.size()) {
            throw new RuntimeException("一级保养操作人员重复");
        }
 
        List<AnnualMaintenanceServiceUser> filteredUsers2 = annualMaintenanceServiceUserList.stream()
                .filter(user -> 2 == user.getUserType())
                .collect(Collectors.toList());
        if (ObjectUtil.isEmpty(filteredUsers2)) {
            throw new RuntimeException("请填写一级保养检查人员");
        }
        Set<Long> userIdSet2 = filteredUsers2.stream()
                .map(AnnualMaintenanceServiceUser::getUserId)
                .collect(Collectors.toSet());
        if (userIdSet2.size() != filteredUsers2.size()) {
            throw new RuntimeException("一级保养检查人员重复");
        }
 
        List<AnnualMaintenanceServiceUser> filteredUsers3 = annualMaintenanceServiceUserList.stream()
                .filter(user -> 3 == user.getUserType())
                .collect(Collectors.toList());
        if (ObjectUtil.isEmpty(filteredUsers3)) {
            throw new RuntimeException("请填写二级保养操作人员");
        }
        Set<Long> userIdSet3 = filteredUsers.stream()
                .map(AnnualMaintenanceServiceUser::getUserId)
                .collect(Collectors.toSet());
        if (userIdSet3.size() != filteredUsers3.size()) {
            throw new RuntimeException("二级保养操作人员重复");
        }
 
        List<AnnualMaintenanceServiceUser> filteredUsers4 = annualMaintenanceServiceUserList.stream()
                .filter(user -> 4 == user.getUserType())
                .collect(Collectors.toList());
        if (ObjectUtil.isEmpty(filteredUsers4)) {
            throw new RuntimeException("请填写二级保养检查人员");
        }
        Set<Long> userIdSet4 = filteredUsers2.stream()
                .map(AnnualMaintenanceServiceUser::getUserId)
                .collect(Collectors.toSet());
        if (userIdSet4.size() != filteredUsers4.size()) {
            throw new RuntimeException("二级保养检查人员重复");
        }
    }
 
    @Override
    public CommonResult getAnnualMaintenanceService(Integer id) {
        AnnualMaintenanceService annualMaintenanceService = annualMaintenanceServiceMapper.selectById(id);
        if (ObjectUtils.isNotEmpty(annualMaintenanceService)) {
            List<AnnualMaintenanceServiceContent> annualMaintenanceServiceContentList = annualMaintenanceServiceContentMapper.selectList(
                    new LambdaQueryWrapper<AnnualMaintenanceServiceContent>().eq(AnnualMaintenanceServiceContent::getAnnualMaintenanceServiceId, id)
                            .eq(AnnualMaintenanceServiceContent::getDelFlag, UserConstant.ENABLE).orderByAsc(AnnualMaintenanceServiceContent::getServiceType));
            annualMaintenanceService.setAnnualMaintenanceServiceContentList(annualMaintenanceServiceContentList);
 
            List<AnnualMaintenanceServiceUser> annualMaintenanceServiceUserList = annualMaintenanceServiceUserMapper.selectList(
                    new LambdaQueryWrapper<AnnualMaintenanceServiceUser>().eq(AnnualMaintenanceServiceUser::getAnnualMaintenanceServiceId, id)
                            .eq(AnnualMaintenanceServiceUser::getDelFlag, UserConstant.ENABLE).orderByAsc(AnnualMaintenanceServiceUser::getUserType));
            annualMaintenanceService.setAnnualMaintenanceServiceUserList(annualMaintenanceServiceUserList);
        }
        return CommonResult.success(annualMaintenanceService);
    }
 
    @Override
    @Transactional
    public CommonResult deletedAnnualMaintenanceService(Integer id) {
        AnnualMaintenanceService annualMaintenanceService = new AnnualMaintenanceService();
        annualMaintenanceService.setId(Long.valueOf(id));
        annualMaintenanceService.setDelFlag(UserConstant.DEPT_DISABLE);
        int update = annualMaintenanceServiceMapper.updateById(annualMaintenanceService);
        if (update > 0) {
 
            int update1 = annualMaintenanceServiceUserMapper.update(new AnnualMaintenanceServiceUser(),
                    new LambdaUpdateWrapper<AnnualMaintenanceServiceUser>().eq(AnnualMaintenanceServiceUser::getAnnualMaintenanceServiceId, id)
                            .set(AnnualMaintenanceServiceUser::getDelFlag, UserConstant.DEPT_DISABLE)
                            .set(AnnualMaintenanceServiceUser::getUpdateTime, LocalDateTime.now())
                            .set(AnnualMaintenanceServiceUser::getUpdateBy, SecurityUtils.getUsername()));
            if (update1 <= 0) {
                throw new RuntimeException("删除失败");
            }
            int update2 = annualMaintenanceServiceContentMapper.update(new AnnualMaintenanceServiceContent(),
                    new LambdaUpdateWrapper<AnnualMaintenanceServiceContent>().eq(AnnualMaintenanceServiceContent::getAnnualMaintenanceServiceId, id)
                            .set(AnnualMaintenanceServiceContent::getDelFlag, UserConstant.DEPT_DISABLE)
                            .set(AnnualMaintenanceServiceContent::getUpdateTime, LocalDateTime.now())
                            .set(AnnualMaintenanceServiceContent::getUpdateBy, SecurityUtils.getUsername())
            );
            if (update2 <= 0) {
                throw new RuntimeException("删除失败");
            }
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}