hsun 1 рік тому
батько
коміт
7b1f9e7488
2 змінених файлів з 163 додано та 4 видалено
  1. 1 3
      go.mod
  2. 162 1
      services/index.go

+ 1 - 3
go.mod

@@ -11,8 +11,8 @@ require (
 	github.com/go-redis/redis/v8 v8.11.5
 	github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
 	github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
-	github.com/olivere/elastic/v7 v7.0.32
 	github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
+	github.com/patrickmn/go-cache v2.1.0+incompatible
 	github.com/rdlucklib/rdluck_tools v1.0.3
 	github.com/robfig/cron/v3 v3.0.1
 	github.com/spf13/viper v1.15.0
@@ -31,7 +31,6 @@ require (
 	github.com/cespare/xxhash/v2 v2.1.2 // indirect
 	github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
-	github.com/garyburd/redigo v1.6.3 // indirect
 	github.com/gin-contrib/sse v0.1.0 // indirect
 	github.com/go-openapi/jsonpointer v0.19.5 // indirect
 	github.com/go-openapi/jsonreference v0.19.6 // indirect
@@ -55,7 +54,6 @@ require (
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
-	github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
 	github.com/pelletier/go-toml/v2 v2.0.6 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/richardlehane/mscfb v1.0.4 // indirect

+ 162 - 1
services/index.go

@@ -3,6 +3,7 @@ package services
 import (
 	"fmt"
 	"github.com/xuri/excelize/v2"
+	"hongze/hongtao3_watch/utils"
 	"strings"
 	"time"
 )
@@ -15,7 +16,7 @@ const (
 //var checkLock sync.RWMutex
 
 // ReadExcel 读取Excel
-func ReadExcel(filePath string) {
+func ReadExcelV2(filePath string) {
 	time.Sleep(5 * time.Second)
 	excelInfo, err := excelize.OpenFile(filePath)
 	if err != nil {
@@ -153,3 +154,163 @@ func ReadExcel(filePath string) {
 	// 调用指标库公共服务处理该数据
 	HandleExcelDataByEdbLib(dataMap, indexNameList, thirdIndexIdList, frequencyList, unitList, filePath)
 }
+
+// ReadExcel 读取Excel
+func ReadExcel(filePath string) {
+	var err error
+	defer func() {
+		if err != nil {
+			tips := fmt.Sprintf("ReadExcel, err: %s", err.Error())
+			fmt.Println(tips)
+		}
+	}()
+
+	time.Sleep(5 * time.Second)
+	excelInfo, e := excelize.OpenFile(filePath)
+	if e != nil {
+		err = fmt.Errorf("OpenFile err: %s", e.Error())
+		return
+	}
+	defer func() {
+		_ = excelInfo.Close()
+	}()
+	fmt.Println("读取excel:", filePath)
+
+	indexNameList := make([]string, 0)
+	thirdIndexIdList := make([]string, 0)
+	frequencyList := make([]string, 0)
+	unitList := make([]string, 0)
+	dataMap := make(map[string]map[string]string, 0)
+
+	type IndexItem struct {
+		IndexName string
+		IndexCode string
+		Frequency string
+		Unit      string
+		DataList  map[string]string
+	}
+	indexes := make([]*IndexItem, 0)
+
+	startName := "数据项名称"
+	sheetList := excelInfo.GetSheetList()
+	for _, sheet := range sheetList {
+		rows, e := excelInfo.GetRows(sheet)
+		if e != nil {
+			fmt.Println("GetRows: ", e.Error())
+			continue
+		}
+		if len(rows) <= 13 {
+			fmt.Println("错误的excel表")
+			continue
+		}
+
+		colIndexMap := make(map[int]*IndexItem) // 列对应的指标信息
+		colTitle := make(map[int]bool)          // 列对应的是否为表头
+		colDateCols := make(map[int][]int)      // 日期列对应的指标列集合
+		colYes := make(map[int]bool)            // 列是否对应指标列
+
+		// 遍历行
+		for rk, row := range rows {
+
+			// 遍历列
+			lastDate := "" // 上一个日期
+			for ck, col := range row {
+				col = strings.TrimSpace(col)
+
+				// 指标名称行
+				if rk == 0 {
+					// 读取到"数据项名称"或空时跳过
+					if col == startName {
+						colTitle[ck] = true
+						colDateCols[ck] = make([]int, 0)
+						continue
+					}
+					if col == "" {
+						continue
+					}
+					colYes[ck] = true
+					if colIndexMap[ck] == nil {
+						colIndexMap[ck] = new(IndexItem)
+					}
+					colIndexMap[ck].IndexName = col
+					colIndexMap[ck].DataList = make(map[string]string, 0)
+				}
+
+				// 指标ID
+				if rk == 1 && colYes[ck] {
+					colIndexMap[ck].IndexCode = col
+				}
+
+				// 频度
+				if rk == 8 && colYes[ck] {
+					if strings.Contains(col, "日度") {
+						colIndexMap[ck].Frequency = "日度"
+					} else if strings.Contains(col, "周度") {
+						colIndexMap[ck].Frequency = "周度"
+					} else if strings.Contains(col, "旬度") {
+						colIndexMap[ck].Frequency = "旬度"
+					} else if strings.Contains(col, "月度") {
+						colIndexMap[ck].Frequency = "月度"
+					} else if strings.Contains(col, "季度") {
+						colIndexMap[ck].Frequency = "季度"
+					} else if strings.Contains(col, "年度") {
+						colIndexMap[ck].Frequency = "年度"
+					} else {
+						colIndexMap[ck].Frequency = col
+					}
+				}
+
+				// 单位
+				if rk == 10 && colYes[ck] {
+					colIndexMap[ck].Unit = col
+				}
+
+				if rk <= 12 {
+					continue
+				}
+
+				// 标题列取日期
+				if colTitle[ck] {
+					if col == "" {
+						continue
+					}
+					_, e = time.Parse(utils.FormatDate, col)
+					if e != nil {
+						continue
+					}
+					lastDate = col
+				}
+				// 指标列-即值列
+				if colYes[ck] {
+					if col == "" || col == "--" || col == "#N/A" {
+						continue
+					}
+					// 取上一个日期列的日期
+					if lastDate != "" {
+						colIndexMap[ck].DataList[lastDate] = col
+					}
+				}
+			}
+		}
+
+		// 每一页读取后赋值给indexes
+		for _, v := range colIndexMap {
+			indexes = append(indexes, v)
+		}
+	}
+
+	// 转为指定格式返回
+	if len(indexes) == 0 {
+		return
+	}
+	for _, v := range indexes {
+		indexNameList = append(indexNameList, v.IndexName)
+		thirdIndexIdList = append(thirdIndexIdList, v.IndexCode)
+		frequencyList = append(frequencyList, v.Frequency)
+		unitList = append(unitList, v.Unit)
+		dataMap[v.IndexCode] = v.DataList
+	}
+
+	// 调用指标库公共服务处理该数据
+	HandleExcelDataByEdbLib(dataMap, indexNameList, thirdIndexIdList, frequencyList, unitList, filePath)
+}