Search in sources :

Example 1 with NodeDataModel

use of com.eservice.api.service.common.NodeDataModel in project sinsim by WilsonHu.

the class TaskPlanServiceImpl method addTaskPlans.

// 每新增1个工序,调用一次。
@Transactional(rollbackFor = Exception.class)
public boolean addTaskPlans(@RequestParam List<Integer> taskRecordIds, Integer planType, String machineStrId, Date planDate, Integer userId) {
    for (int i = 0; i < taskRecordIds.size(); i++) {
        Condition tempCondition = new Condition(TaskPlan.class);
        Integer id = taskRecordIds.get(i);
        tempCondition.createCriteria().andCondition("task_record_id = ", id);
        List<TaskPlan> existPlans = findByCondition(tempCondition);
        if (existPlans.size() > 0) {
            return false;
        }
        TaskPlan plan = new TaskPlan();
        plan.setCreateTime(new Date());
        plan.setUserId(userId);
        plan.setTaskRecordId(id);
        plan.setPlanType(Constant.DAILY_PLAN);
        if (planType.intValue() == Constant.DAILY_PLAN.intValue()) {
            plan.setPlanTime(planDate);
        } else if (planType.intValue() == Constant.FLEX_PLAN.intValue()) {
            plan.setDeadline(planDate);
        }
        save(plan);
        // 更改task record状态为已计划
        TaskRecord taskRecord = taskRecordService.findById(id);
        if (taskRecord != null) {
            // 检查是否为第一个计划项,如果是,需要设置为待安装状态
            Integer processRecordId = taskRecord.getProcessRecordId();
            ProcessRecord processRecord = processRecordService.findById(processRecordId);
            List<LinkDataModel> linkDataList = JSON.parseArray(processRecord.getLinkData(), LinkDataModel.class);
            for (LinkDataModel item : linkDataList) {
                if (item.getTo().equals(taskRecord.getNodeKey().intValue())) {
                    if (item.getFrom() == null || item.getFrom() == -1) {
                        taskRecord.setUpdateTime(new Date());
                        taskRecord.setStatus(Constant.TASK_INSTALL_WAITING);
                        break;
                    } else {
                        // TODO:如果是重新配置流程项,比如:改单、拆单以后再最后增加了安装项,则需要设置成待安装状态
                        List<Integer> parentNodeList = new ArrayList<>();
                        String nodeData = processRecord.getNodeData();
                        List<NodeDataModel> ndList = JSON.parseArray(nodeData, NodeDataModel.class);
                        for (LinkDataModel tmp : linkDataList) {
                            if (tmp.getTo() == taskRecord.getNodeKey().intValue()) {
                                parentNodeList.add(tmp.getFrom());
                            }
                        }
                        boolean allParentFinished = true;
                        for (Integer parentNodeKey : parentNodeList) {
                            for (NodeDataModel nodeDataModel : ndList) {
                                if (parentNodeKey.intValue() == Integer.valueOf(nodeDataModel.getKey())) {
                                    if (Integer.valueOf(nodeDataModel.getTaskStatus()) != Constant.TASK_QUALITY_DONE.intValue()) {
                                        allParentFinished = false;
                                        break;
                                    }
                                }
                            }
                            if (!allParentFinished) {
                                break;
                            }
                        }
                        if (allParentFinished) {
                            taskRecord.setUpdateTime(new Date());
                            taskRecord.setStatus(Constant.TASK_INSTALL_WAITING);
                        }
                    }
                }
            }
            if (taskRecord.getStatus().equals(Constant.TASK_INITIAL)) {
                taskRecord.setStatus(Constant.TASK_PLANED);
            }
            taskRecordService.update(taskRecord);
            // 更新task_record以外,但是跟task record相关的状态,机器状态,process_record中的task_status
            commonService.updateTaskRecordRelatedStatus(taskRecord);
            if (taskRecord.getStatus().equals(Constant.TASK_INSTALL_WAITING)) {
                // MQTT 计划后,通知安装组长,可以进行安装
                String taskName = taskRecord.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(taskRecord.getProcessRecordId());
                Machine machine = machineService.findById(pr.getMachineId());
                MachineOrder machineOrder = machineOrderService.findById(machine.getOrderId());
                ServerToClientMsg msg = new ServerToClientMsg();
                msg.setOrderNum(machineOrder.getOrderNum());
                msg.setNameplate(machine.getNameplate());
                mqttMessageHelper.sendToClient(Constant.S2C_TASK_INSTALL + taskList.get(0).getGroupId(), JSON.toJSONString(msg));
            }
            // /在”计划管理“中 添加计划安排时,自动添加了 总装排产 InstallPlan。 (InstallPlanActual则在app扫码完成工序时添加)
            // 因为把总装排产,合并到“计划管理”,所以需要在安排计划时 自动生成总装排产(这样在app上扫码完成时,才能有总装完成自动填充完成头数)
            InstallPlan installPlan1 = new InstallPlan();
            installPlan1.setType(Constant.STR_INSTALL_TYPE_WHOLE);
            InstallGroup installGroup = installGroupService.getInstallGroupByTaskName(taskRecord.getTaskName());
            installPlan1.setInstallGroupId(installGroup.getId());
            installPlan1.setInstallDatePlan(plan.getPlanTime());
            ProcessRecord pr = processRecordService.findById(taskRecord.getProcessRecordId());
            Machine machine = machineService.findById(pr.getMachineId());
            MachineOrder machineOrder = machineOrderService.findById(machine.getOrderId());
            installPlan1.setOrderId(machineOrder.getId());
            installPlan1.setMachineId(machine.getId());
            installPlan1.setValid(Byte.valueOf("1"));
            installPlan1.setCreateDate(new Date());
            installPlanService.save(installPlan1);
            logger.info("自动添加了 总装排产 for 铭牌号: " + machine.getNameplate() + ", 工序: " + taskRecord.getTaskName());
        } else {
            // 进行事务操作
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException();
        }
    }
    if (taskRecordIds.size() > 0 && machineStrId != null) {
        List<Machine> machineList = machineService.selectMachines(null, null, null, machineStrId, null, null, null, null, null, null, false);
        if (machineList.size() == 1) {
            // 如果机器状态小于计划中,则更新为计划中
            Machine machine = machineList.get(0);
            if (machine.getStatus() < Constant.MACHINE_PLANING) {
                machine.setStatus(Constant.MACHINE_PLANING);
                machineService.update(machine);
            }
        } else {
            // 进行事务rollback操作
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException();
        }
    }
    return true;
}
Also used : TaskRecord(com.eservice.api.model.task_record.TaskRecord) Task(com.eservice.api.model.task.Task) TaskPlan(com.eservice.api.model.task_plan.TaskPlan) ArrayList(java.util.ArrayList) InstallGroup(com.eservice.api.model.install_group.InstallGroup) Machine(com.eservice.api.model.machine.Machine) NodeDataModel(com.eservice.api.service.common.NodeDataModel) ProcessRecord(com.eservice.api.model.process_record.ProcessRecord) MachineOrder(com.eservice.api.model.machine_order.MachineOrder) Condition(tk.mybatis.mapper.entity.Condition) LinkDataModel(com.eservice.api.service.common.LinkDataModel) ServerToClientMsg(com.eservice.api.service.mqtt.ServerToClientMsg) Date(java.util.Date) InstallPlan(com.eservice.api.model.install_plan.InstallPlan) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with NodeDataModel

use of com.eservice.api.service.common.NodeDataModel 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) 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 3 with NodeDataModel

use of com.eservice.api.service.common.NodeDataModel 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

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