zhangfeng
2022-11-08 f241a39f07acc5d5ede28b4152d74cbb84e4885b
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
package com.gkhy.safePlatform.equipment.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.equipment.entity.SafeMaterialClassifyInfo;
import com.gkhy.safePlatform.equipment.excepiton.EquipmentException;
import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialClassifyAddReq;
import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialClassifyModReq;
import com.gkhy.safePlatform.equipment.model.dto.req.SafeMaterialClassifyQuery;
import com.gkhy.safePlatform.equipment.model.dto.resp.SafeMaterialClassifyDto;
import com.gkhy.safePlatform.equipment.service.MaterialClassifyService;
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialClassifyInfoService;
import com.gkhy.safePlatform.equipment.service.baseService.SafeMaterialInfoService;
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;
 
@Service("MaterialClassifyService")
public class MaterialClassifyServiceImpl implements MaterialClassifyService {
    @Autowired
    private SafeMaterialClassifyInfoService safeMaterialClassifyInfoService;
    @Autowired
    private SafeMaterialInfoService safeMaterialInfoService;
 
    @Override
    public ResultVO<List<SafeMaterialClassifyDto>> list(SafeMaterialClassifyQuery query) {
        List<SafeMaterialClassifyInfo> list = safeMaterialClassifyInfoService.listByCondition(query);
        List<SafeMaterialClassifyDto> respList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for(SafeMaterialClassifyInfo classifyInfo : list){
                SafeMaterialClassifyDto respDTO = new SafeMaterialClassifyDto();
                respDTO.setId(classifyInfo.getId());
                respDTO.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
                respList.add(respDTO);
            }
        }
        return new ResultVO<>(ResultCodes.OK,respList);
    }
 
    @Override
    public ResultVO save(ContextCacheUser currentUser, SafeMaterialClassifyAddReq req) {
        ResultVO resultVO = null;
        SafeMaterialClassifyInfo classifyInfo = new SafeMaterialClassifyInfo();
        classifyInfo.setMaterialClassifyName(req.getMaterialClassifyName());
        boolean flag = safeMaterialClassifyInfoService.save(classifyInfo);
        if (flag){
            resultVO = new ResultVO(ResultCodes.OK);
        }else {
            resultVO = new ResultVO(ResultCodes.SERVER_ADD_ERROR);
        }
        return  resultVO;
    }
 
    @Override
    public ResultVO update(ContextCacheUser currentUser, SafeMaterialClassifyModReq req) {
        ResultVO resultVO = null;
        if(req.getId() == null){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        SafeMaterialClassifyInfo classifyInfo = new SafeMaterialClassifyInfo();
        classifyInfo.setMaterialClassifyName(req.getMaterialClassifyName());
        classifyInfo.setId(req.getId());
        boolean flag = safeMaterialClassifyInfoService.updateById(classifyInfo);
        if (flag){
            resultVO = new ResultVO(ResultCodes.OK);
        }else {
            resultVO = new ResultVO(ResultCodes.SERVER_UPDATE_ERROR);
        }
        return  resultVO;
    }
 
    @Override
    public ResultVO delete(Long id) {
        if(id == null){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        ResultVO resultVO = null;
        //判断是否被绑定
        int count = safeMaterialInfoService.getCountByClassify(id);
        if(count > 0){
            throw new EquipmentException(ResultCodes.SERVER_DEL_ERROR,"该类型已被绑定,不可删除!");
        }else{
            //如果没有被绑定,逻辑删除
            SafeMaterialClassifyInfo classifyInfo = new SafeMaterialClassifyInfo();
            classifyInfo.setId(id);
            classifyInfo.setDelFlag(1);
            boolean flag = safeMaterialClassifyInfoService.updateById(classifyInfo);
            if (flag){
                resultVO = new ResultVO(ResultCodes.OK);
            }else {
                resultVO = new ResultVO(ResultCodes.SERVER_DEL_ERROR);
            }
        }
        return  resultVO;
    }
 
    @Override
    public ResultVO<SafeMaterialClassifyDto> queryById(Long id) {
        if(id == null){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(id);
        SafeMaterialClassifyDto respDTO = new SafeMaterialClassifyDto();
        respDTO.setId(classifyInfo.getId());
        respDTO.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
        return new ResultVO<>(ResultCodes.OK,respDTO);
    }
}