task_json_config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package archive
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. tmod "go-common/app/job/main/videoup-report/model/task"
  7. "go-common/app/job/main/videoup-report/model/utils"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _assignConfigsSQL = "SELECT id,uids,pool,config_mids,config_tids,config_time,adminid,state,stime,etime FROM task_config WHERE state=0"
  14. _delAConfsSQL = "UPDATE task_config SET state=1 WHERE id IN (%s)"
  15. _weightConfSQL = "SELECT id,description,mtime FROM task_weight_config WHERE state=0" // 查
  16. _delWConfsSQL = "UPDATE task_weight_config SET state=1 WHERE id IN (%s)"
  17. )
  18. //AssignConfigs take config
  19. func (d *Dao) AssignConfigs(c context.Context) (tasks map[int64]*tmod.AssignConfig, err error) {
  20. rows, err := d.db.Query(c, _assignConfigsSQL)
  21. if err != nil {
  22. log.Error("d.db.Query error(%v)", err)
  23. return
  24. }
  25. tasks = make(map[int64]*tmod.AssignConfig)
  26. defer rows.Close()
  27. for rows.Next() {
  28. var (
  29. midStr string
  30. uidsStr string
  31. tidStr string
  32. durationStr string
  33. mids []int64
  34. tids []int64
  35. durations []int64
  36. )
  37. t := &tmod.AssignConfig{}
  38. if err = rows.Scan(&t.ID, &uidsStr, &t.Pool, &midStr, &tidStr, &durationStr, &t.AdminID, &t.State, &t.STime, &t.ETime); err != nil {
  39. log.Error("rows.Scan error(%v)", err)
  40. continue
  41. }
  42. if uidsStr != "" {
  43. if t.UIDs, err = xstr.SplitInts(uidsStr); err != nil {
  44. log.Error("xstr.SplitInts(%s) errror(%v)", uidsStr, err)
  45. err = nil
  46. continue
  47. }
  48. }
  49. if midStr != "" {
  50. if mids, err = xstr.SplitInts(midStr); err != nil {
  51. log.Error("xstr.SplitInts(%s) error(%v)", midStr, err)
  52. err = nil
  53. continue
  54. }
  55. t.MIDs = make(map[int64]struct{}, len(mids))
  56. for _, mid := range mids {
  57. t.MIDs[mid] = struct{}{}
  58. }
  59. }
  60. if tidStr != "" {
  61. if tids, err = xstr.SplitInts(tidStr); err != nil {
  62. log.Error("xstr.SplitInts(%s) error(%v)", tidStr, err)
  63. err = nil
  64. continue
  65. }
  66. t.TIDs = make(map[int16]struct{}, len(tids))
  67. for _, tid := range tids {
  68. t.TIDs[int16(tid)] = struct{}{}
  69. }
  70. }
  71. if durationStr != "" {
  72. if durations, err = xstr.SplitInts(durationStr); err != nil || len(durations) != 2 {
  73. log.Error("xstr.SplitInts(%s) error(%v)", durationStr, err)
  74. err = nil
  75. continue
  76. }
  77. t.MinDuration = durations[0]
  78. t.MaxDuration = durations[1]
  79. }
  80. if len(t.UIDs) > 0 {
  81. tasks[t.ID] = t
  82. }
  83. }
  84. return
  85. }
  86. // DelAssignConfs 删除指派配置
  87. func (d *Dao) DelAssignConfs(c context.Context, ids []int64) (rows int64, err error) {
  88. sqlstring := fmt.Sprintf(_delAConfsSQL, xstr.JoinInts(ids))
  89. res, err := d.db.Exec(c, sqlstring)
  90. if err != nil {
  91. log.Error("d.db.Exec(%s) error(%v)", sqlstring, err)
  92. return
  93. }
  94. return res.RowsAffected()
  95. }
  96. // WeightConf 所有有效的配置(用于检测是否和以及有的配置冲突)
  97. func (d *Dao) WeightConf(c context.Context) (items []*tmod.ConfigItem, err error) {
  98. var (
  99. id int64
  100. descb []byte
  101. rows *sql.Rows
  102. wci *tmod.ConfigItem
  103. mtime utils.FormatTime
  104. )
  105. if rows, err = d.db.Query(c, _weightConfSQL); err != nil {
  106. log.Error("d.db.Query(%s) error(%v)", _weightConfSQL, err)
  107. return
  108. }
  109. defer rows.Close()
  110. for rows.Next() {
  111. wci = new(tmod.ConfigItem)
  112. if err = rows.Scan(&id, &descb, &mtime); err != nil {
  113. log.Error("rows.Scan error(%v)", err)
  114. return
  115. }
  116. if err = json.Unmarshal(descb, wci); err != nil {
  117. log.Error("json.Unmarshal(%s) error(%v)", string(descb), err)
  118. continue
  119. }
  120. wci.Mtime = mtime
  121. wci.ID = id
  122. items = append(items, wci)
  123. }
  124. return
  125. }
  126. // DelWeightConfs 删除权重配置
  127. func (d *Dao) DelWeightConfs(c context.Context, ids []int64) (rows int64, err error) {
  128. sqlstring := fmt.Sprintf(_delWConfsSQL, xstr.JoinInts(ids))
  129. res, err := d.db.Exec(c, sqlstring)
  130. if err != nil {
  131. log.Error("d.db.Exec(%s) error(%v)", sqlstring, err)
  132. return
  133. }
  134. return res.RowsAffected()
  135. }