cp.go 2.34 KB
Newer Older
hujiebin's avatar
hujiebin committed
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
package cp_s

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"hilo-user/_const/enum/cp_e"
	"hilo-user/cv/cp_cv"
	"hilo-user/cv/user_cv"
	"hilo-user/domain/model/cp_m"
	"time"
)

type CpService struct {
	svc *domain.Service
}

func NewCpService(myContext *mycontext.MyContext) *CpService {
	svc := domain.CreateService(myContext)
	return &CpService{svc}
}

// 获取用户cp信息
func (s *CpService) GetUserCp(userId uint64, lang string) (*user_cv.CvCp, error) {
	var model = domain.CreateModelContext(s.svc.MyContext)
	cpLevel := cp_e.CpLevel0
	cpRelation, exists := cp_m.GetCpRelation(model, userId)
	if !exists {
		return nil, nil
	}
	var myPrivilegeList []user_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 nil, err
	}
	privilegeList := cp_cv.CopyCpLevelPrivilegeList(level.Level, "en")
	for i, v := range privilegeList {
		if v.CanSwitch {
			privilegeList[i].UserSwitch = userPrivileges[userId][v.Type]
		}
	}
	// 我的特权,有开关并且打开才返回
	for _, v := range privilegeList {
		if !v.CanSwitch {
			myPrivilegeList = append(myPrivilegeList, user_cv.CvPrivilege{Type: v.Type})
		} else if v.UserSwitch {
			myPrivilegeList = append(myPrivilegeList, user_cv.CvPrivilege{Type: v.Type})
		}
	}
	userBases, err := user_cv.GetUserBaseForCpMap([]uint64{cpUserId})
	if err != nil {
		return nil, err
	}
	// 返回值
	title := cp_cv.GetTranslate(237, lang)
	if msgId, ok := cp_e.CpLevelTitle[level.Level]; ok {
		title = cp_cv.GetTranslate(msgId, lang)
	}
	var oldScore uint32
	if oldCp, _ := cp_m.GetOldConnectCp(model, cpRelation.UserId1, cpRelation.UserId2); oldCp != nil {
		oldScore = uint32(oldCp.Score)
	}
	response := &user_cv.CvCp{
		CpUserInfo: userBases[cpUserId],
		CpLevel: user_cv.CvCpLevel{
			Level:  cpLevel,
			Points: oldScore + cp_m.SumCpPoints(model, cpRelation.Id), // 历史分数
			Title:  title,
		},
		MyPrivilegeList: myPrivilegeList,
		CpDays:          int(time.Now().Sub(cpRelation.CreatedTime).Hours()/24) + 1,
		CreatedUnix:     cpRelation.CreatedTime.Unix(),
	}
	return response, nil
}