From aab066ab2d90d235848f67d4aac3b15c8f778b3c Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期六, 19 八月 2023 18:37:07 +0800 Subject: [PATCH] 增加获取完成进度接口 --- api/v1/task.go | 183 ++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 161 insertions(+), 22 deletions(-) diff --git a/api/v1/task.go b/api/v1/task.go index 196ee69..17c8dba 100644 --- a/api/v1/task.go +++ b/api/v1/task.go @@ -1,47 +1,186 @@ package v1 import ( - "apsClient/model/request" + "apsClient/model" + "apsClient/model/response" _ "apsClient/model/response" "apsClient/pkg/contextx" + "apsClient/pkg/convertx" "apsClient/pkg/ecode" + "apsClient/pkg/logx" "apsClient/service" - "encoding/json" + "fmt" "github.com/gin-gonic/gin" + "time" ) type TaskApi struct{} -// TaskList -// @Tags Base -// @Summary 浠诲姟寮�鍚�氱煡 +// TaskGet +// @Tags Task +// @Summary 鑾峰彇浠诲姟 // @Produce application/json -// @Param object query request.TaskList true "鏌ヨ鍙傛暟" -// @Success 200 {object} contextx.Response{data=[]model.ScheduleTask} "鎴愬姛" -// @Router /v1/task/list [get] -func (slf *TaskApi) TaskList(c *gin.Context) { - var params request.TaskList - ctx, ok := contextx.NewContext(c, ¶ms) +// @Success 200 {object} contextx.Response{data=response.TaskData} "鎴愬姛" +// @Router /v1/task/get [get] +func (slf *TaskApi) TaskGet(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) if !ok { return } - taskList, total, code := service.NewTaskService().GetTaskList(params.Page, params.PageSize) + taskData, code := service.NewTaskService().GetTask() + if code != ecode.OK { + ctx.Fail(code) + return + } + ctx.OkWithDetailed(taskData) +} + +// TaskStart +// @Tags Task +// @Summary 浠诲姟寮�濮� +// @Produce application/json +// @Param id path int true "宸ュ簭id" +// @Success 200 {object} contextx.Response{data=response.ProcessParamsResponse} "鎴愬姛" +// @Router /v1/task/start/{id} [get] +func (slf *TaskApi) TaskStart(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + idx := c.Param("id") + if idx == "" { + ctx.Fail(ecode.ParamsErr) + return + } + id := convertx.Atoi(idx) + procedure, code := service.NewTaskService().GetProcedureById(id) if code != ecode.OK { ctx.Fail(code) return } - for _, task := range taskList { - if task.Data != "" { - err := json.Unmarshal([]byte(task.Data), &task.TaskInfo) - if err != nil { - ctx.Fail(ecode.UnknownErr) - return - } - } - + if procedure.Status != model.ProcedureStatusUnFinished { + ctx.FailWithMsg(ecode.ParamsErr, "璇ュ伐搴忓凡缁撴潫") + return } - ctx.ResultList(taskList, total) + order, err := service.NewTaskService().GetOrderByWorkOrderId(procedure.WorkOrderID) + if err != nil { + ctx.Fail(ecode.UnknownErr) + return + } + + params := service.GetProcessModelParams{ + WorkOrder: procedure.WorkOrderID, + OrderId: procedure.OrderID, + Product: order.ProductName, + Procedure: procedure.ProceduresInfo.ProcedureName, + Device: procedure.ProceduresInfo.DeviceName, + } + + resp, err := service.ProcessModel{}.GetProcessModel(params) + + if err != nil { + logx.Errorf("TaskStart Notice GetProcessModel error: %v", err.Error()) + ctx.FailWithMsg(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, + }) + } + response := response.ProcessParamsResponse{ + Number: resp.Number, + Params: processParamsArr, + } + logx.Infof("TaskStart Notice GetProcessModel: %+v", resp) + ctx.OkWithDetailed(response) +} + +// TaskFinish +// @Tags Task +// @Summary 浠诲姟缁撴潫 +// @Produce application/json +// @Param id path int true "宸ュ簭id" +// @Success 200 {object} contextx.Response{service.GetProcessModel} "鎴愬姛" +// @Router /v1/task/finish/{id} [put] +func (slf *TaskApi) TaskFinish(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + idx := c.Param("id") + if idx == "" { + ctx.Fail(ecode.ParamsErr) + return + } + id := convertx.Atoi(idx) + _, code := service.NewTaskService().GetProcedureById(id) + if code != ecode.OK { + ctx.Fail(code) + return + } + err := service.NewTaskService().UpdateProcedureStatus(id, model.ProcedureStatusFinished) + if err != nil { + logx.Errorf("UpdateProcedureStatus err: %v", err.Error()) + ctx.Fail(ecode.UnknownErr) + return + } + ctx.Ok() +} + +// SendProcessParams +// @Tags Task +// @Summary 涓嬪彂宸ヨ壓鍙傛暟 +// @Produce application/json +// @Param id path int true "宸ュ簭id" +// @Success 200 {object} contextx.Response{service.GetProcessModel} "鎴愬姛" +// @Router /v1/task/sendProcessParams/{id} [post] +func (slf *TaskApi) SendProcessParams(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + idx := c.Param("id") + if idx == "" { + ctx.Fail(ecode.ParamsErr) + return + } + id := convertx.Atoi(idx) + procedure, code := service.NewTaskService().GetProcedureById(id) + if code != ecode.OK { + ctx.Fail(code) + return + } + order, err := service.NewTaskService().GetOrderByWorkOrderId(procedure.WorkOrderID) + if err != nil { + ctx.Fail(ecode.UnknownErr) + return + } + + params := service.GetProcessModelParams{ + WorkOrder: "", + OrderId: procedure.OrderID, + Product: order.ProductName, + Procedure: procedure.ProceduresInfo.ProcedureName, + Device: procedure.ProceduresInfo.DeviceID, + } + + resp, err := service.ProcessModel{}.GetProcessModel(params) + if err != nil { + logx.Errorf("SendProcessModel GetProcessModel err: %v", err.Error()) + ctx.Fail(ecode.UnknownErr) + return + } + fmt.Println("----------------寮�濮嬩笅鍙戝伐鑹哄弬鏁�-----------------") + for k, v := range resp.ParamsMap { + fmt.Println(fmt.Sprintf("%v : %v", k, v)) + time.Sleep(time.Millisecond * 300) + } + fmt.Println("----------------涓嬪彂宸ヨ壓鍙傛暟瀹屾瘯-----------------") + ctx.Ok() } -- Gitblit v1.8.0