sign_update_tid_job.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package service
  2. import (
  3. "go-common/app/admin/main/up/util"
  4. "go-common/app/admin/main/up/util/mathutil"
  5. "go-common/app/job/main/up/model/signmodel"
  6. "go-common/app/job/main/up/model/upcrmmodel"
  7. "go-common/library/log"
  8. "time"
  9. )
  10. // UpdateUpTidJob 检查sign_up中的状态
  11. func (s *Service) UpdateUpTidJob(date time.Time) {
  12. log.Info("start run UpdateUpTidJob, date=%s", date)
  13. s.updateUpTidJob(date)
  14. log.Info("finish run UpdateUpTidJob, date=%s", date)
  15. }
  16. func (s *Service) updateUpTidJob(date time.Time) {
  17. // 获取所有mid信息
  18. var limit = 200
  19. var count = limit
  20. var err error
  21. var mids []int64
  22. var offset = 0
  23. for limit == count {
  24. var signList []*signmodel.SignUp
  25. if err = s.crmdb.GetDb().Offset(offset).Limit(limit).Find(&signList).Error; err != nil {
  26. log.Error("fail to get signs from sign ups, err=%v", err)
  27. return
  28. }
  29. count = len(signList)
  30. offset += count
  31. for _, v := range signList {
  32. mids = append(mids, v.Mid)
  33. }
  34. }
  35. mids = util.Unique(mids)
  36. // 从up_base_info中读取tid并更新
  37. // <tid, mid list>
  38. var tidMidMap = make(map[int64][]int64)
  39. for begin := 0; begin < len(mids); begin += limit {
  40. var end = mathutil.Min(begin+limit, len(mids))
  41. var baseInfoList []*upcrmmodel.UpBaseInfo
  42. if err = s.crmdb.GetDb().Select("mid, active_tid").Where("mid in (?)", mids[begin:end]).Limit(limit).Find(&baseInfoList).Error; err != nil {
  43. log.Error("fail to get signs from sign ups, err=%v", err)
  44. return
  45. }
  46. // 更新到sign表中
  47. for _, v := range baseInfoList {
  48. tidMidMap[v.ActiveTid] = append(tidMidMap[v.ActiveTid], v.Mid)
  49. }
  50. }
  51. // 更新到sign_up表中
  52. for k, v := range tidMidMap {
  53. if err = s.crmdb.GetDb().Table(signmodel.TableNameSignUp).Where("mid in (?)", v).Update("active_tid", k).Error; err != nil {
  54. log.Error("update sign up's active tid fail, err=%v", err)
  55. return
  56. }
  57. }
  58. }