member.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/admin/main/member/model"
  7. "github.com/pkg/errors"
  8. )
  9. var (
  10. _sharding = int64(100)
  11. )
  12. func baseTable(mid int64) string {
  13. return fmt.Sprintf("user_base_%02d", mid%_sharding)
  14. }
  15. func expTable(mid int64) string {
  16. return fmt.Sprintf("user_exp_%02d", mid%_sharding)
  17. }
  18. // Base is.
  19. func (d *Dao) Base(ctx context.Context, mid int64) (*model.Base, error) {
  20. b := &model.Base{}
  21. if err := d.member.Table(baseTable(mid)).Where("mid=?", mid).Find(b).Error; err != nil {
  22. return nil, err
  23. }
  24. b.RandFaceURL()
  25. return b, nil
  26. }
  27. // Bases is.
  28. func (d *Dao) Bases(ctx context.Context, mids []int64) (map[int64]*model.Base, error) {
  29. bs := make(map[int64]*model.Base, len(mids))
  30. for _, mid := range mids {
  31. b, err := d.Base(ctx, mid)
  32. if err != nil {
  33. continue
  34. }
  35. bs[b.Mid] = b
  36. }
  37. return bs, nil
  38. }
  39. // Exp is.
  40. func (d *Dao) Exp(ctx context.Context, mid int64) (*model.Exp, error) {
  41. e := &model.Exp{}
  42. if err := d.member.Table(expTable(mid)).Where("mid=?", mid).Find(e).Error; err != nil {
  43. return nil, err
  44. }
  45. return e, nil
  46. }
  47. // Moral is.
  48. func (d *Dao) Moral(ctx context.Context, mid int64) (*model.Moral, error) {
  49. m := &model.Moral{
  50. Mid: mid,
  51. Moral: 7000,
  52. }
  53. if err := d.member.Table("user_moral").
  54. Where(&model.Moral{Mid: mid}).
  55. FirstOrCreate(m).Error; err != nil {
  56. return nil, err
  57. }
  58. return m, nil
  59. }
  60. // UpName is.
  61. func (d *Dao) UpName(ctx context.Context, mid int64, name string) error {
  62. ups := map[string]string{
  63. "name": name,
  64. }
  65. if err := d.member.Table(baseTable(mid)).Where("mid=?", mid).Updates(ups).Error; err != nil {
  66. err = errors.Wrap(err, "dao update name")
  67. return err
  68. }
  69. return nil
  70. }
  71. // UpSign is.
  72. func (d *Dao) UpSign(ctx context.Context, mid int64, sign string) error {
  73. ups := map[string]string{
  74. "sign": sign,
  75. }
  76. if err := d.member.Table(baseTable(mid)).Where("mid=?", mid).Updates(ups).Error; err != nil {
  77. err = errors.Wrap(err, "dao update sign")
  78. return err
  79. }
  80. return nil
  81. }
  82. // UpFace is.
  83. func (d *Dao) UpFace(ctx context.Context, mid int64, face string) error {
  84. ups := map[string]string{
  85. "face": face,
  86. }
  87. if err := d.member.Table(baseTable(mid)).Where("mid=?", mid).Updates(ups).Error; err != nil {
  88. err = errors.Wrap(err, "dao update face")
  89. return err
  90. }
  91. return nil
  92. }
  93. // BatchUserAddit is.
  94. func (d *Dao) BatchUserAddit(ctx context.Context, mids []int64) (map[int64]*model.UserAddit, error) {
  95. uas := []*model.UserAddit{}
  96. if err := d.member.Table("user_addit").Where("mid in (?)", mids).Find(&uas).Error; err != nil {
  97. return nil, err
  98. }
  99. uasMap := make(map[int64]*model.UserAddit, len(uas))
  100. for _, ua := range uas {
  101. uasMap[ua.Mid] = ua
  102. }
  103. return uasMap, nil
  104. }
  105. // PubExpMsg is.
  106. func (d *Dao) PubExpMsg(ctx context.Context, msg *model.AddExpMsg) error {
  107. return d.expMsgDatabus.Send(ctx, strconv.FormatInt(msg.Mid, 10), msg)
  108. }
  109. // UserAddit is.
  110. func (d *Dao) UserAddit(ctx context.Context, mid int64) (*model.UserAddit, error) {
  111. addit := &model.UserAddit{}
  112. if err := d.member.Table("user_addit").Where("mid=?", mid).Find(addit).Error; err != nil {
  113. return nil, err
  114. }
  115. return addit, nil
  116. }
  117. // UpAdditRemark update remark.
  118. func (d *Dao) UpAdditRemark(ctx context.Context, mid int64, remark string) error {
  119. addit := &model.UserAddit{
  120. Mid: mid,
  121. }
  122. upRemark := map[string]string{
  123. "remark": remark,
  124. }
  125. if err := d.member.Table("user_addit").Where("mid=?", mid).Assign(upRemark).FirstOrCreate(addit).Error; err != nil {
  126. err = errors.Wrap(err, "dao insert or update addit")
  127. return err
  128. }
  129. return nil
  130. }