zf
2024-03-25 cd02923a7ce1ffa004b3abbb7af67ab6173dd99d
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package com.gkhy.safePlatform.equipment.service.impl;
 
import cn.hutool.core.util.IdUtil;
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.account.rpc.apimodel.model.resp.DepRPCRespDTO;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.equipment.entity.*;
import com.gkhy.safePlatform.equipment.enums.ConsumableEnum;
import com.gkhy.safePlatform.equipment.enums.EquipmentResultCodes;
import com.gkhy.safePlatform.equipment.excepiton.EquipmentException;
import com.gkhy.safePlatform.equipment.model.dto.req.ParamForm;
import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialAddReq;
import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialModReq;
import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialQuery;
import com.gkhy.safePlatform.equipment.model.dto.resp.BaseMaterialDto;
import com.gkhy.safePlatform.equipment.model.dto.resp.MaterialClassificationDto;
import com.gkhy.safePlatform.equipment.model.dto.resp.MaterialDepartmentDto;
import com.gkhy.safePlatform.equipment.model.dto.resp.SafeMaterialDto;
import com.gkhy.safePlatform.equipment.service.SafeMaterialService;
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialClassifyInfoService;
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialDetailInfoService;
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Param;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
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.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@Service("SafeMaterialService")
public class SafeMaterialServiceImpl implements SafeMaterialService {
    @Autowired
    private SafeMaterialInfoService safeMaterialInfoService;
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
    @Autowired
    private SafeMaterialClassifyInfoService safeMaterialClassifyInfoService;
    @Autowired
    private SafeMaterialDetailInfoService safeMaterialDetailInfoService;
    @Autowired
    private RedissonClient redissonClient;
 
    @Override
    public ResultVO save(ContextCacheUser currentUser,SafeMaterialAddReq req) {
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_INSERT");
        try {
            lock.lock(10, TimeUnit.SECONDS);
            //获取物资类型
            SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
            if(null == classifyInfo){
                throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
            }
            //判断该部门是否已经创建物资
            boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), currentUser.getDepId(), null);
            if(flag){
                throw new EquipmentException(EquipmentResultCodes.DATA_EXIST,"该种物资已存在不可重复创建");
            }
 
            //获取数量
            int safeMaterialTotalCount = safeMaterialInfoService.getTotalCount();
            SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
            safeMaterialInfo.setId(IdUtil.getSnowflake(0,0).nextId());
            safeMaterialInfo.setConsumable(classifyInfo.getConsumable());
            safeMaterialInfo.setDepId(currentUser.getDepId());
            safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
            safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
            safeMaterialInfo.setDepName(currentUser.getDepName());
            safeMaterialInfo.setSerialNum(this.generateSerialNum(safeMaterialTotalCount));
            safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
            safeMaterialInfo.setTotalCount(0);
            safeMaterialInfo.setStockCount(0);
            //插入
            safeMaterialInfoService.save(safeMaterialInfo);
 
            //创建成功,释放锁
            lock.unlock();
        }catch (EquipmentException e) {
            e.printStackTrace();
            throw new EquipmentException(e.getCode(), e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodes.SERVER_ERROR);
        }finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        }
        return new ResultVO(ResultCodes.OK);
    }
 
    @Override
    public ResultVO update(ContextCacheUser currentUser,SafeMaterialModReq req) {
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_UPDATE");
        try {
            lock.lock(10, TimeUnit.SECONDS);
 
 
            //获取物资类型
            SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getSmallClassifyId());
            if(null == classifyInfo){
                throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
            }
            boolean flag = safeMaterialInfoService.checkMaterial(req.getSmallClassifyId(), currentUser.getDepId(), req.getId());
            if(flag){
                throw new EquipmentException(EquipmentResultCodes.DATA_EXIST,"该种物资已存在不可重复创建");
            }
            SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
            safeMaterialInfo.setConsumable(classifyInfo.getConsumable());
            safeMaterialInfo.setSmallClassifyId(req.getSmallClassifyId());
            safeMaterialInfo.setMaterialName(classifyInfo.getMaterialClassifyName());
            safeMaterialInfo.setDepName(currentUser.getDepName());
            safeMaterialInfo.setBigClassifyId(req.getBigClassifyId());
            safeMaterialInfo.setId(req.getId());
            //跟新
            safeMaterialInfoService.updateById(safeMaterialInfo);
        //创建成功,释放锁
            lock.unlock();
        }catch (EquipmentException e) {
            e.printStackTrace();
            throw new EquipmentException(e.getCode(), e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodes.SERVER_ERROR);
        }finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        }
        return new ResultVO(ResultCodes.OK);
    }
 
    @Override
    public ResultVO queryById(ContextCacheUser currentUser,Long id) {
        if(null == id){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        SafeMaterialDO safeMaterialDO = safeMaterialInfoService.queryById(id);
        SafeMaterialDto safeMaterialDto = new SafeMaterialDto();
        if(null != safeMaterialDO){
            BeanUtils.copyProperties(safeMaterialDO,safeMaterialDto);
            if(null != ConsumableEnum.getByCode(safeMaterialDO.getConsumable())){
                safeMaterialDto.setConsumableName(ConsumableEnum.getByCode(safeMaterialDO.getConsumable()).getValue());
            }
        }
        return new ResultVO(ResultCodes.OK,safeMaterialDto);
    }
 
    public ResultVO delete(ContextCacheUser currentUser,Long id) {
        ResultVO resultVO = null;
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_DELETE");
        try {
            lock.lock(10, TimeUnit.SECONDS);
            //删除之前检查详表中是否还有该物物资数据
            int count = safeMaterialDetailInfoService.getCountBySmId(id);
            if(count > 0){
                throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"该种物资已被绑定物资详情数据,不可删除!");
            }
            SafeMaterialInfo materialInfo = new SafeMaterialInfo();
            materialInfo.setId(id);
            materialInfo.setDelFlag(1);
            boolean flag = safeMaterialInfoService.updateById(materialInfo);
            if(flag){
                resultVO = new ResultVO(ResultCodes.OK);
            }else{
                resultVO = new ResultVO(ResultCodes.SERVER_DEL_ERROR);
            }
            //创建成功,释放锁
            lock.unlock();
        }catch (EquipmentException e) {
            e.printStackTrace();
            throw new EquipmentException(e.getCode(), e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodes.SERVER_ERROR);
        }finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        }
        return resultVO;
    }
 
    @Override
    public ResultVO deleteBatch(ContextCacheUser currentUser, ParamForm paramForm) {
 
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_SM_DELETEBATCH");
        try {
            lock.lock(10, TimeUnit.SECONDS);
 
            int count = safeMaterialDetailInfoService.getCountBySmIds(paramForm.getIds());
            //判断是否绑定具体安全物资数据
            if(count > 0){
                throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"物资已被绑定物资详情数据,不可删除!");
            }
            safeMaterialInfoService.deleteBatch(paramForm.getIds());
            //创建成功,释放锁
            lock.unlock();
        }catch (EquipmentException e) {
            e.printStackTrace();
            throw new EquipmentException(e.getCode(), e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodes.SERVER_ERROR);
        }finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        }
        return new ResultVO(ResultCodes.OK);
    }
 
 
 
    /**
     * 获取列表
     * @return
     */
    @Override
    public List<MaterialClassificationDto> listByDepId(ContextCacheUser currentUser) {
        //获取所有数据
        List<SafeMaterialInfo> materialInfoList = safeMaterialInfoService.listByDepId(currentUser.getDepId());
        //获取所有物资类型
        List<SafeMaterialClassifyInfo> classifyInfoList = safeMaterialClassifyInfoService.getParentList();
 
        List<MaterialClassificationDto> classificationDtoList = new ArrayList<>();
        for (SafeMaterialClassifyInfo classifyInfo:classifyInfoList) {
            MaterialClassificationDto classificationDto = new MaterialClassificationDto();
            classificationDto.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
            classificationDto.setMaterialClassifyId(classifyInfo.getId());
            //过滤出物资数据
            List<SafeMaterialInfo> selectMaterialList = materialInfoList
                    .stream()
                    .filter(item -> classifyInfo.getId().equals(item.getBigClassifyId()))
                    .collect(Collectors.toList());
            List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
            if(selectMaterialList.size()>0){
                //循环物资
                for (SafeMaterialInfo materialInfo:selectMaterialList){
                    //填充基础物资数据
                    BaseMaterialDto baseMaterialDto = new BaseMaterialDto();
                    baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
                    baseMaterialDto.setSmId(materialInfo.getId());
                    baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
                    baseMaterialDto.setConsumable(materialInfo.getConsumable());
                    baseMaterialDto.setConsumableName(ConsumableEnum.getByCode(materialInfo.getConsumable()).getValue());
                    baseMaterialDtoList.add(baseMaterialDto);
                }
                classificationDto.setBaseMaterialList(baseMaterialDtoList);
                classificationDtoList.add(classificationDto);
            }
        }
        return classificationDtoList;
    }
    @Override
    public List<MaterialDepartmentDto> list(ContextCacheUser currentUser) {
        //获取所有数据
        List<SafeMaterialInfo> materialInfoList = safeMaterialInfoService.list();
        //获取所有部门
        List<DepRPCRespDTO> depInfoList = getDepInfoList();
        //获取所有物资类型
        List<SafeMaterialClassifyInfo> classifyInfoList = safeMaterialClassifyInfoService.getParentList();
        //循环部门
        List<MaterialDepartmentDto> departmentDtoList = new ArrayList<>();
        recursiveDep(depInfoList,departmentDtoList,materialInfoList,classifyInfoList);
 
        return departmentDtoList;
    }
    private void recursiveDep(List<DepRPCRespDTO> depInfoList,List<MaterialDepartmentDto> departmentDtoList,List<SafeMaterialInfo> materialInfoList,List<SafeMaterialClassifyInfo> classifyInfoList){
        for (DepRPCRespDTO dep:depInfoList){
            MaterialDepartmentDto materialDepartmentDto = new MaterialDepartmentDto();
            materialDepartmentDto.setDepId(dep.getDepId());
            materialDepartmentDto.setDepName(dep.getDepName());
            List<MaterialClassificationDto> classificationDtoList = new ArrayList<>();
            for (SafeMaterialClassifyInfo classifyInfo:classifyInfoList) {
                MaterialClassificationDto classificationDto = new MaterialClassificationDto();
                classificationDto.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
                classificationDto.setMaterialClassifyId(classifyInfo.getId());
                //过滤出物资数据
                List<SafeMaterialInfo> selectMaterialList = materialInfoList
                        .stream()
                        .filter(item -> classifyInfo.getId().equals(item.getBigClassifyId()) && dep.getDepId().equals(item.getDepId()))
                        .collect(Collectors.toList());
                List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
                if(selectMaterialList.size()>0){
                    //循环物资
                    for (SafeMaterialInfo materialInfo:selectMaterialList){
                        //填充基础物资数据
                        BaseMaterialDto baseMaterialDto = new BaseMaterialDto();
                        baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
                        baseMaterialDto.setSmId(materialInfo.getId());
                        baseMaterialDtoList.add(baseMaterialDto);
                    }
                    classificationDto.setBaseMaterialList(baseMaterialDtoList);
                    classificationDtoList.add(classificationDto);
                }
            }
            if(classificationDtoList.size()>0){
                materialDepartmentDto.setClassificationList(classificationDtoList);
                departmentDtoList.add(materialDepartmentDto);
            }
            //子集部门
            if(!CollectionUtils.isEmpty(dep.getChildren())){
                this.recursiveDep(dep.getChildren(),departmentDtoList,materialInfoList,classifyInfoList);
            }
        }
    }
    @Override
    public SearchResultVO<List<SafeMaterialDto>> listByPage(ContextCacheUser currentUser,PageQuery<SafeMaterialQuery> pageQuery) {
        Page<SafeMaterialDO> page= new Page(pageQuery.getPageIndex(),pageQuery.getPageSize());
        SafeMaterialQuery query = pageQuery.getSearchParams();
        query.setDepId(currentUser.getDepId());
        List<SafeMaterialDO> materialInfoList = safeMaterialInfoService.listByPage(page, query);
        List<SafeMaterialDto> materialDtoList = new ArrayList<>();
        for (SafeMaterialDO materialDO:materialInfoList){
            SafeMaterialDto materialDto = new SafeMaterialDto();
            BeanUtils.copyProperties(materialDO,materialDto);
            if(null != ConsumableEnum.getByCode(materialDO.getConsumable())){
                materialDto.setConsumableName(ConsumableEnum.getByCode(materialDO.getConsumable()).getValue());
            }
            materialDtoList.add(materialDto);
        }
        return new SearchResultVO<>(
                true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                materialDtoList,ResultCodes.OK);
 
 
    }
    private DepInfoRPCRespDTO getDepInfoByDepId(Long deptId) {
        DepInfoRPCRespDTO dep = new DepInfoRPCRespDTO();
        ResultVO<DepInfoRPCRespDTO> rpcResult = accountDepartmentService.getDepInfoByDepId(deptId);
        if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
            if (rpcResult.getData() != null) {
                dep = (DepInfoRPCRespDTO) rpcResult.getData();
            }else {
                throw new EquipmentException(ResultCodes.CLIENT_DEP_NOT_EXIST);
            }
        } else {
            throw new EquipmentException(ResultCodes.RPC_RESULT_NULL);
        }
        return dep;
    }
    private List<DepRPCRespDTO> getDepInfoList() {
        List<DepRPCRespDTO> depInfoList = new ArrayList<>();
        ResultVO<List<DepRPCRespDTO>> rpcResult = accountDepartmentService.depList();
        if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
            if (rpcResult.getData() != null) {
                depInfoList = (List<DepRPCRespDTO>) rpcResult.getData();
            }else{
                throw new EquipmentException(ResultCodes.CLIENT_DEP_NOT_EXIST);
            }
        } else {
            throw new EquipmentException(ResultCodes.CLIENT_DEP_NOT_EXIST);
        }
        return depInfoList;
    }
 
    private String generateSerialNum(int count){
        if(count < 0){
            return null;
        }
        String code = null;
        String prefix = "ID-";
        String serialCode = null;
        if(count >= 10000){
            serialCode = "" + (count+1);
        }else if(count >=0){
            String countStr = String.valueOf(count+1);
            serialCode = "00000".substring(0,(5 - countStr.length()))+countStr;
        }
        if(serialCode != null && !serialCode.isEmpty()){
            code = prefix+serialCode;
        }
        return code;
    }
}