mobile.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package mobile
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/interface/main/app-wall/conf"
  7. "go-common/app/interface/main/app-wall/model/mobile"
  8. "go-common/library/cache/memcache"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _inOrderSyncSQL = `INSERT IGNORE INTO mobile_order (orderid,userpseudocode,channelseqid,price,actiontime,actionid,effectivetime,expiretime,channelid,productid,ordertype,threshold)
  14. VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE orderid=?,channelseqid=?,price=?,actiontime=?,actionid=?,effectivetime=?,expiretime=?,channelid=?,productid=?,ordertype=?,threshold=?`
  15. _upOrderFlowSQL = `UPDATE mobile_order SET threshold=?,resulttime=? WHERE userpseudocode=? AND productid=?`
  16. _orderSyncByUserSQL = `SELECT orderid,userpseudocode,channelseqid,price,actionid,effectivetime,expiretime,channelid,productid,ordertype,threshold FROM mobile_order WHERE effectivetime<? AND userpseudocode=?`
  17. )
  18. type Dao struct {
  19. db *xsql.DB
  20. inOrderSyncSQL *xsql.Stmt
  21. upOrderFlowSQL *xsql.Stmt
  22. orderUserSyncSQL *xsql.Stmt
  23. // memcache
  24. mc *memcache.Pool
  25. expire int32
  26. }
  27. func New(c *conf.Config) (d *Dao) {
  28. d = &Dao{
  29. db: xsql.NewMySQL(c.MySQL.Show),
  30. // memcache
  31. mc: memcache.NewPool(c.Memcache.Operator.Config),
  32. expire: int32(time.Duration(c.Memcache.Operator.Expire) / time.Second),
  33. }
  34. d.inOrderSyncSQL = d.db.Prepared(_inOrderSyncSQL)
  35. d.upOrderFlowSQL = d.db.Prepared(_upOrderFlowSQL)
  36. d.orderUserSyncSQL = d.db.Prepared(_orderSyncByUserSQL)
  37. return
  38. }
  39. func (d *Dao) InOrdersSync(ctx context.Context, u *mobile.MobileXML) (row int64, err error) {
  40. res, err := d.inOrderSyncSQL.Exec(ctx, u.Orderid, u.Userpseudocode, u.Channelseqid, u.Price, u.Actiontime, u.Actionid, u.Effectivetime, u.Expiretime,
  41. u.Channelid, u.Productid, u.Ordertype, u.Threshold,
  42. u.Orderid, u.Channelseqid, u.Price, u.Actiontime, u.Actionid, u.Effectivetime, u.Expiretime,
  43. u.Channelid, u.Productid, u.Ordertype, u.Threshold)
  44. if err != nil {
  45. log.Error("d.inOrderSyncSQL.Exec error(%v)", err)
  46. return
  47. }
  48. tmp := &mobile.Mobile{}
  49. tmp.MobileXMLMobile(u)
  50. if err = d.UpdateMobileCache(ctx, u.Userpseudocode, tmp); err != nil {
  51. log.Error("mobile d.UpdateMobileCache usermob(%v) error(%v)", u.Userpseudocode, err)
  52. return
  53. }
  54. return res.RowsAffected()
  55. }
  56. // FlowSync update OrdersSync
  57. func (d *Dao) FlowSync(ctx context.Context, u *mobile.MobileXML) (row int64, err error) {
  58. res, err := d.upOrderFlowSQL.Exec(ctx, u.Threshold, u.Resulttime, u.Userpseudocode, u.Productid)
  59. if err != nil {
  60. log.Error("d.upOrderFlowSQL.Exec error(%v)", err)
  61. return
  62. }
  63. thresholdInt, _ := strconv.Atoi(u.Threshold)
  64. tmp := &mobile.Mobile{
  65. Threshold: thresholdInt,
  66. Productid: u.Productid,
  67. }
  68. if err = d.UpdateMobileFlowCache(ctx, u.Userpseudocode, tmp); err != nil {
  69. log.Error("mobile d.UpdateMobileFlowCache usermob(%v) error(%v)", u.Userpseudocode, err)
  70. return
  71. }
  72. return res.RowsAffected()
  73. }
  74. // OrdersUserFlow select user OrdersSync
  75. func (d *Dao) OrdersUserFlow(ctx context.Context, usermob string, now time.Time) (res map[string][]*mobile.Mobile, err error) {
  76. rows, err := d.orderUserSyncSQL.Query(ctx, now, usermob)
  77. if err != nil {
  78. log.Error("query error (%v)", err)
  79. return
  80. }
  81. defer rows.Close()
  82. res = map[string][]*mobile.Mobile{}
  83. for rows.Next() {
  84. u := &mobile.Mobile{}
  85. if err = rows.Scan(&u.Orderid, &u.Userpseudocode, &u.Channelseqid, &u.Price, &u.Actionid, &u.Effectivetime, &u.Expiretime,
  86. &u.Channelid, &u.Productid, &u.Ordertype, &u.Threshold); err != nil {
  87. log.Error("Mobile OrdersUserFlow rows.Scan err (%v)", err)
  88. return
  89. }
  90. u.MobileChange()
  91. res[u.Userpseudocode] = append(res[u.Userpseudocode], u)
  92. }
  93. return
  94. }