智慧食堂
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 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 PeriodDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. EnterpriseId int `json:"enterprise_id"`
  11. }
  12. func (PeriodDb *PeriodDb) Set(enterpriseId int) { // set方法
  13. PeriodDb.Db = Db
  14. PeriodDb.EnterpriseId = enterpriseId
  15. }
  16. func (PeriodDb *PeriodDb) FindPeriod() (*[]model.Period, error) {
  17. var m []model.Period
  18. if err := PeriodDb.Db.Desc("id").Where("enterprise_id =?", PeriodDb.EnterpriseId).Find(&m); err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. return &m, nil
  22. }
  23. func (PeriodDb *PeriodDb) PeriodDeleteBySession(id interface{}) (int64, error) {
  24. if reflect.TypeOf(id).Kind() == reflect.Slice {
  25. return PeriodDb.Db.In("id", id).Delete(model.Period{})
  26. } else {
  27. return PeriodDb.Db.Where("id = ?", id).Delete(model.Period{})
  28. }
  29. }
  30. func (PeriodDb *PeriodDb) ClassDeleteBySessionForEnterprise(session *xorm.Session, enterpriseId interface{}) (int64, error) {
  31. if reflect.TypeOf(enterpriseId).Kind() == reflect.Slice {
  32. return session.In("enterprise_id", enterpriseId).Delete(model.Period{})
  33. } else {
  34. return session.Where("enterprise_id = ?", enterpriseId).Delete(model.Period{})
  35. }
  36. }
  37. func (PeriodDb *PeriodDb) PeriodInsert(m *model.Period) (int, error) {
  38. _, err := PeriodDb.Db.InsertOne(m)
  39. if err != nil {
  40. return 0, err
  41. }
  42. return m.Id, nil
  43. }
  44. func (PeriodDb *PeriodDb) PeriodUpdate(m *model.Period, columns ...string) (int64, error) {
  45. affected, err := PeriodDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  46. if err != nil {
  47. return 0, err
  48. }
  49. return affected, nil
  50. }
  51. func (PeriodDb *PeriodDb) GetPeriod(id int) (m *model.Period, err error) {
  52. m = new(model.Period)
  53. has, err := PeriodDb.Db.Where("id =?", id).Get(m)
  54. if err != nil {
  55. return nil, logx.Error(err)
  56. }
  57. if has == false {
  58. return nil, nil
  59. }
  60. return m, nil
  61. }