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.constant.UserConstant;
import com.gkhy.hazmat.common.domain.entity.SysUser;
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.HzHazmatBasic;
import com.gkhy.hazmat.system.mapper.HzHazmatBasicMapper;
import com.gkhy.hazmat.system.service.HzHazmatBasicService;
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 HzHazmatBasicServiceImpl extends ServiceImpl implements HzHazmatBasicService {
@Override
public CommonPage selectHazmatBasicList(HzHazmatBasic hazmatBasic) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
hazmatBasic.setCompanyId(currentUser.getCompanyId());
}
PageUtils.startPage();
List basicList = baseMapper.selectHazmatBasicList(hazmatBasic);
return CommonPage.restPage(basicList);
}
@Override
public HzHazmatBasic selectHazmatBasicById(Long hazmatBasicId) {
HzHazmatBasic hazmatBasic = baseMapper.selectById(hazmatBasicId);
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
if (currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
return hazmatBasic;
} else if (!hazmatBasic.getCompanyId().equals(currentUser.getCompanyId())) {
throw new ApiException("无权限查看其它企业数据");
}
return hazmatBasic;
}
@Override
public int insertHazmatBasic(HzHazmatBasic hazmatBasic) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
hazmatBasic.setCreateBy(currentUser.getUsername());
hazmatBasic.setCompanyId(currentUser.getCompanyId());
if (!checkProductSnUnique(hazmatBasic)) {
throw new ApiException("产品编号已存在");
}
checkUserAllowed(null,currentUser);
int row = baseMapper.insert(hazmatBasic);
if (row < 1) {
throw new ApiException("新增危化品基础信息失败");
}
return row;
}
@Override
public int updateHazmatBasic(HzHazmatBasic hazmatBasic) {
if (!checkProductSnUnique(hazmatBasic)) {
throw new ApiException("产品编号已存在");
}
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(hazmatBasic,currentUser);
hazmatBasic.setUpdateBy(currentUser.getUsername());
int row=baseMapper.updateById(hazmatBasic);
if(row<1){
throw new ApiException("更新危化品基础信息失败");
}
return row;
}
public void checkUserAllowed(HzHazmatBasic hazmatBasic,SysUser user) {
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
throw new ApiException("管理员不能操作");
}
if(hazmatBasic!=null){
if(!Objects.equals(user.getCompanyId(), hazmatBasic.getCompanyId())){
throw new ApiException("无权限操作其他企业数据");
}
}
}
@Override
public int deleteHazmatBasicById(Long hazmatBasicId) {
HzHazmatBasic hazmatBasic=baseMapper.selectById(hazmatBasicId);
if(hazmatBasic==null){
throw new ApiException("危化品基础信息不存在");
}
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(hazmatBasic,currentUser);
baseMapper.deleteHazmatBasicById(hazmatBasicId);
return 0;
}
@Override
public boolean checkProductSnUnique(HzHazmatBasic hazmatBasic) {
Long hazmatBasicId=hazmatBasic.getId()==null?-1L:hazmatBasic.getId();
HzHazmatBasic hb= baseMapper.checkProductSnUnique(hazmatBasic.getProductSn(),hazmatBasic.getCompanyId());
if(hb!=null&&hb.getId().longValue()!=hazmatBasicId.longValue()){
return UserConstant.NOT_UNIQUE;
}
return UserConstant.UNIQUE;
}
}