package com.gkhy.hazmat.system.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gkhy.hazmat.common.api.CommonPage; import com.gkhy.hazmat.common.config.IdTableNameHandler; import com.gkhy.hazmat.common.domain.entity.SysUser; import com.gkhy.hazmat.common.enums.CodePrexEnum; import com.gkhy.hazmat.common.enums.HazmatStatusEnum; import com.gkhy.hazmat.common.enums.OperateStatusEnum; import com.gkhy.hazmat.common.enums.UserTypeEnum; import com.gkhy.hazmat.common.exception.ApiException; import com.gkhy.hazmat.common.utils.PageUtils; import com.gkhy.hazmat.common.utils.SecurityUtils; import com.gkhy.hazmat.system.domain.*; import com.gkhy.hazmat.system.domain.vo.HzHazmatWarehouseVO; import com.gkhy.hazmat.system.mapper.*; import com.gkhy.hazmat.system.service.HzHazmatService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; /** *

* 危化品表 服务实现类 *

* * @author kzy * @since 2024-08-05 14:41:40 */ @Service public class HzHazmatServiceImpl extends ServiceImpl implements HzHazmatService { @Autowired private HzHazmatFlowMapper hazmatFlowMapper; @Autowired private HzWarehouseRecordMapper warehouseRecordMapper; @Autowired private HzHazmatBasicMapper hazmatBasicMapper; @Autowired private HzWarehouseMapper warehouseMapper; @Override public CommonPage selectHazmatList(HzHazmat hzHazmat) { if(hzHazmat.getWarehouseId()==null||hzHazmat.getBasicId()==null){ throw new ApiException("仓库id或者危化品基础数据id不能为空"); } SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); hzHazmat.setCompanyId(currentUser.getCompanyId()); PageUtils.startPage(); List hazmatList = baseMapper.selectHazmatList(hzHazmat); IdTableNameHandler.removeCurrentId(); return CommonPage.restPage(hazmatList); } @Override public CommonPage selectHazmatGroupWarehouse(HzHazmat hzHazmat) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); hzHazmat.setCompanyId(currentUser.getCompanyId()); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); PageUtils.startPage(); List hazmatList = baseMapper.selectHazmatGroupWareHouse(hzHazmat); if(!hazmatList.isEmpty()) { List warehouseIds = hazmatList.stream().map(HzHazmatWarehouseVO::getWarehouseId).collect(Collectors.toList()); List basicIds = hazmatList.stream().map(HzHazmatWarehouseVO::getBasicId).collect(Collectors.toList()); List cupboardIds = hazmatList.stream().map(HzHazmatWarehouseVO::getCupboardId).collect(Collectors.toList()); List hazmatBasicList = hazmatBasicMapper.selectHazmatBasicListByIds(basicIds); List warehouseList = warehouseMapper.selectWarehouseListByIds(warehouseIds); List hzWarehouseCupboards = warehouseMapper.selectByCupboardIds(cupboardIds); Map hazmatBasicMap=hazmatBasicList.stream().collect(Collectors.toMap(HzHazmatBasic::getId, item->item)); Map warehouseMap=warehouseList.stream().collect(Collectors.toMap(HzWarehouse::getId, item->item)); Map cupboardMap = hzWarehouseCupboards.stream().collect(Collectors.toMap(HzWarehouseCupboard::getId, item -> item)); for(HzHazmatWarehouseVO hazmatWarehouseVO:hazmatList){ HzHazmatBasic hazmatBasic=hazmatBasicMap.get(hazmatWarehouseVO.getBasicId()); HzWarehouse warehouse=warehouseMap.get(hazmatWarehouseVO.getWarehouseId()); HzWarehouseCupboard hzWarehouseCupboard = cupboardMap.get(hazmatWarehouseVO.getCupboardId()); if(hazmatBasic!=null){ hazmatWarehouseVO.setHazmatBasic(hazmatBasic); } if(warehouse!=null){ hazmatWarehouseVO.setWarehouseName(warehouse.getName()); } if (hzWarehouseCupboard != null){ hazmatWarehouseVO.setCupboardName(hzWarehouseCupboard.getCupboardName()); } } } IdTableNameHandler.removeCurrentId(); return CommonPage.restPage(hazmatList); } @Override public HzHazmat selectHazmatById(Long hazmatId) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat hazmat = baseMapper.selectById(hazmatId); checkUserAllowed(hazmat,currentUser); IdTableNameHandler.removeCurrentId(); return hazmat; } @Override public int deleteHazmatById(Long hazmatId) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat hazmat=baseMapper.selectById(hazmatId); if(hazmat==null){ throw new ApiException("危化品不存在"); } checkUserAllowed(hazmat,currentUser); baseMapper.deleteHazmatById(hazmatId); IdTableNameHandler.removeCurrentId(); return 0; } @Override public HzHazmat selectHazmatByCode(String code) { if(!code.startsWith(CodePrexEnum.MATERIAL.getCode())){ throw new ApiException("该条码格式不正确"); } SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat hazmat=baseMapper.selectHazmatByCode(code,currentUser.getCompanyId()); IdTableNameHandler.removeCurrentId(); if(hazmat==null){ throw new ApiException("该条码数据不存在"); } return hazmat; } @Override @Transactional(rollbackFor = RuntimeException.class) public void hazmatUse(Long hazmatId, Integer used) { SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat hazmat=getById(hazmatId); if(!hazmat.getState().equals(HazmatStatusEnum.WAREHOUSEIN.getCode())&&!hazmat.getState().equals(HazmatStatusEnum.USEWAREHOUSEIN.getCode())){ throw new ApiException("危化品不在库中,不能进行领用操作"); } checkUserAllowed(hazmat,currentUser); //获取变动前仓库库存 int count = baseMapper.selectHazmatCountOfWarehouse(hazmat.getWarehouseId(), hazmat.getBasicId(), currentUser.getCompanyId(),hazmat.getCupboardId()); //生成流向 BigDecimal remaining=hazmat.getRemaining(); HzHazmatFlow hazmatFlow=new HzHazmatFlow(); hazmatFlow.setHazmatId(hazmatId); hazmatFlow.setBasicId(hazmat.getBasicId()); hazmatFlow.setState(OperateStatusEnum.USING.getCode()); hazmatFlow.setCompanyId(currentUser.getCompanyId()); hazmatFlow.setNum(remaining.multiply(BigDecimal.valueOf(-1))); hazmatFlow.setCreateId(currentUser.getId()); hazmatFlowMapper.insert(hazmatFlow); hazmat.setState(HazmatStatusEnum.USING.getCode()); if(used==1){//用尽流向 hazmatFlow=new HzHazmatFlow(); hazmatFlow.setHazmatId(hazmatId); hazmatFlow.setBasicId(hazmat.getBasicId()); hazmatFlow.setState(OperateStatusEnum.USE_UP.getCode()); hazmatFlow.setCompanyId(currentUser.getCompanyId()); hazmatFlow.setNum(remaining.multiply(BigDecimal.valueOf(-1))); hazmatFlow.setCreateId(currentUser.getId()); hazmatFlowMapper.insert(hazmatFlow); hazmat.setState(HazmatStatusEnum.USED.getCode()); hazmat.setRemaining(BigDecimal.valueOf(0)); } hazmat.setUpdateBy(currentUser.getUsername()); updateById(hazmat);//更新试剂状态 //生成库存变动记录 //新增危化品变动记录 HzWarehouseRecord warehouseRecord = new HzWarehouseRecord() .setWarehouseId(hazmat.getWarehouseId()) .setBasicId(hazmat.getBasicId()) .setCupboardId(hazmat.getCupboardId()) .setCreateId(currentUser.getId()) .setNum(-1) .setState(OperateStatusEnum.USING.getCode()) .setCompanyId(currentUser.getCompanyId()) .setRemaining(count-1); warehouseRecordMapper.insert(warehouseRecord); IdTableNameHandler.removeCurrentId(); } @Override @Transactional(rollbackFor = RuntimeException.class) public void hazmatReturn(HzHazmat hazmat) { if(hazmat.getId()==null||hazmat.getRemaining()==null||hazmat.getRemaining().compareTo(BigDecimal.ZERO)<=0){ throw new ApiException("参数不正确"); } SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat dbhazmat=getById(hazmat.getId()); BigDecimal remaining=dbhazmat.getRemaining(); if(!dbhazmat.getState().equals(HazmatStatusEnum.USING.getCode())&&!dbhazmat.getState().equals(HazmatStatusEnum.USED.getCode())){ throw new ApiException("危化品非使用中或者用尽状态,不能进行归还操作"); } if(dbhazmat.getState().equals(HazmatStatusEnum.USING.getCode())&&remaining.compareTo(hazmat.getRemaining())<0){ throw new ApiException("退还的容量不能高于库存容量"); } if(dbhazmat.getState().equals(HazmatStatusEnum.USED.getCode())){ HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(dbhazmat.getBasicId()); if(hazmatBasic.getMetering().compareTo(hazmat.getRemaining())<0) { throw new ApiException("退还的容量不能高于最小包装数量"); } } checkUserAllowed(dbhazmat,currentUser); //获取变动前仓库库存 int count = baseMapper.selectHazmatCountOfWarehouse(dbhazmat.getWarehouseId(), dbhazmat.getBasicId(), currentUser.getCompanyId(),dbhazmat.getCupboardId()); hazmat.setState(HazmatStatusEnum.USEWAREHOUSEIN.getCode()); hazmat.setUpdateBy(currentUser.getUsername()); updateById(hazmat); //生成流向 HzHazmatFlow hazmatFlow=new HzHazmatFlow(); hazmatFlow.setHazmatId(dbhazmat.getId()); hazmatFlow.setBasicId(dbhazmat.getBasicId()); hazmatFlow.setState(OperateStatusEnum.RETURN.getCode()); hazmatFlow.setCompanyId(currentUser.getCompanyId()); hazmatFlow.setNum(hazmat.getRemaining()); hazmatFlow.setCreateId(currentUser.getId()); hazmatFlowMapper.insert(hazmatFlow); //生成库存变动记录 //新增危化品变动记录 HzWarehouseRecord warehouseRecord = new HzWarehouseRecord() .setWarehouseId(dbhazmat.getWarehouseId()) .setBasicId(dbhazmat.getBasicId()) .setCupboardId(hazmat.getCupboardId()) .setCreateId(currentUser.getId()) .setNum(1) .setState(OperateStatusEnum.RETURN.getCode()) .setCompanyId(currentUser.getCompanyId()) .setRemaining(count+1); warehouseRecordMapper.insert(warehouseRecord); IdTableNameHandler.removeCurrentId(); } @Override @Transactional(rollbackFor = RuntimeException.class) public void hazmatUsed(Long hazmatId) { SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat hazmat=getById(hazmatId); if(!hazmat.getState().equals(HazmatStatusEnum.USING.getCode())){ throw new ApiException("危化品非使用中状态,不能进行用完登记操作"); } BigDecimal remaining=hazmat.getRemaining(); hazmat.setState(HazmatStatusEnum.USED.getCode()); hazmat.setRemaining(BigDecimal.valueOf(0)); hazmat.setUpdateBy(currentUser.getUsername()); updateById(hazmat); //生成流向 HzHazmatFlow hazmatFlow=new HzHazmatFlow(); hazmatFlow.setHazmatId(hazmat.getId()); hazmatFlow.setBasicId(hazmat.getBasicId()); hazmatFlow.setState(OperateStatusEnum.USE_UP.getCode()); hazmatFlow.setCompanyId(currentUser.getCompanyId()); hazmatFlow.setNum(remaining.multiply(BigDecimal.valueOf(-1))); hazmatFlow.setCreateId(currentUser.getId()); hazmatFlowMapper.insert(hazmatFlow); IdTableNameHandler.removeCurrentId(); } @Override @Transactional(rollbackFor = RuntimeException.class) public void hazmatDiscard(Long hazmatId) { SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat hazmat=getById(hazmatId); if(!hazmat.getState().equals(HazmatStatusEnum.WAREHOUSEIN.getCode())&&!hazmat.getState().equals(HazmatStatusEnum.USEWAREHOUSEIN.getCode())){ throw new ApiException("危化品不在库中,不能进行作废操作"); } checkUserAllowed(hazmat,currentUser); //获取变动前仓库库存 int count = baseMapper.selectHazmatCountOfWarehouse(hazmat.getWarehouseId(), hazmat.getBasicId(), currentUser.getCompanyId(),hazmat.getCupboardId()); hazmat.setState(HazmatStatusEnum.DISCARD.getCode()); hazmat.setUpdateBy(currentUser.getUsername()); updateById(hazmat); //生成流向 HzHazmatFlow hazmatFlow=new HzHazmatFlow(); hazmatFlow.setHazmatId(hazmat.getId()); hazmatFlow.setBasicId(hazmat.getBasicId()); hazmatFlow.setState(OperateStatusEnum.DISCARD.getCode()); hazmatFlow.setCompanyId(currentUser.getCompanyId()); hazmatFlow.setNum(hazmat.getRemaining().multiply(BigDecimal.valueOf(-1))); hazmatFlow.setCreateId(currentUser.getId()); hazmatFlowMapper.insert(hazmatFlow); //生成库存变动记录 //新增危化品变动记录 HzWarehouseRecord warehouseRecord = new HzWarehouseRecord() .setWarehouseId(hazmat.getWarehouseId()) .setBasicId(hazmat.getBasicId()) .setCupboardId(hazmat.getCupboardId()) .setCreateId(currentUser.getId()) .setNum(-1) .setState(OperateStatusEnum.DISCARD.getCode()) .setCompanyId(currentUser.getCompanyId()) .setRemaining(count-1); warehouseRecordMapper.insert(warehouseRecord); IdTableNameHandler.removeCurrentId(); } @Override public void changeState(HzHazmat hazmat) { if(hazmat.getId()==null||hazmat.getCompanyId()==null||hazmat.getState()==null){ throw new ApiException("参数不正确"); } SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(hazmat,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat newHazmat=new HzHazmat().setId(hazmat.getId()).setState(hazmat.getState()); newHazmat.setUpdateBy(currentUser.getUsername()); updateById(newHazmat); IdTableNameHandler.removeCurrentId(); } @Override @Transactional(rollbackFor = RuntimeException.class) public void changeRemaining(HzHazmat hazmat) { if(hazmat.getId()==null||hazmat.getRemaining()==null){ throw new ApiException("参数不正确"); } SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmat dbHazmat=getById(hazmat.getId()); checkUserAllowed(dbHazmat,currentUser); if(hazmat.getRemaining().compareTo(dbHazmat.getRemaining())>=0){ throw new ApiException("修改值不能大于等于在库容量"); } //校验是否产生流向 Long flowCount=hazmatFlowMapper.selectCount(Wrappers.lambdaQuery().eq(HzHazmatFlow::getHazmatId,hazmat.getId()) .ne(HzHazmatFlow::getState,OperateStatusEnum.ENTRY.getCode())); if(flowCount>0){ throw new ApiException("该危化品已流转,不能修改!"); } HzHazmat newHazmat=new HzHazmat().setId(hazmat.getId()).setRemaining(hazmat.getRemaining()); newHazmat.setUpdateBy(currentUser.getUsername()); updateById(newHazmat); //生成流向 HzHazmatFlow hazmatFlow=new HzHazmatFlow(); hazmatFlow.setHazmatId(hazmat.getId()); hazmatFlow.setBasicId(dbHazmat.getBasicId()); hazmatFlow.setState(OperateStatusEnum.REMNANT.getCode()); hazmatFlow.setCompanyId(currentUser.getCompanyId()); hazmatFlow.setNum(hazmat.getRemaining()); hazmatFlow.setCreateId(currentUser.getId()); hazmatFlowMapper.insert(hazmatFlow); IdTableNameHandler.removeCurrentId(); } public void checkUserAllowed(HzHazmat hazmat,SysUser user) { if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) { throw new ApiException("管理员不能操作"); } if(hazmat!=null){ if(!Objects.equals(user.getCompanyId(), hazmat.getCompanyId())){ throw new ApiException("无权限操作其他企业数据"); } } } }