123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 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))
- }
- }
|