userext.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/interface/live/app-room/model"
  5. "go-common/app/service/live/userext/api/liverpc/v0"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "strconv"
  9. )
  10. //GetUserConf 获取用户配置
  11. func (d *Dao) GetUserConf(c context.Context, mid int64, targetId int64, keys []int64) (values model.ConfValues, err error) {
  12. req := &v0.ConfGetReq{
  13. Uid: mid,
  14. TargetId: targetId,
  15. Type: keys,
  16. }
  17. resp, err := d.UserExtAPI.V0Conf.Get(c, req)
  18. if err != nil {
  19. log.Error("call getUserConf sys error %v", err.Error())
  20. return
  21. }
  22. if resp.Code != 0 {
  23. log.Error("call getUserConf failed %d %v", resp.Code, resp.Msg)
  24. err = ecode.ParamInvalid
  25. return
  26. }
  27. o := resp.Data
  28. values = make(map[int64]string, len(o))
  29. for i, v := range o {
  30. s, e := strconv.ParseInt(i, 10, 64)
  31. if e != nil {
  32. log.Error("call getUserConf string2int64 failed %v", e.Error())
  33. err = e
  34. return
  35. }
  36. values[s] = v
  37. }
  38. return
  39. }
  40. //AsyncSetUserConf 异步设置用户一个配置 "存在"
  41. func (d *Dao) AsyncSetUserConf(c context.Context, mid int64, targetId int64, key int64) (err error) {
  42. value := model.Set
  43. vs := make(map[int64]string, 1)
  44. vs[key] = value
  45. err = d.giftCache.Save(func() {
  46. d.SetMoreUserConf(context.Background(), mid, targetId, vs)
  47. })
  48. if err != nil {
  49. err = d.SetMoreUserConf(c, mid, targetId, vs)
  50. }
  51. return
  52. }
  53. //SetUserConf 设置用户一个配置 "存在"
  54. func (d *Dao) SetUserConf(c context.Context, mid int64, targetId int64, key int64) (err error) {
  55. value := model.Set
  56. vs := make(map[int64]string, 1)
  57. vs[key] = value
  58. err = d.SetMoreUserConf(c, mid, targetId, vs)
  59. return
  60. }
  61. //DelUserConf 删除用户一个配置 "不存在"
  62. func (d *Dao) DelUserConf(c context.Context, mid int64, targetId int64, key int64) (err error) {
  63. value := model.Empty
  64. vs := make(map[int64]string, 1)
  65. vs[key] = value
  66. err = d.SetMoreUserConf(c, mid, targetId, vs)
  67. return
  68. }
  69. //SetMoreUserConf 设置用户多个配置
  70. func (d *Dao) SetMoreUserConf(c context.Context, mid int64, targetId int64, values map[int64]string) (err error) {
  71. varList := make([]*v0.ConfSetReq_Var, len(values))
  72. ic := 0
  73. for k, v := range values {
  74. rv := &v0.ConfSetReq_Var{
  75. Uid: mid,
  76. TargetId: targetId,
  77. Type: k,
  78. Content: v,
  79. }
  80. varList[ic] = rv
  81. ic++
  82. }
  83. req := &v0.ConfSetReq{
  84. VarList: varList,
  85. }
  86. r, err := d.UserExtAPI.V0Conf.Set(c, req)
  87. if err != nil {
  88. log.Error("call setUserConf sys error %v", err.Error())
  89. return
  90. }
  91. if r.Code != 0 {
  92. log.Error("call setUserConf failed %d %v", r.Code, r.Msg)
  93. err = ecode.ParamInvalid
  94. return
  95. }
  96. return
  97. }