out_link.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. Paging *paging.PagingItem `description:"分页数据"`
  28. }
  29. type OutLinkSaveReq struct {
  30. Id int `description:"序号"`
  31. Title string `description:"菜单名称"`
  32. Url string `description:"链接地址"`
  33. }
  34. type OutLinkReq struct {
  35. Id int `description:"序号"`
  36. }
  37. func (ol *OutLink) TableName() string {
  38. return "out_link"
  39. }
  40. func (ol *OutLink) Create() (err error) {
  41. o := orm.NewOrm()
  42. _, err = o.Insert(ol)
  43. if err != nil {
  44. return
  45. }
  46. return
  47. }
  48. func (ol *OutLink) GetItemById(id int) (err error) {
  49. o := orm.NewOrm()
  50. sql := `SELECT * FROM out_link WHERE id = ? LIMIT 1`
  51. err = o.Raw(sql, id).QueryRow(&ol)
  52. return
  53. }
  54. func (ol *OutLink) GetItemsByCondition(condition string, pars []interface{}, orderRule string) (items []*OutLink, err error) {
  55. o := orm.NewOrm()
  56. sql := `SELECT * FROM out_link WHERE 1=1 `
  57. sql += condition
  58. order := ` ORDER BY create_time ASC`
  59. if orderRule != "" {
  60. order = ` ORDER BY ` + orderRule
  61. }
  62. sql += order
  63. _, err = o.Raw(sql, pars).QueryRows(&items)
  64. return
  65. }
  66. func (ol *OutLink) Update(cols []string) (err error) {
  67. o := orm.NewOrm()
  68. _, err = o.Update(ol, cols...)
  69. return
  70. }
  71. func (ol *OutLink) Del() (err error) {
  72. o := orm.NewOrm()
  73. sql := `DELETE FROM out_link WHERE id = ? LIMIT 1`
  74. _, err = o.Raw(sql, ol.Id).Exec()
  75. return
  76. }
  77. func (ol *OutLink) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  78. o := orm.NewOrm()
  79. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, ol.TableName(), condition)
  80. err = o.Raw(sql, pars).QueryRow(&count)
  81. return
  82. }
  83. func (ol *OutLink) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*OutLink, err error) {
  84. o := orm.NewOrm()
  85. fields := strings.Join(fieldArr, ",")
  86. if len(fieldArr) == 0 {
  87. fields = `*`
  88. }
  89. order := `ORDER BY create_time ASC`
  90. if orderRule != "" {
  91. order = ` ORDER BY ` + orderRule
  92. }
  93. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, ol.TableName(), condition, order)
  94. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  95. if err = o.Raw(totalSql, pars).QueryRow(&total); err != nil {
  96. return
  97. }
  98. sql += ` LIMIT ?,?`
  99. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  100. return
  101. }