123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package services
- import (
- "fmt"
- "github.com/xuri/excelize/v2"
- "hongze/hongtao3_watch/utils"
- "strings"
- "time"
- )
- const (
- FormatTime = "15:04:05" //时间格式
- FormatTimeStr = "15:04" //时间格式
- )
- //var checkLock sync.RWMutex
- // ReadExcel 读取Excel
- func ReadExcelV2(filePath string) {
- time.Sleep(5 * time.Second)
- excelInfo, err := excelize.OpenFile(filePath)
- if err != nil {
- fmt.Println("OpenFile excel err:" + err.Error())
- return
- }
- fmt.Println("开始读取EXCEL了:", filePath)
- defer func() {
- excelInfo.Close()
- }()
- sheetList := excelInfo.GetSheetList()
- indexNameList := make([]string, 0)
- thirdIndexIdList := make([]string, 0)
- frequencyList := make([]string, 0)
- unitList := make([]string, 0)
- //indexIdList := make([]int, 0)
- dataMap := make(map[string]map[string]string, 0)
- for _, sheet := range sheetList {
- //fmt.Println(sheet)
- sheetThirdIndexIdList := make([]string, 0)
- rows, err := excelInfo.GetRows(sheet)
- if err != nil {
- fmt.Println("err:", err)
- continue
- }
- // excel异常
- if len(rows) <= 13 {
- fmt.Println("错误的excel表")
- continue
- }
- indexLen := len(rows[0])
- //fmt.Println(indexLen)
- // 没有指标
- if indexLen <= 1 {
- fmt.Println("没有指标")
- continue
- }
- for k, row := range rows {
- //fmt.Println(row)
- // 指标信息处理
- {
- // 指标名称
- if k == 0 {
- for colIndex, col := range row {
- if colIndex == 0 {
- continue
- }
- indexNameList = append(indexNameList, col)
- }
- }
- //第三方指标id
- if k == 1 {
- for colIndex, col := range row {
- if colIndex == 0 {
- continue
- }
- thirdIndexIdList = append(thirdIndexIdList, col)
- sheetThirdIndexIdList = append(sheetThirdIndexIdList, col)
- }
- }
- //频度
- if k == 8 {
- for colIndex, col := range row {
- if colIndex == 0 {
- continue
- }
- if strings.Contains(col, "日度") {
- frequencyList = append(frequencyList, "日度")
- } else if strings.Contains(col, "周度") {
- frequencyList = append(frequencyList, "周度")
- } else if strings.Contains(col, "旬度") {
- frequencyList = append(frequencyList, "旬度")
- } else if strings.Contains(col, "月度") {
- frequencyList = append(frequencyList, "月度")
- } else if strings.Contains(col, "季度") {
- frequencyList = append(frequencyList, "季度")
- } else if strings.Contains(col, "年度") {
- frequencyList = append(frequencyList, "年度")
- } else {
- frequencyList = append(frequencyList, col)
- }
- }
- }
- //单位
- if k == 10 {
- for colIndex, col := range row {
- if colIndex == 0 {
- continue
- }
- unitList = append(unitList, col)
- }
- }
- }
- if k <= 12 {
- continue
- }
- // 数据处理
- var currDate string
- for colIndex, col := range row {
- // 数据日期
- if colIndex == 0 {
- currDate = col
- continue
- }
- // 没有数据的情况
- if col == `` {
- continue
- }
- // 校验数据长度
- lenSheetThirdIndexIdList := len(sheetThirdIndexIdList)
- if lenSheetThirdIndexIdList != colIndex {
- fmt.Println("长度不一致,sheetThirdIndexIdList:", lenSheetThirdIndexIdList, ";colIndex:", colIndex)
- continue
- }
- thirdIndexId := sheetThirdIndexIdList[colIndex-1]
- if _, ok := dataMap[thirdIndexId]; !ok {
- tmpDataMap := make(map[string]string)
- dataMap[thirdIndexId] = tmpDataMap
- }
- dataMap[thirdIndexId][currDate] = col
- }
- }
- }
- // 调用指标库公共服务处理该数据
- 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)
- }
|