mcn.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/mcn/model"
  6. "go-common/library/log"
  7. "github.com/pkg/errors"
  8. )
  9. // UpMcnSignStateCron .
  10. func (s *Service) UpMcnSignStateCron() {
  11. defer func() {
  12. if r := recover(); r != nil {
  13. r = errors.WithStack(r.(error))
  14. log.Error("recover panic error(%+v)", r)
  15. }
  16. }()
  17. var (
  18. err error
  19. c = context.TODO()
  20. now = time.Now()
  21. nowDate = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  22. mss []*model.MCNSignInfo
  23. )
  24. if mss, err = s.dao.McnSigns(c); err != nil {
  25. log.Error("s.dao.McnSigns error(%+v)", err)
  26. return
  27. }
  28. if len(mss) == 0 {
  29. log.Warn("mcn sign data is empty!")
  30. return
  31. }
  32. for _, v := range mss {
  33. var state int8
  34. switch {
  35. case v.State.NotDealState():
  36. continue
  37. case nowDate > v.EndDate.Time().Unix() && nowDate-v.EndDate.Time().Unix() <= model.ThirtyDayUnixTime && v.State != model.MCNSignStateOnCooling:
  38. state = int8(model.MCNSignStateOnCooling)
  39. case nowDate > v.EndDate.Time().Unix() && nowDate-v.EndDate.Time().Unix() > model.ThirtyDayUnixTime:
  40. state = int8(model.MCNSignStateOnExpire)
  41. case nowDate < v.BeginDate.Time().Unix() && v.State != model.MCNSignStateOnPreOpen:
  42. state = int8(model.MCNSignStateOnPreOpen)
  43. case v.BeginDate.Time().Unix() <= nowDate && nowDate <= v.EndDate.Time().Unix() && v.State != model.MCNSignStateOnSign && v.State == model.MCNSignStateOnPreOpen:
  44. state = int8(model.MCNSignStateOnSign)
  45. default:
  46. continue
  47. }
  48. if _, err = s.dao.UpMcnSignStateOP(c, v.SignID, state); err != nil {
  49. log.Error("s.dao.UpMcnSignStateOP(%d,%d) error(%+v)", v.SignID, state, err)
  50. continue
  51. }
  52. if err = s.dao.DelMcnSignCache(c, v.McnMid); err != nil {
  53. log.Error("s.dao.DelMcnSignCache(%d) error(%+v)", v.McnMid, err)
  54. continue
  55. }
  56. log.Info("signID(%d) change old state(%d) to new state(%d)", v.SignID, v.State, state)
  57. }
  58. }
  59. // UpExpirePayCron .
  60. func (s *Service) UpExpirePayCron() {
  61. defer func() {
  62. if r := recover(); r != nil {
  63. r = errors.WithStack(r.(error))
  64. log.Error("recover panic error(%+v)", r)
  65. }
  66. }()
  67. var (
  68. err error
  69. c = context.TODO()
  70. sps []*model.SignPayInfo
  71. )
  72. if sps, err = s.dao.McnSignPayWarns(c); err != nil {
  73. log.Error("s.dao.McnSignPayWarns error(%+v)", err)
  74. return
  75. }
  76. if len(sps) == 0 {
  77. log.Warn("mcn sign pay date is empty!")
  78. return
  79. }
  80. ms := make(map[int64]struct{})
  81. for _, v := range sps {
  82. ms[v.SignID] = struct{}{}
  83. }
  84. for signID := range ms {
  85. if _, err = s.dao.UpMcnSignPayExpOP(c, signID); err != nil {
  86. log.Error("s.dao.UpMcnSignPayExpOP(%d) error(%+v)", signID, err)
  87. continue
  88. }
  89. log.Info("sign_id(%d) change pay data warn state to 2", signID)
  90. }
  91. }