Browse Source

fix: 同步前三个月电话会90%

hsun 3 years ago
parent
commit
e1ba68b0c5

+ 2 - 2
controller/activity/activity.go

@@ -255,10 +255,10 @@ func SyncOldActivityData(c *gin.Context) {
 		response.Fail("无权操作", c)
 		return
 	}
-	countSuccess, err := activity.SyncTeleconferenceOldData()
+	countTotal, countSuccess, err := activity.SyncTeleconferenceOldData()
 	if err != nil {
 		response.Fail("同步失败" + err.Error(), c)
 		return
 	}
-	response.OkData("同步成功", "同步成功数:" + strconv.Itoa(countSuccess), c)
+	response.OkData("同步成功", "同步总数:" + strconv.Itoa(countTotal) + ", 同步成功数:" + strconv.Itoa(countSuccess), c)
 }

+ 6 - 0
models/tables/research_report/query.go

@@ -22,3 +22,9 @@ func GetByWhereMap(where map[string]interface{}) (item *ResearchReport, err erro
 	err = global.DEFAULT_MYSQL.Where(cond, whereVal...).First(&item).Error
 	return
 }
+
+// GetListByResearchReportIds 根据ids获取报告集合
+func GetListByResearchReportIds(ids []int) (list []*ResearchReport, err error) {
+	err = global.DEFAULT_MYSQL.Model(ResearchReport{}).Where("research_report_id IN (?)", ids).Scan(&list).Error
+	return
+}

+ 9 - 0
models/tables/teleconference_video/query.go

@@ -0,0 +1,9 @@
+package teleconference_video
+
+import "hongze/hongze_yb/global"
+
+// GetListByTeleconferenceIds 通过电话会IDs获取集合
+func GetListByTeleconferenceIds(ids []int) (list []*TeleconferenceVideo, err error) {
+	err = global.DEFAULT_MYSQL.Model(TeleconferenceVideo{}).Where("teleconference_id IN (?)", ids).Scan(&list).Error
+	return
+}

+ 37 - 0
models/tables/teleconference_video/teleconference_video.go

@@ -0,0 +1,37 @@
+package teleconference_video
+
+import (
+	"time"
+)
+
+// TeleconferenceVideo [...]
+type TeleconferenceVideo struct {
+	TeleconferenceVideoID int       `gorm:"primaryKey;column:teleconference_video_id;type:int(11);not null" json:"-"`
+	TeleconferenceID      int       `gorm:"index:idx_teleconference_id;column:teleconference_id;type:int(11);default:0" json:"teleconferenceId"`
+	VideoURL              string    `gorm:"unique;column:video_url;type:varchar(500);default:''" json:"videoUrl"`
+	VideoName             string    `gorm:"column:video_name;type:varchar(255);default:''" json:"videoName"`
+	VideoPlaySeconds      string    `gorm:"column:video_play_seconds;type:varchar(255);default:''" json:"videoPlaySeconds"`
+	CreateTime            time.Time `gorm:"column:create_time;type:datetime" json:"createTime"`
+}
+
+// TableName get sql table name.获取数据库表名
+func (m *TeleconferenceVideo) TableName() string {
+	return "teleconference_video"
+}
+
+// TeleconferenceVideoColumns get sql column name.获取数据库列名
+var TeleconferenceVideoColumns = struct {
+	TeleconferenceVideoID string
+	TeleconferenceID      string
+	VideoURL              string
+	VideoName             string
+	VideoPlaySeconds      string
+	CreateTime            string
+}{
+	TeleconferenceVideoID: "teleconference_video_id",
+	TeleconferenceID:      "teleconference_id",
+	VideoURL:              "video_url",
+	VideoName:             "video_name",
+	VideoPlaySeconds:      "video_play_seconds",
+	CreateTime:            "create_time",
+}

+ 6 - 0
models/tables/yb_activity_type/query.go

@@ -6,4 +6,10 @@ import "hongze/hongze_yb/global"
 func GetTypeByPid(pid int) (typeList []*YbActivityType, err error) {
 	err = global.DEFAULT_MYSQL.Model(YbActivityType{}).Where("pid", pid).Scan(&typeList).Error
 	return
+}
+
+// GetTypeList 获取活动类型列表
+func GetTypeList() (typeList []*YbActivityType, err error) {
+	err = global.DEFAULT_MYSQL.Model(YbActivityType{}).Scan(&typeList).Error
+	return
 }

+ 1 - 0
models/tables/yb_speaker/query.go

@@ -0,0 +1 @@
+package yb_speaker

+ 27 - 0
models/tables/yb_speaker/yb_speaker.go

@@ -0,0 +1,27 @@
+package yb_speaker
+
+type YbSpeaker struct {
+	SpeakerID            uint32 `gorm:"primaryKey;column:speaker_id;type:int(9) unsigned;not null" json:"speakerId"`                    // 主讲人id
+	SpeakerName          string `gorm:"column:speaker_name;type:varchar(255)" json:"speakerName"`                               // 主讲人名称
+	SpeakerHeadPic       string `gorm:"column:speaker_head_pic;type:varchar(255);default:''" json:"speakerHeadPic"`             // 主讲人头像
+	SpeakerBackgroundPic string `gorm:"column:speaker_background_pic;type:varchar(255);default:''" json:"speakerBackgroundPic"` // 主讲人背景图
+}
+
+// TableName get sql table name.获取数据库表名
+func (m *YbSpeaker) TableName() string {
+	return "yb_speaker"
+}
+
+// YbSpeakerColumns get sql column name.获取数据库列名
+var YbSpeakerColumns = struct {
+	SpeakerID            string
+	SpeakerName          string
+	SpeakerHeadPic       string
+	SpeakerBackgroundPic string
+}{
+	SpeakerID:            "speaker_id",
+	SpeakerName:          "speaker_name",
+	SpeakerHeadPic:       "speaker_head_pic",
+	SpeakerBackgroundPic: "speaker_background_pic",
+}
+

+ 100 - 29
services/activity/activity.go

@@ -1,13 +1,17 @@
 package activity
 
 import (
+	"errors"
 	"gorm.io/gorm"
 	"hongze/hongze_yb/global"
+	"hongze/hongze_yb/models/tables/research_report"
 	"hongze/hongze_yb/models/tables/teleconference"
+	"hongze/hongze_yb/models/tables/teleconference_video"
 	"hongze/hongze_yb/models/tables/yb_activity"
 	"hongze/hongze_yb/models/tables/yb_activity_permission"
 	"hongze/hongze_yb/models/tables/yb_activity_register"
 	"hongze/hongze_yb/models/tables/yb_activity_remind"
+	"hongze/hongze_yb/models/tables/yb_activity_type"
 	"hongze/hongze_yb/models/tables/yb_activity_voice"
 	"hongze/hongze_yb/services/company"
 	"hongze/hongze_yb/services/user"
@@ -189,8 +193,8 @@ func GetUserAuthActivityIds(userInfo user.UserInfo) (acrivityIds []int, err erro
 }
 
 // SyncTeleconferenceOldData 同步旧数据
-func SyncTeleconferenceOldData() (countSuccess int, err error) {
-	// 查询所有三个月内的数据及相关音频
+func SyncTeleconferenceOldData() (countTotal, countSuccess int, err error) {
+	// 查询所有前三个月的电话会
 	condition := "enabled = 1"
 	pars := make([]interface{}, 0)
 	nowTime := time.Now()
@@ -201,9 +205,54 @@ func SyncTeleconferenceOldData() (countSuccess int, err error) {
 	if err != nil {
 		return
 	}
+	countTotal = len(teleconferenceList)
+	
+	// 取出电话会IDs和报告Ids
+	var teleconferenceIds, reportIds []int
+	for i := 0; i < len(teleconferenceList); i++ {
+		teleconferenceIds = append(teleconferenceIds, int(teleconferenceList[i].TeleconferenceID))
+		reportIds = append(reportIds, int(teleconferenceList[i].ReferResearchReportID))
+	}
 
-	// 开启事务
+	// 报告名称map
+	var reportIdNameMap = make(map[int]string, 0)
+	reportList, err := research_report.GetListByResearchReportIds(reportIds)
+	if err != nil {
+		return
+	}
+	for i := 0; i < len(reportList); i++ {
+		reportIdNameMap[int(reportList[i].ResearchReportID)] = reportList[i].ResearchReportName
+	}
+
+	// 活动类型map
+	var typeNameIdMap = make(map[string]int, 0)
+	var typeFirstMap = make(map[int]int, 0)
+	var typeIdNameMap = make(map[int]string, 0)
+	activityTypeList, err := yb_activity_type.GetTypeList()
+	if err != nil {
+		return
+	}
+	for _, activityType := range activityTypeList {
+		typeNameIdMap[activityType.ActivityTypeName] = activityType.ActivityTypeID
+		typeFirstMap[activityType.ActivityTypeID] = activityType.Pid
+		typeIdNameMap[activityType.ActivityTypeID] = activityType.ActivityTypeName
+	}
 
+	// 音频列表
+	voicesList, err := teleconference_video.GetListByTeleconferenceIds(teleconferenceIds)
+	if err != nil {
+		return
+	}
+
+	// 开启事务
+	tx := global.DEFAULT_MYSQL.Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+		} else {
+			tx.Commit()
+		}
+	}()
 
 	// 遍历数据
 	for _, item := range teleconferenceList {
@@ -214,37 +263,48 @@ func SyncTeleconferenceOldData() (countSuccess int, err error) {
 			linkUrl = `http://advisoryadmin.brilliantstart.cn/xcx_h5/ficcReportDetail?research_report_id=`
 		}
 		reportLink := linkUrl + strconv.Itoa(reportId)
+		reportName := reportIdNameMap[reportId]
 
-		// 匹配活动类型
+		// 匹配活动类型-中文匹配ID
+		var firstActivityTypeId, activityTypeId int
+		var firstActivityTypeName, activityTypeName string
+		activityTypeId = typeNameIdMap[item.Type]
+		if activityTypeId > 0 {
+			// 类型匹配到了
+			activityTypeName = typeIdNameMap[activityTypeId]
+			firstActivityTypeId = typeFirstMap[activityTypeId]
+			firstActivityTypeName = typeIdNameMap[firstActivityTypeId]
+		}
 
-		// 匹配主讲人信息
-		/*if firstActivityTypeId == 1 {
+		// TODO:匹配主讲人头像-规则暂定
+		backgroundPic := ""
+		if firstActivityTypeId == 1 {
 			// 电话会议
 			backgroundPic = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202112/20211214/nNSTxmH6lxlT0PAp7xcfJJTGX9Gu.png`
 		} else {
 			// 线下沙龙
 			backgroundPic = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202112/20211214/k77gDxvFvuAnYUhF6PU1FOVDHI8O.png`
-		}*/
+		}
 
 		// 新增活动数据
 		activityInfo := &yb_activity.YbActivity{
-			FirstActivityTypeID: 0,
-			FirstActivityTypeName: "",
-			ActivityTypeID: 0,
-			ActivityTypeName: "",
+			FirstActivityTypeID: firstActivityTypeId,
+			FirstActivityTypeName: firstActivityTypeName,
+			ActivityTypeID: activityTypeId,
+			ActivityTypeName: activityTypeName,
 			ActivityName: item.TeleconferenceTitle,
 			StartTime: item.StartTime,
 			EndTime: item.EndTime,
-			Speaker: "",
+			Speaker: item.Speaker,
 			SpeakerHeadPic: "",
-			SpeakerBackgroundPic: "",
-			SingaporeTel: "",
+			SpeakerBackgroundPic: backgroundPic,
+			SingaporeTel: item.SingaporeTelephone,
 			ParticipationCode: item.Password,
 			MainlandTel: item.Telephone,
 			HongKongTel: item.HkTelephone,
 			ReportID: reportId,
 			ReportLink: reportLink,
-			ReportName: "",
+			ReportName: reportName,
 			Remarks: item.Remark,
 			PublishStatus: 1,
 			IsSendWxMsg: 1,
@@ -252,22 +312,31 @@ func SyncTeleconferenceOldData() (countSuccess int, err error) {
 			ModifyTime: nowTime,
 			CreateTime: nowTime,
 		}
-		activityId, tempErr := activityInfo.Create()
-		if tempErr != nil {
+
+		// 开启事务之后的操作要使用tx
+		activityCreate := tx.Debug().Create(activityInfo)
+		if activityCreate.Error != nil {
+			err = errors.New("新增活动失败")
 			return
 		}
+		activityId := activityInfo.ActivityID
 
 		// 5.新增活动音频-音频列表
-		activityVoice := &yb_activity_voice.YbActivityVoice{
-			ActivityVoiceID: activityId,
-			VoiceURL: item.VideoURL,
-			VoiceName: item.VideoName,
-			VoicePlaySeconds: item.VideoPlaySeconds,
-			CreateTime: nowTime,
-		}
-		tempErr = activityVoice.Create()
-		if tempErr != nil {
-			return
+		for _, voice := range voicesList {
+			if int(item.TeleconferenceID) == voice.TeleconferenceID {
+				activityVoice := &yb_activity_voice.YbActivityVoice{
+					ActivityVoiceID: activityId,
+					VoiceURL: voice.VideoURL,
+					VoiceName: voice.VideoName,
+					VoicePlaySeconds: voice.VideoPlaySeconds,
+					CreateTime: nowTime,
+				}
+				activityVoiceCreate := tx.Debug().Create(activityVoice)
+				if activityVoiceCreate.Error != nil {
+					err = errors.New("新增活动音频失败")
+					return
+				}
+			}
 		}
 
 		// 6.新增活动关联权限-源数据权限仅一个
@@ -277,10 +346,12 @@ func SyncTeleconferenceOldData() (countSuccess int, err error) {
 			PermissionName: "",
 			CreateTime: nowTime,
 		}
-		tempErr = activityPermission.Create()
-		if tempErr != nil {
+		activityPermissionCreate := tx.Debug().Create(activityPermission)
+		if activityPermissionCreate.Error != nil {
+			err = errors.New("新增活动权限失败")
 			return
 		}
+		countSuccess += 1
 	}
 
 	return