task_weight.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "go-common/library/xstr"
  8. "strings"
  9. "time"
  10. "go-common/app/admin/main/videoup-task/model"
  11. "go-common/library/cache/redis"
  12. "go-common/library/database/sql"
  13. "go-common/library/log"
  14. )
  15. const (
  16. _getMaxWeightSQL = "SELECT MAX(weight) FROM task_dispatch WHERE state in (0,1)"
  17. _upCwAfterAddSQL = "INSERT INTO `task_dispatch_extend` (`task_id`,`description`) VALUES(?,?) ON DUPLICATE KEY UPDATE description=?"
  18. _inWeightConfSQL = "INSERT INTO task_weight_config(mid,rule,weight,uid,uname,radio,description) VALUES (?,?,?,?,?,?,?)" // 增
  19. _delWeightConfSQL = "UPDATE task_weight_config SET state=1 WHERE id=?" // 软删
  20. _listWeightConfSQL = "SELECT id,uname,state,rule,weight,mtime,description FROM task_weight_config" // 查
  21. _WeightConfSQL = "SELECT id,description FROM task_weight_config WHERE state=0" // 查
  22. _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)"
  23. // archive_config
  24. _confSQL = "SELECT value FROM archive_config WHERE state=0 AND name=?"
  25. _upconfSQL = "UPDATE archive_config SET value=?,remark=? WHERE name=?"
  26. _inconfSQL = "INSERT archive_config(value,remark,name,state) VALUE (?,?,?,0)"
  27. _twexpire = 24 * 60 * 60 // 1 day
  28. )
  29. // GetMaxWeight 获取当前最大权重数值
  30. func (d *Dao) GetMaxWeight(c context.Context) (max int64, err error) {
  31. if err = d.arcDB.QueryRow(c, _getMaxWeightSQL).Scan(&max); err != nil {
  32. log.Error("d.arcDB.QueryRow error(%v)", err)
  33. err = nil
  34. }
  35. return
  36. }
  37. // UpCwAfterAdd update config weight after add config
  38. func (d *Dao) UpCwAfterAdd(c context.Context, id int64, desc string) (rows int64, err error) {
  39. row, err := d.arcDB.Exec(c, _upCwAfterAddSQL, id, desc, desc)
  40. if err != nil {
  41. log.Error("arcDB.Exec(%s,%d,%s,%s) error(%v)", _upCwAfterAddSQL, id, desc, desc, err)
  42. return
  43. }
  44. return row.RowsAffected()
  45. }
  46. // InWeightConf 写入权重配置表
  47. func (d *Dao) InWeightConf(c context.Context, mcases map[int64]*model.WCItem) (err error) {
  48. tx, err := d.arcDB.Begin(c)
  49. if err != nil {
  50. log.Error("db.Begin() error(%v)", err)
  51. return
  52. }
  53. for _, item := range mcases {
  54. var descb []byte
  55. if descb, err = json.Marshal(item); err != nil {
  56. log.Error("json.Marshal(%+v) error(%v)", item, err)
  57. tx.Rollback()
  58. return
  59. }
  60. if _, err = tx.Exec(_inWeightConfSQL, item.CID, item.Rule, item.Weight, item.UID, item.Uname, item.Radio, string(descb)); err != nil {
  61. log.Error("db.Exec(%s) error(%v)", _inWeightConfSQL, err)
  62. tx.Rollback()
  63. return
  64. }
  65. }
  66. if err = tx.Commit(); err != nil {
  67. log.Error("tx.Commit() error(%v)", err)
  68. return
  69. }
  70. return
  71. }
  72. // DelWeightConf 删除权重配置
  73. func (d *Dao) DelWeightConf(c context.Context, id int64) (rows int64, err error) {
  74. res, err := d.arcDB.Exec(c, _delWeightConfSQL, id)
  75. if err != nil {
  76. log.Error("tx.Exec(%s %d) error(%v)", _delWeightConfSQL, id, err)
  77. return
  78. }
  79. return res.RowsAffected()
  80. }
  81. // ListWeightConf 查看权重配置表列表
  82. func (d *Dao) ListWeightConf(c context.Context, cf *model.Confs) (citems []*model.WCItem, err error) {
  83. var (
  84. count int64
  85. rows *sql.Rows
  86. where string
  87. wherecase []string
  88. descb []byte
  89. bt = cf.Bt.TimeValue()
  90. et = cf.Et.TimeValue()
  91. )
  92. if cid := cf.Cid; cid != -1 {
  93. wherecase = append(wherecase, fmt.Sprintf("mid=%d", cid))
  94. }
  95. if operator := cf.Operator; len(operator) > 0 {
  96. wherecase = append(wherecase, fmt.Sprintf("uname='%s'", operator))
  97. }
  98. if rule := cf.Rule; rule != -1 {
  99. wherecase = append(wherecase, fmt.Sprintf("rule=%d", rule))
  100. }
  101. wherecase = append(wherecase, fmt.Sprintf("radio=%d AND state=%d", cf.Radio, cf.State))
  102. where = "WHERE " + strings.Join(wherecase, " AND ")
  103. sqlstring := fmt.Sprintf("%s %s LIMIT %d,%d", _listWeightConfSQL, where, (cf.Pn-1)*cf.Ps, cf.Pn*cf.Ps)
  104. rows, err = d.arcDB.Query(c, sqlstring)
  105. if err != nil {
  106. log.Error("d.arcDB.Query(%s) error(%v)", sqlstring, err)
  107. return
  108. }
  109. defer rows.Close()
  110. for rows.Next() {
  111. wci := &model.WCItem{}
  112. if err = rows.Scan(&wci.ID, &wci.Uname, &wci.State, &wci.Rule, &wci.Weight, &wci.Mtime, &descb); err != nil {
  113. log.Error("rows.Scan(%s) error(%v)", sqlstring, err)
  114. return
  115. }
  116. if len(descb) > 0 {
  117. if err = json.Unmarshal(descb, wci); err != nil {
  118. log.Error("json.Unmarshal(%s) error(%v)", string(descb), err)
  119. err = nil
  120. continue
  121. }
  122. eti := wci.Et.TimeValue()
  123. // filter time
  124. if !et.IsZero() && !bt.IsZero() && (bt.After(wci.Mtime.TimeValue()) || et.Before(wci.Mtime.TimeValue())) {
  125. log.Info("config expired (%+v) parse et(%v)", wci, et)
  126. continue
  127. }
  128. // filter state
  129. if cf.State == 0 && !eti.IsZero() && eti.Before(time.Now()) {
  130. log.Info("config expired (%+v) parse et(%v)", wci, eti)
  131. continue
  132. }
  133. }
  134. if count > 50 {
  135. break
  136. }
  137. count++
  138. citems = append(citems, wci)
  139. }
  140. return
  141. }
  142. // WeightConf 所有有效的配置(用于检测是否和已有的配置冲突)
  143. func (d *Dao) WeightConf(c context.Context) (items []*model.WCItem, err error) {
  144. var (
  145. id int64
  146. descb []byte
  147. rows *sql.Rows
  148. wci *model.WCItem
  149. )
  150. if rows, err = d.arcDB.Query(c, _WeightConfSQL); err != nil {
  151. log.Error("d.arcDB.Query(%s) error(%v)", _WeightConfSQL, err)
  152. return
  153. }
  154. defer rows.Close()
  155. for rows.Next() {
  156. wci = new(model.WCItem)
  157. if err = rows.Scan(&id, &descb); err != nil {
  158. log.Error("rows.Scan error(%v)", err)
  159. return
  160. }
  161. if err = json.Unmarshal(descb, wci); err != nil {
  162. log.Error("json.Unmarshal(%s) error(%v)", string(descb), err)
  163. err = nil
  164. continue
  165. }
  166. wci.ID = id
  167. items = append(items, wci)
  168. }
  169. return
  170. }
  171. // LWConfigHelp 补充任务对应稿件的title和filename
  172. func (d *Dao) LWConfigHelp(c context.Context, ids []int64) (res map[int64][]interface{}, err error) {
  173. var (
  174. taskid, vid int64
  175. filename, title string
  176. rows *sql.Rows
  177. )
  178. rows, err = d.arcDB.Query(c, fmt.Sprintf(_lwconfigHelpSQL, xstr.JoinInts(ids)))
  179. if err != nil {
  180. log.Error("d.arcDB.Query(%v) error(%v)", ids, err)
  181. return
  182. }
  183. defer rows.Close()
  184. res = make(map[int64][]interface{})
  185. for rows.Next() {
  186. err = rows.Scan(&taskid, &vid, &title, &filename)
  187. if err != nil {
  188. log.Error("rows.Scan error(%v)", err)
  189. continue
  190. }
  191. res[taskid] = []interface{}{filename, title, vid}
  192. }
  193. return
  194. }
  195. func key(id int64) string {
  196. return fmt.Sprintf("tw_%d", id)
  197. }
  198. //SetWeightRedis 设置权重配置
  199. func (d *Dao) SetWeightRedis(c context.Context, mcases map[int64]*model.TaskPriority) (err error) {
  200. conn := d.redis.Get(c)
  201. defer conn.Close()
  202. for tid, mcase := range mcases {
  203. var bs []byte
  204. key := key(tid)
  205. if bs, err = json.Marshal(mcase); err != nil {
  206. log.Error("json.Marshal(%+v) error(%v)", mcase, err)
  207. continue
  208. }
  209. if err = conn.Send("SET", key, bs); err != nil {
  210. log.Error("SET error(%v)", err)
  211. continue
  212. }
  213. if err = conn.Send("EXPIRE", key, _twexpire); err != nil {
  214. log.Error("EXPIRE error(%v)", err)
  215. continue
  216. }
  217. }
  218. if err = conn.Flush(); err != nil {
  219. log.Error("conn.Flush error(%v)", err)
  220. return
  221. }
  222. for i := 0; i < 2*len(mcases); i++ {
  223. if _, err = conn.Receive(); err != nil {
  224. log.Error("conn.Receive() error(%v)", err)
  225. return
  226. }
  227. }
  228. return
  229. }
  230. //GetWeightRedis 获取实时任务的权重配置
  231. func (d *Dao) GetWeightRedis(c context.Context, ids []int64) (mcases map[int64]*model.TaskPriority, err error) {
  232. conn := d.redis.Get(c)
  233. defer conn.Close()
  234. mcases = make(map[int64]*model.TaskPriority)
  235. for _, id := range ids {
  236. var bs []byte
  237. key := key(int64(id))
  238. if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  239. if err == redis.ErrNil {
  240. err = nil
  241. } else {
  242. log.Error("conn.Do(GET, %v) error(%v)", key, err)
  243. }
  244. continue
  245. }
  246. p := &model.TaskPriority{}
  247. if err = json.Unmarshal(bs, p); err != nil {
  248. log.Error("json.Unmarshal(%s) error(%v)", string(bs), err)
  249. err = nil
  250. continue
  251. }
  252. mcases[int64(id)] = p
  253. }
  254. return
  255. }
  256. // WeightVC 获取权重分值
  257. func (d *Dao) WeightVC(c context.Context) (wvc *model.WeightVC, err error) {
  258. var value []byte
  259. row := d.arcDB.QueryRow(c, _confSQL, model.ConfForWeightVC)
  260. if err = row.Scan(&value); err != nil {
  261. if err == sql.ErrNoRows {
  262. err = nil
  263. } else {
  264. log.Error("row.Scan error(%v)", err)
  265. }
  266. return
  267. }
  268. wvc = new(model.WeightVC)
  269. if err = json.Unmarshal(value, wvc); err != nil {
  270. log.Error("json.Unmarshal error(%v)", err)
  271. wvc = nil
  272. }
  273. return
  274. }
  275. // SetWeightVC 设置权重分值
  276. func (d *Dao) SetWeightVC(c context.Context, wvc *model.WeightVC, desc string) (rows int64, err error) {
  277. var (
  278. valueb []byte
  279. res xsql.Result
  280. )
  281. if valueb, err = json.Marshal(wvc); err != nil {
  282. log.Error("json.Marshal(%+v) error(%v)", wvc, err)
  283. return
  284. }
  285. if res, err = d.arcDB.Exec(c, _upconfSQL, string(valueb), desc, model.ConfForWeightVC); err != nil {
  286. log.Error("d.arcDB.Exec(%s, %s, %s, %s) error(%v)", _upconfSQL, string(valueb), desc, model.ConfForWeightVC, err)
  287. return
  288. }
  289. return res.RowsAffected()
  290. }
  291. // InWeightVC 插入
  292. func (d *Dao) InWeightVC(c context.Context, wvc *model.WeightVC, desc string) (rows int64, err error) {
  293. var (
  294. valueb []byte
  295. res xsql.Result
  296. )
  297. if valueb, err = json.Marshal(wvc); err != nil {
  298. log.Error("json.Marshal(%+v) error(%v)", wvc, err)
  299. return
  300. }
  301. if res, err = d.arcDB.Exec(c, _inconfSQL, string(valueb), desc, model.ConfForWeightVC); err != nil {
  302. log.Error("d.arcDB.Exec(%s, %s, %s, %s) error(%v)", _inconfSQL, string(valueb), desc, model.ConfForWeightVC, err)
  303. return
  304. }
  305. return res.LastInsertId()
  306. }