task.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package controller
  2. import (
  3. "strconv"
  4. "github.com/QuantumNous/new-api/common"
  5. "github.com/QuantumNous/new-api/constant"
  6. "github.com/QuantumNous/new-api/dto"
  7. "github.com/QuantumNous/new-api/model"
  8. "github.com/QuantumNous/new-api/relay"
  9. "github.com/QuantumNous/new-api/service"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // UpdateTaskBulk 薄入口,实际轮询逻辑在 service 层
  13. func UpdateTaskBulk() {
  14. service.TaskPollingLoop()
  15. }
  16. func GetAllTask(c *gin.Context) {
  17. pageInfo := common.GetPageQuery(c)
  18. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  19. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  20. // 解析其他查询参数
  21. queryParams := model.SyncTaskQueryParams{
  22. Platform: constant.TaskPlatform(c.Query("platform")),
  23. TaskID: c.Query("task_id"),
  24. Status: c.Query("status"),
  25. Action: c.Query("action"),
  26. StartTimestamp: startTimestamp,
  27. EndTimestamp: endTimestamp,
  28. ChannelID: c.Query("channel_id"),
  29. }
  30. items := model.TaskGetAllTasks(pageInfo.GetStartIdx(), pageInfo.GetPageSize(), queryParams)
  31. total := model.TaskCountAllTasks(queryParams)
  32. pageInfo.SetTotal(int(total))
  33. pageInfo.SetItems(tasksToDto(items))
  34. common.ApiSuccess(c, pageInfo)
  35. }
  36. func GetUserTask(c *gin.Context) {
  37. pageInfo := common.GetPageQuery(c)
  38. userId := c.GetInt("id")
  39. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  40. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  41. queryParams := model.SyncTaskQueryParams{
  42. Platform: constant.TaskPlatform(c.Query("platform")),
  43. TaskID: c.Query("task_id"),
  44. Status: c.Query("status"),
  45. Action: c.Query("action"),
  46. StartTimestamp: startTimestamp,
  47. EndTimestamp: endTimestamp,
  48. }
  49. items := model.TaskGetAllUserTask(userId, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), queryParams)
  50. total := model.TaskCountAllUserTask(userId, queryParams)
  51. pageInfo.SetTotal(int(total))
  52. pageInfo.SetItems(tasksToDto(items))
  53. common.ApiSuccess(c, pageInfo)
  54. }
  55. func tasksToDto(tasks []*model.Task) []*dto.TaskDto {
  56. result := make([]*dto.TaskDto, len(tasks))
  57. for i, task := range tasks {
  58. result[i] = relay.TaskModel2Dto(task)
  59. }
  60. return result
  61. }