user_level_mapping.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package models
  2. import (
  3. "eta/eta_mini_crm/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // UserLevelMapping 用户等级分类关联表
  10. type UserLevelMapping struct {
  11. Id int `orm:"column(id);pk"`
  12. UserLevelId int `description:"用户等级ID"`
  13. ClassifyId int `description:"分类ID"`
  14. CreateTime time.Time `description:"创建时间"`
  15. }
  16. func (m *UserLevelMapping) TableName() string {
  17. return "user_level_mapping"
  18. }
  19. type UserLevelMappingCols struct {
  20. PrimaryId string
  21. UserLevelId string
  22. ClassifyId string
  23. CreateTime string
  24. }
  25. func (m *UserLevelMapping) Cols() UserLevelMappingCols {
  26. return UserLevelMappingCols{
  27. PrimaryId: "id",
  28. UserLevelId: "user_level_id",
  29. ClassifyId: "classify_id",
  30. CreateTime: "create_time",
  31. }
  32. }
  33. func (m *UserLevelMapping) Create() (err error) {
  34. o := orm.NewOrm()
  35. id, err := o.Insert(m)
  36. if err != nil {
  37. return
  38. }
  39. m.Id = int(id)
  40. return
  41. }
  42. func (m *UserLevelMapping) CreateMulti(items []*UserLevelMapping) (err error) {
  43. if len(items) == 0 {
  44. return
  45. }
  46. o := orm.NewOrm()
  47. _, err = o.InsertMulti(len(items), items)
  48. return
  49. }
  50. func (m *UserLevelMapping) Update(cols []string) (err error) {
  51. o := orm.NewOrm()
  52. _, err = o.Update(m, cols...)
  53. return
  54. }
  55. func (m *UserLevelMapping) Remove() (err error) {
  56. o := orm.NewOrm()
  57. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  58. _, err = o.Raw(sql, m.Id).Exec()
  59. return
  60. }
  61. func (m *UserLevelMapping) MultiRemove(ids []int) (err error) {
  62. if len(ids) == 0 {
  63. return
  64. }
  65. o := orm.NewOrm()
  66. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  67. _, err = o.Raw(sql, ids).Exec()
  68. return
  69. }
  70. func (m *UserLevelMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
  71. if condition == "" {
  72. return
  73. }
  74. o := orm.NewOrm()
  75. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  76. _, err = o.Raw(sql, pars).Exec()
  77. return
  78. }
  79. func (m *UserLevelMapping) GetItemById(id int) (item *UserLevelMapping, err error) {
  80. o := orm.NewOrm()
  81. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 AND %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  82. err = o.Raw(sql, id).QueryRow(&item)
  83. return
  84. }
  85. func (m *UserLevelMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *UserLevelMapping, err error) {
  86. o := orm.NewOrm()
  87. order := ``
  88. if orderRule != "" {
  89. order = ` ORDER BY ` + orderRule
  90. }
  91. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  92. err = o.Raw(sql, pars).QueryRow(&item)
  93. return
  94. }
  95. func (m *UserLevelMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  96. o := orm.NewOrm()
  97. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  98. err = o.Raw(sql, pars).QueryRow(&count)
  99. return
  100. }
  101. func (m *UserLevelMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*UserLevelMapping, err error) {
  102. o := orm.NewOrm()
  103. fields := strings.Join(fieldArr, ",")
  104. if len(fieldArr) == 0 {
  105. fields = `*`
  106. }
  107. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  108. if orderRule != "" {
  109. order = ` ORDER BY ` + orderRule
  110. }
  111. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  112. _, err = o.Raw(sql, pars).QueryRows(&items)
  113. return
  114. }
  115. func (m *UserLevelMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*UserLevelMapping, err error) {
  116. o := orm.NewOrm()
  117. fields := strings.Join(fieldArr, ",")
  118. if len(fieldArr) == 0 {
  119. fields = `*`
  120. }
  121. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  122. if orderRule != "" {
  123. order = ` ORDER BY ` + orderRule
  124. }
  125. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  126. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  127. return
  128. }