package com.gkhy.hazmat.system.service.impl;
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.List;
import java.util.Objects;
/**
*
* 危化品流向表 服务实现类
*
*
* @author kzy
* @since 2024-08-05 14:41:40
*/
@Service
public class HzHazmatFlowServiceImpl extends ServiceImpl implements HzHazmatFlowService {
@Autowired
private HzHazmatMapper hazmatMapper;
@Override
public CommonPage selectHazmatFlowList(HzHazmatFlow hazmatFlow) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
hazmatFlow.setCompanyId(currentUser.getCompanyId());
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
PageUtils.startPage();
List 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 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 hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatFlow.getHazmatId());
IdTableNameHandler.removeCurrentId();
return hazmatFlowList;
}
@Override
public List selectAllHazmatFlowByCode(String code) {
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
if(!code.startsWith(CodePrexEnum.MATERIAL.getCode())){
throw new ApiException("条码格式不正确");
}
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzHazmat hazmat=hazmatMapper.selectHazmatByCode(code,currentUser.getCompanyId());
if(hazmat==null){
throw new ApiException("危化品条码数据不存在");
}
List hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmat.getId());
IdTableNameHandler.removeCurrentId();
return hazmatFlowList;
}
@Override
public CommonPage 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 hazmatFlowList = baseMapper.selectAllHazmatFlowByUser(hazmat);
IdTableNameHandler.removeCurrentId();
return CommonPage.restPage(hazmatFlowList);
}
@Override
public List getAllHazmatFlowByHazmatId(Long hazmatId) {
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzHazmat hazmat=hazmatMapper.selectById(hazmatId);
if(hazmat==null){
throw new ApiException("危化品数据不存在");
}
checkUserAllowed(new HzHazmatFlow().setCompanyId(hazmat.getCompanyId()),currentUser);
List 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(!Objects.equals(user.getCompanyId(), hazmatFlow.getCompanyId())){
throw new ApiException("无权限操作其他企业数据");
}
}
}
}