service.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/growup/conf"
  7. "go-common/app/job/main/growup/dao"
  8. "go-common/app/job/main/growup/dao/charge"
  9. "go-common/app/job/main/growup/dao/dataplatform"
  10. "go-common/app/job/main/growup/dao/email"
  11. "go-common/app/job/main/growup/dao/income"
  12. "go-common/app/job/main/growup/dao/tag"
  13. "go-common/app/job/main/growup/service/ctrl"
  14. "go-common/library/log"
  15. "go-common/library/queue/databus"
  16. )
  17. // Service struct
  18. type Service struct {
  19. conf *conf.Config
  20. dao *dao.Dao
  21. email *email.Dao
  22. dp *dataplatform.Dao
  23. tag *tag.Dao
  24. income *income.Dao
  25. charge *charge.Dao
  26. // databus sub
  27. arcSub *databus.Databus
  28. }
  29. // New fn
  30. func New(c *conf.Config, executor ctrl.Executor) (s *Service) {
  31. s = &Service{
  32. conf: c,
  33. dao: dao.New(c),
  34. email: email.New(c),
  35. dp: dataplatform.New(c),
  36. tag: tag.New(c),
  37. income: income.New(c),
  38. charge: charge.New(c),
  39. arcSub: databus.New(c.ArchiveSub),
  40. }
  41. // init task status service
  42. taskSvr = &taskService{
  43. dao: s.dao,
  44. dp: s.dp,
  45. }
  46. log.Info("service start")
  47. executor.Submit(
  48. s.checkExpired,
  49. s.updateDateBlacklist,
  50. s.sendMail,
  51. s.updateCheat,
  52. s.mailTagIncome,
  53. s.calCreativeActivity,
  54. s.updateUpInfoVideo,
  55. s.creativeBudget,
  56. s.checkAvBreach,
  57. s.checkUpPunish,
  58. s.archiveConsume,
  59. s.syncIncomeBubbleMetaTask,
  60. s.snapshotIncomeBubbleTask,
  61. )
  62. return s
  63. }
  64. // check the account state of video up
  65. func (s *Service) checkExpired(ctx context.Context) {
  66. for {
  67. // up_info_video
  68. s.expiredCheck(0, 5, 0, "up_info_video") // check all expired from quit to default
  69. s.expiredCheck(0, 7, 3, "up_info_video") // check all expired from forbidden to signed
  70. s.expiredCheck(1, 4, 0, "up_info_video") // check ugc expired from reject to default
  71. s.expiredCheck(2, 4, 1, "up_info_video") // check pgc expired from reject to pre-audit
  72. // up_info_column
  73. s.expiredCheck(0, 5, 0, "up_info_column") // check all expired from quit to default
  74. s.expiredCheck(0, 7, 3, "up_info_column") // check all expired from forbidden to signed
  75. s.expiredCheck(1, 4, 0, "up_info_column") // check ugc expired from reject to default
  76. s.expiredCheck(2, 4, 1, "up_info_column") // check pgc expired from reject to pre-audit
  77. time.Sleep(NextDay(0, 0, 0))
  78. }
  79. }
  80. // updateDateBlacklist update blacklist at 17:00 every day
  81. func (s *Service) updateDateBlacklist(ctx context.Context) {
  82. for {
  83. time.Sleep(NextDay(17, 0, 0))
  84. msg := ""
  85. log.Info("Exec growup-job updateDateBlacklist begin:%v", time.Now().Format("2006-01-02 15:04:05"))
  86. err := s.UpdateBlacklist(context.TODO())
  87. if err != nil {
  88. msg = fmt.Sprintf("UpdateBlacklist error(%v)", err)
  89. } else {
  90. msg = "Success"
  91. }
  92. err = s.email.SendMail(time.Now(), msg, "创作激励同步黑名单%d年%d月%d日", "shaozhenyu@bilibili.com", "gaopeng@bilibili.com", "limengqing@bilibili.com")
  93. if err != nil {
  94. log.Error("s.email.SendMail error(%v)", err)
  95. }
  96. log.Info("Exec growup-job updateDateBlacklist end:%v", time.Now().Format("2006-01-02 15:04:05"))
  97. }
  98. }
  99. func (s *Service) updateUpInfoVideo(ctx context.Context) {
  100. var mailReceivers []string
  101. for _, v := range s.conf.Mail.Send {
  102. if v.Type == 4 {
  103. mailReceivers = v.Addr
  104. break
  105. }
  106. }
  107. for {
  108. time.Sleep(NextDay(11, 0, 0))
  109. log.Info("Exec growup-job updateUpInfoVideo begin:%v", time.Now().Format("2006-01-02 15:04:05"))
  110. msg := ""
  111. err := s.UpdateUpInfo(context.TODO())
  112. if err != nil {
  113. msg = fmt.Sprintf("s.UpdateUpInfo error(%v)", err)
  114. } else {
  115. msg, err = s.autoExamination(context.TODO())
  116. if err != nil {
  117. msg = fmt.Sprintf("s.autoExamination error(%v)", err)
  118. }
  119. }
  120. err = s.email.SendMail(time.Now(), msg, "创作激励自动过审%d年%d月%d日", mailReceivers...)
  121. if err != nil {
  122. log.Error("s.email.SendMail error(%v)", err)
  123. }
  124. log.Info("Exec growup-job updateUpInfoVideo end:%v", time.Now().Format("2006-01-02 15:04:05"))
  125. }
  126. }
  127. func (s *Service) sendMail(ctx context.Context) {
  128. for {
  129. time.Sleep(NextDay(12, 0, 0))
  130. log.Info("Exec growup-job sendMail begin:%v", time.Now().Format("2006-01-02 15:04:05"))
  131. s.CombineMails()
  132. log.Info("Exec growup-job sendMail end:%v", time.Now().Format("2006-01-02 15:04:05"))
  133. }
  134. }
  135. func (s *Service) updateCheat(ctx context.Context) {
  136. for {
  137. time.Sleep(NextDay(13, 0, 0))
  138. log.Info("Exec growup-job updateCheat begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  139. t := time.Now().Add(-24 * time.Hour)
  140. s.CheatStatistics(context.TODO(), t)
  141. log.Info("Exec growup-job updateCheat end: %v", time.Now().Format("2006-01-02 15:04:05"))
  142. }
  143. }
  144. func (s *Service) mailTagIncome(ctx context.Context) {
  145. for {
  146. time.Sleep(NextDay(19, 30, 0))
  147. log.Info("Exec growup-job mailTagIncome begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  148. t := time.Now().Add(-24 * time.Hour)
  149. date := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  150. s.sendTagIncome(context.TODO(), date)
  151. log.Info("End growup-job mailTagIncome: %v", time.Now().Format("2006-01-02 15:04:05"))
  152. }
  153. }
  154. func (s *Service) calCreativeActivity(ctx context.Context) {
  155. for {
  156. time.Sleep(NextDay(15, 30, 0))
  157. msg := ""
  158. log.Info("Exec growup-job calCreativeActivity begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  159. t := time.Now().Add(-24 * time.Hour)
  160. date := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  161. err := s.CreativeActivity(context.TODO(), date)
  162. if err != nil {
  163. msg = fmt.Sprintf("calCreativeActivity error(%v)", err)
  164. } else {
  165. msg = "Success"
  166. }
  167. err = s.email.SendMail(date, msg, "创作激励活动每日计算%d年%d月%d日", "shaozhenyu@bilibili.com", "gaopeng@bilibili.com", "limengqing@bilibili.com")
  168. if err != nil {
  169. log.Error("s.email.SendMail error(%v)", err)
  170. }
  171. log.Info("End growup-job calCreativeActivity: %v", time.Now().Format("2006-01-02 15:04:05"))
  172. }
  173. }
  174. func (s *Service) creativeBudget(ctx context.Context) {
  175. for {
  176. time.Sleep(NextDay(20, 0, 0))
  177. msg := ""
  178. log.Info("Exec growup-job creativeBudget begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  179. t := time.Now().Add(-24 * time.Hour)
  180. date := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  181. err := s.CreativeBudget(context.TODO(), date)
  182. if err != nil {
  183. msg = fmt.Sprintf("creativeBudget error(%v)", err)
  184. } else {
  185. msg = "Success"
  186. }
  187. err = s.email.SendMail(date, msg, "创作激励预算每日计算%d年%d月%d日", "shaozhenyu@bilibili.com", "gaopeng@bilibili.com", "limengqing@bilibili.com")
  188. if err != nil {
  189. log.Error("s.email.SendMail error(%v)", err)
  190. }
  191. log.Info("End growup-job creativeBudget: %v", time.Now().Format("2006-01-02 15:04:05"))
  192. }
  193. }
  194. func (s *Service) checkAvBreach(ctx context.Context) {
  195. var mailReceivers []string
  196. for _, v := range s.conf.Mail.Send {
  197. if v.Type == 4 {
  198. mailReceivers = v.Addr
  199. break
  200. }
  201. }
  202. for {
  203. time.Sleep(NextDay(0, 0, 0))
  204. log.Info("Exec growup-job check and auto breach av begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  205. t := time.Now().Add(-48 * time.Hour)
  206. date := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  207. msg, err := s.autoAvBreach(context.TODO(), date.Format(_layout))
  208. if err != nil {
  209. msg = fmt.Sprintf("autoAvBreach error(%v)", err)
  210. }
  211. err = s.email.SendMail(date, msg, "创作激励自制转转载每日扣除%d年%d月%d日", mailReceivers...)
  212. if err != nil {
  213. log.Error("s.email.SendMail error(%v)", err)
  214. }
  215. log.Info("End growup-job autoAvBreach: %v", time.Now().Format("2006-01-02 15:04:05"))
  216. }
  217. }
  218. func (s *Service) checkUpPunish(ctx context.Context) {
  219. var mailReceivers []string
  220. for _, v := range s.conf.Mail.Send {
  221. if v.Type == 4 {
  222. mailReceivers = v.Addr
  223. break
  224. }
  225. }
  226. for {
  227. time.Sleep(NextDay(10, 0, 0))
  228. if time.Now().Weekday() != time.Monday {
  229. continue
  230. }
  231. log.Info("Exec growup-job checkUpPunish begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  232. msg, err := s.autoUpPunish(context.TODO())
  233. if err != nil {
  234. msg = fmt.Sprintf("s.autoUpPunish error(%v)", err)
  235. }
  236. err = s.email.SendMail(time.Now(), msg, "创作激励自制转转载处罚%d年%d月%d日", mailReceivers...)
  237. if err != nil {
  238. log.Error("s.email.SendMail error(%v)", err)
  239. }
  240. log.Info("End growup-job checkUpPunish: %v", time.Now().Format("2006-01-02 15:04:05"))
  241. }
  242. }
  243. func (s *Service) syncIncomeBubbleMetaTask(ctx context.Context) {
  244. for {
  245. msg := "ok"
  246. time.Sleep(NextDay(12, 0, 0))
  247. log.Info("Exec growup-job syncIncomeBubbleMetaTask begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  248. t := time.Now().Add(-24 * time.Hour)
  249. date := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  250. err := s.SyncIncomeBubbleMetaTask(context.TODO(), date)
  251. if err != nil {
  252. msg = fmt.Sprintf("s.syncIncomeBubbleMetaTask error(%v)", err)
  253. }
  254. err = s.email.SendMail(time.Now(), msg, "创作激励同步动态转发抽奖%d年%d月%d日", "shaozhenyu@bilibili.com", "gaopeng@bilibili.com", "limengqing@bilibili.com")
  255. if err != nil {
  256. log.Error("s.email.SendMail error(%v)", err)
  257. }
  258. log.Info("End growup-job syncIncomeBubbleMetaTask: %v", time.Now().Format("2006-01-02 15:04:05"))
  259. }
  260. }
  261. func (s *Service) snapshotIncomeBubbleTask(ctx context.Context) {
  262. for {
  263. msg := "ok"
  264. time.Sleep(NextDay(19, 0, 0))
  265. log.Info("Exec growup-job snapshotIncomeBubbleTask begin: %v", time.Now().Format("2006-01-02 15:04:05"))
  266. t := time.Now().Add(-24 * time.Hour)
  267. date := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
  268. err := s.SnapshotBubbleIncomeTask(context.TODO(), date)
  269. if err != nil {
  270. msg = fmt.Sprintf("s.snapshotIncomeBubbleTask error(%v)", err)
  271. }
  272. err = s.email.SendMail(time.Now(), msg, "创作激励动态转发抽奖收入同步%d年%d月%d日", "shaozhenyu@bilibili.com", "gaopeng@bilibili.com", "limengqing@bilibili.com")
  273. if err != nil {
  274. log.Error("s.email.SendMail error(%v)", err)
  275. }
  276. log.Info("End growup-job snapshotIncomeBubbleTask: %v", time.Now().Format("2006-01-02 15:04:05"))
  277. }
  278. }
  279. // NextDay next day x hours
  280. func NextDay(hour, min, second int) time.Duration {
  281. n := time.Now()
  282. d := time.Date(n.Year(), n.Month(), n.Day(), hour, min, second, 0, n.Location())
  283. for !d.After(n) {
  284. d = d.AddDate(0, 0, 1)
  285. }
  286. return time.Until(d)
  287. }
  288. // Close close the service
  289. func (s *Service) Close() {
  290. s.dao.Close()
  291. s.tag.Close()
  292. s.income.Close()
  293. s.charge.Close()
  294. }
  295. // Ping check server ok.
  296. func (s *Service) Ping(c context.Context) (err error) {
  297. return s.dao.Ping(c)
  298. }