互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

package bahe_service
import (
"fmt"
"hudongzhuanjia/models"
"hudongzhuanjia/utils/define"
"github.com/ouxuanserver/osmanthuswine/src/core"
)
type CountMembersResult struct {
Number int `json:"number,omitempty"`
TeamId int64 `json:"team_id,omitempty"`
Color string `json:"color,omitempty"`
}
func CountMembers(baheId int64) ([]*CountMembersResult, error) {
result := make([]*CountMembersResult, 0)
err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
Select("count(m.id) as number, t.id as team_id, t.bahe_team_name as color").
Join("left", "ox_bahe_team_member as m", "m.team_id=t.id and m.is_delete=0").
Where("t.is_delete=0 and t.bahe_activity_id = ?", baheId).Desc("color").
GroupBy("t.id").Find(&result)
return result, err
}
func GetMember(bid, rid, uid int64) (*models.BaheTeamMember, bool, error) {
member := new(models.BaheTeamMember)
exist, err := core.GetXormAuto().Where("is_delete=0 and rehearsal_id=? and "+
"bahe_activity_id=? and member_id=?", rid, bid, uid).Get(member)
return member, exist, err
}
func GetCurrentTugWar(aid, uid, rid int64) (map[string]interface{}, error) {
bahe := new(models.TugOfWar)
exist, err := bahe.GetCurrent(aid)
if err != nil {
return nil, err
}
if !exist {
return nil, fmt.Errorf("轮次尚未开启")
}
member := new(models.BaheTeamMember)
exist, err = member.GetMemberByBaheIdAndUserId(uid, bahe.Id, rid)
if err != nil {
return nil, err
}
if exist {
return map[string]interface{}{
"member": member,
}, nil
}
if bahe.Model == define.TUGWAR_MODEL_RANDOM {
return map[string]interface{}{
"model": bahe.Model,
"bahe_activity_id": bahe.Id,
}, nil
}
res, err := CountMembers(bahe.Id)
if err != nil {
return nil, err
}
return map[string]interface{}{
"list": res,
"model": bahe.Model,
"bahe_activity_id": bahe.Id,
"limit": bahe.Number,
}, nil
}
type JoinTeamResult struct {
Number int
TeamId int64
TeamName string
}
func GetJoinTeamByTeamId(teamId, rehearsalId interface{}) (*JoinTeamResult, bool, error) {
result := new(JoinTeamResult)
exist, err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number").
Join("LEFT", new(models.BaheTeamMember).Alias("m"),
"m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", rehearsalId).
Where("t.is_delete=0 and t.id=?", teamId).GroupBy("t.id").Get(result)
return result, exist, err
}
func GetJoinTeamByBaheId(baheId, rehearsalId interface{}) (*JoinTeamResult, bool, error) {
result := new(JoinTeamResult)
exist, err := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number").
Join("LEFT", new(models.BaheTeamMember).Alias("m"),
"m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", rehearsalId).
Where("t.is_delete=0 and t.bahe_activity_id=? ", baheId).
GroupBy("t.id").Asc("number").Get(result)
return result, exist, err
}
//
//team := new(JoinTeamResult)
//session := core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t").
// Select("t.id as team_id, t.bahe_team_name as team_name, count(m.id) as number").
// Join("LEFT", new(models.BaheTeamMember).Alias("m"),
// "m.team_id=t.id and m.is_delete=0 and m.rehearsal_id=?", activity.RehearsalId)
//if teamId == 0 { // 人数最少的一队
// session = session.Where("t.is_delete=0 and t.bahe_activity_id=? ", bahe.Id).
// GroupBy("t.id").Asc("number")
//} else {
// session = session.Where("t.is_delete=0 and t.id=?", teamId).GroupBy("t.id")
//}
//exist, err = session.Get(team)