out_link.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package models
  2. import (
  3. "eta_gn/eta_api/global"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "strings"
  7. "time"
  8. )
  9. type OutLink struct {
  10. Id int `gorm:"column:id;primaryKey;autoIncrement;type:int" orm:"column(id);pk;auto" description:"自增序号"`
  11. Title string `gorm:"column:title;type:varchar(255)" orm:"column(title)" description:"菜单名称"`
  12. Url string `gorm:"column:url;type:varchar(255)" orm:"column(url)" description:"链接地址"`
  13. SysAdminId int `gorm:"column:sys_admin_id;type:int" orm:"column(sys_admin_id)" description:"创建人ID"`
  14. SysAdminName string `gorm:"column:sys_admin_name;type:varchar(255)" orm:"column(sys_admin_name)" description:"创建人姓名"`
  15. CreateTime time.Time `gorm:"column:create_time;type:datetime" orm:"column(create_time)" description:"创建时间"`
  16. ModifyTime time.Time `gorm:"column:modify_time;type:datetime" orm:"column(modify_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. err = global.DEFAULT_DmSQL.Create(ol).Error
  43. return
  44. }
  45. func (ol *OutLink) GetItemById(id int) (err error) {
  46. sql := `SELECT * FROM out_link WHERE id = ? LIMIT 1`
  47. err = global.DEFAULT_DmSQL.Raw(sql, id).First(ol).Error
  48. return
  49. }
  50. func (ol *OutLink) GetItemsByCondition(condition string, pars []interface{}, orderRule string) (items []*OutLink, err error) {
  51. sql := `SELECT * FROM out_link WHERE 1=1 `
  52. sql += condition
  53. order := ` ORDER BY create_time ASC`
  54. if orderRule != "" {
  55. order = ` ORDER BY ` + orderRule
  56. }
  57. sql += order
  58. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  59. return
  60. }
  61. func (ol *OutLink) Update(cols []string) (err error) {
  62. err = global.DEFAULT_DmSQL.Model(ol).Select(cols).Updates(ol).Error
  63. return
  64. }
  65. func (ol *OutLink) Del() (err error) {
  66. sql := `DELETE FROM out_link WHERE id = ? LIMIT 1`
  67. err = global.DEFAULT_DmSQL.Exec(sql, ol.Id).Error
  68. return
  69. }
  70. func (ol *OutLink) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  71. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, ol.TableName(), condition)
  72. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  73. return
  74. }
  75. func (ol *OutLink) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*OutLink, err error) {
  76. fields := strings.Join(fieldArr, ",")
  77. if len(fieldArr) == 0 {
  78. fields = `*`
  79. }
  80. order := `ORDER BY create_time ASC`
  81. if orderRule != "" {
  82. order = ` ORDER BY ` + orderRule
  83. }
  84. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, ol.TableName(), condition, order)
  85. totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z`
  86. if err = global.DEFAULT_DmSQL.Raw(totalSql, pars...).Scan(&total).Error; err != nil {
  87. return
  88. }
  89. sql += ` LIMIT ?,?`
  90. pars = append(pars, startSize)
  91. pars = append(pars, pageSize)
  92. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  93. return
  94. }