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.HzProduct;
import com.gkhy.hazmat.system.domain.HzProductFlow;
import com.gkhy.hazmat.system.mapper.HzProductFlowMapper;
import com.gkhy.hazmat.system.mapper.HzProductMapper;
import com.gkhy.hazmat.system.service.HzProductFlowService;
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-06 16:03:53
*/
@Service
public class HzProductFlowServiceImpl extends ServiceImpl implements HzProductFlowService {
@Autowired
private HzProductMapper productMapper;
@Override
public CommonPage selectProductFlowList(HzProductFlow productFlow) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
productFlow.setCompanyId(currentUser.getCompanyId());
PageUtils.startPage();
List flowList = baseMapper.selectProductFlowList(productFlow);
IdTableNameHandler.removeCurrentId();
return CommonPage.restPage(flowList);
}
@Override
public HzProductFlow selectProductFlowById(Long productFlowId) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzProductFlow productFlow = baseMapper.selectById(productFlowId);
IdTableNameHandler.removeCurrentId();
checkUserAllowed(productFlow,currentUser);
return productFlow;
}
@Override
public int deleteProductFlowById(Long productFlowId) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzProductFlow productFlow=baseMapper.selectById(productFlowId);
if(productFlow==null){
throw new ApiException("流向信息不存在");
}
checkUserAllowed(productFlow,currentUser);
baseMapper.deleteById(productFlow);
IdTableNameHandler.removeCurrentId();
return 0;
}
@Override
public List selectAllProductFlowById(Long productFlowId) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzProductFlow productFlow=getById(productFlowId);
if(productFlow==null){
throw new ApiException("流向不存在");
}
List hzProductFlows = baseMapper.selectAllProductFlowByProductId(productFlow.getProductId());
IdTableNameHandler.removeCurrentId();
return hzProductFlows;
}
@Override
public List selectAllProductFlowByCode(String code) {
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
if(!code.startsWith(CodePrexEnum.GOOD.getCode())){
throw new ApiException("条码格式不正确");
}
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzProduct product=productMapper.selectProductByCode(code,currentUser.getCompanyId());
if(product==null){
throw new ApiException("成品条码数据不存在");
}
List hzProductFlows = baseMapper.selectAllProductFlowByProductId(product.getId());
IdTableNameHandler.removeCurrentId();
return hzProductFlows;
}
@Override
public CommonPage selectAllProductFlowByUser() {
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzProductFlow productFlow=new HzProductFlow()
.setCreateId(currentUser.getId())
.setCompanyId(currentUser.getCompanyId());
PageUtils.startPage();
List productFlowList = baseMapper.selectAllProductFlowByUser(productFlow);
IdTableNameHandler.removeCurrentId();
return CommonPage.restPage(productFlowList);
}
@Override
public List getAllProductFlowByProductId(Long productId) {
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
checkUserAllowed(null,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
HzProduct product=productMapper.selectById(productId);
if(product==null){
throw new ApiException("成品数据不存在");
}
checkUserAllowed(new HzProductFlow().setCompanyId(product.getCompanyId()),currentUser);
List hzProductFlows = baseMapper.selectAllProductFlowByProductId(productId);
IdTableNameHandler.removeCurrentId();
return hzProductFlows;
}
public void checkUserAllowed(HzProductFlow productFlow,SysUser user) {
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
throw new ApiException("管理员不能操作");
}
if(productFlow!=null){
if(!Objects.equals(user.getCompanyId(), productFlow.getCompanyId())){
throw new ApiException("无权限操作其他企业数据");
}
}
}
}