From ffdeee34afd3c28f603584e80039fc1dc30f1ff9 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 13 九月 2023 17:27:22 +0800 Subject: [PATCH] 下发工艺参数时记录该工序对应的设备位置 --- service/task.go | 169 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 132 insertions(+), 37 deletions(-) diff --git a/service/task.go b/service/task.go index 86ee38e..16bfd92 100644 --- a/service/task.go +++ b/service/task.go @@ -1,9 +1,17 @@ package service import ( + "apsClient/conf" + "apsClient/constvar" "apsClient/model" + "apsClient/model/common" "apsClient/model/response" + "apsClient/nsq" "apsClient/pkg/ecode" + "apsClient/pkg/logx" + "apsClient/pkg/structx" + "encoding/json" + "fmt" "gorm.io/gorm" "time" ) @@ -16,45 +24,57 @@ } // GetTask 鑾峰彇浠诲姟锛屾湭瀹屾垚鐨勫紑濮嬫椂闂存渶鏃╃殑浠诲姟 -func (slf TaskService) GetTask() (taskData *response.TaskData, code int) { +func (slf TaskService) GetTask(page, pageSize int) (taskDataList []*response.TaskData, count int64, code int) { nowTs := time.Now().Unix() - orderSearch := model.NewOrderSearch(nil) - orderSearch.SetOrder("created_at asc"). - SetStartTimeMax(nowTs). - SetStatus(model.OrderStatusUnFinished) var ( - err error - order *model.Order + err error + orders []*model.Order + procedures []*model.Procedures + workOrderIds []string ) - order, err = orderSearch.First() //鍏堝彇杩涜涓殑浠诲姟 - if err == gorm.ErrRecordNotFound { - err = nil - return nil, ecode.OK - } - if err != nil { - return nil, ecode.DBErr - } - procedure, err := model.NewProceduresSearch(nil). - SetOrderId(order.OrderID). + procedures, err = model.NewProceduresSearch(nil).SetOrder("start_time asc"). SetStartTimeMax(nowTs). - SetStatus(model.ProcedureStatusUnFinished). - First() - if err == gorm.ErrRecordNotFound { //璇ヨ鍗曟湰璁惧鍏ㄩ儴宸ュ簭瀹屾垚锛屾妸璁㈠崟缃负瀹屾垚鐘舵�侊紙鍙槸瀵规湰璁惧瀹屾垚锛� - err = nil - err = model.NewOrderSearch(nil).SetOrderId(order.OrderID).UpdateByMap(map[string]interface{}{ - "status": model.OrderStatusFinished, - }) - if err != nil { - return nil, ecode.DBErr - } - return slf.GetTask() //褰撳墠璁㈠崟宸插畬鍏紝缁х画鏌ヨ鏈畬鎴愯鍗� - } + SetDeviceId(conf.Conf.System.DeviceId). + SetStatusNot(model.ProcedureStatusFinished). + SetPage(page, pageSize). + FindNotTotal() if err != nil { - return nil, ecode.DBErr + return nil, 0, ecode.DBErr } - taskData.Order = order - taskData.Procedure = procedure - return taskData, ecode.OK + count, err = model.NewProceduresSearch(nil). + SetDeviceId(conf.Conf.System.DeviceId). + SetStatusNot(model.ProcedureStatusFinished). + Count() + if err != nil { + return nil, 0, ecode.DBErr + } + if len(procedures) == 0 { + return nil, 0, ecode.OK + } + + for _, procedure := range procedures { + workOrderIds = append(workOrderIds, procedure.WorkOrderID) + } + orders, err = model.NewOrderSearch(nil).SetWorkOrderIds(workOrderIds).FindNotTotal() //鐢卞伐搴忓弽鎺ㄥ伐鍗� + if err != nil { + return nil, 0, ecode.DBErr + } + + orderMap := make(map[string]*model.Order, len(workOrderIds)) + for _, order := range orders { + orderMap[order.WorkOrderID] = order + } + for _, procedure := range procedures { + taskData := response.TaskData{ + Order: orderMap[procedure.WorkOrderID], + Procedure: procedure, + AllProcedures: nil, + CurrentProcedureIndex: 0, + Position: procedure.Position, + } + taskDataList = append(taskDataList, &taskData) + } + return taskDataList, count, ecode.OK } func (slf TaskService) GetProcedureById(id int) (*model.Procedures, int) { @@ -65,12 +85,87 @@ return procedure, ecode.OK } -func (slf TaskService) UpdateProcedureStatus(id int, status model.ProcedureStatus) error { - return model.NewProceduresSearch(nil).SetId(id).UpdateByMap(map[string]interface{}{ +func (slf TaskService) UpdateProcedureStatusAndPosition(db *gorm.DB, id int, status model.ProcedureStatus, position int) error { + if status == model.ProcedureStatusFinished { + ProgressCacheUnset() + } + return model.NewProceduresSearch(db).SetId(id).UpdateByMap(map[string]interface{}{ + "status": status, + "position": position, + }) +} + +func (slf TaskService) UpdateProcedureStatus(db *gorm.DB, id int, status model.ProcedureStatus) error { + if status == model.ProcedureStatusFinished { + ProgressCacheUnset() + } + return model.NewProceduresSearch(db).SetId(id).UpdateByMap(map[string]interface{}{ "status": status, }) } -func (slf TaskService) GetOrderByOrderId(orderId string) (order *model.Order, err error) { - return model.NewOrderSearch(nil).SetOrderId(orderId).First() +func (slf TaskService) UpdateOrderStatus(db *gorm.DB, id uint, status model.OrderStatus) error { + return model.NewOrderSearch(db).SetId(id).UpdateByMap(map[string]interface{}{ + "status": status, + }) +} + +func (slf TaskService) GetOrderByWorkOrderId(workOrderId string) (order *model.Order, err error) { + return model.NewOrderSearch(nil).SetWorkOrderId(workOrderId).First() +} + +func (slf TaskService) GetProcessParams(procedure *model.Procedures, order *model.Order) (processModel *model.ProcessModel, err error) { + data, err := model.NewProcessModelSearch(). + SetWorkOrder(procedure.WorkOrderID). + SetDevice(procedure.ProceduresInfo.DeviceName). + SetOrderId(procedure.OrderID). + SetProcedure(procedure.ProceduresInfo.ProcedureName). + SetProduct(order.ProductName). + SetOrder("id desc").First() + if err != nil && err != gorm.ErrRecordNotFound { + logx.Errorf("TaskStart GetProcessModel err:%v", err) + return nil, err + } + if err == nil { + err = json.Unmarshal([]byte(data.Params), &data.ParamsMap) + if err != nil { + logx.Errorf("process model json.Unmarshal:%v", err) + return + } + return data, nil + } + + if err == gorm.ErrRecordNotFound { //濡傛灉鏁版嵁搴撴病鏈変粠浜戠鑾峰彇 + caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicProcessParamsRequest, conf.Conf.NsqConf.NodeId), fmt.Sprintf(constvar.NsqTopicProcessParamsResponse, conf.Conf.NsqConf.NodeId)) + var result common.ResponseProcessParams + err = caller.Call(common.RequestProcessParams{ + WorkOrder: procedure.WorkOrderID, + OrderId: procedure.OrderID, + Product: order.ProductName, + Procedure: procedure.ProceduresInfo.ProcedureName, + Device: procedure.ProceduresInfo.DeviceName, + DeviceId: conf.Conf.System.DeviceId, + }, &result, time.Second*3) + if err != nil { + logx.Errorf("TaskStart GetProcessModel error:%v", err.Error()) + return + } + if result.ParamsMap == nil { + logx.Errorf("TaskStart GetProcessModel response miss process params:%v", result) + return + } + processModel = new(model.ProcessModel) + err = structx.AssignTo(result, &processModel) + return processModel, err + } + return +} + +// GetNextTask 鑾峰彇鏈紑濮嬬殑浠诲姟 +func (slf TaskService) GetNextTask() (workOrder *model.Order, err error) { + nowTs := time.Now().Unix() + orderSearch := model.NewOrderSearch(nil) + return orderSearch.SetOrder("start_time asc"). + SetStartTimeMin(nowTs). + SetStatus(model.OrderStatusWaitProcess).First() } -- Gitblit v1.8.0