智慧食堂
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.

87 regels
1.9 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 BannerDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (bannerDb *BannerDb) Set() { // set方法
  12. bannerDb.Db = Db
  13. }
  14. func (bannerDb *BannerDb) GetBanner(id int) (m *model.Banner, err error) {
  15. m = new(model.Banner)
  16. has, err := bannerDb.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 (bannerDb *BannerDb) FindBannerById(ids interface{}) (*[]model.Banner, error) {
  26. var m []model.Banner
  27. if err := bannerDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil {
  28. return nil, logx.Error(err)
  29. }
  30. return &m, nil
  31. }
  32. func (bannerDb *BannerDb) FindBanner(limit, start int) (*[]model.Banner, error) {
  33. var m []model.Banner
  34. if limit == 0 || start == 0 {
  35. if err := bannerDb.Db.Desc("sort").Find(&m); err != nil {
  36. return nil, logx.Error(err)
  37. }
  38. } else {
  39. if err := bannerDb.Db.Desc("sort").Limit(limit, start).Find(m); err != nil {
  40. return nil, logx.Error(err)
  41. }
  42. }
  43. return &m, nil
  44. }
  45. func (bannerDb *BannerDb) GetBannerByName(name string) (m *model.Banner, err error) {
  46. m = new(model.Banner)
  47. has, err := bannerDb.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 (bannerDb *BannerDb) BannerInsert(m *model.Banner) (int, error) {
  57. _, err := bannerDb.Db.InsertOne(m)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return m.Id, nil
  62. }
  63. func (bannerDb *BannerDb) BannerUpdate(m *model.Banner, columns ...string) (int64, error) {
  64. affected, err := bannerDb.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 (bannerDb *BannerDb) BannerDelete(id interface{}) (int64, error) {
  71. if reflect.TypeOf(id).Kind() == reflect.Slice {
  72. return Db.In("id", id).Delete(model.Banner{})
  73. } else {
  74. return Db.Where("id = ?", id).Delete(model.Banner{})
  75. }
  76. }