瀏覽代碼

Merge branch 'bzq/excel_rule_bug' of eta_server/eta_chart_lib into debug

baoziqiang 2 月之前
父節點
當前提交
7f47af96a5
共有 3 個文件被更改,包括 75 次插入48 次删除
  1. 2 2
      controllers/excel_info.go
  2. 46 43
      services/excel/luck_sheet_table.go
  3. 27 3
      services/excel/lucky_sheet.go

+ 2 - 2
controllers/excel_info.go

@@ -151,7 +151,7 @@ func (this *ExcelInfoController) GetTableDetail() {
 			br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error()
 			return
 		}
-		tableData, err = excel.GetTableDataByMixedTableData(newResult, true)
+		tableData, err = excel.GetTableDataByMixedTableData(newResult, true, excelInfo.ExcelInfoId)
 		if err != nil {
 			br.Msg = "获取失败"
 			br.ErrMsg = "转换成table失败,Err:" + err.Error()
@@ -223,7 +223,7 @@ func (this *ExcelInfoController) GetTableDetail() {
 	}
 
 	tableData = excel.HandleTableCell(tableData)
-	tableData, err = excel.HandleRuleToTableCell(excelInfo.ExcelInfoId, tableData)
+	// tableData, err = excel.HandleRuleToTableCell(excelInfo.ExcelInfoId, tableData)
 	if err != nil {
 		utils.FileLog.Info("表格管理规则处理失败,HandleRuleToTableCell err:", err.Error())
 	}

+ 46 - 43
services/excel/luck_sheet_table.go

@@ -2,6 +2,7 @@ package excel
 
 import (
 	"eta/eta_chart_lib/models"
+	"eta/eta_chart_lib/models/request"
 	"eta/eta_chart_lib/utils"
 	"strconv"
 	"strings"
@@ -9,42 +10,42 @@ import (
 )
 
 // HandleRuleToTableCell 根据管理规则渲染单元格数据
-func HandleRuleToTableCell(excelInfoId int, oldTableData TableData) (newTableData TableData, err error) {
-	newTableData = oldTableData
-	excelRuleMappingList, err := models.GetExcelRuleMappingByExcelInfoId(excelInfoId)
-	if err != nil {
-		return
-	}
-	if len(excelRuleMappingList) == 0 {
-		return
-	}
-	tableDataList := oldTableData.TableDataList
-	excelRuleMap := make(map[int]*models.ExcelInfoRuleMappingView)
-	for _, v := range excelRuleMappingList {
-		excelRuleMap[v.ExcelInfoRuleMappingId] = v
-	}
-	ruleScopeMap := generateRuleScopeIndexMap(excelRuleMappingList)
-	for row, scopeValues := range ruleScopeMap {
-		for col, ruleId := range scopeValues {
-			if v, ok := excelRuleMap[ruleId]; ok {
-				if len(tableDataList) > row && len(tableDataList[row]) > col {
-					// 符合管理规则要求,则进行字体和背景颜色的渲染
-					if checkCellRule(v, tableDataList[row][col].Monitor, tableDataList) {
-						tableDataList[row][col].Background = v.BackgroundColor
-						tableDataList[row][col].FontColor = v.FontColor
-					}
-				} else {
-					continue
-				}
-			} else {
-				continue
-			}
-		}
-	}
-	return
-}
+// func HandleRuleToTableCell(excelInfoId int, oldTableData TableData) (newTableData TableData, err error) {
+// 	newTableData = oldTableData
+// 	excelRuleMappingList, err := models.GetExcelRuleMappingByExcelInfoId(excelInfoId)
+// 	if err != nil {
+// 		return
+// 	}
+// 	if len(excelRuleMappingList) == 0 {
+// 		return
+// 	}
+// 	tableDataList := oldTableData.TableDataList
+// 	excelRuleMap := make(map[int]*models.ExcelInfoRuleMappingView)
+// 	for _, v := range excelRuleMappingList {
+// 		excelRuleMap[v.ExcelInfoRuleMappingId] = v
+// 	}
+// 	ruleScopeMap := generateRuleScopeIndexMap(excelRuleMappingList)
+// 	for row, scopeValues := range ruleScopeMap {
+// 		for col, ruleId := range scopeValues {
+// 			if v, ok := excelRuleMap[ruleId]; ok {
+// 				if len(tableDataList) > row && len(tableDataList[row]) > col {
+// 					// 符合管理规则要求,则进行字体和背景颜色的渲染
+// 					if checkCellRule(v, tableDataList[row][col].Monitor, tableDataList) {
+// 						tableDataList[row][col].Background = v.BackgroundColor
+// 						tableDataList[row][col].FontColor = v.FontColor
+// 					}
+// 				} else {
+// 					continue
+// 				}
+// 			} else {
+// 				continue
+// 			}
+// 		}
+// 	}
+// 	return
+// }
 
-func getCellValueByType(value string, valueType int, tableDataList [][]LuckySheetDataValue) (float64, bool) {
+func getCellValueByType(value string, valueType int, tableDataList [][]request.MixedTableCellDataReq) (float64, bool) {
 	if valueType == 2 {
 		coords := strings.Split(value, ",")
 		var coordIntArr []int
@@ -54,7 +55,7 @@ func getCellValueByType(value string, valueType int, tableDataList [][]LuckyShee
 		}
 		if len(coordIntArr) == 2 {
 			x, y := coordIntArr[0]-1, coordIntArr[1]-1
-			conditionValue, err := strconv.ParseFloat(tableDataList[y][x].Monitor, 64)
+			conditionValue, err := strconv.ParseFloat(tableDataList[y][x].ShowValue, 64)
 			if err != nil {
 				return 0, false
 			}
@@ -70,7 +71,7 @@ func getCellValueByType(value string, valueType int, tableDataList [][]LuckyShee
 	return 0, false
 }
 
-func checkCellRule(ruleInfo *models.ExcelInfoRuleMappingView, value string, tableDataList [][]LuckySheetDataValue) bool {
+func checkCellRule(ruleInfo *models.ExcelInfoRuleMappingView, value string, tableDataList [][]request.MixedTableCellDataReq) bool {
 	var tableValue float64
 	var tableTime time.Time
 	var err error
@@ -195,8 +196,8 @@ func checkCellRule(ruleInfo *models.ExcelInfoRuleMappingView, value string, tabl
 	return false
 }
 
-func generateRuleScopeIndexMap(items []*models.ExcelInfoRuleMappingView) (ruleScopeMap map[int]map[int]int) {
-	ruleScopeMap = make(map[int]map[int]int)
+func generateRuleScopeIndexMap(items []*models.ExcelInfoRuleMappingView) (ruleScopeMap map[int]map[int][]int) {
+	ruleScopeMap = make(map[int]map[int][]int)
 	for _, item := range items {
 		coords := strings.Split(item.ScopeCoord, ",")
 		var coordIntArr []int
@@ -210,18 +211,20 @@ func generateRuleScopeIndexMap(items []*models.ExcelInfoRuleMappingView) (ruleSc
 			for i := ymin; i <= ymax; i++ {
 				for j := xmin; j <= xmax; j++ {
 					if _, ok := ruleScopeMap[i]; !ok {
-						ruleScopeMap[i] = make(map[int]int)
+						ruleScopeMap[i] = make(map[int][]int)
 					}
-					ruleScopeMap[i][j] = item.ExcelInfoRuleMappingId
+					ruleScopeMap[i][j] = append(ruleScopeMap[i][j], item.ExcelInfoRuleMappingId)
+					// ruleScopeMap[i][j] = item.ExcelInfoRuleMappingId
 				}
 			}
 		}
 		if len(coords) == 2 {
 			x, y := coordIntArr[0]-1, coordIntArr[1]-1
 			if _, ok := ruleScopeMap[y]; !ok {
-				ruleScopeMap[y] = make(map[int]int)
+				ruleScopeMap[y] = make(map[int][]int)
 			}
-			ruleScopeMap[y][x] = item.ExcelInfoRuleMappingId
+			ruleScopeMap[y][x] = append(ruleScopeMap[y][x], item.ExcelInfoRuleMappingId)
+			// ruleScopeMap[y][x] = item.ExcelInfoRuleMappingId
 		}
 	}
 	return

+ 27 - 3
services/excel/lucky_sheet.go

@@ -2,12 +2,14 @@ package excel
 
 import (
 	"encoding/json"
+	"eta/eta_chart_lib/models"
 	"eta/eta_chart_lib/models/request"
 	"eta/eta_chart_lib/utils"
 	"fmt"
 	"reflect"
 	"sort"
 	"strconv"
+	"strings"
 
 	"github.com/shopspring/decimal"
 )
@@ -1204,15 +1206,29 @@ func GetTableDataByCustomData(excelType int, data request.TableDataReq, lang str
 }
 
 // GetTableDataByMixedTableData 通过混合表格数据获取表格数据
-func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hideMerged bool) (selfTableData TableData, err error) {
+func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hideMerged bool, excelInfoId int) (selfTableData TableData, err error) {
 	tableDataList := make([][]LuckySheetDataValue, 0)
 	mergeList := make([]TableDataMerge, 0)
 
+	// 获取管理规则
+	excelRuleMappingList, err := models.GetExcelRuleMappingByExcelInfoId(excelInfoId)
+	if err != nil {
+		return
+	}
+	if len(excelRuleMappingList) == 0 {
+		return
+	}
+	excelRuleMap := make(map[int]*models.ExcelInfoRuleMappingView)
+	for _, v := range excelRuleMappingList {
+		excelRuleMap[v.ExcelInfoRuleMappingId] = v
+	}
+	ruleScopeMap := generateRuleScopeIndexMap(excelRuleMappingList)
+
 	// 开始文本行了
 	{
-		for _, row := range config {
+		for i, row := range config {
 			dataCol := make([]LuckySheetDataValue, 0)
-			for _, cell := range row {
+			for j, cell := range row {
 				tmp := LuckySheetDataValue{
 					Value:     cell.Value,
 					Monitor:   cell.ShowValue,
@@ -1328,6 +1344,14 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 
 					}
 				}
+				if ruleIds, ok := ruleScopeMap[i][j]; ok {
+					for _, ruleId := range ruleIds {
+						if checkCellRule(excelRuleMap[ruleId], cell.ShowValue, config) {
+							tmp.Background = strings.TrimSpace(excelRuleMap[ruleId].BackgroundColor)
+							tmp.FontColor = strings.TrimSpace(excelRuleMap[ruleId].FontColor)
+						}
+					}
+				}
 				dataCol = append(dataCol, tmp)
 			}
 			tableDataList = append(tableDataList, dataCol)