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

db_class.go 2.7 KiB

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