service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. "go-common/app/job/main/ugcpay/conf"
  7. "go-common/app/job/main/ugcpay/dao"
  8. "go-common/app/job/main/ugcpay/service/pay"
  9. "go-common/library/queue/databus"
  10. "github.com/robfig/cron"
  11. )
  12. var (
  13. ctx = context.Background()
  14. )
  15. // Service struct
  16. type Service struct {
  17. c *conf.Config
  18. dao *dao.Dao
  19. binlogMQ *databus.Databus
  20. elecBinlogMQ *databus.Databus
  21. cron *cron.Cron
  22. pay *pay.Pay
  23. taskLog *taskLog
  24. }
  25. // New init
  26. func New(c *conf.Config) (s *Service) {
  27. s = &Service{
  28. c: c,
  29. dao: dao.New(c),
  30. binlogMQ: databus.New(c.BinlogMQ),
  31. elecBinlogMQ: databus.New(c.ElecBinlogMQ),
  32. cron: cron.New(),
  33. pay: &pay.Pay{
  34. ID: conf.Conf.Biz.Pay.ID,
  35. Token: conf.Conf.Biz.Pay.Token,
  36. RechargeShellNotifyURL: conf.Conf.Biz.Pay.RechargeCallbackURL,
  37. },
  38. }
  39. s.taskLog = &taskLog{
  40. d: s.dao,
  41. }
  42. // 创建日账单任务
  43. taskBillDaily := &taskBillDaily{
  44. dao: s.dao,
  45. pay: s.pay,
  46. rnd: rand.New(rand.NewSource(time.Now().Unix())),
  47. dayOffset: conf.Conf.Biz.Task.DailyBillOffset,
  48. namePrefix: conf.Conf.Biz.Task.DailyBillPrefix,
  49. tl: s.taskLog,
  50. }
  51. if err := s.cron.AddFunc(conf.Conf.Biz.Cron.TaskDailyBill, s.wrapDisProc(taskBillDaily)); err != nil {
  52. panic(err)
  53. }
  54. // 创建up虚拟账户入账任务
  55. taskAccountUser := &taskAccountUser{
  56. dao: s.dao,
  57. taskPre: taskBillDaily, // 前置任务
  58. dayOffset: conf.Conf.Biz.Task.DailyBillOffset,
  59. namePrefix: conf.Conf.Biz.Task.AccountUserPrefix,
  60. tl: s.taskLog,
  61. }
  62. if err := s.cron.AddFunc(conf.Conf.Biz.Cron.TaskAccountUser, s.wrapDisProc(taskAccountUser)); err != nil {
  63. panic(err)
  64. }
  65. // 创建资金池入账任务
  66. taskAccountBiz := &taskAccountBiz{
  67. dao: s.dao,
  68. taskPre: taskBillDaily, // 前置任务
  69. dayOffset: conf.Conf.Biz.Task.DailyBillOffset,
  70. namePrefix: conf.Conf.Biz.Task.AccountBizPrefix,
  71. tl: s.taskLog,
  72. }
  73. if err := s.cron.AddFunc(conf.Conf.Biz.Cron.TaskAccountBiz, s.wrapDisProc(taskAccountBiz)); err != nil {
  74. panic(err)
  75. }
  76. // 创建月账单任务
  77. taskBillMonthly := &taskBillMonthly{
  78. dao: s.dao,
  79. rnd: rand.New(rand.NewSource(time.Now().Unix())),
  80. monthOffset: conf.Conf.Biz.Task.MonthBillOffset,
  81. namePrefix: conf.Conf.Biz.Task.MonthBillPrefix,
  82. tl: s.taskLog,
  83. }
  84. if err := s.cron.AddFunc(conf.Conf.Biz.Cron.TaskMonthlyBill, s.wrapDisProc(taskBillMonthly)); err != nil {
  85. panic(err)
  86. }
  87. // 创建转贝壳任务
  88. taskRechargeShell := &taskRechargeShell{
  89. dao: s.dao,
  90. pay: s.pay,
  91. rnd: rand.New(rand.NewSource(time.Now().Unix())),
  92. monthOffset: conf.Conf.Biz.Task.RechargeShellOffset,
  93. namePrefix: conf.Conf.Biz.Task.RechargeShellPrefix,
  94. tl: s.taskLog,
  95. }
  96. if err := s.cron.AddFunc(conf.Conf.Biz.Cron.TaskRechargeShell, s.wrapDisProc(taskRechargeShell)); err != nil {
  97. panic(err)
  98. }
  99. s.cron.Start()
  100. go s.binlogproc()
  101. go s.elecbinlogproc()
  102. // go s.repairOrderUser() 修复订单用
  103. return s
  104. }
  105. // Ping Service
  106. func (s *Service) Ping(c context.Context) (err error) {
  107. return s.dao.Ping(c)
  108. }
  109. // Close Service
  110. func (s *Service) Close() {
  111. s.dao.Close()
  112. }