From f5eb41782ed6584664b797df820423f63490d4d3 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 30 十月 2023 12:12:42 +0800 Subject: [PATCH] 按通道号和查询类型查询任务列表 --- api/v1/task.go | 71 +++++++++++++++++++++++++++++++++++ 1 files changed, 71 insertions(+), 0 deletions(-) diff --git a/api/v1/task.go b/api/v1/task.go index 36ab772..cd04286 100644 --- a/api/v1/task.go +++ b/api/v1/task.go @@ -414,3 +414,74 @@ logx.Info("----------------涓嬪彂宸ヨ壓鍙傛暟瀹屾瘯-----------------") return nil } + +// TaskListByChannel +// @Tags Task +// @Summary 鑾峰彇浠诲姟鍒楄〃2 +// @Produce application/json +// @Param object query request.SimpleTaskList true "鏌ヨ鍙傛暟" +// @Success 200 {object} contextx.Response{data=map[int32]response.taskResponse} "鎴愬姛" +// @Router /v1/task/listByChannel [get] +func (slf *TaskApi) TaskListByChannel(c *gin.Context) { + var params request.TaskListByChannel + ctx, ok := contextx.NewContext(c, ¶ms) + if !ok { + return + } + + channelAmount, err := service.NewDevicePlcService().GetDeviceChannelAmount() + if err != nil { + ctx.FailWithMsg(ecode.NeedConfirmedErr, err.Error()) + return + } + + dataMap := make(map[int32]*response.TaskResponse, channelAmount) + if params.Channel != nil { + taskResponse, err := getTaskResponseByChannel(params, *params.Channel) + if err != nil { + ctx.FailWithMsg(ecode.DBErr, err.Error()) + return + } + dataMap[*params.Channel] = taskResponse + ctx.OkWithDetailed(dataMap) + return + } + + //涓嶄紶channel鍙栨墍鏈塩hannel鐨� + var wg sync.WaitGroup + var mu sync.Mutex + for i := 0; i < channelAmount; i++ { + wg.Add(1) + go func(channel int32) { + defer wg.Done() + taskResponse, err := getTaskResponseByChannel(params, channel) + if err != nil { + ctx.FailWithMsg(ecode.DBErr, err.Error()) + return + } + mu.Lock() + defer mu.Unlock() + dataMap[channel] = taskResponse + }(int32(i)) + } + wg.Wait() + ctx.OkWithDetailed(dataMap) +} + +func getTaskResponseByChannel(params request.TaskListByChannel, channel int32) (taskResponse *response.TaskResponse, err error) { + taskResponse, err = service.NewTaskService().GetTask2(params.Offset, params.Limit, []int32{channel}, params.Type) //鍙栬繘琛屼腑鐨勬垨鏈紑濮嬬殑 + if err != nil { + return + } + + nowTs := time.Now().Unix() + flagMap := make(map[int32]struct{}, 0) + for _, task := range taskResponse.Tasks { + if _, ok := flagMap[task.Channel]; !ok && !service.TaskFlagGet(task.Channel) && task.Procedure.StartTime <= nowTs { + task.CanStarted = true + flagMap[task.Channel] = struct{}{} + } + } + taskResponse.Prompt = conf.Conf.Prompt + return +} -- Gitblit v1.8.0