package pc import ( "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time" ) type CalorieCtl struct { controllers.AuthorCtl } func (t *CalorieCtl) Ready() { calorieId := t.MustGetInt64("calorie_id") calorie := new(models.Calorie) exist, err := models.GetById(calorie, calorieId) t.CheckErr(err) t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里活动不存在") if calorie.Status != define.StatusNotBegin { t.ERROR(fmt.Sprintf("该活动%s", calorie.Status), code.MSG_MODULE_STATUS_ERROR) } activity := new(models.Activity) exist, err = models.GetById(activity, calorie.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") _, err = calorie.UpdateToStatusByActivityId(calorie.ActivityId, define.StatusReady, define.StatusNotBegin) t.CheckErr(err) calorie.StartTime = 0 _, err = calorie.UpdateStatusById(calorieId, define.StatusReady) t.CheckErr(err) t.SUCCESS("操作成功") } // 开始, 加一个开始时间 func (t *CalorieCtl) Start() { calorieId := t.MustGetInt64("calorie_id") calorie := new(models.Calorie) exist, err := models.GetById(calorie, calorieId) t.CheckErr(err) t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里活动不存在") if calorie.Status != define.StatusReady { t.ERROR(fmt.Sprintf("该活动%s", calorie.Status), code.MSG_MODULE_STATUS_ERROR) } activity := new(models.Activity) exist, err = models.GetById(activity, calorie.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") calorie.StartTime = time.Now().Unix() _, err = calorie.UpdateStatusById(calorieId, define.StatusRunning) t.CheckErr(err) t.SUCCESS("操作成功") } // 彩排活动需要恢复 func (t *CalorieCtl) Stop() { calorieId := t.MustGetInt64("calorie_id") calorie := new(models.Calorie) exist, err := models.GetById(calorie, calorieId) t.CheckErr(err) t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里活动不存在") if calorie.Status != define.StatusRunning { t.ERROR(fmt.Sprintf("该活动%s", calorie.Status), code.MSG_MODULE_STATUS_ERROR) } activity := new(models.Activity) exist, err = models.GetById(activity, calorie.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") _, err = calorie.UpdateStatusById(calorieId, define.StatusEnding) t.CheckErr(err) t.SUCCESS("操作成功") } // 列出轮次, 包括倒计时 func (t *CalorieCtl) List() { activityId := t.MustGetInt64("activity_id") calories := make([]*models.Calorie, 0) err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).Find(&calories) t.CheckErr(err) t.JSON(map[string]interface{}{ "list": calories, "count": len(calories), }) } // 二维码 func (t *CalorieCtl) Qrcode() { activityId := t.MustGetInt64("activity_id") uid := t.MustGetUID() calorieId := t.MustGetInt64("calorie_id") qrcode, err := utils.GenH5Qrcode(define.H5Calorie, map[string]interface{}{ "activity_id": activityId, "customer_id": uid, "calorie_id": calorieId, }) t.CheckErr(err) t.JSON(map[string]interface{}{ "qrcode": qrcode, }) } type CalorieCountResult struct { Id int64 `json:"id"` ActivityId int64 `json:"activity_id"` CalorieId int64 `json:"calorie_id"` UserId int64 `json:"user_id"` Avatar string `json:"avatar"` Nickname string `json:"nickname"` Score int64 `json:"score"` } // 统计一些数据, 包括参与人数, 包括当前排名 func (t *CalorieCtl) Count() { activityId := t.MustGetInt64("activity_id") calorieId := t.MustGetInt64("calorie_id") limit := t.DefaultInt("limit", 10) activity := new(models.Activity) exist, err := models.GetById(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) calorie := new(models.Calorie) exist, err = models.GetById(calorie, calorieId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "卡路里不存在") // 统计人数 count, err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?", calorieId, activity.RehearsalId).Count(new(models.CalorieUser)) t.CheckErr(err) // 统计排名 result := make([]*CalorieCountResult, 0) err = core.GetXormAuto().Table(new(models.CalorieUser)).Alias("c"). Select("c.id, c.activity_id, c.calorie_id, c.user_id, u.avatar, u.nickname, c.score, c.join_time"). Join("LEFT", new(models.User).Alias("u"), "c.user_id=u.id and u.is_delete=0"). Where("c.is_delete=0 and c.calorie_id=? and c.rehearsal_id=?", calorieId, activity.RehearsalId). Limit(limit).Desc("c.score").Asc("c.join_time").Find(&result) t.CheckErr(err) //duration := calorie.GameDuration - (time.Now().Unix() - calorie.StartTime) + 6 // 增加6s 倒计时 //if calorie.StartTime == 0 { // duration = calorie.GameDuration //} // 3s 倒计时 //var countDown float64 = 0 //if duration-calorie.GameDuration > 0 { // countDown = math.Ceil(float64(duration-calorie.GameDuration) / 2.0) //} //if calorie.Status == define.StatusEnding || duration <= 0 { //duration = 0 //} t.JSON(map[string]interface{}{ "list": result, // 排名统计 "count": count, // 人数统计 //"time": duration - 2*int64(countDown), // 时长 //"count_down": countDown, //倒计时 //"status": calorie.Status, // 状态 }) }