addit.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package archive
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/videoup/model/archive"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _inAddRdrSQL = "INSERT INTO archive_addit (aid,redirect_url) VALUES (?,?) ON DUPLICATE KEY UPDATE redirect_url=?"
  12. _upAdditSQL = "UPDATE archive_addit SET mission_id=?,source=?,description=?,dynamic=? WHERE aid=?"
  13. _AdditSQL = "SELECT aid,mission_id,from_ip,up_from,recheck_reason,redirect_url,source,order_id,desc_format_id,dynamic,inner_attr FROM archive_addit WHERE aid=?"
  14. _additBatch = "SELECT aid,mission_id,from_ip,up_from,recheck_reason,redirect_url,source,order_id,desc_format_id,dynamic,inner_attr FROM archive_addit WHERE aid IN (%s)"
  15. _inAdditInnerAttrSQL = "INSERT INTO archive_addit (aid, inner_attr) VALUES (?,?) ON DUPLICATE KEY UPDATE inner_attr=?"
  16. )
  17. // TxUpAdditRedirect update archive redirect url.
  18. func (d *Dao) TxUpAdditRedirect(tx *xsql.Tx, aid int64, redirectURL string) (rows int64, err error) {
  19. res, err := tx.Exec(_inAddRdrSQL, aid, redirectURL, redirectURL)
  20. if err != nil {
  21. log.Error("d._inAdditRedirect.Exec() error(%v)", err)
  22. return
  23. }
  24. rows, err = res.RowsAffected()
  25. return
  26. }
  27. // TxUpAddit update archive_addit mission_id && source by aid.
  28. func (d *Dao) TxUpAddit(tx *xsql.Tx, aid, missionID int64, source, description, dynamic string) (rows int64, err error) {
  29. res, err := tx.Exec(_upAdditSQL, missionID, source, description, dynamic, aid)
  30. if err != nil {
  31. log.Error("d.TxUpAddit.tx.Exec error(%v)", err)
  32. return
  33. }
  34. rows, err = res.RowsAffected()
  35. return
  36. }
  37. // Addit get a archive addit by avid.
  38. func (d *Dao) Addit(c context.Context, aid int64) (ad *archive.Addit, err error) {
  39. row := d.rddb.QueryRow(c, _AdditSQL, aid)
  40. ad = &archive.Addit{}
  41. if err = row.Scan(&ad.Aid, &ad.MissionID, &ad.FromIP, &ad.UpFrom, &ad.RecheckReason, &ad.RedirectURL, &ad.Source, &ad.OrderID, &ad.DescFormatID, &ad.Dynamic, &ad.InnerAttr); err != nil {
  42. if err == xsql.ErrNoRows {
  43. err = nil
  44. } else {
  45. log.Error("row.Scan error(%v)", err)
  46. }
  47. }
  48. return
  49. }
  50. //TxUpInnerAttr update archive inner_attr
  51. func (d *Dao) TxUpInnerAttr(tx *xsql.Tx, aid int64, attr int64) (id int64, err error) {
  52. res, err := tx.Exec(_inAdditInnerAttrSQL, aid, attr, attr)
  53. if err != nil {
  54. log.Error("TxUpInnerAttr tx.Exec error(%v) aid(%d) attr(%d)", err, aid, attr)
  55. return 0, err
  56. }
  57. return res.LastInsertId()
  58. }
  59. // ArcStateMap get archive id and state map
  60. func (d *Dao) AdditBatch(c context.Context, aids []int64) (sMap map[int64]*archive.Addit, err error) {
  61. sMap = make(map[int64]*archive.Addit)
  62. if len(aids) == 0 {
  63. return
  64. }
  65. rows, err := d.rddb.Query(c, fmt.Sprintf(_additBatch, xstr.JoinInts(aids)))
  66. if err != nil {
  67. log.Error("db.Query() error(%v)", err)
  68. return
  69. }
  70. defer rows.Close()
  71. for rows.Next() {
  72. ad := &archive.Addit{}
  73. if err = rows.Scan(&ad.Aid, &ad.MissionID, &ad.FromIP, &ad.UpFrom, &ad.RecheckReason, &ad.RedirectURL, &ad.Source, &ad.OrderID, &ad.DescFormatID, &ad.Dynamic, &ad.InnerAttr); err != nil {
  74. log.Error("rows.Scan error(%v)", err)
  75. return
  76. }
  77. sMap[ad.Aid] = ad
  78. }
  79. return
  80. }