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

93 lines
2.4 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) ClassDeleteBySession(id interface{}) (int64, error) {
  30. if reflect.TypeOf(id).Kind() == reflect.Slice {
  31. return classDb.Db.In("id", id).Delete(model.Class{})
  32. } else {
  33. return classDb.Db.Where("id = ?", id).Delete(model.Class{})
  34. }
  35. }
  36. func (classDb *ClassDb) ClassDeleteBySessionForGrade(session *xorm.Session, gradeId interface{}) (int64, error) {
  37. if reflect.TypeOf(gradeId).Kind() == reflect.Slice {
  38. return session.In("grade_id", gradeId).Delete(model.Class{})
  39. } else {
  40. return session.Where("grade_id = ?", gradeId).Delete(model.Class{})
  41. }
  42. }
  43. func (classDb *ClassDb) ClassDeleteBySessionForEnterprise(session *xorm.Session, enterpriseId interface{}) (int64, error) {
  44. if reflect.TypeOf(enterpriseId).Kind() == reflect.Slice {
  45. return session.In("enterprise_id", enterpriseId).Delete(model.Class{})
  46. } else {
  47. return session.Where("enterprise_id = ?", enterpriseId).Delete(model.Class{})
  48. }
  49. }
  50. func (classDb *ClassDb) BatchAddClass(mm []*model.Class) (int64, error) {
  51. affected, err := classDb.Db.Insert(mm)
  52. if err != nil {
  53. return 0, err
  54. }
  55. return affected, nil
  56. }
  57. func (classDb *ClassDb) CountClassForEnterprise(enterpriseId int) (total int64, err error) {
  58. var m model.Class
  59. total, err = classDb.Db.Where("enterprise_id =?", enterpriseId).Count(&m)
  60. if err != nil {
  61. return
  62. }
  63. return
  64. }
  65. func (classDb *ClassDb) ClassUpdate(m *model.Class, columns ...string) (int64, error) {
  66. affected, err := classDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  67. if err != nil {
  68. return 0, err
  69. }
  70. return affected, nil
  71. }
  72. func (classDb *ClassDb) GetClass(id int) (m *model.Class, err error) {
  73. m = new(model.Class)
  74. has, err := classDb.Db.Where("id =?", id).Get(m)
  75. if err != nil {
  76. return nil, logx.Error(err)
  77. }
  78. if has == false {
  79. return nil, nil
  80. }
  81. return m, nil
  82. }