Search in sources :

Example 11 with ProcessRecord

use of com.eservice.api.model.process_record.ProcessRecord in project sinsim by WilsonHu.

the class TaskRecordController method updateTaskInfo.

/**
 * 比如app扫码&结束开始时调用该接口,开始/完成一个工序各调用一次。
 * (质检报正常也是调该接口,即无论是否质检都调这个接口 --原先的质检已经弃用了)
 *
 * app扫码报异常时,调用 addTrArAi (app报正常时,调用的是 updateTaskInfo, 少了两个参数)
 * (质检报正常也是调addTrArAi)
 *
 * 开始安装,eg:
 * [     {
 *     "cmtFeedback":"",
 *     "id":55531,
 *     "installBeginTime":"20210127141618",
 *     "leader":"",
 *     "machine":{
 *         "createTime":"1609394393000",
 *         "id":5756,
 *         "isUrgent":false,
 *         "location":"上C2",
 *         "machineStrId":"CBB142313294",
 *         "machineType":2,
 *         "nameplate":"2011201",
 *         "orderId":2872,
 *         "status":3,
 *         "updateTime":"1609739671000"
 *     },
 *     "machineOrder":{
 *         "contractShipDate":"1608652800000",
 *         "headNum":"24",
 *         "id":2872,
 *         "machineType":2,
 *         "needleNum":"9",
 *         "orderNum":"骆1417C",
 *         "planShipDate":1609862400000
 *     },
 *     "nodeKey":-19,
 *     "processRecordId":5367,
 *     "status":3,  ====> "3" --> 开始安装
 *     "task":{
 *         "id":2,
 *         "qualityUserId":0
 *     },
 *     "taskName":"下轴安装",
 *     "taskPlan":{
 *         "createTime":"1609394393000",
 *         "id":51821,
 *         "planTime":1609776000000,
 *         "planType":1,
 *         "taskRecordId":55531,
 *         "updateTime":"1609739671000",
 *         "userId":169
 *     },
 *     "workerList":""
 *     }     ]
 *     扫描结束安装:
 *     [     {
 *     "cmtFeedback":"拜拜拜拜布b",
 *     "id":55531,
 *     "installBeginTime":"20210127141618",
 *     "installEndTime":"20210127141958",
 *     "leader":"a1",
 *     "machine":{
 *         "createTime":"1609394393000",
 *         "id":5756,
 *         "isUrgent":false,
 *         "location":"上C2",
 *         "machineStrId":"CBB142313294",
 *         "machineType":2,
 *         "nameplate":"2011201",
 *         "orderId":2872,
 *         "status":3,
 *         "updateTime":"1609739671000"
 *     },
 *     "machineOrder":{
 *         "contractShipDate":"1608652800000",
 *         "headNum":"24",
 *         "id":2872,
 *         "machineType":2,
 *         "needleNum":"9",
 *         "orderNum":"骆1417C",
 *         "planShipDate":1609862400000
 *     },
 *     "nodeKey":-19,
 *     "processRecordId":5367,
 *     "qualityBeginTime":"20210127141958",
 *     "qualityEndTime":"20210127141958",
 *     "status":6,  ========> "6" --> 质检完成, 三期新质检,这里不再是6,改为4
 *     "task":{
 *         "id":2,
 *         "qualityUserId":0
 *     },
 *     "taskName":"下轴安装",
 *     "taskPlan":{
 *         "createTime":"1609394393000",
 *         "id":51821,
 *         "planTime":1609776000000,
 *         "planType":1,
 *         "taskRecordId":55531,
 *         "updateTime":"1609739671000",
 *         "userId":169
 *     },
 *     "workerList":"赵建富"
 *     }     ]
 */
@PostMapping("/updateTaskInfo")
@Transactional(rollbackFor = Exception.class)
public Result updateTaskInfo(String taskRecord) {
    TaskRecord tr = JSON.parseObject(taskRecord, TaskRecord.class);
    Integer id = tr.getId();
    if (id == null || id < 0) {
        return ResultGenerator.genFailResult("TaskRecord的ID为空,数据更新失败!");
    }
    // 防止开始安装时间为空的问题
    if (tr.getStatus().intValue() == Constant.TASK_INSTALLING.intValue()) {
        try {
            if (tr.getUpdateTime() != null) {
                Integer timespan = (int) ((new Date().getTime() - tr.getUpdateTime().getTime()) / (1000 * 60 * 60));
                tr.setWaitTimespan(timespan);
                // 同步更新到ProcessRecord对应的task中
                ProcessRecord processRecord = processRecordService.findById(tr.getProcessRecordId());
                String nodeData = processRecord.getNodeData();
                List<NodeDataModel> ndList = JSON.parseArray(nodeData, NodeDataModel.class);
                for (int i = 0; i < ndList.size(); i++) {
                    if (ndList.get(i).getKey().equals(String.valueOf(tr.getNodeKey()))) {
                        ndList.get(i).setWaitTimespan(timespan);
                        ndList.get(i).setBeginTime(new Date().toString());
                        break;
                    }
                }
                processRecord.setNodeData(JSON.toJSONString(ndList));
                processRecordService.update(processRecord);
            }
        } catch (Exception ex) {
            logger.warn("updateTaskInfo exception: " + ex.getMessage());
        }
        tr.setInstallBeginTime(new Date());
    } else if (tr.getStatus().intValue() == Constant.TASK_PLANED.intValue() || tr.getStatus().intValue() == Constant.TASK_INSTALL_WAITING.intValue()) {
        ProcessRecord pr = processRecordService.findById(tr.getProcessRecordId());
        String nData = pr.getNodeData();
        List<NodeDataModel> ndList = JSON.parseArray(nData, NodeDataModel.class);
        for (int i = 0; i < ndList.size(); i++) {
            if (ndList.get(i).getKey().equals(String.valueOf(tr.getNodeKey()))) {
                if (ndList.get(i).getBeginTime() == "" || ndList.get(i).getBeginTime() == null) {
                    ndList.get(i).setBeginTime(new Date().toString());
                }
                break;
            }
        }
        pr.setNodeData(JSON.toJSONString(ndList));
        processRecordService.update(pr);
    }
    taskRecordService.update(tr);
    Integer prId = tr.getProcessRecordId();
    if (prId == null || prId < 0) {
        Logger.getLogger("").log(Level.INFO, "processrecord Id 为空");
    } else {
        // Update task record相关的状态
        if (!commonService.updateTaskRecordRelatedStatus(tr)) {
            // 更新出错进行事务回退
            throw new RuntimeException();
        }
    }
    // 找到工序对应的quality_user_id
    String taskName = tr.getTaskName();
    Condition condition = new Condition(Task.class);
    condition.createCriteria().andCondition("task_name = ", taskName);
    List<Task> taskList = taskService.findByCondition(condition);
    if (taskList == null || taskList.size() <= 0) {
        throw new RuntimeException();
    }
    ProcessRecord pr = processRecordService.findById(prId);
    Machine machine = machineService.findById(pr.getMachineId());
    MachineOrder machineOrder = machineOrderService.findById(machine.getOrderId());
    ServerToClientMsg msg = new ServerToClientMsg();
    msg.setOrderNum(machineOrder.getOrderNum());
    msg.setNameplate(machine.getNameplate());
    if (tr.getStatus().equals(Constant.TASK_INSTALLED)) {
        // MQTT 如果当前工序状态是安装完成等待质检的状态,则通知App
        mqttMessageHelper.sendToClient(Constant.S2C_TASK_QUALITY + taskList.get(0).getQualityUserId(), JSON.toJSONString(msg));
    }
    if (tr.getStatus().equals(Constant.TASK_INSTALLED)) {
        createInstallPlanActual(tr);
    }
    return ResultGenerator.genSuccessResult();
}
Also used : Condition(tk.mybatis.mapper.entity.Condition) TaskRecord(com.eservice.api.model.task_record.TaskRecord) Task(com.eservice.api.model.task.Task) MachineAndTask(com.eservice.api.model.machine.MachineAndTask) ServerToClientMsg(com.eservice.api.service.mqtt.ServerToClientMsg) Date(java.util.Date) Machine(com.eservice.api.model.machine.Machine) NodeDataModel(com.eservice.api.service.common.NodeDataModel) List(java.util.List) ArrayList(java.util.ArrayList) ProcessRecord(com.eservice.api.model.process_record.ProcessRecord) MachineOrder(com.eservice.api.model.machine_order.MachineOrder) PostMapping(org.springframework.web.bind.annotation.PostMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with ProcessRecord

use of com.eservice.api.model.process_record.ProcessRecord 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

ProcessRecord (com.eservice.api.model.process_record.ProcessRecord)12 Machine (com.eservice.api.model.machine.Machine)9 TaskRecord (com.eservice.api.model.task_record.TaskRecord)9 PostMapping (org.springframework.web.bind.annotation.PostMapping)9 MachineOrder (com.eservice.api.model.machine_order.MachineOrder)8 Condition (tk.mybatis.mapper.entity.Condition)8 Task (com.eservice.api.model.task.Task)7 ServerToClientMsg (com.eservice.api.service.mqtt.ServerToClientMsg)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Date (java.util.Date)6 ArrayList (java.util.ArrayList)5 MachineAndTask (com.eservice.api.model.machine.MachineAndTask)3 NodeDataModel (com.eservice.api.service.common.NodeDataModel)3 AbnormalRecord (com.eservice.api.model.abnormal_record.AbnormalRecord)2 InstallGroup (com.eservice.api.model.install_group.InstallGroup)2 InstallPlan (com.eservice.api.model.install_plan.InstallPlan)2 TaskPlan (com.eservice.api.model.task_plan.TaskPlan)2 TaskQualityRecord (com.eservice.api.model.task_quality_record.TaskQualityRecord)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 AbnormalImage (com.eservice.api.model.abnormal_image.AbnormalImage)1