智慧食堂
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

db_grade.go 2.1 KiB

há 1 ano
há 1 ano
há 1 ano
há 1 ano
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type GradeDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. EnterpriseId int `json:"enterprise_id"`
  11. }
  12. func (gradeDb *GradeDb) Set(enterpriseId int) { // set方法
  13. gradeDb.Db = Db
  14. gradeDb.EnterpriseId = enterpriseId
  15. }
  16. func (gradeDb *GradeDb) FindGrade() (*[]model.Grade, error) {
  17. var m []model.Grade
  18. if err := gradeDb.Db.Desc("id").Where("enterprise_id =?", gradeDb.EnterpriseId).Find(&m); err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. return &m, nil
  22. }
  23. func (gradeDb *GradeDb) FindGradeByPeriodId(periodId int) (*[]model.Grade, error) {
  24. var m []model.Grade
  25. if err := gradeDb.Db.Desc("id").Where("enterprise_id =?", gradeDb.EnterpriseId).And("period_id =?", periodId).Find(&m); err != nil {
  26. return nil, logx.Error(err)
  27. }
  28. return &m, nil
  29. }
  30. func (gradeDb *GradeDb) GradeDeleteBySession(id interface{}) (int64, error) {
  31. if reflect.TypeOf(id).Kind() == reflect.Slice {
  32. return gradeDb.Db.In("id", id).Delete(model.Grade{})
  33. } else {
  34. return gradeDb.Db.Where("id = ?", id).Delete(model.Grade{})
  35. }
  36. }
  37. func (gradeDb *GradeDb) ClassDeleteBySessionForEnterprise(session *xorm.Session, enterpriseId interface{}) (int64, error) {
  38. if reflect.TypeOf(enterpriseId).Kind() == reflect.Slice {
  39. return session.In("enterprise_id", enterpriseId).Delete(model.Grade{})
  40. } else {
  41. return session.Where("enterprise_id = ?", enterpriseId).Delete(model.Grade{})
  42. }
  43. }
  44. func (gradeDb *GradeDb) GradeInsert(m *model.Grade) (int, error) {
  45. _, err := gradeDb.Db.InsertOne(m)
  46. if err != nil {
  47. return 0, err
  48. }
  49. return m.Id, nil
  50. }
  51. func (gradeDb *GradeDb) GradeUpdate(m *model.Grade, columns ...string) (int64, error) {
  52. affected, err := gradeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  53. if err != nil {
  54. return 0, err
  55. }
  56. return affected, nil
  57. }
  58. func (gradeDb *GradeDb) GetGrade(id int) (m *model.Grade, err error) {
  59. m = new(model.Grade)
  60. has, err := gradeDb.Db.Where("id =?", id).Get(m)
  61. if err != nil {
  62. return nil, logx.Error(err)
  63. }
  64. if has == false {
  65. return nil, nil
  66. }
  67. return m, nil
  68. }