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

db_notice.go 1.9 KiB

1 년 전
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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) FindNoticeById(ids interface{}) (*[]model.Notice, error) {
  26. var m []model.Notice
  27. if err := noticeDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil {
  28. return nil, logx.Error(err)
  29. }
  30. return &m, nil
  31. }
  32. func (noticeDb *NoticeDb) FindNotice(limit, start int) (*[]model.Notice, error) {
  33. var m []model.Notice
  34. if limit == 0 || start == 0 {
  35. if err := noticeDb.Db.Asc("sort").Find(&m); err != nil {
  36. return nil, logx.Error(err)
  37. }
  38. } else {
  39. if err := noticeDb.Db.Asc("sort").Limit(limit, start).Find(m); err != nil {
  40. return nil, logx.Error(err)
  41. }
  42. }
  43. return &m, nil
  44. }
  45. func (noticeDb *NoticeDb) GetNoticeByName(name string) (m *model.Notice, err error) {
  46. m = new(model.Notice)
  47. has, err := noticeDb.Db.Where("name =?", name).Get(m)
  48. if err != nil {
  49. return nil, logx.Error(err)
  50. }
  51. if has == false {
  52. return nil, nil
  53. }
  54. return m, nil
  55. }
  56. func (noticeDb *NoticeDb) NoticeInsert(m *model.Notice) (int, error) {
  57. _, err := noticeDb.Db.InsertOne(m)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return m.Id, nil
  62. }
  63. func (noticeDb *NoticeDb) NoticeUpdate(m *model.Notice, columns ...string) (int64, error) {
  64. affected, err := noticeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  65. if err != nil {
  66. return 0, err
  67. }
  68. return affected, nil
  69. }
  70. func (noticeDb *NoticeDb) NoticeDelete(id interface{}) (int64, error) {
  71. if reflect.TypeOf(id).Kind() == reflect.Slice {
  72. return Db.In("id", id).Delete(model.Notice{})
  73. } else {
  74. return Db.Where("id = ?", id).Delete(model.Notice{})
  75. }
  76. }