#
Junjie
2024-10-15 f43b508dda7334487a1640b0bbd908d7b3aa3cb6
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
package com.zy.asrs.wcs.core.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.model.enums.DeviceCtgType;
import com.zy.asrs.wcs.core.model.enums.MotionStsType;
import com.zy.asrs.wcs.core.mapper.MotionMapper;
import com.zy.asrs.wcs.core.entity.Motion;
import com.zy.asrs.wcs.core.service.MotionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.asrs.wcs.core.service.MotionStsService;
import com.zy.asrs.wcs.rcs.service.DeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.List;
 
@Service("motionService")
public class MotionServiceImpl extends ServiceImpl<MotionMapper, Motion> implements MotionService {
 
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private MotionStsService motionStsService;
 
    @Override
    public List<Motion> selectUnCompleteByUuidAndDeviceCtg(String uuid, DeviceCtgType deviceCtgType) {
        return this.list(new LambdaQueryWrapper<Motion>()
                .eq(Motion::getUuid, uuid)
                .eq(Motion::getDeviceCtg, deviceTypeService.selectByFlag(deviceCtgType.toString()).getId())
                .lt(Motion::getMotionSts, motionStsService.selectByFlag(MotionStsType.COMPLETE.toString()).getId())
                .orderByDesc(Motion::getPriority)
        );
    }
 
    @Override
    public Motion selectOfTop1(String uuid, Long motionSts, Long hostId) {
        return this.baseMapper.selectOfTop1(uuid, motionSts, hostId);
    }
 
    @Override
    public Boolean hasRunningMotion(String uuid, Long hostId) {
        Motion executeMotion = this.selectOfTop1(uuid, MotionStsType.EXECUTING.val(), hostId);
        Motion errorMotion = this.selectOfTop1(uuid, MotionStsType.ERROR.val(), hostId);
        if(errorMotion != null) {
            return true;
        }
 
        if(executeMotion != null) {
            if (executeMotion.getSync() == 0) {
                //检测是否有同步动作
                List<Motion> syncMotion = this.list(new LambdaQueryWrapper<Motion>()
                        .eq(Motion::getUuid, uuid)
                        .eq(Motion::getMotionSts, MotionStsType.EXECUTING.val())
                        .eq(Motion::getHostId, hostId)
                        .eq(Motion::getSync, 1));
                if (!syncMotion.isEmpty()) {
                    return true;//存在同步动作
                }
                return false;//当前动作为异步操作
            }
            return true;
        }
 
        return false;
    }
 
    @Override
    public List<Motion> selectBySts(Long motionSts) {
        return this.list(new LambdaQueryWrapper<Motion>()
                .eq(Motion::getMotionSts, motionSts)
                .orderByDesc(Motion::getPriority));
    }
 
    @Override
    public Boolean theNextBeWaiting(String uuid, Motion motion) {
        Motion next = null;
        if (null == motion) {
            next = this.baseMapper.selectOfTop1(uuid, MotionStsType.INIT.val(), motion.getHostId());
        } else {
            next = this.selectOfNext(uuid, motion);
        }
        if (null != next) {
            next.setMotionSts(MotionStsType.WAITING.val());
            next.setUpdateTime(new Date());
            if (!this.updateById(next)) {
                throw new CoolException(uuid + " - [" + (motion == null ? 0 : (motion.getPriority() - 1)) + "] 动作更新状态失败");
            }
        }
        return Boolean.TRUE;
    }
 
    @Override
    public Motion selectOfNext(String uuid, Motion motion) {
        return this.baseMapper.selectOfNext(uuid, motion.getPriority(), motion.getHostId());
    }
 
    @Override
    public int batchInsert(List<Motion> motionList, String uuid, Integer taskNo, Long hostId) {
        int i = motionList.size();
        for (Motion motion : motionList) {
            motion.setPriority(i);
            motion.setTaskNo(taskNo);
            motion.setUuid(uuid);
            if (hostId != null) {
                motion.setHostId(hostId);
            }
            if (!this.save(motion)) {
                throw new CoolException(JSON.toJSONString(motion) + "动作保存失败");
            }
            i -= 1;
        }
        return motionList.size();
    }
 
    @Override
    public int batchInsert(List<Motion> motionList, String uuid, Integer taskNo) {
        return batchInsert(motionList, uuid, taskNo, null);
    }
}