package org.jeecg.modules.dnc.service.impl; import cn.hutool.core.util.StrUtil; import com.jeecg.weibo.exception.BusinessException; import liquibase.pro.packaged.S; import org.jeecg.modules.dnc.dto.ComponentHierarchy; import org.jeecg.modules.dnc.dto.TransferPackage; import org.jeecg.modules.dnc.entity.*; import org.jeecg.modules.dnc.mapper.*; import org.jeecg.modules.dnc.service.*; import org.jeecg.modules.system.entity.MdcProduction; import org.jeecg.modules.system.service.IMdcProductionService; import org.jeecg.modules.system.service.ISysUserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service public class DataImportService { private static final Logger logger = LoggerFactory.getLogger(DataImportService.class); @Autowired private ProductInfoMapper productMapper; @Autowired private ComponentInfoMapper componentMapper; @Autowired private PartsInfoMapper partsMapper; @Autowired private ProcessSpecVersionMapper psvMapper; @Autowired private ProcessStreamMapper processMapper; @Autowired private WorkStepMapper workStepMapper; @Autowired private ProductMixMapper productMixMapper; @Autowired private PermissionStreamNewMapper permissionStreamNewMapper; @Autowired private DeviceManagementMapper deviceManagementMapper; @Autowired private DeviceTypeMapper deviceTypeMapper; @Autowired private DocInfoMapper docInfoMapper; @Autowired private DocFileMapper docFileMapper; @Autowired private DocRelativeMapper docRelativeMapper; @Autowired private CutterMapper cutterMapper; @Autowired private GuideCardBatchMapper guideCardBatchMapper; @Autowired private ISysUserService sysUserService; @Autowired private IMdcProductionService mdcProductionService; @Autowired private IProductPermissionService productPermissionService; @Autowired private IProductDepartmentService productDepartmentService; @Autowired private IComponentPermissionService componentPermissionService; @Autowired private IComponentDepartmentService componentDepartmentService; @Autowired private IPartsPermissionService partsPermissionService; @Autowired private IPartsDepartmentService partsDepartmentService; @Autowired private IProcessSpecVersionPermissionService processSpecVersionPermissionService; @Autowired private IProcessSpecVersionDepartmentService processSpecVersionDepartmentService; @Autowired private IProcessStreamPermissionService processStreamPermissionService; @Autowired private IProcessionDepartmentService processionDepartmentService; @Autowired private IWorkStepPermissionService workStepPermissionService; @Autowired private IWorkStepDepartmentService workStepDepartmentService; @Transactional(rollbackFor = Exception.class) public void importTransferPackage(TransferPackage transferPackage) { try { logger.info("开始导入传输包数据, 类型: {}", transferPackage.getDataType()); // 保存产品 if (transferPackage.getTraceChain() != null && transferPackage.getTraceChain().getProduct() != null) { saveProduct(transferPackage.getTraceChain().getProduct()); } // 保存部件层级 if (transferPackage.getTraceChain() != null && transferPackage.getTraceChain().getComponentHierarchy() != null) { saveComponentHierarchy(transferPackage.getTraceChain().getComponentHierarchy()); } // 保存零件 if (transferPackage.getTraceChain() != null && transferPackage.getTraceChain().getParts() != null) { saveParts(transferPackage.getTraceChain().getParts()); } // 保存工艺规程 if (transferPackage.getTraceChain() != null && transferPackage.getTraceChain().getProcessSpec() != null) { saveProcessSpec(transferPackage.getTraceChain().getProcessSpec()); } // 保存工序 if (transferPackage.getTraceChain() != null&& transferPackage.getTraceChain().getProcess() != null) { saveProcess(transferPackage.getTraceChain().getProcess()); } // 保存工步 if (transferPackage.getTraceChain() != null&& transferPackage.getTraceChain().getWorkStep() != null) { saveWorkSteps(transferPackage.getTraceChain().getWorkStep()); } // 保存结构树 if (transferPackage.getTraceChain() != null&& transferPackage.getTraceChain().getTreePath() != null) { saveTreePath(transferPackage.getTraceChain().getTreePath()); } //保存权限 if (transferPackage.getTraceChain() != null&& transferPackage.getTraceChain().getPermissionStreamNewList() != null) { savePermissionStreamNewList(transferPackage.getTraceChain().getPermissionStreamNewList()); } // 保存设备类 if (transferPackage.getTraceChain() != null&& transferPackage.getTraceChain().getDeviceManagement() != null) { saveDeviceManagement(transferPackage.getTraceChain().getDeviceManagement()); } // 保存设备类对应信息 if (transferPackage.getTraceChain() != null&& transferPackage.getTraceChain().getDeviceType() != null) { saveDeviceType(transferPackage.getTraceChain().getDeviceType()); } // 保存文档 if (transferPackage.getTraceChain() != null&& transferPackage.getTraceChain().getDocInfo() != null) { saveDocInfo(transferPackage.getTraceChain().getDocInfo()); } // 保存文件 if (transferPackage.getTraceChain() !=null&& transferPackage.getTraceChain().getDocFile() != null) { saveDocFile(transferPackage.getTraceChain().getDocFile()); } // 保存文档文件对应关系 if (transferPackage.getDocRelative() !=null){ saveDocRelative(transferPackage.getDocRelative()); } // 保存刀具系统 if (transferPackage.getTraceChain() !=null&& transferPackage.getTraceChain().getCutterList() != null) { saveCutterList(transferPackage.getTraceChain().getCutterList()); } //保存数控程序加工确认表 if (transferPackage.getTraceChain() !=null&& transferPackage.getTraceChain().getGuideCardBatch() != null) { saveGuideCardBatch(transferPackage.getTraceChain().getGuideCardBatch()); } logger.info("数据导入成功"); } catch (DuplicateKeyException e) { logger.warn("主键冲突: {}", e.getMessage()); throw new BusinessException("数据已存在,无法重复导入"); } catch (DataIntegrityViolationException e) { logger.error("数据完整性违反: {}", e.getMessage()); throw new BusinessException("数据不完整,请检查必填字段"); } catch (Exception e) { logger.error("数据导入失败: {}", e.getMessage(), e); throw new BusinessException("数据导入失败: " + e.getMessage()); } } private void saveProduct(ProductInfo product) { if (productMapper.selectById(product.getProductId()) == null) { productMapper.insert(product); logger.debug("产品已保存: {}", product.getProductId()); } else { logger.debug("产品已存在: {}", product.getProductId()); } } private void saveComponentHierarchy(ComponentHierarchy hierarchy) { for (ComponentInfo component : hierarchy.getComponents()) { if (componentMapper.selectById(component.getComponentId()) == null) { componentMapper.insert(component); logger.debug("部件已保存: {}", component.getComponentId()); } else { logger.debug("部件已存在: {}", component.getComponentId()); } } } private void saveParts(PartsInfo parts) { if (partsMapper.selectById(parts.getPartsId()) == null) { partsMapper.insert(parts); logger.debug("零件已保存: {}", parts.getPartsId()); } else { logger.debug("零件已存在: {}", parts.getPartsId()); } } private void saveProcessSpec(ProcessSpecVersion processSpec) { if (psvMapper.selectById(processSpec.getId()) == null) { psvMapper.insert(processSpec); logger.debug("工艺规程已保存: {}", processSpec.getId()); } else { logger.debug("工艺规程已存在: {}", processSpec.getId()); } } private void saveProcess(ProcessStream process) { if (processMapper.selectById(process.getProcessId()) == null) { processMapper.insert(process); logger.debug("工序已保存: {}", process.getProcessId()); } else { logger.debug("工序已存在: {}", process.getProcessId()); } } private void saveWorkSteps(WorkStep workStep) { if (workStepMapper.selectById(workStep.getId()) == null) { workStepMapper.insert(workStep); logger.debug("工步已保存: {}", workStep.getId()); } else { logger.debug("工步已存在: {}", workStep.getId()); } } private void saveTreePath(List productMixList){ for (ProductMix productMix : productMixList) { if (productMixMapper.selectById(productMix.getId()) == null) { productMixMapper.insert(productMix); logger.debug("产品组合已保存: {}", productMix.getId()); } else { logger.debug("产品组合已存在: {}", productMix.getId()); } } } private void savePermissionStreamNewList(List permissionStreamNewList) { for (PermissionStreamNew permissionStreamNew : permissionStreamNewList) { if (permissionStreamNewMapper.selectById(permissionStreamNew.getId()) == null) { if (permissionStreamNew.getUserId() != null) { String id=sysUserService.getUserByName(permissionStreamNew.getUserId()).getId(); if (id!=null){ permissionStreamNew.setUserId(id); } } if (permissionStreamNew.getDepartId() != null) { MdcProduction mdcProduction=mdcProductionService.findByOrgCode(permissionStreamNew.getDepartId()); if (mdcProduction!=null){ permissionStreamNew.setDepartId(mdcProduction.getId()); } } permissionStreamNewMapper.insert(permissionStreamNew); logger.debug("权限已保存: {}", permissionStreamNew.getId()); } } //分批添加产品、部件、零件、工艺规程、工序、工步权限 permissionStreamNewList.forEach(item -> { switch (item.getBusinessType()){ case "1": if (StrUtil.isNotEmpty(item.getUserId())){ ProductPermission productPermission = new ProductPermission(); productPermission.setProductId(item.getBusinessId()); productPermission.setUserId(item.getUserId()); productPermissionService.save(productPermission); }else { ProductDepartment productDepartment = new ProductDepartment(); productDepartment.setProductId(item.getBusinessId()); productDepartment.setDepartId(item.getDepartId()); productDepartmentService.save(productDepartment); } break; case "2": if (StrUtil.isNotEmpty(item.getUserId())){ ComponentPermission componentPermission = new ComponentPermission(); componentPermission.setComponentId(item.getBusinessId()); componentPermission.setUserId(item.getUserId()); componentPermissionService.save(componentPermission); }else { ComponentDepartment componentDepartment = new ComponentDepartment(); componentDepartment.setComponentId(item.getBusinessId()); componentDepartment.setDepartId(item.getDepartId()); componentDepartmentService.save(componentDepartment); } break; case "3": if (StrUtil.isNotEmpty(item.getUserId())){ PartsPermission partsPermission = new PartsPermission(); partsPermission.setPartsId(item.getBusinessId()); partsPermission.setUserId(item.getUserId()); partsPermissionService.save(partsPermission); }else { PartsDepartment partsDepartment = new PartsDepartment(); partsDepartment.setPartsId(item.getBusinessId()); partsDepartment.setDepartId(item.getDepartId()); partsDepartmentService.save(partsDepartment); } break; case "4": if (StrUtil.isNotEmpty(item.getUserId())){ ProcessSpecVersionPermission processSpecVersionPermission = new ProcessSpecVersionPermission(); processSpecVersionPermission.setPsvId(item.getBusinessId()); processSpecVersionPermission.setUserId(item.getUserId()); processSpecVersionPermissionService.save(processSpecVersionPermission); }else { ProcessSpecVersionDepartment processSpecVersionDepartment = new ProcessSpecVersionDepartment(); processSpecVersionDepartment.setPsvId(item.getBusinessId()); processSpecVersionDepartment.setDepartId(item.getDepartId()); processSpecVersionDepartmentService.save(processSpecVersionDepartment); } break; case "5": if (StrUtil.isNotEmpty(item.getUserId())){ ProcessionPermission processionPermission = new ProcessionPermission(); processionPermission.setProcessId(item.getBusinessId()); processionPermission.setUserId(item.getUserId()); processStreamPermissionService.save(processionPermission); }else { ProcessionDepartment processionDepartment = new ProcessionDepartment(); processionDepartment.setProcessId(item.getBusinessId()); processionDepartment.setDepartId(item.getDepartId()); processionDepartmentService.save(processionDepartment); } break; case "6": if (StrUtil.isNotEmpty(item.getUserId())){ WorkStepPermission workStepPermission = new WorkStepPermission(); workStepPermission.setStepId(item.getBusinessId()); workStepPermission.setUserId(item.getUserId()); workStepPermissionService.save(workStepPermission); }else { WorkStepDepartment workStepDepartment = new WorkStepDepartment(); workStepDepartment.setStepId(item.getBusinessId()); workStepDepartment.setDepartId(item.getDepartId()); workStepDepartmentService.save(workStepDepartment); } break; default: } }); } private void saveDeviceManagement(DeviceManagement deviceManagement) { if (deviceManagementMapper.selectById(deviceManagement.getId()) == null) { deviceManagementMapper.insert(deviceManagement); logger.debug("设备类信息已保存: {}", deviceManagement.getId()); } else { logger.debug("设备类信息已存在: {}", deviceManagement.getId()); } } private void saveDeviceType(DeviceType deviceType) { if (deviceTypeMapper.selectById(deviceType.getId()) == null) { deviceTypeMapper.insert(deviceType); logger.debug("设备类已保存: {}", deviceType.getId()); } else { logger.debug("设备类已存在: {}", deviceType.getId()); } } private void saveDocInfo(DocInfo docInfo) { if (docInfoMapper.selectById(docInfo.getDocId()) == null) { docInfoMapper.insert(docInfo); logger.debug("文档已保存: {}", docInfo.getDocId()); } else { logger.debug("文档已存在: {}", docInfo.getDocId()); } } private void saveDocFile(DocFile docFile) { if (docFileMapper.selectById(docFile.getFileId()) == null) { docFileMapper.insert(docFile); logger.debug("文档文件已保存: {}", docFile.getFileId()); } else { logger.debug("文档文件已存在: {}", docFile.getFileId()); } } private void saveDocRelative(DocRelative docRelative) { if (docRelativeMapper.selectById(docRelative.getId()) == null) { docRelativeMapper.insert(docRelative); logger.debug("文档对应关系已保存: {}", docRelative.getId()); } else { logger.debug("文档对应关系已存在: {}", docRelative.getId()); } } private void saveCutterList(List cutterList) { for (Cutter cutter : cutterList) { if (cutterMapper.selectById(cutter.getId()) == null) { cutterMapper.insert(cutter); logger.debug("刀具已保存: {}", cutter.getId()); } else { cutterMapper.updateById(cutter); } } } private void saveGuideCardBatch(GuideCardBatch guideCardBatch) { if (guideCardBatchMapper.selectById(guideCardBatch.getId()) == null) { guideCardBatch.setFlowStatus("1"); guideCardBatchMapper.insert(guideCardBatch); logger.debug("刀片批次已保存: {}", guideCardBatch.getId()); } else { logger.debug("刀片批次已存在: {}", guideCardBatch.getId()); } } }