智慧食堂-队列消费项目
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.
 
 
 

79 rivejä
2.1 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 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. }