123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- package services
- import (
- "encoding/json"
- "eta/mysteel_watch/cache"
- "eta/mysteel_watch/global"
- "eta/mysteel_watch/services/alarm_msg"
- "eta/mysteel_watch/utils"
- "eta/mysteel_watch/watch"
- "fmt"
- "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) {
- os.Remove(v.FilePath)
- }
- fmt.Println("IndexCreate:" + v.IndexCode)
- 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) {
- AddIndexRefreshToLPush(saveFilePath)
- }
- fmt.Println("MysteelChemicalRefresh 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()
- }()
- sheetList := templateFile.GetSheetList()
- for k, v := range sheetList {
- if k > 0 {
- templateFile.DeleteSheet(v)
- }
- }
- 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.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
- }
- // 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
- }
- func IndexUpdateCheck() {
- fmt.Println("开始检测")
- //day:周一至周六
- //week,周三至周五
- //month:每月月中和月底
- //season:每个季度的下一个月,月中和月底
- //year:每个月月初
- // 频度:日度,周度,月度,季度,年度
- frequencyBatch := []string{"日度"}
- //today, _ := time.Parse(utils.FormatDate, "2023-09-01")
- today := time.Now()
- todayDate := today.Format(utils.FormatDate)
- // 是否是周度指标更新的日子
- if today.Weekday() >= 3 {
- frequencyBatch = append(frequencyBatch, "周度")
- }
- // 判断是否是月度指标更新的日子
- /*tmpT, _ := time.ParseInLocation(utils.FormatDate, today.Format(utils.FormatYearMonth)+"-01", time.Local)
- lastDate := tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
- if lastDate == todayDate {
- frequencyBatch = append(frequencyBatch, "月度")
- }
- //判断是否是季度指标更新的日子
- if lastDate == todayDate && today.Month()%3 == 0 {
- frequencyBatch = append(frequencyBatch, "季度")
- }
- //判断是否是年度指标更新的日子
- tmpT, _ = time.ParseInLocation(utils.FormatDate, today.Format(utils.FormatYearMonth)+"-01", time.Local)
- if tmpT.Format(utils.FormatDate) == todayDate {
- frequencyBatch = append(frequencyBatch, "年度")
- }*/
- var req watch.RefreshCheckByDayReq
- req.Source = 34
- req.LatestDate = todayDate
- req.FrequencyBatch = strings.Join(frequencyBatch, ",")
- ret, err := watch.RefreshCheckByDay(req)
- if err != nil {
- global.LOG.Error(utils.APPNAME + " 钢联指标更新检查 出错" + time.Now().Format("2006-01-02 15:04:05") + ";Err:" + err.Error())
- go alarm_msg.SendAlarmMsg(utils.APPNAME+" 钢联指标更新检查 出错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
- return
- }
- fmt.Println(ret.UnUpdateNum)
- fmt.Println(ret.UpdateNum)
- halfNum := ret.UpdateNum / 2
- if ret.UnUpdateNum > halfNum {
- tip := fmt.Sprintf("%s 钢联指标更新检查:更新数据过少; %s :已更新指标个数:%d, 未更新指标个数%d", utils.APPNAME, todayDate, ret.UpdateNum, ret.UnUpdateNum)
- global.LOG.Info(tip)
- go alarm_msg.SendAlarmMsg(tip, 3)
- return
- }
- return
- }
|