package group_power_s import ( "encoding/json" "git.hilo.cn/hilo-common/_const/enum/diamond_e" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/txop/award_tx" "git.hilo.cn/hilo-common/utils" "github.com/jinzhu/now" "hilo-group/_const/enum/groupPower_e" "hilo-group/_const/enum/msg_e" "hilo-group/cv/group_power_cv" "hilo-group/cv/user_cv" "hilo-group/domain/event/group_power_ev" "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/user_m" "hilo-group/myerr/bizerr" "time" ) 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 { model := domain.CreateModel(s.svc.CtxAndDb) 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 { model := domain.CreateModel(s.svc.CtxAndDb) 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 } func GetGroupPowerRankResp(model *domain.Model, beginDate, endDate string, userId uint64) (response group_power_cv.CvGroupPowerRank, err error) { rank, err := groupPower_m.GetGroupPowerExpRank(model, beginDate, endDate, 30) if err != nil { return } var ids []mysql.ID for _, g := range rank { ids = append(ids, g.GroupPowerId) } var myGroupPower *groupPower_m.GroupPowerUser if userId > 0 { myGroupPower, err = groupPower_m.GetGroupPowerUserOrNil(model, userId) if err != nil { return } } if myGroupPower != nil { ids = append(ids, myGroupPower.GroupPowerId) } grades, err := groupPower_m.MGetGroupPowerGrade(model, ids) if err != nil { return } groupPowers, err := groupPower_m.MGetGroupPowerInfoMap(model, ids) if err != nil { return } if myGroupPower != nil { myGroupPowerRank, err := groupPower_m.GetMyGroupPowerExpRank(model, beginDate, endDate, myGroupPower.GroupPowerId) if err != nil { return response, err } response.MyGroupPower = &group_power_cv.CvGroupPowerRankData{ CvGroupPowerBase: group_power_cv.CvGroupPowerBase{ Id: myGroupPower.GroupPowerId, Icon: groupPowers[myGroupPower.GroupPowerId].Icon, Name: groupPowers[myGroupPower.GroupPowerId].Name, Nameplate: groupPowers[myGroupPower.GroupPowerId].Nameplate, }, CvGroupPowerGrade: group_power_cv.CvGroupPowerGrade{ Grade: grades[myGroupPower.GroupPowerId].Grade, Exp: myGroupPowerRank.Exp, }, Rank: 0, } } for _, v := range rank { if response.MyGroupPower != nil && v.GroupPowerId == response.MyGroupPower.Id { response.MyGroupPower.Rank = v.Rank } response.Items = append(response.Items, group_power_cv.CvGroupPowerRankData{ CvGroupPowerBase: group_power_cv.CvGroupPowerBase{ Id: v.GroupPowerId, Icon: groupPowers[v.GroupPowerId].Icon, Name: groupPowers[v.GroupPowerId].Name, Nameplate: groupPowers[v.GroupPowerId].Nameplate, }, CvGroupPowerGrade: group_power_cv.CvGroupPowerGrade{ Grade: grades[v.GroupPowerId].Grade, Exp: v.Exp, }, Rank: v.Rank, }) } if response.MyGroupPower != nil && response.MyGroupPower.Rank == 0 { response.MyGroupPower.Rank = 31 // 客户端统一显示30+ } return response, nil } func GetGroupPowerStar(model *domain.Model, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int) ([]*group_power_cv.CvGroupPowerStarData, error) { rank, err := groupPower_m.GetGroupPowerMonthStarRank(model, groupPowerId, _type, offset, limit) if err != nil { return nil, err } //var response []group_power_cv.CvGroupPowerStarData response := make([]*group_power_cv.CvGroupPowerStarData, 0) var userIds []mysql.ID for _, row := range rank { userIds = append(userIds, row.UserId) } users, err := user_m.GetUserMapByIds(model, userIds) for _, row := range rank { user := users[row.UserId] score := row.Score if _type == groupPower_e.GroupPowerStarTypeActive { score = score / 60 } if score <= 0 { continue } response = append(response, &group_power_cv.CvGroupPowerStarData{ User: user_cv.CvUserTiny{ Id: user.ID, ExternalId: user.ExternalId, Code: user.Code, Nick: user.Nick, Avatar: user.Avatar, }, Score: score, }) } return response, nil } func CalcGroupPowerMonthRankAct(model *domain.Model) error { lastDayTime := time.Now().AddDate(0, 0, -1) _now := now.New(lastDayTime) calcMonth := _now.BeginningOfMonth().Format(utils.MONTH_FORMAT) beginDate, endDate := _now.BeginningOfMonth().Format("2006-01-02"), _now.EndOfMonth().Format("2006-01-02") // 计算获奖 response, err := GetGroupPowerRankResp(model, beginDate, endDate, 0) if err != nil { return err } powerIds := make([]uint64, 0, len(response.Items)) for i, v := range response.Items { powerIds = append(powerIds, v.Id) response.Items[i].StarList, err = GetGroupPowerStar(model, v.Id, groupPower_e.GroupPowerStarTypeFamous, 0, 10) if err != nil { return err } } // 读奖励配置 type award struct { MedalId uint64 MedalDay uint32 Diamond uint32 Noble uint32 NobleDay uint32 HeaddressId uint64 HeaddressDay int } confMap := map[int]map[int]*award{ 1: { 0: {MedalId: 1, MedalDay: 30, Diamond: 300000, Noble: 5, NobleDay: 15}, 1: {MedalId: 1, MedalDay: 30, Diamond: 100000, Noble: 5, NobleDay: 7}, 4: {HeaddressId: 1, HeaddressDay: 30}, }, 2: { 0: {MedalId: 1, MedalDay: 30, Diamond: 200000, Noble: 4, NobleDay: 15}, 1: {MedalId: 1, MedalDay: 30, Diamond: 80000, Noble: 4, NobleDay: 7}, 4: {HeaddressId: 1, HeaddressDay: 30}, }, 3: { 0: {MedalId: 1, MedalDay: 30, Diamond: 100000, Noble: 5, NobleDay: 5}, 1: {MedalId: 1, MedalDay: 30, Diamond: 50000}, 4: {HeaddressId: 1, HeaddressDay: 30}, }, 4: { 0: {MedalId: 1, MedalDay: 30}, 1: {MedalId: 1, MedalDay: 30, HeaddressId: 1, HeaddressDay: 30}, }, } awardList := make([]*award_tx.UserAward, 0, 50) // 奖励 ownerMap, err := groupPower_m.GetPowerOwnerMap(model, powerIds) if err != nil { return err } // log list logList := make([]*groupPower_m.ActFamilyMonthRankLog, 0, 50) for _, v := range response.Items { teamRank := 0 if v.Rank >= 1 && v.Rank <= 3 { teamRank = v.Rank } else if v.Rank >= 4 && v.Rank <= 10 { teamRank = 4 } else { break } ownerId, ok := ownerMap[v.Id] if !ok || ownerId <= 0 { model.Log.Errorf("CalcGroupPowerMonthRankAct 获取势力主错误 familyId:%d", v.Id) return bizerr.InvalidParameter } uRankConfMap, tCOk := confMap[teamRank] if !tCOk { break } oAward, aOk := uRankConfMap[0] if !aOk { model.Log.Errorf("CalcGroupPowerMonthRankAct 配置错误:%v", confMap) return bizerr.InvalidParameter } oAwardJ, _ := json.Marshal(oAward) logList = append(logList, &groupPower_m.ActFamilyMonthRankLog{ Period: calcMonth, FamilyId: v.Id, RankFamily: v.Rank, RankUser: 0, UserId: ownerId, Award: string(oAwardJ), }) awardList = append(awardList, &award_tx.UserAward{MedalId: oAward.MedalId, MedalDuration: oAward.MedalDay, Diamond: oAward.Diamond, NobleLevel: oAward.Noble, NobleDuration: oAward.NobleDay}) for i, u := range v.StarList { uRank := i + 1 confIdx := 9 if uRank >= 1 && uRank <= 3 { confIdx = 1 } else if uRank >= 4 && uRank <= 10 { confIdx = 4 } award, aOk := uRankConfMap[confIdx] if !aOk { break } awardJ, _ := json.Marshal(award) logList = append(logList, &groupPower_m.ActFamilyMonthRankLog{ Period: calcMonth, FamilyId: v.Id, RankFamily: v.Rank, RankUser: uRank, UserId: u.User.Id, Award: string(awardJ), }) awardList = append(awardList, &award_tx.UserAward{MedalId: award.MedalId, MedalDuration: award.MedalDay, Diamond: award.Diamond, NobleLevel: award.Noble, NobleDuration: award.NobleDay, HeaddressId: award.HeaddressId, HeaddressDuration: award.HeaddressDay}) } } return model.Transaction(func(model *domain.Model) error { // log err = groupPower_m.CreateActFamilyMonthRankLog(model, logList) if err != nil { model.Log.Errorf("CalcGroupPowerMonthRankAct err:%+v", err) return err } // award for _, v := range awardList { err = award_tx.SendUserAward(model, v, diamond_e.GeneralActivity, msg_e.MgrSendDiamondProperty) if err != nil { model.Log.Errorf("CalcGroupPowerMonthRankAct award:%+v, err:%+v", v, err) return err } } return nil }) }