| | |
| | | |
| | | import ( |
| | | "apsClient/model" |
| | | "apsClient/model/response" |
| | | _ "apsClient/model/response" |
| | | "apsClient/pkg/contextx" |
| | | "apsClient/pkg/convertx" |
| | |
| | | // @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) |
| | |
| | | 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 |
| | |
| | | "LimitTimeIP": 3600, |
| | | "RouterPrefix": "api", |
| | | "SudoPassword": "basic2021", |
| | | "deviceId": "MA-JWW-1" |
| | | "deviceId": "DeviceID 1" |
| | | }, |
| | | "log": { |
| | | "path": "./logs/apsClient.log", |
| | |
| | | "apsClient/pkg/mysqlx" |
| | | "apsClient/pkg/redisx" |
| | | "apsClient/pkg/sqlitex" |
| | | "flag" |
| | | "github.com/spf13/viper" |
| | | "log" |
| | | ) |
| | |
| | | ) |
| | | |
| | | 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 { |
| | |
| | | "200": { |
| | | "description": "成功", |
| | | "schema": { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | }, |
| | | { |
| | | "type": "object", |
| | | "properties": { |
| | | "data": { |
| | | "type": "array", |
| | | "items": { |
| | | "$ref": "#/definitions/response.ProcessParams" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | ] |
| | | } |
| | | } |
| | | } |
| | |
| | | }, |
| | | "unit": { |
| | | "type": "string" |
| | | }, |
| | | "workOrderId": { |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | |
| | | "request.ProcedureWorker": { |
| | | "type": "object", |
| | | "properties": { |
| | | "endTime": { |
| | | "type": "integer" |
| | | }, |
| | | "phoneNum": { |
| | | "type": "string" |
| | | }, |
| | | "startTime": { |
| | | "type": "integer" |
| | | }, |
| | | "workerId": { |
| | | "type": "string" |
| | |
| | | "type": "object", |
| | | "properties": { |
| | | "deviceId": { |
| | | "type": "string" |
| | | }, |
| | | "deviceName": { |
| | | "type": "string" |
| | | }, |
| | | "endTime": { |
| | |
| | | } |
| | | } |
| | | }, |
| | | "response.ProcessParams": { |
| | | "type": "object", |
| | | "properties": { |
| | | "key": { |
| | | "type": "string" |
| | | }, |
| | | "value": {} |
| | | } |
| | | }, |
| | | "response.TaskData": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | "200": { |
| | | "description": "成功", |
| | | "schema": { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | }, |
| | | { |
| | | "type": "object", |
| | | "properties": { |
| | | "data": { |
| | | "type": "array", |
| | | "items": { |
| | | "$ref": "#/definitions/response.ProcessParams" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | ] |
| | | } |
| | | } |
| | | } |
| | |
| | | }, |
| | | "unit": { |
| | | "type": "string" |
| | | }, |
| | | "workOrderId": { |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | |
| | | "request.ProcedureWorker": { |
| | | "type": "object", |
| | | "properties": { |
| | | "endTime": { |
| | | "type": "integer" |
| | | }, |
| | | "phoneNum": { |
| | | "type": "string" |
| | | }, |
| | | "startTime": { |
| | | "type": "integer" |
| | | }, |
| | | "workerId": { |
| | | "type": "string" |
| | |
| | | "type": "object", |
| | | "properties": { |
| | | "deviceId": { |
| | | "type": "string" |
| | | }, |
| | | "deviceName": { |
| | | "type": "string" |
| | | }, |
| | | "endTime": { |
| | |
| | | } |
| | | } |
| | | }, |
| | | "response.ProcessParams": { |
| | | "type": "object", |
| | | "properties": { |
| | | "key": { |
| | | "type": "string" |
| | | }, |
| | | "value": {} |
| | | } |
| | | }, |
| | | "response.TaskData": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | type: integer |
| | | unit: |
| | | type: string |
| | | workOrderId: |
| | | type: string |
| | | type: object |
| | | model.Procedures: |
| | | properties: |
| | |
| | | type: object |
| | | request.ProcedureWorker: |
| | | properties: |
| | | endTime: |
| | | type: integer |
| | | phoneNum: |
| | | type: string |
| | | startTime: |
| | | type: integer |
| | | workerId: |
| | | type: string |
| | | workerName: |
| | |
| | | request.ProductProcedure: |
| | | properties: |
| | | deviceId: |
| | | type: string |
| | | deviceName: |
| | | type: string |
| | | endTime: |
| | | type: integer |
| | |
| | | items: |
| | | $ref: '#/definitions/request.ProcedureWorker' |
| | | type: array |
| | | type: object |
| | | response.ProcessParams: |
| | | properties: |
| | | key: |
| | | type: string |
| | | value: {} |
| | | type: object |
| | | response.TaskData: |
| | | properties: |
| | |
| | | "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 |
| | |
| | | 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 对象 |
| | | } |
| | |
| | | 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 { |
| | |
| | | Order *model.Order |
| | | Procedure *model.Procedures |
| | | } |
| | | |
| | | type ProcessParams struct { |
| | | Key string |
| | | Value interface{} |
| | | } |
| | |
| | | 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() |
| | |
| | | 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 { |
| | |
| | | "apsClient/pkg/logx" |
| | | "apsClient/pkg/structx" |
| | | "encoding/json" |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | |
| | | } |
| | | |
| | | 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()) |
| | |
| | | } |
| | | } |
| | | procedureRecord := model.Procedures{ |
| | | StartTime: procedure.StartTime, |
| | | EndTime: procedure.EndTime, |
| | | WorkOrderID: task.WorkOrder.WorkOrderID, |
| | | OrderID: task.WorkOrder.OrderID, |
| | | Status: model.ProcedureStatusUnFinished, |
| | |
| | | if err != nil { |
| | | return nil, ecode.DBErr |
| | | } |
| | | taskData = new(response.TaskData) |
| | | taskData.Order = order |
| | | taskData.Procedure = procedure |
| | | return taskData, ecode.OK |
New file |
| | |
| | | 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) |
| | | } |
| | | } |
New file |
| | |
| | | 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 |
| | | } |
| | | } |