sfd
2025-05-26 2a64b537e8e3bce9ce030585a3da17d48379c0ad
aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlanTaskServiceImpl.java
@@ -1,7 +1,6 @@
package com.aps.core.service.impl;
import cn.hutool.core.util.IdUtil;
import com.aps.common.core.domain.R;
import com.aps.common.core.utils.DateUtils;
import com.aps.common.core.web.domain.AjaxResult;
import com.aps.common.redis.service.RedisLockUtils;
@@ -10,9 +9,9 @@
import com.aps.core.enums.PLAN_TASK_TYPE;
import com.aps.core.enums.REDIS_LOCK_KEY;
import com.aps.core.mapper.ApsPlateStandardRequireOrderEndDayMapper;
import com.aps.core.service.ApsPlate.IApsPlateProcessShopStatService;
import com.aps.core.service.ApsPlate.IApsPlateStandardRequireBatchService;
import com.aps.core.service.ApsPlate.IApsPlateStandardRequireService;
import com.aps.core.service.ApsPlateStandardRequireOrderEndDayService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -26,6 +25,7 @@
import static com.aps.common.core.web.domain.AjaxResult.success;
import static com.aps.core.enums.REDIS_LOCK_KEY.PLATE_ORDER_PLAN;
/**
* @author zhl
@@ -50,6 +50,9 @@
    @Resource
    ApsPlateStandardRequireOrderEndDayMapper  orderEndDayMapper;
    @Resource
    IApsPlateProcessShopStatService plateProcessShopStatService;
    @Override
    public Page<ApsPlanTask> pagingList(Page<ApsPlanTask> page ,ApsPlanTask task){
@@ -90,36 +93,69 @@
    }
    @Override
    public AjaxResult generatorPlan()
    {
    public AjaxResult generatorPlan() {
        String plateOrderPlanKey = PLATE_ORDER_PLAN.getKey();
        boolean existsLock = redisLockUtils.existLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_PLAN.getCode());
        if (existsLock){
        if (existsLock) {
            return AjaxResult.warn("钣金工单计划任务正在执行中!");
        }
        LambdaQueryWrapper<ApsPlanTask> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ApsPlanTask::getTaskType,PLAN_TASK_TYPE.PLATE_PLAN.getCode());
        queryWrapper.eq(ApsPlanTask::getTaskStatus,PLAN_TASK_STATUS.IN_PROCESS.getCode());
        LambdaQueryWrapper<ApsPlanTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApsPlanTask::getTaskType, PLAN_TASK_TYPE.PLATE_PLAN.getCode());
        queryWrapper.eq(ApsPlanTask::getTaskStatus, PLAN_TASK_STATUS.IN_PROCESS.getCode());
        boolean existsDbTask = mapper.exists(queryWrapper);
        if (existsDbTask){
        if (existsDbTask) {
            return AjaxResult.warn("钣金工单计划任务正在执行中!!");
        }
        String currentBatchNum = requireBatchService.getNewBatchNumber();
        try {
            redisLockUtils.getLock(plateOrderPlanKey,PLAN_TASK_TYPE.PLATE_PLAN.getCode(), 3*60L);
            String batchNum= requireBatchService.getNewBatchNumber();
            this.savePlanTask(batchNum);
            apsPlateStandardRequireService.generatorPlan(batchNum);
            this.updateTaskStatus(batchNum, PLAN_TASK_STATUS.FINISHED);
            redisLockUtils.getLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_PLAN.getCode(), 15 * 60L);
            this.savePlanTask(currentBatchNum);
            //Thread.sleep(60*1000);
            apsPlateStandardRequireService.generatorPlan(currentBatchNum);
            /*清除已经存在的 工单计划完工时间*/
            orderEndDayMapper.batchRemove();
            log.info("计划任务执行完成!"+batchNum);
            this.updateTaskStatus(currentBatchNum, PLAN_TASK_STATUS.FINISHED);
            log.info("计划任务执行完成!" + currentBatchNum);
            return success();
        }catch (Exception e){
            redisLockUtils.releaseLock(plateOrderPlanKey,PLAN_TASK_TYPE.PLATE_PLAN.getCode());
            log.error("计划任务执行失败!"+e.getMessage());
            return AjaxResult.error("计划任务执行失败!"+e.getMessage());
        }finally {
            redisLockUtils.releaseLock(plateOrderPlanKey,PLAN_TASK_TYPE.PLATE_PLAN.getCode());
        } catch (Exception e) {
            redisLockUtils.releaseLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_PLAN.getCode());
            log.error("计划任务执行失败!" + e.getMessage());
            this.updateTaskStatus(currentBatchNum, PLAN_TASK_STATUS.ERROR);
            return AjaxResult.error("计划任务执行失败!" + e.getMessage());
        } finally {
            redisLockUtils.releaseLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_PLAN.getCode());
        }
    }
    @Override
    public AjaxResult generatorPlatePlanTable() {
        String plateOrderPlanKey = REDIS_LOCK_KEY.PLATE_SHOP_PLAN_TABLE.getKey();
        boolean existsLock = redisLockUtils.existLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_SHOP_PLAN_TABLE.getCode());
        if (existsLock) {
            return AjaxResult.warn("钣金计划大表计划任务正在执行中!");
        }
        LambdaQueryWrapper<ApsPlanTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApsPlanTask::getTaskType, PLAN_TASK_TYPE.PLATE_SHOP_PLAN_TABLE.getCode());
        queryWrapper.eq(ApsPlanTask::getTaskStatus, PLAN_TASK_STATUS.IN_PROCESS.getCode());
        boolean existsDbTask = mapper.exists(queryWrapper);
        if (existsDbTask) {
            return AjaxResult.warn("钣金计划大表任务正在执行中!!");
        }
        String currentBatchNum =  String.valueOf(IdUtil.getSnowflakeNextId()) ;
        try {
            redisLockUtils.getLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_SHOP_PLAN_TABLE.getCode(), 15 * 60L);
            this.savePlanTask( currentBatchNum);
            Thread.sleep(60000);
            plateProcessShopStatService.saveShopStat();
            this.updateTaskStatus(currentBatchNum, PLAN_TASK_STATUS.FINISHED);
            log.info("钣金计划大表任务执行完成!" + currentBatchNum);
            return success();
        } catch (Exception e) {
            redisLockUtils.releaseLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_SHOP_PLAN_TABLE.getCode());
            log.error("钣金计划大表任务执行失败!" + e.getMessage());
            this.updateTaskStatus(currentBatchNum, PLAN_TASK_STATUS.ERROR);
            return AjaxResult.error("钣金计划大表任务执行失败!" + e.getMessage());
        } finally {
            redisLockUtils.releaseLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_SHOP_PLAN_TABLE.getCode());
        }
    }