123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package services
- import (
- "encoding/json"
- "eta/eta_data_analysis/models"
- "eta/eta_data_analysis/utils"
- "fmt"
- "github.com/patrickmn/go-cache"
- "github.com/tealeg/xlsx"
- "io/fs"
- "os"
- "path/filepath"
- "strings"
- "syscall"
- "time"
- )
- func clarksonsWatch() {
- fmt.Println("clarksonsWatch start")
- var err error
- defer func() {
- if err != nil {
- fmt.Println("clarksonsWatch Err:" + err.Error())
- }
- }()
- var cacheClient *cache.Cache
- if cacheClient == nil {
- cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
- }
- err = filepath.Walk(utils.ClarkSonsFilePath, func(path string, info fs.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !info.IsDir() {
- fileInfo, err := os.Stat(path)
- if err != nil {
- fmt.Println("os.Stat:", err.Error())
- }
- winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
- modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
- existModifyTime, ok := cacheClient.Get(path)
- if ok {
- existModifyTimeStr := existModifyTime.(string)
- if existModifyTimeStr != modifyTimeStr {
- if strings.Contains(path, "克拉克森") {
- err = Clarksons(path)
- }
- }
- } else {
- if strings.Contains(path, "克拉克森") {
- err = Clarksons(path)
- }
- }
- cacheClient.Delete(path)
- cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
- }
- return nil
- })
- }
- func Clarksons(path string) (err error) {
- defer func() {
- if err != nil {
- fmt.Println("Clarksons Err:" + err.Error())
- utils.FileLog.Info(fmt.Sprintf("Clarksons, Err: %s", err))
- }
- }()
- path = "/Users/xi/Desktop/SIN_Timeseries_20241217022653.xlsx"
- var xlFile *xlsx.File
- exist, err := PathExists(path)
- if err != nil {
- fmt.Println(err)
- return
- }
- if exist {
- xlFile, err = xlsx.OpenFile(path)
- if err != nil {
- fmt.Println("OpenFile err:", err)
- return
- }
- } else {
- fmt.Println("Not Exist")
- return
- }
- sheetDatas := make([]models.SheetData, 0)
- for i, sheet := range xlFile.Sheets {
- if i > 0 {
- break
- }
- data := sheet
- sheetData := models.SheetData{
- Name: data.Name,
- MaxRow: data.MaxRow,
- MaxCol: data.MaxCol,
- Hidden: data.Hidden,
- Selected: data.Selected,
- }
- rows := make([]models.Row, 0)
- for _, v := range data.Rows {
- cells := make([]models.Cell, 0)
- for _, cell := range v.Cells {
- cells = append(cells, models.Cell{
- Value: cell.String(),
- })
- }
- row := models.Row{
- Cells: cells,
- }
- rows = append(rows, row)
- }
- sheetData.Rows = rows
- sheetDatas = append(sheetDatas, sheetData)
- }
- params := make(map[string]interface{})
- params["SheetData"] = sheetDatas
- result, e := PostEdbLib(params, utils.LIB_ROUTE_CLARKSONS)
- if e != nil {
- b, _ := json.Marshal(params)
- fmt.Println(e)
- utils.FileLog.Info(fmt.Sprintf("PostEdbLib err: %s, params: %s", e.Error(), string(b)))
- return
- }
- resp := new(models.BaseEdbLibResponse)
- if e := json.Unmarshal(result, &resp); e != nil {
- utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %s", e))
- return
- }
- if resp.Ret != 200 {
- utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
- return
- }
- return
- }
|