Browse Source

Merge branch 'bzq/6665_excel_bug' of eta_gn_server/eta_api into master

baoziqiang 2 weeks ago
parent
commit
abc8b24fca

+ 3 - 2
controllers/data_manage/excel/balance_table.go

@@ -16,13 +16,14 @@ import (
 	excel2 "eta_gn/eta_api/services/excel"
 	"eta_gn/eta_api/utils"
 	"fmt"
-	"github.com/tealeg/xlsx"
 	"io/ioutil"
 	"os"
 	"sort"
 	"strconv"
 	"strings"
 	"time"
+
+	"github.com/tealeg/xlsx"
 )
 
 // GetChildTable
@@ -1397,7 +1398,7 @@ func downloadBalanceTable(excelInfo *excel.ExcelInfo, lang string) (savePath, zi
 				errMsg = msg
 				return
 			}
-			tableData, er := excel2.GetTableDataByMixedTableData(newResult, false)
+			tableData, er := excel2.GetTableDataByMixedTableData(newResult, false, childExcelInfo.ExcelInfoId)
 			if er != nil {
 				errMsg = "获取失败"
 				err = fmt.Errorf("转换成table失败,Err:" + err.Error())

+ 8 - 9
controllers/data_manage/excel/excel_info.go

@@ -1635,7 +1635,7 @@ func (c *ExcelInfoController) GetExcelTableData() {
 			br.ErrMsg = "获取最新的数据失败,Err:" + err.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()
@@ -1664,7 +1664,7 @@ func (c *ExcelInfoController) GetExcelTableData() {
 	}
 
 	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())
 	}
@@ -2643,7 +2643,7 @@ func (c *ExcelInfoController) Download() {
 			br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
 			return
 		}
-		tableData, err = excel.GetTableDataByMixedTableData(newResult, false)
+		tableData, err = excel.GetTableDataByMixedTableData(newResult, false, excelInfo.ExcelInfoId)
 		if err != nil {
 			br.Msg = "获取失败"
 			br.ErrMsg = "转换成table失败,Err:" + err.Error()
@@ -3225,12 +3225,11 @@ func (c *ExcelInfoController) EditExcelRule() {
 		br.Msg = "应用选区不能为空"
 		return
 	}
-	if req.FontColor == "" {
-		br.Msg = "字体颜色不能为空"
-		return
-	}
-	if req.BackgroundColor == "" {
-		br.Msg = "背景颜色不能为空"
+
+	req.FontColor = strings.TrimSpace(req.FontColor)
+	req.BackgroundColor = strings.TrimSpace(req.BackgroundColor)
+	if req.FontColor == "" && req.BackgroundColor == "" {
+		br.Msg = "字体颜色和背景颜色不能同时为空"
 		return
 	}
 	if req.RuleType == 3 && req.RightValue == "" {

+ 5 - 0
services/data/excel/excel_info.go

@@ -980,6 +980,11 @@ func GetExcelRuleList(excelInfoId int) (resp *response.ExcelRuleListResp, err er
 	if err != nil {
 		return
 	}
+	for _, v := range excelInfoList {
+		v.BackgroundColor = strings.TrimSpace(v.BackgroundColor)
+		v.FontColor = strings.TrimSpace(v.FontColor)
+		resp.List = append(resp.List, v)
+	}
 	resp.List = excelInfoList
 	return
 }

+ 25 - 6
services/excel/lucky_sheet.go

@@ -3,6 +3,7 @@ package excel
 import (
 	"encoding/json"
 	"errors"
+	"eta_gn/eta_api/models/data_manage/excel"
 	"eta_gn/eta_api/models/data_manage/excel/request"
 	"eta_gn/eta_api/utils"
 	"fmt"
@@ -1719,15 +1720,25 @@ 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 := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
+	if err != nil {
+		return
+	}
+	excelRuleMap := make(map[int]*excel.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,
@@ -1751,10 +1762,10 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 					}
 					showFormatValue := fmt.Sprintf("%v", cell.ShowFormatValue)
 					if styleConfig.BackgroundColor != "" {
-						tmp.Background = styleConfig.BackgroundColor
+						tmp.Background = strings.TrimSpace(styleConfig.BackgroundColor)
 					}
 					if styleConfig.Color != "" {
-						tmp.FontColor = styleConfig.Color
+						tmp.FontColor = strings.TrimSpace(styleConfig.Color)
 					}
 					switch styleConfig.Align {
 					case "center":
@@ -1808,8 +1819,16 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 							tmp.Monitor = cell.Value
 						}
 					}
-
 				}
+				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)

+ 46 - 43
services/excel/lucky_sheet_table.go

@@ -2,6 +2,7 @@ package excel
 
 import (
 	"eta_gn/eta_api/models/data_manage/excel"
+	"eta_gn/eta_api/models/data_manage/excel/request"
 	"eta_gn/eta_api/utils"
 	"fmt"
 	"sort"
@@ -106,42 +107,42 @@ func handleCellVal(tmpTableColData LuckySheetDataValue) (valueStr string) {
 }
 
 // HandleRuleToTableCell 根据管理规则渲染单元格数据
-func HandleRuleToTableCell(excelInfoId int, oldTableData TableData) (newTableData TableData, err error) {
-	newTableData = oldTableData
-	excelRuleMappingList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
-	if err != nil {
-		return
-	}
-	if len(excelRuleMappingList) == 0 {
-		return
-	}
-	tableDataList := oldTableData.TableDataList
-	excelRuleMap := make(map[int]*excel.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 := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
+// 	if err != nil {
+// 		return
+// 	}
+// 	if len(excelRuleMappingList) == 0 {
+// 		return
+// 	}
+// 	tableDataList := oldTableData.TableDataList
+// 	excelRuleMap := make(map[int]*excel.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
@@ -151,7 +152,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
 			}
@@ -167,7 +168,7 @@ func getCellValueByType(value string, valueType int, tableDataList [][]LuckyShee
 	return 0, false
 }
 
-func checkCellRule(ruleInfo *excel.ExcelInfoRuleMappingView, value string, tableDataList [][]LuckySheetDataValue) bool {
+func checkCellRule(ruleInfo *excel.ExcelInfoRuleMappingView, value string, tableDataList [][]request.MixedTableCellDataReq) bool {
 	var tableValue float64
 	var tableTime time.Time
 	var err error
@@ -292,8 +293,8 @@ func checkCellRule(ruleInfo *excel.ExcelInfoRuleMappingView, value string, table
 	return false
 }
 
-func generateRuleScopeIndexMap(items []*excel.ExcelInfoRuleMappingView) (ruleScopeMap map[int]map[int]int) {
-	ruleScopeMap = make(map[int]map[int]int)
+func generateRuleScopeIndexMap(items []*excel.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
@@ -307,18 +308,20 @@ func generateRuleScopeIndexMap(items []*excel.ExcelInfoRuleMappingView) (ruleSco
 			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