Search in sources :

Example 6 with TaskPlan

use of com.eservice.api.model.task_plan.TaskPlan in project sinsim by WilsonHu.

the class ProcessRecordController method addProcessForMachine.

@PostMapping("/addProcessForMachine")
@Transactional(rollbackFor = Exception.class)
public Result addProcessForMachine(String taskRecords, String processRecord, String machine) {
    ProcessRecord pr = JSON.parseObject(processRecord, ProcessRecord.class);
    List<TaskRecord> trList = JSON.parseArray(taskRecords, TaskRecord.class);
    Machine machineObj = JSON.parseObject(machine, Machine.class);
    if (pr == null || trList == null) {
        return ResultGenerator.genFailResult("提交到服务端的JSON数据解析错误");
    }
    Integer prId = pr.getId();
    pr.setCreateTime(new Date());
    boolean firstCreate = true;
    try {
        // 已经保存过配置流程的,需要更新process
        if (prId != null && prId > 0) {
            firstCreate = false;
            processRecordService.update(pr);
        } else {
            processRecordService.save(pr);
        }
    } catch (Exception e) {
        e.printStackTrace();
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultGenerator.genFailResult("processRecordService数据库操作失败");
    }
    try {
        if (firstCreate) {
            // trList只有在第一次创建时才会传递值,更新是为[]
            trList.forEach((item) -> {
                item.setProcessRecordId(pr.getId());
            });
            taskRecordService.save(trList);
        } else {
            // 更新时数据在process中
            List<NodeDataModel> nodeDataModelList = JSON.parseArray(pr.getNodeData(), NodeDataModel.class);
            // 找到之前属于该process ID的所有task, 对比传递进入的 task records
            Condition tempCondition = new Condition(TaskRecord.class);
            tempCondition.createCriteria().andCondition("process_record_id = ", prId);
            List<TaskRecord> existTaskRecords = taskRecordService.findByCondition(tempCondition);
            // 删除不存在的list
            List<TaskRecord> validTaskRecordList = new ArrayList<>();
            for (TaskRecord exist : existTaskRecords) {
                boolean same = false;
                for (int i = 0; i < nodeDataModelList.size() && !same; i++) {
                    if (nodeDataModelList.get(i).getKey().equals("-1") || nodeDataModelList.get(i).getKey().equals("-4")) {
                    } else {
                        // task name和key值必须一致,否则会导致不统一
                        if (nodeDataModelList.get(i).getText().equals(exist.getTaskName()) && nodeDataModelList.get(i).getKey().equals(String.valueOf(exist.getNodeKey()))) {
                            same = true;
                        }
                    }
                }
                if (!same) {
                    // 删除该节点,但是已经安装后的工序不能删除,业务需要
                    if (exist.getStatus().intValue() < Constant.TASK_INSTALLING.intValue()) {
                        if (exist.getStatus().intValue() != Constant.TASK_INITIAL) {
                            // 删除task plan中对应数据,否则外键关联会导致失败
                            Condition taskPlanCondition = new Condition(TaskPlan.class);
                            taskPlanCondition.createCriteria().andCondition("task_record_id = ", exist.getId());
                            List<TaskPlan> taskPlanList = taskPlanService.findByCondition(taskPlanCondition);
                            // 只能是一个
                            if (taskPlanList.size() == 1) {
                                taskPlanService.deleteById(taskPlanList.get(0).getId());
                            }
                        }
                        taskRecordService.deleteById(exist.getId());
                        logger.warn("删除Task Record: ==> Name: " + exist.getTaskName() + ", ID: " + exist.getId() + "Status: " + exist.getStatus() + "Process Record ID: " + pr.getId());
                    }
                } else {
                    validTaskRecordList.add(exist);
                }
            }
            // 添加新的task record
            List<TaskRecord> newAddedList = new ArrayList<>();
            for (NodeDataModel item : nodeDataModelList) {
                if (Integer.valueOf(item.getKey()).intValue() == -1 || Integer.valueOf(item.getKey()).intValue() == -4) {
                    continue;
                }
                boolean newAdded = true;
                for (int i = 0; i < validTaskRecordList.size() && newAdded; i++) {
                    if (Integer.valueOf(item.getKey()).intValue() == validTaskRecordList.get(i).getNodeKey().intValue()) {
                        newAdded = false;
                    }
                }
                if (newAdded) {
                    TaskRecord record = new TaskRecord();
                    record.setStatus(Byte.valueOf("0"));
                    record.setProcessRecordId(pr.getId());
                    record.setNodeKey(Byte.valueOf(item.getKey()));
                    record.setTaskName(item.getText());
                    newAddedList.add(record);
                }
            }
            if (newAddedList.size() > 0) {
                taskRecordService.save(newAddedList);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultGenerator.genFailResult("taskRecordService数据库操作失败");
    }
    try {
        if (machineObj.getId() == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultGenerator.genFailResult("机器Id为空,数据更新失败");
        }
        // 如果机器处于初始化状态,则设置为“已配置”,如果已经是其他状态,则不需要更改机器状态
        if (machineObj.getStatus().equals(Constant.MACHINE_INITIAL)) {
            machineObj.setStatus(Constant.MACHINE_CONFIGURED);
        }
        machineService.update(machineObj);
    } catch (Exception e) {
        e.printStackTrace();
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultGenerator.genFailResult("machineService数据库操作失败");
    }
    return ResultGenerator.genSuccessResult();
}
Also used : Condition(tk.mybatis.mapper.entity.Condition) TaskRecord(com.eservice.api.model.task_record.TaskRecord) ArrayList(java.util.ArrayList) TaskPlan(com.eservice.api.model.task_plan.TaskPlan) Machine(com.eservice.api.model.machine.Machine) Date(java.util.Date) NodeDataModel(com.eservice.api.service.common.NodeDataModel) ProcessRecord(com.eservice.api.model.process_record.ProcessRecord) PostMapping(org.springframework.web.bind.annotation.PostMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

TaskPlan (com.eservice.api.model.task_plan.TaskPlan)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 PageInfo (com.github.pagehelper.PageInfo)3 Machine (com.eservice.api.model.machine.Machine)2 ProcessRecord (com.eservice.api.model.process_record.ProcessRecord)2 TaskRecord (com.eservice.api.model.task_record.TaskRecord)2 NodeDataModel (com.eservice.api.service.common.NodeDataModel)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Condition (tk.mybatis.mapper.entity.Condition)2 InstallGroup (com.eservice.api.model.install_group.InstallGroup)1 InstallPlan (com.eservice.api.model.install_plan.InstallPlan)1 MachineOrder (com.eservice.api.model.machine_order.MachineOrder)1 Task (com.eservice.api.model.task.Task)1 LinkDataModel (com.eservice.api.service.common.LinkDataModel)1 ServerToClientMsg (com.eservice.api.service.mqtt.ServerToClientMsg)1