123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package services
- import (
- "context"
- "fmt"
- "hongze/hongze_task/models"
- "hongze/hongze_task/models/yb"
- "hongze/hongze_task/services/alarm_msg"
- "hongze/hongze_task/services/comein"
- "hongze/hongze_task/utils"
- "strings"
- "time"
- )
- // SyncComeinMeeting 同步进门会议参会人员数据
- func SyncComeinMeeting(cont context.Context) (err error) {
- errMsgList := make([]string, 0)
- defer func() {
- if len(errMsgList) > 0 {
- //for _, v := range errMsgList {
- // fmt.Println(v)
- //}
- go alarm_msg.SendAlarmMsg("同步进门会议参会人员数据失败:"+strings.Join(errMsgList, "/n"), 3)
- }
- }()
- nowHour := time.Now().Hour()
- //因为 0点 和 19点 是在更新指标,就不同步参会人数了
- if nowHour == 0 || nowHour == 19 {
- return
- }
- //默认往前面推 1 小时
- beforeHour := time.Duration(1)
- //因为 0点 和 19点 是在更新指标,所以后面的两个时间点同步数据时,需要往前面2小时往后拉取
- if nowHour == 1 || nowHour == 20 {
- beforeHour = time.Duration(2) //往前面推 2 小时
- }
- startTimeInt := time.Now().Add(-3600*beforeHour*time.Second-600).Unix() * 1000 //开始时间
- endTimeInt := time.Now().Unix() * 1000 //结束时间
- roadshowData, err := comein.GetRoadshowDataList(startTimeInt, endTimeInt)
- if err != nil {
- errMsgList = append(errMsgList, fmt.Sprintf("获取进门路演到会信息失败,%s,err:%s", time.Now().Format(utils.FormatDate), err.Error()))
- return
- }
- roadshowDataList := roadshowData.Data
- comeinEventMap := make(map[int]*yb.ComeinEvent)
- // 开始同步
- companyProductMap := make(map[string]*models.CompanyProduct)
- companyMap := make(map[string]*models.Company)
- companyIdProductIdMap := make(map[int]int)
- // 参会人
- for _, v := range roadshowDataList {
- //只记录普通成员(也就是参会人)
- if v.UserIdentity != 4 { //参会者身份,1:主讲人 2:主持人 3:嘉宾 4:普通参会者 5:联席主讲人 6:会议助理
- continue
- }
- if v.UserPhone == "" { //如果手机号为空,那么就不处理了,进入下一个循环
- continue
- }
- tmpComeinEvent, ok := comeinEventMap[int(v.RoadshowID)]
- if !ok {
- // 检测会议是否存在,不存在则创建会议
- tmpComeinEventInfo, tmpErr := yb.GetComeinEventByRoadshowId(int(v.RoadshowID))
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- //会议信息
- tmpComeinEventInfo = &yb.ComeinEvent{
- //ComeinEventId: 0,
- RoadshowId: int(v.RoadshowID),
- //ConferenceId: comeinDataInfo.ConferenceType,
- Title: v.RoadshowTitle,
- StartTime: time.Unix(v.RoadshowBeginTime/1000, 0),
- EndTime: time.Unix(v.RoadshowEndTime/1000, 0),
- People: 0,
- CreateTime: time.Now(),
- }
- tmpErr2 := yb.AddComeinEvent(tmpComeinEventInfo)
- if tmpErr2 != nil {
- //入库失败
- errMsgList = append(errMsgList, fmt.Sprintf("路演id:%d,路演标题:%s,会议入库失败,err:%s", v.RoadshowID, v.RoadshowTitle, err.Error()))
- continue
- }
- } else {
- //数据异常,sql异常
- errMsgList = append(errMsgList, fmt.Sprintf("路演id:%d,路演标题:%s,查找会议失败,err:%s", v.RoadshowID, v.RoadshowTitle, err.Error()))
- continue
- }
- }
- comeinEventMap[tmpComeinEventInfo.RoadshowId] = tmpComeinEventInfo
- tmpComeinEvent = tmpComeinEventInfo
- }
- //校验该记录是否已经入库,如果已经入库,那么就不处理了,进入下一个循环
- tmpComeinEventUser, tmpErr := yb.GetComeinEventUserByComeinDataId(int(v.ID))
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsgList = append(errMsgList, fmt.Sprintf("路演id:%d,路演标题:%s,用户手机号:%s,会议用户入库失败,err:%s", v.RoadshowID, v.RoadshowTitle, v.UserPhone, err.Error()))
- continue
- }
- if tmpComeinEventUser == nil {
- mobile := v.UserPhone
- countryCode := strings.Replace(v.AreaCode, "+", "", -1)
- var companyProductInfo *models.CompanyProduct
- var companyInfo *models.Company
- var wxUserInfo *models.WxUser
- productIdList := []int{1, 2}
- if mobile != "" {
- wxUserInfo, _ = models.GetWxUserByMobileCountryCode(mobile, countryCode)
- if wxUserInfo != nil && wxUserInfo.CompanyId != 1 {
- //获取客户产品信息
- productId, ok := companyIdProductIdMap[wxUserInfo.CompanyId]
- if ok { //已经查询过
- if productId > 0 {
- // 获取客户产品信息
- key := fmt.Sprint(wxUserInfo.CompanyId, "_", productId)
- if tmpCompanyProductInfo, ok := companyProductMap[key]; ok {
- companyProductInfo = tmpCompanyProductInfo
- }
- }
- } else {
- for _, productId := range productIdList {
- key := fmt.Sprint(wxUserInfo.CompanyId, "_", productId)
- companyProductInfo, _ = models.GetCompanyProduct(wxUserInfo.CompanyId, productId)
- if companyProductInfo != nil {
- companyProductMap[key] = companyProductInfo
- companyIdProductIdMap[wxUserInfo.CompanyId] = productId
- //退出产品查询
- break
- }
- }
- if companyProductInfo == nil {
- companyIdProductIdMap[wxUserInfo.CompanyId] = 0
- }
- }
- // 获取客户信息
- key := fmt.Sprint(wxUserInfo.CompanyId)
- if tmpCompanyInfo, ok := companyMap[key]; ok {
- companyInfo = tmpCompanyInfo
- } else {
- companyInfo, _ = models.GetCompanyById(wxUserInfo.CompanyId)
- if companyInfo != nil {
- companyMap[key] = companyInfo
- }
- }
- }
- }
- name := v.UserName
- comeinEventUserInfo := &yb.ComeinEventUser{
- //ComeinUserId int `orm:"column(comein_user_id);pk" description:"自增id"`
- ComeinEventId: tmpComeinEvent.ComeinEventId,
- ComeinDataId: int(v.ID),
- UserId: 0,
- Mobile: mobile,
- Email: v.Email,
- Name: name,
- FirstWatchTime: time.Unix(v.FirstWatchTime/1000, 0),
- LastWatchTime: time.Unix(v.LastWatchTime/1000, 0),
- JoinTime: int(v.JoinTime),
- AuthInfo: v.AuthInfo,
- JoinType: int(v.JoinType),
- DataType: int(v.DataType),
- //RegisterTime: time.Time{},
- ViewTotal: 0,
- //LastViewTime: time.Time{},
- CompanyId: 0,
- ProductId: 0,
- CompanyName: v.Company,
- Occupation: v.Occupation,
- Status: "",
- SellerId: 0,
- SellerName: "",
- CompanyViewTotal: 0,
- CompanyRoadTotal: 0,
- CreateTime: time.Now(),
- }
- //这个时候是系统用户了,美滋滋
- if companyProductInfo != nil {
- comeinEventUserInfo.RegisterTime = wxUserInfo.RegisterTime
- //models.
- userViewStatisticsInfo, _ := models.GetUserLastViewStatisticsByMobile(mobile) //用户阅读信息
- if userViewStatisticsInfo != nil {
- comeinEventUserInfo.ViewTotal = userViewStatisticsInfo.Total
- comeinEventUserInfo.LastViewTime = userViewStatisticsInfo.LastViewTime
- }
- comeinEventUserInfo.UserId = int(wxUserInfo.UserId)
- comeinEventUserInfo.Name = wxUserInfo.RealName
- comeinEventUserInfo.CompanyId = companyProductInfo.CompanyId
- comeinEventUserInfo.ProductId = companyProductInfo.ProductId
- comeinEventUserInfo.CompanyName = companyInfo.CompanyName
- comeinEventUserInfo.Status = companyProductInfo.Status
- comeinEventUserInfo.SellerId = companyProductInfo.SellerId
- comeinEventUserInfo.SellerName = companyProductInfo.SellerName
- comeinEventUserInfo.CompanyViewTotal = companyProductInfo.ViewTotal
- comeinEventUserInfo.CompanyRoadTotal = companyProductInfo.RoadShowTotal
- }
- yb.AddComeinEventUser(comeinEventUserInfo)
- tmpComeinEvent.People = tmpComeinEvent.People + 1
- }
- }
- for _, v := range comeinEventMap {
- v.Update([]string{"People"})
- }
- return
- }
|