archive.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package service
  2. import (
  3. "fmt"
  4. "context"
  5. "go-common/app/admin/main/tv/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _arcOnline = 1
  13. _arcOffline = 2
  14. )
  15. // typeBubbleSort sort type
  16. func typeBubbleSort(pTypes []model.UgcType) (pSortTypes []model.UgcType) {
  17. flag := true
  18. for i := 0; i < len(pTypes)-1; i++ {
  19. flag = true
  20. for j := 0; j < len(pTypes)-i-1; j++ {
  21. if pTypes[j].ID > pTypes[j+1].ID {
  22. pTypes[j], pTypes[j+1] = pTypes[j+1], pTypes[j]
  23. flag = false
  24. }
  25. }
  26. if flag {
  27. break
  28. }
  29. }
  30. pSortTypes = pTypes
  31. return
  32. }
  33. func (s *Service) existArcTps(passed bool) (existTypes map[int32]int, err error) {
  34. var (
  35. arcs []*model.Archive
  36. db = s.DB.Where("deleted = ?", 0)
  37. )
  38. if passed {
  39. db = db.Where("result = ?", 1)
  40. }
  41. existTypes = make(map[int32]int)
  42. if err = db.Select("DISTINCT(typeid)").Find(&arcs).Error; err != nil {
  43. log.Error("DistinctType Error %v", err)
  44. return
  45. }
  46. for _, v := range arcs {
  47. existTypes[v.TypeID] = 1
  48. }
  49. return
  50. }
  51. //arcTp return archive type list
  52. func (s *Service) arcTp(passed bool) (pTypes []model.UgcType, err error) {
  53. var (
  54. cTypeList = make(map[int32][]model.UgcCType)
  55. oriPTypes []model.UgcType
  56. existTypes map[int32]int
  57. )
  58. typeList := s.ArcTypes
  59. if existTypes, err = s.existArcTps(passed); err != nil {
  60. return
  61. }
  62. //make parent and child node sperate
  63. for _, v := range typeList {
  64. if v.Pid == 0 {
  65. oriPTypes = append(oriPTypes, model.UgcType{
  66. ID: v.ID,
  67. Name: v.Name,
  68. })
  69. } else {
  70. cType := model.UgcCType{
  71. Pid: v.Pid,
  72. ID: v.ID,
  73. Name: v.Name,
  74. }
  75. if _, ok := existTypes[v.ID]; ok {
  76. cTypeList[v.Pid] = append(cTypeList[v.Pid], cType)
  77. }
  78. }
  79. }
  80. for _, v := range oriPTypes {
  81. if cValue, ok := cTypeList[v.ID]; ok {
  82. v.Children = cValue
  83. pTypes = append(pTypes, v)
  84. }
  85. }
  86. pTypes = typeBubbleSort(pTypes)
  87. return
  88. }
  89. func (s *Service) loadTps() {
  90. var (
  91. data = &model.AvailTps{}
  92. err error
  93. )
  94. if data.AllTps, err = s.arcTp(false); err != nil {
  95. log.Error("loadTps Passed Err %v", err)
  96. return
  97. }
  98. if data.PassedTps, err = s.arcTp(true); err != nil {
  99. log.Error("loadTps All Err %v", err)
  100. return
  101. }
  102. if len(data.AllTps) > 0 || len(data.PassedTps) > 0 {
  103. s.avaiTps = data
  104. }
  105. }
  106. // GetTps get cms used types data
  107. func (s *Service) GetTps(c context.Context, passed bool) (data []model.UgcType, err error) {
  108. if s.avaiTps == nil {
  109. err = ecode.ServiceUnavailable
  110. return
  111. }
  112. if passed {
  113. data = s.avaiTps.PassedTps
  114. } else {
  115. data = s.avaiTps.AllTps
  116. }
  117. return
  118. }
  119. //GetArchivePid get archive pid with child id
  120. func (s *Service) GetArchivePid(id int32) (pid int32) {
  121. if value, ok := s.ArcTypes[id]; ok {
  122. pid = value.Pid
  123. return
  124. }
  125. return 0
  126. }
  127. func (s *Service) midTreat(param *model.ArcListParam) (mids []int64) {
  128. if param.Mid != 0 {
  129. return []int64{param.Mid}
  130. }
  131. if param.UpName != "" {
  132. var data []*model.Upper
  133. if err := s.DB.Where("ori_name LIKE ?", "%"+param.UpName+"%").Where("deleted = 0").Find(&data).Error; err != nil {
  134. log.Error("ArchiveList MidTreat UpName %s, Err %v", param.UpName, err)
  135. return
  136. }
  137. if len(data) > 0 {
  138. for _, v := range data {
  139. mids = append(mids, v.MID)
  140. }
  141. }
  142. }
  143. return
  144. }
  145. // ArchiveList is used for getting archive list
  146. func (s *Service) ArchiveList(c *bm.Context, param *model.ArcListParam) (pager *model.ArcPager, err error) {
  147. var (
  148. archives []*model.ArcDB
  149. reqES = new(model.ReqArcES)
  150. data *model.EsUgcResult
  151. aids []int64
  152. mids []int64
  153. upsInfo map[int64]string
  154. )
  155. reqES.FromArcListParam(param, s.typeidsTreat(param.Typeid, param.Pid))
  156. reqES.Mids = s.midTreat(param)
  157. pager = new(model.ArcPager)
  158. if data, err = s.dao.ArcES(c, reqES); err != nil {
  159. log.Error("ArchiveList Req %v, Err %v", param, err)
  160. return
  161. }
  162. pager.Page = data.Page
  163. if len(data.Result) == 0 {
  164. return
  165. }
  166. for _, v := range data.Result {
  167. aids = append(aids, v.AID)
  168. mids = append(mids, v.MID)
  169. }
  170. if err = s.DB.Order("mtime " + reqES.MtimeSort()).Where(fmt.Sprintf("aid IN (%s)", xstr.JoinInts(aids))).Find(&archives).Error; err != nil {
  171. log.Error("s.ArchiveList Find archives error(%v)", err)
  172. return
  173. }
  174. if upsInfo, err = s.pickUps(mids); err != nil {
  175. return
  176. }
  177. for _, v := range archives {
  178. item := v.ToList(s.GetArchivePid(v.TypeID))
  179. if name, ok := upsInfo[v.MID]; ok {
  180. item.UpName = name
  181. }
  182. pager.Items = append(pager.Items, item)
  183. }
  184. return
  185. }
  186. func (s *Service) pickUps(mids []int64) (res map[int64]string, err error) {
  187. if len(mids) == 0 {
  188. return
  189. }
  190. var resSlice []*model.CmsUpper
  191. res = make(map[int64]string, len(mids))
  192. if err = s.DB.Where(fmt.Sprintf("mid IN (%s)", xstr.JoinInts(mids))).Where("deleted = 0").Find(&resSlice).Error; err != nil {
  193. log.Error("pickUps Mids %v, Err %v", mids, err)
  194. return
  195. }
  196. for _, v := range resSlice {
  197. res[v.MID] = v.OriName
  198. }
  199. return
  200. }
  201. // ArcAction is used for online ugc archive
  202. func (s *Service) ArcAction(ids []int64, action int) (err error) {
  203. var (
  204. w = map[string]interface{}{"deleted": 0, "result": 1}
  205. tx = s.DB.Model(&model.Archive{}).Begin()
  206. actValid int
  207. )
  208. if action == _arcOnline {
  209. actValid = 1
  210. } else if action == _arcOffline {
  211. actValid = 0
  212. } else {
  213. return ecode.TvDangbeiWrongType
  214. }
  215. for _, v := range ids {
  216. arch := model.Archive{}
  217. if errDB := tx.Where(w).Where("id=?", v).First(&arch).Error; errDB != nil {
  218. err = fmt.Errorf("找不到id为%v的数据", v)
  219. log.Error("s.ArcAction First error(%v)", err)
  220. tx.Rollback()
  221. return
  222. }
  223. if errDB := tx.Where("id=?", v).
  224. Update("valid", actValid).Error; errDB != nil {
  225. err = errDB
  226. log.Error("s.ArcAction Update error(%v)", err)
  227. tx.Rollback()
  228. return
  229. }
  230. }
  231. tx.Commit()
  232. return
  233. }
  234. // ArcUpdate is used for update ugc archive
  235. func (s *Service) ArcUpdate(id int64, cover string, content string, title string) (err error) {
  236. up := map[string]interface{}{
  237. "cover": cover,
  238. "content": content,
  239. "title": title,
  240. }
  241. if err = s.DB.Model(&model.Archive{}).Where("id=?", id).Update(up).Error; err != nil {
  242. log.Error("s.ArcUpdate Update error(%v)", err)
  243. return
  244. }
  245. return
  246. }