task_weight_config.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package archive
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/library/xstr"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/videoup/model/archive"
  10. "go-common/library/database/sql"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _getMaxWeightSQL = "SELECT MAX(weight) FROM task_dispatch WHERE state in (0,1)"
  15. _upCwAfterAddSQL = "INSERT INTO `task_dispatch_extend` (`task_id`,`description`) VALUES(?,?) ON DUPLICATE KEY UPDATE description=?"
  16. _inWeightConfSQL = "INSERT INTO task_weight_config(mid,rule,weight,uid,uname,radio,description) VALUES (?,?,?,?,?,?,?)" // 增
  17. _delWeightConfSQL = "UPDATE task_weight_config SET state=1 WHERE id=?" // 软删
  18. _listWeightConfSQL = "SELECT id,uname,state,rule,weight,mtime,description FROM task_weight_config" // 查
  19. _WeightConfSQL = "SELECT id,description FROM task_weight_config WHERE state=0" // 查
  20. _lwconfigHelpSQL = "SELECT t.id,t.cid,a.title,v.filename FROM task_dispatch t INNER JOIN archive a ON t.aid=a.id INNER JOIN archive_video v ON t.cid=v.cid WHERE t.id IN (%s)"
  21. )
  22. // GetMaxWeight 获取当前最大权重数值
  23. func (d *Dao) GetMaxWeight(c context.Context) (max int64, err error) {
  24. if err = d.rddb.QueryRow(c, _getMaxWeightSQL).Scan(&max); err != nil {
  25. log.Error("d.rddb.QueryRow error(%v)", err)
  26. err = nil
  27. }
  28. return
  29. }
  30. // UpCwAfterAdd update config weight after add config
  31. func (d *Dao) UpCwAfterAdd(c context.Context, id int64, desc string) (rows int64, err error) {
  32. row, err := d.db.Exec(c, _upCwAfterAddSQL, id, desc, desc)
  33. if err != nil {
  34. log.Error("db.Exec(%s,%d,%s,%s) error(%v)", _upCwAfterAddSQL, id, desc, desc, err)
  35. return
  36. }
  37. return row.RowsAffected()
  38. }
  39. // InWeightConf 写入权重配置表
  40. func (d *Dao) InWeightConf(c context.Context, mcases map[int64]*archive.WCItem) (err error) {
  41. tx, err := d.db.Begin(c)
  42. if err != nil {
  43. log.Error("db.Begin() error(%v)", err)
  44. return
  45. }
  46. for _, item := range mcases {
  47. var descb []byte
  48. if descb, err = json.Marshal(item); err != nil {
  49. log.Error("json.Marshal(%+v) error(%v)", item, err)
  50. tx.Rollback()
  51. return
  52. }
  53. if _, err = tx.Exec(_inWeightConfSQL, item.CID, item.Rule, item.Weight, item.UID, item.Uname, item.Radio, string(descb)); err != nil {
  54. log.Error("db.Exec(%s) error(%v)", _inWeightConfSQL, err)
  55. tx.Rollback()
  56. return
  57. }
  58. }
  59. if err = tx.Commit(); err != nil {
  60. log.Error("tx.Commit() error(%v)", err)
  61. return
  62. }
  63. return
  64. }
  65. // DelWeightConf 删除权重配置
  66. func (d *Dao) DelWeightConf(c context.Context, id int64) (rows int64, err error) {
  67. res, err := d.db.Exec(c, _delWeightConfSQL, id)
  68. if err != nil {
  69. log.Error("tx.Exec(%s %d) error(%v)", _delWeightConfSQL, id, err)
  70. return
  71. }
  72. return res.RowsAffected()
  73. }
  74. // ListWeightConf 查看权重配置表列表
  75. func (d *Dao) ListWeightConf(c context.Context, cf *archive.Confs) (citems []*archive.WCItem, err error) {
  76. var (
  77. count int64
  78. rows *sql.Rows
  79. where string
  80. wherecase []string
  81. descb []byte
  82. bt = cf.Bt.TimeValue()
  83. et = cf.Et.TimeValue()
  84. )
  85. if cid := cf.Cid; cid != -1 {
  86. wherecase = append(wherecase, fmt.Sprintf("mid=%d", cid))
  87. }
  88. if operator := cf.Operator; len(operator) > 0 {
  89. wherecase = append(wherecase, fmt.Sprintf("uname='%s'", operator))
  90. }
  91. if rule := cf.Rule; rule != -1 {
  92. wherecase = append(wherecase, fmt.Sprintf("rule=%d", rule))
  93. }
  94. wherecase = append(wherecase, fmt.Sprintf("radio=%d AND state=%d", cf.Radio, cf.State))
  95. where = "WHERE " + strings.Join(wherecase, " AND ")
  96. sqlstring := fmt.Sprintf("%s %s LIMIT %d,%d", _listWeightConfSQL, where, (cf.Pn-1)*cf.Ps, cf.Pn*cf.Ps)
  97. rows, err = d.rddb.Query(c, sqlstring)
  98. if err != nil {
  99. log.Error("d.rddb.Query(%s) error(%v)", sqlstring, err)
  100. return
  101. }
  102. defer rows.Close()
  103. for rows.Next() {
  104. wci := &archive.WCItem{}
  105. if err = rows.Scan(&wci.ID, &wci.Uname, &wci.State, &wci.Rule, &wci.Weight, &wci.Mtime, &descb); err != nil {
  106. log.Error("rows.Scan(%s) error(%v)", sqlstring, err)
  107. return
  108. }
  109. if len(descb) > 0 {
  110. if err = json.Unmarshal(descb, wci); err != nil {
  111. log.Error("json.Unmarshal(%s) error(%v)", string(descb), err)
  112. err = nil
  113. continue
  114. }
  115. eti := wci.Et.TimeValue()
  116. // filter time
  117. if !et.IsZero() && !bt.IsZero() && (bt.After(wci.Mtime.TimeValue()) || et.Before(wci.Mtime.TimeValue())) {
  118. log.Info("config expired (%+v) parse et(%v)", wci, et)
  119. continue
  120. }
  121. // filter state
  122. if cf.State == 0 && !eti.IsZero() && eti.Before(time.Now()) {
  123. log.Info("config expired (%+v) parse et(%v)", wci, eti)
  124. continue
  125. }
  126. }
  127. if count > 50 {
  128. break
  129. }
  130. count++
  131. citems = append(citems, wci)
  132. }
  133. return
  134. }
  135. // WeightConf 所有有效的配置(用于检测是否和已有的配置冲突)
  136. func (d *Dao) WeightConf(c context.Context) (items []*archive.WCItem, err error) {
  137. var (
  138. id int64
  139. descb []byte
  140. rows *sql.Rows
  141. wci *archive.WCItem
  142. )
  143. if rows, err = d.rddb.Query(c, _WeightConfSQL); err != nil {
  144. log.Error("d.rddb.Query(%s) error(%v)", _WeightConfSQL, err)
  145. return
  146. }
  147. defer rows.Close()
  148. for rows.Next() {
  149. wci = new(archive.WCItem)
  150. if err = rows.Scan(&id, &descb); err != nil {
  151. log.Error("rows.Scan error(%v)", err)
  152. return
  153. }
  154. if err = json.Unmarshal(descb, wci); err != nil {
  155. log.Error("json.Unmarshal(%s) error(%v)", string(descb), err)
  156. err = nil
  157. continue
  158. }
  159. wci.ID = id
  160. items = append(items, wci)
  161. }
  162. return
  163. }
  164. // LWConfigHelp 补充任务对应稿件的title和filename
  165. func (d *Dao) LWConfigHelp(c context.Context, ids []int64) (res map[int64][]interface{}, err error) {
  166. var (
  167. taskid, vid int64
  168. filename, title string
  169. rows *sql.Rows
  170. )
  171. rows, err = d.rddb.Query(c, fmt.Sprintf(_lwconfigHelpSQL, xstr.JoinInts(ids)))
  172. if err != nil {
  173. log.Error("d.db.Query(%v) error(%v)", ids, err)
  174. return
  175. }
  176. defer rows.Close()
  177. res = make(map[int64][]interface{})
  178. for rows.Next() {
  179. err = rows.Scan(&taskid, &vid, &title, &filename)
  180. if err != nil {
  181. log.Error("rows.Scan error(%v)", err)
  182. continue
  183. }
  184. res[taskid] = []interface{}{filename, title, vid}
  185. }
  186. return
  187. }