危化品全生命周期管理后端
kongzy
2024-09-14 ed36af4d4cc5feac72a384d85f9032fc6dc1223a
hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/HzHazmatServiceImpl.java
@@ -1,5 +1,6 @@
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;
@@ -309,6 +310,42 @@
        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.<HzHazmatFlow>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("管理员不能操作");