space.go 4.72 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10
package cp_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"
	"hilo-user/_const/enum/cp_e"
	"hilo-user/cv/cp_cv"
	"hilo-user/cv/user_cv"
hujiebin's avatar
hujiebin committed
11
	"hilo-user/domain/event/cp_ev"
hujiebin's avatar
hujiebin committed
12
	"hilo-user/domain/model/cp_m"
hujiebin's avatar
hujiebin committed
13 14 15 16
	"hilo-user/domain/model/user_m"
	"hilo-user/myerr/bizerr"
	"hilo-user/req"
	"hilo-user/resp"
hujiebin's avatar
hujiebin committed
17
	"time"
hujiebin's avatar
hujiebin committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
)

// @Tags CP v2
// @Summary cp空间
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param externalId query string true "查看用户的externalId"
// @Success 200 {object} cp_cv.CvSpace
// @Router /v2/cp/space [get]
func CpSpace(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, lang, err := req.GetUserIdLang(c, myContext)
	if err != nil {
		return myContext, err
	}
	externalId := c.Query("externalId")
	if len(externalId) <= 0 {
		return myContext, bizerr.InvalidParameter
	}
	var model = domain.CreateModelContext(myContext)
	userInfo, err := user_m.GetUser(model, userId)
	if err != nil {
		return myContext, err
	}
hujiebin's avatar
hujiebin committed
42
	targetUserInfo, err := user_m.GetUserByExtId(model, externalId)
hujiebin's avatar
hujiebin committed
43 44 45
	if err != nil {
		return myContext, err
	}
hujiebin's avatar
hujiebin committed
46
	var cpUserInfo *user_m.User
hujiebin's avatar
hujiebin committed
47
	expireAtUnix, cpLevel := int64(0), cp_e.CpLevel0
hujiebin's avatar
hujiebin committed
48
	visitTimes := int64(0)
hujiebin's avatar
hujiebin committed
49 50 51
	settlementDate, cpDays, applyToUnbind := "", 0, false
	nextPoints, curPoints := cp_e.CpLevelPoints[cp_e.CpLevel1], mysql.Num(0)
	cpRelation, exists := cp_m.GetCpRelation(model, targetUserInfo.ID)
hujiebin's avatar
hujiebin committed
52 53 54
	if exists {
		level := cp_m.GetCpLevel(model, cpRelation.ID)
		cpLevel, curPoints = level.Level, level.Points
hujiebin's avatar
hujiebin committed
55 56
		expireAtUnix, settlementDate = level.ExpireAt.Unix(), level.ExpireAt.Format("2006-01-02")
		cpDays = int(time.Now().Sub(cpRelation.CreatedTime).Hours()/24) + 1
hujiebin's avatar
hujiebin committed
57 58 59 60 61 62 63 64 65
		// cp 存在的时候,发布访问cp空间事件
		if err := cp_ev.PublishCpSpaceVisit(model, &cp_ev.SpaceVisitEvent{
			UserId:  userId,
			CpId:    cpRelation.ID,
			UserId1: cpRelation.UserId1,
			UserId2: cpRelation.UserId2,
		}); err != nil {
			return myContext, err
		}
hujiebin's avatar
hujiebin committed
66 67 68 69 70 71
		visitTimes = cp_m.CountCpSpaceVisitors(model, cpRelation.ID)
		cpUserId := cpRelation.UserId2
		if cpUserId == userId {
			cpUserId = cpRelation.UserId1
		}
		applyToUnbind = cp_m.GetApplyToUnbind(model, userId, cpUserId)
hujiebin's avatar
hujiebin committed
72 73 74 75
		cpUserInfo, err = user_m.GetUser(model, cpUserId)
		if err != nil {
			return myContext, err
		}
hujiebin's avatar
hujiebin committed
76
	}
77 78
	if cpLevel != cp_e.CpLevelMax {
		nextPoints = cp_e.CpLevelPoints[cpLevel+1]
hujiebin's avatar
1  
hujiebin committed
79 80
	} else {
		nextPoints = cp_e.CpLevelPoints[cp_e.CpLevelMax]
hujiebin's avatar
hujiebin committed
81
	}
hujiebin's avatar
hujiebin committed
82 83 84 85
	userPrivileges, err := cp_m.MGetUserSvipPrivilege(model, []uint64{userId})
	if err != nil {
		return myContext, err
	}
hujiebin's avatar
hujiebin committed
86 87 88 89 90 91 92
	allPrivilegeList := make([][]cp_cv.CvPrivilege, cp_e.CpLevelMax+1)
	for level := cp_e.CpLevel0; level <= cp_e.CpLevelMax; level++ {
		privilegeList := cp_cv.CopyCpLevelPrivilegeList(level, lang)
		for i, v := range privilegeList {
			if v.CanSwitch {
				privilegeList[i].UserSwitch = userPrivileges[userId][v.Type]
			}
hujiebin's avatar
hujiebin committed
93
		}
hujiebin's avatar
hujiebin committed
94
		allPrivilegeList[level] = privilegeList
hujiebin's avatar
hujiebin committed
95
	}
hujiebin's avatar
hujiebin committed
96
	// 返回值
hujiebin's avatar
hujiebin committed
97
	response := cp_cv.CvSpace{
98
		CpLevel: cp_cv.CvCpLevel{
hujiebin's avatar
hujiebin committed
99 100 101 102 103 104
			Level:          cpLevel,
			Points:         cp_e.CpLevelPoints[cpLevel] + curPoints,
			StartPoints:    cp_e.CpLevelPoints[cpLevel],
			EndPoints:      nextPoints,
			ExpireAtUnix:   expireAtUnix,
			SettlementDate: settlementDate,
hujiebin's avatar
hujiebin committed
105
		},
106
		ResLevelList:  cp_cv.CvResLevelList,
hujiebin's avatar
hujiebin committed
107
		PrivilegeList: allPrivilegeList,
hujiebin's avatar
hujiebin committed
108
	}
hujiebin's avatar
hujiebin committed
109 110 111 112 113 114 115 116 117
	response.CpInfo = cp_cv.CvCpInfo{
		UserInfo:      user_cv.UserToTiny(*userInfo),
		CpDays:        cpDays,
		VisitTimes:    visitTimes,
		ApplyToUnbind: applyToUnbind,
	}
	if cpUserInfo != nil {
		response.CpInfo.CpUserInfo = user_cv.UserToTiny(*cpUserInfo)
	}
hujiebin's avatar
hujiebin committed
118 119 120
	resp.ResponseOk(c, response)
	return myContext, nil
}
hujiebin's avatar
hujiebin committed
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

type CpPrivilegeOpenCloseReq struct {
	Type      cp_e.CpPrivilege `form:"type" binding:"required"`
	OpenClose mysql.OpenClose  `form:"openClose" binding:"required"`
}

// @Tags CP v2
// @Summary cp特权开关
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param type formData int true "1.空间 2.横幅 3.等级勋章 4.证书 5.进场特效 6.头像头饰 7.动态资料卡 8.麦位特效"
// @Param openClose formData int true "1:open 2:close"
// @Success 200
// @Router /v2/cp/privilege/openClose [put]
func CpPrivilegeOpenClose(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	var request CpPrivilegeOpenCloseReq
	if err := c.ShouldBind(&request); err != nil {
		return myCtx, err
	}
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	var model = domain.CreateModelContext(myCtx)
	if err := cp_m.OpenCLoseUserSvipPrivilege(model, userId, request.Type, request.OpenClose); err != nil {
		return myCtx, err
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}