From 8426708ecdf4f25656a04d7b182643b27c188157 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 19 八月 2023 11:05:43 +0800
Subject: [PATCH] 增加测试数据
---
test/test.go | 23 +++
service/task.go | 1
nsq/msg_handler.go | 7
model/procedures.go | 15 +-
test/msg_handler_test.go | 139 +++++++++++++++++++++++
docs/swagger.yaml | 23 +++
docs/docs.go | 38 ++++++
api/v1/task.go | 12 +
docs/swagger.json | 38 ++++++
model/util.go | 4
conf/config.go | 9 -
conf/apsClient.json | 2
model/response/common.go | 5
model/work_order.go | 2
14 files changed, 290 insertions(+), 28 deletions(-)
diff --git a/api/v1/task.go b/api/v1/task.go
index c2e6b8c..9915449 100644
--- a/api/v1/task.go
+++ b/api/v1/task.go
@@ -2,6 +2,7 @@
import (
"apsClient/model"
+ "apsClient/model/response"
_ "apsClient/model/response"
"apsClient/pkg/contextx"
"apsClient/pkg/convertx"
@@ -40,7 +41,7 @@
// @Summary 浠诲姟寮�濮�
// @Produce application/json
// @Param id path int true "宸ュ簭id"
-// @Success 200 {object} contextx.Response{service.GetProcessModel} "鎴愬姛"
+// @Success 200 {object} contextx.Response{data=[]response.ProcessParams} "鎴愬姛"
// @Router /v1/task/start/{id} [get]
func (slf *TaskApi) TaskStart(c *gin.Context) {
ctx, ok := contextx.NewContext(c, nil)
@@ -79,8 +80,15 @@
ctx.Fail(ecode.UnknownErr)
return
}
+ processParamsArr := make([]*response.ProcessParams, 0, len(resp.ParamsMap))
+ for k, v := range resp.ParamsMap {
+ processParamsArr = append(processParamsArr, &response.ProcessParams{
+ Key: k,
+ Value: v,
+ })
+ }
logx.Infof("TaskStart Notice GetProcessModel: %+v", resp)
- ctx.OkWithDetailed(resp)
+ ctx.OkWithDetailed(processParamsArr)
}
// TaskFinish
diff --git a/conf/apsClient.json b/conf/apsClient.json
index 2ed7d69..e4fb3f9 100644
--- a/conf/apsClient.json
+++ b/conf/apsClient.json
@@ -8,7 +8,7 @@
"LimitTimeIP": 3600,
"RouterPrefix": "api",
"SudoPassword": "basic2021",
- "deviceId": "MA-JWW-1"
+ "deviceId": "DeviceID 1"
},
"log": {
"path": "./logs/apsClient.log",
diff --git a/conf/config.go b/conf/config.go
index 5af1d43..c28317b 100644
--- a/conf/config.go
+++ b/conf/config.go
@@ -6,7 +6,6 @@
"apsClient/pkg/mysqlx"
"apsClient/pkg/redisx"
"apsClient/pkg/sqlitex"
- "flag"
"github.com/spf13/viper"
"log"
)
@@ -111,14 +110,6 @@
)
func init() {
- var configFile string
- flag.StringVar(&configFile, "config", "", "config file in json")
- flag.Parse()
-
- if len(configFile) > 0 {
- configName = configFile
- }
-
Viper = viper.New()
Viper.SetConfigName(configName)
for _, path := range configPaths {
diff --git a/docs/docs.go b/docs/docs.go
index 825f266..c7aeffc 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -125,7 +125,22 @@
"200": {
"description": "鎴愬姛",
"schema": {
- "$ref": "#/definitions/contextx.Response"
+ "allOf": [
+ {
+ "$ref": "#/definitions/contextx.Response"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/response.ProcessParams"
+ }
+ }
+ }
+ }
+ ]
}
}
}
@@ -181,6 +196,9 @@
},
"unit": {
"type": "string"
+ },
+ "workOrderId": {
+ "type": "string"
}
}
},
@@ -220,8 +238,14 @@
"request.ProcedureWorker": {
"type": "object",
"properties": {
+ "endTime": {
+ "type": "integer"
+ },
"phoneNum": {
"type": "string"
+ },
+ "startTime": {
+ "type": "integer"
},
"workerId": {
"type": "string"
@@ -235,6 +259,9 @@
"type": "object",
"properties": {
"deviceId": {
+ "type": "string"
+ },
+ "deviceName": {
"type": "string"
},
"endTime": {
@@ -275,6 +302,15 @@
}
}
},
+ "response.ProcessParams": {
+ "type": "object",
+ "properties": {
+ "key": {
+ "type": "string"
+ },
+ "value": {}
+ }
+ },
"response.TaskData": {
"type": "object",
"properties": {
diff --git a/docs/swagger.json b/docs/swagger.json
index 8b082c8..ad30a37 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -113,7 +113,22 @@
"200": {
"description": "鎴愬姛",
"schema": {
- "$ref": "#/definitions/contextx.Response"
+ "allOf": [
+ {
+ "$ref": "#/definitions/contextx.Response"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/response.ProcessParams"
+ }
+ }
+ }
+ }
+ ]
}
}
}
@@ -169,6 +184,9 @@
},
"unit": {
"type": "string"
+ },
+ "workOrderId": {
+ "type": "string"
}
}
},
@@ -208,8 +226,14 @@
"request.ProcedureWorker": {
"type": "object",
"properties": {
+ "endTime": {
+ "type": "integer"
+ },
"phoneNum": {
"type": "string"
+ },
+ "startTime": {
+ "type": "integer"
},
"workerId": {
"type": "string"
@@ -223,6 +247,9 @@
"type": "object",
"properties": {
"deviceId": {
+ "type": "string"
+ },
+ "deviceName": {
"type": "string"
},
"endTime": {
@@ -263,6 +290,15 @@
}
}
},
+ "response.ProcessParams": {
+ "type": "object",
+ "properties": {
+ "key": {
+ "type": "string"
+ },
+ "value": {}
+ }
+ },
"response.TaskData": {
"type": "object",
"properties": {
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 8624ed1..dd25e9c 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -32,6 +32,8 @@
type: integer
unit:
type: string
+ workOrderId:
+ type: string
type: object
model.Procedures:
properties:
@@ -55,8 +57,12 @@
type: object
request.ProcedureWorker:
properties:
+ endTime:
+ type: integer
phoneNum:
type: string
+ startTime:
+ type: integer
workerId:
type: string
workerName:
@@ -65,6 +71,8 @@
request.ProductProcedure:
properties:
deviceId:
+ type: string
+ deviceName:
type: string
endTime:
type: integer
@@ -91,6 +99,12 @@
items:
$ref: '#/definitions/request.ProcedureWorker'
type: array
+ type: object
+ response.ProcessParams:
+ properties:
+ key:
+ type: string
+ value: {}
type: object
response.TaskData:
properties:
@@ -169,7 +183,14 @@
"200":
description: 鎴愬姛
schema:
- $ref: '#/definitions/contextx.Response'
+ allOf:
+ - $ref: '#/definitions/contextx.Response'
+ - properties:
+ data:
+ items:
+ $ref: '#/definitions/response.ProcessParams'
+ type: array
+ type: object
summary: 浠诲姟寮�濮�
tags:
- Task
diff --git a/model/procedures.go b/model/procedures.go
index 4915825..c2803d1 100644
--- a/model/procedures.go
+++ b/model/procedures.go
@@ -12,10 +12,12 @@
type (
Procedures struct {
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:"-"`
- Status ProcedureStatus `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:"-"`
+ StartTime int64 `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"`
+ EndTime int64 `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"`
+ Status ProcedureStatus
ProcedureData string `json:"-"` //request.ProductProcedure json涓�
ProceduresInfo request.ProductProcedure `json:"procedure" gorm:"-"` //request.ProductProcedure 瀵硅薄
}
@@ -41,14 +43,15 @@
func (slf *Procedures) TableName() string {
return "procedures"
}
-func (slf *Procedures) AfterFind() {
+func (slf *Procedures) AfterFind(db *gorm.DB) error {
var proceduresInfo request.ProductProcedure
err := json.Unmarshal([]byte(slf.ProcedureData), &proceduresInfo)
if err != nil {
logx.Errorf("AfterFind Unmarshal err: %v", err.Error())
- return
+ return err
}
slf.ProceduresInfo = proceduresInfo
+ return nil
}
func NewProceduresSearch(db *gorm.DB) *ProceduresSearch {
diff --git a/model/response/common.go b/model/response/common.go
index 05246bb..3d84bab 100644
--- a/model/response/common.go
+++ b/model/response/common.go
@@ -15,3 +15,8 @@
Order *model.Order
Procedure *model.Procedures
}
+
+type ProcessParams struct {
+ Key string
+ Value interface{}
+}
diff --git a/model/util.go b/model/util.go
index 224ad68..55746a4 100644
--- a/model/util.go
+++ b/model/util.go
@@ -1,14 +1,14 @@
package model
import (
- "apsClient/pkg/mysqlx"
+ "apsClient/pkg/sqlitex"
"fmt"
"gorm.io/gorm"
)
// WithTransaction 锛� var funcs []func(db *gorm.DB) error锛屾妸鐩稿叧鍑芥暟娣诲姞杩涘幓
func WithTransaction(fns ...func(*gorm.DB) error) (err error) {
- tx := mysqlx.GetDB().Begin()
+ tx := sqlitex.GetDB().Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
diff --git a/model/work_order.go b/model/work_order.go
index 6848342..80a770b 100644
--- a/model/work_order.go
+++ b/model/work_order.go
@@ -22,7 +22,7 @@
Unit string `gorm:"type:varchar(100);comment:鍗曚綅" json:"unit"`
StartTime int64 `gorm:"comment:璁″垝寮�濮嬫椂闂�" json:"startTime"`
EndTime int64 `gorm:"comment:璁″垝缁撴潫鏃堕棿" json:"endTime"`
- Status OrderStatus `json:"-"`
+ Status OrderStatus
}
OrderSearch struct {
diff --git a/nsq/msg_handler.go b/nsq/msg_handler.go
index dd692fe..7545fe7 100644
--- a/nsq/msg_handler.go
+++ b/nsq/msg_handler.go
@@ -7,7 +7,6 @@
"apsClient/pkg/logx"
"apsClient/pkg/structx"
"encoding/json"
- "fmt"
"gorm.io/gorm"
)
@@ -19,10 +18,8 @@
}
func (slf *ScheduleTask) HandleMessage(data []byte) (err error) {
- fmt.Println(string(data))
-
+ logx.Infof("get an message :%s", data)
var tasks = make([]*request.DeliverScheduleTask, 0)
-
err = json.Unmarshal(data, &tasks)
if err != nil {
logx.Errorf("ScheduleTask HandleMessage Unmarshal json err: %v", err.Error())
@@ -46,6 +43,8 @@
}
}
procedureRecord := model.Procedures{
+ StartTime: procedure.StartTime,
+ EndTime: procedure.EndTime,
WorkOrderID: task.WorkOrder.WorkOrderID,
OrderID: task.WorkOrder.OrderID,
Status: model.ProcedureStatusUnFinished,
diff --git a/service/task.go b/service/task.go
index d2f5970..3cb816f 100644
--- a/service/task.go
+++ b/service/task.go
@@ -52,6 +52,7 @@
if err != nil {
return nil, ecode.DBErr
}
+ taskData = new(response.TaskData)
taskData.Order = order
taskData.Procedure = procedure
return taskData, ecode.OK
diff --git a/test/msg_handler_test.go b/test/msg_handler_test.go
new file mode 100644
index 0000000..c6a2c54
--- /dev/null
+++ b/test/msg_handler_test.go
@@ -0,0 +1,139 @@
+package test
+
+import (
+ "apsClient/model/request"
+ "apsClient/nsq"
+ "encoding/json"
+ "fmt"
+ "github.com/shopspring/decimal"
+ "log"
+ "testing"
+ "time"
+)
+
+func TestHandleMessage(t *testing.T) {
+ Init()
+ var tasks = make([]*request.DeliverScheduleTask, 0)
+ startTime, _ := time.ParseInLocation("2006-01-02 15:04", "2023-08-19 08:00", time.Local)
+ endTime, _ := time.ParseInLocation("2006-01-02 15:04", "2023-08-20 12:00", time.Local)
+ fmt.Println(startTime)
+ fmt.Println(startTime.Unix())
+ startTime1, _ := time.ParseInLocation("2006-01-02 15:04", "2023-08-19 08:00", time.Local)
+ endTime1, _ := time.ParseInLocation("2006-01-02 15:04", "2023-08-19 18:00", time.Local)
+
+ startTime2, _ := time.ParseInLocation("2006-01-02 15:04", "2023-08-19 18:00", time.Local)
+ endTime2, _ := time.ParseInLocation("2006-01-02 15:04", "2023-08-20 06:00", time.Local)
+
+ inputMaterials := []*request.ProcedureMaterial{{
+ MaterialID: "MaterialID 1",
+ MaterialName: "杩欐槸涓�涓緭鍏ョ墿鏂欏悕",
+ Amount: decimal.NewFromFloat(100),
+ Unit: "浠�",
+ }}
+ outputMaterials := []*request.ProcedureMaterial{{
+ MaterialID: "MaterialID",
+ MaterialName: "杩欐槸涓�涓緭鍑虹墿鏂欏悕",
+ Amount: decimal.NewFromFloat(20),
+ Unit: "浠�",
+ }}
+ workers := []*request.ProcedureWorker{{
+ WorkerID: "WorkerID 1",
+ WorkerName: "寮犱笁",
+ PhoneNum: "18800000000",
+ StartTime: startTime1.Unix(),
+ EndTime: endTime1.Unix(),
+ },
+ {
+ WorkerID: "WorkerID 2",
+ WorkerName: "鏉庡洓",
+ PhoneNum: "19900000000",
+ StartTime: startTime2.Unix(),
+ EndTime: endTime2.Unix(),
+ }}
+
+ task1 := request.DeliverScheduleTask{
+ WorkOrder: request.WorkOrder{
+ WorkOrderID: "WorkOrderID 1",
+ OrderID: "OrderID 1",
+ ProductID: "ProductID 1",
+ ProductName: "ProductName 1",
+ Parameter: "Parameter 1",
+ Customer: "Customer 1",
+ DeliverDate: "2023-08-19",
+ OrderAttr: "OrderAttr",
+ Amount: decimal.NewFromFloat(4),
+ Unit: "浠�",
+ StartTime: startTime.Unix(),
+ EndTime: endTime.Unix(),
+ },
+ Procedures: []*request.ProductProcedure{{
+ ProcedureID: "ProcedureID 1",
+ ProcedureName: "ProcedureName 1",
+ DeviceID: "DeviceID 1",
+ DeviceName: "DeviceName 1",
+ StartTime: startTime.Unix(),
+ EndTime: endTime.Unix(),
+ WorkHours: decimal.NewFromFloat(4),
+ InputMaterials: inputMaterials,
+ OutputMaterials: outputMaterials,
+ Workers: workers,
+ }, {ProcedureID: "ProcedureID 2",
+ ProcedureName: "ProcedureName 2",
+ DeviceID: "DeviceID 1",
+ DeviceName: "DeviceName 1",
+ StartTime: startTime.Unix(),
+ EndTime: endTime.Unix(),
+ WorkHours: decimal.NewFromFloat(4),
+ InputMaterials: inputMaterials,
+ OutputMaterials: outputMaterials,
+ Workers: workers,
+ }},
+ }
+ task2 := request.DeliverScheduleTask{
+ WorkOrder: request.WorkOrder{
+ WorkOrderID: "WorkOrderID 2",
+ OrderID: "OrderID 2",
+ ProductID: "ProductID 2",
+ ProductName: "ProductName 2",
+ Parameter: "Parameter 2",
+ Customer: "Customer 2",
+ DeliverDate: "2023-08-19",
+ OrderAttr: "OrderAttr",
+ Amount: decimal.NewFromFloat(4),
+ Unit: "浠�",
+ StartTime: startTime.Unix(),
+ EndTime: endTime.Unix(),
+ },
+ Procedures: []*request.ProductProcedure{{
+ ProcedureID: "ProcedureID 3",
+ ProcedureName: "ProcedureName 3",
+ DeviceID: "DeviceID 1",
+ DeviceName: "DeviceName 1",
+ StartTime: startTime.Unix(),
+ EndTime: endTime.Unix(),
+ WorkHours: decimal.NewFromFloat(4),
+ InputMaterials: inputMaterials,
+ OutputMaterials: outputMaterials,
+ Workers: workers,
+ }, {
+ ProcedureID: "ProcedureID 4",
+ ProcedureName: "ProcedureName 4",
+ DeviceID: "DeviceID 1",
+ DeviceName: "DeviceName 1",
+ StartTime: startTime.Unix(),
+ EndTime: endTime.Unix(),
+ WorkHours: decimal.NewFromFloat(4),
+ InputMaterials: inputMaterials,
+ OutputMaterials: outputMaterials,
+ Workers: workers,
+ },
+ },
+ }
+ tasks = append(tasks, &task1, &task2)
+ data, _ := json.Marshal(&tasks)
+ handler := nsq.ScheduleTask{}
+ err := handler.HandleMessage(data)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/test/test.go b/test/test.go
new file mode 100644
index 0000000..62ec57a
--- /dev/null
+++ b/test/test.go
@@ -0,0 +1,23 @@
+package test
+
+import (
+ "apsClient/conf"
+ "apsClient/model"
+ "apsClient/nsq"
+ "apsClient/pkg/logx"
+)
+
+func Init() {
+ logx.Init(conf.Conf.Log)
+ defer logx.Sync()
+
+ if err := model.Init(); err != nil {
+ logx.Errorf("model Init err:%v", err)
+ return
+ }
+
+ if err := nsq.Init(); err != nil {
+ logx.Errorf("nsq Init err:%v", err)
+ return
+ }
+}
--
Gitblit v1.8.0