label.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/admin/main/tv/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. "github.com/jinzhu/gorm"
  12. )
  13. // ugcLabels refreshes ugc labels
  14. func (s *Service) ugcLabels() (err error) {
  15. var firstTps = s.firstTps()
  16. if err = s.ugcTpLabel(firstTps); err != nil {
  17. log.Error("ugcTpLabel Err %v", err)
  18. return
  19. }
  20. if err = s.ugcPubLabel(firstTps); err != nil {
  21. log.Error("ugcPubLabel Tps %v, Err %v", firstTps, err)
  22. }
  23. return
  24. }
  25. func (s *Service) ugcPubLabel(firstTps []int32) (err error) {
  26. var (
  27. exist bool
  28. cfg = s.c.Cfg.RefLabel
  29. )
  30. for _, v := range firstTps { // check and create pub_time label
  31. pubtCore := &model.LabelCore{
  32. CatType: model.UgcLabel,
  33. Category: v,
  34. Param: model.ParamUgctime,
  35. Value: cfg.AllValue,
  36. Valid: 1,
  37. ParamName: cfg.UgcTime,
  38. Name: cfg.AllName,
  39. }
  40. if exist, err = s.labelExist(pubtCore); err != nil {
  41. return
  42. }
  43. if !exist {
  44. if err = s.DB.Create(&model.LabelDB{LabelCore: *pubtCore}).Error; err != nil {
  45. log.Error("ugcLabels Pubtime All, Create Err %v", err)
  46. return
  47. }
  48. }
  49. time.Sleep(20 * time.Millisecond)
  50. }
  51. return
  52. }
  53. func (s *Service) firstTps() (tps []int32) {
  54. for _, v := range s.ArcTypes {
  55. if v.Pid == 0 {
  56. tps = append(tps, v.ID)
  57. }
  58. }
  59. return
  60. }
  61. func (s *Service) ugcTpLabel(firstTps []int32) (err error) {
  62. var (
  63. exist bool
  64. cfg = s.c.Cfg.RefLabel
  65. )
  66. for _, v := range firstTps {
  67. extCore := &model.LabelCore{
  68. CatType: model.UgcLabel,
  69. Param: model.ParamTypeid,
  70. Valid: 1,
  71. Category: v,
  72. Value: cfg.AllValue,
  73. ParamName: cfg.UgcType,
  74. Name: cfg.AllName,
  75. }
  76. if exist, err = s.labelExist(extCore); err != nil {
  77. return
  78. }
  79. if !exist {
  80. if err = s.DB.Create(&model.LabelDB{LabelCore: *extCore}).Error; err != nil {
  81. log.Error("ugcLabels ArcTypeID %d, Create ALL Label Err %v", v, err)
  82. return
  83. }
  84. }
  85. }
  86. for _, v := range s.ArcTypes {
  87. if v.Pid == 0 { // if first level type, we check the "ALL" label
  88. continue
  89. }
  90. extCore := &model.LabelCore{
  91. CatType: model.UgcLabel,
  92. Param: model.ParamTypeid,
  93. Valid: 1,
  94. Category: v.Pid,
  95. Value: fmt.Sprintf("%d", v.ID),
  96. }
  97. if exist, err = s.labelExist(extCore); err != nil {
  98. return
  99. }
  100. if !exist {
  101. label := model.LabelDB{}
  102. label.FromArcTp(v, s.c.Cfg.RefLabel.UgcType)
  103. if err = s.DB.Create(&label).Error; err != nil {
  104. log.Error("ugcLabels ArcTypeID %d, Create Err %v", v.ID, err)
  105. return
  106. }
  107. time.Sleep(20 * time.Millisecond)
  108. }
  109. }
  110. return
  111. }
  112. // labelsExist distinguishes whether the ids are existing
  113. func (s *Service) labelsExist(ids []int64) (err error) {
  114. var (
  115. labels []*model.LabelDB
  116. idmap = make(map[int64]int)
  117. )
  118. if err = s.DB.Where(fmt.Sprintf("id IN (%s)", xstr.JoinInts(ids))).Where("deleted = 0").Find(&labels).Error; err != nil {
  119. log.Error("labelsExist Ids %v, Err %v", ids, err)
  120. return
  121. }
  122. if len(labels) >= len(ids) {
  123. return
  124. }
  125. for _, v := range labels {
  126. idmap[v.ID] = 1
  127. }
  128. for _, v := range ids {
  129. if _, ok := idmap[v]; !ok {
  130. log.Warn("labelsExist ids %v, not exist %d", ids, v)
  131. return ecode.RequestErr
  132. }
  133. }
  134. return
  135. }
  136. func (s *Service) labelExist(req *model.LabelCore) (exist bool, err error) {
  137. var (
  138. label = model.LabelDB{}
  139. db = s.DB.Model(label).Where("deleted = 0")
  140. )
  141. if req.ID != 0 {
  142. db = db.Where("id = ?", req.ID)
  143. }
  144. if req.Category != 0 {
  145. db = db.Where("category = ?", req.Category)
  146. }
  147. if req.CatType != 0 {
  148. db = db.Where("cat_type = ?", req.CatType)
  149. }
  150. if req.Param != "" {
  151. db = db.Where("param = ?", req.Param)
  152. }
  153. if req.Value != "" {
  154. db = db.Where("value = ?", req.Value)
  155. }
  156. if req.Name != "" {
  157. db = db.Where("name = ?", req.Name)
  158. }
  159. if err = db.First(&label).Error; err != nil {
  160. if err == gorm.ErrRecordNotFound {
  161. err = nil
  162. return
  163. }
  164. log.Error("labelExist V %v, Err %v", req, err)
  165. return
  166. }
  167. if label.ID > 0 {
  168. exist = true
  169. }
  170. return
  171. }
  172. func (s *Service) pgcLabels() (err error) {
  173. var (
  174. result *model.PgcCond
  175. exist bool
  176. )
  177. for _, cat := range s.c.Cfg.SupportCat.PGCTypes {
  178. if result, err = s.dao.PgcCond(context.Background(), cat); err != nil {
  179. log.Error("PgcCond Cat %d, Err %v", cat, err)
  180. return
  181. }
  182. for _, cond := range result.Filter {
  183. if len(cond.Value) == 0 {
  184. continue
  185. }
  186. for _, v := range cond.Value {
  187. if exist, err = s.labelExist(&model.LabelCore{
  188. CatType: model.PgcLabel,
  189. Category: cat,
  190. Param: cond.ID,
  191. Value: v.ID,
  192. }); err != nil {
  193. return
  194. }
  195. if exist {
  196. continue
  197. }
  198. label := model.LabelDB{}
  199. label.FromPgcCond(v, cond, cat)
  200. if err = s.DB.Create(&label).Error; err != nil {
  201. log.Error("pgcLabels Param %s, Cond %v Create Err %v", cond.ID, v, err)
  202. return
  203. }
  204. time.Sleep(20 * time.Millisecond)
  205. }
  206. }
  207. }
  208. return
  209. }
  210. // AddUgcTm adds time label for ugc
  211. func (s *Service) AddUgcTm(tm *model.UgcTime) (err error) {
  212. var (
  213. exist bool
  214. timeV = tm.TimeV()
  215. )
  216. if exist, err = s.labelExist(&model.LabelCore{
  217. Category: tm.Category,
  218. Param: model.ParamUgctime,
  219. Name: tm.Name,
  220. }); err != nil {
  221. return
  222. }
  223. if exist {
  224. err = ecode.TvLabelExist
  225. return
  226. }
  227. label := model.LabelDB{}
  228. label.FromUgcTime(tm, s.c.Cfg.RefLabel.UgcTime)
  229. if err = s.DB.Create(&label).Error; err != nil {
  230. log.Error("ugcTimeLabel Time %s, Create Err %v", timeV, err)
  231. }
  232. return
  233. }
  234. // EditUgcTm edits a time label by name
  235. func (s *Service) EditUgcTm(tm *model.EditUgcTime) (err error) {
  236. var exist bool
  237. if exist, err = s.labelExist(&model.LabelCore{
  238. ID: tm.ID,
  239. }); err != nil {
  240. return
  241. }
  242. if !exist {
  243. err = ecode.NothingFound
  244. return
  245. }
  246. if err = s.DB.Model(&model.LabelDB{}).Where("id = ?", tm.ID).Update(map[string]string{
  247. "name": tm.Name,
  248. "value": tm.TimeV(),
  249. }).Error; err != nil {
  250. log.Error("ugcTimeLabel LabelID %d, Update Err %v", tm.ID, err)
  251. }
  252. return
  253. }
  254. // ActLabels act on labels
  255. func (s *Service) ActLabels(ids []int64, act int) (err error) {
  256. if err = s.labelsExist(ids); err != nil {
  257. return
  258. }
  259. if err = s.DB.Model(&model.LabelDB{}).Where(fmt.Sprintf("id IN (%s)", xstr.JoinInts(ids))).Update(map[string]int{
  260. "valid": act,
  261. }).Error; err != nil {
  262. log.Error("ActLabels LabelID %s, Update Err %v", ids, err)
  263. }
  264. return
  265. }
  266. // DelLabels deletes labels
  267. func (s *Service) DelLabels(ids []int64) (err error) {
  268. var exist bool
  269. for _, v := range ids {
  270. if exist, err = s.labelExist(&model.LabelCore{
  271. ID: v,
  272. CatType: model.UgcLabel,
  273. Param: model.ParamUgctime,
  274. }); err != nil {
  275. return
  276. }
  277. if !exist {
  278. log.Warn("DelLabels IDs %v, ID not exist %d", ids, v)
  279. return ecode.RequestErr
  280. }
  281. }
  282. if err = s.DB.Exec(fmt.Sprintf("UPDATE tv_label SET deleted = 1 WHERE id IN (%s)", xstr.JoinInts(ids))).Error; err != nil {
  283. log.Error("DelLabels LabelID %s, Update Err %v", ids, err)
  284. }
  285. return
  286. }
  287. // DynamicLabels picks the defined pgc label types
  288. func (s *Service) labelTypes() (tps []*model.TpLabel, err error) {
  289. var rows *sql.Rows
  290. // select category, param , param_name from tv_label where deleted = 0 and cat_type = 1 group by category,param
  291. if rows, err = s.DB.Model(&model.LabelDB{}).Where("deleted = 0").
  292. Where("cat_type = ?", model.PgcLabel).Select("category, param, param_name").Group("category,param").Rows(); err != nil {
  293. log.Error("labelTypes rows Err %v", err)
  294. return
  295. }
  296. defer rows.Close()
  297. for rows.Next() {
  298. var cont = &model.TpLabel{}
  299. if err = rows.Scan(&cont.Category, &cont.Param, &cont.ParamName); err != nil {
  300. log.Error("rows.Scan error(%v)", err)
  301. return
  302. }
  303. tps = append(tps, cont)
  304. }
  305. if err = rows.Err(); err != nil {
  306. log.Error("labelTypes rows Err %v", err)
  307. }
  308. return
  309. }
  310. func (s *Service) loadLabel() (err error) {
  311. var (
  312. newTps = make(map[int][]*model.TpLabel)
  313. labels []*model.TpLabel
  314. )
  315. if labels, err = s.labelTypes(); err != nil {
  316. log.Error("labelTypes err %v", err)
  317. time.Sleep(time.Duration(10 * time.Second))
  318. return
  319. }
  320. for _, v := range labels {
  321. if lbs, ok := newTps[v.Category]; ok {
  322. newTps[v.Category] = append(lbs, v)
  323. } else {
  324. newTps[v.Category] = append([]*model.TpLabel{}, v)
  325. }
  326. }
  327. if len(newTps) > 0 {
  328. s.labelTps = newTps
  329. }
  330. return
  331. }
  332. // LabelTp returns the category's label types
  333. func (s *Service) LabelTp(category int) (lbs []*model.TpLabel, err error) {
  334. var ok bool
  335. if lbs, ok = s.labelTps[category]; !ok {
  336. err = ecode.RequestErr
  337. }
  338. return
  339. }
  340. // PickLabels picks ugc labels
  341. func (s *Service) PickLabels(req *model.ReqLabel, catType int) (data []*model.LabelList, err error) {
  342. var (
  343. db = s.DB.Model(&model.LabelDB{}).
  344. Where("param = ?", req.Param).
  345. Where("category = ?", req.Category).
  346. Where("deleted = 0").
  347. Where("cat_type = ?", catType)
  348. labels []*model.LabelDB
  349. )
  350. if req.Title != "" {
  351. db = db.Where("name LIKE ?", "%"+req.Title+"%")
  352. }
  353. if req.ID != 0 {
  354. db = db.Where("id = ?", req.ID)
  355. }
  356. if err = db.Order("position ASC").Find(&labels).Error; err != nil {
  357. log.Error("PickLabels Req %v, Err %v", req, err)
  358. }
  359. for _, v := range labels {
  360. data = append(data, v.ToList())
  361. }
  362. return
  363. }
  364. // EditLabel edits a pgc label
  365. func (s *Service) EditLabel(id int64, name string) (err error) {
  366. var exist bool
  367. if exist, err = s.labelExist(&model.LabelCore{
  368. ID: id,
  369. }); err != nil {
  370. return
  371. }
  372. if !exist {
  373. err = ecode.NothingFound
  374. return
  375. }
  376. if err = s.DB.Model(&model.LabelDB{}).Where("id = ?", id).Update(map[string]string{
  377. "name": name,
  378. }).Error; err != nil {
  379. log.Error("EditLabel LabelID %d, Update Err %v", id, err)
  380. }
  381. return
  382. }
  383. // PubLabel publish label's order
  384. func (s *Service) PubLabel(ids []int64) (err error) {
  385. if len(ids) == 0 {
  386. return
  387. }
  388. var (
  389. labels []*model.LabelDB
  390. position = 1
  391. tx = s.DB.Begin()
  392. labelMap = make(map[int64]*model.LabelDB, len(ids))
  393. )
  394. if err = s.DB.Where(fmt.Sprintf("id IN (%s)", xstr.JoinInts(ids))).Find(&labels).Error; err != nil {
  395. log.Error("PubLabel Ids %v, Err %v", ids, err)
  396. return
  397. }
  398. if len(labels) == 0 {
  399. err = ecode.NothingFound
  400. return
  401. }
  402. for _, v := range labels {
  403. labelMap[v.ID] = v
  404. }
  405. for _, id := range ids {
  406. lbl, ok := labelMap[id]
  407. if !ok {
  408. err = ecode.RequestErr
  409. log.Warn("PubLabel Id %d Not found", id)
  410. return
  411. }
  412. if !lbl.SameType(labels[0]) {
  413. log.Error("PubLabel Id %d FirstLabel ID %d, Not same type", lbl.ID, labels[0].ID)
  414. err = ecode.RequestErr
  415. tx.Rollback()
  416. return
  417. }
  418. if err = tx.Model(&model.LabelDB{}).Where("id = ?", lbl.ID).Update(map[string]int{"position": position}).Error; err != nil {
  419. log.Error("PubLabel ID %d, Err %v", lbl.ID, err)
  420. tx.Rollback()
  421. return
  422. }
  423. position = position + 1
  424. }
  425. tx.Commit()
  426. return
  427. }