inner.go 7.96 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7
package user_r

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"github.com/gin-gonic/gin"
hujiebin's avatar
hujiebin committed
8 9
	"hilo-user/_const/enum/cp_e"
	"hilo-user/cv/cp_cv"
hujiebin's avatar
hujiebin committed
10 11
	"hilo-user/cv/user_cv"
	"hilo-user/domain/model/bag_m"
hujiebin's avatar
hujiebin committed
12
	"hilo-user/domain/model/cp_m"
hujiebin's avatar
hujiebin committed
13
	"hilo-user/domain/model/noble_m"
hujiebin's avatar
hujiebin committed
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
	"hilo-user/domain/model/res_m"
	"hilo-user/domain/model/user_m"
	"hilo-user/resp"
	"time"
)

type MGetUserLevelReq struct {
	Ids []mysql.ID `form:"ids" binding:"required"`
}

// @Tags 用户-内部
// @Summary 批量获取用户等级
// @Param ids query string true "用户id,如:ids=1&ids=2&ids=3"
// @Success 200 {object} user_cv.MGetUserLevelData
// @Router /inner/user/levels [get]
func MGetUserLevels(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	var model = domain.CreateModelContext(myCtx)
	var req MGetUserLevelReq
	if err := c.ShouldBindQuery(&req); err != nil {
		return myCtx, err
	}
	wealthGrade, err := user_m.MGetWealthGrade(model, req.Ids)
	if err != nil {
		return myCtx, err
	}
	charmGrade, err := user_m.MGetCharmGrade(model, req.Ids)
hujiebin's avatar
hujiebin committed
41 42
	activeGrade, err := user_m.MGetActiveGrade(model, req.Ids)
	nobleLevel, err := noble_m.BatchGetNobleLevel(model, req.Ids)
hujiebin's avatar
hujiebin committed
43 44 45 46 47 48
	response := user_cv.MGetUserLevelData{}
	for _, userId := range req.Ids {
		response[userId] = user_cv.CvUserLevel{
			UserId:          userId,
			WealthUserGrade: wealthGrade[userId],
			CharmUserGrade:  charmGrade[userId],
hujiebin's avatar
hujiebin committed
49 50
			ActiveUserGrade: activeGrade[userId],
			NobleLevel:      nobleLevel[userId],
hujiebin's avatar
hujiebin committed
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
		}
	}

	resp.ResponseOk(c, response)
	return myCtx, nil
}

type GetUserBagReq struct {
	BagId mysql.ID `form:"bagId" binding:"required"`
}

// @Tags 用户-内部
// @Summary 获取单个背包
// @Param bagId query int true "背包id"
// @Success 200 {object} user_cv.UserBag
// @Router /inner/user/bag/id [get]
func GetUserBagId(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	var model = domain.CreateModelContext(myCtx)
	var req GetUserBagReq
	if err := c.ShouldBindQuery(&req); err != nil {
		return myCtx, err
	}
	bag, err := bag_m.GetUserBag(model, req.BagId)
	if err != nil {
		return myCtx, err
	}
	gift, err := res_m.FindResGift(model, bag.ResId)
	if err != nil {
		return myCtx, err
	}
	resp.ResponseOk(c, user_cv.UserBag{
		BagId:      bag.ID,
		ResType:    bag.ResType,
		ResId:      bag.ResId,
		GiftId:     bag.ResId,
		Name:       gift.Name,
		DiamondNum: gift.DiamondNum,
		IconUrl:    gift.IconUrl,
		SvgaUrl:    gift.SvagUrl,
		Count:      bag.Count,
		RemainDays: int(bag.EndTime.Sub(time.Now()).Hours() / 24),
	})
	return myCtx, nil
}
hujiebin's avatar
hujiebin committed
96 97

type GetUserCpReq struct {
hujiebin's avatar
hujiebin committed
98 99
	Id       mysql.ID `form:"id" binding:"required"`
	Language string   `form:"language"`
hujiebin's avatar
hujiebin committed
100 101 102 103 104
}

// @Tags 用户-内部
// @Summary 获取用户cp
// @Param id query int true "用户id"
hujiebin's avatar
hujiebin committed
105
// @Param language query string true "语言"
hujiebin's avatar
hujiebin committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
// @Success 200 {object} cp_cv.CvCp
// @Router /inner/user/cp [get]
func GetUserCp(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	var model = domain.CreateModelContext(myContext)
	var req GetUserCpReq
	if err := c.ShouldBindQuery(&req); err != nil {
		return myContext, err
	}
	userId := req.Id
	var response cp_cv.CvCp
	cpLevel := cp_e.CpLevel0
	cpRelation, exists := cp_m.GetCpRelation(model, userId)
	if !exists {
		resp.ResponseOk(c, response)
hujiebin's avatar
hujiebin committed
121
		return myContext, nil
hujiebin's avatar
hujiebin committed
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
	}
	var myPrivilegeList []cp_cv.CvPrivilege
	level := cp_m.GetCpLevel(model, cpRelation.ID)
	if level.ExpireAt.Before(time.Now()) {
		level.ExpireAt = time.Now().AddDate(0, 1, 0)
	}
	cpLevel = level.Level
	cpUserId := cpRelation.UserId2
	if cpUserId == userId {
		cpUserId = cpRelation.UserId1
	}
	userPrivileges, err := cp_m.MGetUserSvipPrivilege(model, []uint64{userId})
	if err != nil {
		return myContext, err
	}
	privilegeList := cp_cv.CopyCpLevelPrivilegeList(level.Level, "en")
	for i, v := range privilegeList {
		if v.CanSwitch {
			privilegeList[i].UserSwitch = userPrivileges[userId][v.Type]
		}
	}
	// 我的特权,有开关并且打开才返回
	for i, v := range privilegeList {
		if !v.CanSwitch {
			myPrivilegeList = append(myPrivilegeList, privilegeList[i])
		} else if v.UserSwitch {
			myPrivilegeList = append(myPrivilegeList, privilegeList[i])
		}
	}
hujiebin's avatar
hujiebin committed
151 152 153 154
	userBases, err := user_cv.GetUserBaseMap([]uint64{cpUserId}, userId)
	if err != nil {
		return myContext, err
	}
hujiebin's avatar
hujiebin committed
155
	// 返回值
hujiebin's avatar
hujiebin committed
156 157 158 159
	title := ""
	if msgId, ok := cp_e.CpLevelTitle[level.Level]; ok {
		title = cp_cv.GetTranslate(msgId, req.Language)
	}
hujiebin's avatar
hujiebin committed
160
	response = cp_cv.CvCp{
hujiebin's avatar
hujiebin committed
161
		CpUserInfo: userBases[cpUserId],
hujiebin's avatar
hujiebin committed
162
		CpLevel: cp_cv.CvCpLevel{
hujiebin's avatar
hujiebin committed
163 164 165
			Level:  cpLevel,
			Points: cp_e.CpLevelPoints[cpLevel] + level.Points,
			Title:  title,
hujiebin's avatar
hujiebin committed
166
		},
hujiebin's avatar
hujiebin committed
167
		MyPrivilegeList: myPrivilegeList,
hujiebin's avatar
hujiebin committed
168 169 170 171
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}
hujiebin's avatar
hujiebin committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

// @Tags 用户-内部
// @Summary 获取用户cp关系
// @Param id query int true "用户id"
// @Success 200 {object} cp_cv.CvCpRelation
// @Router /inner/user/cpRelation [get]
func GetUserCpRelation(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	var model = domain.CreateModelContext(myContext)
	var req GetUserCpReq
	if err := c.ShouldBindQuery(&req); err != nil {
		return myContext, err
	}
	userId := req.Id
	var response cp_cv.CvCpRelation
	cpRelation, exists := cp_m.GetCpRelation(model, userId)
	if !exists {
		resp.ResponseOk(c, response)
		return myContext, nil
	}
	cpUserId := cpRelation.UserId2
	if cpUserId == userId {
		cpUserId = cpRelation.UserId1
	}
hujiebin's avatar
hujiebin committed
196
	cpUser, _ := user_m.GetUser(model, cpUserId)
hujiebin's avatar
hujiebin committed
197
	response = cp_cv.CvCpRelation{
hujiebin's avatar
hujiebin committed
198 199 200 201
		CpId:         cpRelation.ID,
		UserId:       userId,
		CpUserId:     cpUserId,
		CpUserAvatar: cpUser.Avatar,
hujiebin's avatar
hujiebin committed
202 203 204 205
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}
hujiebin's avatar
hujiebin committed
206

hujiebin's avatar
hujiebin committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
type MGetUserCpRelationReq struct {
	Ids []mysql.ID `form:"ids" binding:"required"`
}

// @Tags 用户-内部
// @Summary 批量获取用户cp关系
// @Param ids query int true "用户ids"
// @Success 200 {object} map[uint64]cp_cv.CvCpRelation
// @Router /inner/user/cpRelations [get]
func MGetUserCpRelation(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	var model = domain.CreateModelContext(myContext)
	var req MGetUserCpRelationReq
	if err := c.ShouldBindQuery(&req); err != nil {
		return myContext, err
	}
	cpRelations := cp_m.MGetCpRelation(model, req.Ids)
	var m = make(map[uint64]cp_m.CpRelationTmp)
hujiebin's avatar
hujiebin committed
225
	var userIds []uint64
hujiebin's avatar
hujiebin committed
226 227 228
	for i, v := range cpRelations {
		m[v.UserId1] = cpRelations[i]
		m[v.UserId2] = cpRelations[i]
hujiebin's avatar
hujiebin committed
229 230 231 232 233 234
		userIds = append(userIds, v.UserId1)
		userIds = append(userIds, v.UserId2)
	}
	users, err := user_m.GetUserMapByIds(model, userIds)
	if err != nil {
		return myContext, err
hujiebin's avatar
hujiebin committed
235 236 237 238 239 240 241 242 243
	}
	var response = make(map[uint64]cp_cv.CvCpRelation)
	for _, uid := range req.Ids {
		if cpRelation, ok := m[uid]; ok {
			cpUserId := cpRelation.UserId2
			if cpUserId == uid {
				cpUserId = cpRelation.UserId1
			}
			response[uid] = cp_cv.CvCpRelation{
hujiebin's avatar
hujiebin committed
244 245 246 247
				CpId:         cpRelation.ID,
				UserId:       uid,
				CpUserId:     cpUserId,
				CpUserAvatar: users[cpUserId].Avatar,
hujiebin's avatar
hujiebin committed
248 249 250 251 252 253 254
			}
		}
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}

hujiebin's avatar
hujiebin committed
255 256 257 258 259 260 261 262
type GetUserCpPairReq struct {
	Ids []mysql.ID `form:"ids" binding:"required"`
}

// @Tags 用户-内部
// @Summary 给出指定uids下的cp对
// @Param ids query string true "用户id,如:ids=1&ids=2&ids=3"
// @Success 200 {object} [][]uint64
hujiebin's avatar
hujiebin committed
263
// @Router /inner/user/cp/pair [get]
hujiebin's avatar
hujiebin committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
func GetUserCpPair(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	var model = domain.CreateModelContext(myContext)
	var req GetUserCpPairReq
	if err := c.ShouldBindQuery(&req); err != nil {
		return myContext, err
	}
	userIds := req.Ids
	m := make(map[mysql.ID]bool)
	for _, uid := range userIds {
		m[uid] = true
	}
	pairs := cp_m.MGetCpRelation(model, userIds)
	var response [][2]uint64
	for _, pair := range pairs {
		if m[pair.UserId1] && m[pair.UserId2] {
			response = append(response, [2]mysql.ID{pair.UserId1, pair.UserId2})
		}
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}