|
@@ -0,0 +1,380 @@
|
|
|
+package watch
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "hongze/mysteel_watch/global"
|
|
|
+ "hongze/mysteel_watch/utils"
|
|
|
+ "log"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/fsnotify/fsnotify"
|
|
|
+ "github.com/xuri/excelize/v2"
|
|
|
+)
|
|
|
+
|
|
|
+func ListenFolderNewV2() {
|
|
|
+ fmt.Println("-----文件夹监听-------")
|
|
|
+ watcher, err := fsnotify.NewWatcher()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("fsnotify.NewWatcher err:" + err.Error())
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ defer watcher.Close()
|
|
|
+
|
|
|
+ done2 := make(chan bool)
|
|
|
+ go func() {
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case event, ok := <-watcher.Events:
|
|
|
+ fmt.Println("event.Name", event.Name)
|
|
|
+ fmt.Println(event.Op)
|
|
|
+ if ok && event.Op == fsnotify.Create &&
|
|
|
+ !strings.Contains(event.Name, "tmp") &&
|
|
|
+ !strings.Contains(event.Name, ".TMP") &&
|
|
|
+ !strings.Contains(event.Name, "~") &&
|
|
|
+ (strings.Contains(event.Name, "xlsx") || strings.Contains(event.Name, "xls")) {
|
|
|
+ // 监听文件变更事件
|
|
|
+ WatchIndexFile(event.Name)
|
|
|
+ }
|
|
|
+ case err := <-watcher.Errors:
|
|
|
+ fmt.Println("watcher.Errors:", err)
|
|
|
+ log.Println("error:", err)
|
|
|
+ case <-time.After(60 * time.Second):
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ fmt.Println("watch dir:" + global.CONFIG.Serve.IndexSaveDir)
|
|
|
+ err = watcher.Add(global.CONFIG.Serve.IndexSaveDir)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("watcher.Add:" + err.Error())
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ <-done2
|
|
|
+}
|
|
|
+
|
|
|
+// ListenFolderNewMerge 生产合并文件夹监听
|
|
|
+func ListenFolderNewMergeV2() {
|
|
|
+ fmt.Println("-----生产合并文件夹监听-------")
|
|
|
+ watcher, err := fsnotify.NewWatcher()
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ defer watcher.Close()
|
|
|
+
|
|
|
+ done2 := make(chan bool)
|
|
|
+ go func() {
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case event, ok := <-watcher.Events:
|
|
|
+ if ok && (event.Op == fsnotify.Create || event.Op == fsnotify.Write) &&
|
|
|
+ !strings.Contains(event.Name, "tmp") &&
|
|
|
+ !strings.Contains(event.Name, ".TMP") &&
|
|
|
+ !strings.Contains(event.Name, "~") &&
|
|
|
+ (strings.Contains(event.Name, "xlsx") || strings.Contains(event.Name, "xls")) {
|
|
|
+ WatchIndexFileMergeRelease(event.Name)
|
|
|
+ }
|
|
|
+ case err := <-watcher.Errors:
|
|
|
+ log.Println("error:", err)
|
|
|
+ case <-time.After(60 * time.Second):
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ err = watcher.Add(global.CONFIG.Serve.IndexMergeSaveDir)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ <-done2
|
|
|
+}
|
|
|
+
|
|
|
+// WatchIndexFile 检测指标文件
|
|
|
+func WatchIndexFileV2(filePath string) {
|
|
|
+ fmt.Println("filePath:", filePath)
|
|
|
+ //filePath:D:\mysteel_data\CM0000568866_release.xlsx
|
|
|
+ time.Sleep(10 * time.Second)
|
|
|
+ if !utils.FileIsExist(filePath) {
|
|
|
+ fmt.Println("filePath is not exist:" + filePath)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //读取文件内容
|
|
|
+ global.LOG.Info("WatchFile:" + filePath)
|
|
|
+ f, err := excelize.OpenFile(filePath)
|
|
|
+ global.LOG.Info("OpenFile:" + filePath)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("OpenFile:" + filePath + ",Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var newFilePath string
|
|
|
+ defer func() {
|
|
|
+ if err := f.Close(); err != nil {
|
|
|
+ fmt.Println("FileClose Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //重命名文件
|
|
|
+ if filePath != newFilePath {
|
|
|
+ err := os.Rename(filePath, newFilePath)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("os.Rename Err:" + err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ reqList := make([]*HandleMysteelIndex, 0)
|
|
|
+
|
|
|
+ var wg = sync.WaitGroup{}
|
|
|
+ wg.Add(1)
|
|
|
+ go func() {
|
|
|
+ sheetList := f.GetSheetList()
|
|
|
+ for _, sv := range sheetList {
|
|
|
+
|
|
|
+ lenRow := 0 //指标数
|
|
|
+
|
|
|
+ // excel表的指标数据
|
|
|
+ indexExcelDataList := make([]map[string]string, 0)
|
|
|
+
|
|
|
+ indexNameMap := make(map[int]string)
|
|
|
+ indexCodeMap := make(map[int]string)
|
|
|
+ unitMap := make(map[int]string)
|
|
|
+ sourceMap := make(map[int]string)
|
|
|
+ frequencyMap := make(map[int]string)
|
|
|
+ startDateMap := make(map[int]string)
|
|
|
+ endDateMap := make(map[int]string)
|
|
|
+ describeMap := make(map[int]string)
|
|
|
+ updateDateMap := make(map[int]string)
|
|
|
+
|
|
|
+ rows, err := f.GetRows(sv)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetRows Err:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for row, cols := range rows {
|
|
|
+ if row == 0 {
|
|
|
+ // 第一行是 钢联数据的备注
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 指标名称
|
|
|
+ if row == 1 {
|
|
|
+ lenRow = len(cols) - 1
|
|
|
+ for i := 1; i <= lenRow; i++ {
|
|
|
+ tmpIndexExcelDataList := make(map[string]string, 0)
|
|
|
+ indexExcelDataList = append(indexExcelDataList, tmpIndexExcelDataList)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if row < 10 {
|
|
|
+ for k, colCell := range cols {
|
|
|
+ switch row {
|
|
|
+ case 1: //指标名称
|
|
|
+ indexNameMap[k-1] = colCell
|
|
|
+ case 2: //单位
|
|
|
+ unitMap[k-1] = colCell
|
|
|
+ case 3: //数据来源
|
|
|
+ sourceMap[k-1] = colCell
|
|
|
+ case 4: //指标编码
|
|
|
+ indexCodeMap[k-1] = colCell
|
|
|
+ case 5: //频度
|
|
|
+ tmpFrequency := colCell
|
|
|
+ if !strings.Contains(tmpFrequency, "度") {
|
|
|
+ tmpFrequency = tmpFrequency + "度"
|
|
|
+ }
|
|
|
+ frequencyMap[k-1] = tmpFrequency
|
|
|
+ case 6: //时间区间
|
|
|
+ dateArr := strings.Split(colCell, "~")
|
|
|
+ if len(dateArr) >= 2 {
|
|
|
+ startDateMap[k-1] = dateArr[0]
|
|
|
+ endDateMap[k-1] = dateArr[1]
|
|
|
+ }
|
|
|
+ case 7: //备注
|
|
|
+ describeMap[k-1] = colCell
|
|
|
+ case 9:
|
|
|
+ updateDateMap[k-1] = colCell
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ date := ``
|
|
|
+ for k, col := range cols {
|
|
|
+ if k == 0 {
|
|
|
+ date = col
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if date == `` {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if col != `` {
|
|
|
+ indexExcelDataList[k-1][date] = col
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, excelDataMap := range indexExcelDataList {
|
|
|
+ indexItem := new(HandleMysteelIndex)
|
|
|
+ indexItem.IndexName = indexNameMap[k]
|
|
|
+ indexItem.IndexCode = indexCodeMap[k]
|
|
|
+ indexItem.Unit = unitMap[k]
|
|
|
+ indexItem.Source = sourceMap[k]
|
|
|
+ indexItem.Frequency = frequencyMap[k]
|
|
|
+ indexItem.StartDate = startDateMap[k]
|
|
|
+ indexItem.EndDate = endDateMap[k]
|
|
|
+ indexItem.Describe = describeMap[k]
|
|
|
+ indexItem.UpdateDate = updateDateMap[k]
|
|
|
+ indexItem.ExcelDataMap = excelDataMap
|
|
|
+ reqList = append(reqList, indexItem)
|
|
|
+ //mysteelIndexHandle(runMode, indexNameMap[k], indexCodeMap[k], unitMap[k], sourceMap[k], frequencyMap[k], startDateMap[k], endDateMap[k], describeMap[k], updateDateMap[k], excelDataMap)
|
|
|
+ }
|
|
|
+
|
|
|
+ resp := new(HandleMysteelIndexReq)
|
|
|
+ resp.List = reqList
|
|
|
+ postHandleMysteelIndex(resp)
|
|
|
+ }
|
|
|
+ wg.Done()
|
|
|
+ }()
|
|
|
+ wg.Wait()
|
|
|
+}
|
|
|
+
|
|
|
+// WatchIndexFileMergeRelease 监听生产的合并excel文件的处理
|
|
|
+func WatchIndexFileMergeReleaseV2(filePath string) {
|
|
|
+ fmt.Println("filePath:", filePath)
|
|
|
+
|
|
|
+ time.Sleep(5 * time.Second)
|
|
|
+ if !utils.FileIsExist(filePath) {
|
|
|
+ fmt.Println("filePath is not exist:" + filePath)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //读取文件内容
|
|
|
+ global.LOG.Info("WatchFile:" + filePath)
|
|
|
+ f, err := excelize.OpenFile(filePath)
|
|
|
+ global.LOG.Info("OpenFile:" + filePath)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("OpenFile:" + filePath + ",Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err := f.Close(); err != nil {
|
|
|
+ fmt.Println("FileClose Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ //runMode := "release"
|
|
|
+ reqList := make([]*HandleMysteelIndex, 0)
|
|
|
+ var wg = sync.WaitGroup{}
|
|
|
+ wg.Add(1)
|
|
|
+ go func() {
|
|
|
+
|
|
|
+ sheetList := f.GetSheetList()
|
|
|
+ for _, sv := range sheetList {
|
|
|
+
|
|
|
+ lenRow := 0 //指标数
|
|
|
+
|
|
|
+ // excel表的指标数据
|
|
|
+ indexExcelDataList := make([]map[string]string, 0)
|
|
|
+
|
|
|
+ indexNameMap := make(map[int]string)
|
|
|
+ indexCodeMap := make(map[int]string)
|
|
|
+ unitMap := make(map[int]string)
|
|
|
+ sourceMap := make(map[int]string)
|
|
|
+ frequencyMap := make(map[int]string)
|
|
|
+ startDateMap := make(map[int]string)
|
|
|
+ endDateMap := make(map[int]string)
|
|
|
+ describeMap := make(map[int]string)
|
|
|
+ updateDateMap := make(map[int]string)
|
|
|
+
|
|
|
+ rows, err := f.GetRows(sv)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("GetRows Err:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for row, cols := range rows {
|
|
|
+ if row == 0 {
|
|
|
+ // 第一行是 钢联数据的备注
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 指标名称
|
|
|
+ if row == 1 {
|
|
|
+ lenRow = len(cols) - 1
|
|
|
+ for i := 1; i <= lenRow; i++ {
|
|
|
+ tmpIndexExcelDataList := make(map[string]string, 0)
|
|
|
+ indexExcelDataList = append(indexExcelDataList, tmpIndexExcelDataList)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if row < 10 {
|
|
|
+ for k, colCell := range cols {
|
|
|
+ switch row {
|
|
|
+ case 1: //指标名称
|
|
|
+ indexNameMap[k-1] = colCell
|
|
|
+ case 2: //单位
|
|
|
+ unitMap[k-1] = colCell
|
|
|
+ case 3: //数据来源
|
|
|
+ sourceMap[k-1] = colCell
|
|
|
+ case 4: //指标编码
|
|
|
+ indexCodeMap[k-1] = colCell
|
|
|
+ case 5: //频度
|
|
|
+ tmpFrequency := colCell
|
|
|
+ if !strings.Contains(tmpFrequency, "度") {
|
|
|
+ tmpFrequency = tmpFrequency + "度"
|
|
|
+ }
|
|
|
+ frequencyMap[k-1] = tmpFrequency
|
|
|
+ case 6: //时间区间
|
|
|
+ dateArr := strings.Split(colCell, "~")
|
|
|
+ if len(dateArr) >= 2 {
|
|
|
+ startDateMap[k-1] = dateArr[0]
|
|
|
+ endDateMap[k-1] = dateArr[1]
|
|
|
+ }
|
|
|
+ case 7: //备注
|
|
|
+ describeMap[k-1] = colCell
|
|
|
+ case 9:
|
|
|
+ updateDateMap[k-1] = colCell
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ date := ``
|
|
|
+ for k, col := range cols {
|
|
|
+ if k == 0 {
|
|
|
+ date = col
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if date == `` {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if col != `` {
|
|
|
+ indexExcelDataList[k-1][date] = col
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, excelDataMap := range indexExcelDataList {
|
|
|
+ indexItem := new(HandleMysteelIndex)
|
|
|
+ indexItem.IndexName = indexNameMap[k]
|
|
|
+ indexItem.IndexCode = indexCodeMap[k]
|
|
|
+ indexItem.Unit = unitMap[k]
|
|
|
+ indexItem.Source = sourceMap[k]
|
|
|
+ indexItem.Frequency = frequencyMap[k]
|
|
|
+ indexItem.StartDate = startDateMap[k]
|
|
|
+ indexItem.EndDate = endDateMap[k]
|
|
|
+ indexItem.Describe = describeMap[k]
|
|
|
+ indexItem.UpdateDate = updateDateMap[k]
|
|
|
+ indexItem.ExcelDataMap = excelDataMap
|
|
|
+ reqList = append(reqList, indexItem)
|
|
|
+ //mysteelIndexHandle(runMode, indexNameMap[k], indexCodeMap[k], unitMap[k], sourceMap[k], frequencyMap[k], startDateMap[k], endDateMap[k], describeMap[k], updateDateMap[k], excelDataMap)
|
|
|
+ }
|
|
|
+
|
|
|
+ resp := new(HandleMysteelIndexReq)
|
|
|
+ resp.List = reqList
|
|
|
+ postHandleMysteelIndex(resp)
|
|
|
+ }
|
|
|
+
|
|
|
+ wg.Done()
|
|
|
+ }()
|
|
|
+ wg.Wait()
|
|
|
+
|
|
|
+}
|