sign_state_job.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package service
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "go-common/app/job/main/up/dao/upcrm"
  5. "go-common/app/job/main/up/model/signmodel"
  6. "go-common/app/service/main/upcredit/mathutil"
  7. "go-common/library/log"
  8. "time"
  9. )
  10. // change a lot state for sign up
  11. // state, 签约状态,正常或过期
  12. // due_wan, 签约状态即将过期
  13. // pay_expire_state,付费状态
  14. //CheckStateJob 检查sign_up中的状态
  15. func (s *Service) CheckStateJob(date time.Time) {
  16. log.Info("start run state job, date=%s", date)
  17. s.checkState(date)
  18. log.Info("finish run state job, date=%s", date)
  19. }
  20. func (s *Service) checkState(date time.Time) {
  21. // 更新签约状态:
  22. var crmdb = s.crmdb.GetDb()
  23. // 签约30天内到期
  24. var signExpireList []*signmodel.SignUp
  25. var signDueList []*signmodel.SignUp
  26. var limit, offset = 200, 0
  27. var count = limit
  28. var err error
  29. var signEndDate = date.AddDate(0, 0, 30)
  30. for count == limit {
  31. var items []*signmodel.SignUp
  32. // 把endDate在[-60, +30]范围内的用户全都找出来,过一遍
  33. err = crmdb.Offset(offset).Limit(limit).Where("end_date<? and end_date>?", signEndDate, date.AddDate(0, -4, 0)).Find(&items).Error
  34. if err != nil {
  35. log.Error("fail to get sign ups, err=%v", err)
  36. break
  37. }
  38. count = len(items)
  39. offset += count
  40. for _, v := range items {
  41. if v.EndDate.Time().Before(date) && v.State == signmodel.SignStateOnSign {
  42. // 过期
  43. signExpireList = append(signExpireList, v)
  44. } else if !(v.EndDate.Time().Before(date) || v.EndDate.Time().After(signEndDate)) && v.DueWarn <= signmodel.DueWarnNoWarn {
  45. // 快到期
  46. signDueList = append(signDueList, v)
  47. }
  48. }
  49. }
  50. var expireIDList []uint32
  51. for _, v := range signExpireList {
  52. expireIDList = append(expireIDList, v.ID)
  53. }
  54. err = updateListWithLimit(crmdb, "state", signmodel.SignStateExpire, expireIDList, limit)
  55. if err != nil {
  56. log.Error("fail to update sign state, err=%v", err)
  57. return
  58. }
  59. var dueIDList []uint32
  60. for _, v := range signDueList {
  61. dueIDList = append(dueIDList, v.ID)
  62. }
  63. err = updateListWithLimit(crmdb, "due_warn", signmodel.DueWarnWarn, dueIDList, limit)
  64. if err != nil {
  65. log.Error("fail to update sign due_warn, err=%v", err)
  66. return
  67. }
  68. // 付款7天内到期
  69. offset = 0
  70. count = limit
  71. var payDueDate = date.AddDate(0, 0, 7)
  72. var payExpireMap = map[uint32]int8{}
  73. for count == limit {
  74. var items []*signmodel.SignPay
  75. // 把endDate在[-max, +7]范围内的用户全都找出来,过一遍
  76. err = crmdb.Offset(offset).Limit(limit).Where("due_date<?", payDueDate).Find(&items).Error
  77. if err != nil {
  78. log.Error("fail to get sign ups, err=%v", err)
  79. break
  80. }
  81. count = len(items)
  82. offset += count
  83. for _, v := range items {
  84. // 如果有即将到期,则立即标记为即将到期
  85. if v.State == upcrm.PayStateUnpay {
  86. payExpireMap[v.SignID] = signmodel.PayExpireStateDue
  87. } else {
  88. // 如果没有即将到期的状态,则把其标记为未到期
  89. // 即:只有所有的付款都已标记完成,才会认为是未到期状态
  90. if payExpireMap[v.SignID] != signmodel.PayExpireStateDue {
  91. payExpireMap[v.SignID] = signmodel.PayExpireStateNormal
  92. }
  93. }
  94. }
  95. }
  96. // 更新到期状态
  97. var stateSignIDListMap = map[int8][]uint32{}
  98. for k, v := range payExpireMap {
  99. stateSignIDListMap[v] = append(stateSignIDListMap[v], k)
  100. }
  101. // 分区更新
  102. limit = 200
  103. for state, list := range stateSignIDListMap {
  104. err = updateListWithLimit(crmdb, "pay_expire_state", state, list, limit)
  105. if err != nil {
  106. log.Error("err update pay_expire_state, err=%v", err)
  107. return
  108. }
  109. }
  110. }
  111. func updateListWithLimit(crmdb *gorm.DB, field string, state int8, list []uint32, limit int) (err error) {
  112. for begin := 0; begin < len(list); begin += limit {
  113. var end = mathutil.Min(begin+limit, len(list))
  114. var needUpdate = list[begin:end]
  115. err = updateSignTable(crmdb, needUpdate, field, state)
  116. if err != nil {
  117. log.Error("fail to update state [%s], err=%v", field, err)
  118. return
  119. }
  120. }
  121. return
  122. }
  123. func updateSignTable(crmdb *gorm.DB, ids interface{}, field string, state int8) (err error) {
  124. err = crmdb.Table(signmodel.TableNameSignUp).Where("id in (?)", ids).Update(field, state).Error
  125. return
  126. }