modules.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package service
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "go-common/app/admin/main/tv/model"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "github.com/jinzhu/gorm"
  10. )
  11. //ModulesAdd is used for add modules
  12. func (s *Service) ModulesAdd(v *model.Modules) (err error) {
  13. var (
  14. order uint8
  15. mod *model.Modules
  16. )
  17. if mod, err = s.isModulesExists(v.PageID, v.Title); err != nil {
  18. return
  19. }
  20. if mod != nil {
  21. err = fmt.Errorf("当前模块下,标题已存在")
  22. return
  23. }
  24. if order, err = s.getOrder(v.PageID); err != nil {
  25. return
  26. }
  27. //在已存在顺序上加一
  28. v.Order = order + 1
  29. if err = s.DB.Model(&model.Modules{}).Create(v).Error; err != nil {
  30. return
  31. }
  32. return
  33. }
  34. //isModulesExists is use for checking is module exists
  35. func (s *Service) isModulesExists(pageID string, title string) (v *model.Modules, err error) {
  36. v = &model.Modules{}
  37. w := map[string]interface{}{
  38. "deleted": model.ModulesNotDelete,
  39. "page_id": pageID,
  40. "title": title,
  41. }
  42. if err = s.DB.Where(w).First(v).Error; err != nil {
  43. if err == gorm.ErrRecordNotFound {
  44. return nil, nil
  45. }
  46. return
  47. }
  48. return
  49. }
  50. //getOrder is used for getting existed model order
  51. func (s *Service) getOrder(pageID string) (order uint8, err error) {
  52. var v model.Modules
  53. if err = s.DB.Where("deleted = 0").Where("page_id = ?", pageID).Order("`order` DESC").First(&v).Error; err != nil {
  54. if err == gorm.ErrRecordNotFound {
  55. return 0, nil
  56. }
  57. log.Error("getOrder Err %v", err)
  58. return
  59. }
  60. order = v.Order
  61. return
  62. }
  63. //ModulesList is used for get module list
  64. func (s *Service) ModulesList(pageID string) (v []*model.Modules, err error) {
  65. selectStr := []string{
  66. "id",
  67. "title",
  68. "page_id",
  69. "source",
  70. "type",
  71. "flexible",
  72. "icon",
  73. "capacity",
  74. "more",
  75. "`order`",
  76. "moretype",
  77. "morepage",
  78. "valid",
  79. "src_type",
  80. }
  81. w := map[string]interface{}{
  82. "deleted": model.ModulesNotDelete,
  83. "page_id": pageID,
  84. }
  85. if err = s.DB.Where(w).Select(selectStr).Order("`order` ASC").Find(&v).Error; err != nil {
  86. return
  87. }
  88. for i := range v {
  89. attr := v[i]
  90. pid, _ := strconv.Atoi(attr.PageID)
  91. switch pid {
  92. case model.PageMain:
  93. attr.PageID = "主页"
  94. case model.PageJP:
  95. attr.PageID = "番剧"
  96. case model.PageMovie:
  97. attr.PageID = "电影"
  98. case model.PageDocumentary:
  99. attr.PageID = "纪录片"
  100. case model.PageCN:
  101. attr.PageID = "国创"
  102. case model.PageSoapopera:
  103. attr.PageID = "电视剧"
  104. }
  105. t, _ := strconv.Atoi(attr.Type)
  106. switch t {
  107. case model.TypeSevenFocus:
  108. attr.Type = "首页七格焦点图"
  109. case model.TypeFiveFocus:
  110. attr.Type = "5格焦点"
  111. case model.TypeSixFocus:
  112. attr.Type = "6格焦点"
  113. case model.TypeVertListFirst:
  114. attr.Type = "竖图1列表"
  115. case model.TypeVertListSecond:
  116. attr.Type = "竖图2列表"
  117. case model.TypeHorizList:
  118. attr.Type = "横图列表"
  119. case model.TypeZhuiFan:
  120. attr.Type = "追番模块"
  121. }
  122. }
  123. return
  124. }
  125. //ModulesEditGet is used for get module with module id
  126. func (s *Service) ModulesEditGet(id uint64) (v *model.Modules, err error) {
  127. selectStr := []string{
  128. "id",
  129. "title",
  130. "page_id",
  131. "type",
  132. "source",
  133. "flexible",
  134. "icon",
  135. "capacity",
  136. "more",
  137. "`order`",
  138. "moretype",
  139. "morepage",
  140. "src_type",
  141. }
  142. w := map[string]interface{}{
  143. "id": id,
  144. "deleted": model.ModulesNotDelete,
  145. }
  146. v = &model.Modules{}
  147. if err = s.DB.Where(w).Select(selectStr).First(v).Error; err != nil {
  148. return
  149. }
  150. return
  151. }
  152. //ModulesEditPost is used for update module value
  153. func (s *Service) ModulesEditPost(id uint64, v *model.Modules) (err error) {
  154. var (
  155. mod = &model.Modules{}
  156. )
  157. if mod, err = s.isModulesExists(v.PageID, v.Title); err != nil {
  158. return
  159. }
  160. if mod != nil && mod.ID != id {
  161. err = fmt.Errorf("当前模块下,标题已存在")
  162. return
  163. }
  164. return s.DB.Model(&model.Modules{}).Where("id = ?", id).Update(v).Error
  165. }
  166. //GetModPub is used for get publish status from MC
  167. func (s *Service) GetModPub(c *bm.Context, pageID string) (p model.ModPub, err error) {
  168. return s.dao.GetModPub(c, pageID)
  169. }
  170. //ModulesPublish is used for publish module or deleted modules
  171. func (s *Service) ModulesPublish(c *bm.Context, pageID string, state uint8, ids []int, deletedIds []int) (err error) {
  172. if len(ids) > 30 {
  173. err = fmt.Errorf("模块发布不能超过30个")
  174. return
  175. }
  176. tx := s.DB.Begin()
  177. for k, v := range ids {
  178. up := map[string]interface{}{
  179. "order": k + 1,
  180. "valid": model.ModulesValid,
  181. }
  182. where := map[string]interface{}{
  183. "id": v,
  184. "page_id": pageID,
  185. }
  186. if err = tx.Model(&model.Modules{}).Where(where).Update(up).Error; err != nil {
  187. tx.Rollback()
  188. return
  189. }
  190. }
  191. if len(deletedIds) > 0 {
  192. deletedUp := map[string]interface{}{
  193. "deleted": model.ModulesDelete,
  194. }
  195. if err = s.DB.Model(&model.Modules{}).Where("id in (?)", deletedIds).Where("page_id=?", pageID).
  196. Update(deletedUp).Error; err != nil {
  197. tx.Rollback()
  198. return
  199. }
  200. }
  201. if err = s.SetPublish(c, pageID, state); err != nil {
  202. tx.Rollback()
  203. return
  204. }
  205. tx.Commit()
  206. return
  207. }
  208. //SetPublish is used for set publish status
  209. func (s *Service) SetPublish(c *bm.Context, pageID string, state uint8) (err error) {
  210. nowTime := time.Now()
  211. t := nowTime.Format("2006-01-02 15:04:05")
  212. p := model.ModPub{
  213. Time: t,
  214. State: state,
  215. }
  216. return s.dao.SetModPub(c, pageID, p)
  217. }
  218. // TypeSupport distinguish whether the source is supported or not
  219. func (s *Service) TypeSupport(srcType int, source int) bool {
  220. if srcType == _TypePGC {
  221. _, ok := s.supCatMap.PgcMap[int32(source)]
  222. return ok
  223. }
  224. if srcType == _TypeUGC {
  225. _, ok := s.supCatMap.UgcMap[int32(source)]
  226. return ok
  227. }
  228. return false
  229. }
  230. // loadCats, reload pgc & ugc support cats
  231. func (s *Service) loadCats() {
  232. var (
  233. pgcTypes = s.c.Cfg.SupportCat.PGCTypes
  234. ugcTypes = s.c.Cfg.SupportCat.UGCTypes
  235. newCats = []*model.ParentCat{}
  236. newCatsMap = &model.SupCats{
  237. UgcMap: make(map[int32]int),
  238. PgcMap: make(map[int32]int),
  239. }
  240. )
  241. // load supporting pgc types
  242. if len(pgcTypes) > 0 {
  243. for _, v := range pgcTypes {
  244. newCats = append(newCats, &model.ParentCat{
  245. ID: v,
  246. Name: s.pgcCatToName(int(v)),
  247. Type: _TypePGC,
  248. })
  249. newCatsMap.PgcMap[v] = 1
  250. }
  251. }
  252. // load support ugc first level types and their children
  253. if len(ugcTypes) > 0 {
  254. for _, v := range ugcTypes {
  255. newCatsMap.UgcMap[v] = 1
  256. var ugcCat = &model.ParentCat{
  257. ID: v,
  258. Type: _TypeUGC,
  259. }
  260. if tp, ok := s.ArcTypes[v]; ok {
  261. ugcCat.Name = tp.Name
  262. }
  263. for _, types := range s.ArcTypes { // gather children
  264. if types.Pid == v {
  265. ugcCat.Children = append(ugcCat.Children, &model.CommonCat{
  266. ID: types.ID,
  267. Name: types.Name,
  268. PID: types.Pid,
  269. Type: _TypeUGC,
  270. })
  271. newCatsMap.UgcMap[types.ID] = 1
  272. }
  273. }
  274. newCats = append(newCats, ugcCat)
  275. }
  276. }
  277. if len(newCats) > 0 {
  278. s.SupCats = newCats
  279. s.supCatMap = newCatsMap
  280. }
  281. }