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
package com.mes.job.opccallback;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kangaroohy.milo.runner.subscription.SubscriptionCallback;
import com.mes.common.config.Const;
import com.mes.edgstoragecage.entity.EdgStorageCage;
import com.mes.edgstoragecage.entity.EdgStorageCageDetails;
import com.mes.edgstoragecage.service.EdgStorageCageDetailsService;
import com.mes.edgstoragecage.service.EdgStorageCageService;
import com.mes.opctask.entity.EdgStorageDeviceTask;
import com.mes.opctask.entity.EdgStorageDeviceTaskHistory;
import com.mes.opctask.service.EdgStorageDeviceTaskHistoryService;
import com.mes.opctask.service.EdgStorageDeviceTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * @Author : zhoush
 * @Date: 2024/10/10 14:13
 * @Description:
 */
@Service
@Slf4j
public class CacheGlassFinishCallback implements SubscriptionCallback {
 
    private static final String EDG_STORAGE_DEVICE_ONE_TASK = "edg_storage_device_one_task";
 
    private static final String EDG_STORAGE_DEVICE_TWO_TASK = "edg_storage_device_two_task";
 
    @Value("${mes.glassGap}")
    private int glassGap;
    @Value("${mes.cellLength}")
    private int cellLength;
 
    @Resource
    EdgStorageDeviceTaskService edgStorageDeviceTaskService;
    @Resource
    EdgStorageCageDetailsService edgStorageCageDetailsService;
    @Resource
    EdgStorageCageService edgStorageCageService;
    @Resource
    EdgStorageDeviceTaskHistoryService edgStorageDeviceTaskHistoryService;
 
    @Override
    public void onSubscribe(String identifier, Object value) {
        String tableName = identifier.contains("01") ? EDG_STORAGE_DEVICE_ONE_TASK : EDG_STORAGE_DEVICE_TWO_TASK;
        EdgStorageDeviceTask task = edgStorageDeviceTaskService.queryTaskMessage(tableName);
        if (task == null) {
            log.info("任务表基础数据录入失败,请检查数据是否录入成功");
            return;
        }
        if (task.getTaskState() <= 4) {
            log.info("不存在未完成的任务");
            return;
        }
        Integer cell = task.getStartCell();
        Integer state = task.getTaskState();
        task.setTaskRunning(Const.GLASS_CACHE_TYPE_EMPTY);
        task.setTaskState(Const.GLASS_CACHE_TYPE_EMPTY);
        task.setStartCell(0);
        edgStorageDeviceTaskService.updateTaskMessage(tableName, task);
        edgStorageDeviceTaskHistoryService.update(new LambdaUpdateWrapper<EdgStorageDeviceTaskHistory>()
                .eq(EdgStorageDeviceTaskHistory::getTaskState, Const.RAW_GLASS_TASK_NEW)
                .set(EdgStorageDeviceTaskHistory::getTaskState,
                        Const.GLASS_CACHE_TYPE_FINISH.equals(state) ? Const.RAW_GLASS_TASK_SUCCESS : Const.RAW_GLASS_TASK_FAILURE)
        );
        updateCellRemainWidth(cell);
    }
 
    private boolean updateCellRemainWidth(int slot) {
        List<EdgStorageCageDetails> list = edgStorageCageDetailsService.list(new LambdaQueryWrapper<EdgStorageCageDetails>().eq(EdgStorageCageDetails::getSlot, slot)
                .eq(EdgStorageCageDetails::getState, Const.GLASS_STATE_IN));
        if (CollectionUtil.isEmpty(list)) {
            log.info("格子{}内无玻璃,无法更新", slot);
            return Boolean.FALSE;
        }
        int widthTotal = (int) list.stream().map(e -> e.getWidth() + glassGap).mapToDouble(Double::intValue).sum();
        int remainWidth = cellLength - widthTotal >= 0 ? cellLength - widthTotal : 0;
        edgStorageCageService.update(new LambdaUpdateWrapper<EdgStorageCage>().
                set(EdgStorageCage::getRemainWidth, remainWidth).eq(EdgStorageCage::getSlot, slot));
        return Boolean.FALSE;
    }
}