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.HzWarehouse;
|
import com.gkhy.hazmat.system.mapper.HzWarehouseMapper;
|
import com.gkhy.hazmat.system.service.HzWarehouseService;
|
import org.springframework.stereotype.Service;
|
|
import java.util.List;
|
import java.util.Objects;
|
|
/**
|
* <p>
|
* 仓库表 服务实现类
|
* </p>
|
*
|
* @author kzy
|
* @since 2024-08-05 14:41:40
|
*/
|
@Service
|
public class HzWarehouseServiceImpl extends ServiceImpl<HzWarehouseMapper, HzWarehouse> implements HzWarehouseService {
|
@Override
|
public CommonPage selectWarehouseList(HzWarehouse warehouse) {
|
SysUser currentUser= SecurityUtils.getLoginUser().getUser();
|
if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
|
warehouse.setCompanyId(currentUser.getCompanyId());
|
}
|
PageUtils.startPage();
|
List<HzWarehouse> warehouseList=baseMapper.selectWarehouseList(warehouse);
|
return CommonPage.restPage(warehouseList);
|
}
|
|
@Override
|
public HzWarehouse selectWarehouseById(Long warehouseId) {
|
HzWarehouse warehouse= baseMapper.selectById(warehouseId);
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
|
return warehouse;
|
}else if(!warehouse.getCompanyId().equals(currentUser.getCompanyId())){
|
throw new ApiException("无权限查看其它企业数据");
|
}
|
return warehouse;
|
}
|
|
@Override
|
public int insertWarehouse(HzWarehouse warehouse) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
warehouse.setCreateBy(currentUser.getUsername());
|
warehouse.setCompanyId(currentUser.getCompanyId());
|
if (!checkNameUnique(warehouse)) {
|
throw new ApiException("仓库名称已存在");
|
}
|
checkUserAllowed(null,currentUser);
|
int row = baseMapper.insert(warehouse);
|
if (row < 1) {
|
throw new ApiException("新增仓库失败");
|
}
|
return row;
|
}
|
|
@Override
|
public int updateWarehouse(HzWarehouse warehouse) {
|
if (!checkNameUnique(warehouse)) {
|
throw new ApiException("仓库名称已存在");
|
}
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(warehouse,currentUser);
|
warehouse.setUpdateBy(currentUser.getUsername());
|
int row=baseMapper.updateById(warehouse);
|
if(row<1){
|
throw new ApiException("更新仓库信息失败");
|
}
|
return row;
|
}
|
|
public void checkUserAllowed(HzWarehouse warehouse, SysUser user) {
|
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
|
throw new ApiException("管理员不能操作");
|
}
|
if(warehouse!=null){
|
if(!Objects.equals(user.getCompanyId(), warehouse.getCompanyId())){
|
throw new ApiException("无权限操作其他企业数据");
|
}
|
}
|
}
|
|
@Override
|
public int deleteWarehouseById(Long warehouseId) {
|
HzWarehouse warehouse=baseMapper.selectById(warehouseId);
|
if(warehouse==null){
|
throw new ApiException("仓库信息不存在");
|
}
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(warehouse,currentUser);
|
baseMapper.deleteWarehouseById(warehouseId);
|
return 0;
|
}
|
|
@Override
|
public boolean checkNameUnique(HzWarehouse warehouse) {
|
Long warsehouseId=warehouse.getId()==null?-1L:warehouse.getId();
|
HzWarehouse wh= baseMapper.checkNameUnique(warehouse.getName(),warehouse.getCompanyId());
|
if(wh!=null&&wh.getId().longValue()!=warsehouseId.longValue()){
|
return UserConstant.NOT_UNIQUE;
|
}
|
return UserConstant.UNIQUE;
|
}
|
}
|