words_learn.go 893 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package words
  2. import (
  3. "context"
  4. v1 "star/api/words/v1"
  5. "star/internal/dao"
  6. "star/internal/model/entity"
  7. "github.com/gogf/gf/v2/errors/gerror"
  8. )
  9. func (w *Words) Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) {
  10. if limit <= 0 {
  11. limit = 50
  12. }
  13. var (
  14. err error
  15. cls = dao.Words.Columns()
  16. orm = dao.Words.Ctx(ctx)
  17. list = make([]entity.Words, limit)
  18. )
  19. if uid > 0 {
  20. orm = orm.Where(cls.Uid, uid)
  21. }
  22. err = orm.Limit(int(limit)).OrderRandom().Scan(&list)
  23. return list, err
  24. }
  25. func (w *Words) SetLevel(ctx context.Context, uid, id uint, level v1.ProficiencyLevel) error {
  26. if level <= 0 || level > 5 {
  27. return gerror.New("熟练度值不合法")
  28. }
  29. var (
  30. cls = dao.Words.Columns()
  31. orm = dao.Words.Ctx(ctx)
  32. )
  33. if uid > 0 {
  34. orm = orm.Where(cls.Uid, uid)
  35. }
  36. _, err := orm.Data(cls.ProficiencyLevel, level).Where(cls.Id, id).Update()
  37. return err
  38. }