package com.gkhy.hazmat.system.service.impl;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
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.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.HzHazmat;
|
import com.gkhy.hazmat.system.domain.HzHazmatFlow;
|
import com.gkhy.hazmat.system.mapper.HzHazmatFlowMapper;
|
import com.gkhy.hazmat.system.mapper.HzHazmatMapper;
|
import com.gkhy.hazmat.system.service.HzHazmatFlowService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 危化品流向表 服务实现类
|
* </p>
|
*
|
* @author kzy
|
* @since 2024-08-05 14:41:40
|
*/
|
@Service
|
public class HzHazmatFlowServiceImpl extends ServiceImpl<HzHazmatFlowMapper, HzHazmatFlow> implements HzHazmatFlowService {
|
@Autowired
|
private HzHazmatMapper hazmatMapper;
|
|
@Override
|
public CommonPage selectHazmatFlowList(HzHazmatFlow hazmatFlow) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
|
IdTableNameHandler.setCurrentId(hazmatFlow.getCompanyId());
|
}else {
|
hazmatFlow.setCompanyId(currentUser.getCompanyId());
|
//设置分表id
|
//todo
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
}
|
PageUtils.startPage();
|
List<HzHazmatFlow> flowList = baseMapper.selectHazmatFlowList(hazmatFlow);
|
IdTableNameHandler.removeCurrentId();
|
return CommonPage.restPage(flowList);
|
}
|
|
@Override
|
public HzHazmatFlow selectHazmatFlowById(Long hazmatFlowId) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzHazmatFlow hazmatFlow = baseMapper.selectById(hazmatFlowId);
|
IdTableNameHandler.removeCurrentId();
|
if (!hazmatFlow.getCompanyId().equals(currentUser.getCompanyId())) {
|
throw new ApiException("无权限查看其它企业数据");
|
}
|
return hazmatFlow;
|
}
|
|
@Override
|
public int insertHazmatFlow(HzHazmatFlow hazmatFlow) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
hazmatFlow.setCreateBy(currentUser.getUsername());
|
hazmatFlow.setCompanyId(currentUser.getCompanyId());
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
int row = baseMapper.insert(hazmatFlow);
|
IdTableNameHandler.removeCurrentId();
|
if (row < 1) {
|
throw new ApiException("新增危化品流向失败");
|
}
|
return row;
|
}
|
|
@Override
|
public int deleteHazmatFlowById(Long hazmatFlowId) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzHazmatFlow hazmatFlow=baseMapper.selectById(hazmatFlowId);
|
IdTableNameHandler.removeCurrentId();
|
if(hazmatFlow==null){
|
throw new ApiException("流向信息不存在");
|
}
|
checkUserAllowed(hazmatFlow,currentUser);
|
|
baseMapper.deleteById(hazmatFlow);
|
return 0;
|
}
|
|
@Override
|
public List<HzHazmatFlow> selectAllHazmatFlowById(Long hazmatFlowId) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzHazmatFlow hazmatFlow=getById(hazmatFlowId);
|
if(hazmatFlow==null){
|
throw new ApiException("流向不存在");
|
}
|
List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatFlow.getHazmatId());
|
IdTableNameHandler.removeCurrentId();
|
return hazmatFlowList;
|
}
|
|
@Override
|
public List<HzHazmatFlow> selectAllHazmatFlowByCode(String code,Long companyId) {
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
if(!code.startsWith(CodePrexEnum.MATERIAL.getCode())){
|
throw new ApiException("条码格式不正确");
|
}
|
if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
|
IdTableNameHandler.setCurrentId(companyId);
|
}else {
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
}
|
HzHazmat hazmat=hazmatMapper.selectHazmatByCode(code,currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())?companyId:currentUser.getCompanyId());
|
if(hazmat==null){
|
throw new ApiException("危化品条码数据不存在");
|
}
|
List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmat.getId());
|
IdTableNameHandler.removeCurrentId();
|
return hazmatFlowList;
|
}
|
|
@Override
|
public CommonPage<HzHazmatFlow> selectAllHazmatFlowByUser() {
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
HzHazmatFlow hazmat=new HzHazmatFlow()
|
.setCreateId(currentUser.getId())
|
.setCompanyId(currentUser.getCompanyId());
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
PageUtils.startPage();
|
List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByUser(hazmat);
|
IdTableNameHandler.removeCurrentId();
|
return CommonPage.restPage(hazmatFlowList);
|
}
|
|
@Override
|
public List<HzHazmatFlow> getAllHazmatFlowByHazmatId(Long hazmatId,Long companyId) {
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
|
IdTableNameHandler.setCurrentId(companyId);
|
}else {
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
}
|
HzHazmat hazmat=hazmatMapper.selectById(hazmatId);
|
if(hazmat==null){
|
throw new ApiException("危化品数据不存在");
|
}
|
checkUserAllowed(new HzHazmatFlow().setCompanyId(hazmat.getCompanyId()),currentUser);
|
List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatId);
|
IdTableNameHandler.removeCurrentId();
|
return hazmatFlowList;
|
}
|
|
public void checkUserAllowed(HzHazmatFlow hazmatFlow,SysUser user) {
|
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
|
throw new ApiException("管理员不能操作");
|
}
|
if(hazmatFlow!=null){
|
if (!user.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
|
if(!Objects.equals(user.getCompanyId(), hazmatFlow.getCompanyId())){
|
throw new ApiException("无权限操作其他企业数据");
|
}
|
}
|
}
|
}
|
}
|