¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.modules.eam.job; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.util.DateUtils; |
| | | import org.jeecg.modules.eam.constant.BusinessCodeConst; |
| | | import org.jeecg.modules.eam.constant.MaintenanceCategoryEnum; |
| | | import org.jeecg.modules.eam.constant.OrderCreationMethodEnum; |
| | | import org.jeecg.modules.eam.entity.EamEquipment; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandard; |
| | | import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail; |
| | | import org.jeecg.modules.eam.entity.EamSecondMaintenanceOrderDetail; |
| | | import org.jeecg.modules.eam.request.EamSecondMaintenanceRequest; |
| | | import org.jeecg.modules.eam.service.*; |
| | | import org.jeecg.modules.quartz.entity.QuartzJob; |
| | | import org.jeecg.modules.quartz.entity.SysQuartzLog; |
| | | import org.jeecg.modules.quartz.service.IQuartzJobService; |
| | | import org.jeecg.modules.quartz.service.ISysQuartzLogService; |
| | | import org.jeecg.modules.system.service.ISysBusinessCodeRuleService; |
| | | import org.quartz.Job; |
| | | import org.quartz.JobExecutionContext; |
| | | import org.quartz.JobExecutionException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Author: Lyh |
| | | * @CreateTime: 2025-09-01 |
| | | * @Description: äºä¿å·¥åçæ |
| | | */ |
| | | @Component |
| | | @Slf4j |
| | | public class SecondMaintenanceOrderGenerateJob implements Job { |
| | | |
| | | @Autowired |
| | | private IEamEquipmentService equipmentService; |
| | | @Autowired |
| | | private IEamSecondMaintenanceOrderService eamSecondMaintenanceOrderService; |
| | | @Autowired |
| | | private IEamMaintenanceStandardService eamMaintenanceStandardService; |
| | | @Autowired |
| | | private IEamMaintenanceStandardDetailService eamMaintenanceStandardDetailService; |
| | | @Autowired |
| | | private ISysBusinessCodeRuleService businessCodeRuleService; |
| | | @Autowired |
| | | private ISysQuartzLogService sysQuartzLogService; |
| | | @Autowired |
| | | private IQuartzJobService quartzJobService; |
| | | @Autowired |
| | | private IEamEquipmentExtendService equipmentExtendService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { |
| | | // 任塿¥å¿åå§å |
| | | SysQuartzLog quartzLog = new SysQuartzLog(); |
| | | quartzLog.setCreateTime(new Date()); |
| | | List<QuartzJob> byJobClassName = quartzJobService.findByJobClassName(this.getClass().getName()); |
| | | if (byJobClassName != null && !byJobClassName.isEmpty()) { |
| | | quartzLog.setJobId(byJobClassName.get(0).getId()); |
| | | } |
| | | long startTime = System.currentTimeMillis(); |
| | | // è·åéè¦äºä¿ç设å¤å表 |
| | | List<EamEquipment> equipmentList = equipmentService.selectSecondMaintenanceEquipmentList(); |
| | | if (CollectionUtil.isEmpty(equipmentList)) { |
| | | log.warn("没æéè¦çæäºä¿å·¥åç设å¤ï¼æ¥æï¼{}", DateUtils.date2Str(DateUtils.date_sdf.get())); |
| | | quartzLog.setIsSuccess(-1); |
| | | quartzLog.setExceptionDetail("没æéè¦çæäºä¿å·¥åç设å¤"); |
| | | saveQuartzLog(quartzLog, startTime); |
| | | return; |
| | | } |
| | | |
| | | AtomicInteger successCount = new AtomicInteger(0); |
| | | AtomicInteger failCount = new AtomicInteger(0); |
| | | StringBuilder sb = new StringBuilder(); |
| | | |
| | | // éå设å¤çæå·¥å |
| | | for (EamEquipment equipment : equipmentList) { |
| | | generateSecondMaintenanceOrder( |
| | | equipment, |
| | | successCount, |
| | | failCount, |
| | | sb |
| | | ); |
| | | } |
| | | |
| | | // ä¿å任塿¥å¿ |
| | | quartzLog.setIsSuccess(failCount.get() > 0 ? -1 : 0); |
| | | sb.insert(0, "äºä¿å·¥åçæç»æï¼"); |
| | | sb.append("\næåï¼").append(successCount.get()).append("ï¼å¤±è´¥ï¼").append(failCount.get()); |
| | | quartzLog.setExceptionDetail(sb.toString()); |
| | | saveQuartzLog(quartzLog, startTime); |
| | | } |
| | | |
| | | private void saveQuartzLog(SysQuartzLog quartzLog, long startTime) { |
| | | long endTime = System.currentTimeMillis(); |
| | | quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime))); |
| | | sysQuartzLogService.save(quartzLog); |
| | | } |
| | | |
| | | private void generateSecondMaintenanceOrder(EamEquipment equipment, AtomicInteger successCount, AtomicInteger failCount, StringBuilder sb) { |
| | | Date nextMaintenanceDate=equipment.getNextSecondMaintenance(); |
| | | //æ£æ¥æ¯å¦æ»¡è¶³çææ¡ä»¶ï¼æåä¸å¤©çæ |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(nextMaintenanceDate); |
| | | cal.add(Calendar.DAY_OF_MONTH, -1); |
| | | Date targetDate = cal.getTime(); |
| | | |
| | | // è·åå½åæ¥æï¼ä¸å«æ¶é´é¨åï¼ |
| | | Calendar today = Calendar.getInstance(); |
| | | today.set(Calendar.HOUR_OF_DAY, 0); |
| | | today.set(Calendar.MINUTE, 0); |
| | | today.set(Calendar.SECOND, 0); |
| | | today.set(Calendar.MILLISECOND, 0); |
| | | // æ¯è¾æ¥ææ¯å¦å¹é
|
| | | if (targetDate.equals(today.getTime())) { |
| | | // çæäºä¿å·¥å |
| | | EamMaintenanceStandard standard = eamMaintenanceStandardService.queryByEquipmentIdAndCategory( |
| | | equipment.getId(), |
| | | MaintenanceCategoryEnum.SECOND_MAINTENANCE.name() |
| | | ); |
| | | |
| | | if (standard == null) { |
| | | handleFailure(equipment, "æªé
ç½®äºçº§ä¿å
»è§è", failCount, sb); |
| | | return; |
| | | } |
| | | |
| | | // è·åè§èæç» |
| | | List<EamMaintenanceStandardDetail> details = eamMaintenanceStandardDetailService.selectByStandardId(standard.getId()); |
| | | if (CollectionUtil.isEmpty(details)) { |
| | | handleFailure(equipment, "æªé
ç½®äºçº§ä¿å
»è§èæç»", failCount, sb); |
| | | return; |
| | | } |
| | | |
| | | // æå»ºå·¥åè¯·æ± |
| | | EamSecondMaintenanceRequest request = new EamSecondMaintenanceRequest(); |
| | | request.setOrderNum(businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.SECOND_MAINTENANCE_CODE_RULE)); |
| | | request.setEquipmentId(equipment.getId()); |
| | | request.setStandardId(standard.getId()); |
| | | request.setMaintenanceDate(nextMaintenanceDate); |
| | | request.setCreationMethod(OrderCreationMethodEnum.AUTO.name()); |
| | | |
| | | // 转æ¢è§èæç» |
| | | List<EamSecondMaintenanceOrderDetail> orderDetails = details.stream() |
| | | .map(EamSecondMaintenanceOrderDetail::new) |
| | | .collect(Collectors.toList()); |
| | | request.setTableDetailList(orderDetails); |
| | | |
| | | try { |
| | | // å建工å |
| | | eamSecondMaintenanceOrderService.addMaintenance(request); |
| | | // æ´æ°è®¾å¤ç¶æ |
| | | equipmentExtendService.updateSecondMaintenanceParam( |
| | | equipment.getId(), |
| | | null, |
| | | CommonConstant.STATUS_1 |
| | | ); |
| | | successCount.incrementAndGet(); |
| | | log.info("è®¾å¤ {} äºä¿å·¥åçææå", equipment.getEquipmentCode()); |
| | | } catch (Exception e) { |
| | | handleFailure(equipment, "çæå¤±è´¥: " + e.getMessage(), failCount, sb); |
| | | log.error("è®¾å¤ {} äºä¿å·¥åçæå¼å¸¸", equipment.getEquipmentCode(), e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void handleFailure(EamEquipment equipment, String reason, AtomicInteger failCount, StringBuilder sb) { |
| | | failCount.incrementAndGet(); |
| | | sb.append("\nè®¾å¤ ") |
| | | .append(equipment.getEquipmentCode()) |
| | | .append("ï¼") |
| | | .append(reason); |
| | | log.warn("è®¾å¤ {} äºä¿å·¥åçæå¤±è´¥ï¼{}", equipment.getEquipmentCode(), reason); |
| | | } |
| | | |
| | | |
| | | } |