123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- package services
- import (
- "encoding/json"
- "fmt"
- "hongze/mysteel_watch/cache"
- "hongze/mysteel_watch/global"
- "hongze/mysteel_watch/services/alarm_msg"
- "hongze/mysteel_watch/utils"
- "hongze/mysteel_watch/watch"
- "os"
- "strconv"
- "strings"
- "sync"
- "time"
- "github.com/xuri/excelize/v2"
- )
- const (
- FormatTime = "15:04:05" //时间格式
- FormatTimeStr = "15:04" //时间格式
- )
- var checkLock sync.RWMutex
- func IndexCreateCheck() (err error) {
- checkLock.Lock()
- defer func() {
- checkLock.Unlock()
- }()
- fmt.Println("IndexCreateCheck")
- //indexObj := new(index.BaseFromMysteelChemicalIndex)
- //list, err := indexObj.GetIndexCreate()
- list, err := watch.GetIndexCreate()
- if err != nil {
- return
- }
- fmt.Println("listLen:", len(list))
- time.Sleep(5 * time.Second)
- if len(list) <= 0 {
- return nil
- }
- var indexCode []string
- for _, v := range list {
- indexCode = append(indexCode, v.IndexCode)
- }
- indexCodeStr := strings.Join(indexCode, ";")
- fmt.Println("indexCodeStr:" + indexCodeStr)
- //go alarm_msg.SendAlarmMsg(utils.APPNAME+" 存在指标数据未生成:"+indexCodeStr, 3)
- for _, v := range list {
- fmt.Println("IndexCreateCheck start:", v.IndexCode, v.FilePath)
- if v.FilePath != "" && utils.FileIsExist(v.FilePath) {
- fmt.Println("Refresh:" + v.IndexCode)
- err = UpdateComment(v.FilePath)
- if err != nil {
- fmt.Println("UpdateComment Err:" + err.Error())
- go alarm_msg.SendAlarmMsg(utils.APPNAME+" 指标数据未生成检测失败:"+err.Error()+";file:"+v.FilePath, 3)
- }
- time.Sleep(1 * time.Second)
- //MysteelChemicalRefresh(v.FilePath)
- AddIndexRefreshToLpush(v.FilePath)
- } else {
- fmt.Println("IndexCreate:" + v.IndexCode)
- //saveFilePath, err := IndexCreate(v)
- saveFilePath, err := IndexCreate(v.UpdateWeek, v.IndexCode)
- if err != nil {
- fmt.Println("IndexCreate Err:" + err.Error())
- go alarm_msg.SendAlarmMsg(utils.APPNAME+" 指标数据未生成检测失败:"+err.Error(), 3)
- }
- v.FilePath = saveFilePath
- fmt.Println("IndexCreate saveFilePath:" + v.FilePath)
- time.Sleep(1 * time.Second)
- if utils.FileIsExist(saveFilePath) {
- //MysteelChemicalRefresh(saveFilePath)
- AddIndexRefreshToLpush(saveFilePath)
- }
- fmt.Println("MysteelChemicalRefresh end:" + v.IndexCode)
- }
- fmt.Println("IndexCreateCheck end:" + v.IndexCode)
- }
- return nil
- }
- // func IndexCreate(item *index.BaseFromMysteelChemicalIndex) (saveFilePath string, err error) {
- func IndexCreate(updateWeek, indexCode string) (saveFilePath string, err error) {
- updateWeek = utils.GetUpdateWeekEn(updateWeek)
- global.LOG.Info("task IndexCreate:" + time.Now().Format(utils.FormatDateTime))
- runMode := "release"
- //fileName := req.IndexName + "_" + req.IndexCode + ".xlsx"
- var fileName string
- if updateWeek != "" {
- fileName = indexCode + "_" + updateWeek + "_" + runMode + ".xlsx" //保存的文件名称
- } else {
- fileName = indexCode + "_" + runMode + ".xlsx" //保存的文件名称
- }
- filePath := global.CONFIG.Serve.IndexSaveDir + fileName
- if utils.FileIsExist(filePath) {
- saveFilePath = filePath
- return
- os.Remove(filePath)
- }
- templatePath := global.CONFIG.Serve.IndexSaveDir + "index_template.xlsx"
- templateFile, err := excelize.OpenFile(templatePath)
- if err != nil {
- fmt.Println("OpenFile template err:" + err.Error())
- return
- }
- defer func() {
- templateFile.Close()
- }()
- startDate := "1990-01-01"
- commentStr := `"BlankValue":"0","CanMark":true,"ChartLineType":"0","DateBlock":0,"DateBlockCount":1,"DateFormat":0,"DateTimeTag":"","EndDate":"","ExportType":0,"HasDescription":true,"HasEmptyRows":false,"HasFrequency":true,"HasIndexID":true,"HasLastDate":true,"HasSourceName":true,"HasTimeInterval":true,"HasUnit":true,"HasUpdateDate":true,"IsCreateChart":false,"IsDataSort":true,"IsNewSheet":false,"IsNewWorkbook":false,"Models":[{"DataFormat":0,"DataStartDate":"` + startDate + `","DefineName":"","DefineUnit":"","DisplayIndexCode":"` + indexCode + `","IndexCode":"` + indexCode + `","IndexFormula":"` + indexCode + `","PointValue":0,"UnionStart":""}],"Position":"A1","RangeData":"A2:B280","ShowBlankLines":false,"StartDate":"","Transpose":false,"UpdateMode":1,"lookModel":{"IsLast":false,"LookValue":0,"lookType":0},"ver":3}
- `
- commentMap := make(map[string]interface{})
- commentMap["author"] = "{"
- commentMap["text"] = commentStr
- commentJson, err := json.Marshal(commentMap)
- if err != nil {
- fmt.Println("json.Marshal err:" + err.Error())
- }
- fmt.Println("commentJson")
- fmt.Println(string(commentJson))
- templateFile.DeleteComment("Sheet1", "A1")
- templateFile.AddComment("Sheet1", "A1", string(commentJson))
- if err := templateFile.SaveAs(filePath); err != nil {
- fmt.Println(err)
- return "", err
- }
- saveFilePath = filePath
- return
- }
- //func IndexRefreshAll() {
- // fmt.Println("IndexCreateCheck")
- // indexObj := new(index.BaseFromMysteelChemicalIndex)
- // list, err := indexObj.GetIndexRefreshAll()
- // if err != nil {
- // fmt.Println("GetIndexRefreshAll Err:" + err.Error())
- // return
- // }
- // fmt.Println("listLen:", len(list))
- // if len(list) <= 0 {
- // return
- // }
- // nowDate := time.Now().Format(utils.FormatDate)
- // monthLastDay := utils.GetNowMonthLastDay()
- // monthLastDayStr := monthLastDay.Format(utils.FormatDate)
- // yearLast := strconv.Itoa(time.Now().Year()) + "12-31"
- // for _, v := range list {
- // err = UpdateComment(v.FilePath)
- // if err != nil {
- // go alarm_msg.SendAlarmMsg(utils.APPNAME+" 指标数据未生成检测失败:"+err.Error()+";file:"+v.FilePath, 3)
- // }
- // time.Sleep(1 * time.Second)
- // if v.Frequency == "周度" {
- // if v.UpdateWeek == "" && v.UpdateTime == "" && v.UpdateTime2 == "" {
- // //判断本周的数据是否已经存在
- // MysteelChemicalRefresh(v.FilePath)
- // }
- // } else if v.Frequency == "月度" || v.Frequency == "旬度" {
- // if nowDate == monthLastDayStr {
- // MysteelChemicalRefresh(v.FilePath)
- // }
- // } else if v.Frequency == "年度" && nowDate == yearLast {
- // if nowDate == yearLast {
- // MysteelChemicalRefresh(v.FilePath)
- // }
- // } else if v.Frequency == "日度" {
- // if v.EndDate.Format(utils.FormatDate) != nowDate {
- // MysteelChemicalRefresh(v.FilePath)
- // }
- // } else {
- // global.LOG.Info("无效频度:" + v.IndexCode + ";" + v.Frequency)
- // }
- // }
- // return
- //}
- func IndexRefreshAll() {
- fmt.Println("IndexCreateCheck")
- //indexObj := new(index.BaseFromMysteelChemicalIndex)
- //list, err := indexObj.GetIndexRefreshAllByMergeFile()
- frequency := global.CONFIG.Serve.Frequency
- list, err := watch.GetIndexRefreshAllByMergeFile()
- if err != nil {
- fmt.Println("GetIndexRefreshAll Err:" + err.Error())
- return
- }
- fmt.Println("listLen:", len(list))
- if len(list) <= 0 {
- return
- }
- now := time.Now()
- month := int(now.Month())
- day := now.Day()
- week := int(now.Weekday())
- for _, v := range list {
- rn := utils.GetRandInt(1, 10)
- time.Sleep(time.Duration(rn) * time.Second)
- if v.Frequency == "年度" {
- if month == 1 && day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "季度" {
- if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "月度" {
- if day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "周度" && frequency == "周度" {
- if week > 2 && week < 6 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else {
- if week < 6 && frequency == "" {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- }
- }
- return
- }
- func IndexRefreshMethanol() {
- fmt.Println("IndexRefreshMethanol")
- //indexObj := new(index.BaseFromMysteelChemicalIndex)
- //list, err := indexObj.GetIndexRefreshMethanolByMergeFile()
- list, err := watch.GetIndexRefreshMethanolByMergeFile()
- if err != nil {
- fmt.Println("GetIndexRefreshAll Err:" + err.Error())
- return
- }
- fmt.Println("listLen:", len(list))
- if len(list) <= 0 {
- return
- }
- now := time.Now()
- month := int(now.Month())
- day := now.Day()
- week := int(now.Weekday())
- for _, v := range list {
- rn := utils.GetRandInt(1, 10)
- time.Sleep(time.Duration(rn) * time.Second)
- time.Sleep(3 * time.Second)
- if v.Frequency == "年度" {
- if month == 1 && day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "季度" {
- if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "月度" {
- if day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "周度" {
- if week > 2 && week < 6 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else {
- if week < 6 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- }
- }
- return
- }
- func IndexRefreshTimely() {
- fmt.Println("IndexRefreshTimely")
- listLen := 0
- defer func() {
- if listLen > 0 {
- go alarm_msg.SendAlarmMsg(utils.APPNAME+" 及时刷新指标, listLen: "+strconv.Itoa(listLen), 3)
- }
- }()
- //indexObj := new(index.BaseFromMysteelChemicalIndex)
- //list, err := indexObj.GetIndexRefreshMethanolByTimely()
- list, err := watch.GetIndexRefreshMethanolByTimely()
- if err != nil {
- fmt.Println("GetIndexRefreshAll Err:" + err.Error())
- return
- }
- listLen = len(list)
- fmt.Println("listLen:", len(list))
- if listLen <= 0 {
- return
- }
- now := time.Now()
- month := int(now.Month())
- day := now.Day()
- week := int(now.Weekday())
- for _, v := range list {
- rn := utils.GetRandInt(1, 10)
- time.Sleep(time.Duration(rn) * time.Second)
- time.Sleep(3 * time.Second)
- if v.Frequency == "年度" {
- if month == 1 && day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "季度" {
- if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "月度" {
- if day == 1 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else if v.Frequency == "周度" {
- if week > 2 && week < 6 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- } else {
- if week < 6 {
- //MysteelChemicalRefresh(v.MergeFilePath)
- AddIndexRefreshToLpush(v.MergeFilePath)
- }
- }
- }
- return
- }
- var lock sync.RWMutex
- // 刷新周度指标数据
- func IndexRefreshWeek() {
- lock.Lock()
- nowWeek := time.Now().Weekday().String()
- nowWeekZn := utils.GetWeekZn(nowWeek)
- fmt.Println("nowWeekZn:" + nowWeekZn)
- //indexObj := new(index.BaseFromMysteelChemicalIndex)
- startTime := time.Now().Add(-1 * time.Minute).Format(FormatTimeStr)
- endTime := time.Now().Add(1 * time.Minute).Format(FormatTimeStr)
- //list, err := indexObj.GetIndexRefreshWeek(nowWeekZn, startTime, endTime)
- list, err := watch.GetIndexRefreshWeek(nowWeekZn, startTime, endTime)
- if err != nil {
- fmt.Println("GetIndexRefreshWeek Err:" + err.Error())
- return
- }
- fmt.Println("listLen:", len(list))
- if len(list) <= 0 {
- return
- }
- for _, v := range list {
- if global.Re == nil && global.Rc != nil {
- key := utils.REFRESH_INDEX_CODE + v.IndexCode
- if global.Rc.SetNX(key, v.FilePath, 5*time.Hour) {
- cache.IndexAutoRefresh(v.FilePath)
- }
- }
- }
- lock.Unlock()
- return
- }
- // 修改指标备注指标
- func UpdateComment(filePath string) (err error) {
- templateFile, err := excelize.OpenFile(filePath)
- if err != nil {
- fmt.Println("OpenFile Err:" + err.Error())
- return
- }
- defer func() {
- templateFile.Close()
- }()
- commentArr := templateFile.GetComments()
- for k, v := range commentArr {
- fmt.Println(k, v)
- for _, sv := range v {
- fmt.Println("text:", sv.Text)
- if strings.Contains(sv.Text, `"UpdateMode":0`) {
- newText := strings.Replace(sv.Text, `"UpdateMode":0`, `"UpdateMode":1`, -1)
- newText = strings.Trim(newText, "{")
- commentMap := make(map[string]interface{})
- commentMap["author"] = "{"
- commentMap["text"] = newText
- commentJson, err := json.Marshal(commentMap)
- if err != nil {
- fmt.Println("json.Marshal err:" + err.Error())
- }
- fmt.Println("add text:" + string(commentJson))
- templateFile.DeleteComment("Sheet1", "A1")
- err = templateFile.AddComment("Sheet1", "A1", string(commentJson))
- if err != nil {
- fmt.Println("AddComment Err:" + err.Error())
- return err
- }
- }
- }
- }
- if err := templateFile.SaveAs(filePath); err != nil {
- fmt.Println("templateFile.SaveAs Err:", err)
- return err
- }
- return
- }
- // 获取指标备注指标
- func GetComment(filePath string) string {
- excelFile, err := excelize.OpenFile(filePath)
- if err != nil {
- fmt.Println("OpenFile Err:" + err.Error())
- return ""
- }
- defer func() {
- excelFile.Close()
- }()
- commentArr := excelFile.GetComments()
- for _, v := range commentArr {
- //fmt.Println(k, v)
- for _, sv := range v {
- //fmt.Println("text:", sv.Text)
- if sv.Text != "" {
- return sv.Text
- }
- }
- }
- return ""
- }
- var pushLock sync.RWMutex
- // 刷新周度指标数据
- func AddIndexRefreshToLpush(filePath string) {
- pushLock.Lock()
- result := cache.IndexAutoRefresh(filePath)
- fmt.Println("IndexAutoRefresh result:", result)
- pushLock.Unlock()
- return
- }
|