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.HazmatStatusEnum;
|
import com.gkhy.hazmat.common.enums.OperateStatusEnum;
|
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.*;
|
import com.gkhy.hazmat.system.domain.vo.HzProductWarehouseVO;
|
import com.gkhy.hazmat.system.mapper.*;
|
import com.gkhy.hazmat.system.service.HzProductService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 成品表 服务实现类
|
* </p>
|
*
|
* @author kzy
|
* @since 2024-08-06 16:03:53
|
*/
|
@Service
|
public class HzProductServiceImpl extends ServiceImpl<HzProductMapper, HzProduct> implements HzProductService {
|
@Autowired
|
private HzProductFlowMapper productFlowMapper;
|
@Autowired
|
private HzProductWarehouseRecordMapper productWarehouseRecordMapper;
|
@Autowired
|
private HzProductBasicMapper productBasicMapper;
|
@Autowired
|
private HzWarehouseMapper warehouseMapper;
|
|
@Override
|
public CommonPage selectProductList(HzProduct product) {
|
if(product.getWarehouseId()==null||product.getBasicId()==null){
|
throw new ApiException("仓库id或者成品基础数据id不能为空");
|
}
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
product.setCompanyId(currentUser.getCompanyId());
|
PageUtils.startPage();
|
List<HzProduct> productList = baseMapper.selectProductList(product);
|
IdTableNameHandler.removeCurrentId();
|
return CommonPage.restPage(productList);
|
}
|
|
@Override
|
public HzProduct selectProductById(Long productId) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzProduct product = baseMapper.selectById(productId);
|
IdTableNameHandler.removeCurrentId();
|
checkUserAllowed(product,currentUser);
|
return product;
|
}
|
|
@Override
|
public int deleteProductById(Long productId) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzProduct product=baseMapper.selectById(productId);
|
if(product==null){
|
throw new ApiException("成品不存在");
|
}
|
checkUserAllowed(product,currentUser);
|
baseMapper.deleteProductById(productId);
|
IdTableNameHandler.removeCurrentId();
|
return 0;
|
}
|
|
@Override
|
public HzProduct selectProductByCode(String code) {
|
if(!code.startsWith(CodePrexEnum.GOOD.getCode())){
|
throw new ApiException("该条码格式不正确");
|
}
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzProduct product=baseMapper.selectProductByCode(code,currentUser.getCompanyId());
|
IdTableNameHandler.removeCurrentId();
|
if(product==null){
|
throw new ApiException("该条码数据不存在");
|
}
|
return product;
|
}
|
|
@Override
|
@Transactional(rollbackFor = RuntimeException.class)
|
public void productSold(Long productId) {
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzProduct product=getById(productId);
|
if(!product.getState().equals(HazmatStatusEnum.WAREHOUSEIN.getCode())){
|
throw new ApiException("成品不在库中,不能进行销售操作");
|
}
|
checkUserAllowed(product,currentUser);
|
//获取变动前仓库库存
|
int count = baseMapper.selectProductCountOfWarehouse(product.getWarehouseId(), product.getBasicId(), currentUser.getCompanyId());
|
product.setState(HazmatStatusEnum.SOLD.getCode());
|
product.setUpdateBy(currentUser.getUsername());
|
updateById(product);
|
//生成流向
|
HzProductFlow productFlow=new HzProductFlow();
|
productFlow.setProductId(productId);
|
productFlow.setBasicId(product.getBasicId());
|
productFlow.setState(OperateStatusEnum.SOLD.getCode());
|
productFlow.setCompanyId(currentUser.getCompanyId());
|
productFlow.setNum(product.getRemaining().multiply(BigDecimal.valueOf(-1)));
|
productFlow.setCreateId(currentUser.getId());
|
productFlowMapper.insert(productFlow);
|
//生成库存变动记录
|
//新增危化品变动记录
|
HzProductWarehouseRecord warehouseRecord = new HzProductWarehouseRecord()
|
.setWarehouseId(product.getWarehouseId())
|
.setCreateId(currentUser.getId())
|
.setBasicId(product.getBasicId())
|
.setNum(-1)
|
.setState(OperateStatusEnum.SOLD.getCode())
|
.setCompanyId(currentUser.getCompanyId())
|
.setRemaining(count-1);
|
productWarehouseRecordMapper.insert(warehouseRecord);
|
IdTableNameHandler.removeCurrentId();
|
}
|
|
@Override
|
@Transactional(rollbackFor = RuntimeException.class)
|
public void productDiscard(Long productId) {
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
HzProduct product=getById(productId);
|
if(!product.getState().equals(HazmatStatusEnum.WAREHOUSEIN.getCode())){
|
throw new ApiException("危化品不在库中,不能进行作废操作");
|
}
|
checkUserAllowed(product,currentUser);
|
//获取变动前仓库库存
|
int count = baseMapper.selectProductCountOfWarehouse(product.getWarehouseId(), product.getBasicId(), currentUser.getCompanyId());
|
product.setState(HazmatStatusEnum.DISCARD.getCode());
|
product.setUpdateBy(currentUser.getUsername());
|
updateById(product);
|
//生成流向
|
HzProductFlow productFlow=new HzProductFlow();
|
productFlow.setProductId(product.getId());
|
productFlow.setBasicId(product.getBasicId());
|
productFlow.setState(OperateStatusEnum.DISCARD.getCode());
|
productFlow.setCompanyId(currentUser.getCompanyId());
|
productFlow.setNum(product.getRemaining().multiply(BigDecimal.valueOf(-1)));
|
productFlow.setCreateId(currentUser.getId());
|
productFlowMapper.insert(productFlow);
|
|
//生成库存变动记录
|
//新增危化品变动记录
|
HzProductWarehouseRecord warehouseRecord = new HzProductWarehouseRecord()
|
.setWarehouseId(product.getWarehouseId())
|
.setBasicId(product.getBasicId())
|
.setCreateId(currentUser.getId())
|
.setNum(-1)
|
.setState(OperateStatusEnum.DISCARD.getCode())
|
.setCompanyId(currentUser.getCompanyId())
|
.setRemaining(count-1);
|
productWarehouseRecordMapper.insert(warehouseRecord);
|
IdTableNameHandler.removeCurrentId();
|
}
|
|
@Override
|
public void changeState(HzProduct product) {
|
if(product.getId()==null||product.getCompanyId()==null||product.getState()==null){
|
throw new ApiException("参数不正确");
|
}
|
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(product,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
|
HzProduct newProduct=new HzProduct().setId(product.getId()).setState(product.getState());
|
newProduct.setUpdateBy(currentUser.getUsername());
|
updateById(newProduct);
|
IdTableNameHandler.removeCurrentId();
|
}
|
|
@Override
|
public CommonPage selectProductGroupWarehouse(HzProduct product) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(null,currentUser);
|
//设置分表id
|
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
|
product.setCompanyId(currentUser.getCompanyId());
|
PageUtils.startPage();
|
List<HzProductWarehouseVO> productList = baseMapper.selectProductGroupWareHouse(product);
|
if(!productList.isEmpty()) {
|
List<Long> warehouseIds = productList.stream().map(HzProductWarehouseVO::getWarehouseId).collect(Collectors.toList());
|
List<Long> basicIds = productList.stream().map(HzProductWarehouseVO::getBasicId).collect(Collectors.toList());
|
List<HzProductBasic> hazmatBasicList = productBasicMapper.selectProductBasicListByIds(basicIds);
|
List<HzWarehouse> warehouseList = warehouseMapper.selectWarehouseListByIds(warehouseIds);
|
Map<Long,HzProductBasic> productBasicMap=hazmatBasicList.stream().collect(Collectors.toMap(HzProductBasic::getId, item->item));
|
Map<Long,HzWarehouse> warehouseMap=warehouseList.stream().collect(Collectors.toMap(HzWarehouse::getId, item->item));
|
for(HzProductWarehouseVO productWarehouseVO:productList){
|
HzProductBasic productBasic=productBasicMap.get(productWarehouseVO.getBasicId());
|
HzWarehouse warehouse=warehouseMap.get(productWarehouseVO.getWarehouseId());
|
if(productBasic!=null){
|
productWarehouseVO.setProductBasic(productBasic);
|
}
|
if(warehouse!=null){
|
productWarehouseVO.setWarehouseName(warehouse.getName());
|
}
|
}
|
}
|
IdTableNameHandler.removeCurrentId();
|
return CommonPage.restPage(productList);
|
}
|
|
public void checkUserAllowed(HzProduct product,SysUser user) {
|
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
|
throw new ApiException("管理员不能操作");
|
}
|
if(product!=null){
|
if(!Objects.equals(user.getCompanyId(), product.getCompanyId())){
|
throw new ApiException("无权限操作其他企业数据");
|
}
|
}
|
}
|
}
|