group_power.go 1.82 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
package group_power_s

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"hilo-group/domain/event/group_power_ev"
	"hilo-group/domain/model/groupPower_m"
)

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 {
23
		model := domain.CreateModel(s.svc.CtxAndDb)
hujiebin's avatar
hujiebin committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
		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 {
46
		model := domain.CreateModel(s.svc.CtxAndDb)
hujiebin's avatar
hujiebin committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		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
}