From 468ceb8a9bfe9e15e3be470b23809dde83a11f0d Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 09 十月 2023 19:14:53 +0800
Subject: [PATCH] merge master

---
 test/test.go             |    2 
 service/task.go          |   27 +-
 model/procedures.go      |   24 +-
 docs/swagger.yaml        |   62 +++++-
 api/v1/process_model.go  |   21 -
 docs/docs.go             |   90 ++++++++-
 model/request/task.go    |    3 
 pkg/contextx/contextx.go |    1 
 api/v1/task.go           |  104 +++++++++--
 docs/swagger.json        |   90 ++++++++-
 test/task_test.go        |   31 +++
 service/cache_store.go   |   17 +
 service/process_model.go |    4 
 router/index.go          |   19 +-
 model/process_model.go   |   20 +-
 15 files changed, 394 insertions(+), 121 deletions(-)

diff --git a/api/v1/process_model.go b/api/v1/process_model.go
index 06c1711..64becd0 100644
--- a/api/v1/process_model.go
+++ b/api/v1/process_model.go
@@ -15,23 +15,12 @@
 // @Summary   鑾峰彇宸ヨ壓妯″瀷鍒楄〃
 // @Produce   application/json
 // @Param     object  query    request.ProcessModelList true  "鏌ヨ鍙傛暟"
-// @Success   200   {object}  contextx.Response{data=response.TaskCountdown}  "鎴愬姛"
+// @Success   200   {object}  contextx.Response{data=[]model.ProcessModel}  "鎴愬姛"
 // @Router    /v1/processModel/list [get]
 func (slf *ProcessModelApi) List(c *gin.Context) {
 	var params request.ProcessModelList
 	ctx, ok := contextx.NewContext(c, &params)
 	if !ok {
-		return
-	}
-	procedure, code := service.NewTaskService().GetProcedureById(params.ProcedureId)
-	if code != ecode.OK {
-		ctx.Fail(code)
-		return
-	}
-
-	order, err := service.NewTaskService().GetOrderByWorkOrderId(procedure.WorkOrderID)
-	if err != nil {
-		ctx.Fail(ecode.DBErr)
 		return
 	}
 
@@ -40,10 +29,16 @@
 		ctx.Fail(ecode.DBErr)
 		return
 	}
+	oldProcessModel, _ := service.GetProcessModelByNumber(params.Number)
 	for _, processModel := range list {
-		if processModel.Procedure == procedure.ProceduresInfo.ProcedureName && processModel.Product == order.ProductName {
+		if processModel.Procedure == oldProcessModel.Procedure && processModel.Product == oldProcessModel.Product {
 			processModel.IsUpdate = processModel.Number != params.Number
 		}
+		if processModel.IsUpdate {
+
+			processModel.NewNumber, processModel.NewParamsMap = processModel.Number, processModel.ParamsMap
+			processModel.Number, processModel.ParamsMap = oldProcessModel.Number, oldProcessModel.ParamsMap
+		}
 	}
 
 	ctx.ResultList(list, total)
diff --git a/api/v1/task.go b/api/v1/task.go
index f0631d5..2d66ffe 100644
--- a/api/v1/task.go
+++ b/api/v1/task.go
@@ -68,10 +68,14 @@
 		return
 	}
 
+	taskCount := service.NewTaskService().NewTaskCount()
 	params.Page = 1
-
 	if params.PageSize <= 0 {
-		params.PageSize = channelAmount
+		if params.TaskMode == constvar.TaskModeUnStarted {
+			params.PageSize = int(taskCount)
+		} else {
+			params.PageSize = channelAmount
+		}
 	}
 
 	taskMode := constvar.TaskModeCurrent
@@ -107,15 +111,18 @@
 		return taskResponse.Tasks[i].Channel < taskResponse.Tasks[i].Channel
 	})
 
+	nowTs := time.Now().Unix()
+	flagMap := make(map[int32]struct{}, 0)
 	for _, task := range taskResponse.Tasks {
-		if !service.TaskFlagGet(task.Channel) {
+		if _, ok := flagMap[task.Channel]; !ok && !service.TaskFlagGet(task.Channel) && task.Procedure.StartTime <= nowTs {
 			task.CanStarted = true
+			flagMap[task.Channel] = struct{}{}
 		}
 	}
 
 	taskResponse.Prompt = conf.Conf.Prompt
 	taskResponse.ChannelAmount = channelAmount
-	taskResponse.TaskCount = service.NewTaskService().NewTaskCount()
+	taskResponse.TaskCount = taskCount
 
 	ctx.OkWithDetailed(taskResponse)
 }
@@ -149,11 +156,19 @@
 		ctx.Fail(ecode.DBErr)
 		return
 	}
-
-	processModel, err := service.NewTaskService().GetProcessParams(procedure, order)
-	if err != nil || processModel == nil || processModel.ParamsMap == nil {
-		ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
-		return
+	var processModel *model.ProcessModel
+	if procedure.ProcessModelNumber != "" {
+		processModel, err = service.GetProcessModelByNumber(procedure.ProcessModelNumber)
+		if err != nil || processModel == nil || processModel.ParamsMap == nil {
+			ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
+			return
+		}
+	} else {
+		processModel, err = service.NewTaskService().GetProcessParams(procedure, order)
+		if err != nil || processModel == nil || processModel.ParamsMap == nil {
+			ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
+			return
+		}
 	}
 
 	processParamsArr := make([]response.ProcessParams, 0, len(processModel.ParamsMap))
@@ -272,16 +287,15 @@
 	}
 	processModel, err := taskService.GetProcessParams(procedure, order)
 	if err != nil || processModel == nil || processModel.ParamsMap == nil {
-		ctx.Fail(ecode.UnknownErr)
+		ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
 		return
 	}
 
 	err = model.WithTransaction(func(db *gorm.DB) error {
-		err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusProcessing, params.Channel)
+		err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusProcessing, procedure.Channel, processModel.Number)
 		if err != nil {
 			return err
 		}
-		procedure.Channel = params.Channel
 		err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusProcessing)
 		if err != nil {
 			return err
@@ -303,12 +317,7 @@
 	if err != nil {
 		logx.Errorf("SendProcessParams: %v", err.Error())
 		err = model.WithTransaction(func(db *gorm.DB) error {
-			err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusWaitProcess, params.Channel)
-			if err != nil {
-				return err
-			}
-			procedure.Channel = params.Channel
-			err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusWaitProcess)
+			err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusWaitProcess, procedure.Channel, "")
 			if err != nil {
 				return err
 			}
@@ -317,12 +326,8 @@
 		ctx.FailWithMsg(ecode.NeedConfirmedErr, "PLC璇锋眰澶辫触锛岃妫�鏌LC閰嶇疆锛�")
 		return
 	}
-	if code != ecode.OK {
-		logx.Errorf("get plcConfig err: %v", err.Error())
-		return
-	}
 	plcConfig.CurrentTryTimes = 0
-	err = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, params.Channel, order.Amount.IntPart())
+	err = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, procedure.Channel, order.Amount.IntPart())
 	if err != nil {
 		ctx.FailWithMsg(ecode.NeedConfirmedErr, "PLC璇锋眰澶辫触锛岃妫�鏌LC閰嶇疆")
 		return
@@ -345,6 +350,59 @@
 	ctx.Ok()
 }
 
+// UpdateProcessParams
+// @Tags      Task
+// @Summary   鏇存柊宸ヨ壓鍙傛暟锛堣繘琛屼腑鐨勪换鍔★級
+// @Produce   application/json
+// @Param     object  body    request.SendProcessParams true  "鏌ヨ鍙傛暟"
+// @Success   200   {object}  contextx.Response{}  "鎴愬姛"
+// @Router    /v1/task/updateProcessParams [post]
+func (slf *TaskApi) UpdateProcessParams(c *gin.Context) {
+	mutex.Lock()
+	defer mutex.Unlock()
+	var params request.SendProcessParams
+	ctx, ok := contextx.NewContext(c, &params)
+	if !ok {
+		return
+	}
+	taskService := service.NewTaskService()
+	procedure, code := taskService.GetProcedureById(params.ProcedureId)
+	if code != ecode.OK {
+		ctx.Fail(code)
+		return
+	}
+	order, err := taskService.GetOrderByWorkOrderId(procedure.WorkOrderID)
+	if err != nil {
+		ctx.Fail(ecode.UnknownErr)
+		return
+	}
+
+	if procedure.Status != model.ProcedureStatusProcessing { //鍙兘杩涜涓殑鍙互鏇存柊
+		ctx.FailWithMsg(ecode.ParamsErr, "鍙兘杩涜涓殑宸ュ簭鍙互鏇存柊宸ヨ壓鍙傛暟")
+		return
+	}
+	processModel, err := taskService.GetProcessParams(procedure, order)
+	if err != nil || processModel == nil || processModel.ParamsMap == nil {
+		ctx.FailWithMsg(ecode.ParamsErr, "鏈幏鍙栧埌宸ヨ壓鍙傛暟锛岃鍦ㄥ伐鑹烘ā鍨嬪簱涓笂浼狅紒")
+		return
+	}
+
+	plcConfig, code := service.NewDevicePlcService().GetDevicePlc()
+	if code != ecode.OK || plcConfig.Id == 0 {
+		ctx.FailWithMsg(ecode.NeedConfirmedErr, "璇峰厛閰嶇疆PLC")
+		return
+	}
+	plcConfig.MaxTryTimes = 2
+	err = SendParams(processModel.ParamsMap, plcConfig)
+	if err != nil {
+		logx.Errorf("update process params err: %v", err.Error())
+		ctx.FailWithMsg(ecode.NeedConfirmedErr, "PLC璇锋眰澶辫触锛岃妫�鏌LC閰嶇疆锛�")
+		return
+	}
+	_ = taskService.UpdateProcedureStatusAndChannel(nil, params.ProcedureId, model.ProcedureStatusProcessing, procedure.Channel, processModel.Number)
+	ctx.Ok()
+}
+
 func SendParams(paramsMap map[string]interface{}, plcConfig *model.DevicePlc) error {
 	if len(paramsMap) == 0 {
 		return errors.New("empty params")
diff --git a/docs/docs.go b/docs/docs.go
index 1ad485b..5868d78 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -415,7 +415,10 @@
                                     "type": "object",
                                     "properties": {
                                         "data": {
-                                            "$ref": "#/definitions/response.TaskCountdown"
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/model.ProcessModel"
+                                            }
                                         }
                                     }
                                 }
@@ -614,6 +617,36 @@
                                     }
                                 }
                             ]
+                        }
+                    }
+                }
+            }
+        },
+        "/v1/task/updateProcessParams": {
+            "post": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Task"
+                ],
+                "summary": "鏇存柊宸ヨ壓鍙傛暟锛堣繘琛屼腑鐨勪换鍔★級",
+                "parameters": [
+                    {
+                        "description": "鏌ヨ鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.SendProcessParams"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "$ref": "#/definitions/contextx.Response"
                         }
                     }
                 }
@@ -1038,11 +1071,54 @@
                 "procedureId": {
                     "type": "string"
                 },
+                "processModelNumber": {
+                    "description": "宸ヨ壓妯″瀷缂栧彿",
+                    "type": "string"
+                },
                 "startTime": {
                     "type": "integer"
                 },
                 "status": {
                     "$ref": "#/definitions/model.ProcedureStatus"
+                }
+            }
+        },
+        "model.ProcessModel": {
+            "type": "object",
+            "properties": {
+                "deviceId": {
+                    "description": "鐢ㄤ簬杩囨护鑾峰彇nsq娑堟伅",
+                    "type": "string"
+                },
+                "isUpdate": {
+                    "description": "鍓嶇鐢�",
+                    "type": "boolean"
+                },
+                "newNumber": {
+                    "type": "string"
+                },
+                "newParamsMap": {
+                    "type": "object",
+                    "additionalProperties": true
+                },
+                "number": {
+                    "description": "宸ヨ壓妯″瀷缂栧彿",
+                    "type": "string"
+                },
+                "params": {
+                    "type": "string"
+                },
+                "paramsMap": {
+                    "type": "object",
+                    "additionalProperties": true
+                },
+                "procedure": {
+                    "description": "宸ュ簭",
+                    "type": "string"
+                },
+                "product": {
+                    "description": "浜у搧鍚嶇О",
+                    "type": "string"
                 }
             }
         },
@@ -1063,9 +1139,6 @@
                 "procedureId"
             ],
             "properties": {
-                "channel": {
-                    "type": "integer"
-                },
                 "procedureId": {
                     "type": "integer"
                 }
@@ -1132,15 +1205,6 @@
                 },
                 "data": {},
                 "msg": {
-                    "type": "string"
-                }
-            }
-        },
-        "response.Message": {
-            "type": "object",
-            "properties": {
-                "data": {},
-                "event": {
                     "type": "string"
                 }
             }
diff --git a/docs/swagger.json b/docs/swagger.json
index 03ce549..39f51a6 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -403,7 +403,10 @@
                                     "type": "object",
                                     "properties": {
                                         "data": {
-                                            "$ref": "#/definitions/response.TaskCountdown"
+                                            "type": "array",
+                                            "items": {
+                                                "$ref": "#/definitions/model.ProcessModel"
+                                            }
                                         }
                                     }
                                 }
@@ -602,6 +605,36 @@
                                     }
                                 }
                             ]
+                        }
+                    }
+                }
+            }
+        },
+        "/v1/task/updateProcessParams": {
+            "post": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Task"
+                ],
+                "summary": "鏇存柊宸ヨ壓鍙傛暟锛堣繘琛屼腑鐨勪换鍔★級",
+                "parameters": [
+                    {
+                        "description": "鏌ヨ鍙傛暟",
+                        "name": "object",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/request.SendProcessParams"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "鎴愬姛",
+                        "schema": {
+                            "$ref": "#/definitions/contextx.Response"
                         }
                     }
                 }
@@ -1026,11 +1059,54 @@
                 "procedureId": {
                     "type": "string"
                 },
+                "processModelNumber": {
+                    "description": "宸ヨ壓妯″瀷缂栧彿",
+                    "type": "string"
+                },
                 "startTime": {
                     "type": "integer"
                 },
                 "status": {
                     "$ref": "#/definitions/model.ProcedureStatus"
+                }
+            }
+        },
+        "model.ProcessModel": {
+            "type": "object",
+            "properties": {
+                "deviceId": {
+                    "description": "鐢ㄤ簬杩囨护鑾峰彇nsq娑堟伅",
+                    "type": "string"
+                },
+                "isUpdate": {
+                    "description": "鍓嶇鐢�",
+                    "type": "boolean"
+                },
+                "newNumber": {
+                    "type": "string"
+                },
+                "newParamsMap": {
+                    "type": "object",
+                    "additionalProperties": true
+                },
+                "number": {
+                    "description": "宸ヨ壓妯″瀷缂栧彿",
+                    "type": "string"
+                },
+                "params": {
+                    "type": "string"
+                },
+                "paramsMap": {
+                    "type": "object",
+                    "additionalProperties": true
+                },
+                "procedure": {
+                    "description": "宸ュ簭",
+                    "type": "string"
+                },
+                "product": {
+                    "description": "浜у搧鍚嶇О",
+                    "type": "string"
                 }
             }
         },
@@ -1051,9 +1127,6 @@
                 "procedureId"
             ],
             "properties": {
-                "channel": {
-                    "type": "integer"
-                },
                 "procedureId": {
                     "type": "integer"
                 }
@@ -1120,15 +1193,6 @@
                 },
                 "data": {},
                 "msg": {
-                    "type": "string"
-                }
-            }
-        },
-        "response.Message": {
-            "type": "object",
-            "properties": {
-                "data": {},
-                "event": {
                     "type": "string"
                 }
             }
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index b73d1c3..e17229c 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -287,10 +287,41 @@
         description: common.ProductProcedure  瀵硅薄
       procedureId:
         type: string
+      processModelNumber:
+        description: 宸ヨ壓妯″瀷缂栧彿
+        type: string
       startTime:
         type: integer
       status:
         $ref: '#/definitions/model.ProcedureStatus'
+    type: object
+  model.ProcessModel:
+    properties:
+      deviceId:
+        description: 鐢ㄤ簬杩囨护鑾峰彇nsq娑堟伅
+        type: string
+      isUpdate:
+        description: 鍓嶇鐢�
+        type: boolean
+      newNumber:
+        type: string
+      newParamsMap:
+        additionalProperties: true
+        type: object
+      number:
+        description: 宸ヨ壓妯″瀷缂栧彿
+        type: string
+      params:
+        type: string
+      paramsMap:
+        additionalProperties: true
+        type: object
+      procedure:
+        description: 宸ュ簭
+        type: string
+      product:
+        description: 浜у搧鍚嶇О
+        type: string
     type: object
   request.AddPlcBrand:
     properties:
@@ -301,8 +332,6 @@
     type: object
   request.SendProcessParams:
     properties:
-      channel:
-        type: integer
       procedureId:
         type: integer
     required:
@@ -352,12 +381,6 @@
         type: integer
       data: {}
       msg:
-        type: string
-    type: object
-  response.Message:
-    properties:
-      data: {}
-      event:
         type: string
     type: object
   response.ProcessParams:
@@ -679,7 +702,9 @@
             - $ref: '#/definitions/contextx.Response'
             - properties:
                 data:
-                  $ref: '#/definitions/response.TaskCountdown'
+                  items:
+                    $ref: '#/definitions/model.ProcessModel'
+                  type: array
               type: object
       summary: 鑾峰彇宸ヨ壓妯″瀷鍒楄〃
       tags:
@@ -802,4 +827,23 @@
       summary: 浠诲姟寮�濮嬶紙鑾峰彇宸ヨ壓鍙傛暟锛�
       tags:
       - Task
+  /v1/task/updateProcessParams:
+    post:
+      parameters:
+      - description: 鏌ヨ鍙傛暟
+        in: body
+        name: object
+        required: true
+        schema:
+          $ref: '#/definitions/request.SendProcessParams'
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: 鎴愬姛
+          schema:
+            $ref: '#/definitions/contextx.Response'
+      summary: 鏇存柊宸ヨ壓鍙傛暟锛堣繘琛屼腑鐨勪换鍔★級
+      tags:
+      - Task
 swagger: "2.0"
diff --git a/model/procedures.go b/model/procedures.go
index b3d9e73..de7397d 100644
--- a/model/procedures.go
+++ b/model/procedures.go
@@ -11,17 +11,19 @@
 
 type (
 	Procedures struct {
-		gorm.Model
-		WorkOrderID    string `gorm:"index;type:varchar(191);not null;comment:宸ュ崟ID" json:"-"`
-		OrderID        string `gorm:"index;type:varchar(191);not null;comment:璁㈠崟ID" json:"-"`
-		DeviceID       string `gorm:"index;type:varchar(191);comment:璁惧ID" json:"deviceId"`
-		ProcedureID    string `gorm:"index;type:varchar(191);comment:宸ュ簭ID" json:"procedureId"`
-		Channel        int32  `gorm:"index;comment:閫氶亾" json:"channel"` //閫氶亾
-		StartTime      int64  `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"`
-		EndTime        int64  `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"`
-		Status         ProcedureStatus
-		ProcedureData  string                  `json:"-"`                  //common.ProductProcedure  json涓�
-		ProceduresInfo common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure  瀵硅薄
+		gorm.Model         `json:"-"`
+		ID                 int    `gorm:"primarykey"`
+		WorkOrderID        string `gorm:"index;type:varchar(191);not null;comment:宸ュ崟ID" json:"-"`
+		OrderID            string `gorm:"index;type:varchar(191);not null;comment:璁㈠崟ID" json:"-"`
+		DeviceID           string `gorm:"index;type:varchar(191);comment:璁惧ID" json:"deviceId"`
+		ProcedureID        string `gorm:"index;type:varchar(191);comment:宸ュ簭ID" json:"procedureId"`
+		Channel            int32  `gorm:"index;comment:閫氶亾" json:"channel"`                //閫氶亾
+		ProcessModelNumber string `gorm:"index;comment:宸ヨ壓妯″瀷缂栧彿" json:"processModelNumber"` //宸ヨ壓妯″瀷缂栧彿
+		StartTime          int64  `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"`
+		EndTime            int64  `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"`
+		Status             ProcedureStatus
+		ProcedureData      string                  `json:"-"`                  //common.ProductProcedure  json涓�
+		ProceduresInfo     common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure  瀵硅薄
 	}
 
 	ProceduresSearch struct {
diff --git a/model/process_model.go b/model/process_model.go
index 57e73a8..7c3afb4 100644
--- a/model/process_model.go
+++ b/model/process_model.go
@@ -11,15 +11,17 @@
 type (
 	// ProcessModel 宸ヨ壓鍙傛暟
 	ProcessModel struct {
-		gorm.Model
-		Number    string                 `gorm:"index;column:number;type:varchar(255);not null;default '';comment:宸ヨ壓妯″瀷缂栧彿" json:"number"` //宸ヨ壓妯″瀷缂栧彿
-		Product   string                 `gorm:"column:product;type:varchar(255);not null;default '';comment:浜у搧鍚嶇О" json:"product"`       //浜у搧鍚嶇О
-		Procedure string                 `gorm:"column:procedure;type:varchar(255);not null;default '';comment:宸ュ簭" json:"procedure"`     //宸ュ簭
-		Params    string                 `gorm:"type:text;comment:宸ヨ壓鍙傛暟閿�煎json涓�"`
-		ParamsMap map[string]interface{} `json:"paramsMap" gorm:"-"`
-		DeviceId  string                 `json:"deviceId" gorm:"-"`                        //鐢ㄤ簬杩囨护鑾峰彇nsq娑堟伅
-		IsNew     bool                   `json:"isNew" gorm:"column:is_new;comment:鏄惁鏈�鏂扮殑"` //鏄惁鏈�鏂扮殑
-		IsUpdate  bool                   `json:"isUpdate" gorm:"-"`                        //鍓嶇鐢�
+		gorm.Model   `json:"-"`
+		Number       string                 `gorm:"index;column:number;type:varchar(255);not null;default '';comment:宸ヨ壓妯″瀷缂栧彿" json:"number"` //宸ヨ壓妯″瀷缂栧彿
+		Product      string                 `gorm:"column:product;type:varchar(255);not null;default '';comment:浜у搧鍚嶇О" json:"product"`       //浜у搧鍚嶇О
+		Procedure    string                 `gorm:"column:procedure;type:varchar(255);not null;default '';comment:宸ュ簭" json:"procedure"`     //宸ュ簭
+		Params       string                 `gorm:"type:text;comment:宸ヨ壓鍙傛暟閿�煎json涓�"`
+		ParamsMap    map[string]interface{} `json:"paramsMap" gorm:"-"`
+		DeviceId     string                 `json:"deviceId" gorm:"-"`                    //鐢ㄤ簬杩囨护鑾峰彇nsq娑堟伅
+		IsNew        bool                   `json:"-" gorm:"column:is_new;comment:鏄惁鏈�鏂扮殑"` //鏄惁鏈�鏂扮殑
+		IsUpdate     bool                   `json:"isUpdate" gorm:"-"`                    //鍓嶇鐢�
+		NewParamsMap map[string]interface{} `json:"newParamsMap" gorm:"-"`
+		NewNumber    string                 `json:"newNumber" gorm:"-"`
 	}
 
 	ProcessModelSearch struct {
diff --git a/model/request/task.go b/model/request/task.go
index b1b8ba8..6be2efa 100644
--- a/model/request/task.go
+++ b/model/request/task.go
@@ -18,8 +18,7 @@
 }
 
 type SendProcessParams struct {
-	ProcedureId uint  `json:"procedureId" binding:"required"`
-	Channel     int32 `json:"channel"`
+	ProcedureId int `json:"procedureId" binding:"required"`
 }
 
 type GetProductProgress struct {
diff --git a/pkg/contextx/contextx.go b/pkg/contextx/contextx.go
index aa450eb..1cd5c40 100644
--- a/pkg/contextx/contextx.go
+++ b/pkg/contextx/contextx.go
@@ -81,6 +81,7 @@
 	slf.ctx.JSON(http.StatusOK, ResponseList{
 		Data:  data,
 		Total: total,
+		Code:  ecode.OK,
 	})
 }
 
diff --git a/router/index.go b/router/index.go
index ce53646..8a26e61 100644
--- a/router/index.go
+++ b/router/index.go
@@ -35,11 +35,12 @@
 	taskApi := new(v1.TaskApi)
 	taskGroup := v1Group.Group("task")
 	{
-		taskGroup.GET("countdown", taskApi.TaskCountdown)      // 鏂颁换鍔″�掕鏃�
-		taskGroup.GET("get", taskApi.TaskGet)                  // 鑾峰彇宸ュ簭
-		taskGroup.GET("start/:id", taskApi.GetProcessParams)   // 鑾峰彇宸ヨ壓鍙傛暟
-		taskGroup.POST("sendProcessParams", taskApi.TaskStart) // 涓嬪彂宸ヨ壓鍙傛暟骞跺紑濮嬪伐搴�
-		taskGroup.PUT("finish/:id", taskApi.TaskFinish)        // 瀹屾垚宸ュ簭
+		taskGroup.GET("countdown", taskApi.TaskCountdown)                  // 鏂颁换鍔″�掕鏃�
+		taskGroup.GET("get", taskApi.TaskGet)                              // 鑾峰彇宸ュ簭
+		taskGroup.GET("start/:id", taskApi.GetProcessParams)               // 鑾峰彇宸ヨ壓鍙傛暟
+		taskGroup.POST("sendProcessParams", taskApi.TaskStart)             // 涓嬪彂宸ヨ壓鍙傛暟骞跺紑濮嬪伐搴�
+		taskGroup.POST("updateProcessParams", taskApi.UpdateProcessParams) // 鏇存柊宸ヨ壓鍙傛暟
+		taskGroup.PUT("finish/:id", taskApi.TaskFinish)                    // 瀹屾垚宸ュ簭
 	}
 
 	configApi := new(v1.ConfigApi)
@@ -54,15 +55,15 @@
 	plcApi := new(v1.PlcApi)
 	plcGroup := v1Group.Group("plc")
 	{
-		plcGroup.POST("productProgress", plcApi.GetProductProgress)                 // 鑾峰彇鐢熶骇杩涘害
-		plcGroup.POST("productProgressRealTime", plcApi.GetProductProgressRealTime) // 瀹炴椂鑾峰彇鐢熶骇杩涘害
-		plcGroup.POST("setProductNumber", plcApi.SetProductNumber)                  // 涓嬪彂鐢熶骇鎬婚噺
+		plcGroup.POST("productProgress", plcApi.GetProductProgress) // 鑾峰彇鐢熶骇杩涘害
+		//plcGroup.POST("productProgressRealTime", plcApi.GetProductProgressRealTime) // 瀹炴椂鑾峰彇鐢熶骇杩涘害
+		plcGroup.POST("setProductNumber", plcApi.SetProductNumber) // 涓嬪彂鐢熶骇鎬婚噺
 	}
 
 	processModelApi := new(v1.ProcessModelApi)
 	processModelGroup := v1Group.Group("processModel")
 	{
-		processModelGroup.GET("list", processModelApi.List) // 鏂颁换鍔″�掕鏃�
+		processModelGroup.GET("list", processModelApi.List) // 宸ヨ壓鍙傛暟鍒楄〃
 	}
 
 	//eventsApi := new(v1.EventsApi)
diff --git a/service/cache_store.go b/service/cache_store.go
index 2f6c515..743e4a7 100644
--- a/service/cache_store.go
+++ b/service/cache_store.go
@@ -1,9 +1,11 @@
 package service
 
 import (
+	"apsClient/conf"
 	"apsClient/constvar"
 	"apsClient/model"
 	"fmt"
+	"github.com/jinzhu/gorm"
 	"github.com/spf13/cast"
 	"sync"
 	"time"
@@ -79,18 +81,25 @@
 }
 
 func TaskFlagSet(channel int32) {
-	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), struct{}{})
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), true)
 }
 
 func TaskFlagUnset(channel int32) {
-	defaultCacheStore.Remove(fmt.Sprintf(CurrentTaskCacheKey, channel))
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), false)
 }
 
 func TaskFlagGet(channel int32) bool {
-	if _, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentTaskCacheKey, channel)); ok {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentTaskCacheKey, channel)); ok {
+		return v.(bool)
+	}
+	_, err := model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.System.DeviceId).SetStatus(model.ProcedureStatusProcessing).SetChannels([]int32{channel}).First()
+	if err == gorm.ErrRecordNotFound {
+		defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), false)
+		return false
+	} else {
+		defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), true)
 		return true
 	}
-	return false
 }
 
 func ProgressCacheGet(channel int32) (*model.ProductionProgress, bool) {
diff --git a/service/process_model.go b/service/process_model.go
index c4ff7dd..728248d 100644
--- a/service/process_model.go
+++ b/service/process_model.go
@@ -15,3 +15,7 @@
 		SetPage(page, pageSize).SetProcedures(device.ProceduresArr).Find()
 	return
 }
+
+func GetProcessModelByNumber(num string) (record *model.ProcessModel, err error) {
+	return model.NewProcessModelSearch().SetNumber(num).First()
+}
diff --git a/service/task.go b/service/task.go
index 94df214..4ff5cb8 100644
--- a/service/task.go
+++ b/service/task.go
@@ -34,7 +34,6 @@
 		Workers:   slf.WorkerDistinct(workers),
 	}
 
-	nowTs := time.Now().Unix()
 	var (
 		err          error
 		orders       []*model.Order
@@ -47,14 +46,10 @@
 
 	if mode == constvar.TaskModeUnStarted {
 		search.SetStatus(model.ProcedureStatusWaitProcess).
-			SetStartTimeMax(nowTs).
-			SetEndTimeMin(nowTs).
 			SetOrder("start_time asc")
 	} else if mode == constvar.TaskModeCurrent {
 		search.SetStatus(model.ProcedureStatusProcessing).
-			SetStartTimeMax(nowTs).
-			SetEndTimeMin(nowTs).
-			SetOrder("status desc, start_time asc")
+			SetOrder("start_time asc")
 	} else if mode == constvar.TaskModeLastFinished {
 		search.SetStatus(model.ProcedureStatusFinished).SetOrder("updated_at desc")
 		if len(channels) > 0 {
@@ -112,8 +107,7 @@
 	return taskResp, ecode.OK
 }
 func (slf TaskService) NewTaskCount() (count int64) {
-	nowTs := time.Now().Unix()
-	count, _ = model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.System.DeviceId).SetStatus(model.ProcedureStatusWaitProcess).SetEndTimeMin(nowTs).Count()
+	count, _ = model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.System.DeviceId).SetStatus(model.ProcedureStatusWaitProcess).Count()
 	return count
 }
 
@@ -137,14 +131,19 @@
 	return procedure, ecode.OK
 }
 
-func (slf TaskService) UpdateProcedureStatusAndChannel(db *gorm.DB, id uint, status model.ProcedureStatus, channel int32) error {
-	if status == model.ProcedureStatusFinished {
+func (slf TaskService) UpdateProcedureStatusAndChannel(db *gorm.DB, id int, status model.ProcedureStatus, channel int32, processModelNumber string) error {
+	if status == model.ProcedureStatusFinished || status == model.ProcedureStatusWaitProcess {
 		ProgressCacheUnset(channel)
 	}
-	return model.NewProceduresSearch(db).SetId(id).UpdateByMap(map[string]interface{}{
-		"status":  status,
-		"channel": channel,
-	})
+
+	upMap := map[string]interface{}{"status": status}
+	if status == model.ProcedureStatusProcessing {
+		upMap["process_model_number"] = processModelNumber
+	} else if status == model.ProcedureStatusWaitProcess {
+		upMap["process_model_number"] = ""
+	}
+
+	return model.NewProceduresSearch(db).SetId(id).UpdateByMap(upMap)
 }
 
 func (slf TaskService) UpdateProcedureStatus(db *gorm.DB, id uint, status model.ProcedureStatus, channel int32) error {
diff --git a/test/task_test.go b/test/task_test.go
new file mode 100644
index 0000000..caa2a00
--- /dev/null
+++ b/test/task_test.go
@@ -0,0 +1,31 @@
+package test
+
+import (
+	"apsClient/conf"
+	"apsClient/constvar"
+	"apsClient/model/common"
+	"apsClient/nsq"
+	"apsClient/pkg/logx"
+	"apsClient/service"
+	"fmt"
+	"testing"
+)
+
+func TestTaskStatusUpdate(t *testing.T) {
+	Init()
+	procedureId := 1
+	taskService := service.NewTaskService()
+	procedure, _ := taskService.GetProcedureById(procedureId)
+	msg := &common.MsgTaskStatusUpdate{
+		WorkOrderId:  procedure.WorkOrderID,
+		ProcedureID:  procedure.ProceduresInfo.ProcedureID,
+		DeviceId:     procedure.ProceduresInfo.DeviceID,
+		IsProcessing: false,
+		IsFinish:     true,
+	}
+	caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicTaskProcedureStatusUpdate, conf.Conf.NsqConf.NodeId), "")
+	err := caller.Send(msg)
+	if err != nil {
+		logx.Errorf("send task status update msg error:%v", err.Error())
+	}
+}
diff --git a/test/test.go b/test/test.go
index 62ec57a..c5dd986 100644
--- a/test/test.go
+++ b/test/test.go
@@ -10,7 +10,7 @@
 func Init() {
 	logx.Init(conf.Conf.Log)
 	defer logx.Sync()
-
+	conf.Conf.Sqlite.Dsn = "../aps.db"
 	if err := model.Init(); err != nil {
 		logx.Errorf("model Init err:%v", err)
 		return

--
Gitblit v1.8.0