package org.jeecg.modules.mes.job; import lombok.extern.slf4j.Slf4j; import org.jeecg.modules.lsw.service.ILswMaterialService; import org.jeecg.modules.mdc.util.DateUtils; import org.jeecg.modules.mdc.util.ThrowableUtil; import org.jeecg.modules.mes.entity.MesProductionOrder; import org.jeecg.modules.mes.service.IMesProductionOrderService; import org.jeecg.modules.pms.service.IPmsMaterialProcessService; import org.jeecg.modules.pms.service.IPmsProcessBillMaterialsService; 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.sap.dto.OrderBomDTO; import org.jeecg.modules.sap.dto.OrderProcessDTO; import org.jeecg.modules.sap.dto.ProductionOrderDTO; import org.jeecg.modules.sap.request.ProductionOrderSyncRequest; import org.jeecg.modules.sap.service.OrderBomSync; import org.jeecg.modules.sap.service.OrderProcessSync; import org.jeecg.modules.sap.service.ProductionOrderSync; 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 java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.Map; @Component @Slf4j public class ProductionOrderSyncUpdateJob implements Job { //工厂编码(新火炬 2301) private static final String FACTORY_CODE = "2301"; /** * 订单类型 * 标准生产订单 Z001 * 客退返工生产订单 Z002 * 呆滞处理返工生产订单 Z003 * 试制生产订单(新火炬) Z011 * 拆零生产订单(新火炬) 2012 */ private static final String ORDER_TYPE_CODE = "Z001"; /** * 生产调度员 * 001 一分厂调度员 * 002 二分厂调度员 * 003 三分厂调度员 * 004 四分厂调度员 * 005 ABS调度员 * 006 调质调度员 * 007 五分厂调度员 * 008 试制车间调度员 * 009 外采调度员 * 010 六厂调度员 * 012 八分厂调度员 */ private static final String PRODUCTION_MANAGER = "010"; /** * 生产订单状态,实际上,只有REL状态的工单才可以进行操作 * CRTD 新建 * REL 下达 * TECO 关闭 */ private static final String ORDER_STATUS = "REL"; /** * 请求成功编码 */ private static final String SUCCESS_CODE = "S"; @Autowired private ProductionOrderSync productionOrderSync; @Autowired private OrderBomSync orderBomSync; @Autowired private OrderProcessSync orderProcessSync; @Autowired private IMesProductionOrderService productionOrderService; @Autowired private IPmsProcessBillMaterialsService processBillMaterialsService; @Autowired private IPmsMaterialProcessService materialProcessService; @Autowired private ISysQuartzLogService sysQuartzLogService; @Autowired private IQuartzJobService quartzJobService; @Autowired private ILswMaterialService lswMaterialService; @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { //任务日志 SysQuartzLog quartzLog = new SysQuartzLog(); quartzLog.setCreateTime(new Date()); List byJobClassName = quartzJobService.findByJobClassName(this.getClass().getName()); if (byJobClassName != null && !byJobClassName.isEmpty()) { quartzLog.setJobId(byJobClassName.get(0).getId()); } long startTime = System.currentTimeMillis(); //获取上次同步时间 String lastSyncDateLow = productionOrderService.getLastSyncUpdateDate(); String lastSyncDateHigh = null; if (lastSyncDateLow != null) { lastSyncDateHigh = DateUtils.format(new Date(), "yyyyMMdd"); if (lastSyncDateLow.equals(lastSyncDateHigh)) { lastSyncDateHigh = null; } } ProductionOrderSyncRequest request = new ProductionOrderSyncRequest(); request.setFactoryCode(FACTORY_CODE); request.setOrderTypeCode(ORDER_TYPE_CODE); request.setProductionManager(PRODUCTION_MANAGER); request.setOrderStatus(ORDER_STATUS); request.setUpdateTimeLow(lastSyncDateLow); request.setUpdateTimeHigh(lastSyncDateHigh); try { //调用SAP接口获取生产订单 Map productionOrderMap = productionOrderSync.syncProductionOrder(request); if (productionOrderMap == null || !SUCCESS_CODE.equals(productionOrderMap.get("ztype"))) { log.error("未同步到订单信息!日期:{}", LocalDateTime.now()); return; } //调用成功,获取返回数据 Object result = productionOrderMap.get("result"); boolean b = result instanceof List; if (!b) { log.error("返回类型错误, class:{}", result == null ? null : result.getClass()); return; } List productionOrderDTOList = (List) result; Map orderMap = productionOrderService.saveOrUpdateProductionOrder(productionOrderDTOList); String orderCodes = String.join(",", orderMap.keySet()); //订单BOM同步 Map orderBomMap = orderBomSync.syncOrderBom(FACTORY_CODE, orderCodes); if (orderBomMap == null || !SUCCESS_CODE.equals(orderBomMap.get("ztype"))) { log.error("未同步到订单BOM信息!日期:{}", LocalDateTime.now()); return; } //调用成功,获取返回数据 result = orderBomMap.get("result"); b = result instanceof List; if (!b) { log.error("返回类型错误, class:{}", result == null ? null : result.getClass()); return; } List orderBomDTOList = (List) result; b = processBillMaterialsService.saveOrUpdateOrderBom(orderMap, orderBomDTOList); if (!b) { log.error("保存订单BOM失败,日期:{}", LocalDateTime.now()); return; } //物料数据处理 b = lswMaterialService.saveOrUpdateMaterial(orderMap, orderBomDTOList); if (!b) { log.error("保存物料失败,日期:{}", LocalDateTime.now()); return; } //订单工序同步 Map orderProcessMap = orderProcessSync.syncOrderProcess(FACTORY_CODE, orderCodes); if (orderBomMap == null || !SUCCESS_CODE.equals(orderProcessMap.get("ztype"))) { log.error("未同步到订单工序信息!日期:{}", LocalDateTime.now()); return; } //调用成功,获取返回数据 result = orderProcessMap.get("result"); b = result instanceof List; if (!b) { log.error("返回类型错误, class:{}", result == null ? null : result.getClass()); return; } List orderProcessDTOList = (List) result; b = materialProcessService.saveOrUpdateOrderProcess(orderMap, orderProcessDTOList); if (!b) { log.error("保存订单工序失败,日期:{}", LocalDateTime.now()); } quartzLog.setIsSuccess(0); } catch (Exception e) { log.error("定时任务失败,{}", e.getMessage(), e); quartzLog.setIsSuccess(-1); quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e)); } long endTime = System.currentTimeMillis(); quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime))); sysQuartzLogService.save(quartzLog); } }