agentmgr.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package agent
  2. import (
  3. "encoding/json"
  4. "sync"
  5. task "bet24.com/servers/micros/task/proto"
  6. user "bet24.com/servers/micros/userservices/proto"
  7. item "bet24.com/servers/micros/item_inventory/proto"
  8. "bet24.com/servers/common"
  9. inventory "bet24.com/servers/micros/item_inventory/proto"
  10. "bet24.com/log"
  11. )
  12. type agentmgr struct {
  13. ConfigInfo *configInfo // 代理配置
  14. Groups map[int]*groupInfo // 代理群信息
  15. lock *sync.RWMutex
  16. }
  17. func newAgentMgr() *agentmgr {
  18. obj := new(agentmgr)
  19. obj.lock = &sync.RWMutex{}
  20. obj.Groups = make(map[int]*groupInfo)
  21. obj.load()
  22. log.Debug("agent manager running")
  23. return obj
  24. }
  25. // 加载系统信息
  26. func (this *agentmgr) load() {
  27. this.ConfigInfo = getConfigInfo()
  28. awards := getBindAwards()
  29. this.ConfigInfo.BindAwards = append(this.ConfigInfo.BindAwards, awards...)
  30. }
  31. // 获取组信息
  32. func (this *agentmgr) getGroup(userId int) *groupInfo {
  33. if userId <= 0 {
  34. return nil
  35. }
  36. // 判断是否开启
  37. if !this.isOpen() {
  38. return nil
  39. }
  40. this.lock.RLock()
  41. userGroup, ok := this.Groups[userId]
  42. this.lock.RUnlock()
  43. if !ok {
  44. userGroup = newUserGroup(userId)
  45. this.lock.Lock()
  46. this.Groups[userId] = userGroup
  47. this.lock.Unlock()
  48. }
  49. return userGroup
  50. }
  51. // 设置群组信息
  52. func (this *agentmgr) updateGroup(userId int, id int, groupName, groupLink string) int {
  53. // 判断是否开启
  54. if !this.isOpen() {
  55. return 21
  56. }
  57. // 获取代理信息
  58. info := this.info(userId)
  59. if info == nil {
  60. return 11
  61. }
  62. userGroup := this.getGroup(userId)
  63. return userGroup.updateLink(id, groupName, groupLink)
  64. }
  65. // 配置信息
  66. func (this *agentmgr) getJsonConfigs() string {
  67. d, _ := json.Marshal(this.ConfigInfo)
  68. return string(d)
  69. }
  70. // 申请
  71. func (this *agentmgr) apply(userId int, memo string) int {
  72. // 判断是否开启
  73. if !this.isOpen() {
  74. return 21
  75. }
  76. if userId <= 0 {
  77. return 11
  78. }
  79. return apply(userId, memo)
  80. }
  81. // 是否开启
  82. func (this *agentmgr) isOpen() bool {
  83. return this.ConfigInfo.IsOpen == 1
  84. }
  85. // 绑码
  86. func (this *agentmgr) bind(userId, higherUserId int) (int, []item.ItemPack) {
  87. // 判断是否开启
  88. if !this.isOpen() {
  89. return 21, nil
  90. }
  91. if userId <= 0 || higherUserId <= 0 || userId == higherUserId {
  92. return 11, nil
  93. }
  94. retCode := bind(userId, higherUserId)
  95. var items []item.ItemPack
  96. // 绑码成功
  97. if retCode == 1 && this.ConfigInfo.BindSend > 0 {
  98. items = append(items, item.ItemPack{
  99. ItemId: item.Item_Gold,
  100. Count: this.ConfigInfo.BindSend,
  101. })
  102. // 加道具
  103. if success := inventory.AddItems(userId, items, "绑码赠送", common.LOGTYPE_BIND_TEACHER); !success {
  104. log.Error("agentMgr.bind AddItems fail userId=%d higherUserId=%d items=%+v", userId, higherUserId, items)
  105. }
  106. // 刷新任务
  107. go task.RefreshTask(userId)
  108. }
  109. return retCode, items
  110. }
  111. // 代理信息
  112. func (this *agentmgr) info(userId int) *info_out {
  113. return info(userId)
  114. }
  115. // 会员
  116. func (this *agentmgr) members(userId, pageIndex, pageSize int) *memberList {
  117. ret := members(userId, pageIndex, pageSize)
  118. for i := 0; i < len(ret.List); i++ {
  119. u := user.GetUserInfo(ret.List[i].UserID)
  120. if u == nil {
  121. log.Error("agentMgr.members userId=%d", ret.List[i].UserID)
  122. return ret
  123. }
  124. ret.List[i].NickName = u.NickName
  125. ret.List[i].Sex = u.Sex
  126. ret.List[i].FaceID = u.FaceId
  127. ret.List[i].FaceUrl = u.FaceUrl
  128. }
  129. return ret
  130. }
  131. // 代理统计
  132. func (this *agentmgr) stat(userId, pageIndex, pageSize int) *stat_out {
  133. return commissionStat(userId, pageIndex, pageSize)
  134. }
  135. // 收益记录
  136. func (this *agentmgr) commissionLog(userId, fromUserId, pageIndex, pageSize int) *commission_out {
  137. ret := commissionLog(userId, fromUserId, pageIndex, pageSize)
  138. for i := 0; i < len(ret.List); i++ {
  139. u := user.GetUserInfo(ret.List[i].FromUserID)
  140. if u == nil {
  141. log.Error("agentMgr.commissionLog userId=%d", ret.List[i].FromUserID)
  142. return ret
  143. }
  144. ret.List[i].FromNickName = u.NickName
  145. ret.List[i].Sex = u.Sex
  146. ret.List[i].FaceID = u.FaceId
  147. ret.List[i].FaceUrl = u.FaceUrl
  148. }
  149. return ret
  150. }
  151. // 提取收益
  152. func (this *agentmgr) commissionToAmount(userId int, ipAddress string) (int, int) {
  153. return commissionToAmount(userId, ipAddress)
  154. }
  155. func (this *agentmgr) clear(userId int) {
  156. this.lock.Lock()
  157. defer this.lock.Unlock()
  158. delete(this.Groups, userId)
  159. }