package pc import ( "fmt" "hudongzhuanjia/controllers" "hudongzhuanjia/models" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time" "github.com/ouxuanserver/osmanthuswine/src/core" ) type TugOfWarCtl struct { controllers.AuthorCtl } func (t *TugOfWarCtl) Ready() { // 准备中 baheId := t.MustGetInt("bahe_activity_id") bahe := new(models.TugOfWar) exist, err := models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在") if bahe.Status != define.StatusNotBegin { t.ERROR(fmt.Sprintf("该活动%s", bahe.Status), code.MSG_ERR) } // done: 把其他的准备中的状态改为未开始 _, err = bahe.UpdateToStatusByAid(bahe.ActivityId, define.StatusReady, define.StatusNotBegin) t.CheckErr(err) // 创建队伍 只容许创建一次 redTeam := &models.BaheTeam{ BaheTeamName: "red", ActivityId: bahe.ActivityId, BaheActivityId: bahe.Id, IsDelete: false, CreatedAt: time.Now(), UpdatedAt: time.Now(), } err = models.Save(map[string]interface{}{ "bahe_activity_id=": bahe.Id, "is_delete=": 0, "bahe_team_name=": define.TUGWAR_TEAM_RED, }, redTeam) t.CheckErr(err) blueTeam := &models.BaheTeam{ BaheTeamName: "blue", ActivityId: bahe.ActivityId, BaheActivityId: bahe.Id, IsDelete: false, CreatedAt: time.Now(), UpdatedAt: time.Now(), } err = models.Save(map[string]interface{}{ "bahe_activity_id=": bahe.Id, "is_delete=": 0, "bahe_team_name=": define.TUGWAR_TEAM_BULE, }, blueTeam) t.CheckErr(err) _, err = bahe.UpdateStatusById(bahe.Id, define.StatusReady) t.CheckErr(err) t.SUCCESS("操作成功") } func (t *TugOfWarCtl) Start() { baheId := t.MustGetInt("bahe_activity_id") bahe := new(models.TugOfWar) exist, err := models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在") if bahe.Status != define.StatusReady { t.ERROR(fmt.Sprintf("该活动%s", bahe.Status), code.MSG_ERR) } _, err = bahe.UpdateStatusById(baheId, define.StatusRunning) t.CheckErr(err) t.SUCCESS("操作成功") } func (t *TugOfWarCtl) Stop() { baheId := t.MustGetInt("bahe_activity_id") bahe := new(models.TugOfWar) exist, err := models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "拔河不存在") if bahe.Status != define.StatusRunning { t.ERROR(fmt.Sprintf("该活动%s", bahe.Status), code.MSG_ERR) } bahe.Status = define.StatusEnding bahe.UpdatedAt = time.Now() _, err = bahe.UpdateStatusById(bahe.Id, bahe.Status) t.CheckErr(err) t.SUCCESS("操作成功") } func (t *TugOfWarCtl) Team() { baheId := t.MustGetInt("bahe_activity_id") customerId := t.GetAccountId() customer := new(models.Customer) exist, err := models.Get(customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") bahe := new(models.TugOfWar) exist, err = models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在") if bahe.Status == define.StatusNotBegin { t.ERROR("拔河尚未开始", code.MSG_TUGWAR_NOT_EXIST) return } activity := new(models.Activity) exist, err = models.Get(activity, bahe.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") area := new(models.AreaStore) exist, err = area.GetByCustomerId(customerId, bahe.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区信息异常") //获取队伍信息: 名字、二维码、颜色 teams := make([]*models.BaheTeam, 0) total, err := core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", bahe.Id).FindAndCount(&teams) t.CheckErr(err) // 队伍颜色 qrcode := "" // 二维码 link := define.H5TugOfWar if customer.IsSpecial == 1 { link = define.H5TugOfWar2020 } if bahe.Model != "随机分配模式" { // 非随机模式,自选队伍 for index := range teams { qrcode, err = utils.GenH5Qrcode(link, map[string]interface{}{ "area_id": area.Id, "activity_id": bahe.ActivityId, "customer_id": customerId, "bahe_activity_id": bahe.Id, "bahe_team_id": teams[index].Id, "rehearsal_id": activity.RehearsalId, }) t.CheckErr(err) teams[index].Qrcode = qrcode } } else { qrcode, err = utils.GenH5Qrcode(link, map[string]interface{}{ "area_id": area.Id, "activity_id": bahe.ActivityId, "customer_id": customerId, "bahe_activity_id": bahe.Id, "bahe_team_id": 0, "rehearsal_id": activity.RehearsalId, }) t.CheckErr(err) } t.JSON(map[string]interface{}{ "total": total, "list": teams, "qrcode": qrcode, "model": bahe.Model, }) } func (t *TugOfWarCtl) Member() { baheId := t.MustGetInt("bahe_activity_id") bahe := &models.TugOfWar{} exist, err := models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在") activity := new(models.Activity) exist, err = models.Get(activity, bahe.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") teams := make([]*models.BaheTeam, 0) err = core.GetXormAuto().Where("is_delete=0 and bahe_activity_id=?", baheId).Find(&teams) t.CheckErr(err) var total int64 = 0 for index := range teams { members := make([]*models.BaheTeamMember, 0) num, err := core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=? and arch_id=?", teams[index].Id, activity.RehearsalId, activity.ArchId).Desc("score"). Asc("sort_time").FindAndCount(&members) t.CheckErr(err) total += num teams[index].Members = members } t.JSON(map[string]interface{}{ "total": total, "list": teams, }) } type BaheScoreResult struct { TotalScore int `json:"total_score" description:"总分数"` Id int `json:"id" description:"id"` ActivityId int `json:"activity_id" description:"活动id"` BaheActivityId int `json:"bahe_activity_id" description:"拔河活动id"` BaheTeamName string `json:"bahe_team_name" description:"拔河队伍得名称"` Members []*models.BaheTeamMember `json:"members"` } func (t *TugOfWarCtl) Score() { baheId := t.MustGetInt("bahe_activity_id") bahe := &models.TugOfWar{} exist, err := models.Get(bahe, baheId) t.CheckErr(err) t.Assert(exist, code.MSG_TUGWAR_NOT_EXIST, "拔河不存在") activity := new(models.Activity) exist, err = models.Get(activity, bahe.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") teams := make([]*BaheScoreResult, 0) err = core.GetXormAuto().Table(new(models.BaheTeam)).Alias("t"). Select("sum(m.score) as total_score, t.id, t.activity_id, t.bahe_activity_id, t.bahe_team_name"). Join("LEFT", new(models.BaheTeamMember).Alias("m"), "t.id=m.team_id and m.is_delete=0 and m.rehearsal_id=? and m.arch_id=?", activity.RehearsalId, activity.ArchId).Where("t.is_delete=0 and t.bahe_activity_id=?", baheId). GroupBy("t.id").Find(&teams) t.CheckErr(err) for index := range teams { members := make([]*models.BaheTeamMember, 0) err = core.GetXormAuto().Where("is_delete=0 and team_id=? and rehearsal_id=? and arch_id=?", teams[index].Id, activity.RehearsalId, activity.ArchId).Desc("score"). Asc("sort_time").Find(&members) t.CheckErr(err) teams[index].Members = members } t.JSON(map[string]interface{}{ "total": len(teams), "list": teams, }) } func (t *TugOfWarCtl) List() { activityId := t.MustGetInt("activity_id") bahes := make([]*models.TugOfWar, 0) err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId). Asc("created_at").Find(&bahes) t.CheckErr(err) t.JSON(map[string]interface{}{ "bahe_activities": bahes, }) }