zhangqian
2023-08-19 8426708ecdf4f25656a04d7b182643b27c188157
增加测试数据
2个文件已添加
12个文件已修改
318 ■■■■■ 已修改文件
api/v1/task.go 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/apsClient.json 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/config.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/procedures.go 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/response/common.go 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/util.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/work_order.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
nsq/msg_handler.go 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/task.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/msg_handler_test.go 139 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/test.go 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
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",
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 {
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": {
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": {
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
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 {
model/response/common.go
@@ -15,3 +15,8 @@
    Order     *model.Order
    Procedure *model.Procedures
}
type ProcessParams struct {
    Key   string
    Value interface{}
}
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()
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 {
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,
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
test/msg_handler_test.go
New file
@@ -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)
    }
}
test/test.go
New file
@@ -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
    }
}