package com.gkhy.hazmat.system.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.api.CommonResult; 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.HzHazmat; import com.gkhy.hazmat.system.domain.HzHazmatBasic; import com.gkhy.hazmat.system.domain.HzHazmatFlow; import com.gkhy.hazmat.system.domain.HzWarehouseRecord; import com.gkhy.hazmat.system.domain.dto.ChemicalLedger; import com.gkhy.hazmat.system.mapper.HzHazmatBasicMapper; import com.gkhy.hazmat.system.mapper.HzHazmatFlowMapper; import com.gkhy.hazmat.system.mapper.HzHazmatMapper; import com.gkhy.hazmat.system.mapper.HzWarehouseRecordMapper; import com.gkhy.hazmat.system.service.HzHazmatFlowService; 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.*; import java.util.stream.Collectors; /** *

* 危化品流向表 服务实现类 *

* * @author kzy * @since 2024-08-05 14:41:40 */ @Service public class HzHazmatFlowServiceImpl extends ServiceImpl implements HzHazmatFlowService { @Autowired private HzHazmatMapper hazmatMapper; @Autowired private HzHazmatBasicMapper hazmatBasicMapper; @Autowired private HzWarehouseRecordMapper warehouseRecordMapper; @Override public CommonPage selectHazmatFlowList(HzHazmatFlow hazmatFlow) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); // if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){ // IdTableNameHandler.setCurrentId(hazmatFlow.getCompanyId()); // }else { // // //设置分表id // //todo // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); // } hazmatFlow.setCompanyId(currentUser.getCompanyId()); PageUtils.startPage(); List flowList = baseMapper.selectHazmatFlowList(hazmatFlow); // IdTableNameHandler.removeCurrentId(); return CommonPage.restPage(flowList); } @Override public HzHazmatFlow selectHazmatFlowById(Long hazmatFlowId) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmatFlow hazmatFlow = baseMapper.selectById(hazmatFlowId); // IdTableNameHandler.removeCurrentId(); if (!hazmatFlow.getCompanyId().equals(currentUser.getCompanyId())) { throw new ApiException("无权限查看其它企业数据"); } return hazmatFlow; } @Override public int insertHazmatFlow(HzHazmatFlow hazmatFlow) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); hazmatFlow.setCreateBy(currentUser.getUsername()); hazmatFlow.setCompanyId(currentUser.getCompanyId()); checkUserAllowed(null,currentUser); //设置分表id // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); int row = baseMapper.insert(hazmatFlow); // IdTableNameHandler.removeCurrentId(); if (row < 1) { throw new ApiException("新增危化品流向失败"); } return row; } @Override public int deleteHazmatFlowById(Long hazmatFlowId) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmatFlow hazmatFlow=baseMapper.selectById(hazmatFlowId); // IdTableNameHandler.removeCurrentId(); if(hazmatFlow==null){ throw new ApiException("流向信息不存在"); } checkUserAllowed(hazmatFlow,currentUser); baseMapper.deleteById(hazmatFlow); return 0; } @Override public List selectAllHazmatFlowById(Long hazmatFlowId) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); //设置分表id // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); HzHazmatFlow hazmatFlow=getById(hazmatFlowId); if(hazmatFlow==null){ throw new ApiException("流向不存在"); } List hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatFlow.getHazmatId()); // IdTableNameHandler.removeCurrentId(); return hazmatFlowList; } @Override public List selectAllHazmatFlowByCode(String code,Long companyId) { SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); if(!code.startsWith(CodePrexEnum.MATERIAL.getCode())){ throw new ApiException("条码格式不正确"); } // if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){ // IdTableNameHandler.setCurrentId(companyId); // }else { // //设置分表id // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); // } HzHazmat hazmat=hazmatMapper.selectHazmatByCode(code,currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())?companyId:currentUser.getCompanyId()); if(hazmat==null){ throw new ApiException("危化品条码数据不存在"); } List hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmat.getId()); // IdTableNameHandler.removeCurrentId(); return hazmatFlowList; } @Override public CommonPage selectAllHazmatFlowByUser() { SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); HzHazmatFlow hazmat=new HzHazmatFlow() .setCreateId(currentUser.getId()) .setCompanyId(currentUser.getCompanyId()); //设置分表id // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); PageUtils.startPage(); List hazmatFlowList = baseMapper.selectAllHazmatFlowByUser(hazmat); // IdTableNameHandler.removeCurrentId(); return CommonPage.restPage(hazmatFlowList); } @Override public List getAllHazmatFlowByHazmatId(Long hazmatId,Long companyId) { SysUser currentUser=SecurityUtils.getLoginUser().getUser(); checkUserAllowed(null,currentUser); // if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){ // IdTableNameHandler.setCurrentId(companyId); // }else { // //设置分表id // IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); // } HzHazmat hazmat=hazmatMapper.selectById(hazmatId); if(hazmat==null){ throw new ApiException("危化品数据不存在"); } checkUserAllowed(new HzHazmatFlow().setCompanyId(hazmat.getCompanyId()),currentUser); List hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatId); // IdTableNameHandler.removeCurrentId(); return hazmatFlowList; } @Override @Transactional public CommonResult pushFlowFromThirdParty(List flows) { List hazmatFlowList = new ArrayList<>(); List errorMessages = new ArrayList<>(); int successCount = 0; int failCount = 0; for (int i = 0; i < flows.size(); i++) { ChemicalLedger flowData = flows.get(i); try { HzHazmatFlow hazmatFlow = convertToHzHazmatFlow(flowData); HzHazmatBasic hzHazmatBasic = hazmatBasicMapper.selectByCas(flowData.getCas()); hazmatFlow.setBasicId(hzHazmatBasic.getId()); if (hazmatFlow.getBasicId() == null) { errorMessages.add(String.format("第%d条数据:cas号没有对应基础信息", i + 1)); failCount++; continue; } if (hazmatFlow.getState() == null) { errorMessages.add(String.format("第%d条数据:操作状态不能为空", i + 1)); failCount++; continue; } HzHazmat hzHazmat = hazmatMapper.selectById(hazmatFlow.getHazmatId()); int count = hazmatMapper.selectHazmatCountOfWarehouse(hazmatFlow.getCabinetId(), hazmatFlow.getBasicId(),18L,hazmatFlow.getAuxiliaryCabinetId()); if (hazmatFlow.getState()==3){ if (hzHazmat.getState()==2||hzHazmat.getState()==3||hzHazmat.getState()==4){ errorMessages.add(String.format("第%d条数据:危化品状态异常,不能出库", i + 1)); }else { hzHazmat.setState(HazmatStatusEnum.USING.getCode()); hzHazmat.setUpdateBy(hazmatFlow.getCreateBy()); HzWarehouseRecord warehouseRecord = new HzWarehouseRecord() .setWarehouseId(hzHazmat.getWarehouseId()) .setBasicId(hzHazmat.getBasicId()) .setCupboardId(hzHazmat.getCupboardId()) .setCreateBy(hazmatFlow.getCreateBy()) .setNum(-1) .setState(OperateStatusEnum.USING.getCode()) .setCompanyId(18L) .setRemaining(count-1); warehouseRecordMapper.insert(warehouseRecord); } hazmatMapper.updateById(hzHazmat); } if (hazmatFlow.getState()==4){ HzWarehouseRecord warehouseRecord = new HzWarehouseRecord() .setWarehouseId(hzHazmat.getWarehouseId()) .setBasicId(hzHazmat.getBasicId()) .setCupboardId(hzHazmat.getCupboardId()) .setCreateBy(hazmatFlow.getCreateBy()) .setNum(-1) .setState(OperateStatusEnum.DISCARD.getCode()) .setCompanyId(18L) .setRemaining(count-1); warehouseRecordMapper.insert(warehouseRecord); hzHazmat.setUpdateBy(hazmatFlow.getCreateBy()); if (hazmatFlow.getReasonId()==0||hazmatFlow.getReasonId()==3){ hzHazmat.setState(HazmatStatusEnum.USED.getCode()); }else { hzHazmat.setState(HazmatStatusEnum.DISCARD.getCode()); } hazmatMapper.updateById(hzHazmat); } if (hazmatFlow.getState()==2&&hzHazmat.getState()==HazmatStatusEnum.USING.getCode()){ HzWarehouseRecord warehouseRecord = new HzWarehouseRecord() .setWarehouseId(hzHazmat.getWarehouseId()) .setBasicId(hzHazmat.getBasicId()) .setCupboardId(hzHazmat.getCupboardId()) .setCreateBy(hazmatFlow.getCreateBy()) .setNum(1) .setState(OperateStatusEnum.RETURN.getCode()) .setCompanyId(18L) .setRemaining(count+1); warehouseRecordMapper.insert(warehouseRecord); hzHazmat.setUpdateBy(hazmatFlow.getCreateBy()); hzHazmat.setState(HazmatStatusEnum.USEWAREHOUSEIN.getCode()); hazmatMapper.updateById(hzHazmat); }else{ errorMessages.add(String.format("第%d条数据:危化品状态异常,不能回库", i + 1)); } // if (hazmatFlow.getNum() == null || hazmatFlow.getNum().compareTo(BigDecimal.ZERO) <= 0) { // errorMessages.add(String.format("第%d条数据:数量必须大于0", i + 1)); // failCount++; // continue; // } hazmatFlowList.add(hazmatFlow); successCount++; } catch (Exception e) { errorMessages.add(String.format("第%d条数据处理失败:%s", i + 1, e.getMessage())); failCount++; } } if (!hazmatFlowList.isEmpty()) { boolean saveResult = this.saveBatch(hazmatFlowList, 50); if (!saveResult) { return CommonResult.failed("批量保存流向数据失败"); } } Map resultData = new HashMap<>(); resultData.put("totalCount", flows.size()); resultData.put("successCount", successCount); resultData.put("failCount", failCount); if (!errorMessages.isEmpty()) { resultData.put("errorMessages", errorMessages); } if (failCount > 0) { return CommonResult.success(resultData, "批量推送完成,部分数据失败"); } return CommonResult.success(resultData, "批量推送成功"); } private HzHazmatFlow convertToHzHazmatFlow(ChemicalLedger flowData) { HzHazmatFlow hazmatFlow = new HzHazmatFlow(); hazmatFlow.setUuid(flowData.getId()); hazmatFlow.setHazmatId(flowData.getHazmatId()); // hazmatFlow.setBasicId(flowData.getBasicId()); hazmatFlow.setState(flowData.getStorageType()); hazmatFlow.setNum(BigDecimal.valueOf(flowData.getNum())); hazmatFlow.setCompanyId(18L); // hazmatFlow.setCreateId(flowData.getCreateId()); hazmatFlow.setCabinetId(Long.valueOf(flowData.getCabinetId())); hazmatFlow.setCabinetName(flowData.getCabinetName()); hazmatFlow.setAuxiliaryCabinetId(Long.valueOf(flowData.getAuxiliaryCabinetId())); hazmatFlow.setAuxiliaryCabinetName(flowData.getAuxiliaryCabinetName()); hazmatFlow.setOwner(flowData.getOwner()); hazmatFlow.setChemicalType(flowData.getChemicalType()); hazmatFlow.setContainerNo(flowData.getContainerNo()); hazmatFlow.setContainerUse(flowData.getContainerUse()); hazmatFlow.setControlType(flowData.getControlType()); hazmatFlow.setCreateBy(flowData.getCreateUserName()); hazmatFlow.setUNameOne(flowData.getUNameOne()); hazmatFlow.setFacelmgUrl(flowData.getFaceImgUrl()); hazmatFlow.setUNameTwo(flowData.getUNameTwo()); hazmatFlow.setFacelmgUrlTwo(flowData.getFaceImgUrlTwo()); hazmatFlow.setHazchemType(flowData.getHazchemType()); hazmatFlow.setOperate(flowData.getOperate()); hazmatFlow.setOrgName(flowData.getOrgName()); hazmatFlow.setReason(flowData.getReason()); hazmatFlow.setReasonId(flowData.getReasonId()); hazmatFlow.setRegulatory(flowData.getRegulatory()); hazmatFlow.setRoomName(flowData.getRoomName()); hazmatFlow.setSchoolName(flowData.getSchoolName()); hazmatFlow.setUsername(flowData.getUsername()); hazmatFlow.setWeight(flowData.getWeight()); hazmatFlow.setWeightUnit(flowData.getWeightUnit()); hazmatFlow.setConsumption(flowData.getConsumption()); if (flowData.getCreateTime() != null) { hazmatFlow.setCreateTime(flowData.getCreateTime()); } return hazmatFlow; } public void checkUserAllowed(HzHazmatFlow hazmatFlow,SysUser user) { if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) { throw new ApiException("管理员不能操作"); } if(hazmatFlow!=null){ if (!user.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){ if(!Objects.equals(user.getCompanyId(), hazmatFlow.getCompanyId())){ throw new ApiException("无权限操作其他企业数据"); } } } } }