offer.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package offer
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. "go-common/app/interface/main/app-wall/conf"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. //offer
  12. _inClkSQL = "INSERT IGNORE INTO wall_click (ip,cid,mac,idfa,cb,ctime,mtime) VALUES(?,?,?,?,?,?,?)"
  13. _inActSQL = "INSERT IGNORE INTO wall_active (ip,mid,rmac,mac,idfa,device,ctime,mtime) VALUES(?,?,?,?,?,?,?,?)"
  14. _upActIdfaSQL = "UPDATE wall_click SET idfa_active=?,mtime=? WHERE idfa=?"
  15. _cbSQL = "SELECT cid,cb FROM wall_click WHERE (idfa=? OR idfa=?) and ctime>? and ctime<? ORDER BY id DESC LIMIT 1"
  16. _rMacCntSQL = "SELECT COUNT(*) FROM wall_active WHERE rmac=?"
  17. _idfaSQL = "SELECT id FROM wall_active WHERE idfa=?"
  18. _inANClkSQL = "INSERT IGNORE INTO `wall_click_an` (`channel`,`imei`,`androidid`,`mac`,`ip`,`cb`,`ctime`,`mtime`) VALUES (?,?,?,?,?,?,?,?)"
  19. _anActiveSQL = "SELECT COUNT(*) FROM `wall_click_an` WHERE `type`=1 AND ((`androidid`=? AND `androidid`<>'') OR ((`imei`=? OR `imei`=?) AND `imei`<>''))"
  20. _anCbSQL = "SELECT `id`,`channel`,`cb`,`type` FROM `wall_click_an` WHERE (`androidid`=? AND `androidid`<>'') OR (`imei`=? AND `imei`<>'') ORDER BY `id` DESC LIMIT 1"
  21. _anGdtCbSQL = "SELECT `id`,`channel`,`cb`,`type` FROM `wall_click_an` WHERE `imei`=? ORDER BY `id` DESC LIMIT 1"
  22. _upClkActSQL = "UPDATE `wall_click_an` SET `type`=? WHERE `id`=?"
  23. )
  24. // Dao is wall dao.
  25. type Dao struct {
  26. db *xsql.DB
  27. inClkSQL *xsql.Stmt
  28. inActSQL *xsql.Stmt
  29. upActIdfaSQL *xsql.Stmt
  30. cbSQL *xsql.Stmt
  31. rMacCntSQL *xsql.Stmt
  32. idfaSQL *xsql.Stmt
  33. // android
  34. inANClkSQL *xsql.Stmt
  35. anActiveSQL *xsql.Stmt
  36. anCbSQL *xsql.Stmt
  37. anGdtCbSQL *xsql.Stmt
  38. upClkActSQL *xsql.Stmt
  39. }
  40. func New(c *conf.Config) (d *Dao) {
  41. d = &Dao{
  42. db: xsql.NewMySQL(c.MySQL.Show),
  43. }
  44. d.inClkSQL = d.db.Prepared(_inClkSQL)
  45. d.inActSQL = d.db.Prepared(_inActSQL)
  46. d.upActIdfaSQL = d.db.Prepared(_upActIdfaSQL)
  47. d.cbSQL = d.db.Prepared(_cbSQL)
  48. d.rMacCntSQL = d.db.Prepared(_rMacCntSQL)
  49. d.idfaSQL = d.db.Prepared(_idfaSQL)
  50. // android
  51. d.inANClkSQL = d.db.Prepared(_inANClkSQL)
  52. d.anActiveSQL = d.db.Prepared(_anActiveSQL)
  53. d.anCbSQL = d.db.Prepared(_anCbSQL)
  54. d.anGdtCbSQL = d.db.Prepared(_anGdtCbSQL)
  55. d.upClkActSQL = d.db.Prepared(_upClkActSQL)
  56. return
  57. }
  58. func (d *Dao) InClick(ctx context.Context, ip uint32, cid, mac, idfa, cb string, now time.Time) (row int64, err error) {
  59. res, err := d.inClkSQL.Exec(ctx, ip, cid, mac, idfa, cb, now, now)
  60. if err != nil {
  61. log.Error("tx.Exec error(%v)", err)
  62. return
  63. }
  64. return res.RowsAffected()
  65. }
  66. func (d *Dao) InActive(ctx context.Context, ip uint32, mid, rmac, mac, idfa, device string, now time.Time) (row int64, err error) {
  67. res, err := d.inActSQL.Exec(ctx, ip, mid, rmac, mac, idfa, device, now, now)
  68. if err != nil {
  69. log.Error("tx.Exec error(%v)", err)
  70. return
  71. }
  72. return res.RowsAffected()
  73. }
  74. func (d *Dao) UpIdfaActive(ctx context.Context, idfaAct, idfa string, now time.Time) (row int64, err error) {
  75. res, err := d.upActIdfaSQL.Exec(ctx, idfaAct, now, idfa)
  76. if err != nil {
  77. log.Error("tx.Exec error(%v)", err)
  78. return
  79. }
  80. return res.RowsAffected()
  81. }
  82. func (d *Dao) Callback(ctx context.Context, idfa, gdtIdfa string, now time.Time) (cid, cb string, err error) {
  83. lo := now.AddDate(0, 0, -1)
  84. row := d.cbSQL.QueryRow(ctx, idfa, gdtIdfa, lo, now)
  85. if row == nil {
  86. log.Error("d.cbSQL.QueryRow is null")
  87. return
  88. }
  89. if err = row.Scan(&cid, &cb); err != nil {
  90. if err == sql.ErrNoRows {
  91. err = nil
  92. } else {
  93. log.Error("Callback row.Scan error(%v)", err)
  94. }
  95. return
  96. }
  97. return
  98. }
  99. func (d *Dao) RMacCount(ctx context.Context, rmac string) (count int, err error) {
  100. row := d.rMacCntSQL.QueryRow(ctx, rmac)
  101. if row == nil {
  102. log.Error("d.rMacCntSQL.QueryRow is null")
  103. return
  104. }
  105. if err = row.Scan(&count); err != nil {
  106. if err == sql.ErrNoRows {
  107. err = nil
  108. } else {
  109. log.Error("RMacCount row.Scan error(%v)", err)
  110. }
  111. return
  112. }
  113. return
  114. }
  115. func (d *Dao) Exists(ctx context.Context, idfa string) (exist bool, err error) {
  116. row := d.idfaSQL.QueryRow(ctx, idfa)
  117. var id int
  118. if row == nil {
  119. log.Error("d.idfaSQL.QueryRow is null")
  120. return
  121. }
  122. if err = row.Scan(&id); err != nil {
  123. if err == sql.ErrNoRows {
  124. err = nil
  125. } else {
  126. log.Error("RMacCount row.Scan error(%v)", err)
  127. }
  128. return
  129. }
  130. exist = id > 0
  131. return
  132. }
  133. func (d *Dao) InANClick(c context.Context, channel, imei, androidid, mac, cb string, ip uint32, now time.Time) (row int64, err error) {
  134. res, err := d.inANClkSQL.Exec(c, channel, imei, androidid, mac, ip, cb, now, now)
  135. if err != nil {
  136. return
  137. }
  138. return res.RowsAffected()
  139. }
  140. func (d *Dao) ANActive(c context.Context, androidid, imei, gdtImei string) (count int, err error) {
  141. row := d.anActiveSQL.QueryRow(c, androidid, imei, gdtImei)
  142. if row == nil {
  143. return
  144. }
  145. if err = row.Scan(&count); err != nil {
  146. if err == sql.ErrNoRows {
  147. err = nil
  148. }
  149. }
  150. return
  151. }
  152. func (d *Dao) ANCallback(c context.Context, androidid, imei, gdtImei string) (id int64, channel, cb string, typ int, err error) {
  153. row := d.anCbSQL.QueryRow(c, androidid, imei)
  154. if err = row.Scan(&id, &channel, &cb, &typ); err != nil {
  155. if err == sql.ErrNoRows {
  156. err = nil
  157. } else {
  158. return
  159. }
  160. }
  161. if gdtImei != "" {
  162. var (
  163. gdtID int64
  164. gdtChannel, gdtCb string
  165. gdtTyp int
  166. )
  167. row := d.anGdtCbSQL.QueryRow(c, gdtImei)
  168. if err = row.Scan(&gdtID, &gdtChannel, &gdtCb, &gdtTyp); err != nil {
  169. if err == sql.ErrNoRows {
  170. err = nil
  171. }
  172. return
  173. }
  174. if gdtID > id {
  175. id = gdtID
  176. channel = gdtChannel
  177. cb = gdtCb
  178. typ = gdtTyp
  179. }
  180. }
  181. return
  182. }
  183. func (d *Dao) ANClickAct(c context.Context, id int64, typ int) (row int64, err error) {
  184. res, err := d.upClkActSQL.Exec(c, typ, id)
  185. if err != nil {
  186. return
  187. }
  188. return res.RowsAffected()
  189. }