skill.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package academy
  2. import (
  3. "reflect"
  4. "go-common/app/service/main/archive/api"
  5. "go-common/library/time"
  6. )
  7. //Occupation for occupation.
  8. type Occupation struct {
  9. ID int64 `json:"id"`
  10. Rank int64 `json:"rank"`
  11. Name string `json:"name"`
  12. Desc string `json:"desc"`
  13. MainStep string `json:"main_step"`
  14. MainSoftWare string `json:"main_software"`
  15. Logo string `json:"logo"`
  16. }
  17. //Skill for Skill.
  18. type Skill struct {
  19. ID int64 `json:"id"`
  20. OID int64 `json:"oid"`
  21. Name string `json:"name"`
  22. Desc string `json:"desc"`
  23. }
  24. //SkillArc for Skill.
  25. type SkillArc struct {
  26. ID int64 `json:"id"`
  27. AID int64 `json:"aid"`
  28. Type int `json:"type"`
  29. PID int64 `json:"pid"`
  30. SkID int64 `json:"skid"`
  31. SID int64 `json:"sid"`
  32. }
  33. //ArcMeta for skill arc meta.
  34. type ArcMeta struct {
  35. AID int64 `json:"aid"`
  36. MID int64 `json:"mid"`
  37. Cover string `json:"cover"`
  38. Title string `json:"title"`
  39. Type string `json:"type"`
  40. Duration int64 `json:"duration,omitempty"`
  41. PlayTime time.Time `json:"play_time"` //历史课程上次学习时间
  42. Watch int8 `json:"watch"` //标记是否观看过
  43. ArcStat *api.Stat `json:"arc_stat,omitempty"`
  44. Skill *SkillArc `json:"-"`
  45. Business int8 `json:"business"`
  46. Tags map[string][]*Tag `json:"tags,omitempty"`
  47. }
  48. //ArcList for archive list.
  49. type ArcList struct {
  50. Items []*ArcMeta `json:"items"`
  51. Page *ArchivePage `json:"page"`
  52. }
  53. //NewbCourseList for NewbCourse list.
  54. type NewbCourseList struct {
  55. Items []*ArcMeta `json:"items"`
  56. Title string `json:"title"`
  57. TID int64 `json:"tid"`
  58. }
  59. //Play for academy play list.
  60. type Play struct {
  61. MID int64 `json:"mid"`
  62. AID int64 `json:"aid"`
  63. Business int8 `json:"business"`
  64. Watch int8 `json:"watch"`
  65. CTime time.Time `json:"ctime"`
  66. MTime time.Time `json:"mtime"`
  67. }
  68. //SearchKeywords for academy h5 search keywords.
  69. type SearchKeywords struct {
  70. ID int64 `json:"id"`
  71. Rank int64 `json:"rank"`
  72. ParentID int64 `json:"parent_id"`
  73. State int8 `json:"state"`
  74. Name string `json:"name"`
  75. Comment string `json:"comment"`
  76. CTime string `json:"-"`
  77. MTime string `json:"-"`
  78. Count int `json:"count,omitempty"`
  79. Children []*SearchKeywords `json:"children,omitempty"`
  80. }
  81. //Trees for generate tree data set
  82. // data - db result set
  83. // idFieldStr - primary key in table map to struct
  84. // pidFieldStr - top parent id in table map to struct
  85. // chFieldStr - struct child nodes
  86. func Trees(data interface{}, idFieldStr, pidFieldStr, chFieldStr string) (res []interface{}) {
  87. if reflect.TypeOf(data).Kind() != reflect.Slice {
  88. return
  89. }
  90. sli := reflect.ValueOf(data)
  91. top := make(map[int64]interface{})
  92. res = make([]interface{}, 0, sli.Len())
  93. for i := 0; i < sli.Len(); i++ {
  94. v := sli.Index(i).Interface()
  95. if reflect.TypeOf(v).Kind() != reflect.Ptr {
  96. continue
  97. }
  98. if reflect.ValueOf(v).IsNil() {
  99. continue
  100. }
  101. getValue := reflect.ValueOf(v).Elem()
  102. getType := reflect.TypeOf(v).Elem()
  103. pid := getValue.FieldByName(pidFieldStr).Interface().(int64)
  104. if _, ok := getType.FieldByName(pidFieldStr); ok && pid == 0 {
  105. id := getValue.FieldByName(idFieldStr).Interface().(int64)
  106. top[id] = v
  107. res = append(res, v)
  108. }
  109. }
  110. for i := 0; i < sli.Len(); i++ {
  111. v := sli.Index(i).Interface()
  112. if reflect.TypeOf(v).Kind() != reflect.Ptr {
  113. continue
  114. }
  115. if reflect.ValueOf(v).IsNil() {
  116. continue
  117. }
  118. pid := reflect.ValueOf(v).Elem().FieldByName(pidFieldStr).Interface().(int64)
  119. if pid == 0 {
  120. continue
  121. }
  122. if p, ok := top[pid]; ok {
  123. ch := reflect.ValueOf(p).Elem().FieldByName(chFieldStr)
  124. ch.Set(reflect.Append(ch, reflect.ValueOf(v)))
  125. }
  126. }
  127. return
  128. }