group.go 6.41 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package rpc

import (
	"encoding/json"
chenweijian's avatar
chenweijian committed
5
	"errors"
hujiebin's avatar
hujiebin committed
6
	"fmt"
7
	"git.hilo.cn/hilo-common/_const/enum/timezone_e"
hujiebin's avatar
hujiebin committed
8 9 10 11
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/consul"
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
12
	"git.hilo.cn/hilo-common/utils"
hujiebin's avatar
hujiebin committed
13 14 15 16 17 18
	"math/rand"
)

const (
	defaultGroupConsulName   = "hiloGroup"
	defaultGroupServerScheme = "http"
hujiebin's avatar
hujiebin committed
19
	defaultGroupServerAddr   = "127.0.0.1:9050" // 默认内网转发,本地回环
hujiebin's avatar
hujiebin committed
20 21 22 23 24 25
)

var groupServerHost = []string{defaultGroupServerAddr}

func init() {
	go func() {
hujiebin's avatar
hujiebin committed
26 27 28
		consul.RegisterWatcher(defaultGroupConsulName, func(addr []string) {
			if len(addr) > 0 {
				groupServerHost = addr
hujiebin's avatar
hujiebin committed
29
			}
hujiebin's avatar
hujiebin committed
30
		})
hujiebin's avatar
hujiebin committed
31 32 33 34 35 36 37
	}()
}

// 家族信息
type CvGroupPowerInfo struct {
	CvGroupPowerBase   `json:",inline"`
	CvGroupPowerMember `json:",inline"`
hujiebin's avatar
hujiebin committed
38
	CvGroupPowerGrade  `json:",inline"`
hujiebin's avatar
hujiebin committed
39 40 41 42
}

// 家族基本信息
type CvGroupPowerBase struct {
hujiebin's avatar
hujiebin committed
43 44 45 46
	Id        mysql.ID `json:"id"`        // 家族id
	Icon      string   `json:"icon"`      // 家族图片
	Name      string   `json:"name"`      // 家族名
	Nameplate string   `json:"nameplate"` // 铭牌
hujiebin's avatar
hujiebin committed
47 48 49 50
}

// 家族成员
type CvGroupPowerMember struct {
hujiebin's avatar
hujiebin committed
51 52
	MemberNum mysql.Num `json:"memberNum"` // 当前成员数
	MemberMax mysql.Num `json:"memberMax"` // 成员上限
hujiebin's avatar
hujiebin committed
53 54
}

hujiebin's avatar
hujiebin committed
55 56 57 58 59 60 61 62 63
// 家族等级
type CvGroupPowerGrade struct {
	Grade    int    `json:"grade"`              // 等级 0:无 1:青铜 2:白银 3:黄金 4:黑金
	Exp      uint32 `json:"exp"`                // 经验值
	NextExp  uint32 `json:"nextExp,omitempty"`  // 升级所需经验值
	ExpireAt string `json:"expireAt,omitempty"` // 有效期
	ShowExp  bool   `json:"showExp"`            // 是否展示经验值
}

hujiebin's avatar
hujiebin committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
// 获取家族
func GetGroupPower(model *domain.Model, groupPowerId mysql.ID) (CvGroupPowerInfo, error) {
	infos, err := MGetGroupPower(model, []mysql.ID{groupPowerId})
	if err != nil {
		return CvGroupPowerInfo{}, nil
	}
	return infos[groupPowerId], nil
}

// 批量获取家族
func MGetGroupPower(model *domain.Model, groupPowerIds []mysql.ID) (map[mysql.ID]CvGroupPowerInfo, error) {
	type Response struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Data    map[mysql.ID]CvGroupPowerInfo
	}
	var res = make(map[mysql.ID]CvGroupPowerInfo)
	if len(groupPowerIds) <= 0 {
		return res, nil
	}
	var idsStr []string
	for _, userId := range groupPowerIds {
		idsStr = append(idsStr, fmt.Sprintf("%d", userId))
	}
	_url := fmt.Sprintf("%v://%v/inner/groupPower/infos", defaultGroupServerScheme, getGroupHost())
	resp, err := HttpGet(model, _url, nil, map[string][]string{
		"ids": idsStr,
	})
	if err != nil {
		model.Log.Errorf("MGetGroupPower fail:%v", err)
		return res, err
	}
	response := new(Response)
	if err = json.Unmarshal(resp, response); err != nil {
		model.Log.Errorf("MGetUserSvip json fail:%v", err)
		return res, err
	}
	res = response.Data
	return res, nil
}

105
// 批量获取用户天上麦时长
106
// param day 获取的天,格式 2006-01-02
107
// param tz 时区,0:北京时间 1:沙特时间
108
// return userId->seconds
109
func MGetUserOnMic(model *domain.Model, day string, userIds []mysql.ID, tz ...timezone_e.Timezone) (map[mysql.ID]mysql.Num, error) {
110 111 112 113 114 115 116 117 118 119 120 121 122 123
	type Response struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Data    map[mysql.ID]mysql.Num
	}
	var res = make(map[mysql.ID]mysql.Num)
	if len(userIds) <= 0 {
		return res, nil
	}
	var userIdsStr []string
	for _, userId := range userIds {
		userIdsStr = append(userIdsStr, fmt.Sprintf("%d", userId))
	}
	_url := fmt.Sprintf("%v://%v/inner/mic/onMicSeconds", defaultGroupServerScheme, getGroupHost())
124 125 126 127
	_tz := timezone_e.TimezoneBeijing
	if len(tz) > 0 {
		_tz = tz[0]
	}
128 129 130
	resp, err := HttpGet(model, _url, nil, map[string][]string{
		"ids": userIdsStr,
		"day": {day},
131
		"tz":  {fmt.Sprintf("%d", _tz)},
132 133 134 135 136 137 138 139 140 141 142 143 144 145
	})
	if err != nil {
		model.Log.Errorf("MGetUserOnMic fail:%v", err)
		return res, err
	}
	response := new(Response)
	if err = json.Unmarshal(resp, response); err != nil {
		model.Log.Errorf("MGetUserOnMic json fail:%v", err)
		return res, err
	}
	res = response.Data
	return res, nil
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
// 批量获取用户范围上麦时长
// param beginDate 获取的天,格式 2006-01-02
// param endDate 获取的天,格式 2006-01-02
// param tz 时区,0:北京时间 1:沙特时间
// return userId->seconds
func MGetUserOnMicRange(model *domain.Model, beginDate, endDate string, userIds []mysql.ID, tz ...timezone_e.Timezone) (map[mysql.ID]mysql.Num, error) {
	type Response struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Data    map[mysql.ID]mysql.Num
	}
	var res = make(map[mysql.ID]mysql.Num)
	if len(userIds) <= 0 {
		return res, nil
	}
	var userIdsStr []string
	for _, userId := range userIds {
		userIdsStr = append(userIdsStr, fmt.Sprintf("%d", userId))
	}
	_url := fmt.Sprintf("%v://%v/inner/mic/onMicSeconds/range", defaultGroupServerScheme, getGroupHost())
	_tz := timezone_e.TimezoneBeijing
	if len(tz) > 0 {
		_tz = tz[0]
	}
	resp, err := HttpGet(model, _url, nil, map[string][]string{
		"ids":       userIdsStr,
		"beginDate": {beginDate},
		"endDate":   {endDate},
		"tz":        {fmt.Sprintf("%d", _tz)},
	})
	if err != nil {
		model.Log.Errorf("MGetUserOnMic fail:%v", err)
		return res, err
	}
	response := new(Response)
	if err = json.Unmarshal(resp, response); err != nil {
		model.Log.Errorf("MGetUserOnMic json fail:%v", err)
		return res, err
	}
	res = response.Data
	return res, nil
}

hujiebin's avatar
hujiebin committed
189 190 191 192 193 194
func getGroupHost() string {
	l := len(groupServerHost)
	r := rand.Intn(l) // 随机一个
	mylogrus.MyLog.Infof("getHostGroup:%v---%v", r, groupServerHost[r])
	return groupServerHost[r]
}
chenweijian's avatar
chenweijian committed
195

chenweijian's avatar
chenweijian committed
196
// 上麦
chenweijian's avatar
chenweijian committed
197
func MicIn(model *domain.Model, groupId, token string) error {
chenweijian's avatar
chenweijian committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	defer utils.CheckGoPanic()
	type MicInResp struct {
		Code int `json:"code"`
		Data struct {
			MicIndex int `json:"micIndex"`
		} `json:"data"`
	}
	_url := fmt.Sprintf("%v://%v/v1/imGroup/mic/in", defaultGroupServerScheme, getGroupHost())
	header := map[string]string{
		"token": token,
	}
	resp, err := HttpPostForm(model, _url, header, map[string]string{
		"groupUuid": groupId,
		"i":         "", // 空则随意上一个空位置
	})
	if err != nil {
		model.Log.Errorf("MicIn fail:%v", err)
		return err
	}
	response := new(MicInResp)
	if err = json.Unmarshal(resp, response); err != nil {
		model.Log.Errorf("MicIn json fail:%v", err)
		return err
	}
chenweijian's avatar
chenweijian committed
222
	if response.Code != 200 {
chenweijian's avatar
chenweijian committed
223 224 225 226 227
		model.Log.Errorf(fmt.Sprintf("Mic In Not 200:%v,groupId:%v", response, groupId))
		return errors.New(fmt.Sprintf("Mic In Not 200:%v", response))
	}
	return nil
}