package com.gkhy.safePlatform.equipment.service.impl;
|
|
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.enums.ResultCodes;
|
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.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.repository.SafeMaterialDetailInfoRepository;
|
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.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.security.core.parameters.P;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
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;
|
|
@Override
|
public ResultVO save(SafeMaterialAddReq req) {
|
ResultVO resultVO = null;
|
//获取部门信息
|
DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
|
|
if(null == ConsumableEnum.getByCode(req.getConsumable())){
|
throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"耗材类型不合法!");
|
}
|
//获取物资类型
|
SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getMaterialClassifyId());
|
if(null == classifyInfo){
|
throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
|
}
|
//获取数量
|
int safeMaterialTotalCount = safeMaterialInfoService.getTotalCount();
|
SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
|
BeanUtils.copyProperties(req,safeMaterialInfo);
|
safeMaterialInfo.setDepName(depInfo.getDepName());
|
safeMaterialInfo.setSerialNum(this.generateSerialNum(safeMaterialTotalCount));
|
//插入
|
boolean flag = safeMaterialInfoService.save(safeMaterialInfo);
|
if(flag){
|
resultVO = new ResultVO(ResultCodes.OK);
|
}else {
|
resultVO = new ResultVO(ResultCodes.SERVER_ADD_ERROR);
|
}
|
|
return resultVO;
|
}
|
|
@Override
|
public ResultVO update(SafeMaterialModReq req) {
|
ResultVO resultVO = null;
|
//获取部门信息
|
DepInfoRPCRespDTO depInfo = getDepInfoByDepId(req.getDepId());
|
|
if(null == ConsumableEnum.getByCode(req.getConsumable())){
|
throw new EquipmentException(ResultCodes.CLIENT_PARAM_ILLEGAL,"耗材类型不合法!");
|
}
|
//获取物资类型
|
SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(req.getMaterialClassifyId());
|
if(null == classifyInfo){
|
throw new EquipmentException(EquipmentResultCodes.DATA_NOT_EXIST,"物资类型不存在!");
|
}
|
SafeMaterialInfo safeMaterialInfo = new SafeMaterialInfo();
|
BeanUtils.copyProperties(req,safeMaterialInfo);
|
safeMaterialInfo.setDepName(depInfo.getDepName());
|
//跟新
|
boolean flag = safeMaterialInfoService.updateById(safeMaterialInfo);
|
if(flag){
|
resultVO = new ResultVO(ResultCodes.OK);
|
}else {
|
resultVO = new ResultVO(ResultCodes.SERVER_UPDATE_ERROR);
|
}
|
|
return resultVO;
|
}
|
|
@Override
|
public ResultVO queryById(Long id) {
|
if(null == id){
|
throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
|
}
|
SafeMaterialInfo safeMaterialInfo = safeMaterialInfoService.queryById(id);
|
SafeMaterialDto safeMaterialDto = new SafeMaterialDto();
|
if(null != safeMaterialInfo){
|
SafeMaterialClassifyInfo classifyInfo = safeMaterialClassifyInfoService.queryById(safeMaterialInfo.getMaterialClassifyId());
|
Integer validStockCount = safeMaterialDetailInfoService.getValidStockCount(safeMaterialInfo.getId());
|
BeanUtils.copyProperties(safeMaterialInfo,safeMaterialDto);
|
safeMaterialDto.setConsumable(ConsumableEnum.getByCode(safeMaterialInfo.getConsumable()).getCode());
|
if(null != classifyInfo){
|
safeMaterialDto.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
|
}
|
safeMaterialDto.setValidStockCount(validStockCount);
|
|
}
|
return new ResultVO(ResultCodes.OK,safeMaterialDto);
|
}
|
|
public ResultVO delete(Long id) {
|
ResultVO resultVO = null;
|
//删除之前检查详表中是否还有该物物资数据
|
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);
|
}
|
return resultVO;
|
}
|
|
@Override
|
public ResultVO deleteBatch(Long[] ids) {
|
if(ids.length == 0){
|
throw new EquipmentException(ResultCodes.CLIENT_PARAM_NULL);
|
}
|
List<Long> idList = Arrays.asList(ids);
|
int count = safeMaterialDetailInfoService.getCountBySmIds(idList);
|
//判断是否绑定具体安全物资数据
|
if(count > 0){
|
throw new EquipmentException(EquipmentResultCodes.DATA_HAS_BEEN_BOND,"物资已被绑定物资详情数据,不可删除!");
|
}
|
safeMaterialInfoService.deleteBatch(idList);
|
return new ResultVO(ResultCodes.OK);
|
}
|
|
/**
|
* 获取列表
|
* @return
|
*/
|
@Override
|
public ResultVO list() {
|
//获取所有数据
|
List<SafeMaterialInfo> materialInfoList = safeMaterialInfoService.list();
|
//获取所有部门
|
List<DepRPCRespDTO> depInfoList = getDepInfoList();
|
//获取所有物资类型
|
List<SafeMaterialClassifyInfo> classifyInfoList = safeMaterialClassifyInfoService.getList();
|
//循环物资分类
|
List<MaterialClassificationDto> classificationDtoList = new ArrayList<>();
|
for (SafeMaterialClassifyInfo classifyInfo:classifyInfoList){
|
MaterialClassificationDto classificationDto = new MaterialClassificationDto();
|
classificationDto.setMaterialClassifyId(classifyInfo.getId());
|
classificationDto.setMaterialClassifyName(classifyInfo.getMaterialClassifyName());
|
//循环部门
|
List<MaterialDepartmentDto> departmentDtoList = new ArrayList<>();
|
for (DepRPCRespDTO dep:depInfoList){
|
//过滤出物资数据
|
List<SafeMaterialInfo> selectMaterialList = materialInfoList
|
.stream()
|
.filter(item -> classifyInfo.getId().equals(item.getMaterialClassifyId()) && dep.getDepId().equals(item.getDepId()))
|
.collect(Collectors.toList());
|
if(selectMaterialList.size()>0){
|
//填充部门数据
|
MaterialDepartmentDto departmentDto = new MaterialDepartmentDto();
|
departmentDto.setDepId(dep.getDepId());
|
departmentDto.setDepName(dep.getDepName());
|
|
List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
|
//循环物资
|
for (SafeMaterialInfo materialInfo:selectMaterialList){
|
//填充基础物资数据
|
BaseMaterialDto baseMaterialDto = new BaseMaterialDto();
|
baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
|
baseMaterialDto.setSmId(materialInfo.getId());
|
baseMaterialDtoList.add(baseMaterialDto);
|
}
|
departmentDto.setBaseMaterialList(baseMaterialDtoList);
|
departmentDtoList.add(departmentDto);
|
}
|
//子集部门
|
if(!CollectionUtils.isEmpty(dep.getChildren())){
|
this.recursiveDep(dep.getChildren(),departmentDtoList,materialInfoList,classifyInfo.getId());
|
}
|
}
|
classificationDto.setDepartmentList(departmentDtoList);
|
classificationDtoList.add(classificationDto);
|
}
|
|
return new ResultVO(ResultCodes.OK,classificationDtoList);
|
}
|
private void recursiveDep(List<DepRPCRespDTO> depList,List<MaterialDepartmentDto> departmentDtoList,List<SafeMaterialInfo> materialInfoList,Long classifyId){
|
for(DepRPCRespDTO dep:depList){
|
//过滤出物资数据
|
List<SafeMaterialInfo> selectMaterialList = materialInfoList
|
.stream()
|
.filter(item -> classifyId.equals(item.getMaterialClassifyId()) && dep.getDepId().equals(item.getDepId()))
|
.collect(Collectors.toList());
|
if(selectMaterialList.size()>0){
|
//填充部门数据
|
MaterialDepartmentDto departmentDto = new MaterialDepartmentDto();
|
departmentDto.setDepId(dep.getDepId());
|
departmentDto.setDepName(dep.getDepName());
|
|
List<BaseMaterialDto> baseMaterialDtoList = new ArrayList<>();
|
//循环物资
|
for (SafeMaterialInfo materialInfo:selectMaterialList){
|
//填充基础物资数据
|
BaseMaterialDto baseMaterialDto = new BaseMaterialDto();
|
baseMaterialDto.setMaterialName(materialInfo.getMaterialName());
|
baseMaterialDto.setSmId(materialInfo.getId());
|
baseMaterialDtoList.add(baseMaterialDto);
|
}
|
departmentDto.setBaseMaterialList(baseMaterialDtoList);
|
departmentDtoList.add(departmentDto);
|
}
|
//子集
|
if(!CollectionUtils.isEmpty(dep.getChildren())){
|
this.recursiveDep(dep.getChildren(),departmentDtoList,materialInfoList,classifyId);
|
}
|
}
|
}
|
@Override
|
public SearchResultVO<List<SafeMaterialDto>> listByPage(PageQuery<SafeMaterialQuery> pageQuery) {
|
Page<SafeMaterialDO> page= new Page(pageQuery.getPageIndex(),pageQuery.getPageSize());
|
List<SafeMaterialDO> materialInfoList = safeMaterialInfoService.listByPage(page, pageQuery.getSearchParams());
|
List<Long> materialIdList = new ArrayList<>();
|
List<SafeMaterialDetailCountDO> statisticsList = new ArrayList<>();
|
if(materialInfoList.size()>0){
|
materialIdList = materialInfoList.stream().map(SafeMaterialDO::getId).collect(Collectors.toList());
|
}
|
if(materialIdList.size()>0){
|
statisticsList = safeMaterialDetailInfoService.getStatisticsValidStock(materialIdList);
|
}
|
List<SafeMaterialDto> materialDtoList = new ArrayList<>();
|
for (SafeMaterialDO materialDO:materialInfoList){
|
SafeMaterialDto materialDto = new SafeMaterialDto();
|
BeanUtils.copyProperties(materialDO,materialDto);
|
materialDto.setConsumableName(ConsumableEnum.getByCode(materialDO.getConsumable()).getValue());
|
//过滤出当前物资详细
|
List<SafeMaterialDetailCountDO> selectList = statisticsList
|
.stream()
|
.filter(item -> item.getSmId().equals(materialDO.getId()))
|
.collect(Collectors.toList());
|
if(selectList.size()>0){
|
materialDto.setValidStockCount(selectList.get(0).getCount());
|
}else{
|
materialDto.setValidStockCount(0);
|
}
|
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;
|
}
|
}
|