sa_label.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package semantic_analysis
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "strings"
  7. "time"
  8. )
  9. type SaLabel struct {
  10. SaLabelId int `orm:"column(sa_label_id);pk" description:"标签ID"`
  11. LabelName string `description:"标签名称"`
  12. SysAdminId int `description:"创建人ID"`
  13. SysAdminName string `description:"创建人名称"`
  14. CreateTime time.Time `description:"创建时间"`
  15. ModifyTime time.Time `description:"修改时间"`
  16. }
  17. var SaLabelColumns = struct {
  18. SaLabelId string
  19. LabelName string
  20. SysAdminId string
  21. SysAdminName string
  22. CreateTime string
  23. ModifyTime string
  24. }{
  25. SaLabelId: "sa_label_id",
  26. LabelName: "label_name",
  27. SysAdminId: "sys_admin_id",
  28. SysAdminName: "sys_admin_name",
  29. CreateTime: "create_time",
  30. ModifyTime: "modify_time",
  31. }
  32. func (m *SaLabel) TableName() string {
  33. return "sa_label"
  34. }
  35. func (m *SaLabel) Create() (err error) {
  36. o := orm.NewOrm()
  37. id, err := o.Insert(m)
  38. if err != nil {
  39. return
  40. }
  41. m.SaLabelId = int(id)
  42. return
  43. }
  44. func (m *SaLabel) Update(cols []string) (err error) {
  45. o := orm.NewOrm()
  46. _, err = o.Update(m, cols...)
  47. return
  48. }
  49. func (m *SaLabel) Del() (err error) {
  50. o := orm.NewOrm()
  51. sql := `DELETE FROM sa_label WHERE sa_label_id = ? LIMIT 1`
  52. _, err = o.Raw(sql, m.SaLabelId).Exec()
  53. return
  54. }
  55. func (m *SaLabel) GetItemById(id int) (err error) {
  56. o := orm.NewOrm()
  57. sql := `SELECT * FROM sa_label WHERE sa_label_id = ? LIMIT 1`
  58. err = o.Raw(sql, id).QueryRow(&m)
  59. return
  60. }
  61. func (m *SaLabel) GetItemByCondition(condition string, pars []interface{}) (err error) {
  62. o := orm.NewOrm()
  63. sql := `SELECT * FROM sa_label WHERE 1=1 `
  64. sql += condition
  65. sql += ` LIMIT 1`
  66. err = o.Raw(sql, pars).QueryRow(&m)
  67. return
  68. }
  69. func (m *SaLabel) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  70. o := orm.NewOrm()
  71. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  72. err = o.Raw(sql, pars).QueryRow(&count)
  73. return
  74. }
  75. func (m *SaLabel) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaLabel, err error) {
  76. o := orm.NewOrm()
  77. fields := strings.Join(fieldArr, ",")
  78. if len(fieldArr) == 0 {
  79. fields = `*`
  80. }
  81. order := `ORDER BY create_time DESC`
  82. if orderRule != "" {
  83. order = ` ORDER BY ` + orderRule
  84. }
  85. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  86. _, err = o.Raw(sql, pars).QueryRows(&items)
  87. return
  88. }
  89. func (m *SaLabel) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaLabel, err error) {
  90. o := orm.NewOrm()
  91. fields := strings.Join(fieldArr, ",")
  92. if len(fieldArr) == 0 {
  93. fields = `*`
  94. }
  95. order := `ORDER BY create_time DESC`
  96. if orderRule != "" {
  97. order = ` ORDER BY ` + orderRule
  98. }
  99. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  100. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  101. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  102. return
  103. }
  104. sql += ` LIMIT ?,?`
  105. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  106. return
  107. }
  108. type SaLabelAddReq struct {
  109. LabelName string `description:"标签名称"`
  110. }
  111. type SaLabelEditReq struct {
  112. SaLabelId int `description:"标签ID"`
  113. SaLabelAddReq
  114. }
  115. type SaLabelDelReq struct {
  116. SaLabelId int `description:"标签ID"`
  117. }
  118. type SaLabelPageListResp struct {
  119. List []*SaLabelItem
  120. Paging *paging.PagingItem `description:"分页数据"`
  121. }
  122. type SaLabelItem struct {
  123. SaLabelId int `description:"标签ID"`
  124. LabelName string `description:"标签名称"`
  125. UserNum int `description:"引用数量"`
  126. SysAdminId int `description:"创建人ID"`
  127. SysAdminName string `description:"创建人姓名"`
  128. CreateTime string `description:"创建时间"`
  129. }