group_power.go 3.83 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6
package group_power_s

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
7
	"git.hilo.cn/hilo-common/utils"
chenweijian's avatar
chenweijian committed
8 9
	"hilo-group/_const/enum/groupPower_e"
	"hilo-group/cv/group_power_cv"
hujiebin's avatar
hujiebin committed
10 11
	"hilo-group/domain/event/group_power_ev"
	"hilo-group/domain/model/groupPower_m"
chenweijian's avatar
chenweijian committed
12
	"hilo-group/domain/model/msg_m"
chenweijian's avatar
chenweijian committed
13
	"hilo-group/domain/model/noble_m"
chenweijian's avatar
chenweijian committed
14
	"hilo-group/domain/model/user_m"
hujiebin's avatar
hujiebin committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28
)

type GroupPowerService struct {
	svc *domain.Service
}

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

//用户加入国家势力
func (s *GroupPowerService) GroupPowerUserJoin(groupPowerId mysql.ID, userId mysql.ID) error {
	return s.svc.Transactional(func() error {
29
		model := domain.CreateModel(s.svc.CtxAndDb)
hujiebin's avatar
hujiebin committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		groupPower, err := groupPower_m.GetGroupPower(model, groupPowerId)
		if err != nil {
			return err
		}
		groupPowerUser, err := groupPower.UserJoin(userId)
		if err != nil {
			return err
		}
		if err := groupPowerUser.Persistent(); err != nil {
			return err
		}
		return group_power_ev.PublishGroupPowerJoin(model, &group_power_ev.GroupPowerJoinEvent{
			UserId:       userId,
			GroupPowerId: groupPowerId,
		})
	})
}

//用户退出国家势力
func (s *GroupPowerService) GroupPowerUserLeave(groupPowerId mysql.ID, userId mysql.ID) (int, error) {
	remainSeconds := 0
	err := s.svc.Transactional(func() error {
52
		model := domain.CreateModel(s.svc.CtxAndDb)
hujiebin's avatar
hujiebin committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		groupPower, err := groupPower_m.GetGroupPower(model, groupPowerId)
		if err != nil {
			return err
		}
		groupPowerUser, timeDiff, err := groupPower.UserLeave(userId)
		if err != nil {
			if timeDiff > 0 {
				remainSeconds = int(timeDiff.Seconds())
				return err
			}
			return err
		}
		if err := groupPowerUser.Persistent(); err != nil {
			return err
		}
		return group_power_ev.PublishGroupPowerLeave(model, &group_power_ev.GroupPowerLeaveEvent{
			UserId:       userId,
			GroupPowerId: groupPowerId,
		})
	})
	return remainSeconds, err
}
chenweijian's avatar
chenweijian committed
75 76 77 78 79 80 81 82

func GetGroupPowerMaxMemberNum(model *domain.Model, familyId uint64, groupPowerGrade groupPower_e.GroupPowerGrade) (maxNum int) {
	maxNum = group_power_cv.GroupPowerGradePrivilegeNum[groupPowerGrade][0].Num
	ownerId, err := groupPower_m.GetPowerOwner(model.Db, familyId)
	if err == nil {
		//检查是否是贵族
		if flag, err := noble_m.CheckNobleLevel(model.Db, ownerId, 6); err != nil {
		} else if flag {
chenweijian's avatar
chenweijian committed
83 84 85 86 87
			newNumMap := map[groupPower_e.GroupPowerGrade]int{
				groupPower_e.GroupPowerGrade0: 400,
				groupPower_e.GroupPowerGrade1: 600,
				groupPower_e.GroupPowerGrade2: 1000,
				groupPower_e.GroupPowerGrade3: 1400}
chenweijian's avatar
chenweijian committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			if newNum, ok := newNumMap[groupPowerGrade]; ok {
				maxNum = newNum
			}
		}
	}
	users, err := groupPower_m.GetGroupPowerUser(model, familyId)
	if err != nil {
		return
	}
	memberNum := len(users)
	if memberNum > maxNum {
		maxNum = memberNum
	}
	return
}
chenweijian's avatar
chenweijian committed
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

// 检查需不需要通知用户拉取家族小助手消息
func CheckoutSendMsgToMgr(myContext *mycontext.MyContext, groupPowerId uint64) {
	defer utils.CheckGoPanic()
	model := domain.CreateModelContext(myContext)
	//// 申请人数
	//cnt, err := groupPower_m.CountGroupPowerApply(model, groupPowerId, 0)
	//if err != nil {
	//	model.Log.Errorf("CheckoutSendMsgToMgr err:%v, id:%v", err, groupPowerId)
	//	return
	//}
	//if cnt <= 0 {
	//	return
	//}
	// 找到家族管理员们
	mgrList, err := groupPower_m.GetGroupPowerMgrList(model, groupPowerId)
	if err != nil {
		model.Log.Errorf("CheckoutSendMsgToMgr err:%v, id:%v", err, groupPowerId)
		return
	}
	userMap, err := user_m.GetUserMapByIds(model, mgrList)
	if err != nil {
		model.Log.Errorf("CheckoutSendMsgToMgr err:%v, id:%v", err, groupPowerId)
		return
	}
	for _, v := range mgrList {
		if user, ok := userMap[v]; ok {
			msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType)
		}
	}
}