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

108 lines
2.5 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 MerchantDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (merchantDb *MerchantDb) Set() { // set方法
  12. merchantDb.Db = Db
  13. }
  14. func (merchantDb *MerchantDb) GetMerchant(id int) (m *model.Merchant, err error) {
  15. m = new(model.Merchant)
  16. has, err := merchantDb.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 (merchantDb *MerchantDb) GetPopToCentralKitchen() (m *model.Merchant, err error) {
  26. m = new(model.Merchant)
  27. has, err := merchantDb.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 (merchantDb *MerchantDb) FindMerchantById(ids interface{}) (*[]model.Merchant, error) {
  37. var m []model.Merchant
  38. if err := merchantDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil {
  39. return nil, logx.Error(err)
  40. }
  41. return &m, nil
  42. }
  43. func (merchantDb *MerchantDb) FindMerchant(limit, start int) (*[]model.Merchant, error) {
  44. var m []model.Merchant
  45. if limit == 0 || start == 0 {
  46. if err := merchantDb.Db.Asc("sort").Find(&m); err != nil {
  47. return nil, logx.Error(err)
  48. }
  49. } else {
  50. if err := merchantDb.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 (merchantDb *MerchantDb) GetMerchantByName(name string) (m *model.Merchant, err error) {
  57. m = new(model.Merchant)
  58. has, err := merchantDb.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 (merchantDb *MerchantDb) MerchantInsert(m *model.Merchant) (int, error) {
  68. _, err := merchantDb.Db.InsertOne(m)
  69. if err != nil {
  70. return 0, err
  71. }
  72. return m.Id, nil
  73. }
  74. func (merchantDb *MerchantDb) MerchantUpdate(m *model.Merchant, columns ...string) (int64, error) {
  75. affected, err := merchantDb.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 (merchantDb *MerchantDb) MerchantDelete(id interface{}) (int64, error) {
  82. if reflect.TypeOf(id).Kind() == reflect.Slice {
  83. return Db.In("id", id).Delete(model.Merchant{})
  84. } else {
  85. return Db.Where("id = ?", id).Delete(model.Merchant{})
  86. }
  87. }
  88. type MerchantWithEnterpriseInfo struct {
  89. model.Merchant `xorm:"extends"`
  90. model.Enterprise `xorm:"extends"`
  91. }
  92. func (MerchantWithEnterpriseInfo) TableName() string {
  93. return "merchant"
  94. }