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

53 lines
2.2 KiB

5 years ago
  1. package models
  2. import (
  3. "github.com/ouxuanserver/osmanthuswine/src/core"
  4. "hudongzhuanjia/utils/define"
  5. "time"
  6. )
  7. const CalorieTableName = TableNamePrefix + "calorie"
  8. type Calorie struct {
  9. Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"`
  10. ActivityId int64 `json:"activity_id" xorm:"not null comment('互动id') INT(11)"`
  11. GameDuration int64 `json:"game_duration" xorm:"not null default(60) comment('游戏时长秒') INT(11)"`
  12. StartTime int64 `json:"start_time" xorm:"not null default(0) comment('游戏开始时间') INT(11)"`
  13. Status string `json:"status" xorm:"not null default('未开始') comment('状态[未开始,准备中,进行中,已结束]') VARCHAR(11)"`
  14. IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('是否删除') TINYINT(1)"`
  15. CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
  16. UpdatedAt time.Time `json:"updated_at" xorm:"not null default(CURRENT_TIMESTAMP) updated TIMESTAMP"`
  17. }
  18. func (t *Calorie) TableName() string {
  19. return CalorieTableName
  20. }
  21. func (t *Calorie) UpdateToStatusByActivityId(aid int64, before, after string) (int64, error) {
  22. t.Status = after
  23. t.UpdatedAt = time.Now()
  24. return core.GetXormAuto().Where("is_delete=0 and status=? and activity_id=?", before, aid).
  25. Cols("status", "updated_at").Update(t)
  26. }
  27. func (t *Calorie) UpdateStatusById(id int64, status string) (int64, error) {
  28. t.Status = status
  29. t.UpdatedAt = time.Now()
  30. return core.GetXormAuto().Where("is_delete=0 and id=?", id).Cols("status", "start_time", "updated_at").Update(t)
  31. }
  32. func (t *Calorie) GetCurrent(aid int64) (bool, error) {
  33. return core.GetXormAuto().Where("is_delete=0 and activity_id=? and (status=? or status=?)",
  34. aid, define.StatusReady, define.StatusRunning).Desc("created_at").Get(t)
  35. }
  36. func UpdateCalorieStatusByActivityId(aid int64) (int64, error) {
  37. return core.GetXormAuto().Where("is_delete=0 and activity_id=?", aid).
  38. Update(&Calorie{Status: define.StatusNotBegin})
  39. }
  40. func GetCaloriesByActivityId(activityId interface{}) ([]*Calorie, error) {
  41. calories := make([]*Calorie, 0)
  42. err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).Find(&calories)
  43. return calories, err
  44. }