zhanghl
2025-05-23 c8abec662e3c634365a9aeeda5c57e0f0e51cb41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.aps.core.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.aps.common.core.utils.DateUtils;
import com.aps.common.core.web.domain.AjaxResult;
import com.aps.common.redis.service.RedisLockUtils;
import com.aps.common.security.utils.SecurityUtils;
import com.aps.core.enums.PLAN_TASK_STATUS;
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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aps.core.domain.ApsPlanTask;
import com.aps.core.service.ApsPlanTaskService;
import com.aps.core.mapper.ApsPlanTaskMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import static com.aps.common.core.web.domain.AjaxResult.success;
import static com.aps.core.enums.REDIS_LOCK_KEY.PLATE_ORDER_PLAN;
 
 
/**
* @author zhl
* @description 针对表【aps_plan_task(计划生成日志表)】的数据库操作Service实现
* @createDate 2025-05-16 14:41:49
*/
 
@Slf4j
@Service
public class ApsPlanTaskServiceImpl extends ServiceImpl<ApsPlanTaskMapper, ApsPlanTask> implements ApsPlanTaskService{
 
 
    @Autowired
    ApsPlanTaskMapper mapper;
    @Autowired
    private IApsPlateStandardRequireService apsPlateStandardRequireService;
    @Resource
    IApsPlateStandardRequireBatchService requireBatchService;
 
    @Resource
    RedisLockUtils redisLockUtils;
 
    @Resource
    ApsPlateStandardRequireOrderEndDayMapper  orderEndDayMapper;
 
    @Resource
    IApsPlateProcessShopStatService plateProcessShopStatService;
 
    @Override
    public Page<ApsPlanTask> pagingList(Page<ApsPlanTask> page ,ApsPlanTask task){
        LambdaQueryWrapper<ApsPlanTask> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like( null!=task.getTaskType(),  ApsPlanTask::getTaskType,task.getTaskType());
        return mapper.selectPage(page, queryWrapper);
    }
 
    /**
     * 创建新生计划任务
     */
    @Override
    public void savePlanTask(String batchNum){
        ApsPlanTask task = ApsPlanTask.builder().id(IdUtil.getSnowflakeNextId())
                .batchNum(batchNum)
                .taskStatus(PLAN_TASK_STATUS.IN_PROCESS.getCode())
                .taskType(PLAN_TASK_TYPE.PLATE_PLAN.getCode())
                .createBy(SecurityUtils.getUsername())
                .createTime(DateUtils.getNowDate())
                .delFlag(0)
                .build();
        baseMapper.insert(task);
 
    }
    /**
     * 根据批次号 更新状态
     * */
    @Override
    public void updateTaskStatus(String batchNum, PLAN_TASK_STATUS status){
        LambdaQueryWrapper<ApsPlanTask> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(ApsPlanTask::getBatchNum,batchNum);
        ApsPlanTask task = ApsPlanTask.builder()
                .taskStatus(status.getCode())
                .updateBy(SecurityUtils.getUsername())
                .updateTime(DateUtils.getNowDate())
                .build();
        baseMapper.update(task,queryWrapper);
    }
 
    @Override
    public AjaxResult generatorPlan() {
        String plateOrderPlanKey = PLATE_ORDER_PLAN.getKey();
        boolean existsLock = redisLockUtils.existLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_PLAN.getCode());
        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());
        boolean existsDbTask = mapper.exists(queryWrapper);
        if (existsDbTask) {
            return AjaxResult.warn("钣金工单计划任务正在执行中!!");
        }
        String currentBatchNum = requireBatchService.getNewBatchNumber();
        try {
            redisLockUtils.getLock(plateOrderPlanKey, PLAN_TASK_TYPE.PLATE_PLAN.getCode(), 15 * 60L);
            this.savePlanTask(currentBatchNum);
            //Thread.sleep(60*1000);
            apsPlateStandardRequireService.generatorPlan(currentBatchNum);
            /*清除已经存在的 工单计划完工时间*/
            orderEndDayMapper.batchRemove();
            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());
            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());
        }
    }
 
}