#
Junjie
2024-04-19 e222fba1a5aab1c9877667560629ae643133cb7d
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/rcs/thread/FlowExecute.java
@@ -3,10 +3,16 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.wcs.asrs.entity.param.FlowLogicCodeParam;
import com.zy.asrs.wcs.core.entity.Task;
import com.zy.asrs.wcs.core.service.TaskService;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.core.utils.ShuttleDispatcher;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@@ -15,44 +21,63 @@
@Component
public class FlowExecute {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ShuttleDispatcher shuttleDispatcher;
    //执行流程图
    public boolean execute(List<JSONObject> list) {
        String currentId = list.get(0).getString("id");
        String listId = DigestUtils.md5Hex(JSON.toJSONString(list));
        JSONObject devpType = list.get(0).getJSONObject("data").getJSONObject("devpType");
        String staNo = devpType.getString("staNo");
        //搜索工作档
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getOriginSite, staNo)
                .eq(Task::getStatus, 1));
        while (currentId != null) {
            //获取流程图
            JSONObject flow = findFLow(list, currentId);
            if (flow == null) {
                break;
        for (Task task : tasks) {
            String redisKey = DigestUtils.md5Hex(JSON.toJSONString(list));
            while (currentId != null) {
                //获取流程图
                JSONObject flow = findFLow(list, currentId);
                if (flow == null) {
                    break;
                }
                //执行
                boolean result = executeFlow(task, flow, redisKey);
                //执行后续流程
                if (Cools.isEmpty(flow.get("nextTrue")) && Cools.isEmpty(flow.get("nextFalse"))) {
                    break;//无后续流程
                }
                //更新id
                currentId = result ? flow.getString("nextTrue") : flow.getString("nextFalse");
            }
            //执行
            boolean result = executeFlow(flow, listId);
            //执行后续流程
            if (Cools.isEmpty(flow.get("nextTrue")) && Cools.isEmpty(flow.get("nextFalse"))) {
                break;//无后续流程
            }
            //更新id
            currentId = result ? flow.getString("nextTrue") : flow.getString("nextFalse");
            System.out.println("执行完成");
            redisUtil.del(redisKey);//释放缓存
        }
        System.out.println("执行完成");
        return true;
    }
    private boolean executeFlow(JSONObject flow, String listId) {
    private boolean executeFlow(Task task, JSONObject flow, String redisKey) {
        System.out.println(flow.getString("id") + "被执行");
        String type = flow.getString("type");
        JSONObject data = flow.getJSONObject("data");
        String type = data.getString("type");
        boolean result = false;
        if (type.equals("devp")) {
            result = executeDevpFlow(task, data, redisKey);
        } else if (type.equals("shuttle")) {
            result = executeShuttleFlow(task, data, redisKey);
        }
        return true;
        return result;
    }
    private JSONObject findFLow(List<JSONObject> list, String id) {
@@ -64,6 +89,48 @@
        return null;
    }
    private boolean executeDevpFlow(Task task, JSONObject data, String redisKey) {
        JSONObject devpType = data.getJSONObject("devpType");
        String staNo = devpType.getString("staNo");
        //搜索工作
        return true;
    }
    private boolean executeShuttleFlow(Task task, JSONObject data, String redisKey) {
        JSONObject shuttleType = data.getJSONObject("shuttleType");
        JSONArray shuttleOper = shuttleType.getJSONArray("shuttleOper");
        for (Object object : shuttleOper) {
            String oper = String.valueOf(object);
            if (oper.equals("searchIdleShuttle")) {
                //搜索空闲车
                ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
                if (shuttleThread == null) {
                    return false;
                }
                //存入缓存
                redisSet(redisKey, "shuttleDevice", shuttleThread.getDevice());
                return true;
            }
        }
        return true;
    }
    private boolean redisSet(String redisKey, String key, Object data) {
        if (redisUtil.hasKey(redisKey)) {
            Object obj = redisUtil.get(redisKey);
            JSONObject object = JSON.parseObject(String.valueOf(obj));
            object.put(key, data);
            redisUtil.set(redisKey, JSON.toJSONString(object));
        } else {
            JSONObject object = new JSONObject();
            object.put(key, data);
            redisUtil.set(redisKey, JSON.toJSONString(object));
        }
        return true;
    }
//    private boolean executeFlow(List<Map<String, Object>> list) {
//        for (Map<String, Object> map : list) {
//            JSONObject data = (JSONObject) map.get("data");