user_label.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "time"
  10. )
  11. // 添加用户活动到会标签到Redis
  12. func ActivityUserLabelLogAdd(activityId int, userIdArr []int) (err error) {
  13. if len(userIdArr) == 0 {
  14. return
  15. }
  16. defer func() {
  17. if err != nil {
  18. fmt.Println(err)
  19. msg := fmt.Sprint("activityId:", activityId, "mobile:", userIdArr)
  20. go alarm_msg.SendAlarmMsg("添加用户活动到会标签到Redis,写入Redis队列消息失败:"+err.Error()+msg, 2)
  21. }
  22. }()
  23. var condition string
  24. var pars []interface{}
  25. condition = ` AND activity_id = ? `
  26. pars = append(pars, activityId)
  27. total, e := cygx.GetCygxIndustrialActivityGroupManagementCount(condition+" AND source = 1 ", pars)
  28. if e != nil {
  29. err = errors.New("GetCygxIndustrialActivityGroupManagementCount" + e.Error())
  30. return
  31. }
  32. if total == 0 {
  33. //没有关联产业的活动不做标签处理
  34. return
  35. }
  36. industrialList, e := cygx.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars)
  37. if e != nil {
  38. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  39. return
  40. }
  41. //如果有行产业归类就按照产业报告处理
  42. var topCond string
  43. var topPars []interface{}
  44. var industrialManagementIds []int
  45. for _, v := range industrialList {
  46. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  47. }
  48. idsLen := len(industrialManagementIds)
  49. if idsLen > 0 {
  50. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  51. topPars = append(topPars, industrialManagementIds)
  52. } else {
  53. return
  54. }
  55. industrNamelist, e := cygx.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  56. if e != nil {
  57. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  58. return
  59. }
  60. userList, e := models.GetWxUserByUserIds(userIdArr)
  61. if e != nil {
  62. err = errors.New("GetWxUserByOutboundMobiles" + e.Error())
  63. return
  64. }
  65. listActivityHistory, e := cygx.GetCygxUserLabelActivity(condition, pars, 0, 9999)
  66. if e != nil {
  67. err = errors.New("GetCygxUserLabelActivity" + e.Error())
  68. return
  69. }
  70. activityHistoryMap := make(map[int]bool)
  71. for _, v := range listActivityHistory {
  72. activityHistoryMap[v.UserId] = true
  73. }
  74. var items []*cygx.CygxUserLabelActivity
  75. for _, user := range userList {
  76. //已经提交到会的活动写入标签的不做二次添加处理
  77. if activityHistoryMap[int(user.UserId)] {
  78. continue
  79. }
  80. // SourceType 1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。
  81. log := &cygx.CygxUserLabelLogRedis{UserId: int(user.UserId), SourceId: activityId, SourceType: 3, CreateTime: time.Now()}
  82. if utils.Re == nil {
  83. err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log)
  84. if err != nil {
  85. fmt.Println("CygxUserLabelLogRedis LPush Err:" + err.Error())
  86. }
  87. }
  88. for _, industr := range industrNamelist {
  89. item := new(cygx.CygxUserLabelActivity)
  90. item.UserId = int(user.UserId)
  91. item.CompanyId = user.CompanyId
  92. item.RealName = user.RealName
  93. item.Mobile = user.Mobile
  94. item.Email = user.Email
  95. item.ActivityId = activityId
  96. item.IndustrialManagementId = industr.IndustrialManagementId
  97. item.Label = industr.IndustryName
  98. item.CreateTime = time.Now()
  99. item.ModifyTime = time.Now()
  100. items = append(items, item)
  101. }
  102. }
  103. if len(items) > 0 {
  104. _, err = cygx.AddCygxUserLabelActivityList(items)
  105. }
  106. return
  107. }
  108. // 添加用户活动到会标签到Redis
  109. func ActivitySpecialUserLabelLogAdd(activityId int, userIdArr []int) (err error) {
  110. if len(userIdArr) == 0 {
  111. return
  112. }
  113. defer func() {
  114. if err != nil {
  115. fmt.Println(err)
  116. msg := fmt.Sprint("activityId:", activityId, "mobile:", userIdArr)
  117. go alarm_msg.SendAlarmMsg("添加用户专项调研活动到会标签到Redis,写入Redis队列消息失败:"+err.Error()+msg, 2)
  118. }
  119. }()
  120. var condition string
  121. var pars []interface{}
  122. condition = ` AND activity_id = ? `
  123. pars = append(pars, activityId)
  124. total, e := cygx.GetCygxIndustrialActivityGroupManagementCount(condition+" AND source = 2 ", pars)
  125. if e != nil {
  126. err = errors.New("GetCygxIndustrialActivityGroupManagementCount" + e.Error())
  127. return
  128. }
  129. if total == 0 {
  130. //没有关联产业的活动不做标签处理
  131. return
  132. }
  133. industrialList, e := cygx.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 2 ", pars)
  134. if e != nil {
  135. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  136. return
  137. }
  138. //如果有行产业归类就按照产业报告处理
  139. var topCond string
  140. var topPars []interface{}
  141. var industrialManagementIds []int
  142. for _, v := range industrialList {
  143. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  144. }
  145. idsLen := len(industrialManagementIds)
  146. if idsLen > 0 {
  147. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  148. topPars = append(topPars, industrialManagementIds)
  149. } else {
  150. return
  151. }
  152. industrNamelist, e := cygx.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  153. if e != nil {
  154. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  155. return
  156. }
  157. userList, e := models.GetWxUserByUserIds(userIdArr)
  158. if e != nil {
  159. err = errors.New("GetWxUserByOutboundMobiles" + e.Error())
  160. return
  161. }
  162. listActivityHistory, e := cygx.GetCygxUserLabelActivitySpecial(condition, pars, 0, 9999)
  163. if e != nil {
  164. err = errors.New("GetCygxUserLabelActivity" + e.Error())
  165. return
  166. }
  167. activityHistoryMap := make(map[int]bool)
  168. for _, v := range listActivityHistory {
  169. activityHistoryMap[v.UserId] = true
  170. }
  171. var items []*cygx.CygxUserLabelActivitySpecial
  172. for _, user := range userList {
  173. //已经提交到会的活动写入标签的不做二次添加处理
  174. if activityHistoryMap[int(user.UserId)] {
  175. continue
  176. }
  177. // SourceType 1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。
  178. log := &cygx.CygxUserLabelLogRedis{UserId: int(user.UserId), SourceId: activityId, SourceType: 5, CreateTime: time.Now()}
  179. if utils.Re == nil {
  180. err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log)
  181. if err != nil {
  182. fmt.Println("CygxUserLabelLogRedis LPush Err:" + err.Error())
  183. }
  184. }
  185. for _, industr := range industrNamelist {
  186. item := new(cygx.CygxUserLabelActivitySpecial)
  187. item.UserId = int(user.UserId)
  188. item.CompanyId = user.CompanyId
  189. item.RealName = user.RealName
  190. item.Mobile = user.Mobile
  191. item.Email = user.Email
  192. item.ActivityId = activityId
  193. item.IndustrialManagementId = industr.IndustrialManagementId
  194. item.Label = industr.IndustryName
  195. item.CreateTime = time.Now()
  196. item.ModifyTime = time.Now()
  197. items = append(items, item)
  198. }
  199. }
  200. if len(items) > 0 {
  201. _, err = cygx.AddCygxUserLabelActivitySpecialList(items)
  202. }
  203. return
  204. }
  205. // 用户关注产业更新相关标签到Redis
  206. func IndustryFllowUserLabelLogAdd(industrialManagementId, count, uid int) (err error) {
  207. var isFllow int
  208. if count == 0 {
  209. isFllow = 1
  210. } else {
  211. isFllow = 0
  212. }
  213. defer func() {
  214. if err != nil {
  215. fmt.Println(err)
  216. msg := fmt.Sprint("industrialManagementId:", industrialManagementId, "isFllow:", isFllow, "userId:", uid)
  217. go alarm_msg.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  218. }
  219. }()
  220. log := &cygx.CygxUserLabelLogRedis{UserId: uid, SourceId: industrialManagementId, SourceType: 2, IsFllow: isFllow, CreateTime: time.Now()}
  221. if utils.Re == nil {
  222. err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log)
  223. if err != nil {
  224. fmt.Println("RecordNewLogs LPush Err:" + err.Error())
  225. }
  226. }
  227. return
  228. }