|
@@ -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)
|
|
|
+}
|