| | |
| | | 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.ProcessParams} "成功" |
| | | // @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, |
| | | }) |
| | | } |
| | | logx.Infof("TaskStart Notice GetProcessModel: %+v", resp) |
| | | ctx.OkWithDetailed(processParamsArr) |
| | | } |
| | | |
| | | // 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() |
| | | } |