智慧食堂
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

117 righe
3.2 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type ClassDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. GradeId int `json:"enterprise_id"`
  11. }
  12. func (classDb *ClassDb) Set(gradeId int) { // set方法
  13. classDb.Db = Db
  14. classDb.GradeId = gradeId
  15. }
  16. func (classDb *ClassDb) FindClass() (*[]model.Class, error) {
  17. var m []model.Class
  18. if classDb.GradeId == 0 {
  19. if err := classDb.Db.Desc("id").Find(&m); err != nil {
  20. return nil, logx.Error(err)
  21. }
  22. } else {
  23. if err := classDb.Db.Desc("id").Where("grade_id =?", classDb.GradeId).Find(&m); err != nil {
  24. return nil, logx.Error(err)
  25. }
  26. }
  27. return &m, nil
  28. }
  29. func (classDb *ClassDb) FindClassByEnterprise(enterpriseId interface{}) (*[]model.Class, error) {
  30. var m []model.Class
  31. if err := classDb.Db.Desc("id").Where("enterprise_id =?", enterpriseId).Find(&m); err != nil {
  32. return nil, logx.Error(err)
  33. }
  34. return &m, nil
  35. }
  36. func (classDb *ClassDb) FindClassAscByEnterprise(enterpriseId interface{}) (*[]model.Class, error) {
  37. var m []model.Class
  38. if err := classDb.Db.Asc("sort").Where("enterprise_id =?", enterpriseId).Find(&m); err != nil {
  39. return nil, logx.Error(err)
  40. }
  41. return &m, nil
  42. }
  43. func (classDb *ClassDb) ClassDeleteBySession(id interface{}) (int64, error) {
  44. if reflect.TypeOf(id).Kind() == reflect.Slice {
  45. return classDb.Db.In("id", id).Delete(model.Class{})
  46. } else {
  47. return classDb.Db.Where("id = ?", id).Delete(model.Class{})
  48. }
  49. }
  50. func (classDb *ClassDb) ClassDeleteBySessionForGrade(session *xorm.Session, gradeId interface{}) (int64, error) {
  51. if reflect.TypeOf(gradeId).Kind() == reflect.Slice {
  52. return session.In("grade_id", gradeId).Delete(model.Class{})
  53. } else {
  54. return session.Where("grade_id = ?", gradeId).Delete(model.Class{})
  55. }
  56. }
  57. func (classDb *ClassDb) ClassDeleteBySessionForEnterprise(session *xorm.Session, enterpriseId interface{}) (int64, error) {
  58. if reflect.TypeOf(enterpriseId).Kind() == reflect.Slice {
  59. return session.In("enterprise_id", enterpriseId).Delete(model.Class{})
  60. } else {
  61. return session.Where("enterprise_id = ?", enterpriseId).Delete(model.Class{})
  62. }
  63. }
  64. func (classDb *ClassDb) FindClassById(ids interface{}) (*[]model.Class, error) {
  65. var m []model.Class
  66. if err := classDb.Db.In("id", ids).Find(&m); err != nil {
  67. return nil, logx.Error(err)
  68. }
  69. return &m, nil
  70. }
  71. func (classDb *ClassDb) BatchAddClass(mm []*model.Class) (int64, error) {
  72. affected, err := classDb.Db.Insert(mm)
  73. if err != nil {
  74. return 0, err
  75. }
  76. return affected, nil
  77. }
  78. func (classDb *ClassDb) CountClassForEnterprise(enterpriseId int) (total int64, err error) {
  79. var m model.Class
  80. total, err = classDb.Db.Where("enterprise_id =?", enterpriseId).Count(&m)
  81. if err != nil {
  82. return
  83. }
  84. return
  85. }
  86. func (classDb *ClassDb) ClassUpdate(m *model.Class, columns ...string) (int64, error) {
  87. affected, err := classDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  88. if err != nil {
  89. return 0, err
  90. }
  91. return affected, nil
  92. }
  93. func (classDb *ClassDb) GetClass(id int) (m *model.Class, err error) {
  94. m = new(model.Class)
  95. has, err := classDb.Db.Where("id =?", id).Get(m)
  96. if err != nil {
  97. return nil, logx.Error(err)
  98. }
  99. if has == false {
  100. return nil, nil
  101. }
  102. return m, nil
  103. }