industry_fllow.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/cygx"
  7. "hongze/hz_crm_api/services/alarm_msg"
  8. "hongze/hz_crm_api/utils"
  9. "strconv"
  10. "time"
  11. )
  12. //func init() {
  13. // AddUserFllowCompanyLossToTryOut(12841)
  14. //}
  15. // 流失客户转试用给下面的用户自动添加产业关注
  16. func AddUserFllowCompanyLossToTryOut(companyId int) {
  17. var err error
  18. defer func() {
  19. if err != nil {
  20. fmt.Println(err)
  21. go alarm_msg.SendAlarmMsg("流失客户转试用给下面的用户自动添加产业关注。失败 ErrMsg:"+err.Error()+"companyId :"+strconv.Itoa(companyId), 2)
  22. }
  23. }()
  24. //获取机构下所有的用户
  25. userList, e := models.GetWxUserListCompanyId(companyId)
  26. if e != nil {
  27. err = errors.New("GetWxUserListByUserMobileHaveCompany" + e.Error())
  28. return
  29. }
  30. if len(userList) == 0 {
  31. return
  32. }
  33. var condition string
  34. var pars []interface{}
  35. //所有的产业
  36. industrNamelist, e := cygx.GetTopOneMonthArtReadNumIndustryAll(condition, pars)
  37. if e != nil {
  38. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  39. return
  40. }
  41. var userIds []int
  42. for _, v := range userList {
  43. userIds = append(userIds, v.UserId)
  44. }
  45. condition = " AND user_id IN (" + utils.GetOrmInReplace(len(userIds)) + ") "
  46. pars = append(pars, userIds)
  47. userFllowList, e := cygx.GetCygxIndustryFllowList(condition, pars, 0, 0)
  48. if e != nil {
  49. err = errors.New("GetCygxIndustryFllowList, Err: " + e.Error())
  50. return
  51. }
  52. mapUserFllow := make(map[string]bool) // 用户已经关注了某些产业
  53. for _, v := range userFllowList {
  54. mapUserFllow[fmt.Sprint("Uid_", v.UserId, "IndustrialManagementId_", v.IndustrialManagementId)] = true //用户ID与产业ID组成唯一索引Map
  55. }
  56. var numk int
  57. var industryFllowItems []*cygx.CygxIndustryFllow
  58. var Items []*cygx.CygxIndustryFllow
  59. for _, vUser := range userList {
  60. for _, vIndustry := range industrNamelist {
  61. if mapUserFllow[fmt.Sprint("Uid_", vUser.UserId, "IndustrialManagementId_", vIndustry.IndustrialManagementId)] {
  62. continue
  63. }
  64. item := new(cygx.CygxIndustryFllow)
  65. item.IndustrialManagementId = vIndustry.IndustrialManagementId
  66. item.UserId = vUser.UserId
  67. item.Email = vUser.Email
  68. item.Mobile = vUser.Mobile
  69. item.RealName = vUser.RealName
  70. item.CompanyId = vUser.CompanyId
  71. item.CompanyName = vUser.CompanyName
  72. item.Source = 3 // 通过程序或者管理员手动添加的关注
  73. item.Type = 1
  74. item.CreateTime = time.Now()
  75. item.ModifyTime = time.Now()
  76. Items = append(Items, item)
  77. }
  78. }
  79. //fmt.Println("Items", len(Items))
  80. //如果小于五千条就直接插入,大于五千条就批量插入
  81. if len(Items) < 5000 {
  82. e = cygx.AddCygxIndustryFllowMulti(Items)
  83. if e != nil {
  84. err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
  85. return
  86. }
  87. } else {
  88. for _, vUser := range Items {
  89. numk++
  90. item := new(cygx.CygxIndustryFllow)
  91. item.IndustrialManagementId = vUser.IndustrialManagementId
  92. item.UserId = vUser.UserId
  93. item.Email = vUser.Email
  94. item.Mobile = vUser.Mobile
  95. item.RealName = vUser.RealName
  96. item.CompanyId = vUser.CompanyId
  97. item.CompanyName = vUser.CompanyName
  98. item.Source = 3 // 通过程序或者管理员手动添加的关注
  99. item.Type = 1
  100. item.CreateTime = time.Now()
  101. item.ModifyTime = time.Now()
  102. industryFllowItems = append(industryFllowItems, item)
  103. //一次插入五千条
  104. if numk%5000 == 0 {
  105. e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
  106. if e != nil {
  107. err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
  108. return
  109. }
  110. industryFllowItems = make([]*cygx.CygxIndustryFllow, 0)
  111. numk = 0
  112. }
  113. }
  114. if len(industryFllowItems) > 0 {
  115. e = cygx.AddCygxIndustryFllowMulti(industryFllowItems)
  116. if e != nil {
  117. err = errors.New("AddCygxIndustryFllowMulti , Err: " + e.Error())
  118. return
  119. }
  120. }
  121. }
  122. //fmt.Println("AddUserFllowCompanyLossToTryOut end")
  123. return
  124. }