eta_business_menu_relate.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package eta_business
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. )
  7. // EtaBusinessMenuRelate ETA商家菜单关联表
  8. type EtaBusinessMenuRelate struct {
  9. Id int `orm:"column(id);pk"`
  10. EtaBusinessId int `description:"商家ID"`
  11. MenuId int `description:"菜单ID"`
  12. Type int `description:"类型: 0-全选; 1-半选(此字段仅供前端回显做区分)"`
  13. }
  14. func (m *EtaBusinessMenuRelate) TableName() string {
  15. return "eta_business_menu_relate"
  16. }
  17. func (m *EtaBusinessMenuRelate) PrimaryId() string {
  18. return "id"
  19. }
  20. func (m *EtaBusinessMenuRelate) Create() (err error) {
  21. o := orm.NewOrm()
  22. id, err := o.Insert(m)
  23. if err != nil {
  24. return
  25. }
  26. m.MenuId = int(id)
  27. return
  28. }
  29. func (m *EtaBusinessMenuRelate) CreateMulti(businessId int, items []*EtaBusinessMenuRelate) (err error) {
  30. if businessId == 0 {
  31. return
  32. }
  33. o := orm.NewOrm()
  34. tx, err := o.Begin()
  35. if err != nil {
  36. return
  37. }
  38. defer func() {
  39. if err != nil {
  40. _ = tx.Rollback()
  41. return
  42. }
  43. _ = tx.Commit()
  44. }()
  45. sql := fmt.Sprintf(`DELETE FROM %s WHERE eta_business_id = ?`, m.TableName())
  46. _, err = tx.Raw(sql, businessId).Exec()
  47. if err != nil {
  48. return
  49. }
  50. if len(items) > 0 {
  51. _, err = tx.InsertMulti(len(items), items)
  52. }
  53. return
  54. }
  55. func (m *EtaBusinessMenuRelate) Update(cols []string) (err error) {
  56. o := orm.NewOrm()
  57. _, err = o.Update(m, cols...)
  58. return
  59. }
  60. func (m *EtaBusinessMenuRelate) Del() (err error) {
  61. o := orm.NewOrm()
  62. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  63. _, err = o.Raw(sql, m.MenuId).Exec()
  64. return
  65. }
  66. func (m *EtaBusinessMenuRelate) GetItemById(id int) (item *EtaBusinessMenuRelate, err error) {
  67. o := orm.NewOrm()
  68. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  69. err = o.Raw(sql, id).QueryRow(&item)
  70. return
  71. }
  72. func (m *EtaBusinessMenuRelate) GetItemByCondition(condition string, pars []interface{}) (item *EtaBusinessMenuRelate, err error) {
  73. o := orm.NewOrm()
  74. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  75. err = o.Raw(sql, pars).QueryRow(&item)
  76. return
  77. }
  78. func (m *EtaBusinessMenuRelate) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  79. o := orm.NewOrm()
  80. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  81. err = o.Raw(sql, pars).QueryRow(&count)
  82. return
  83. }
  84. func (m *EtaBusinessMenuRelate) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaBusinessMenuRelate, err error) {
  85. o := orm.NewOrm()
  86. fields := strings.Join(fieldArr, ",")
  87. if len(fieldArr) == 0 {
  88. fields = `*`
  89. }
  90. order := ``
  91. if orderRule != "" {
  92. order = ` ORDER BY ` + orderRule
  93. }
  94. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  95. _, err = o.Raw(sql, pars).QueryRows(&items)
  96. return
  97. }
  98. func (m *EtaBusinessMenuRelate) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaBusinessMenuRelate, err error) {
  99. o := orm.NewOrm()
  100. fields := strings.Join(fieldArr, ",")
  101. if len(fieldArr) == 0 {
  102. fields = `*`
  103. }
  104. order := ``
  105. if orderRule != "" {
  106. order = ` ORDER BY ` + orderRule
  107. }
  108. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  109. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  110. return
  111. }
  112. // EtaBusinessMenuRelateSaveReq 保存商家菜单关联
  113. type EtaBusinessMenuRelateSaveReq struct {
  114. EtaBusinessId int `description:"商家ID"`
  115. MenuIds []int `description:"菜单IDs"`
  116. HalfMenuIds []int `description:"半选菜单IDs-仅供前端回显用的"`
  117. }