group_support.go 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
package group_r

import (
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"github.com/gin-gonic/gin"
	"hilo-group/_const/enum/gift_e"
	"hilo-group/_const/enum/group_e"
	"hilo-group/_const/enum/msg_e"
	"hilo-group/cv/group_cv"
	"hilo-group/cv/user_cv"
	"hilo-group/domain/cache/group_c"
	"hilo-group/domain/model/gift_m"
	"hilo-group/domain/model/group_m"
	"hilo-group/domain/model/res_m"
	"hilo-group/domain/model/user_m"
	"hilo-group/domain/service/group_s"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"hilo-group/req"
	"hilo-group/resp"
	"strings"
	"time"
)

// @Tags 群组
// @Summary 查询是否能打开支持页
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId path string true "群ID"
// @Success 200 {object} bool
// @Router /v1/imGroup/support/page/{groupId} [get]
func GetSupportPage(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	groupId := c.Param("groupId")
	if len(groupId) <= 0 {
		return myContext, bizerr.ParaMissing
	}

	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	pa, err := group_m.GetProfitAllocator(model, groupId)
	if err != nil {
		return myContext, err
	}

	resp.ResponseOk(c, pa == userId)
	return myContext, nil
}

// @Tags 群组
// @Summary 支持页详情
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId query string false "群ID"
// @Success 200 {object} cv.SupportPageDetail
// @Router /v1/imGroup/support/detail [get]
func GetSupportDetail(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	model := domain.CreateModelContext(myContext)

	groupId := c.Query("groupId")
	txGroupId := groupId
	if len(groupId) <= 0 {
		// groupId 没填的话,用user自己的群
		groups, err := group_m.FindGroupByOwner(model, userId)
		if err != nil {
			return myContext, err
		}
		if len(groups) > 0 {
			groupId = groups[0].ImGroupId
			txGroupId = groups[0].TxGroupId
		} else {
			return myContext, bizerr.GroupNotFound
		}
	} else {
		groupId, err = group_m.ToImGroupId(model, groupId)
		if err != nil {
			return myContext, err
		}
	}

	pa, err := group_m.GetProfitAllocator(model, groupId)
	if err != nil {
		return myContext, err
	}
	if userId != pa {
		return myContext, bizerr.NoPrivileges
	}

	result := group_cv.SupportPageDetail{GroupId: txGroupId}

	now := time.Now()
	beginTime, endTime, _ := group_m.GetSupportLevelTime(now)
	result.RemainSecond = endTime.Unix() - now.Unix()

	g := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, SceneUid: groupId, Model: model}
	result.CurrentCount, result.CurrentConsume, err = g.GetConsumeByRange(beginTime, endTime)
	if err != nil {
		return myContext, err
	}

	beginTime, endTime, _ = group_m.GetSupportLevelTime(now.AddDate(0, 0, -group_e.SUPPORT_LEVEL_PERIOD_DAY))
	result.LastCount, result.LastConsume, err = g.GetConsumeByRange(beginTime, endTime)
	if err != nil {
		return myContext, err
	}

	rec, err := res_m.GetResGroupSupportBy(model, result.LastCount, result.LastConsume)
	if err != nil {
		return myContext, err
	}
	if rec != nil {
		result.SupportLevel = string(64 + rec.Grade)
		result.SupporterLimit = rec.MgrNum
	}

	rec, err = res_m.GetResGroupSupportBy(model, result.CurrentCount, result.CurrentConsume)
	if err != nil {
		return myContext, err
	}
	if rec != nil {
		result.CurrentSupportLevel = string(64 + rec.Grade)
	}

	userBase, err := user_cv.GetUserBase(pa, userId)
	if err != nil {
		return myContext, err
	}
	if userBase != nil {
		result.ProfitAllocator = *userBase
	}

	// 判断这个周期这个群的奖金是否已经发过
	_, _, period := group_m.GetLastSupportPeriod(now)
	gsaa := group_m.GroupSupportAwardAdmin{Period: period, GroupUid: groupId}
	rows, err := gsaa.Get(model.Db)
	if err != nil {
		return myContext, err
	}

	if len(rows) > 0 {
		result.IsDispatched = true
	}

	resp.ResponseOk(c, result)
	return myContext, nil
}

// @Tags 群组
// @Summary 检查用户能不能成为群支持者
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId path string false "群ID"
// @Param externalId query string true "用户的externalId"
// @Success 200
// @Router /v1/imGroup/support/award/{groupId} [get]
func TryAddSupporter(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	myUserId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}

	groupId := c.Param("groupId")
	if len(groupId) <= 0 {
		return myContext, myerr.NewSysError("groupId 为必填项")
	}

	externalId := c.Query("externalId")
	if len(externalId) <= 0 {
		return myContext, myerr.NewSysError("externalId 为必填项")
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	pa, err := group_m.GetProfitAllocator(model, groupId)
	if err != nil {
		return myContext, err
	}
	if myUserId != pa {
		return myContext, bizerr.NoPrivileges
	}

	user, err := user_m.GetUserByExtId(model, externalId)
	if err != nil {
		return myContext, err
	}
	userId := user.ID

	role, err := group_m.GetRoleInGroup(model, userId, groupId)
	if err != nil {
		return myContext, err
	}

	if role != group_e.GROUP_MANAGER && role != group_e.GROUP_ADMIN {
		return myContext, bizerr.NotManagerOrAdmin
	}

	// 判断用户是否作为助手领取过奖励
	sameImeiUsers, err := user_m.GetSameImeiMap(model, userId)
	if err != nil {
		return myContext, err
	}
	_, _, period := group_m.GetLastSupportPeriod(time.Now())
	gsaa := group_m.GroupSupportAwardMgr{Period: period}
	awards, err := gsaa.Get(model.Db)
	if err != nil {
		return myContext, err
	}

	for _, i := range awards {
		if _, ok := sameImeiUsers[i.UserId]; ok {
			if i.UserId == userId {
				return myContext, bizerr.UserAlreadyAwarded
			} else {
				return myContext, bizerr.ImeiAlreadyAwarded
			}
		}
	}

	resp.ResponseOk(c, nil)
	return myContext, nil
}

// @Tags 群组
// @Summary 领取群支持奖励
// @Accept application/x-www-form-urlencoded
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupId path string true "群ID"
// @Param supporters formData string false "群支持者的externalId,用逗号分隔"
// @Success 200 {object} cv.AwardResult
// @Router /v1/imGroup/support/award/{groupId} [post]
func TakeSupportAward(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)

	//userId, err := getUserId(c)
	userId, lang, err := req.GetUserIdLang(c, myContext)
	if err != nil {
		return myContext, err
	}

	groupId := c.Param("groupId")
	if len(groupId) <= 0 {
		return myContext, myerr.NewSysError("groupId 为必填项")
	}

	model := domain.CreateModelContext(myContext)
	groupId, err = group_m.ToImGroupId(model, groupId)
	if err != nil {
		return myContext, err
	}

	pa, err := group_m.GetProfitAllocator(model, groupId)
	if err != nil {
		return myContext, err
	}
	if userId != pa {
		return myContext, bizerr.NoPrivileges
	}

	s := c.PostForm("supporters")
	extIds := make([]string, 0)
	extId2Uid := make(map[string]uint64, 0)
	uid2extId := make(map[uint64]string, 0)
	codeMap := make(map[mysql.ID]string, 0)
	if len(s) > 0 {
		extIds = strings.Split(s, ",")
		users, err := user_m.BatchGetUserByExtIds(model, extIds)
		if err != nil {
			return myContext, err
		}
		for _, i := range users {
			extId2Uid[i.ExternalId] = i.ID
			uid2extId[i.ID] = i.ExternalId
			codeMap[i.ID] = i.Code
		}
	}

	userIds := make([]uint64, 0)
	for _, i := range extIds {
		if _, ok := extId2Uid[i]; ok {
			userIds = append(userIds, extId2Uid[i])
		} else {
			// FIXME: report error
		}
	}

	//  检查是否已经发放了
	_, _, period := group_m.GetLastSupportPeriod(time.Now())
	gsa := group_m.GroupSupportAwardAdmin{Period: period, GroupUid: groupId}
	rows, err := gsa.Get(model.Db)
	if err != nil {
		return myContext, err
	}

	if len(rows) > 0 {
		return myContext, bizerr.GroupAlreadyAwarded
	}

	userIds, outUserIds, err := group_s.NewGroupService(myContext).GroupSupportList(groupId, userIds)
	model.Log.Infof("TakeSupportAward: %v, %v", userIds, outUserIds)

	model = domain.CreateModelContext(myContext)

	resSupportId, _, err := group_s.NewGroupService(myContext).GetSupportLevel(groupId)
	if err != nil {
		return myContext, err
	}

	if resSupportId <= 0 {
		return myContext, bizerr.NotQualified
	}

	// 检查userIds的ip限制
	userIp, err := user_m.GetUserIpMap(model.Db, userIds)
	if err != nil {
		return myContext, err
	}
	var sameIp = make(map[string]int)
	var ipUser = make(map[string]mysql.ID)
	for uid, ip := range userIp {
		sameIp[ip]++
		ipUser[ip] = uid
		if len(ip) > 0 {
			// IP已经领取过6次奖励,点击领奖校验则提示“ID:%s为重复账户
			var msg = fmt.Sprintf(bizerr.GroupSupportIpLimit.GetMsg(), codeMap[uid])
			if resMul, _ := res_m.GetResMultiTextBy(model.Db, msg_e.MSG_ID_REPEAT_ACCOUNT, lang); resMul != nil {
				msg = fmt.Sprintf(resMul.Content, codeMap[uid])
			}
			if times, err := group_c.GetGroupSupportAwardIpTimes(model, ip); err != nil || times >= 6 {
				return myContext, myerr.NewBusinessCodeNoCheck(bizerr.GroupSupportIpLimit.GetCode(), msg, myerr.BusinessData{})
			}
		}
	}
	for ip, cnt := range sameIp {
		if cnt >= 6 {
			var msg = fmt.Sprintf(bizerr.GroupSupportIpLimit.GetMsg(), ipUser[ip])
			if resMul, _ := res_m.GetResMultiTextBy(model.Db, msg_e.MSG_ID_REPEAT_ACCOUNT, lang); resMul != nil {
				msg = fmt.Sprintf(resMul.Content, ipUser[ip])
			}
			return myContext, myerr.NewBusinessCodeNoCheck(bizerr.GroupSupportIpLimit.GetCode(), msg, myerr.BusinessData{})
		}
	}

	// 真正地放奖励
	err = group_s.NewGroupService(myContext).GroupSupportAward(groupId, pa, userIds, resSupportId, period)
	if err != nil {
		return myContext, err
	}

	// 记录ip获奖
	for _, userId := range userIds {
		if ip, ok := userIp[userId]; ok && len(ip) > 0 {
			_, _ = group_c.IncrGroupSupportAwardIp(model, ip)
		}
	}

	// 保存记录
	if err = group_s.NewGroupService(myContext).RenewGroupSupporter(groupId, userIds); err != nil {
		model.Log.Warnf("TakeSupportAward, failed in saving group %s supporters", groupId)
	}

	result := group_cv.AwardResult{}
	for _, i := range userIds {
		result.Success = append(result.Success, uid2extId[i])
	}
	for _, i := range outUserIds {
		result.Failed = append(result.Failed, uid2extId[i])
	}

	resp.ResponseOk(c, result)
	return myContext, nil
}