SZH
2023-03-09 53e7dce8d55487cbac64e4374ec9aa1b52a6c6ed
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
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.commons.co.ContextCacheUser;
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.equipment.entity.SafeMaterialClassifyDO;
import com.gkhy.safePlatform.equipment.entity.SafeMaterialClassifyInfo;
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.MaterialClassifyQuery;
import com.gkhy.safePlatform.equipment.model.dto.req.ParamForm;
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.resp.SafeMaterialClassifyDto;
import com.gkhy.safePlatform.equipment.model.dto.resp.SafeMaterialClassifyStockDto;
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.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.stream.Collectors;
 
@Service("MaterialClassifyService")
public class MaterialClassifyServiceImpl implements MaterialClassifyService {
    @Autowired
    private SafeMaterialClassifyInfoService safeMaterialClassifyInfoService;
    @Autowired
    private SafeMaterialInfoService safeMaterialInfoService;
 
    @Override
    public List<SafeMaterialClassifyDto> list() {
        List<SafeMaterialClassifyInfo> list = safeMaterialClassifyInfoService.list();
        //过滤出父级
        List<SafeMaterialClassifyInfo> parentList = list
                .stream()
                .filter(item -> item.getParentId().equals(0l))
                .collect(Collectors.toList());
 
        List<SafeMaterialClassifyDto> respDTOList = new ArrayList<>();
        //只有二级不采用递归
        for (SafeMaterialClassifyInfo classifyInfo:parentList) {
            SafeMaterialClassifyDto respDTO = new SafeMaterialClassifyDto();
            respDTO.setId(classifyInfo.getId());
            respDTO.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
            respDTO.setParentId(classifyInfo.getParentId());
            List<SafeMaterialClassifyInfo> childList = list
                    .stream()
                    .filter(item -> item.getParentId().equals(classifyInfo.getId()))
                    .collect(Collectors.toList());
            List<SafeMaterialClassifyDto> childDTOList = new ArrayList<>();
            for (SafeMaterialClassifyInfo child:childList){
                SafeMaterialClassifyDto childDTO = new SafeMaterialClassifyDto();
                childDTO.setId(child.getId());
                childDTO.setMaterialClassifyName(child.getMaterialClassifyName());
                if(null != child.getConsumable()){
                    childDTO.setConsumableName(ConsumableEnum.getByCode(child.getConsumable()).getValue());
                }
                childDTO.setConsumable(child.getConsumable());
                childDTO.setParentId(child.getParentId());
                childDTOList.add(childDTO);
            }
            respDTO.setChildList(childDTOList);
            respDTOList.add(respDTO);
        }
 
        return respDTOList;
    }
 
    @Override
    public ResultVO save(ContextCacheUser currentUser, SafeMaterialClassifyAddReq req) {
        ResultVO resultVO = null;
        if(null != req.getParentId()){
            if(null == req.getConsumable()){
                throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL,"是否耗材未选择");
            }
            if(null == ConsumableEnum.getByCode(req.getConsumable())){
                throw new EquipmentException(EquipmentResultCodes.DATA_ILLEGAL);
            }
        }
 
        SafeMaterialClassifyInfo classifyInfo = new SafeMaterialClassifyInfo();
        classifyInfo.setId(IdUtil.getSnowflake(0,0).nextId());
        classifyInfo.setMaterialClassifyName(req.getMaterialClassifyName());
        classifyInfo.setConsumable(req.getConsumable());
        if(null != req.getParentId()){
            classifyInfo.setParentId(req.getParentId());
        }else{
            classifyInfo.setParentId(0l);
        }
 
        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 safeMaterialClassifyInfo = safeMaterialClassifyInfoService.queryById(req.getId());
        if(safeMaterialClassifyInfo.getParentId() != 0 ){
            if(null == req.getConsumable()){
                throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL,"是否耗材未选择");
            }
            if(null == ConsumableEnum.getByCode(req.getConsumable())){
                throw new EquipmentException(EquipmentResultCodes.DATA_ILLEGAL);
            }
        }
 
        SafeMaterialClassifyInfo classifyInfo = new SafeMaterialClassifyInfo();
        classifyInfo.setMaterialClassifyName(req.getMaterialClassifyName());
        classifyInfo.setConsumable(req.getConsumable());
        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(ContextCacheUser currentUser,Long id) {
        if(id == null){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        SafeMaterialClassifyInfo classify = safeMaterialClassifyInfoService.queryById(id);
        if(null == classify){
            throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST);
        }
        if(0 == classify.getParentId()){//一级
            List<SafeMaterialClassifyInfo> childrenList = safeMaterialClassifyInfoService.getListByParentId(id);
            if (childrenList.size()>0){
                throw new EquipmentException(ResultCodes.SERVER_DEL_ERROR,"改类型存在小类,不可删除!");
            }
        }else {//二级
            //判断是否被绑定
            int count = safeMaterialInfoService.getCountBySmallClassifyId(id);
            if(count > 0){
                throw new EquipmentException(ResultCodes.SERVER_DEL_ERROR,"该类型已被绑定,不可删除!");
            }
        }
        //如果没有被绑定,逻辑删除
        SafeMaterialClassifyInfo classifyInfo = new SafeMaterialClassifyInfo();
        classifyInfo.setId(id);
        classifyInfo.setDelFlag(1);
        safeMaterialClassifyInfoService.updateById(classifyInfo);
 
        return new ResultVO(ResultCodes.OK);
    }
 
    @Override
    public SafeMaterialClassifyDto queryById(ContextCacheUser currentUser,Long id) {
        if(id == null){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(id);
        if(null == classifyInfo){
            throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST);
        }
        List<SafeMaterialClassifyInfo> childList = new ArrayList<>();
        if(0l ==  classifyInfo.getParentId()){
            childList = safeMaterialClassifyInfoService.getListByParentId(id);
        }
        SafeMaterialClassifyDto respDTO = new SafeMaterialClassifyDto();
        respDTO.setId(classifyInfo.getId());
        respDTO.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
        respDTO.setParentId(classifyInfo.getParentId());
        respDTO.setConsumable(classifyInfo.getConsumable());
        respDTO.setConsumableName(ConsumableEnum.getByCode(classifyInfo.getConsumable()).getValue());
        List<SafeMaterialClassifyDto> childDtoList = new ArrayList<>();
        for (SafeMaterialClassifyInfo child:childList){
            SafeMaterialClassifyDto childDto = new SafeMaterialClassifyDto();
            childDto.setId(child.getId());
            childDto.setMaterialClassifyName(child.getMaterialClassifyName());
            childDto.setParentId(child.getParentId());
            childDto.setConsumable(child.getConsumable());
            if(null != child.getConsumable()){
                childDto.setConsumableName(ConsumableEnum.getByCode(child.getConsumable()).getValue());
            }
            childDtoList.add(childDto);
        }
        respDTO.setChildList(childDtoList);
        return respDTO;
    }
 
    /**
     * 根据小类id获取大小类相关信息
     * @param smallClassifyId
     * @return
     */
    public SafeMaterialClassifyDO getBigAndSmallClassifyInfo(Long smallClassifyId){
        if(null == smallClassifyId){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        SafeMaterialClassifyDO classifyDO = safeMaterialClassifyInfoService.getBigAndSmallClassify(smallClassifyId);
        return classifyDO;
    }
    /**
     * 根据小类ids获取小类相关信息
     * @param ids
     * @return
     */
    public List<SafeMaterialClassifyDto> getClassifyListByIds(List<Long> ids){
        if(CollectionUtils.isEmpty(ids)){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        List<SafeMaterialClassifyInfo> list = safeMaterialClassifyInfoService.getClassifyListByIds(ids);
        List<SafeMaterialClassifyDto> classifyDtoList = new ArrayList<>();
        for (SafeMaterialClassifyInfo classifyInfo:list){
            SafeMaterialClassifyDto classifyDto = new SafeMaterialClassifyDto();
            BeanUtils.copyProperties(classifyInfo,classifyDto);
            classifyDtoList.add(classifyDto);
        }
        return classifyDtoList;
    }
 
    @Override
    public List<SafeMaterialClassifyDO> getTraceabilityClassifyList(List<Long> smallClassifyIds) {
        if(CollectionUtils.isEmpty(smallClassifyIds)){
            throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
        }
        return safeMaterialClassifyInfoService.getTraceabilityClassifyList(smallClassifyIds);
    }
 
    @Override
    public SearchResultVO<List<SafeMaterialClassifyDto>> listByPage(ContextCacheUser currentUser,PageQuery<MaterialClassifyQuery> pageQuery) {
        Page<SafeMaterialClassifyInfo> page = new Page<>(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<SafeMaterialClassifyInfo> parentList = safeMaterialClassifyInfoService.listByPage(page, pageQuery.getSearchParams());
        List<SafeMaterialClassifyInfo> childList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(parentList)){
            List<Long> parentIdList = parentList.stream().map(SafeMaterialClassifyInfo::getId).collect(Collectors.toList());
            childList = safeMaterialClassifyInfoService.getListByParentIds(parentIdList);
        }
 
        List<SafeMaterialClassifyDto> respList = new ArrayList<>();
        for(SafeMaterialClassifyInfo parentClassify:parentList){
            SafeMaterialClassifyDto classifyDto = new SafeMaterialClassifyDto();
            classifyDto.setId(parentClassify.getId());
            classifyDto.setMaterialClassifyName(parentClassify.getMaterialClassifyName());
            classifyDto.setParentId(parentClassify.getParentId());
 
            //获取子级
            List<SafeMaterialClassifyInfo> selectList = childList
                    .stream()
                    .filter(cl -> cl.getParentId().equals(parentClassify.getId()))
                    .collect(Collectors.toList());
 
            List<SafeMaterialClassifyDto> childRespList = new ArrayList<>();
            for(SafeMaterialClassifyInfo child:selectList){
                SafeMaterialClassifyDto childClassify = new SafeMaterialClassifyDto();
                childClassify.setId(child.getId());
                childClassify.setMaterialClassifyName(child.getMaterialClassifyName());
                childClassify.setParentId(child.getParentId());
                childClassify.setConsumable(child.getConsumable());
                if(null != child.getConsumable()){
                    childClassify.setConsumableName(ConsumableEnum.getByCode(child.getConsumable()).getValue());
                }
                childRespList.add(childClassify);
            }
            classifyDto.setChildList(childRespList);
            respList.add(classifyDto);
        }
        return new SearchResultVO<>(true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                respList,
                ResultCodes.OK);
    }
 
 
 
 
}