Browse Source

同步已上架的图表分类下的图

xyxie 1 tháng trước cách đây
mục cha
commit
04966d5ac4

+ 25 - 1
models/chart_classify.go

@@ -3,8 +3,9 @@ package models
 import (
 	"eta/eta_forum_task/utils"
 	"fmt"
-	"github.com/beego/beego/v2/client/orm"
 	"time"
+
+	"github.com/beego/beego/v2/client/orm"
 )
 
 type ChartClassify struct {
@@ -348,3 +349,26 @@ func GetChartClassifyBySourceAndIsJoinPermission(source, isJoinPermission int) (
 	_, err = o.Raw(sql, source, isJoinPermission).QueryRows(&items)
 	return
 }
+
+// GetChartClassifyListByCondition 根据条件获取分类列表
+func GetChartClassifyListByCondition(condition string, pars []interface{}) (items []*ChartClassify, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT * FROM chart_classify WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	_, err = o.Raw(sql, pars).QueryRows(&items)
+	return
+}
+
+func SetForumChartInfoId(chart_info_id, forum_chart_info_id, resourceStatus int) (err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := ` UPDATE  chart_info
+			SET
+              forum_chart_info_id=?,
+			  modify_time = NOW(),
+			  resource_status = ?
+			WHERE chart_info_id = ?`
+	_, err = o.Raw(sql, forum_chart_info_id, resourceStatus, chart_info_id).Exec()
+	return
+}

+ 283 - 65
services/chart_info.go

@@ -3,84 +3,35 @@ package services
 import (
 	"context"
 	"encoding/json"
-	"eta/eta_forum_task/models"
+	"eta/eta_forum_task/models"	
 	"eta/eta_forum_task/services/alarm_msg"
 	"eta/eta_forum_task/services/eta_forum"
 	"eta/eta_forum_task/utils"
 	"fmt"
+	"strconv"
 	"time"
 )
 
 func EtaForumChartUpdate(cont context.Context) (err error) {
-	var tmpErr []error
-	deleteCache := true
-	cacheKey := "eta_forum_task:EtaForumChartUpdate"
-	defer func() {
-		if deleteCache {
-			_ = utils.Rc.Delete(cacheKey)
-		}
-		stack := ""
-		if err != nil {
-			stack = fmt.Sprintln(stack + err.Error())
-		}
-		if len(tmpErr) > 0 {
-			for _, v := range tmpErr {
-				stack = fmt.Sprintln(stack + v.Error())
-			}
-		}
-		if stack != "" {
-			go alarm_msg.SendAlarmMsg(utils.APP_NAME_CN+"更新社区图表信息失败"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
-		}
-	}()
-	if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
-		deleteCache = false
-		err = fmt.Errorf("系统处理中,请稍后重试!")
-		return
-	}
-	// 设置缓存判断处理中则不更新
-	condition := " and forum_chart_info_id > 0 and source=1 "
-	// 查询需要更新的图表信息总数
-	total, err := models.GetChartInfoCountByCondition(condition, []interface{}{})
-	if err != nil {
-		return
-	}
-	if total == 0 {
-		return
-	}
-	// 更新图表数据
-	offset := 0
-	pageSize := 100
-	success := 0
-
-	// 循环更新100个图表数据
-	for i := 0; offset < total; i++ {
-		// 查询需要更新的图表信息
-		chartInfos, e := models.GetChartInfoListByCondition(condition, []interface{}{}, offset, pageSize)
-		if e != nil {
-			err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
-			return
-		}
-		// 循环更新图表数据
-		for _, chartInfo := range chartInfos {
-			// 更新图表数据
-			er, msg := UpdateChart(chartInfo.ChartInfoId)
-			if er != nil {
-				er = fmt.Errorf("图表ID %d, 更新图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
-				tmpErr = append(tmpErr, er)
-				continue
-			}
-			success += 1
-		}
-
-		offset = (i + 1) * pageSize
-		time.Sleep(2 * time.Second)
-	}
-	fmt.Println("更新图表数据完成, 更新图表数据总数:", success)
+	err = ChartInfoSaveBatch()
 	return
 }
 
 // UpdateChart 更新社区里的图表接口
 func UpdateChart(chartInfoId int) (err error, errMsg string) {
+	// 设置缓存 防止重复更新
+	cacheKey := "eta_forum_task:UpdateChart:" + strconv.Itoa(chartInfoId)
+	if utils.Rc.Get(cacheKey) != nil {
+		err = fmt.Errorf("系统处理中,请稍后重试!")
+		return
+	}
+	utils.Rc.SetNX(cacheKey, 1, 30*time.Second)
+	defer func() {
+		if err != nil {
+			go alarm_msg.SendAlarmMsg(fmt.Sprintf("更新图表至社区失败:Err:%v,ErrMsg:%s", err, errMsg), 3)
+		}
+		utils.Rc.Delete(cacheKey)
+	}()
 	// 查询图表信息
 	chartInfo, err := models.GetChartInfoById(chartInfoId)
 	if err != nil {
@@ -290,3 +241,270 @@ func traceEdbInfoByEdbInfoId(edbInfoId int, hasFindMap map[int]struct{}, edbInfo
 	hasFindMap[edbInfoId] = struct{}{}
 	return
 }
+
+// 批量上传图表分类信息
+func ChartInfoSaveBatch() (err error) {
+	var tmpErr []error
+	deleteCache := true
+	cacheKey := "eta_forum_task:ChartInfoSaveBatchAdmin"
+	defer func() {
+		if deleteCache {
+			_ = utils.Rc.Delete(cacheKey)
+		}
+		stack := ""
+		if err != nil {
+			stack = fmt.Sprintln(stack + err.Error())
+		}
+		if len(tmpErr) > 0 {
+			for _, v := range tmpErr {
+				stack = fmt.Sprintln(stack + v.Error())
+			}
+		}
+		if stack != "" {
+			utils.FileLog.Error("批量上传资源库图表信息失败"+"<br/>"+stack)
+			go alarm_msg.SendAlarmMsg("批量上传资源库图表信息失败"+"<br/>"+stack, 3)
+		}
+	}()
+	if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
+		deleteCache = false
+		err = fmt.Errorf("系统处理中,请稍后重试!")
+		return
+	}
+
+	// 已上架的图表都更新,批量更新图表信息
+	condition := " and source=1 and forum_chart_info_id > 0 and resource_status =1"
+	// 查询需要更新的图表信息总数
+	total, err := models.GetChartInfoCountByCondition(condition, []interface{}{})
+	if err != nil {
+		return
+	}
+	if total == 0 {
+		return
+	}
+
+	// 批量上传图表信息
+	offset := 0
+	pageSize := 100
+	success := 0
+
+	// 循环更新100个图表数据
+	for i := 0; offset < total; i++ {
+		// 查询需要更新的图表信息
+		chartInfos, e := models.GetChartInfoListByCondition(condition, []interface{}{}, offset, pageSize)
+		if e != nil {
+			err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
+			return
+		}
+		// 循环更新图表数据
+		for _, chartInfo := range chartInfos {
+			var er error
+			var msg string
+			// 更新图表数据
+			er, msg = UpdateChart(chartInfo.ChartInfoId)
+			
+			if er != nil {
+				er = fmt.Errorf("图表ID %d, 更新图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
+				tmpErr = append(tmpErr, er)
+				continue
+			}
+			success += 1
+		}
+
+		offset = (i + 1) * pageSize
+	}
+	fmt.Println("更新图表数据完成, 更新图表数据总数:", success)
+
+
+	sysUser, err := models.GetSysUserByAdminName("admin")
+	if err != nil {
+		err = fmt.Errorf("获取系统用户失败,Err:" + err.Error())
+		return
+	}
+	// 获取精选资源分类已经上架的分类
+	classifyCondition := " AND source=? and is_selected=1 and resource_status=1 "
+	var classifyPars []interface{}
+	classifyPars = append(classifyPars, utils.CHART_SOURCE_DEFAULT)
+	classifyList, err := models.GetChartClassifyListByCondition(classifyCondition, classifyPars)
+	if err != nil {
+		return
+	}
+	chartClassifyIdList := make([]string, 0)
+	for _, v := range classifyList {
+		chartClassifyIdList = append(chartClassifyIdList, strconv.Itoa(v.ChartClassifyId))
+	}
+	if len(chartClassifyIdList) == 0 {
+		return
+	}
+	// 批量上传图表信息
+	condition = " and source=1 and forum_chart_info_id = 0 and resource_status !=1 and chart_classify_id in ("+utils.GetOrmInReplace(len(chartClassifyIdList))+") "
+	// 查询需要更新的图表信息总数
+	total, err = models.GetChartInfoCountByCondition(condition, []interface{}{chartClassifyIdList})
+	if err != nil {
+		return
+	}
+	if total == 0 {
+		return
+	}
+
+	// 批量上传图表信息
+	offset = 0
+	pageSize = 100
+	success = 0
+
+	// 循环更新100个图表数据
+	for i := 0; offset < total; i++ {
+		// 查询需要更新的图表信息
+		chartInfos, e := models.GetChartInfoListByCondition(condition, []interface{}{chartClassifyIdList}, offset, pageSize)
+		if e != nil {
+			err = fmt.Errorf("查询需要更新的图表信息失败: %v", e)
+			return
+		}
+		// 循环更新图表数据
+		for _, chartInfo := range chartInfos {
+			var er error
+			var msg string
+			// 上传图表数据
+			er, msg = UploadChart(chartInfo.ChartInfoId, "", sysUser)
+			
+			if er != nil {
+				er = fmt.Errorf("图表ID %d, 上传图表数据失败: %s, %v", chartInfo.ChartInfoId, msg, er)
+				tmpErr = append(tmpErr, er)
+				continue
+			}
+			success += 1
+		}
+
+		offset = (i + 1) * pageSize
+	}
+	fmt.Println("上传图表数据完成, 上传图表数据总数:", success)
+
+	return
+}
+
+// UploadChart 上传图表接口
+func UploadChart(chartInfoId int, description string, uploaderInfo *models.Admin) (err error, errMsg string) {
+	// 设置缓存 防止重复上传
+	cacheKey := "eta_forum_task:UploadChart:" + strconv.Itoa(chartInfoId)
+	if utils.Rc.Get(cacheKey) != nil {
+		err = fmt.Errorf("系统处理中,请稍后重试!")
+		return
+	}
+	utils.Rc.SetNX(cacheKey, 1, 30*time.Second)
+	defer func() {
+		if err != nil {
+			go alarm_msg.SendAlarmMsg(fmt.Sprintf("上传图表至社区失败:Err:%v,ErrMsg:%s", err, errMsg), 3)
+		}
+		utils.Rc.Delete(cacheKey)
+	}()
+
+	// 查询图表信息
+	chartInfo, err := models.GetChartInfoById(chartInfoId)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			errMsg = "图表不存在"
+			err = fmt.Errorf(errMsg)
+			return
+		}
+		errMsg = "获取图表信息失败"
+		err = fmt.Errorf("获取图表信息失败,Err:" + err.Error())
+		return
+	}
+	if chartInfo.ForumChartInfoId > 0 {
+		errMsg = "该图表已上传至社区,无法再次上传"
+		err = fmt.Errorf(errMsg)
+		return
+	}
+	//查询图表主题
+
+	//查询图表指标
+	//获取原图表关联的指标信息列表
+	chartMappingList, err := models.GetChartMappingList(chartInfoId)
+	if err != nil {
+		errMsg = "获取图表关联的指标信息失败"
+		err = fmt.Errorf("获取图表关联的指标信息失败,Err:" + err.Error())
+		return
+	}
+	edbIds := make([]int, 0)
+	for _, v := range chartMappingList {
+		edbIds = append(edbIds, v.EdbInfoId)
+	}
+	chartSeriesList := make([]*models.ChartSeries, 0)
+	chartSeriesEdbList := make([]*models.ChartSeriesEdbMapping, 0)
+	if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
+		chartSeriesList, err = models.GetChartSeriesByChartInfoId(chartInfoId)
+		if err != nil {
+			errMsg = "获取图表关联的系列信息失败"
+			err = fmt.Errorf("获取图表关联的系列信息失败,Err:" + err.Error())
+			return
+		}
+
+		chartSeriesEdbList, err = models.GetChartSeriesEdbByChartInfoId(chartInfoId)
+		if err != nil {
+			errMsg = "获取图表关联的系列指标信息失败"
+			err = fmt.Errorf("获取图表关联的系列指标信息失败,Err:" + err.Error())
+			return
+		}
+	}
+
+	var (
+		edbInfoList     []*models.EdbInfo
+		edbMappingList  []*models.EdbInfoCalculateMapping
+		edbInfoDataList []*eta_forum.AddEdbDataReq
+	)
+	//查询指标详情
+	edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds)
+	if err != nil {
+		errMsg = "获取指标详情失败"
+		err = fmt.Errorf("获取指标详情失败,Err:" + err.Error())
+		return
+	}
+
+	req := new(eta_forum.ChartSaveLibReq)
+	req.ChartInfo = chartInfo
+	req.ChartEdbMapping = chartMappingList
+	req.EdbInfoList = edbInfoList
+	req.EdbInfoDataList = edbInfoDataList
+	req.EdbInfoCalculateMapping = edbMappingList
+	req.Description = description
+	req.ChartSeries = chartSeriesList
+	req.ChartSeriesEdbMapping = chartSeriesEdbList
+
+	// 查询创建者信息
+	creatorInfo, _ := models.GetSysUserById(chartInfo.SysUserId)
+	if creatorInfo != nil {
+		req.CreatorInfo = creatorInfo
+	}
+
+	req.UploaderInfo = uploaderInfo
+	// 添加计算指标
+	reqJson, err := json.Marshal(req)
+	if err != nil {
+		errMsg = "参数解析异常"
+		err = fmt.Errorf("参数解析异常,Err:" + err.Error())
+		return
+	}
+	respItem, err := eta_forum.ChartSaveLib(string(reqJson))
+	if err != nil {
+		errMsg = "上传失败"
+		err = fmt.Errorf("上传失败,Err:" + err.Error())
+		return
+	}
+	if respItem.Ret != 200 {
+		errMsg = "上传失败"
+		err = fmt.Errorf(respItem.ErrMsg)
+		return
+	}
+
+	if respItem.Data != nil && respItem.Data.ChartInfoId != 0 {
+		// 更新社区返回的图表ID
+		err = models.SetForumChartInfoId(chartInfoId, respItem.Data.ChartInfoId, utils.ChartClassifyResourceStatusUp)
+		if err != nil {
+			errMsg = "更新图表ID失败"
+			err = fmt.Errorf("更新图表ID失败,Err:" + err.Error())
+			return
+		}
+	}
+
+	return
+}
+

+ 1 - 0
services/eta_forum/eta_forum_hub.go

@@ -25,6 +25,7 @@ type ChartSaveLibReq struct {
 	ChartSeries             []*models.ChartSeries
 	ChartSeriesEdbMapping   []*models.ChartSeriesEdbMapping
 	CreatorInfo             *models.Admin //创建者的账号信息
+	UploaderInfo            *models.Admin //上传者的账号信息
 }
 
 type ChartSaveLibResp struct {

+ 11 - 0
services/eta_forum/eta_forum_hub_lib.go

@@ -10,6 +10,17 @@ import (
 	"strings"
 )
 
+// ChartSaveLib 上传图表信息
+func ChartSaveLib(req string) (resp *ChartSaveLibResp, err error) {
+	_, resultByte, err := post(req, "/v1/chart/save")
+	err = json.Unmarshal(resultByte, &resp)
+	if err != nil {
+		return
+	}
+	return
+}
+
+
 // ChartUpdateLib 更新图表信息
 func ChartUpdateLib(req string) (resp *ChartSaveLibResp, err error) {
 	_, resultByte, err := post(req, "/v1/chart/update")

+ 8 - 0
utils/constants.go

@@ -255,3 +255,11 @@ const (
 	CHART_TYPE_RADAR           = 11 //雷达图
 	CHART_TYPE_SECTION_COMBINE = 14 //截面组合图
 )
+
+// 图表分类设置精选资源分类
+const (
+	ChartClassifyIsSelected = 1 // 图表分类设置精选资源分类
+	ChartClassifyResourceStatusUp = 1 // 图表分类上架状态
+	ChartClassifyResourceStatusDown = 2 // 图表分类下架状态
+	ChartClassifyResourceStatusDefault = 0 // 图表分类默认状态
+)