互动
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.

150 lines
4.2 KiB

package pc
import (
"fmt"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
calorie_service "hudongzhuanjia/services/calorie"
"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.Get(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.Get(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.Get(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.Get(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.Get(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.Get(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, err := models.GetCaloriesByActivityId(activityId)
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")
area := new(models.AreaStore)
exist, err := area.GetByCustomerId(uid, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户信息异常")
qrcode, err := utils.GenH5Qrcode(define.H5Calorie, map[string]interface{}{
"activity_id": activityId,
"customer_id": uid,
"area_id": area.Id,
"calorie_id": calorieId,
})
t.CheckErr(err)
t.JSON(map[string]interface{}{
"qrcode": qrcode,
})
}
// 统计一些数据, 包括参与人数, 包括当前排名
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.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
calorie := new(models.Calorie)
exist, err = models.Get(calorie, calorieId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "卡路里不存在")
// 统计人数
count, err := new(models.CalorieUser).Count(calorieId, activity.RehearsalId)
t.CheckErr(err)
// 统计排名
result, err := calorie_service.RankCalorieUser(calorieId, activity.RehearsalId, limit)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"list": result, // 排名统计
"count": count, // 人数统计
})
}