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 |   73 +++++++++++++++++++++++++-----------
 1 files changed, 50 insertions(+), 23 deletions(-)

diff --git a/service/task.go b/service/task.go
index 4def207..16bfd92 100644
--- a/service/task.go
+++ b/service/task.go
@@ -24,39 +24,57 @@
 }
 
 // GetTask 鑾峰彇浠诲姟锛屾湭瀹屾垚鐨勫紑濮嬫椂闂存渶鏃╃殑浠诲姟
-func (slf TaskService) GetTask() (taskData *response.TaskData, code int) {
-	if v, ok := TaskCacheGet(); ok {
-		return v, ecode.OK
-	}
-
+func (slf TaskService) GetTask(page, pageSize int) (taskDataList []*response.TaskData, count int64, code int) {
 	nowTs := time.Now().Unix()
 	var (
-		err       error
-		order     *model.Order
-		procedure *model.Procedures
+		err          error
+		orders       []*model.Order
+		procedures   []*model.Procedures
+		workOrderIds []string
 	)
-	procedure, err = model.NewProceduresSearch(nil).SetOrder("start_time asc").
+	procedures, err = model.NewProceduresSearch(nil).SetOrder("start_time asc").
 		SetStartTimeMax(nowTs).
 		SetDeviceId(conf.Conf.System.DeviceId).
 		SetStatusNot(model.ProcedureStatusFinished).
-		First()
-	if err == gorm.ErrRecordNotFound {
-		return nil, ecode.OK
+		SetPage(page, pageSize).
+		FindNotTotal()
+	if err != nil {
+		return nil, 0, ecode.DBErr
+	}
+	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
 	}
 
-	order, err = model.NewOrderSearch(nil).SetWorkOrderId(procedure.WorkOrderID).First() //鐢卞伐搴忓弽鎺ㄥ伐鍗�
-	if err == gorm.ErrRecordNotFound {
-		err = nil
-		return nil, ecode.OK
+	for _, procedure := range procedures {
+		workOrderIds = append(workOrderIds, procedure.WorkOrderID)
 	}
+	orders, err = model.NewOrderSearch(nil).SetWorkOrderIds(workOrderIds).FindNotTotal() //鐢卞伐搴忓弽鎺ㄥ伐鍗�
 	if err != nil {
-		return nil, ecode.DBErr
+		return nil, 0, ecode.DBErr
 	}
-	taskData = new(response.TaskData)
-	taskData.Order = order
-	taskData.Procedure = procedure
-	TaskCacheSet(taskData)
-	return taskData, ecode.OK
+
+	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) {
@@ -67,9 +85,18 @@
 	return procedure, ecode.OK
 }
 
+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 {
-		TaskCacheUnset()
 		ProgressCacheUnset()
 	}
 	return model.NewProceduresSearch(db).SetId(id).UpdateByMap(map[string]interface{}{

--
Gitblit v1.8.0