package com.gkhy.hazmat.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.constant.Constant;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.EntryStateEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.enums.WarehouseRecordEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.service.RedisService;
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.HzEntryTransferVO;
import com.gkhy.hazmat.system.domain.vo.TabooDisVo;
import com.gkhy.hazmat.system.mapper.*;
import com.gkhy.hazmat.system.service.HzEntryRecordService;
import com.gkhy.hazmat.system.service.HzHazmatService;
import com.gkhy.hazmat.system.service.TaBooComparisonService;
import com.gkhy.hazmat.system.service.TabooWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 系统配置表 服务实现类
*
*
* @author hh
* @since 2025-02-13 08:39:55
*/
@Service
public class TaBooWarningServiceImpl extends ServiceImpl implements TabooWarningService {
@Autowired
private RedisService redisService;
@Autowired
private HzHazmatService hazmatService;
@Autowired
private HzHazmatBasicMapper hazmatBasicMapper;
@Autowired
private HzHazmatMapper hazmatMapper;
@Autowired
private HzWarehouseRecordMapper warehouseRecordMapper;
@Autowired
private HzEntryRecordService entryRecordService;
@Override
public CommonPage selectHzTabooWarningPageList(HzTabooWarning hzTabooWarning) {
PageUtils.startPage();
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
hzTabooWarning.setCompanyId(currentUser.getCompanyId());
List hzTabooWarnings = baseMapper.selectHzTabooWarningList(hzTabooWarning);
return CommonPage.restPage(hzTabooWarnings);
}
@Override
public List selectHzTabooWarningList(HzTabooWarning tabooWarning) {
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
tabooWarning.setCompanyId(currentUser.getCompanyId());
tabooWarning.setState(0);
return baseMapper.selectHzTabooWarningList(tabooWarning);
}
@Override
@Transactional(rollbackFor = RuntimeException.class)
public void doTransfer(HzEntryTransferVO entryTransferVO) {
HzTabooWarning hzTabooWarning = baseMapper.selectById(entryTransferVO.getId());
if (hzTabooWarning == null){
throw new ApiException("相忌报警信息不存在");
}
Long entryId = hzTabooWarning.getEntryId();
HzEntryRecord entryRecord=entryRecordService.getById(entryId);
if (entryRecord == null){
throw new ApiException("入库信息不存在");
}
if (!entryRecord.getState().toString().equals(EntryStateEnum.ENTER.getCode().toString())){
throw new ApiException("入库状态不正确");
}
if (entryRecord.getCupboardId().equals(entryTransferVO.getCupboardId())){
throw new ApiException("不能转入原存储柜");
}
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
checkUserAllowed(entryRecord,currentUser);
HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(entryRecord.getBasicId());
if(hazmatBasic==null){
throw new ApiException("危化品基础数据不存在");
}
//获取当前仓库库存
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
int countOut = hazmatMapper.selectHazmatCountOfWarehouse(entryRecord.getWarehouseId(), hazmatBasic.getId(), currentUser.getCompanyId(), entryRecord.getCupboardId());
//新增危化品变动记录转出
HzWarehouseRecord warehouseRecordOut = new HzWarehouseRecord()
.setWarehouseId(entryRecord.getWarehouseId())
.setCupboardId(entryRecord.getCupboardId())
.setBasicId(hazmatBasic.getId())
.setNum(entryRecord.getNum())
.setCompanyId(currentUser.getCompanyId())
.setCreateId(currentUser.getId())
.setRemaining(countOut - entryRecord.getNum())
.setState(WarehouseRecordEnum.TRANSFEROUT.getCode());
warehouseRecordMapper.insert(warehouseRecordOut);
int count = hazmatMapper.selectHazmatCountOfWarehouse(entryTransferVO.getWarehouseId(), hazmatBasic.getId(), currentUser.getCompanyId(), entryTransferVO.getCupboardId());
//新增危化品变动记录转入
HzWarehouseRecord warehouseRecord = new HzWarehouseRecord()
.setWarehouseId(entryTransferVO.getWarehouseId())
.setCupboardId(entryTransferVO.getCupboardId())
.setBasicId(hazmatBasic.getId())
.setNum(entryRecord.getNum())
.setCompanyId(currentUser.getCompanyId())
.setCreateId(currentUser.getId())
.setRemaining(entryRecord.getNum() + count)
.setState(WarehouseRecordEnum.TRANSFERIN.getCode());
warehouseRecordMapper.insert(warehouseRecord);
// 校验生成相忌数据
Map redata = (Map)redisService.get(Constant.TABOO_UNIQUE_KEY);
if (!redata.isEmpty()){
// 查询对应仓库和柜子的物品信息获取品类 获取相冲相弱相吸相异
List collectData =
hazmatMapper.selectHazmatWarehouseCheck(entryTransferVO.getWarehouseId(), currentUser.getCompanyId(), entryTransferVO.getCupboardId());
if (!collectData.isEmpty() && collectData.size() > 0) {
for (TabooDisVo collectDatum : collectData) {
String key1 = collectDatum.getSpNum() + "_" + hazmatBasic.getPeculiarityNumber();
String key2 = hazmatBasic.getPeculiarityNumber() + "_" + collectDatum.getSpNum();
if (redata.containsKey(key1) || redata.containsKey(key2)) {
// 记录数据
Long l = redata.get(key1);
Long l1 = l == null ? redata.get(key2) : l;
HzTabooWarning tabooWarning = new HzTabooWarning()
.setWarningType(l1)
.setWarehouseId(entryTransferVO.getWarehouseId())
.setCupboardId(entryTransferVO.getCupboardId())
.setEntryId(entryRecord.getId())
.setCompanyId(currentUser.getCompanyId())
.setTabooBasicId(collectDatum.getId()).setCreateBy(currentUser.getUsername());
baseMapper.insert(tabooWarning);
break;
}
}
}
}else {
throw new ApiException("危化品相忌信息不存在");
}
//更新危化品信息
hazmatService.update(new LambdaUpdateWrapper().eq(HzHazmat::getEntryId,entryRecord.getId())
.set(HzHazmat::getWarehouseId,entryTransferVO.getWarehouseId())
.set(HzHazmat::getCupboardId,entryTransferVO.getCupboardId())
.set(HzHazmat::getUpdateBy,currentUser.getUsername()));
// }
//更新入库记录状态
entryRecordService.update(new LambdaUpdateWrapper()
.eq(HzEntryRecord::getId,entryRecord.getId()).set(HzEntryRecord::getWarehouseId,entryTransferVO.getWarehouseId())
.set(HzEntryRecord::getCupboardId,entryTransferVO.getCupboardId())
.set(HzEntryRecord::getUpdateBy,currentUser.getUsername()));
IdTableNameHandler.removeCurrentId();
//处理数据本身
baseMapper.updateById(new HzTabooWarning().setState(1).
setUpdateBy(currentUser.getUsername()).setReCupboardId(entryTransferVO.getCupboardId()).setReWarehouseId(entryTransferVO.getWarehouseId())
.setId(entryTransferVO.getId()));
}
public void checkUserAllowed(HzEntryRecord entryRecord,SysUser user) {
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
throw new ApiException("管理员不能操作");
}
if(entryRecord!=null){
if(!Objects.equals(user.getCompanyId(), entryRecord.getCompanyId())){
throw new ApiException("无权限操作其他企业数据");
}
}
}
}