zhangfeng
2022-09-27 6de6bb0fc1bf4cd5535e33484ad96b0f273b2b9b
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
package com.gkhy.safePlatform.emergency.service.impl;
 
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
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.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.enums.EmergencySuppliesClassificationEnum;
import com.gkhy.safePlatform.emergency.enums.EmergencySuppliesStatusEnum;
import com.gkhy.safePlatform.emergency.excepiton.EmergencyException;
import com.gkhy.safePlatform.emergency.model.dto.req.*;
import com.gkhy.safePlatform.emergency.model.dto.resp.*;
import com.gkhy.safePlatform.emergency.query.EmergencySuppliesCountQuery;
import com.gkhy.safePlatform.emergency.query.EmergencySuppliesQuery;
import com.gkhy.safePlatform.emergency.query.db.EmergencySuppliesDBQuery;
import com.gkhy.safePlatform.emergency.service.EmergencySuppliesMaintainService;
import com.gkhy.safePlatform.emergency.service.EmergencySuppliesService;
import com.gkhy.safePlatform.emergency.service.baseService.*;
import org.apache.dubbo.config.annotation.DubboReference;
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.*;
import java.util.stream.Collectors;
 
@Service("emergencySuppliesService")
public class EmergencySuppliesServiceImpl implements EmergencySuppliesService {
 
    @Autowired
    private EmergencySuppliesInfoService emergencySuppliesInfoService;
 
    @Autowired
    private EmergencySuppliesInspectInfoService emergencySuppliesInspectInfoService;
 
    @Autowired
    private EmergencySuppliesInspectUserInfoService emergencySuppliesInspectUserInfoService;
 
    @Autowired
    private EmergencySuppliesMaintainInfoService emergencySuppliesMaintainInfoService;
 
    @Autowired
    private EmergencySuppliesMaintainUserInfoService emergencySuppliesMaintainUserInfoService;
 
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
 
 
    @Override
    public SearchResultVO<List<EmergencySuppliesPageRespDTO>> selectEmergencySuppliesList(PageQuery<EmergencySuppliesQuery> query) {
        Long pageIndex = query.getPageIndex();
        Long pageSize = query.getPageSize();
        Page<EmergencySuppliesInfoPageDO> page = new Page<>(pageIndex, pageSize);
 
        EmergencySuppliesDBQuery emergencySuppliesDBQuery = new EmergencySuppliesDBQuery();
        if (query.getSearchParams() != null) {
            BeanUtils.copyProperties(query.getSearchParams(), emergencySuppliesDBQuery);
        }
        List<EmergencySuppliesInfoPageDO> emergencySuppliesListDoInfoList = emergencySuppliesInfoService.selectEmergencySuppliesList(page,emergencySuppliesDBQuery);
        List<EmergencySuppliesPageRespDTO> respList =  BeanCopyUtils.copyBeanList(emergencySuppliesListDoInfoList, EmergencySuppliesPageRespDTO.class);
 
        Map<Long, String> depPool = new HashMap<>();
 
        respList.forEach(AccidentExpressPageRespDTO ->{
            // 设置部门名称
            if (!depPool.containsKey(AccidentExpressPageRespDTO.getDepartmentId())) {
                ResultVO<DepInfoRPCRespDTO> rpcResult = accountDepartmentService.getDepInfoByDepId(AccidentExpressPageRespDTO.getDepartmentId());
                if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
                    if (rpcResult.getData() != null) {
                        DepInfoRPCRespDTO dep = (DepInfoRPCRespDTO) rpcResult.getData();
 
                        depPool.put(dep.getDepId(), dep.getDepName());
                    }
                }}
 
            String depName = depPool.get(AccidentExpressPageRespDTO.getDepartmentId());
            AccidentExpressPageRespDTO.setDeptName(depName);
        });
 
        return new SearchResultVO<>(
                true,
                pageIndex,
                pageSize,page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK
                );
    }
 
    @Override
    public ResultVO addEmergencySupplies(Long uid, EmergencySuppliesReqDTO emergencySuppliesReqDTO) {
 
        // 验证必填
        checkRequired(emergencySuppliesReqDTO);
        //验证枚举
        checkEnum(emergencySuppliesReqDTO);
 
        Date nowDate = new Date();
        // 新增应急物资
        EmergencySuppliesInfo emergencySuppliesInfo = new EmergencySuppliesInfo();
        BeanUtils.copyProperties(emergencySuppliesReqDTO,emergencySuppliesInfo);
        emergencySuppliesInfo.setDelFlag(false);
        emergencySuppliesInfo.setCreateUid(uid);
        emergencySuppliesInfo.setGmtCreate(nowDate);
        emergencySuppliesInfoService.addEmergencySupplies(emergencySuppliesInfo);
 
        return new ResultVO<>(ResultCodes.OK);
    }
 
    @Override
    public ResultVO<EmergencySuppliesDetailRespDTO> getEmergencySuppliesById(Long id) {
        EmergencySuppliesDetailRespDTO emergencySuppliesDetailRespDTO = new EmergencySuppliesDetailRespDTO();
        // 查询是否存在
        EmergencySuppliesInfoDetailDO emergencySuppliesInfoDetailDO = emergencySuppliesInfoService.selectEmergencySuppliesById(id);
        if (emergencySuppliesInfoDetailDO==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NOT_EXIST);
        }else{
            BeanUtils.copyProperties(emergencySuppliesInfoDetailDO,emergencySuppliesDetailRespDTO);
 
            // 查找检查记录
            List<EmergencySuppliesInspectInfoDetailDO> inspectDetailDOList = emergencySuppliesInspectInfoService.selectEmergencySuppliesInspectBySuppliesId(emergencySuppliesDetailRespDTO.getId());
            if (!CollectionUtils.isEmpty(inspectDetailDOList)){
                List<EmergencySuppliesInspectDetailRespDTO> inspectList = BeanCopyUtils.copyBeanList(inspectDetailDOList,EmergencySuppliesInspectDetailRespDTO.class);
                // 创建人
                for (EmergencySuppliesInspectDetailRespDTO emergencySuppliesInspectDetailRespDTO :inspectList){
                    List<EmergencySuppliesInspectUserInfoDO> inspectUserDOList = emergencySuppliesInspectUserInfoService.selectEmergencySuppliesInspectUserBySuppliesInspectId(emergencySuppliesInspectDetailRespDTO.getId());
                    if (!CollectionUtils.isEmpty(inspectUserDOList)){
                        List<EmergencySuppliesInspectUserRespDTO> inspectUserList = BeanCopyUtils.copyBeanList(inspectUserDOList,EmergencySuppliesInspectUserRespDTO.class);
                        emergencySuppliesInspectDetailRespDTO.setUserList(inspectUserList);
                    }
                }
                emergencySuppliesDetailRespDTO.setInspectList(inspectList);
            }
            // 查找保养记录
            List<EmergencySuppliesMaintainInfoDetailDO> maintainDetailDOList = emergencySuppliesMaintainInfoService.selectEmergencySuppliesMaintainBySuppliesId(emergencySuppliesDetailRespDTO.getId());
            if (!CollectionUtils.isEmpty(maintainDetailDOList)){
                List<EmergencySuppliesMaintainDetailRespDTO> maintainList = BeanCopyUtils.copyBeanList(maintainDetailDOList,EmergencySuppliesMaintainDetailRespDTO.class);
                // 创建人
                for (EmergencySuppliesMaintainDetailRespDTO emergencySuppliesMaintainDetailRespDTO :maintainList){
                    List<EmergencySuppliesMaintainUserInfoDO> maintainUserDOList = emergencySuppliesMaintainUserInfoService.selectEmergencySuppliesMaintainUserBySuppliesMaintainId(emergencySuppliesMaintainDetailRespDTO.getId());
                    if (!CollectionUtils.isEmpty(maintainUserDOList)){
                        List<EmergencySuppliesMaintainUserRespDTO> maintainUserList = BeanCopyUtils.copyBeanList(maintainUserDOList,EmergencySuppliesMaintainUserRespDTO.class);
                        emergencySuppliesMaintainDetailRespDTO.setUserList(maintainUserList);
                    }
                }
                emergencySuppliesDetailRespDTO.setMaintainList(maintainList);
            }
 
            return new ResultVO<>(ResultCodes.OK,emergencySuppliesDetailRespDTO);
        }
    }
 
    @Override
    public ResultVO updateEmergencySupplies(Long uid, EmergencySuppliesReqDTO emergencySuppliesReqDTO) {
        // 验证必填
        checkRequired(emergencySuppliesReqDTO);
        //验证枚举
        checkEnum(emergencySuppliesReqDTO);
 
        Date nowDate = new Date();
        // 查询是否存在
        EmergencySuppliesInfoDetailDO emergencySuppliesInfoDetailDO = emergencySuppliesInfoService.selectEmergencySuppliesById(emergencySuppliesReqDTO.getId());
        if (emergencySuppliesInfoDetailDO==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NOT_EXIST);
        }else{
            EmergencySuppliesInfo emergencySuppliesInfo = new EmergencySuppliesInfo();
            BeanUtils.copyProperties(emergencySuppliesReqDTO,emergencySuppliesInfo);
            emergencySuppliesInfo.setUpdateUid(uid);
            emergencySuppliesInfo.setGmtModitify(nowDate);
            emergencySuppliesInfoService.updateEmergencySupplies(emergencySuppliesInfo);
 
            return new ResultVO<>(ResultCodes.OK);
        }
    }
 
    @Override
    public ResultVO batchDeleteEmergencySupplies( Long[]  ids) {
 
        if (ids == null ||  ids.length==0){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NULL);
        }else{
            for (Long id : ids){
                deleteEmergencySupplies(id);
            }
            return new ResultVO(ResultCodes.OK);
        }
    }
 
    /**
     * 按照物资类型分类获取列表
     * @param currentUser
     * @return
     */
    @Override
    public ResultVO<List<EmergencySuppliesTypeRespDTO>> emergencySuppliesList(ContextCacheUser currentUser) {
 
        List<EmergencySuppliesInfo> emergencySuppliesInfoList = emergencySuppliesInfoService.listByNoConditions();
        List<EmergencySuppliesTypeRespDTO> typeRespDTOList = new ArrayList<>();
        for (EmergencySuppliesClassificationEnum teamLevelEnum : EmergencySuppliesClassificationEnum.values()) {
            EmergencySuppliesTypeRespDTO typeRespDTO = new EmergencySuppliesTypeRespDTO();
            typeRespDTO.setMaterialType(teamLevelEnum.getCode());
            typeRespDTO.setMaterialTypeName(teamLevelEnum.getValue());
            List<EmergencySuppliesRespDTO> respDTOList = new ArrayList<>();
            List<EmergencySuppliesInfo> collect = emergencySuppliesInfoList.stream().filter(item -> item.getClassification().equals(teamLevelEnum.getCode())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect)){
                for (EmergencySuppliesInfo info : collect) {
                    EmergencySuppliesRespDTO respDTO = new EmergencySuppliesRespDTO();
                    BeanUtils.copyProperties(info,respDTO);
                    respDTO.setMaterialType(info.getClassification());
                    respDTO.setMaterialTypeName(teamLevelEnum.getValue());
                    respDTOList.add(respDTO);
                }
            }
            typeRespDTO.setList(respDTOList);
            typeRespDTOList.add(typeRespDTO);
        }
        return new ResultVO<>(ResultCodes.OK,typeRespDTOList);
    }
 
    @Override
    public ResultVO<List<EmergencySuppliesRespDTO>> listCountByIds(ContextCacheUser currentUser, List<Long> ids) {
        if(null == ids || ids.size() == 0 ){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_PRAM_NULL);
        }
        List<EmergencySuppliesInfo> emergencySuppliesInfos = emergencySuppliesInfoService.listCountByIds(ids);
        List<EmergencySuppliesRespDTO> respDTOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(emergencySuppliesInfos)){
            respDTOList = BeanCopyUtils.copyBeanList(emergencySuppliesInfos,EmergencySuppliesRespDTO.class);
        }
        return new ResultVO<>(ResultCodes.OK,respDTOList);
    }
 
    private void deleteEmergencySupplies(Long id) {
        //查询是否存在
        EmergencySuppliesInfoDetailDO emergencySuppliesInfoDetailDO = emergencySuppliesInfoService.selectEmergencySuppliesById(id);
        if (emergencySuppliesInfoDetailDO==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NOT_EXIST);
        }else{
            Long SuppliesId = emergencySuppliesInfoDetailDO.getId();
            emergencySuppliesInfoService.deleteEmergencySupplies(SuppliesId);
 
            // 删除保养记录
            emergencySuppliesMaintainInfoService.deleteBySuppliesId(id);
            // 删除检查记录
            emergencySuppliesInspectInfoService.deleteBySuppliesId(id);
 
        }
    }
 
 
 
    /**
     * 验证必填项
     * @return
     */
    private void checkRequired(EmergencySuppliesReqDTO emergencySuppliesReqDTO) {
        //名称
        if (StringUtils.isBlank(emergencySuppliesReqDTO.getName())) {
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_NAME_NOT_EXIST);
        }
    }
 
    /**
     * 验证枚举
     */
    private void checkEnum(EmergencySuppliesReqDTO emergencySuppliesReqDTO){
        if (EmergencySuppliesClassificationEnum.getByCode(emergencySuppliesReqDTO.getClassification())==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_ENUM_CLASSIFICATION_NOT_EXIST);
        }
        if (EmergencySuppliesStatusEnum.getByCode(emergencySuppliesReqDTO.getStatus())==null){
            throw new EmergencyException(EmergencyResultCodes.SUPPLIES_ENUM_STATUS_NOT_EXIST);
        }
    }
}