click.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/job/main/click/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _cliSQL = "SELECT aid,web,h5,outside,ios,android,android_tv FROM %s WHERE aid=?"
  11. _addCliSQL = "INSERT INTO %s(aid,web,h5,outside,ios,android,android_tv) VALUES(?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE web=?,h5=?,outside=?,ios=?,android=?,android_tv=?"
  12. _upCliSQL = "UPDATE %s SET web=web+?,h5=h5+?,outside=outside+?,ios=ios+?,android=android+?,android_tv=android_tv+? WHERE aid=?"
  13. _upSpecialCliSQL = "UPDATE %s SET %s=? WHERE aid=?"
  14. )
  15. func getTable(aid int64) string {
  16. return fmt.Sprintf("archive_click_%02d", aid%100)
  17. }
  18. // Click get click
  19. func (d *Dao) Click(c context.Context, aid int64) (cli *model.ClickInfo, err error) {
  20. row := d.db.QueryRow(c, fmt.Sprintf(_cliSQL, getTable(aid)), aid)
  21. cli = &model.ClickInfo{}
  22. if err = row.Scan(&cli.Aid, &cli.Web, &cli.H5, &cli.Outer, &cli.Ios, &cli.Android, &cli.AndroidTV); err != nil {
  23. if err == sql.ErrNoRows {
  24. err = nil
  25. cli = nil
  26. } else {
  27. log.Error("row.Scan error(%v)")
  28. }
  29. }
  30. return
  31. }
  32. // AddClick add av clicks
  33. func (d *Dao) AddClick(c context.Context, aid, web, h5, out, ios, android, androidTV int64) (rows int64, err error) {
  34. res, err := d.db.Exec(c, fmt.Sprintf(_addCliSQL, getTable(aid)), aid, web, h5, out, ios, android, androidTV, web, h5, out, ios, android, androidTV)
  35. if err != nil {
  36. log.Error("d.addCliStmt.Exec(%d) error(%v)", aid, err)
  37. return
  38. }
  39. return res.RowsAffected()
  40. }
  41. // UpClick update av clicks
  42. func (d *Dao) UpClick(c context.Context, cli *model.ClickInfo) (rows int64, err error) {
  43. res, err := d.db.Exec(c, fmt.Sprintf(_upCliSQL, getTable(cli.Aid)), cli.Web, cli.H5, cli.Outer, cli.Ios, cli.Android, cli.AndroidTV, cli.Aid)
  44. if err != nil {
  45. log.Error("d.upCliStmt.Exec(+%v) error(%v)", cli, err)
  46. return
  47. }
  48. return res.RowsAffected()
  49. }
  50. // UpSpecial update special platform click
  51. func (d *Dao) UpSpecial(c context.Context, aid int64, tp string, num int64) (rows int64, err error) {
  52. res, err := d.db.Exec(c, fmt.Sprintf(_upSpecialCliSQL, getTable(aid), tp), num, aid)
  53. if err != nil {
  54. log.Error("d.db.Exec error(%v)", err)
  55. return
  56. }
  57. return res.RowsAffected()
  58. }