comein.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/models/yb"
  7. "hongze/hongze_task/services/alarm_msg"
  8. "hongze/hongze_task/services/comein"
  9. "hongze/hongze_task/utils"
  10. "strings"
  11. "time"
  12. )
  13. // SyncComeinMeeting 同步进门会议参会人员数据
  14. func SyncComeinMeeting(cont context.Context) (err error) {
  15. errMsgList := make([]string, 0)
  16. defer func() {
  17. if len(errMsgList) > 0 {
  18. //for _, v := range errMsgList {
  19. // fmt.Println(v)
  20. //}
  21. go alarm_msg.SendAlarmMsg("同步进门会议参会人员数据失败:"+strings.Join(errMsgList, "/n"), 3)
  22. }
  23. }()
  24. nowHour := time.Now().Hour()
  25. //因为 0点 和 19点 是在更新指标,就不同步参会人数了
  26. if nowHour == 0 || nowHour == 19 {
  27. return
  28. }
  29. //默认往前面推 1 小时
  30. beforeHour := time.Duration(1)
  31. //因为 0点 和 19点 是在更新指标,所以后面的两个时间点同步数据时,需要往前面2小时往后拉取
  32. if nowHour == 1 || nowHour == 20 {
  33. beforeHour = time.Duration(2) //往前面推 2 小时
  34. }
  35. startTimeInt := time.Now().Add(-3600*beforeHour*time.Second-600).Unix() * 1000 //开始时间
  36. endTimeInt := time.Now().Unix() * 1000 //结束时间
  37. roadshowData, err := comein.GetRoadshowDataList(startTimeInt, endTimeInt)
  38. if err != nil {
  39. errMsgList = append(errMsgList, fmt.Sprintf("获取进门路演到会信息失败,%s,err:%s", time.Now().Format(utils.FormatDate), err.Error()))
  40. return
  41. }
  42. roadshowDataList := roadshowData.Data
  43. comeinEventMap := make(map[int]*yb.ComeinEvent)
  44. // 开始同步
  45. companyProductMap := make(map[string]*models.CompanyProduct)
  46. companyMap := make(map[string]*models.Company)
  47. companyIdProductIdMap := make(map[int]int)
  48. // 参会人
  49. for _, v := range roadshowDataList {
  50. //只记录普通成员(也就是参会人)
  51. if v.UserIdentity != 4 { //参会者身份,1:主讲人 2:主持人 3:嘉宾 4:普通参会者 5:联席主讲人 6:会议助理
  52. continue
  53. }
  54. if v.UserPhone == "" { //如果手机号为空,那么就不处理了,进入下一个循环
  55. continue
  56. }
  57. tmpComeinEvent, ok := comeinEventMap[int(v.RoadshowID)]
  58. if !ok {
  59. // 检测会议是否存在,不存在则创建会议
  60. tmpComeinEventInfo, tmpErr := yb.GetComeinEventByRoadshowId(int(v.RoadshowID))
  61. if tmpErr != nil {
  62. if tmpErr.Error() == utils.ErrNoRow() {
  63. //会议信息
  64. tmpComeinEventInfo = &yb.ComeinEvent{
  65. //ComeinEventId: 0,
  66. RoadshowId: int(v.RoadshowID),
  67. //ConferenceId: comeinDataInfo.ConferenceType,
  68. Title: v.RoadshowTitle,
  69. StartTime: time.Unix(v.RoadshowBeginTime/1000, 0),
  70. EndTime: time.Unix(v.RoadshowEndTime/1000, 0),
  71. People: 0,
  72. CreateTime: time.Now(),
  73. }
  74. tmpErr2 := yb.AddComeinEvent(tmpComeinEventInfo)
  75. if tmpErr2 != nil {
  76. //入库失败
  77. errMsgList = append(errMsgList, fmt.Sprintf("路演id:%d,路演标题:%s,会议入库失败,err:%s", v.RoadshowID, v.RoadshowTitle, err.Error()))
  78. continue
  79. }
  80. } else {
  81. //数据异常,sql异常
  82. errMsgList = append(errMsgList, fmt.Sprintf("路演id:%d,路演标题:%s,查找会议失败,err:%s", v.RoadshowID, v.RoadshowTitle, err.Error()))
  83. continue
  84. }
  85. }
  86. comeinEventMap[tmpComeinEventInfo.RoadshowId] = tmpComeinEventInfo
  87. tmpComeinEvent = tmpComeinEventInfo
  88. }
  89. //校验该记录是否已经入库,如果已经入库,那么就不处理了,进入下一个循环
  90. tmpComeinEventUser, tmpErr := yb.GetComeinEventUserByComeinDataId(int(v.ID))
  91. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  92. errMsgList = append(errMsgList, fmt.Sprintf("路演id:%d,路演标题:%s,用户手机号:%s,会议用户入库失败,err:%s", v.RoadshowID, v.RoadshowTitle, v.UserPhone, err.Error()))
  93. continue
  94. }
  95. if tmpComeinEventUser == nil {
  96. mobile := v.UserPhone
  97. countryCode := strings.Replace(v.AreaCode, "+", "", -1)
  98. var companyProductInfo *models.CompanyProduct
  99. var companyInfo *models.Company
  100. var wxUserInfo *models.WxUser
  101. productIdList := []int{1, 2}
  102. if mobile != "" {
  103. wxUserInfo, _ = models.GetWxUserByMobileCountryCode(mobile, countryCode)
  104. if wxUserInfo != nil && wxUserInfo.CompanyId != 1 {
  105. //获取客户产品信息
  106. productId, ok := companyIdProductIdMap[wxUserInfo.CompanyId]
  107. if ok { //已经查询过
  108. if productId > 0 {
  109. // 获取客户产品信息
  110. key := fmt.Sprint(wxUserInfo.CompanyId, "_", productId)
  111. if tmpCompanyProductInfo, ok := companyProductMap[key]; ok {
  112. companyProductInfo = tmpCompanyProductInfo
  113. }
  114. }
  115. } else {
  116. for _, productId := range productIdList {
  117. key := fmt.Sprint(wxUserInfo.CompanyId, "_", productId)
  118. companyProductInfo, _ = models.GetCompanyProduct(wxUserInfo.CompanyId, productId)
  119. if companyProductInfo != nil {
  120. companyProductMap[key] = companyProductInfo
  121. companyIdProductIdMap[wxUserInfo.CompanyId] = productId
  122. //退出产品查询
  123. break
  124. }
  125. }
  126. if companyProductInfo == nil {
  127. companyIdProductIdMap[wxUserInfo.CompanyId] = 0
  128. }
  129. }
  130. // 获取客户信息
  131. key := fmt.Sprint(wxUserInfo.CompanyId)
  132. if tmpCompanyInfo, ok := companyMap[key]; ok {
  133. companyInfo = tmpCompanyInfo
  134. } else {
  135. companyInfo, _ = models.GetCompanyById(wxUserInfo.CompanyId)
  136. if companyInfo != nil {
  137. companyMap[key] = companyInfo
  138. }
  139. }
  140. }
  141. }
  142. name := v.UserName
  143. comeinEventUserInfo := &yb.ComeinEventUser{
  144. //ComeinUserId int `orm:"column(comein_user_id);pk" description:"自增id"`
  145. ComeinEventId: tmpComeinEvent.ComeinEventId,
  146. ComeinDataId: int(v.ID),
  147. UserId: 0,
  148. Mobile: mobile,
  149. Email: v.Email,
  150. Name: name,
  151. FirstWatchTime: time.Unix(v.FirstWatchTime/1000, 0),
  152. LastWatchTime: time.Unix(v.LastWatchTime/1000, 0),
  153. JoinTime: int(v.JoinTime),
  154. AuthInfo: v.AuthInfo,
  155. JoinType: int(v.JoinType),
  156. DataType: int(v.DataType),
  157. //RegisterTime: time.Time{},
  158. ViewTotal: 0,
  159. //LastViewTime: time.Time{},
  160. CompanyId: 0,
  161. ProductId: 0,
  162. CompanyName: v.Company,
  163. Occupation: v.Occupation,
  164. Status: "",
  165. SellerId: 0,
  166. SellerName: "",
  167. CompanyViewTotal: 0,
  168. CompanyRoadTotal: 0,
  169. CreateTime: time.Now(),
  170. }
  171. //这个时候是系统用户了,美滋滋
  172. if companyProductInfo != nil {
  173. comeinEventUserInfo.RegisterTime = wxUserInfo.RegisterTime
  174. //models.
  175. userViewStatisticsInfo, _ := models.GetUserLastViewStatisticsByMobile(mobile) //用户阅读信息
  176. if userViewStatisticsInfo != nil {
  177. comeinEventUserInfo.ViewTotal = userViewStatisticsInfo.Total
  178. comeinEventUserInfo.LastViewTime = userViewStatisticsInfo.LastViewTime
  179. }
  180. comeinEventUserInfo.UserId = int(wxUserInfo.UserId)
  181. comeinEventUserInfo.Name = wxUserInfo.RealName
  182. comeinEventUserInfo.CompanyId = companyProductInfo.CompanyId
  183. comeinEventUserInfo.ProductId = companyProductInfo.ProductId
  184. comeinEventUserInfo.CompanyName = companyInfo.CompanyName
  185. comeinEventUserInfo.Status = companyProductInfo.Status
  186. comeinEventUserInfo.SellerId = companyProductInfo.SellerId
  187. comeinEventUserInfo.SellerName = companyProductInfo.SellerName
  188. comeinEventUserInfo.CompanyViewTotal = companyProductInfo.ViewTotal
  189. comeinEventUserInfo.CompanyRoadTotal = companyProductInfo.RoadShowTotal
  190. }
  191. yb.AddComeinEventUser(comeinEventUserInfo)
  192. tmpComeinEvent.People = tmpComeinEvent.People + 1
  193. }
  194. }
  195. for _, v := range comeinEventMap {
  196. v.Update([]string{"People"})
  197. }
  198. return
  199. }