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 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()); 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 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 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 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 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); } @Override public SearchResultVO> listByPage(ContextCacheUser currentUser,PageQuery pageQuery) { Page page = new Page<>(pageQuery.getPageIndex(),pageQuery.getPageSize()); List parentList = safeMaterialClassifyInfoService.listByPage(page, pageQuery.getSearchParams()); List childList = new ArrayList<>(); if(!CollectionUtils.isEmpty(parentList)){ List parentIdList = parentList.stream().map(SafeMaterialClassifyInfo::getId).collect(Collectors.toList()); childList = safeMaterialClassifyInfoService.getListByParentIds(parentIdList); } List respList = new ArrayList<>(); for(SafeMaterialClassifyInfo parentClassify:parentList){ SafeMaterialClassifyDto classifyDto = new SafeMaterialClassifyDto(); classifyDto.setId(parentClassify.getId()); classifyDto.setMaterialClassifyName(parentClassify.getMaterialClassifyName()); classifyDto.setParentId(parentClassify.getParentId()); //获取子级 List selectList = childList .stream() .filter(cl -> cl.getParentId().equals(parentClassify.getId())) .collect(Collectors.toList()); List 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); } }