read_count.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package autocode
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  5. autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  7. "gorm.io/gorm"
  8. )
  9. type ReadCountService struct {
  10. }
  11. // CreateReadCount 创建ReadCount记录
  12. // Author [piexlmax](https://github.com/piexlmax)
  13. func (readCountService *ReadCountService) CreateReadCount(readCount autocode.ReadCount) (err error) {
  14. err = global.GVA_DB.Create(&readCount).Error
  15. return err
  16. }
  17. // DeleteReadCount 删除ReadCount记录
  18. // Author [piexlmax](https://github.com/piexlmax)
  19. func (readCountService *ReadCountService) DeleteReadCount(readCount autocode.ReadCount) (err error) {
  20. err = global.GVA_DB.Delete(&readCount).Error
  21. return err
  22. }
  23. // DeleteReadCountByIds 批量删除ReadCount记录
  24. // Author [piexlmax](https://github.com/piexlmax)
  25. func (readCountService *ReadCountService) DeleteReadCountByIds(ids request.IdsReq) (err error) {
  26. err = global.GVA_DB.Delete(&[]autocode.ReadCount{}, "id in ?", ids.Ids).Error
  27. return err
  28. }
  29. // UpdateReadCount 更新ReadCount记录
  30. // Author [piexlmax](https://github.com/piexlmax)
  31. func (readCountService *ReadCountService) UpdateReadCount(readCount autocode.ReadCount) (err error) {
  32. err = global.GVA_DB.Save(&readCount).Error
  33. return err
  34. }
  35. // GetReadCount 根据id获取ReadCount记录
  36. // Author [piexlmax](https://github.com/piexlmax)
  37. func (readCountService *ReadCountService) GetReadCountByUserId(userId int, problemId int) (err error, readCount autocode.ReadCount) {
  38. err = global.GVA_DB.Where("user_id = ?", userId).Where("problem_id = ?", problemId).First(&readCount).Error
  39. return
  40. }
  41. func (readCountService *ReadCountService) UpdateReadCountBy(id uint) (err error) {
  42. err = global.GVA_DB.Model(&autocode.ReadCount{}).Where("id=?", id).Update("count", gorm.Expr("count+1")).Error
  43. return err
  44. }
  45. func (readCountService *ReadCountService) GetReadCount(id uint) (err error, readCount autocode.ReadCount) {
  46. err = global.GVA_DB.Where("id = ?", id).First(&readCount).Error
  47. return
  48. }
  49. // GetReadCountInfoList 分页获取ReadCount记录
  50. // Author [piexlmax](https://github.com/piexlmax)
  51. func (readCountService *ReadCountService) GetReadCountInfoList(info autoCodeReq.ReadCountSearch) (err error, list interface{}, total int64) {
  52. limit := info.PageSize
  53. offset := info.PageSize * (info.Page - 1)
  54. // 创建db
  55. db := global.GVA_DB.Model(&autocode.ReadCount{})
  56. var readCounts []autocode.ReadCount
  57. db.Where("problem_id = ?", info.ProblemId)
  58. // 如果有条件搜索 下方会自动创建搜索语句
  59. err = db.Count(&total).Error
  60. err = db.Limit(limit).Offset(offset).Find(&readCounts).Error
  61. return err, readCounts, total
  62. }