service.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package charge
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/growup/conf"
  7. chargeD "go-common/app/job/main/growup/dao/charge"
  8. "go-common/app/job/main/growup/dao/email"
  9. "go-common/app/job/main/growup/service"
  10. "go-common/app/job/main/growup/service/ctrl"
  11. "go-common/library/log"
  12. )
  13. // Service struct
  14. type Service struct {
  15. conf *conf.Config
  16. dao *chargeD.Dao
  17. email *email.Dao
  18. }
  19. // New fn
  20. func New(c *conf.Config, executor ctrl.Executor) (s *Service) {
  21. s = &Service{
  22. conf: c,
  23. dao: chargeD.New(c),
  24. email: email.New(c),
  25. }
  26. log.Info("charge service start")
  27. executor.Submit(
  28. s.calDailyCreativeCharge,
  29. s.checkColumnDailyCharge,
  30. )
  31. return s
  32. }
  33. func (s *Service) calDailyCreativeCharge(ctx context.Context) {
  34. for {
  35. time.Sleep(service.NextDay(17, 0, 0))
  36. log.Info("calDailyCreativeCharge begin:%v", time.Now().Format("2006-01-02 15:04:05"))
  37. now := time.Now()
  38. date := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
  39. err := s.RunAndSendMail(context.TODO(), date)
  40. if err != nil {
  41. log.Error("s.RunAndSendMail error(%v)", err)
  42. }
  43. log.Info("calDailyCreativeCharge end:%v", time.Now().Format("2006-01-02 15:04:05"))
  44. }
  45. }
  46. func (s *Service) checkColumnDailyCharge(ctx context.Context) {
  47. for {
  48. time.Sleep(service.NextDay(13, 0, 0))
  49. msg := ""
  50. log.Info("checkColumnDailyCharge begin:%v", time.Now().Format("2006-01-02 15:04:05"))
  51. now := time.Now()
  52. date := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
  53. err := s.CheckTaskColumn(context.TODO(), date.Format(_layout))
  54. if err != nil {
  55. log.Error("s.CheckTaskColumn error(%v)", err)
  56. }
  57. if err != nil {
  58. msg = fmt.Sprintf("CheckTaskColumn error(%v)", err)
  59. } else {
  60. msg = "Success"
  61. }
  62. err = s.email.SendMail(date, msg, "创作激励专栏补贴数据%d年%d月%d日", "shaozhenyu@bilibili.com", "gaopeng@bilibili.com", "limengqing@bilibili.com")
  63. if err != nil {
  64. log.Error("s.email.SendMail error(%v)", err)
  65. }
  66. log.Info("checkColumnDailyCharge end:%v", time.Now().Format("2006-01-02 15:04:05"))
  67. }
  68. }
  69. // Ping check dao health.
  70. func (s *Service) Ping(c context.Context) (err error) {
  71. return s.dao.Ping(c)
  72. }