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

5 years ago
5 years ago
5 years ago
5 years ago
  1. package models
  2. import (
  3. "github.com/ouxuanserver/osmanthuswine/src/core"
  4. "time"
  5. )
  6. const CalorieUserTableName = TableNamePrefix + "calorie_user"
  7. type CalorieUser struct {
  8. Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"`
  9. ActivityId int64 `json:"activity_id" xorm:"not null comment('互动id') INT(11)"`
  10. Avatar string `json:"avatar" xorm:"-"`
  11. Nickname string `json:"nickname" xorm:"-"`
  12. UserId int64 `json:"user_id" xorm:"not null comment('用户表id') INT(11)"`
  13. CalorieId int64 `json:"calorie_id" xorm:"not null comment('calorie表id') INT(11)"`
  14. RehearsalId int64 `json:"rehearsal_id" xorm:"not null default(0) comment('彩排id') INT(11)"`
  15. Score int64 `json:"score" xorm:"not null default(0) comment('分数') INT(11)"`
  16. JoinTime int64 `json:"join_time" xorm:"not null default('0') comment('加入时间纳秒') VARCHAR(128)"`
  17. IsDelete bool `json:"is_delete" xorm:"not null default(0) comment('是否删除') TINYINT(1)"`
  18. CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
  19. UpdatedAt time.Time `json:"updated_at" xorm:"not null default(CURRENT_TIMESTAMP) updated comment('创建时间') TIMESTAMP"`
  20. }
  21. func (t *CalorieUser) TableName() string {
  22. return CalorieUserTableName
  23. }
  24. func (t *CalorieUser) GetByCalorieIdAndUserId(cid, uid, rid int64) (bool, error) {
  25. return core.GetXormAuto().Where("is_delete=0 and calorie_id=? and user_id=? and rehearsal_id=?", cid, uid, rid).Get(t)
  26. }
  27. func (t *CalorieUser) IncrScore(score int) (int64, error) {
  28. t.JoinTime = time.Now().UnixNano()
  29. return core.GetXormAuto().Where("is_delete=0 and id=?", t.Id).Cols("score", "join_time").Incr("score", score).Update(t)
  30. }
  31. func (t *CalorieUser) CountByCalorieId(calorieId, rehearsalId interface{}) (int64, error) {
  32. return core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?",
  33. calorieId, rehearsalId).Count(&CalorieUser{})
  34. }
  35. func GetCalorieUsersByCalorieIdAndScore(calorieId, rehearsalId, score interface{}) ([]*CalorieUser, error) {
  36. users := make([]*CalorieUser, 0)
  37. err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=? and score<=?",
  38. calorieId, rehearsalId, score).Desc("score").Asc("join_time").Find(&users)
  39. return users, err
  40. }
  41. func (t *CalorieUser) Count(calorieId, rehearsalId interface{}) (int64, error) {
  42. return core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?",
  43. calorieId, rehearsalId).Count(t)
  44. }