智慧食堂
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

db_notice.go 2.2 KiB

1 yıl önce
1 yıl önce
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type NoticeDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (noticeDb *NoticeDb) Set() { // set方法
  12. noticeDb.Db = Db
  13. }
  14. func (noticeDb *NoticeDb) GetNotice(id int) (m *model.Notice, err error) {
  15. m = new(model.Notice)
  16. has, err := noticeDb.Db.Where("id =?", id).Get(m)
  17. if err != nil {
  18. return nil, logx.Error(err)
  19. }
  20. if has == false {
  21. return nil, nil
  22. }
  23. return m, nil
  24. }
  25. func (noticeDb *NoticeDb) GetPopToCentralKitchen() (m *model.Notice, err error) {
  26. m = new(model.Notice)
  27. has, err := noticeDb.Db.Where("is_pop_to_central_kitchen =?", 1).Get(m)
  28. if err != nil {
  29. return nil, logx.Error(err)
  30. }
  31. if has == false {
  32. return nil, nil
  33. }
  34. return m, nil
  35. }
  36. func (noticeDb *NoticeDb) FindNoticeById(ids interface{}) (*[]model.Notice, error) {
  37. var m []model.Notice
  38. if err := noticeDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil {
  39. return nil, logx.Error(err)
  40. }
  41. return &m, nil
  42. }
  43. func (noticeDb *NoticeDb) FindNotice(limit, start int) (*[]model.Notice, error) {
  44. var m []model.Notice
  45. if limit == 0 || start == 0 {
  46. if err := noticeDb.Db.Asc("sort").Find(&m); err != nil {
  47. return nil, logx.Error(err)
  48. }
  49. } else {
  50. if err := noticeDb.Db.Asc("sort").Limit(limit, start).Find(m); err != nil {
  51. return nil, logx.Error(err)
  52. }
  53. }
  54. return &m, nil
  55. }
  56. func (noticeDb *NoticeDb) GetNoticeByName(name string) (m *model.Notice, err error) {
  57. m = new(model.Notice)
  58. has, err := noticeDb.Db.Where("name =?", name).Get(m)
  59. if err != nil {
  60. return nil, logx.Error(err)
  61. }
  62. if has == false {
  63. return nil, nil
  64. }
  65. return m, nil
  66. }
  67. func (noticeDb *NoticeDb) NoticeInsert(m *model.Notice) (int, error) {
  68. _, err := noticeDb.Db.InsertOne(m)
  69. if err != nil {
  70. return 0, err
  71. }
  72. return m.Id, nil
  73. }
  74. func (noticeDb *NoticeDb) NoticeUpdate(m *model.Notice, columns ...string) (int64, error) {
  75. affected, err := noticeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  76. if err != nil {
  77. return 0, err
  78. }
  79. return affected, nil
  80. }
  81. func (noticeDb *NoticeDb) NoticeDelete(id interface{}) (int64, error) {
  82. if reflect.TypeOf(id).Kind() == reflect.Slice {
  83. return Db.In("id", id).Delete(model.Notice{})
  84. } else {
  85. return Db.Where("id = ?", id).Delete(model.Notice{})
  86. }
  87. }