| | |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "gorm.io/gorm" |
| | | "sort" |
| | | "time" |
| | | ) |
| | | |
| | |
| | | // @Summary 获取任务 |
| | | // @Produce application/json |
| | | // @Param object query request.TaskList true "查询参数" |
| | | // @Success 200 {object} contextx.Response{data=response.TaskData} "成功" |
| | | // @Success 200 {object} contextx.Response{data=response.TaskResponse} "成功" |
| | | // @Router /v1/task/get [get] |
| | | func (slf *TaskApi) TaskGet(c *gin.Context) { |
| | | var params request.TaskList |
| | |
| | | if !ok { |
| | | return |
| | | } |
| | | if params.Page <= 0 { |
| | | params.Page = 1 |
| | | } |
| | | if params.PageSize <= 0 { |
| | | params.PageSize = 1 |
| | | |
| | | channelAmount, err := service.NewDevicePlcService().GetDeviceChannelAmount() |
| | | if err != nil { |
| | | ctx.FailWithMsg(ecode.NeedConfirmedErr, err.Error()) |
| | | return |
| | | } |
| | | |
| | | taskResponse, code := service.NewTaskService().GetTask(params.Page, params.PageSize, service.TaskModeCurrent) //取进行中的或未开始的 |
| | | params.Page = 1 |
| | | |
| | | if params.PageSize <= 0 { |
| | | params.PageSize = channelAmount |
| | | } |
| | | |
| | | taskResponse, code := service.NewTaskService().GetTask(params.Page, params.PageSize, service.TaskModeCurrent, nil) //取进行中的或未开始的 |
| | | if code != ecode.OK { |
| | | ctx.Fail(code) |
| | | return |
| | | } |
| | | if len(taskResponse.Tasks) == 0 { |
| | | taskResponse, code = service.NewTaskService().GetTask(params.Page, params.PageSize, service.TaskModeLastFinished) //取上一个完成的 |
| | | if code != ecode.OK { |
| | | ctx.Fail(code) |
| | | return |
| | | |
| | | if len(taskResponse.Tasks) < channelAmount { |
| | | var existsChannel = make(map[int32]bool, channelAmount) |
| | | for _, task := range taskResponse.Tasks { |
| | | existsChannel[task.Channel] = true |
| | | } |
| | | for i := 0; i < channelAmount; i++ { |
| | | if existsChannel[int32(i)] { |
| | | continue |
| | | } |
| | | taskResponseTemp, code := service.NewTaskService().GetTask(params.Page, 1, service.TaskModeLastFinished, []int32{int32(i)}) //取上一个完成的 |
| | | if code != ecode.OK { |
| | | ctx.Fail(code) |
| | | return |
| | | } |
| | | taskResponse.Tasks = append(taskResponse.Tasks, taskResponseTemp.Tasks...) |
| | | } |
| | | } |
| | | |
| | | sort.Slice(taskResponse.Tasks, func(i, j int) bool { |
| | | return taskResponse.Tasks[i].Channel < taskResponse.Tasks[i].Channel |
| | | }) |
| | | |
| | | for _, task := range taskResponse.Tasks { |
| | | if task.Procedure.Status == model.ProcedureStatusWaitProcess { |
| | | task.CanStarted = true |
| | | } |
| | | } |
| | | |
| | | taskResponse.Prompt = conf.Conf.Prompt |
| | | taskResponse.ChannelAmount = channelAmount |
| | | |
| | | ctx.OkWithDetailed(taskResponse) |
| | | } |
| | |
| | | params.Page = 1 |
| | | } |
| | | if params.PageSize <= 0 { |
| | | params.PageSize = 1 |
| | | params.PageSize = 100 |
| | | } |
| | | |
| | | taskResponse, code := service.NewTaskService().GetTask(params.Page, params.PageSize, service.TaskModeUnStarted) //时间到了未开始的 |
| | | taskResponse, code := service.NewTaskService().GetTask(params.Page, params.PageSize, service.TaskModeUnStarted, nil) //时间到了未开始的 |
| | | if code != ecode.OK { |
| | | ctx.Fail(code) |
| | | return |
| | |
| | | ctx.Fail(code) |
| | | return |
| | | } |
| | | err := service.NewTaskService().UpdateProcedureStatus(nil, id, model.ProcedureStatusFinished, procedure.Position) |
| | | err := service.NewTaskService().UpdateProcedureStatus(nil, id, model.ProcedureStatusFinished, procedure.Channel) |
| | | if err != nil { |
| | | logx.Errorf("UpdateProcedureStatus err: %v", err.Error()) |
| | | ctx.Fail(ecode.UnknownErr) |
| | |
| | | } |
| | | |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = taskService.UpdateProcedureStatusAndPosition(db, params.ProcedureId, model.ProcedureStatusProcessing, params.Position) |
| | | err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusProcessing, params.Channel) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | procedure.Position = params.Position |
| | | procedure.Channel = params.Channel |
| | | err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusProcessing) |
| | | if err != nil { |
| | | return err |
| | |
| | | if err != nil { |
| | | logx.Errorf("SendProcessParams: %v", err.Error()) |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = taskService.UpdateProcedureStatusAndPosition(db, params.ProcedureId, model.ProcedureStatusWaitProcess, params.Position) |
| | | err = taskService.UpdateProcedureStatusAndChannel(db, params.ProcedureId, model.ProcedureStatusWaitProcess, params.Channel) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | procedure.Position = params.Position |
| | | procedure.Channel = params.Channel |
| | | err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusWaitProcess) |
| | | if err != nil { |
| | | return err |
| | |
| | | return |
| | | } |
| | | plcConfig.CurrentTryTimes = 0 |
| | | err = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, params.Position, order.Amount.IntPart()) |
| | | err = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, params.Channel, order.Amount.IntPart()) |
| | | if err != nil { |
| | | ctx.FailWithMsg(ecode.NeedConfirmedErr, "糟糕,工艺下发失败。") |
| | | return |