zhangf
2024-05-08 0414ddb0b2b3a7199ae6181a770f97ac140dbd73
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
package com.gkhy.safePlatform.emergency.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.emergency.entity.*;
import com.gkhy.safePlatform.emergency.enums.EmergencyResultCodes;
import com.gkhy.safePlatform.emergency.excepiton.EmergencyException;
import com.gkhy.safePlatform.emergency.model.dto.req.EmergencySuppliesInspectReqDTO;
import com.gkhy.safePlatform.emergency.model.dto.req.EmergencySuppliesInspectUserReqDTO;
import com.gkhy.safePlatform.emergency.model.dto.resp.EmergencySuppliesInspectDetailRespDTO;
import com.gkhy.safePlatform.emergency.model.dto.resp.EmergencySuppliesInspectPageRespDTO;
import com.gkhy.safePlatform.emergency.model.dto.resp.EmergencySuppliesInspectUserRespDTO;
import com.gkhy.safePlatform.emergency.query.EmergencySuppliesInspectQuery;
import com.gkhy.safePlatform.emergency.query.db.EmergencySuppliesInspectDBQuery;
import com.gkhy.safePlatform.emergency.service.EmergencySuppliesInspectService;
import com.gkhy.safePlatform.emergency.service.baseService.EmergencySuppliesInfoService;
import com.gkhy.safePlatform.emergency.service.baseService.EmergencySuppliesInspectInfoService;
import com.gkhy.safePlatform.emergency.service.baseService.EmergencySuppliesInspectUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service("emergencySuppliesInspectService")
public class EmergencySuppliesInspectServiceImpl implements EmergencySuppliesInspectService {
 
    @Autowired
    private EmergencySuppliesInspectInfoService emergencySuppliesInspectInfoService;
 
    @Autowired
    private EmergencySuppliesInspectUserInfoService emergencySuppliesInspectUserInfoService;
 
    @Autowired
    private EmergencySuppliesInfoService emergencySuppliesInfoService;
 
 
    @Override
    public SearchResultVO<List<EmergencySuppliesInspectPageRespDTO>> selectEmergencySuppliesInspectList(PageQuery<EmergencySuppliesInspectQuery> query) {
        Long pageIndex = query.getPageIndex();
        Long pageSize = query.getPageSize();
        Page<EmergencySuppliesInspectInfoPageDO> page = new Page<>(pageIndex, pageSize);
 
        EmergencySuppliesInspectDBQuery emergencySuppliesInspectDBQuery = new EmergencySuppliesInspectDBQuery();
        if (query.getSearchParams() != null) {
            BeanUtils.copyProperties(query.getSearchParams(), emergencySuppliesInspectDBQuery);
        }
        List<EmergencySuppliesInspectInfoPageDO> emergencySuppliesInspectListDoInfoList = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectList(page,emergencySuppliesInspectDBQuery);
        List<EmergencySuppliesInspectPageRespDTO> respList =  BeanCopyUtils.copyBeanList(emergencySuppliesInspectListDoInfoList, EmergencySuppliesInspectPageRespDTO.class);
 
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
                );
    }
 
    @Override
    public ResultVO addEmergencySuppliesInspect(Long uid, EmergencySuppliesInspectReqDTO emergencySuppliesInspectReqDTO) {
        Long suppliesId = emergencySuppliesInspectReqDTO.getSuppliesId();
 
        if (suppliesId==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NULL);
        }else{
            EmergencySuppliesInfoDetailDO emergencySuppliesInfoDetailDO = emergencySuppliesInfoService.selectEmergencySuppliesById(suppliesId);
            if (emergencySuppliesInfoDetailDO==null){
                throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NOT_EXIST);
            }else{
                Date nowDate = new Date();
                // 新增应急物资检查
                EmergencySuppliesInspectInfo emergencySuppliesInspectInfo = new EmergencySuppliesInspectInfo();
                BeanUtils.copyProperties(emergencySuppliesInspectReqDTO,emergencySuppliesInspectInfo);
                emergencySuppliesInspectInfo.setDelFlag(false);
                emergencySuppliesInspectInfo.setCreateUid(uid);
                emergencySuppliesInspectInfo.setGmtCreate(nowDate);
                emergencySuppliesInspectInfoService.addEmergencySuppliesInspect(emergencySuppliesInspectInfo);
 
                // 新增急演练计划应急人员表
                if (!CollectionUtils.isEmpty(emergencySuppliesInspectReqDTO.getUserList())) {
                    addEmergencySuppliesInspectUser(uid, emergencySuppliesInspectInfo.getId(), nowDate, emergencySuppliesInspectReqDTO.getUserList());
                }
                return new ResultVO<>(ResultCodes.OK);
            }
        }
    }
 
    private void addEmergencySuppliesInspectUser(Long uid, Long inspectId, Date nowDate, List<EmergencySuppliesInspectUserReqDTO> userList) {
        List<EmergencySuppliesInspectUserInfo> emergencySuppliesInspectUserInfoList = BeanCopyUtils.copyBeanList(userList, EmergencySuppliesInspectUserInfo.class);
        emergencySuppliesInspectUserInfoList.forEach(EmergencySuppliesInspectUserInfo -> {
            EmergencySuppliesInspectUserInfo.setDelFlag(false);
            EmergencySuppliesInspectUserInfo.setCreateUid(uid);
            EmergencySuppliesInspectUserInfo.setGmtCreate(nowDate);
            EmergencySuppliesInspectUserInfo.setInspectId(inspectId);
        });
        for (EmergencySuppliesInspectUserInfo emergencySuppliesInspectUserInfo : emergencySuppliesInspectUserInfoList) {
            emergencySuppliesInspectUserInfoService.addEmergencySuppliesInspectUser(emergencySuppliesInspectUserInfo);
        }
    }
 
 
    @Override
    public ResultVO<EmergencySuppliesInspectDetailRespDTO> getEmergencySuppliesInspectById(Long id) {
        EmergencySuppliesInspectDetailRespDTO emergencySuppliesInspectDetailRespDTO = new EmergencySuppliesInspectDetailRespDTO();
        // 查询是否存在
        EmergencySuppliesInspectInfoDetailDO emergencySuppliesInspectInfoDetailDO = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectById(id);
        if (emergencySuppliesInspectInfoDetailDO==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NOT_EXIST);
        }else{
            BeanUtils.copyProperties(emergencySuppliesInspectInfoDetailDO,emergencySuppliesInspectDetailRespDTO);
 
            // 查找对应的人员表
            List<EmergencySuppliesInspectUserInfoDO> emergencySuppliesInspectUserInfoDOList = emergencySuppliesInspectUserInfoService.selectEmergencySuppliesInspectUserBySuppliesInspectId(id);
            if (!CollectionUtils.isEmpty(emergencySuppliesInspectUserInfoDOList)) {
                List<EmergencySuppliesInspectUserRespDTO> emergencySuppliesInspectUserRespDTOList = BeanCopyUtils.copyBeanList(emergencySuppliesInspectUserInfoDOList, EmergencySuppliesInspectUserRespDTO.class);
                emergencySuppliesInspectDetailRespDTO.setUserList(emergencySuppliesInspectUserRespDTOList);
            }
 
            return new ResultVO<>(ResultCodes.OK,emergencySuppliesInspectDetailRespDTO);
        }
    }
 
    @Override
    public ResultVO updateEmergencySuppliesInspect(Long uid, EmergencySuppliesInspectReqDTO emergencySuppliesInspectReqDTO) {
        Date nowDate = new Date();
        // 查询是否存在
        EmergencySuppliesInspectInfoDetailDO emergencySuppliesInspectInfoDetailDO = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectById(emergencySuppliesInspectReqDTO.getId());
        if (emergencySuppliesInspectInfoDetailDO==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NOT_EXIST);
        }else{
            EmergencySuppliesInspectInfo emergencySuppliesInspectInfo = new EmergencySuppliesInspectInfo();
            BeanUtils.copyProperties(emergencySuppliesInspectReqDTO,emergencySuppliesInspectInfo);
            emergencySuppliesInspectInfo.setUpdateUid(uid);
            emergencySuppliesInspectInfo.setGmtModitify(nowDate);
            emergencySuppliesInspectInfoService.updateEmergencySuppliesInspect(emergencySuppliesInspectInfo);
 
            // 更新急演练计划应急人员表
            if (!CollectionUtils.isEmpty(emergencySuppliesInspectReqDTO.getUserList())) {
                updateEmergencySuppliesInspectUser(uid, emergencySuppliesInspectInfo.getId(), nowDate, emergencySuppliesInspectReqDTO.getUserList());
            }
            return new ResultVO<>(ResultCodes.OK);
        }
    }
 
    private void updateEmergencySuppliesInspectUser(Long uid, Long suppliesInspectId, Date
            nowDate, List<EmergencySuppliesInspectUserReqDTO> UserReqDTOList) {
        List<EmergencySuppliesInspectUserInfoDO> emergencySuppliesInspectUserInfoDOList = emergencySuppliesInspectUserInfoService.selectEmergencySuppliesInspectUserBySuppliesInspectId(suppliesInspectId);
        List<Long> oldIdsList = emergencySuppliesInspectUserInfoDOList.stream().map(EmergencySuppliesInspectUserInfoDO::getId).collect(Collectors.toList());
        List<Long> newIdsList = new ArrayList<>();
 
        //新增的区域集合
        List<EmergencySuppliesInspectUserInfo> addList = new ArrayList<>();
        //删除的区域集合(id)
        List<Long> deleteList = new ArrayList<>();
        for (EmergencySuppliesInspectUserReqDTO emergencySuppliesInspectUserReqDTO : UserReqDTOList) {
            //如果不存在id则表示页面新增的附件
            if (emergencySuppliesInspectUserReqDTO.getId() == null) {
                EmergencySuppliesInspectUserInfo emergencySuppliesInspectUserInfo = new EmergencySuppliesInspectUserInfo();
                BeanUtils.copyProperties(emergencySuppliesInspectUserReqDTO, emergencySuppliesInspectUserInfo);
                emergencySuppliesInspectUserInfo.setDelFlag(false);
                emergencySuppliesInspectUserInfo.setGmtCreate(nowDate);
                emergencySuppliesInspectUserInfo.setCreateUid(uid);
                emergencySuppliesInspectUserInfo.setInspectId(suppliesInspectId);
                addList.add(emergencySuppliesInspectUserInfo);
            }
            //如果存在id则判断页面是否删除
            else {
                newIdsList.add(emergencySuppliesInspectUserReqDTO.getId());
            }
        }
        for (Long oldId : oldIdsList) {
            if (!newIdsList.contains(oldId)) {
                deleteList.add(oldId);
            }
        }
        if (!CollectionUtils.isEmpty(addList)) {
            for (EmergencySuppliesInspectUserInfo emergencySuppliesInspectUserInfo : addList) {
                emergencySuppliesInspectUserInfoService.addEmergencySuppliesInspectUser(emergencySuppliesInspectUserInfo);
            }
        }
        if (!CollectionUtils.isEmpty(deleteList)) {
            emergencySuppliesInspectUserInfoService.deleteEmergencySuppliesInspectUserByIds(deleteList);
        }
    }
 
    @Override
    public ResultVO batchDeleteEmergencySuppliesInspect(Long[] ids) {
 
        if (ids == null ||  ids.length==0){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NULL);
        }else{
            for (Long id : ids){
                deleteEmergencySuppliesInspect(id);
            }
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    private void deleteEmergencySuppliesInspect(Long id) {
        //查询是否存在
        EmergencySuppliesInspectInfoDetailDO emergencySuppliesInspectInfoDetailDO = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectById(id);
        if (emergencySuppliesInspectInfoDetailDO==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_INSPECT_NOT_EXIST);
        }else{
            Long suppliesInspectId = emergencySuppliesInspectInfoDetailDO.getId();
            emergencySuppliesInspectInfoService.deleteEmergencySuppliesInspect(suppliesInspectId);
            //删除人员
            emergencySuppliesInspectUserInfoService.deleteEmergencySuppliesInspectUserBySuppliesInspectId(suppliesInspectId);
        }
    }
}