task_repository.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package dao
  2. import (
  3. "Zythum/common"
  4. "database/sql"
  5. _ "github.com/go-sql-driver/mysql"
  6. "log"
  7. )
  8. func GetNewTask() (common.Task, error) {
  9. tx, err := db.Begin()
  10. committed := false
  11. if err != nil {
  12. log.Println("Failed to begin transaction:", err)
  13. return common.Task{ID: -1, Path: ""}, err
  14. }
  15. defer func(tx *sql.Tx) {
  16. if !committed {
  17. err := tx.Rollback()
  18. if err != nil {
  19. log.Println("Failed to rollback transaction:", err)
  20. }
  21. }
  22. }(tx)
  23. rows, err := tx.Query("SELECT id, path FROM financial_reports WHERE status = 0 ORDER BY id LIMIT 1 FOR UPDATE;")
  24. if err != nil {
  25. return common.Task{ID: -1, Path: ""}, err
  26. }
  27. var task common.Task
  28. rows.Next()
  29. err = rows.Scan(&task.ID, &task.Path)
  30. if err != nil {
  31. return common.Task{ID: -2, Path: ""}, err
  32. }
  33. err = rows.Close()
  34. if err != nil {
  35. log.Println("Failed to close rows:", err)
  36. }
  37. _, err = tx.Exec("UPDATE financial_reports SET status = 1 WHERE id = ?", task.ID)
  38. if err != nil {
  39. return common.Task{ID: -3, Path: ""}, err
  40. }
  41. if err := tx.Commit(); err != nil {
  42. return common.Task{ID: -4, Path: ""}, err
  43. } else {
  44. committed = true
  45. }
  46. return task, nil
  47. }
  48. func UpdateTaskStatus(id int, status int) int {
  49. _, err := db.Exec("UPDATE financial_reports SET status = ? WHERE id = ?;", status, id)
  50. if err != nil {
  51. log.Println("Database update error:", err)
  52. return -1
  53. }
  54. return 0
  55. }
  56. func GetTaskStatus() common.TaskStatus {
  57. var taskStatus common.TaskStatus
  58. err := db.QueryRow(
  59. "SELECT (SELECT COUNT(id) FROM financial_reports WHERE status = 0) AS ready_task_count, "+
  60. "(SELECT COUNT(id) FROM financial_reports WHERE status = 1) AS pending_task_count, "+
  61. "(SELECT COUNT(id) FROM financial_reports WHERE status = 2) AS completed_task_count, "+
  62. "(SELECT COUNT(id) FROM financial_reports WHERE status = 3) AS failed_task_count;").Scan(&taskStatus.ReadyTaskCount, &taskStatus.PendingTaskCount, &taskStatus.CompletedTaskCount, &taskStatus.FailedTaskCount)
  63. if err != nil {
  64. log.Println("Database query error:", err)
  65. return common.TaskStatus{}
  66. }
  67. return taskStatus
  68. }
  69. func getTasksByStatus(status int) {
  70. rows, err := db.Query("SELECT id, path FROM financial_reports WHERE status = ?;", status)
  71. if err != nil {
  72. log.Println("Database query error:", err)
  73. return
  74. }
  75. for rows.Next() {
  76. var task common.Task
  77. err := rows.Scan(&task.ID, &task.Path)
  78. if err != nil {
  79. log.Println("Database scan error:", err)
  80. return
  81. }
  82. log.Println("Task:", task)
  83. }
  84. defer rows.Close()
  85. }