log.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package unicom
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/interface/main/app-wall/model/unicom"
  7. "go-common/library/database/elastic"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _logUserKey = "log_user_action_91_%s"
  12. )
  13. func keyLogUser(now time.Time) string {
  14. return fmt.Sprintf(_logUserKey, now.Format("2006_01"))
  15. }
  16. func timeToString(now time.Time) string {
  17. return now.Format("2006-01-02 15:04:05")
  18. }
  19. // SearchUserBindLog unicom user bind log
  20. func (d *Dao) SearchUserBindLog(ctx context.Context, mid int64, now time.Time) (ulogs []*unicom.UserLog, err error) {
  21. var data struct {
  22. Result []struct {
  23. Ctime string `json:"ctime"`
  24. ExtraData string `json:"extra_data"`
  25. } `json:"result"`
  26. }
  27. var (
  28. endtime = now.AddDate(0, -1, 0)
  29. endWeekTime = now.AddDate(0, 0, -7)
  30. )
  31. req := d.es.NewRequest("log_user_action")
  32. req.Index(keyLogUser(now), keyLogUser(endtime)).WhereEq("mid", mid).WhereIn("action", []string{"unicom_userpack_add", "unicom_userpack_deduct"}).
  33. WhereRange("ctime", timeToString(endWeekTime), timeToString(now), elastic.RangeScopeLcRc).Order("ctime", "desc").
  34. Pn(1).Ps(2000)
  35. if err = req.Scan(ctx, &data); err != nil {
  36. log.Error("search user bind params(%s) error(%v)", req.Params(), err)
  37. return
  38. }
  39. for _, d := range data.Result {
  40. ulog := &unicom.UserLog{}
  41. if err = ulog.UserLogJSONChange(d.ExtraData); err != nil {
  42. log.Error("ulog json change error(%v)", err)
  43. continue
  44. }
  45. ulog.Ctime = d.Ctime
  46. ulogs = append(ulogs, ulog)
  47. }
  48. return
  49. }