package services import ( "encoding/json" "fmt" "hongze/hongqi_watch/cache" "hongze/hongqi_watch/global" "hongze/hongqi_watch/utils" "strings" "sync" "time" "github.com/xuri/excelize/v2" ) type SciHqExcel struct { IndexName string ExcelIndexCode string Frequency string Unit string TerminalCode string FilePath string Data map[string]string } // pushLock excel路径写入的读写锁 var pushLock sync.RWMutex // AddIndexRefreshToLPush 添加到指标刷新 func AddIndexRefreshToLPush(filePath string) { pushLock.Lock() //result := cache.IndexAutoRefresh(filePath) cache.IndexAutoRefresh(filePath) //fmt.Println("IndexAutoRefresh result:", result) pushLock.Unlock() return } var dateFormats = []string{"01-02-06", utils.FormatDate} func ReadHqExcel(filePath string) { // time.Sleep(time.Second) excelInfo, err := excelize.OpenFile(filePath) if err != nil { fmt.Println("OpenFile excel err:" + err.Error()) return } fmt.Println("开始读取EXCEL了") defer func() { excelInfo.Close() }() sheetList := excelInfo.GetSheetList() excelInfoList := make([]*SciHqExcel, 0) for _, sheet := range sheetList { rows, err := excelInfo.GetRows(sheet) if err != nil { fmt.Println("err:", err) continue } if len(rows) < 6 { fmt.Println("错误的excel表") continue } indexLen := len(rows[0]) // 没有指标 if indexLen <= 1 { fmt.Println("没有指标") continue } for col := 1; col < indexLen; col++ { indexInfo := new(SciHqExcel) dataLen := len(rows) if rows[0][0] != "数据名称" || rows[1][0] != "数据项ID" || rows[2][0] != "发布周期" || rows[3][0] != "单位" || rows[4][0] != "频率" { fmt.Println("sheet错误,ErrSheetName:", sheet) break } else { indexInfo.IndexName = rows[0][col] indexInfo.ExcelIndexCode = rows[1][col] if len(rows[3]) <= col { indexInfo.Unit = "无" } else { indexInfo.Unit = rows[3][col] } if strings.Contains(rows[4][col], "日度") { indexInfo.Frequency = "日度" } else if strings.Contains(rows[4][col], "周度") { indexInfo.Frequency = "周度" } else if strings.Contains(rows[4][col], "旬度") { indexInfo.Frequency = "旬度" } else if strings.Contains(rows[4][col], "月度") { indexInfo.Frequency = "月度" } else if strings.Contains(rows[4][col], "季度") { indexInfo.Frequency = "季度" } else if strings.Contains(rows[4][col], "年度") { indexInfo.Frequency = "年度" } else { indexInfo.Frequency = "周度" } indexInfo.Frequency = rows[4][col] indexInfo.Data = make(map[string]string) indexInfo.FilePath = filePath indexInfo.TerminalCode = global.CONFIG.Serve.TerminalCode } for row := 5; row < dataLen; row++ { var newTime time.Time var tmpErr error for _, v := range dateFormats { newTime, tmpErr = time.Parse(v, rows[row][0]) if !newTime.IsZero() { break } if tmpErr != nil { continue } } if newTime.IsZero() { fmt.Println("日期类型错误,ErrDate:", rows[row][0]) continue } // 没有数据 if len(rows[row]) <= col { continue } if rows[row][col] == "" { continue } indexInfo.Data[newTime.Format(utils.FormatDate)] = rows[row][col] } excelInfoList = append(excelInfoList, indexInfo) } } resp, err := HandleHqExcelDataByEdbLib(excelInfoList) if err != nil { global.LOG.Info("调用指标库公共服务处理数据失败,err:" + err.Error()) return } if resp.Ret != 200 { b, err := json.Marshal(resp) if err != nil { fmt.Println("json.Marshal err:" + err.Error()) return } fmt.Println("调用指标库公共服务处理数据失败, return:" + string(b)) global.LOG.Info("调用指标库公共服务处理数据失败,return:" + string(b)) } }