list_model.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package models
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "github.com/qor/oss"
  9. "github.com/qor5/admin/publish"
  10. "github.com/theplant/sliceutils"
  11. "gorm.io/gorm"
  12. )
  13. type ListModel struct {
  14. gorm.Model
  15. Title string
  16. publish.Status
  17. publish.Schedule
  18. publish.List
  19. publish.Version
  20. }
  21. func (this *ListModel) PrimarySlug() string {
  22. return fmt.Sprintf("%v_%v", this.ID, this.Version.Version)
  23. }
  24. func (this *ListModel) PermissionRN() []string {
  25. return []string{"list_models", strconv.Itoa(int(this.ID)), this.Version.Version}
  26. }
  27. func (this *ListModel) PrimaryColumnValuesBySlug(slug string) map[string]string {
  28. segs := strings.Split(slug, "_")
  29. if len(segs) != 2 {
  30. panic("wrong slug")
  31. }
  32. return map[string]string{
  33. "id": segs[0],
  34. "version": segs[1],
  35. }
  36. }
  37. func (this *ListModel) GetPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
  38. objs = append(objs, &publish.PublishAction{
  39. Url: this.getPublishUrl(),
  40. Content: this.getPublishContent(),
  41. IsDelete: false,
  42. })
  43. if this.GetStatus() == publish.StatusOnline && this.GetOnlineUrl() != this.getPublishUrl() {
  44. objs = append(objs, &publish.PublishAction{
  45. Url: this.GetOnlineUrl(),
  46. IsDelete: true,
  47. })
  48. }
  49. this.SetOnlineUrl(this.getPublishUrl())
  50. return
  51. }
  52. func (this *ListModel) GetUnPublishActions(db *gorm.DB, ctx context.Context, storage oss.StorageInterface) (objs []*publish.PublishAction, err error) {
  53. objs = append(objs, &publish.PublishAction{
  54. Url: this.GetOnlineUrl(),
  55. IsDelete: true,
  56. })
  57. return
  58. }
  59. func (this ListModel) getPublishUrl() string {
  60. return fmt.Sprintf("/list_model/%v/index.html", this.ID)
  61. }
  62. func (this ListModel) getPublishContent() string {
  63. return fmt.Sprintf("id: %v, title: %v", this.ID, this.Title)
  64. }
  65. func (this ListModel) GetListUrl(pageNumber string) string {
  66. return fmt.Sprintf("/list_model/list/%v.html", pageNumber)
  67. }
  68. func (this ListModel) GetListContent(db *gorm.DB, onePageItems *publish.OnePageItems) string {
  69. pageNumber := onePageItems.PageNumber
  70. var result string
  71. for _, item := range onePageItems.Items {
  72. result = result + fmt.Sprintf("%v</br>", item)
  73. }
  74. result = result + fmt.Sprintf("</br>pageNumber: %v</br>", pageNumber)
  75. return result
  76. }
  77. func (this ListModel) Sort(array []interface{}) {
  78. var temp []*ListModel
  79. sliceutils.Unwrap(array, &temp)
  80. sort.Sort(SliceListModel(temp))
  81. for k, v := range temp {
  82. array[k] = v
  83. }
  84. return
  85. }
  86. type SliceListModel []*ListModel
  87. func (x SliceListModel) Len() int { return len(x) }
  88. func (x SliceListModel) Less(i, j int) bool { return x[i].Title < x[j].Title }
  89. func (x SliceListModel) Swap(i, j int) { x[i], x[j] = x[j], x[i] }