| | |
| | | package org.jeecg.modules.eam.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.jeecg.weibo.exception.BusinessException; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.jeecg.common.api.vo.FileUploadResult; |
| | | import org.flowable.engine.TaskService; |
| | | import org.flowable.task.api.Task; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.exception.JeecgBootException; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.modules.system.entity.BaseFactory; |
| | | import org.jeecg.modules.system.entity.BaseFactoryUser; |
| | | import org.jeecg.modules.system.service.IBaseFactoryService; |
| | | import org.jeecg.modules.system.service.IBaseFactoryUserService; |
| | | import org.jeecg.modules.eam.constant.BusinessCodeConst; |
| | | import org.jeecg.modules.eam.constant.EamMaintenanceStandardDetailCategory; |
| | | import org.jeecg.modules.eam.constant.MaintenanceStandardStatusEnum; |
| | | import org.jeecg.modules.eam.entity.EamEquipment; |
| | | import org.jeecg.modules.eam.entity.EamEquipmentExtend; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandard; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail; |
| | | import org.jeecg.modules.eam.mapper.EamMaintenanceStandardMapper; |
| | | import org.jeecg.modules.eam.request.EamMaintenanceStandardRequest; |
| | | import org.jeecg.modules.eam.request.ImportException; |
| | | import org.jeecg.modules.eam.service.IEamEquipmentExtendService; |
| | | import org.jeecg.modules.eam.service.IEamEquipmentService; |
| | | import org.jeecg.modules.eam.service.IEamMaintenanceStandardDetailService; |
| | | import org.jeecg.modules.eam.service.IEamMaintenanceStandardService; |
| | | import org.jeecg.modules.eam.vo.*; |
| | | import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness; |
| | | import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService; |
| | | import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI; |
| | | import org.jeecg.modules.flowable.apithird.service.FlowCommonService; |
| | | import org.jeecg.modules.flowable.service.IFlowDefinitionService; |
| | | import org.jeecg.modules.flowable.service.IFlowTaskService; |
| | | import org.jeecg.modules.system.service.ISysBusinessCodeRuleService; |
| | | import org.jeecg.modules.system.service.ISysUserService; |
| | | import org.jeecg.modules.system.vo.UserSelector; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | * @Date: 2025-03-26 |
| | | * @Version: V1.0 |
| | | */ |
| | | @Service |
| | | public class EamMaintenanceStandardServiceImpl extends ServiceImpl<EamMaintenanceStandardMapper, EamMaintenanceStandard> implements IEamMaintenanceStandardService { |
| | | @Service("IEamMaintenanceStandardService") |
| | | public class EamMaintenanceStandardServiceImpl extends ServiceImpl<EamMaintenanceStandardMapper, EamMaintenanceStandard> implements IEamMaintenanceStandardService , FlowCallBackServiceI { |
| | | |
| | | @Resource |
| | | private EamMaintenanceStandardMapper eamMaintenanceStandardMapper; |
| | | |
| | | @Autowired |
| | | private IEamMaintenanceStandardDetailService eamMaintenanceStandardDetailService; |
| | | @Autowired |
| | | private IBaseFactoryUserService baseFactoryUserService; |
| | | @Autowired |
| | | private IBaseFactoryService baseFactoryService; |
| | | @Resource |
| | | private FlowCommonService flowCommonService; |
| | | @Resource |
| | | private IFlowDefinitionService flowDefinitionService; |
| | | @Autowired |
| | | private IFlowTaskService flowTaskService; |
| | | @Autowired |
| | | private TaskService taskService; |
| | | @Autowired |
| | | private IFlowMyBusinessService flowMyBusinessService; |
| | | @Autowired |
| | | private ISysUserService sysUserService; |
| | | @Autowired |
| | | private IEamEquipmentService eamEquipmentService; |
| | | @Autowired |
| | | private IEamEquipmentExtendService eamEquipmentExtendService; |
| | | @Autowired |
| | | private ISysBusinessCodeRuleService businessCodeRuleService; |
| | | |
| | | // 常量定义 |
| | | private static final String REPAIR_TITLE = "维修人员保养内容"; |
| | | private static final String OPERATOR_TITLE = "操作人员保养内容"; |
| | | private static final Map<String, EamMaintenanceStandardDetailCategory> SECOND_CATEGORY_MAPPING = new HashMap<>(); |
| | | |
| | | // 枚举映射初始化 |
| | | static { |
| | | SECOND_CATEGORY_MAPPING.put(REPAIR_TITLE, EamMaintenanceStandardDetailCategory.REPAIRER_MAINTENANCE); |
| | | SECOND_CATEGORY_MAPPING.put(OPERATOR_TITLE, EamMaintenanceStandardDetailCategory.OPERATOR_MAINTENANCE); |
| | | } |
| | | |
| | | @Override |
| | | public IPage<EamMaintenanceStandard> queryPageList(Page<EamMaintenanceStandard> page, EamMaintenanceStandard eamMaintenanceStandard) { |
| | |
| | | if (sysUser == null) { |
| | | return page; |
| | | } |
| | | if (StringUtils.isNotBlank(sysUser.getEquipmentIds())) { |
| | | if (StringUtils.isNotBlank(sysUser.getEamEquipmentIds())) { |
| | | //选择了设备,根据设备id过滤设备 |
| | | List<String> equipArr = Arrays.asList(sysUser.getEquipmentIds().split(",")); |
| | | List<String> equipArr = Arrays.asList(sysUser.getEamEquipmentIds().split(",")); |
| | | queryWrapper.in("e.equipment_code", equipArr); |
| | | } else { |
| | | //没有选择设备,根据车间过滤设备 |
| | | queryWrapper.exists("select 1 from mdc_user_production t where t.user_id={0} and t.pro_id=e.org_id ", sysUser.getId()); |
| | | //没有选择设备,根据中心过滤设备 |
| | | List<BaseFactoryUser> baseFactoryUserList=baseFactoryUserService. |
| | | list(new LambdaQueryWrapper<BaseFactoryUser>().eq(BaseFactoryUser::getUserId,sysUser.getId())); |
| | | if(!CollectionUtils.isEmpty(baseFactoryUserList)){ |
| | | List<String> factoryIds = baseFactoryUserList.stream().map(BaseFactoryUser::getFactoryId).collect(Collectors.toList()); |
| | | List<String> factoryCode= baseFactoryService.listByIds(factoryIds).stream().map(BaseFactory::getFactoryCode).collect(Collectors.toList()); |
| | | queryWrapper.in("e.factory_org_code", factoryCode); |
| | | } |
| | | } |
| | | if(eamMaintenanceStandard != null) { |
| | | //编码 模糊查询 |
| | |
| | | } |
| | | //名称 模糊查询 |
| | | if(StringUtils.isNotBlank(eamMaintenanceStandard.getStandardName())) { |
| | | queryWrapper.like("ems.standard_name", eamMaintenanceStandard.getStandardCode()); |
| | | queryWrapper.like("ems.standard_name", eamMaintenanceStandard.getStandardName()); |
| | | } |
| | | //设备 |
| | | if(StringUtils.isNotBlank(eamMaintenanceStandard.getEquipmentId())) { |
| | |
| | | .or() |
| | | .like("e.equipment_code", eamMaintenanceStandard.getKeyword())); |
| | | } |
| | | //id |
| | | if(StringUtils.isNotBlank(eamMaintenanceStandard.getId())) { |
| | | queryWrapper.eq("ems.id", eamMaintenanceStandard.getId()); |
| | | } |
| | | } |
| | | queryWrapper.orderByDesc("ems.create_time"); |
| | | return eamMaintenanceStandardMapper.queryPageList(page, queryWrapper); |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean addMaintenanceStandard(EamMaintenanceStandardRequest standardRequest) { |
| | | EamMaintenanceStandard entity = new EamMaintenanceStandard(); |
| | | entity.setStandardCode(standardRequest.getStandardCode()); |
| | | entity.setStandardName(standardRequest.getStandardName()); |
| | | entity.setMaintenanceCategory(standardRequest.getMaintenanceCategory()); |
| | | entity.setMaintenancePeriod(standardRequest.getMaintenancePeriod()); |
| | | entity.setInitialDate(standardRequest.getInitialDate()); |
| | | entity.setFileCode(standardRequest.getFileCode()); |
| | | entity.setStandardStatus(MaintenanceStandardStatusEnum.NORMAL.name()); |
| | | BeanUtils.copyProperties(standardRequest, entity); |
| | | entity.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name()); |
| | | //版本递增 |
| | | entity.setStandardVersion(CommonConstant.OPERATE_TYPE_1); |
| | | entity.setStandardVersion("v"+CommonConstant.OPERATE_TYPE_1); |
| | | //设备处理 |
| | | entity.setEquipmentId(standardRequest.getEquipmentId()); |
| | | //删除标记 |
| | |
| | | EamMaintenanceStandard exist = checkDuplicate(entity.getEquipmentId(), entity.getMaintenanceCategory()); |
| | | if(exist != null){ |
| | | throw new JeecgBootException("设备标准已存在,不能重复添加!"); |
| | | } |
| | | //处理附件 |
| | | if(CollectionUtil.isNotEmpty(standardRequest.getFileList())) { |
| | | FileUploadResult fileUploadResult = standardRequest.getFileList().get(0); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | try { |
| | | String referenceFile = mapper.writeValueAsString(fileUploadResult); |
| | | entity.setReferenceFile(referenceFile); |
| | | } catch (JsonProcessingException e) { |
| | | log.error("JSON转换失败:" + e.getMessage(), e); |
| | | } |
| | | } |
| | | eamMaintenanceStandardMapper.insert(entity); |
| | | //处理明细数据 |
| | |
| | | } |
| | | entity.setStandardName(standardRequest.getStandardName()); |
| | | entity.setMaintenancePeriod(standardRequest.getMaintenancePeriod()); |
| | | entity.setFileCode(standardRequest.getFileCode()); |
| | | //处理附件 |
| | | if(CollectionUtil.isNotEmpty(standardRequest.getFileList())) { |
| | | FileUploadResult fileUploadResult = standardRequest.getFileList().get(0); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | try { |
| | | String referenceFile = mapper.writeValueAsString(fileUploadResult); |
| | | entity.setReferenceFile(referenceFile); |
| | | } catch (JsonProcessingException e) { |
| | | log.error("JSON转换失败:" + e.getMessage(), e); |
| | | } |
| | | }else { |
| | | entity.setReferenceFile(null); |
| | | } |
| | | eamMaintenanceStandardMapper.updateById(entity); |
| | | //处理详情 |
| | | if(CollectionUtil.isNotEmpty(standardRequest.getTableDetailList())) { |
| | |
| | | if(entity == null){ |
| | | throw new JeecgBootException("编辑的数据已删除,请刷新重试!"); |
| | | } |
| | | entity.setStandardStatus(MaintenanceStandardStatusEnum.ABOLISH.name()); |
| | | //原来的作废 |
| | | eamMaintenanceStandardMapper.updateById(entity); |
| | | |
| | | //新增一个版本 |
| | | EamMaintenanceStandard newEntity = new EamMaintenanceStandard(); |
| | |
| | | newEntity.setMaintenanceCategory(standardRequest.getMaintenanceCategory()); |
| | | newEntity.setMaintenancePeriod(standardRequest.getMaintenancePeriod()); |
| | | newEntity.setInitialDate(standardRequest.getInitialDate()); |
| | | newEntity.setFileCode(standardRequest.getFileCode()); |
| | | newEntity.setStandardStatus(MaintenanceStandardStatusEnum.NORMAL.name()); |
| | | newEntity.setPeriodUnit(standardRequest.getPeriodUnit()); |
| | | newEntity.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name()); |
| | | //版本递增 |
| | | newEntity.setStandardVersion(entity.getStandardVersion() + 1); |
| | | //获取数字 |
| | | Pattern pattern = Pattern.compile("(\\d+)(?:\\.\\d+)*$"); |
| | | Matcher matcher = pattern.matcher(entity.getStandardVersion()); |
| | | if (matcher.find()) { |
| | | try { |
| | | int mainVersion = Integer.parseInt(matcher.group(1)); |
| | | newEntity.setStandardVersion("v"+(mainVersion+1)); |
| | | } catch (NumberFormatException ignored) { |
| | | } |
| | | } |
| | | //设备处理 |
| | | newEntity.setEquipmentId(standardRequest.getEquipmentId()); |
| | | //删除标记 |
| | |
| | | EamMaintenanceStandard exist = checkDuplicate(newEntity.getEquipmentId(), newEntity.getMaintenanceCategory()); |
| | | if(exist != null){ |
| | | throw new JeecgBootException("设备标准已存在,不能重复添加!"); |
| | | } |
| | | //处理附件 |
| | | if(CollectionUtil.isNotEmpty(standardRequest.getFileList())) { |
| | | FileUploadResult fileUploadResult = standardRequest.getFileList().get(0); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | try { |
| | | String referenceFile = mapper.writeValueAsString(fileUploadResult); |
| | | newEntity.setReferenceFile(referenceFile); |
| | | } catch (JsonProcessingException e) { |
| | | log.error("JSON转换失败:" + e.getMessage(), e); |
| | | } |
| | | } |
| | | eamMaintenanceStandardMapper.insert(newEntity); |
| | | //处理明细数据 |
| | |
| | | queryWrapper.eq(EamMaintenanceStandard::getEquipmentId, equipmentId); |
| | | queryWrapper.eq(EamMaintenanceStandard::getMaintenanceCategory, maintenanceCategory); |
| | | queryWrapper.eq(EamMaintenanceStandard::getDelFlag, CommonConstant.DEL_FLAG_0); |
| | | queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, MaintenanceStandardStatusEnum.NORMAL.name()); |
| | | queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, MaintenanceStandardStatusEnum.WAIT_SUBMIT.name()); |
| | | queryWrapper.orderByDesc(EamMaintenanceStandard::getStandardVersion); |
| | | |
| | | List<EamMaintenanceStandard> list = eamMaintenanceStandardMapper.selectList(queryWrapper); |
| | |
| | | query.setEquipmentId(equipmentId); |
| | | query.setKeyword(keyword); |
| | | query.setMaintenanceCategory(maintenanceCategory); |
| | | query.setStandardStatus(MaintenanceStandardStatusEnum.NORMAL.name()); |
| | | query.setStandardStatus(MaintenanceStandardStatusEnum.START.name()); |
| | | IPage<EamMaintenanceStandard> pageData = this.queryPageList(page, query); |
| | | return pageData.getRecords(); |
| | | } |
| | |
| | | LambdaQueryWrapper<EamMaintenanceStandard> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(EamMaintenanceStandard::getDelFlag, CommonConstant.DEL_FLAG_0); |
| | | queryWrapper.eq(EamMaintenanceStandard::getMaintenanceCategory, maintenanceCategory); |
| | | queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, MaintenanceStandardStatusEnum.NORMAL.name()); |
| | | queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, MaintenanceStandardStatusEnum.START.name()); |
| | | return eamMaintenanceStandardMapper.selectList(queryWrapper); |
| | | } |
| | | |
| | | |
| | | /*流程业务代码--------------------------开始*/ |
| | | |
| | | /** |
| | | * 流程启动,保存对应的数据 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result<?> saveEamMaintenanceStandardProcess(String id){ |
| | | LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | EamMaintenanceStandard maintenanceStandard=this.getById(id); |
| | | if (maintenanceStandard==null){ |
| | | return Result.error("未找到对应保养标准"); |
| | | } |
| | | System.out.println("保养规范流程:" + maintenanceStandard.getId()); |
| | | flowCommonService.initActBusiness(maintenanceStandard.getStandardName()+"规范进行流程审核", |
| | | maintenanceStandard.getId(), "IEamMaintenanceStandardService", "eam_maintenance_standard", null); |
| | | Map<String, Object> variables = new HashMap<>(); |
| | | variables.put("dataId", maintenanceStandard.getId()); |
| | | variables.put("organization", "保养规范启动流程"); |
| | | variables.put("comment", "保养规范启动流程"); |
| | | variables.put("proofreading",true); |
| | | Result result= flowDefinitionService.startProcessInstanceByKey("eam_maintenance_standard", variables); |
| | | if (!result.isSuccess()) { |
| | | super.removeById(maintenanceStandard.getId()); |
| | | }else { |
| | | maintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_REPAIR_DIRECTOR.name()); |
| | | maintenanceStandard.setDesigner(user.getUsername()); |
| | | maintenanceStandard.setDesignTime(new Date()); |
| | | eamMaintenanceStandardMapper.updateById(maintenanceStandard); |
| | | //获取flow的流程实例id,设置下一步的处理人员 |
| | | List<FlowMyBusiness> businessList = flowMyBusinessService.list( |
| | | new QueryWrapper<FlowMyBusiness>() |
| | | .eq("data_id", maintenanceStandard.getId()) |
| | | ); |
| | | if (businessList.isEmpty()) { |
| | | return Result.error("流程记录不存在"); |
| | | } |
| | | FlowMyBusiness flowMyBusiness = businessList.get(0); |
| | | EamEquipment eamEquipment=eamEquipmentService.getById(maintenanceStandard.getEquipmentId()); |
| | | BaseFactory baseFactory=baseFactoryService.getOne(new QueryWrapper<BaseFactory>().eq("org_code", eamEquipment.getFactoryOrgCode())); |
| | | List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0008); |
| | | if (!CollectionUtils.isEmpty(userSelectorList)) { |
| | | List<String> usernameList=userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList()); |
| | | flowMyBusiness.setTodoUsers(JSON.toJSONString(usernameList)); |
| | | flowMyBusinessService.updateById(flowMyBusiness); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 审批操作 |
| | | * @param eamMaintenanceStandardVo |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result<?> auditEamMaintenanceStandard(EamMaintenanceStandardVo eamMaintenanceStandardVo){ |
| | | try { |
| | | // 参数校验 |
| | | if (StrUtil.isEmpty(eamMaintenanceStandardVo.getTaskId()) || StrUtil.isEmpty(eamMaintenanceStandardVo.getDataId())) { |
| | | return Result.error("参数错误"); |
| | | } |
| | | LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | String userId = user.getId(); |
| | | eamMaintenanceStandardVo.setAssignee(user.getUsername()); |
| | | if (StrUtil.isEmpty(userId)) { |
| | | return Result.error("未找到对应用户"); |
| | | } |
| | | |
| | | // 数据查询 |
| | | EamMaintenanceStandard eamMaintenanceStandard = this.getById(eamMaintenanceStandardVo.getDataId()); |
| | | if (eamMaintenanceStandard == null) { |
| | | return Result.error("未找到对应保养标准"); |
| | | } |
| | | |
| | | // 2. 查询流程业务记录(处理空结果) |
| | | List<FlowMyBusiness> businessList = flowMyBusinessService.list( |
| | | new QueryWrapper<FlowMyBusiness>() |
| | | .eq("process_instance_id", eamMaintenanceStandardVo.getInstanceId()) |
| | | ); |
| | | if (businessList.isEmpty()) { |
| | | return Result.error("流程记录不存在"); |
| | | } |
| | | FlowMyBusiness flowMyBusiness = businessList.get(0); |
| | | |
| | | // 3. 校验用户是否为候选处理人 |
| | | List<String> todoUsers = JSON.parseArray(flowMyBusiness.getTodoUsers(), String.class); |
| | | if (todoUsers == null || !todoUsers.contains(user.getUsername())) { |
| | | return Result.error("用户无权操作此任务"); |
| | | } |
| | | |
| | | // 4. 认领任务(处理已被认领的情况) |
| | | String taskId = flowMyBusiness.getTaskId(); |
| | | Task task = taskService.createTaskQuery().taskId(taskId).singleResult(); |
| | | if (task == null) { |
| | | return Result.error("任务不存在或已完成"); |
| | | } |
| | | if (task.getAssignee() != null && !task.getAssignee().equals(user.getUsername())) { |
| | | return Result.error("任务已被他人认领"); |
| | | } |
| | | taskService.claim(taskId, user.getUsername()); |
| | | |
| | | // 设置流程变量 |
| | | Map<String, Object> values = setProcessVariables(eamMaintenanceStandard, userId, eamMaintenanceStandardVo); |
| | | eamMaintenanceStandardVo.setValues(values); |
| | | eamMaintenanceStandardVo.setComment(values.get("comment").toString()); |
| | | // 完成流程任务 |
| | | Result result = flowTaskService.complete(eamMaintenanceStandardVo); |
| | | if (result.isSuccess()) { |
| | | if (eamMaintenanceStandardVo.getRepairManagerApproveResult() != null) { |
| | | if (eamMaintenanceStandardVo.getRepairManagerApproveResult().equals("1")){ |
| | | List<FlowMyBusiness> newbusinessList = flowMyBusinessService.list( |
| | | new QueryWrapper<FlowMyBusiness>() |
| | | .eq("process_instance_id", eamMaintenanceStandardVo.getInstanceId())); |
| | | FlowMyBusiness newflowMyBusiness = newbusinessList.get(0); |
| | | eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_TECHNICAL_DIRECTOR.name()); |
| | | //获取flow的流程实例id,设置下一步的处理人员 |
| | | EamMaintenanceStandard maintenanceStandard=this.getById(eamMaintenanceStandardVo.getDataId()); |
| | | EamEquipment eamEquipment=eamEquipmentService.getById(maintenanceStandard.getEquipmentId()); |
| | | BaseFactory baseFactory=baseFactoryService.getOne(new QueryWrapper<BaseFactory>().eq("org_code", eamEquipment.getFactoryOrgCode())); |
| | | List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0009); |
| | | if (!CollectionUtils.isEmpty(userSelectorList)) { |
| | | List<String> usernameList=userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList()); |
| | | newflowMyBusiness.setTodoUsers(JSON.toJSONString(usernameList)); |
| | | flowMyBusinessService.updateById(newflowMyBusiness); |
| | | } |
| | | }else { |
| | | eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name()); |
| | | } |
| | | eamMaintenanceStandard.setRepairManager(user.getUsername()); |
| | | eamMaintenanceStandard.setRepairManagerApproveResult(eamMaintenanceStandardVo.getRepairManagerApproveResult()); |
| | | eamMaintenanceStandard.setRepairManagerApproveTime(new Date()); |
| | | eamMaintenanceStandard.setRepairManagerApproveComment(eamMaintenanceStandardVo.getComment()); |
| | | } |
| | | if (eamMaintenanceStandardVo.getTechnicalManagerApproveResult() != null) { |
| | | if (eamMaintenanceStandardVo.getTechnicalManagerApproveResult().equals("1")){ |
| | | eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.START.name()); |
| | | //判断是否进行升版,通过设备编号、保养分类、状态进行筛选 |
| | | EamMaintenanceStandard maintenanceStandard=this.getById(eamMaintenanceStandardVo.getDataId()); |
| | | QueryWrapper<EamMaintenanceStandard> queryWrapper=new QueryWrapper<>(); |
| | | queryWrapper.eq("equipment_id", maintenanceStandard.getEquipmentId()); |
| | | queryWrapper.eq("maintenance_category", maintenanceStandard.getMaintenanceCategory()); |
| | | queryWrapper.eq("standard_status", MaintenanceStandardStatusEnum.START.name()); |
| | | List<EamMaintenanceStandard> list=eamMaintenanceStandardMapper.selectList(queryWrapper); |
| | | if (!CollectionUtils.isEmpty(list)) { |
| | | //作废原有 |
| | | for (EamMaintenanceStandard eamMaintenanceStandard1 : list) { |
| | | eamMaintenanceStandard1.setStandardStatus(MaintenanceStandardStatusEnum.ABOLISH.name()); |
| | | this.updateById(eamMaintenanceStandard1); |
| | | } |
| | | } |
| | | }else { |
| | | eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name()); |
| | | } |
| | | eamMaintenanceStandard.setTechnicalManager(user.getUsername()); |
| | | eamMaintenanceStandard.setTechnicalManagerApproveResult(eamMaintenanceStandardVo.getTechnicalManagerApproveResult()); |
| | | eamMaintenanceStandard.setTechnicalManagerApproveTime(new Date()); |
| | | eamMaintenanceStandard.setTechnicalManagerApproveComment(eamMaintenanceStandardVo.getComment()); |
| | | } |
| | | this.updateById(eamMaintenanceStandard); |
| | | } else { |
| | | return result; |
| | | } |
| | | return Result.OK("操作成功"); |
| | | } catch (Exception e) { |
| | | return Result.error("操作失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private Map<String, Object> setProcessVariables(EamMaintenanceStandard eamMaintenanceStandard, String userId, EamMaintenanceStandardVo eamMaintenanceStandardVo) { |
| | | Map<String, Object> values = new HashMap<>(); |
| | | values.put("dataId", eamMaintenanceStandard.getId()); |
| | | values.put("assignee", userId); |
| | | if (eamMaintenanceStandardVo.getRepairManagerApproveResult() != null) { |
| | | values.put("repairManagerApproveResult", eamMaintenanceStandardVo.getRepairManagerApproveResult()); |
| | | values.put("organization", eamMaintenanceStandardVo.getRepairManagerApproveComment()); |
| | | values.put("comment", eamMaintenanceStandardVo.getRepairManagerApproveComment()); |
| | | } |
| | | if (eamMaintenanceStandardVo.getTechnicalManagerApproveResult() != null) { |
| | | values.put("technicalManagerApproveResult", eamMaintenanceStandardVo.getTechnicalManagerApproveResult()); |
| | | values.put("organization", eamMaintenanceStandardVo.getTechnicalManagerApproveComment()); |
| | | values.put("comment", eamMaintenanceStandardVo.getTechnicalManagerApproveComment()); |
| | | } |
| | | return values; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void afterFlowHandle(FlowMyBusiness business) { |
| | | business.getTaskNameId();//接下来审批的节点 |
| | | business.getValues();//前端传进来的参数 |
| | | business.getActStatus(); |
| | | } |
| | | |
| | | @Override |
| | | public Object getBusinessDataById(String dataId) { |
| | | return this.getById(dataId); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> flowValuesOfTask(String taskNameId, Map<String, Object> values) { |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) { |
| | | //业务是否干预流程,业务干预,流程干预,指定人员进行处理 |
| | | return null; |
| | | } |
| | | |
| | | /*流程业务代码--------------------------结束*/ |
| | | |
| | | |
| | | /*导入点检文件Excel--------------------------开始*/ |
| | | /** |
| | | * 点检表导入入口 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result<?> importPointInspectionExcel(MultipartFile file) { |
| | | try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) { |
| | | Sheet sheet = workbook.getSheetAt(0); |
| | | |
| | | // 提取名称 |
| | | String name = extractInspectionTitle(file) + "点检表"; |
| | | |
| | | // 1. 提取设备信息 |
| | | EamMaintenanceStandard eamMaintenanceStandard = extractDeviceInfo(sheet); |
| | | if (eamMaintenanceStandard == null) { |
| | | return Result.error("设备信息提取失败"); |
| | | }else { |
| | | if (eamMaintenanceStandard.getEquipmentId() == null) { |
| | | return Result.error("设备信息提取失败"); |
| | | } |
| | | } |
| | | |
| | | eamMaintenanceStandard.setStandardName(name); |
| | | eamMaintenanceStandardMapper.insert(eamMaintenanceStandard); |
| | | |
| | | // 2. 提取每日点检项目 |
| | | List<EamMaintenanceStandardDetail> dailyDetails = extractDailyItems(sheet, eamMaintenanceStandard); |
| | | if(dailyDetails.isEmpty()){ |
| | | return Result.error("未找到每日点检项目"); |
| | | } |
| | | |
| | | // 3. 提取周保养项目 |
| | | List<EamMaintenanceStandardDetail> weeklyDetails = extractWeeklyItems(sheet, eamMaintenanceStandard); |
| | | if(weeklyDetails.isEmpty()){ |
| | | return Result.error("未找到周保养项目"); |
| | | } |
| | | |
| | | // 合并并保存所有项目 |
| | | List<EamMaintenanceStandardDetail> allDetails = new ArrayList<>(); |
| | | allDetails.addAll(dailyDetails); |
| | | allDetails.addAll(weeklyDetails); |
| | | |
| | | if (!CollectionUtils.isEmpty(allDetails)) { |
| | | eamMaintenanceStandardDetailService.saveBatch(allDetails); |
| | | } |
| | | |
| | | return Result.OK("点检表导入成功"); |
| | | |
| | | } catch (Exception e) { |
| | | throw new BusinessException("点检表导入失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 提取点检表标题 |
| | | */ |
| | | private String extractInspectionTitle(MultipartFile file) { |
| | | try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) { |
| | | Sheet sheet = workbook.getSheetAt(0); |
| | | Row firstRow = sheet.getRow(0); |
| | | |
| | | if (firstRow == null) { |
| | | return "未找到标题"; |
| | | } |
| | | |
| | | Cell firstCell = firstRow.getCell(0); |
| | | if (firstCell == null) { |
| | | return ""; |
| | | } |
| | | |
| | | String title = getCellStringValue(firstCell); |
| | | return extractTextBeforeInspection(title); |
| | | |
| | | } catch (Exception e) { |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 提取点检表之前的文字 |
| | | */ |
| | | private String extractTextBeforeInspection(String title) { |
| | | if (StringUtils.isBlank(title)) { |
| | | return ""; |
| | | } |
| | | |
| | | int index = title.indexOf("点检表"); |
| | | return index > 0 ? title.substring(0, index).trim() : title; |
| | | } |
| | | |
| | | /** |
| | | * 提取设备信息 |
| | | */ |
| | | private EamMaintenanceStandard extractDeviceInfo(Sheet sheet) { |
| | | Row headerRow = sheet.getRow(0); |
| | | if (headerRow == null) { |
| | | return null; |
| | | } |
| | | |
| | | String headerText = getCellStringValue(headerRow.getCell(0)); |
| | | if (StringUtils.isBlank(headerText)) { |
| | | return null; |
| | | } |
| | | |
| | | EamMaintenanceStandard standard = new EamMaintenanceStandard(); |
| | | standard.setEquipmentName(extractField(headerText, "设备名称[::]\\s*(\\S+)")); |
| | | standard.setEquipmentCode(extractField(headerText, "统一编号[::]\\s*(\\S+)")); |
| | | |
| | | |
| | | // 日期处理 |
| | | String dateStr = extractField(headerText, "日期[::]\\s*(\\S+)"); |
| | | if (StringUtils.isNotBlank(dateStr)) { |
| | | try { |
| | | // 支持多种日期格式 |
| | | Date date = parseDate(dateStr); |
| | | standard.setDesignTime(date); |
| | | standard.setInitialDate(date); |
| | | } catch (ParseException ignored) { |
| | | |
| | | } |
| | | } |
| | | |
| | | // 关联设备ID |
| | | if (StrUtil.isNotEmpty(standard.getEquipmentCode())) { |
| | | EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode()); |
| | | if (equipments == null) { |
| | | return null; |
| | | }else { |
| | | standard.setEquipmentId(equipments.getId()); |
| | | } |
| | | } |
| | | String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE); |
| | | standard.setStandardCode(codeSeq); |
| | | standard.setMaintenanceCategory("POINT_INSPECTION"); |
| | | standard.setPeriodUnit("天"); |
| | | standard.setStandardStatus(MaintenanceStandardStatusEnum.START.name()); |
| | | standard.setStandardVersion("v" + CommonConstant.OPERATE_TYPE_1); |
| | | standard.setDelFlag(0); |
| | | standard.setMaintenancePeriod(1); |
| | | |
| | | return standard; |
| | | } |
| | | |
| | | /** |
| | | * 解析日期字符串 |
| | | */ |
| | | private Date parseDate(String dateStr) throws ParseException { |
| | | // 尝试多种日期格式 |
| | | String[] patterns = { |
| | | "yyyy年MM月", |
| | | "yyyy-MM", |
| | | "yyyy/MM", |
| | | "yyyyMM", |
| | | "yyyy年M月" |
| | | }; |
| | | |
| | | for (String pattern : patterns) { |
| | | try { |
| | | SimpleDateFormat sdf = new SimpleDateFormat(pattern); |
| | | sdf.setLenient(false); |
| | | return sdf.parse(dateStr); |
| | | } catch (ParseException e) { |
| | | // 尝试下一个格式 |
| | | } |
| | | } |
| | | |
| | | throw new ParseException("无法解析日期: " + dateStr, 0); |
| | | } |
| | | |
| | | /** |
| | | * 提取每日点检项目 |
| | | */ |
| | | private List<EamMaintenanceStandardDetail> extractDailyItems(Sheet sheet, EamMaintenanceStandard standard) { |
| | | return extractItems(sheet, standard, "点检项目", "完成数据/要求", "DAY_INSPECTION"); |
| | | } |
| | | |
| | | /** |
| | | * 提取周保养项目 |
| | | */ |
| | | private List<EamMaintenanceStandardDetail> extractWeeklyItems(Sheet sheet, EamMaintenanceStandard standard) { |
| | | return extractItems(sheet, standard, "周保养项目", "检查标准", "WEEK_INSPECTION"); |
| | | } |
| | | |
| | | /** |
| | | * 通用项目提取方法 |
| | | */ |
| | | private List<EamMaintenanceStandardDetail> extractItems(Sheet sheet, EamMaintenanceStandard standard, |
| | | String primaryHeader, String secondaryHeader, |
| | | String itemCategory) { |
| | | int[] section = findTableSection(sheet, primaryHeader, secondaryHeader); |
| | | if (section == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | List<EamMaintenanceStandardDetail> details = new ArrayList<>(); |
| | | for (int rowIdx = section[0]; rowIdx <= section[1]; rowIdx++) { |
| | | Row row = sheet.getRow(rowIdx); |
| | | if (row == null || isEmptyRow(row)) { |
| | | continue; |
| | | } |
| | | |
| | | // 确保第一列是序号(数字) |
| | | Cell seqCell = row.getCell(0); |
| | | if (seqCell == null || seqCell.getCellType() != CellType.NUMERIC) { |
| | | continue; |
| | | } |
| | | |
| | | // 创建项目详情 |
| | | EamMaintenanceStandardDetail detail = new EamMaintenanceStandardDetail(); |
| | | detail.setStandardId(standard.getId()); |
| | | detail.setItemName(getCellStringValue(row.getCell(1))); |
| | | detail.setItemCategory(itemCategory); |
| | | |
| | | // 根据项目类型设置需求字段 |
| | | if ("DAY_INSPECTION".equals(itemCategory)) { |
| | | detail.setItemDemand(getCellStringValue(row.getCell(2))); |
| | | } else if ("WEEK_INSPECTION".equals(itemCategory)) { |
| | | detail.setItemDemand(getCellStringValue(row.getCell(2))); |
| | | } |
| | | |
| | | details.add(detail); |
| | | } |
| | | |
| | | return details; |
| | | } |
| | | |
| | | /** |
| | | * 查找表格区域 |
| | | */ |
| | | private int[] findTableSection(Sheet sheet, String primaryHeader, String secondaryHeader) { |
| | | for (int rowIdx = 0; rowIdx <= sheet.getLastRowNum(); rowIdx++) { |
| | | Row row = sheet.getRow(rowIdx); |
| | | if (row == null) continue; |
| | | |
| | | if (isHeaderRow(row, primaryHeader, secondaryHeader)) { |
| | | int startRow = rowIdx + 1; |
| | | int endRow = findDataEnd(sheet, startRow); |
| | | return new int[]{startRow, endRow}; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 检查是否为表头行 |
| | | */ |
| | | private boolean isHeaderRow(Row row, String header1, String header2) { |
| | | boolean foundHeader1 = false; |
| | | boolean foundHeader2 = false; |
| | | |
| | | for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) { |
| | | Cell cell = row.getCell(colIdx); |
| | | if (cell == null) continue; |
| | | |
| | | String cellValue = getCellStringValue(cell); |
| | | if (cellValue.contains(header1)) foundHeader1 = true; |
| | | if (cellValue.contains(header2)) foundHeader2 = true; |
| | | } |
| | | |
| | | return foundHeader1 && foundHeader2; |
| | | } |
| | | |
| | | /** |
| | | * 查找数据结束位置 |
| | | */ |
| | | private int findDataEnd(Sheet sheet, int startRow) { |
| | | for (int rowIdx = startRow; rowIdx <= sheet.getLastRowNum(); rowIdx++) { |
| | | Row row = sheet.getRow(rowIdx); |
| | | if (row == null) return rowIdx - 1; |
| | | |
| | | // 检查是否结束标志行(如签字行) |
| | | if (isSignatureRow(row)) { |
| | | return rowIdx - 1; |
| | | } |
| | | |
| | | // 检查是否新的表头开始 |
| | | if (isNewHeaderStart(row)) { |
| | | return rowIdx - 1; |
| | | } |
| | | } |
| | | return sheet.getLastRowNum(); |
| | | } |
| | | |
| | | /** |
| | | * 识别签字行特征 |
| | | */ |
| | | private boolean isSignatureRow(Row row) { |
| | | for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) { |
| | | Cell cell = row.getCell(colIdx); |
| | | if (cell == null) continue; |
| | | |
| | | String value = getCellStringValue(cell); |
| | | if (value.contains("签字") || value.contains("责任人") || |
| | | value.contains("执行") || value.contains("确认")) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 识别新表头开始 |
| | | */ |
| | | private boolean isNewHeaderStart(Row row) { |
| | | for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) { |
| | | Cell cell = row.getCell(colIdx); |
| | | if (cell == null) continue; |
| | | |
| | | String value = getCellStringValue(cell); |
| | | if ("序号".equals(value) || "点检项目".equals(value) || "周保养项目".equals(value)) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 检查行是否为空 |
| | | */ |
| | | private boolean isEmptyRow(Row row) { |
| | | if (row == null) return true; |
| | | for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) { |
| | | Cell cell = row.getCell(colIdx); |
| | | if (cell != null && cell.getCellType() != CellType.BLANK) { |
| | | String value = getCellStringValue(cell); |
| | | if (StringUtils.isNotBlank(value)) { |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 使用正则提取字段 |
| | | */ |
| | | private String extractField(String text, String regex) { |
| | | if (StringUtils.isBlank(text)) return ""; |
| | | Pattern pattern = Pattern.compile(regex); |
| | | Matcher matcher = pattern.matcher(text); |
| | | return matcher.find() ? matcher.group(1) : ""; |
| | | } |
| | | |
| | | /** |
| | | * 获取单元格字符串值 |
| | | */ |
| | | private String getCellStringValue(Cell cell) { |
| | | if (cell == null) return ""; |
| | | switch (cell.getCellType()) { |
| | | case STRING: |
| | | return cell.getStringCellValue().trim(); |
| | | case NUMERIC: |
| | | if (DateUtil.isCellDateFormatted(cell)) { |
| | | return new SimpleDateFormat("yyyy年MM月").format(cell.getDateCellValue()); |
| | | } |
| | | return String.valueOf((int) cell.getNumericCellValue()); |
| | | case BOOLEAN: |
| | | return String.valueOf(cell.getBooleanCellValue()); |
| | | case FORMULA: |
| | | return handleFormulaCell(cell); |
| | | default: |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理公式单元格 |
| | | */ |
| | | private String handleFormulaCell(Cell cell) { |
| | | try { |
| | | FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator(); |
| | | CellValue cellValue = evaluator.evaluate(cell); |
| | | |
| | | if (cellValue == null) return ""; |
| | | |
| | | switch (cellValue.getCellType()) { |
| | | case STRING: return cellValue.getStringValue(); |
| | | case NUMERIC: return String.valueOf((int) cellValue.getNumberValue()); |
| | | case BOOLEAN: return String.valueOf(cellValue.getBooleanValue()); |
| | | default: return ""; |
| | | } |
| | | } catch (Exception e) { |
| | | return ""; |
| | | } |
| | | } |
| | | /*导入点检文件Excel--------------------------结束*/ |
| | | |
| | | /*导入二保三保文件Excel--------------------------开始*/ |
| | | /** |
| | | * 统一保养规范导入入口 |
| | | * @param file 上传文件 |
| | | * @param type 保养类型 (SECOND/THIRD) |
| | | * @return 导入结果(包含成功/失败信息) |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public Result<?> importMaintenanceStandard(MultipartFile file, String type) { |
| | | String fileName = file.getOriginalFilename(); |
| | | |
| | | try (XWPFDocument doc = new XWPFDocument(file.getInputStream())) { |
| | | // 基础验证 |
| | | if (doc.getTables().isEmpty()) { |
| | | return Result.error(fileName + ": 文档中未找到表格"); |
| | | } |
| | | |
| | | List<XWPFTable> tables = doc.getTables(); |
| | | EamMaintenanceStandard standard = null; |
| | | boolean firstTableProcessed = false; |
| | | List<EamMaintenanceStandardDetail> items = new ArrayList<>(); |
| | | String standardId = null; |
| | | |
| | | // 1. 处理所有表格 |
| | | for (int i = 0; i < tables.size(); i++) { |
| | | XWPFTable table = tables.get(i); |
| | | |
| | | if (i == 0) { // 第一页表格 |
| | | // 验证设备信息表格 |
| | | if (isWrongDocumentType(table, type)) { |
| | | return Result.error(fileName + ": 文档类型不匹配 - " + |
| | | ("SECOND".equals(type) ? "请导入二级保养文档" : "请导入三级保养文档")); |
| | | } |
| | | |
| | | // 提取设备信息 |
| | | standard = extractDeviceInfo(table); |
| | | if (standard == null) { |
| | | return Result.error(fileName + ": 设备信息提取失败"); |
| | | } |
| | | |
| | | // 配置类型相关参数 |
| | | configureStandard(standard, type, file); |
| | | eamMaintenanceStandardMapper.insert(standard); |
| | | standardId = standard.getId(); |
| | | |
| | | // 提取第一页的保养项目 |
| | | if ("SECOND".equals(type)) { |
| | | items.addAll(extractSecondMaintenanceItems(table, standardId, true)); |
| | | } else if ("THIRD".equals(type)) { |
| | | items.addAll(extractThirdMaintenanceItems(table, standardId, true)); |
| | | } |
| | | |
| | | firstTableProcessed = true; |
| | | } else if (firstTableProcessed) { // 后续页面 |
| | | // 提取后续页面的保养项目 |
| | | if ("SECOND".equals(type)) { |
| | | items.addAll(extractSecondMaintenanceItems(table, standardId, false)); |
| | | } else if ("THIRD".equals(type)) { |
| | | items.addAll(extractThirdMaintenanceItems(table, standardId, false)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 验证设备信息提取 |
| | | if (standard == null) { |
| | | return Result.error(fileName + ": 设备信息提取失败"); |
| | | } |
| | | |
| | | // 2. 后处理:根据不同类型进行处理 |
| | | processItemsAfterExtraction(items, type); |
| | | |
| | | // 3. 项目验证 |
| | | if (items.isEmpty()) { |
| | | return Result.error(fileName + ": 未提取到任何保养项目"); |
| | | } |
| | | |
| | | // 4. 保存项目 |
| | | eamMaintenanceStandardDetailService.saveBatch(items); |
| | | |
| | | return Result.ok(fileName + ": 导入成功, 项目数: " + items.size()); |
| | | |
| | | } catch (ImportException e) { |
| | | return Result.error(e.getMessage()); |
| | | } catch (Exception e) { |
| | | return Result.error(fileName + ": 系统错误 - " + e.getClass().getSimpleName()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 后处理方法:根据不同类型进行处理 |
| | | */ |
| | | private void processItemsAfterExtraction(List<EamMaintenanceStandardDetail> items, String type) { |
| | | if ("SECOND".equals(type)) { |
| | | // 二级保养: 删除没有序号的数据 |
| | | items.removeIf(item -> item.getItemCode() == null); |
| | | } else { |
| | | // 三级保养: |
| | | // 1. 删除第一条数据(通常是标题行) |
| | | if (!items.isEmpty()) { |
| | | items.remove(0); |
| | | } |
| | | |
| | | // 2. 为缺失部位的数据填充前一条的保养部位 |
| | | String lastPart = ""; |
| | | for (EamMaintenanceStandardDetail item : items) { |
| | | if (item.getItemPart() != null && !item.getItemPart().isEmpty()) { |
| | | lastPart = item.getItemPart(); |
| | | } else if (!lastPart.isEmpty()) { |
| | | item.setItemPart(lastPart); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /** |
| | | * 提取二级保养项目(区分第一页和后续页面) |
| | | */ |
| | | private List<EamMaintenanceStandardDetail> extractSecondMaintenanceItems( |
| | | XWPFTable table, String standardId, boolean isFirstTable) { |
| | | |
| | | List<EamMaintenanceStandardDetail> items = new ArrayList<>(); |
| | | String currentCategory = null; |
| | | int startRow = 0; |
| | | |
| | | // 对于第一页表格,跳过前两行(设备信息行) |
| | | if (isFirstTable && table.getNumberOfRows() > 2) { |
| | | startRow = 2; // 从第三行开始是保养内容 |
| | | } |
| | | |
| | | for (int i = startRow; i < table.getNumberOfRows(); i++) { |
| | | XWPFTableRow row = table.getRow(i); |
| | | if (row == null) continue; |
| | | |
| | | // 检查是否是标题行(维修人员保养内容或操作人员保养内容) |
| | | String firstCell = getCellText(row.getCell(0)); |
| | | // 处理空行后的部位继承 |
| | | if (firstCell.contains(REPAIR_TITLE)) { |
| | | currentCategory = "REPAIRER_MAINTENANCE"; |
| | | } else if (firstCell.contains(OPERATOR_TITLE)) { |
| | | currentCategory = "OPERATOR_MAINTENANCE"; |
| | | } |
| | | |
| | | // 处理普通项目行 |
| | | if (currentCategory != null && isValidItemRow(row)) { |
| | | EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail(); |
| | | item.setItemCategory(currentCategory); |
| | | item.setStandardId(standardId); |
| | | |
| | | // 提取序号(第二列) |
| | | if (row.getTableCells().size() > 1) { |
| | | String seqText = getCellText(row.getCell(1)); |
| | | try { |
| | | if (!seqText.equals("序号")){ |
| | | item.setItemCode(Integer.parseInt(seqText.trim())); |
| | | } |
| | | } catch (NumberFormatException e) { |
| | | // 忽略序号解析错误 |
| | | } |
| | | } |
| | | |
| | | // 提取内容(第三列) |
| | | if (row.getTableCells().size() > 2) { |
| | | String seqText = getCellText(row.getCell(2)); |
| | | item.setItemName(seqText); |
| | | } |
| | | |
| | | items.add(item); |
| | | } |
| | | } |
| | | return items; |
| | | } |
| | | |
| | | /** |
| | | * 提取三级保养项目(解决跨页空行问题) |
| | | */ |
| | | private List<EamMaintenanceStandardDetail> extractThirdMaintenanceItems( |
| | | XWPFTable table, String standardId, boolean isFirstTable) { |
| | | |
| | | List<EamMaintenanceStandardDetail> items = new ArrayList<>(); |
| | | String currentPart = ""; |
| | | int startRow = 0; |
| | | |
| | | // 对于第一页表格,跳过前两行(设备信息行) |
| | | if (isFirstTable && table.getNumberOfRows() > 2) { |
| | | startRow = 2; // 从第三行开始是保养内容 |
| | | } |
| | | |
| | | for (int i = startRow; i < table.getNumberOfRows(); i++) { |
| | | XWPFTableRow row = table.getRow(i); |
| | | if (row == null) continue; // 确保行对象不为空 |
| | | |
| | | // 检查是否是空行(包含所有单元格都为空的情况) |
| | | if (isRowEmpty(row)) { |
| | | // 空行处理:保留当前位置但不创建项目 |
| | | continue; |
| | | } |
| | | |
| | | // 创建保养项目 |
| | | EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail(); |
| | | item.setItemCategory("THIRD_MAINTENANCE"); |
| | | item.setStandardId(standardId); |
| | | int colCount = row.getTableCells().size(); |
| | | |
| | | // 处理部位列(第一列) |
| | | if (colCount > 0) { |
| | | String firstCell = getCellText(row.getCell(0)).trim(); |
| | | |
| | | // 关键改进:正确处理空行后的部位继承 |
| | | if (!firstCell.isEmpty() && !firstCell.equals("保养部位")) { |
| | | // 更新当前部位 |
| | | currentPart = firstCell; |
| | | } |
| | | item.setItemPart(currentPart); |
| | | } else { |
| | | // 如果没有单元格,使用当前部位 |
| | | item.setItemPart(currentPart); |
| | | } |
| | | |
| | | // 根据列数提取内容(考虑合并单元格情况) |
| | | List<String> cellContents = new ArrayList<>(); |
| | | for (int j = 0; j < colCount; j++) { |
| | | XWPFTableCell cell = row.getCell(j); |
| | | String text = getCellText(cell).trim(); |
| | | |
| | | // 特殊处理:第二页第一行可能是空行后的内容 |
| | | if (j == 0 && !text.isEmpty() && !text.equals(currentPart)) { |
| | | // 如果不是部位列,则添加为内容 |
| | | cellContents.add(text); |
| | | } else if (j > 0) { |
| | | // 其他列作为内容 |
| | | cellContents.add(text); |
| | | } |
| | | } |
| | | |
| | | // 智能解析单元格内容 |
| | | if (cellContents.size() >= 2) { |
| | | // 默认处理方式:最后两个作为内容和标准 |
| | | item.setItemName(cellContents.get(cellContents.size() - 2)); |
| | | item.setItemDemand(cellContents.get(cellContents.size() - 1)); |
| | | } else if (cellContents.size() == 1) { |
| | | // 单列模式:视为内容 |
| | | item.setItemName(cellContents.get(0)); |
| | | } else if (cellContents.isEmpty() && !isRowEmpty(row)) { |
| | | // 特殊处理:行非空但没有提取到内容(可能是复杂合并单元格) |
| | | // 尝试提取整行文本作为内容 |
| | | StringBuilder content = new StringBuilder(); |
| | | for (XWPFTableCell cell : row.getTableCells()) { |
| | | content.append(getCellText(cell).trim()).append(" "); |
| | | } |
| | | item.setItemName(content.toString().trim()); |
| | | } |
| | | |
| | | items.add(item); |
| | | } |
| | | return items; |
| | | } |
| | | |
| | | /** |
| | | * 优化后的空行检测(解决跨页空行问题) |
| | | */ |
| | | private boolean isRowEmpty(XWPFTableRow row) { |
| | | if (row == null || row.getTableCells().isEmpty()) { |
| | | return true; |
| | | } |
| | | |
| | | boolean allCellsEmpty = true; |
| | | for (XWPFTableCell cell : row.getTableCells()) { |
| | | String text = getCellText(cell).trim(); |
| | | // 保留包含换行符等的单元格作为非空行 |
| | | if (!text.isEmpty() && !text.replaceAll("\\s+", "").isEmpty()) { |
| | | allCellsEmpty = false; |
| | | break; |
| | | } |
| | | } |
| | | return allCellsEmpty; |
| | | } |
| | | |
| | | /** |
| | | * 文档类型校验 - 防止二保传入三保或反之 |
| | | */ |
| | | private boolean isWrongDocumentType(XWPFTable table, String requestedType) { |
| | | boolean hasRepairTitle = false; |
| | | boolean hasOperatorTitle = false; |
| | | boolean hasMaintenancePart = false; |
| | | |
| | | // 只检查前10行(通常标题在前几行) |
| | | int maxRows = Math.min(table.getNumberOfRows(), 10); |
| | | |
| | | for (int i = 0; i < maxRows; i++) { |
| | | XWPFTableRow row = table.getRow(i); |
| | | if (row == null) continue; |
| | | |
| | | // 检查所有单元格内容 |
| | | for (int j = 0; j < row.getTableCells().size(); j++) { |
| | | String cellText = getCellText(row.getCell(j)); |
| | | |
| | | // 检查二级保养特征 |
| | | if (cellText.contains(REPAIR_TITLE)) { |
| | | hasRepairTitle = true; |
| | | } |
| | | if (cellText.contains(OPERATOR_TITLE)) { |
| | | hasOperatorTitle = true; |
| | | } |
| | | |
| | | // 检查三级保养特征 |
| | | if (cellText.contains("保养部位")) { |
| | | hasMaintenancePart = true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 逻辑判断 |
| | | if ("SECOND".equals(requestedType)) { |
| | | // 如果请求导入二级保养,但文档中有三级保养特征 |
| | | return !(hasRepairTitle || hasOperatorTitle) || hasMaintenancePart; |
| | | } else if ("THIRD".equals(requestedType)) { |
| | | // 如果请求导入三级保养,但文档中有二级保养特征 |
| | | return !hasMaintenancePart || (hasRepairTitle || hasOperatorTitle); |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 提取设备基本信息 |
| | | */ |
| | | private EamMaintenanceStandard extractDeviceInfo(XWPFTable table) { |
| | | if (table.getNumberOfRows() < 2) return null; |
| | | |
| | | // 提取前两行数据 |
| | | Map<String, String> row1Data = extractRowData(table.getRow(0)); |
| | | Map<String, String> row2Data = extractRowData(table.getRow(1)); |
| | | |
| | | // 创建设备标准对象 |
| | | EamMaintenanceStandard standard = new EamMaintenanceStandard(); |
| | | standard.setEquipmentText(row1Data.get("设备类别")); |
| | | standard.setEquipmentCode(row1Data.get("设备编号")); |
| | | standard.setEquipmentName(row2Data.get("设备名称")); |
| | | standard.setEquipmentModel(row2Data.get("设备型号")); |
| | | |
| | | // 关联设备ID |
| | | if (StrUtil.isNotEmpty(standard.getEquipmentCode())) { |
| | | EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode()); |
| | | if (equipments == null) { |
| | | return null; |
| | | }else { |
| | | standard.setEquipmentId(equipments.getId()); |
| | | } |
| | | } |
| | | |
| | | return standard; |
| | | } |
| | | |
| | | // 简易版字符串非空判断 |
| | | private boolean isNotBlank(String str) { |
| | | return str != null && !str.trim().isEmpty(); |
| | | } |
| | | |
| | | /** |
| | | * 表格行数据解析 |
| | | */ |
| | | private Map<String, String> extractRowData(XWPFTableRow row) { |
| | | Map<String, String> data = new HashMap<>(); |
| | | int cellCount = row.getTableCells().size(); |
| | | |
| | | try { |
| | | // 键值对模式 (标签|值|标签|值) |
| | | if (cellCount >= 4 && cellCount % 2 == 0) { |
| | | for (int i = 0; i < cellCount; i += 2) { |
| | | String key = cleanKey(getCellText(row.getCell(i))); |
| | | String value = getCellText(row.getCell(i + 1)); |
| | | if (!key.isEmpty()) data.put(key, value); |
| | | } |
| | | } |
| | | // 连续单元格模式 |
| | | else { |
| | | for (int i = 0; i < cellCount; i++) { |
| | | String text = getCellText(row.getCell(i)); |
| | | int colonIndex = text.indexOf(':'); |
| | | if (colonIndex > 0) { |
| | | String key = cleanKey(text.substring(0, colonIndex)); |
| | | String value = text.substring(colonIndex + 1); |
| | | data.put(key, value); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("行数据解析异常", e); |
| | | } |
| | | return data; |
| | | } |
| | | |
| | | /** |
| | | * 键名标准化处理 |
| | | */ |
| | | private String cleanKey(String key) { |
| | | if (key == null) return ""; |
| | | // 移除空格和中文冒号 |
| | | return key.replaceAll("\\s", "").replace(":", ""); |
| | | } |
| | | |
| | | /** |
| | | * 配置保养标准参数 |
| | | */ |
| | | private void configureStandard(EamMaintenanceStandard standard, String type, MultipartFile file) { |
| | | // 基础参数 |
| | | String filename = file.getOriginalFilename(); |
| | | if (filename != null && filename.contains(".")) { |
| | | filename = filename.substring(0, filename.lastIndexOf('.')); |
| | | } |
| | | |
| | | standard.setStandardName(filename) |
| | | .setInitialDate(new Date()) |
| | | .setStandardStatus(MaintenanceStandardStatusEnum.START.name()) |
| | | .setStandardVersion("v" + CommonConstant.OPERATE_TYPE_1) |
| | | .setDelFlag(0) |
| | | .setStandardCode(businessCodeRuleService.generateBusinessCodeSeq( |
| | | BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE |
| | | )); |
| | | |
| | | // 类型特定参数 |
| | | if ("SECOND".equals(type)) { |
| | | standard.setMaintenanceCategory("SECOND_MAINTENANCE") |
| | | .setMaintenancePeriod(6) |
| | | .setPeriodUnit("月"); |
| | | } else { |
| | | standard.setMaintenanceCategory("THIRD_MAINTENANCE") |
| | | .setPeriodUnit("年"); |
| | | |
| | | // 获取三级保养周期 |
| | | if (standard.getEquipmentId() != null) { |
| | | EamEquipmentExtend extend = eamEquipmentExtendService.getById(standard.getEquipmentId()); |
| | | if (extend != null) { |
| | | standard.setMaintenancePeriod(extend.getThirdMaintenancePeriod()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建保养项目 |
| | | */ |
| | | private EamMaintenanceStandardDetail createItem( |
| | | EamMaintenanceStandardDetailCategory category, |
| | | String content, |
| | | String standardId) { |
| | | |
| | | EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail(); |
| | | item.setItemCategory(category.name()); |
| | | item.setStandardId(standardId); |
| | | item.setItemName(cleanContent(content)); |
| | | return item; |
| | | } |
| | | |
| | | /** |
| | | * 兼容版单元格文本提取 |
| | | */ |
| | | private String getCellText(XWPFTableCell cell) { |
| | | if (cell == null) return ""; |
| | | |
| | | StringBuilder text = new StringBuilder(); |
| | | for (XWPFParagraph para : cell.getParagraphs()) { |
| | | if (para != null) { |
| | | for (XWPFRun run : para.getRuns()) { |
| | | if (run != null) { |
| | | String runText = run.getText(0); |
| | | if (runText != null) { |
| | | text.append(runText); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return text.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 内容清理 |
| | | */ |
| | | private String cleanContent(String text) { |
| | | if (text == null) return ""; |
| | | |
| | | // 替换特殊空格和合并连续空格 |
| | | text = text.replace('\u00A0', ' ') |
| | | .replace('\u2007', ' ') |
| | | .replace('\u202F', ' ') |
| | | .replaceAll("\\s+", " "); |
| | | |
| | | // 规范标点符号 |
| | | return text.replace(',', '、') |
| | | .replace(',', '、') |
| | | .replace(';', ';') |
| | | .replace(';', ';') |
| | | .replace(':', ':') |
| | | .replace(':', ':') |
| | | .trim(); |
| | | } |
| | | |
| | | /** |
| | | * 验证有效项目行 |
| | | */ |
| | | private boolean isValidItemRow(XWPFTableRow row) { |
| | | return row != null && |
| | | row.getTableCells().size() >= 2 && |
| | | !getCellText(row.getCell(1)).trim().isEmpty(); |
| | | } |
| | | |
| | | /*导入二保三保文件Excel--------------------------结束*/ |
| | | |
| | | } |