浏览代码

Merge remote-tracking branch 'origin/debug' into debug

zwxi 8 月之前
父节点
当前提交
d82daf86ba

+ 271 - 0
controllers/data_manage/excel/custom_analysis.go

@@ -1145,3 +1145,274 @@ func (c *CustomAnalysisController) ShareDetail() {
 	br.Success = true
 	br.Msg = "获取成功"
 }
+
+// CustomAnalysisCommonController 修复数据用的
+type CustomAnalysisCommonController struct {
+	controllers.BaseCommonController
+}
+
+//var OnceCustomAnalysisFixed = false
+
+// FixClassify
+// @Title 修复表格分类(一次性-后续版本可删掉)
+// @Description 修复表格分类
+// @Param   ExcelInfoId  query  int  true  "表格ID"
+// @Success 200 string "操作成功"
+// @router /excel/fix_classify [get]
+func (c *CustomAnalysisCommonController) FixClassify() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		if br.ErrMsg != "" {
+			br.IsSendEmail = false
+		}
+		if br.Ret == 200 {
+			//OnceCustomAnalysisFixed = true
+		}
+		c.Data["json"] = br
+		c.ServeJSON()
+	}()
+	//if OnceCustomAnalysisFixed {
+	//	br.Ret = 200
+	//	br.Success = true
+	//	br.Msg = "请勿重复修复"
+	//	return
+	//}
+	source := utils.CUSTOM_ANALYSIS_TABLE
+
+	// 获取所有分类
+	classifies, e := excelModel.GetExcelClassifyModelBySource(source)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
+		return
+	}
+	classifyMap := make(map[int]*excelModel.ExcelClassify)
+	for _, v := range classifies {
+		classifyMap[v.ExcelClassifyId] = v
+	}
+
+	// 查询所有excel
+	excels, e := excelModel.GetAllExcelInfoBySource(source)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = fmt.Sprintf("获取excel列表失败, %v", e)
+		return
+	}
+	if len(excels) == 0 {
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "无分类需要修复"
+		return
+	}
+
+	// 先按照用户ID分组
+	groupExcels := make(map[int][]*excelModel.ExcelInfo)
+	for _, v := range excels {
+		//if v.SysUserId != 198 {
+		//	continue
+		//}
+		if groupExcels[v.SysUserId] == nil {
+			groupExcels[v.SysUserId] = make([]*excelModel.ExcelInfo, 0)
+		}
+		groupExcels[v.SysUserId] = append(groupExcels[v.SysUserId], v)
+	}
+
+	// 递归函数
+	findAndReplace := func(replaceId map[int]*excelModel.ExcelClassify, userId int, excelItem *excelModel.ExcelInfo, classifyItem *excelModel.ExcelClassify, firstStep bool) (hasParent, updateChild bool, currentClassify *excelModel.ExcelClassify, err error) {
+		// 分类创建人与表格不一致
+		currentClassify = classifyItem
+		if classifyItem.SysUserId != userId {
+			// 是否已存在替换分类
+			_, ok := replaceId[classifyItem.ExcelClassifyId]
+			if !ok {
+				classifyNew := &excelModel.ExcelClassify{
+					ExcelClassifyName: fmt.Sprintf("%d%s", userId, classifyItem.ExcelClassifyName),
+					ParentId:          classifyItem.ParentId, // 根据updateChild递归外更新
+					Source:            source,
+					SysUserId:         excelItem.SysUserId,
+					SysUserRealName:   excelItem.SysUserRealName,
+					Level:             classifyItem.Level,
+					UniqueCode:        utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
+					Sort:              classifyItem.Sort,
+					CreateTime:        time.Now(),
+					ModifyTime:        time.Now(),
+				}
+				_, e = excelModel.AddExcelClassify(classifyNew)
+				if e != nil {
+					err = fmt.Errorf("AddExcelClassify, %v", e)
+					return
+				}
+				replaceId[classifyItem.ExcelClassifyId] = classifyNew
+				excelItem.ExcelClassifyId = classifyNew.ExcelClassifyId
+				currentClassify = classifyNew
+				classifyMap[classifyNew.ExcelClassifyId] = classifyNew
+			} else {
+				excelItem.ExcelClassifyId = replaceId[classifyItem.ExcelClassifyId].ExcelClassifyId
+				currentClassify = replaceId[classifyItem.ExcelClassifyId]
+			}
+			// 仅第一次递归时修改
+			if firstStep {
+				//fmt.Printf("修改ExcelClassifyId: %d\n", excelItem.ExcelClassifyId)
+				excelItem.ModifyTime = time.Now().Local()
+				if e = excelItem.Update([]string{"ExcelClassifyId", "ModifyTime"}); e != nil {
+					err = fmt.Errorf("UpdateExcelInfo, %v", e)
+					return
+				}
+			}
+		}
+
+		// 查上级
+		if classifyItem.ParentId <= 0 {
+			//fmt.Printf("终止递归, ClassifyId: %d", classifyItem.ExcelClassifyId)
+			return
+		}
+		hasParent = true
+		parent := classifyMap[classifyItem.ParentId]
+		if parent == nil {
+			err = fmt.Errorf("未找到上级分类, ClassifyId: %d, ParentId: %d", classifyItem.ExcelClassifyId, classifyItem.ParentId)
+			return
+		}
+		if parent.SysUserId != userId {
+			updateChild = true
+		}
+		return
+	}
+
+	for uid, gv := range groupExcels {
+
+		replaceId := make(map[int]*excelModel.ExcelClassify)
+		for _, v := range gv {
+			classifyItem, e := excelModel.GetExcelClassifyById(v.ExcelClassifyId)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("获取表格分类失败, ExcelClassifyId: %d, %v", v.ExcelClassifyId, e)
+				return
+			}
+			// 首次递归
+			hasParent, updateChild, currentClassify, e := findAndReplace(replaceId, uid, v, classifyItem, true)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("findAndReplace1, %v", e)
+				return
+			}
+			if !hasParent {
+				continue
+			}
+
+			// 二次递归
+			//fmt.Printf("二次递归: %d\n", currentClassify.ParentId)
+			parentClassify, e := excelModel.GetExcelClassifyById(currentClassify.ParentId)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("2-parent-GetExcelClassifyById, %v", e)
+				return
+			}
+			hasParent2, updateChild2, currentClassify2, e := findAndReplace(replaceId, uid, v, parentClassify, false)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("findAndReplace2, %v", e)
+				return
+			}
+			// 更新上次递归的parent_id
+			if updateChild {
+				currentClassify.ParentId = currentClassify2.ExcelClassifyId
+				currentClassify.ModifyTime = time.Now().Local()
+				if e = currentClassify.Update([]string{"ParentId", "ModifyTime"}); e != nil {
+					br.Msg = "操作失败"
+					br.ErrMsg = fmt.Sprintf("2-更新父级分类ID失败, %v", e)
+					return
+				}
+			}
+			if !hasParent2 {
+				continue
+			}
+
+			// 三次递归
+			//fmt.Printf("三次递归: %d\n", currentClassify2.ParentId)
+			parentClassify2, e := excelModel.GetExcelClassifyById(currentClassify2.ParentId)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("3-parent-GetExcelClassifyById, %v", e)
+				return
+			}
+			hasParent3, updateChild3, currentClassify3, e := findAndReplace(replaceId, uid, v, parentClassify2, false)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("findAndReplace3, %v", e)
+				return
+			}
+			if updateChild2 {
+				currentClassify2.ParentId = currentClassify3.ExcelClassifyId
+				currentClassify2.ModifyTime = time.Now().Local()
+				if e = currentClassify2.Update([]string{"ParentId", "ModifyTime"}); e != nil {
+					br.Msg = "操作失败"
+					br.ErrMsg = fmt.Sprintf("3-更新父级分类ID失败, %v", e)
+					return
+				}
+			}
+			if !hasParent3 {
+				continue
+			}
+
+			// 四次递归
+			//fmt.Printf("四次递归: %d\n", currentClassify3.ParentId)
+			parentClassify3, e := excelModel.GetExcelClassifyById(currentClassify3.ParentId)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("4-parent-GetExcelClassifyById, %v", e)
+				return
+			}
+			hasParent4, updateChild4, currentClassify4, e := findAndReplace(replaceId, uid, v, parentClassify3, false)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("findAndReplace4, %v", e)
+				return
+			}
+			if updateChild3 {
+				currentClassify3.ParentId = currentClassify4.ExcelClassifyId
+				currentClassify3.ModifyTime = time.Now().Local()
+				if e = currentClassify3.Update([]string{"ParentId", "ModifyTime"}); e != nil {
+					br.Msg = "操作失败"
+					br.ErrMsg = fmt.Sprintf("4-更新父级分类ID失败, %v", e)
+					return
+				}
+			}
+			if !hasParent4 {
+				continue
+			}
+
+			// 五次递归-理论上没了(自定义分析最多三级目录+最底层的表格)
+			//fmt.Printf("五次递归: %d\n", currentClassify4.ParentId)
+			parentClassify4, e := excelModel.GetExcelClassifyById(currentClassify4.ParentId)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("5-parent-GetExcelClassifyById, %v", e)
+				return
+			}
+			hasParent5, _, currentClassify5, e := findAndReplace(replaceId, uid, v, parentClassify4, false)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = fmt.Sprintf("findAndReplace5, %v", e)
+				return
+			}
+			if updateChild4 {
+				currentClassify4.ParentId = currentClassify5.ExcelClassifyId
+				currentClassify4.ModifyTime = time.Now().Local()
+				if e = currentClassify4.Update([]string{"ParentId", "ModifyTime"}); e != nil {
+					br.Msg = "操作失败"
+					br.ErrMsg = fmt.Sprintf("5-更新父级分类ID失败, %v", e)
+					return
+				}
+			}
+			if !hasParent5 {
+				continue
+			}
+		}
+
+		//fmt.Println("replaceId: ", replaceId)
+	}
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "操作成功"
+}

+ 29 - 4
controllers/data_manage/future_good/future_good_chart_info.go

@@ -511,7 +511,12 @@ func (this *FutureGoodChartInfoController) ChartInfoAdd() {
 		return
 	}
 	mapList := make([]*data_manage.ChartEdbMapping, 0)
+	hasAddMap := make(map[int]struct{})
 	for _, v := range barChartConfReq.EdbInfoIdList {
+		if _, ok := hasAddMap[v.EdbInfoId]; ok {
+			continue
+		}
+		hasAddMap[v.EdbInfoId] = struct{}{}
 		mapItem := new(data_manage.ChartEdbMapping)
 		mapItem.ChartInfoId = int(newId)
 		mapItem.EdbInfoId = v.EdbInfoId
@@ -2974,14 +2979,34 @@ func (this *FutureGoodChartInfoController) PreviewBarChartInfo() {
 	}
 	chartInfo := new(data_manage.ChartInfoView)
 
-	var edbInfoMapping, futureGoodEdbInfoMapping *data_manage.ChartEdbInfoMapping
+	var futureGoodEdbInfoMapping *data_manage.ChartEdbInfoMapping
 	edbInfoMappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
+	edbIds := make([]int, 0)
+	goodEdbIds := make([]int, 0)
 	for _, v := range req.EdbInfoIdList {
 		if v.Source == utils.CHART_SOURCE_DEFAULT {
-			edbInfoMapping, err = data_manage.GetChartEdbMappingByEdbInfoId(v.EdbInfoId)
-			if err != nil {
+			edbIds = append(edbIds, v.EdbInfoId)
+		} else if v.Source == utils.CHART_SOURCE_FUTURE_GOOD {
+			goodEdbIds = append(goodEdbIds, v.EdbInfoId)
+		}
+	}
+	edbInfoMappingListTmp, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbIds)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
+		return
+	}
+	edbInfoMappingListMap := make(map[int]*data_manage.ChartEdbInfoMapping)
+	for _, v := range edbInfoMappingListTmp {
+		edbInfoMappingListMap[v.EdbInfoId] = v
+	}
+
+	for _, v := range req.EdbInfoIdList {
+		if v.Source == utils.CHART_SOURCE_DEFAULT {
+			edbInfoMapping, ok := edbInfoMappingListMap[v.EdbInfoId]
+			if !ok {
 				br.Msg = "获取失败"
-				br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
+				br.ErrMsg = "获取图表,指标信息失败 "
 				return
 			}
 			edbInfoMappingList = append(edbInfoMappingList, edbInfoMapping)

+ 5 - 0
models/data_manage/chart_info.go

@@ -1268,7 +1268,12 @@ func EditFutureGoodChartInfoAndMapping(req *EditFutureGoodChartInfoReq, edbInfoI
 		return err
 	}
 	chartEdbMappingIdList := make([]string, 0)
+	hasAddMap := make(map[int]struct{})
 	for _, v := range req.BarChartInfo.EdbInfoIdList {
+		if _, ok := hasAddMap[v.EdbInfoId]; ok {
+			continue
+		}
+		hasAddMap[v.EdbInfoId] = struct{}{}
 		// 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
 		var tmpChartEdbMapping *ChartEdbMapping
 		csql := `SELECT *  FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `

+ 7 - 0
models/data_manage/excel/excel_classify.go

@@ -240,3 +240,10 @@ func GetAdminExcelClassifyBySource(source, adminId int) (items []*ExcelClassifyI
 	_, err = o.Raw(sql, source, adminId).QueryRows(&items)
 	return
 }
+
+func GetExcelClassifyModelBySource(source int) (items []*ExcelClassify, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT * FROM excel_classify WHERE source = ? AND is_delete = 0 ORDER BY sort ASC, excel_classify_id ASC`
+	_, err = o.Raw(sql, source).QueryRows(&items)
+	return
+}

+ 9 - 0
routers/commentsRouter.go

@@ -871,6 +871,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:CustomAnalysisCommonController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:CustomAnalysisCommonController"],
+        beego.ControllerComments{
+            Method: "FixClassify",
+            Router: `/excel/fix_classify`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:CustomAnalysisController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage/excel:CustomAnalysisController"],
         beego.ControllerComments{
             Method: "Add",

+ 1 - 0
routers/router.go

@@ -288,6 +288,7 @@ func init() {
 		web.NSNamespace("/custom_analysis",
 			web.NSInclude(
 				&excel.CustomAnalysisController{},
+				&excel.CustomAnalysisCommonController{},
 			),
 		),
 		web.NSNamespace("/out_link",

+ 1 - 1
services/data/edb_info.go

@@ -1949,7 +1949,7 @@ func EdbInfoAdd(source, subSource, classifyId int, edbCode, edbName, frequency,
 	edbInfo.EdbNameSource = edbName
 	edbInfo.Frequency = frequency
 	edbInfo.Unit = unit
-	edbInfo.UnitEn = frequency
+	edbInfo.UnitEn = unit
 	edbInfo.StartDate = startDate
 	edbInfo.EndDate = endDate
 	edbInfo.ClassifyId = classifyId