package com.gkhy.safePlatform.equipment.service.impl; import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.vo.ResultVO; import com.gkhy.safePlatform.equipment.entity.SafeMaterialClassifyDO; import com.gkhy.safePlatform.equipment.entity.SafeMaterialClassifyInfo; import com.gkhy.safePlatform.equipment.enums.EquipmentResultCodes; 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.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 list() { List list = safeMaterialClassifyInfoService.list(); //过滤出父级 List parentList = list .stream() .filter(item -> item.getParentId().equals(0l)) .collect(Collectors.toList()); List respDTOList = new ArrayList<>(); //只有二级不采用递归 for (SafeMaterialClassifyInfo classifyInfo:parentList) { SafeMaterialClassifyDto respDTO = new SafeMaterialClassifyDto(); respDTO.setId(classifyInfo.getId()); respDTO.setMaterialClassifyName(classifyInfo.getMaterialClassifyName()); respDTO.setParentId(classifyInfo.getParentId()); List childList = list .stream() .filter(item -> item.getParentId().equals(classifyInfo.getId())) .collect(Collectors.toList()); List childDTOList = new ArrayList<>(); for (SafeMaterialClassifyInfo child:childList){ SafeMaterialClassifyDto childDTO = new SafeMaterialClassifyDto(); childDTO.setId(child.getId()); childDTO.setMaterialClassifyName(child.getMaterialClassifyName()); 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; SafeMaterialClassifyInfo classifyInfo = new SafeMaterialClassifyInfo(); classifyInfo.setMaterialClassifyName(req.getMaterialClassifyName()); 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 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); } SafeMaterialClassifyInfo classify = safeMaterialClassifyInfoService.queryById(id); if(null == classify){ throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST); } if(0 == classify.getParentId()){//一级 List 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(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 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()); List childDtoList = new ArrayList<>(); for (SafeMaterialClassifyInfo child:childList){ SafeMaterialClassifyDto childDto = new SafeMaterialClassifyDto(); childDto.setId(child.getId()); childDto.setMaterialClassifyName(child.getMaterialClassifyName()); childDto.setParentId(child.getParentId()); 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 getClassifyListByIds(List ids){ if(CollectionUtils.isEmpty(ids)){ throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL); } List list = safeMaterialClassifyInfoService.getClassifyListByIds(ids); List classifyDtoList = new ArrayList<>(); for (SafeMaterialClassifyInfo classifyInfo:list){ SafeMaterialClassifyDto classifyDto = new SafeMaterialClassifyDto(); BeanUtils.copyProperties(classifyInfo,classifyDto); classifyDtoList.add(classifyDto); } return classifyDtoList; } @Override public List getTraceabilityClassifyList(List smallClassifyIds) { if(CollectionUtils.isEmpty(smallClassifyIds)){ throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL); } return safeMaterialClassifyInfoService.getTraceabilityClassifyList(smallClassifyIds); } }