mysql.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/service/main/vipinfo/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/xstr"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _byMidSQL = "SELECT id,mid,vip_type,vip_pay_type,vip_status,pay_channel_id,vip_start_time,vip_overdue_time,annual_vip_overdue_time,ctime,mtime,vip_recent_time,ios_overdue_time,ver FROM vip_user_info WHERE mid = ?;"
  13. _byMidsSQL = "SELECT id,mid,vip_type,vip_pay_type,vip_status,pay_channel_id,vip_start_time,vip_overdue_time,annual_vip_overdue_time,ctime,mtime,vip_recent_time,ios_overdue_time,ver FROM vip_user_info WHERE mid IN (%s);"
  14. )
  15. //RawInfo select user info by mid.
  16. func (d *Dao) RawInfo(c context.Context, mid int64) (r *model.VipUserInfo, err error) {
  17. var row = d.db.QueryRow(c, _byMidSQL, mid)
  18. r = new(model.VipUserInfo)
  19. if err = row.Scan(&r.ID, &r.Mid, &r.VipType, &r.VipPayType, &r.VipStatus, &r.PayChannelID, &r.VipStartTime, &r.VipOverdueTime, &r.AnnualVipOverdueTime,
  20. &r.Ctime, &r.Mtime, &r.VipRecentTime, &r.IosOverdueTime, &r.Ver); err != nil {
  21. if err == sql.ErrNoRows {
  22. r = nil
  23. err = nil
  24. return
  25. }
  26. err = errors.Wrapf(err, "dao info bymid(%d)", mid)
  27. }
  28. return
  29. }
  30. // RawInfos get user infos.
  31. func (d *Dao) RawInfos(c context.Context, mids []int64) (res map[int64]*model.VipUserInfo, err error) {
  32. var rows *xsql.Rows
  33. res = make(map[int64]*model.VipUserInfo, len(mids))
  34. midStr := xstr.JoinInts(mids)
  35. if rows, err = d.db.Query(c, fmt.Sprintf(_byMidsSQL, midStr)); err != nil {
  36. err = errors.Wrapf(err, "dao infos mids(%s)", midStr)
  37. return
  38. }
  39. defer rows.Close()
  40. for rows.Next() {
  41. r := new(model.VipUserInfo)
  42. if err = rows.Scan(&r.ID, &r.Mid, &r.VipType, &r.VipPayType, &r.VipStatus, &r.PayChannelID, &r.VipStartTime, &r.VipOverdueTime, &r.AnnualVipOverdueTime,
  43. &r.Ctime, &r.Mtime, &r.VipRecentTime, &r.IosOverdueTime, &r.Ver); err != nil {
  44. err = errors.Wrapf(err, "dao infos scan mids(%s)", midStr)
  45. res = nil
  46. return
  47. }
  48. res[r.Mid] = r
  49. }
  50. err = rows.Err()
  51. return
  52. }