coupon.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package service
  2. import (
  3. "context"
  4. xlog "log"
  5. "time"
  6. "go-common/app/job/main/vip/model"
  7. comol "go-common/app/service/main/coupon/model"
  8. "go-common/library/log"
  9. )
  10. func (s *Service) couponnotifyproc() {
  11. defer func() {
  12. if x := recover(); x != nil {
  13. log.Error("couponnotifyproc panic(%v)", x)
  14. go s.couponnotifyproc()
  15. log.Info("couponnotifyproc recover")
  16. }
  17. }()
  18. for {
  19. f := <-s.notifycouponchan
  20. time.AfterFunc(2*time.Second, f)
  21. }
  22. }
  23. func (s *Service) couponnotify(f func()) {
  24. defer func() {
  25. if x := recover(); x != nil {
  26. log.Error("couponnotifyproc panic(%v)", x)
  27. }
  28. }()
  29. select {
  30. case s.notifycouponchan <- f:
  31. default:
  32. xlog.Panic("s.couponnotifyproc chan full!")
  33. }
  34. }
  35. // CouponNotify coupon notify.
  36. func (s *Service) CouponNotify(c context.Context, o *model.VipPayOrderNewMsg) (err error) {
  37. var (
  38. state int8
  39. retrytimes = 3
  40. )
  41. if o == nil {
  42. return
  43. }
  44. if o.Status == model.SUCCESS {
  45. state = comol.AllowanceUseSuccess
  46. } else {
  47. state = comol.AllowanceUseFaild
  48. }
  49. for i := 0; i < retrytimes; i++ {
  50. if err = s.couponRPC.CouponNotify(c, &comol.ArgNotify{
  51. Mid: o.Mid,
  52. OrderNo: o.OrderNo,
  53. State: state,
  54. }); err != nil {
  55. log.Error("rpc.CouponNotify(%+v) error(%+v)", o, err)
  56. continue
  57. }
  58. break
  59. }
  60. return
  61. }