user_label.go 8.7 KB

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