智慧食堂
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.

109 line
2.9 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("id").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) BatchAddClass(mm []*model.Class) (int64, error) {
  65. affected, err := classDb.Db.Insert(mm)
  66. if err != nil {
  67. return 0, err
  68. }
  69. return affected, nil
  70. }
  71. func (classDb *ClassDb) CountClassForEnterprise(enterpriseId int) (total int64, err error) {
  72. var m model.Class
  73. total, err = classDb.Db.Where("enterprise_id =?", enterpriseId).Count(&m)
  74. if err != nil {
  75. return
  76. }
  77. return
  78. }
  79. func (classDb *ClassDb) ClassUpdate(m *model.Class, columns ...string) (int64, error) {
  80. affected, err := classDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  81. if err != nil {
  82. return 0, err
  83. }
  84. return affected, nil
  85. }
  86. func (classDb *ClassDb) GetClass(id int) (m *model.Class, err error) {
  87. m = new(model.Class)
  88. has, err := classDb.Db.Where("id =?", id).Get(m)
  89. if err != nil {
  90. return nil, logx.Error(err)
  91. }
  92. if has == false {
  93. return nil, nil
  94. }
  95. return m, nil
  96. }