out_link.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package models
  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 OutLink struct {
  10. Id int `orm:"column(id);pk;auto" description:"自增序号"`
  11. Title string `description:"菜单名称"`
  12. Url string `description:"链接地址"`
  13. SysAdminId int `description:"创建人ID"`
  14. SysAdminName string `description:"创建人姓名"`
  15. CreateTime time.Time `description:"创建时间"`
  16. ModifyTime time.Time `description:"修改时间"`
  17. }
  18. type OutLinkItem struct {
  19. Id int `description:"自增序号"`
  20. Title string `description:"菜单名称"`
  21. Url string `description:"链接地址"`
  22. CreateTime string `description:"创建时间"`
  23. ModifyTime string `description:"修改时间"`
  24. }
  25. type OutLinkListResp struct {
  26. List []*OutLinkItem
  27. UserCode string
  28. Paging *paging.PagingItem `description:"分页数据"`
  29. }
  30. type OutLinkSaveReq struct {
  31. Id int `description:"序号"`
  32. Title string `description:"菜单名称"`
  33. Url string `description:"链接地址"`
  34. }
  35. type OutLinkReq struct {
  36. Id int `description:"序号"`
  37. }
  38. func (ol *OutLink) TableName() string {
  39. return "out_link"
  40. }
  41. func (ol *OutLink) Create() (err error) {
  42. o := orm.NewOrm()
  43. _, err = o.Insert(ol)
  44. if err != nil {
  45. return
  46. }
  47. return
  48. }
  49. func (ol *OutLink) GetItemById(id int) (err error) {
  50. o := orm.NewOrm()
  51. sql := `SELECT * FROM out_link WHERE id = ? LIMIT 1`
  52. err = o.Raw(sql, id).QueryRow(&ol)
  53. return
  54. }
  55. func (ol *OutLink) GetItemsByCondition(condition string, pars []interface{}, orderRule string) (items []*OutLink, err error) {
  56. o := orm.NewOrm()
  57. sql := `SELECT * FROM out_link WHERE 1=1 `
  58. sql += condition
  59. order := ` ORDER BY create_time ASC`
  60. if orderRule != "" {
  61. order = ` ORDER BY ` + orderRule
  62. }
  63. sql += order
  64. _, err = o.Raw(sql, pars).QueryRows(&items)
  65. return
  66. }
  67. func (ol *OutLink) Update(cols []string) (err error) {
  68. o := orm.NewOrm()
  69. _, err = o.Update(ol, cols...)
  70. return
  71. }
  72. func (ol *OutLink) Del() (err error) {
  73. o := orm.NewOrm()
  74. sql := `DELETE FROM out_link WHERE id = ? LIMIT 1`
  75. _, err = o.Raw(sql, ol.Id).Exec()
  76. return
  77. }
  78. func (ol *OutLink) 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`, ol.TableName(), condition)
  81. err = o.Raw(sql, pars).QueryRow(&count)
  82. return
  83. }
  84. func (ol *OutLink) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*OutLink, err error) {
  85. o := orm.NewOrm()
  86. fields := strings.Join(fieldArr, ",")
  87. if len(fieldArr) == 0 {
  88. fields = `*`
  89. }
  90. order := `ORDER BY create_time ASC`
  91. if orderRule != "" {
  92. order = ` ORDER BY ` + orderRule
  93. }
  94. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, ol.TableName(), condition, order)
  95. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  96. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  97. return
  98. }
  99. sql += ` LIMIT ?,?`
  100. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  101. return
  102. }