watermark.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/main/tv/model"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. xtime "go-common/library/time"
  12. "go-common/library/ecode"
  13. "go-common/library/xstr"
  14. "github.com/jinzhu/gorm"
  15. )
  16. const _pagesize = 20
  17. //WaterMarkist water mark list
  18. func (s *Service) WaterMarkist(c *bm.Context, param *model.WaterMarkListParam) (pager *model.WaterMarkListPager, err error) {
  19. var (
  20. order string
  21. total int
  22. markList []*model.WaterMarkList
  23. )
  24. selectStr := []string{
  25. "tv_content.id",
  26. "tv_content.epid",
  27. "tv_content.season_id",
  28. "tv_content.title AS content_title",
  29. "tv_content.mark_time",
  30. "tv_ep_season.category",
  31. "tv_ep_season.title AS season_title",
  32. }
  33. db := s.DB.Model(&model.WaterMarkList{})
  34. db = db.Select(selectStr).
  35. Where("tv_content.is_deleted = ?", 0).
  36. Where("tv_content.mark = ?", model.WatermarkWhite).
  37. Joins("LEFT JOIN tv_ep_season ON tv_ep_season.id = tv_content.season_id")
  38. if param.Category != "" {
  39. db = db.Where("tv_ep_season.category = ?", param.Category)
  40. }
  41. if param.EpID != "" {
  42. db = db.Where("tv_content.epid = ?", param.EpID)
  43. }
  44. if param.SeasonID != "" {
  45. db = db.Where("tv_content.season_id = ?", param.SeasonID)
  46. }
  47. if param.Order == model.OrderDesc {
  48. order = "tv_content.mtime DESC"
  49. } else {
  50. order = "tv_content.mtime ASC"
  51. }
  52. if err = db.Order(order).Offset((param.Pn - 1) * param.Ps).Limit(param.Ps).Find(&markList).Error; err != nil {
  53. return
  54. }
  55. for i := range markList {
  56. attr := markList[i]
  57. attr.Category = s.pgcCatToName(atoi(attr.Category))
  58. }
  59. if err = db.Count(&total).Error; err != nil {
  60. return
  61. }
  62. pager = &model.WaterMarkListPager{
  63. Items: markList,
  64. Page: &model.Page{
  65. Num: param.Pn,
  66. Size: param.Ps,
  67. Total: int(total),
  68. },
  69. }
  70. return
  71. }
  72. //AddEpID add water mark by ep id
  73. func (s *Service) AddEpID(c *bm.Context, ids []int64) (res *model.AddEpIDResp, err error) {
  74. var (
  75. notExist bool
  76. )
  77. res = &model.AddEpIDResp{
  78. Succ: []int64{},
  79. NotExist: []int64{},
  80. Invalids: []int64{},
  81. }
  82. for _, v := range ids {
  83. var mark model.WaterMarkOne
  84. if notExist, err = s.valueWithEpID(v, &mark); err != nil {
  85. return
  86. }
  87. if notExist {
  88. res.NotExist = append(res.NotExist, v)
  89. continue
  90. }
  91. if mark.Mark == model.WatermarkWhite {
  92. res.Invalids = append(res.Invalids, v)
  93. continue
  94. }
  95. if err = s.updateWithEpID(v); err != nil {
  96. return
  97. }
  98. res.Succ = append(res.Succ, v)
  99. }
  100. return
  101. }
  102. //valueWithEpID get value with epid
  103. func (s *Service) valueWithEpID(id int64, m *model.WaterMarkOne) (exist bool, err error) {
  104. if err = s.DB.Where("is_deleted = 0").Where("epid = ?", id).First(m).Error; err != nil {
  105. if err == gorm.ErrRecordNotFound {
  106. return true, nil
  107. }
  108. return
  109. }
  110. return false, nil
  111. }
  112. //updateWithEpID update with epid
  113. func (s *Service) updateWithEpID(id int64) (err error) {
  114. up := map[string]interface{}{
  115. "mark": model.WatermarkWhite,
  116. "mark_time": time.Now().Format("2006-01-02 15:04:05"),
  117. }
  118. if err = s.DB.Model(&model.WaterMarkOne{}).Where("epid=?", id).Update(up).Error; err != nil {
  119. return
  120. }
  121. return
  122. }
  123. //AddSeasonID add water mark with season id
  124. func (s *Service) AddSeasonID(c *bm.Context, ids []int64) (res *model.AddEpIDResp, err error) {
  125. var (
  126. notExist bool
  127. )
  128. res = &model.AddEpIDResp{
  129. Succ: []int64{},
  130. NotExist: []int64{},
  131. Invalids: []int64{},
  132. }
  133. for _, v := range ids {
  134. var mark model.WaterMarkOne
  135. if notExist, err = s.valueWithSeasonID(v, &mark); err != nil {
  136. return
  137. }
  138. if notExist {
  139. res.NotExist = append(res.NotExist, v)
  140. continue
  141. }
  142. if err = s.updateWithSeasonID(v); err != nil {
  143. return
  144. }
  145. res.Succ = append(res.Succ, v)
  146. }
  147. return
  148. }
  149. //valueWithSeasonID get value with season id
  150. func (s *Service) valueWithSeasonID(id int64, m *model.WaterMarkOne) (exist bool, err error) {
  151. w := map[string]interface{}{
  152. "is_deleted": 0,
  153. "season_id": id,
  154. //"mark": model.WatermarkDefault,
  155. }
  156. if err = s.DB.Where(w).First(m).Error; err != nil {
  157. if err == gorm.ErrRecordNotFound {
  158. return true, nil
  159. }
  160. return
  161. }
  162. return false, nil
  163. }
  164. //updateWithSeasonID update with seasonID
  165. func (s *Service) updateWithSeasonID(id int64) (err error) {
  166. up := map[string]interface{}{
  167. "mark": model.WatermarkWhite,
  168. "mark_time": time.Now().Format("2006-01-02 15:04:05"),
  169. }
  170. if err = s.DB.Model(&model.WaterMarkOne{}).Where("season_id=?", id).
  171. Update(up).Error; err != nil {
  172. return
  173. }
  174. return
  175. }
  176. //DeleteWatermark delete watermark
  177. func (s *Service) DeleteWatermark(ids []int64) (err error) {
  178. if len(ids) > 50 {
  179. err = fmt.Errorf("更新数量最多为50条")
  180. return
  181. }
  182. up := map[string]interface{}{
  183. "mark": model.WatermarkDefault,
  184. }
  185. if err = s.DB.Model(&model.WaterMarkOne{}).Where("id in (?)", ids).
  186. Update(up).Error; err != nil {
  187. return
  188. }
  189. return
  190. }
  191. // titleMatch picks the title match seasons
  192. func (s *Service) titleMatch(title string) (match []int64) {
  193. if len(s.snsInfo) == 0 {
  194. return
  195. }
  196. for _, v := range s.snsInfo {
  197. if strings.Contains(v.Title, title) {
  198. match = append(match, v.ID)
  199. }
  200. }
  201. return
  202. }
  203. // TransList picks the transcode list
  204. func (s *Service) TransList(ctx context.Context, req *model.TransReq) (data *model.TransPager, err error) {
  205. var (
  206. db *gorm.DB
  207. cntEp int
  208. cntSn = new(model.SnCount)
  209. )
  210. data = &model.TransPager{
  211. Page: &model.Page{
  212. Num: req.Pn, Size: _pagesize, Total: 0,
  213. },
  214. Items: make([]*model.TransReply, 0),
  215. }
  216. if db, err = s.transReqT(req); err != nil {
  217. return
  218. }
  219. if err = db.Count(&cntEp).Error; err != nil { //
  220. log.Error("countEp Err %v", err)
  221. return
  222. }
  223. if cntEp == 0 { // if no result ,just return
  224. data.CountSn = 0
  225. return
  226. }
  227. if (req.Pn-1)*_pagesize >= cntEp { // if page is much bigger than existing pages, just return error
  228. err = ecode.TvDangbeiPageNotExist
  229. return
  230. }
  231. if err = db.Table("tv_content").Select("COUNT(DISTINCT(season_id)) AS count").First(cntSn).Error; err != nil { // count season
  232. log.Error("countSn Err %v", err)
  233. return
  234. }
  235. db = postReqT(req, db) // order by & limit
  236. if data.Items, err = s.transDB(db); err != nil {
  237. log.Error("transDB Err %v", err)
  238. return
  239. }
  240. data.Page = &model.Page{
  241. Num: req.Pn,
  242. Size: _pagesize,
  243. Total: cntEp,
  244. }
  245. data.CountSn = cntSn.Count
  246. return
  247. }
  248. func (s *Service) transDB(db *gorm.DB) (items []*model.TransReply, err error) {
  249. var rows *sql.Rows
  250. rows, err = db.Select("epid, title, transcoded, apply_time, mark_time, season_id").Rows()
  251. if err != nil {
  252. log.Error("rows Err %v", err)
  253. return
  254. }
  255. for rows.Next() {
  256. var (
  257. cont = &model.TransReply{}
  258. aTime, mTime xtime.Time
  259. )
  260. if err = rows.Scan(&cont.EpID, &cont.Etitle, &cont.Transcoded, &aTime, &mTime, &cont.SeasonID); err != nil {
  261. log.Error("rows.Scan error(%v)", err)
  262. return
  263. }
  264. if mTime > xtime.Time(0) { // resolve negative value issue
  265. cont.MarkTime = mTime.Time().Format("2006-01-02 15:04:05")
  266. }
  267. cont.ApplyTime = aTime.Time().Format("2006-01-02 15:04:05")
  268. if sn, ok := s.snsInfo[cont.SeasonID]; ok {
  269. cont.Stitle = sn.Title
  270. cont.Category = s.pgcCatToName(sn.Category)
  271. }
  272. items = append(items, cont)
  273. }
  274. if err = rows.Err(); err != nil {
  275. log.Error("rows Err %v", err)
  276. return
  277. }
  278. return
  279. }
  280. func (s *Service) transReqT(req *model.TransReq) (db *gorm.DB, err error) {
  281. var (
  282. inSids, catSids []int64
  283. ok bool
  284. )
  285. db = s.DB.Model(model.Content{}).Where("apply_time != '0000-00-00 00:00:00'").Where("is_deleted = 0")
  286. if req.Status != "" { // status
  287. switch req.Status {
  288. case "0":
  289. db = db.Where("transcoded = 0")
  290. case "1":
  291. db = db.Where("transcoded = 1")
  292. case "2":
  293. db = db.Where("transcoded = 2")
  294. }
  295. }
  296. if req.EpID != 0 {
  297. db = db.Where("epid = ?", req.EpID)
  298. }
  299. if req.SeasonID != 0 {
  300. db = db.Where("season_id = ?", req.SeasonID)
  301. }
  302. if req.Title != "" {
  303. if inSids = s.titleMatch(req.Title); len(inSids) == 0 {
  304. log.Warn("titleMatch %s, Empty", req.Title)
  305. err = ecode.NothingFound
  306. return
  307. }
  308. }
  309. if req.Category != 0 {
  310. if catSids, ok = s.snsCats[req.Category]; !ok || len(catSids) == 0 {
  311. log.Warn("snsCats %d, Empty", req.Category)
  312. err = ecode.NothingFound
  313. return
  314. }
  315. if len(inSids) > 0 { // title match have sids
  316. if inSids = intersect(catSids, inSids); len(inSids) == 0 {
  317. err = ecode.NothingFound
  318. return
  319. }
  320. } else {
  321. inSids = catSids
  322. }
  323. }
  324. if len(inSids) > 0 {
  325. db = db.Where(fmt.Sprintf("season_id IN (%s)", xstr.JoinInts(inSids)))
  326. }
  327. return
  328. }
  329. func postReqT(req *model.TransReq, db *gorm.DB) (newDb *gorm.DB) {
  330. if req.Order == 2 { // order
  331. newDb = db.Order("apply_time ASC")
  332. } else {
  333. newDb = db.Order("apply_time DESC")
  334. }
  335. newDb = newDb.Offset((req.Pn - 1) * _pagesize).Limit(_pagesize)
  336. return
  337. }
  338. func intersect(big, small []int64) (out []int64) {
  339. var amap = make(map[int64]int, len(big))
  340. for _, v := range big {
  341. amap[v] = 1
  342. }
  343. for _, v := range small {
  344. if _, ok := amap[v]; ok {
  345. out = append(out, v)
  346. }
  347. }
  348. return
  349. }