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.HzProductBasic;
import com.gkhy.hazmat.system.mapper.HzProductBasicMapper;
import com.gkhy.hazmat.system.service.HzProductBasicService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
/**
*
* 成品基础数据表 服务实现类
*
*
* @author kzy
* @since 2024-08-06 16:03:53
*/
@Service
public class HzProductBasicServiceImpl extends ServiceImpl implements HzProductBasicService {
@Override
public CommonPage selectProductBasicList(HzProductBasic productBasic) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
productBasic.setCompanyId(currentUser.getCompanyId());
}
PageUtils.startPage();
List basicList = baseMapper.selectProductBasicList(productBasic);
return CommonPage.restPage(basicList);
}
@Override
public HzProductBasic selectProductBasicById(Long productBasicId) {
HzProductBasic productBasic = baseMapper.selectById(productBasicId);
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
if (currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
return productBasic;
} else if (!productBasic.getCompanyId().equals(currentUser.getCompanyId())) {
throw new ApiException("无权限查看其它企业数据");
}
return productBasic;
}
@Override
public int insertProductBasic(HzProductBasic productBasic) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
productBasic.setCreateBy(currentUser.getUsername());
productBasic.setCompanyId(currentUser.getCompanyId());
if (!checkProductSnUnique(productBasic)) {
throw new ApiException("产品编号已存在");
}
checkUserAllowed(null,currentUser);
int row = baseMapper.insert(productBasic);
if (row < 1) {
throw new ApiException("新增成品基础信息失败");
}
return row;
}
@Override
public int updateProductBasic(HzProductBasic productBasic) {
if (!checkProductSnUnique(productBasic)) {
throw new ApiException("产品编号已存在");
}
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(productBasic,currentUser);
productBasic.setUpdateBy(currentUser.getUsername());
int row=baseMapper.updateById(productBasic);
if(row<1){
throw new ApiException("更新成品基础信息失败");
}
return row;
}
public void checkUserAllowed(HzProductBasic productBasic,SysUser user) {
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
throw new ApiException("管理员不能操作");
}
if(productBasic!=null){
if(!Objects.equals(user.getCompanyId(), productBasic.getCompanyId())){
throw new ApiException("无权限操作其他企业数据");
}
}
}
@Override
public int deleteProductBasicById(Long productBasicId) {
HzProductBasic productBasic=baseMapper.selectById(productBasicId);
if(productBasic==null){
throw new ApiException("成品基础信息不存在");
}
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(productBasic,currentUser);
baseMapper.deleteProductBasicById(productBasicId);
return 0;
}
@Override
public boolean checkProductSnUnique(HzProductBasic productBasic) {
Long productBasicId=productBasic.getId()==null?-1L:productBasic.getId();
HzProductBasic pb= baseMapper.checkProductSnUnique(productBasic.getProductSn(),productBasic.getCompanyId());
if(pb!=null&&pb.getId().longValue()!=productBasicId.longValue()){
return UserConstant.NOT_UNIQUE;
}
return UserConstant.UNIQUE;
}
}