package services import ( "eta/mysteel_watch/global" "eta/mysteel_watch/utils" "eta/mysteel_watch/watch" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "github.com/xuri/excelize/v2" "os" "strconv" "time" ) const ( IndexPageSize = 100 //单个Sheet页,指标个数 ) // MergeDayAndWeek 合并处理日度和周度的指标 func MergeDayAndWeek() { fmt.Println("MergeDayAndWeek start") now := time.Now() week := int(now.Weekday()) dayArr := []int{1, 2, 3, 4, 5, 6} // 日度默认1-6 weekArr := []int{3, 4, 5, 6} // 周度默认3-6 // 有配置则取配置中的时间 if len(global.CONFIG.Serve.DayRefreshWeekdays) > 0 { dayArr = global.CONFIG.Serve.DayRefreshWeekdays } if len(global.CONFIG.Serve.WeekRefreshWeekdays) > 0 { weekArr = global.CONFIG.Serve.WeekRefreshWeekdays } //日度 if utils.InArrayByInt(dayArr, week) { IndexDayMergeV2() } time.Sleep(1 * time.Minute) //周度 if utils.InArrayByInt(weekArr, week) { IndexWeekMergeV2() } fmt.Println("MergeV2 end") } // MergeMonthSeasonYear 合并指标-月度,季度,年度 func MergeMonthSeasonYear() { week := int(time.Now().Weekday()) // 如果不是周日,那么就不处理 if week != 0 { return } fmt.Println("MergeMonthSeasonYear start") // 旬度 IndexTendanMergeV2() //月度 IndexMonthMergeV2() //季度 IndexSeasonMergeV2() //年度 IndexYearMergeV2() fmt.Println("MergeV2 end") } // IndexDayMergeV2 日度指标合并 func IndexDayMergeV2() { frequency := "日度" filePre := "day" startDate := time.Now().AddDate(0, 0, -7).Format(utils.FormatDate) endDate := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate) err := indexMergeV2(frequency, startDate, endDate, filePre) if err != nil { fmt.Println("合并日度指标失败,err:", err) } } // IndexWeekMergeV2 周度指标合并 func IndexWeekMergeV2() { frequency := "周度" filePre := "week" startDate := time.Now().AddDate(0, 0, -49).Format(utils.FormatDate) endDate := utils.GetNowWeekMonday().Format(utils.FormatDate) err := indexMergeV2(frequency, startDate, endDate, filePre) if err != nil { fmt.Println("合并日度指标失败,err:", err) } } // IndexTendanMergeV2 月度指标合并 func IndexTendanMergeV2() { frequency := "旬度" filePre := "tendan" // 7期的数据,就是2个月零10天嘛 startDate := time.Now().AddDate(0, -2, -10).Format(utils.FormatDate) endDate := time.Now().Format(utils.FormatDate) err := indexMergeV2(frequency, startDate, endDate, filePre) if err != nil { fmt.Println("合并旬度指标失败,err:", err) } } // IndexYearMergeV2 年度指标合并 func IndexYearMergeV2() { frequency := "年度" filePre := "year" startDate := time.Now().AddDate(-7, 0, 0).Format(utils.FormatDate) endDate := time.Now().Format(utils.FormatDate) err := indexMergeV2(frequency, startDate, endDate, filePre) if err != nil { fmt.Println("合并年度指标失败,err:", err) } } // IndexSeasonMergeV2 季度指标合并 func IndexSeasonMergeV2() { frequency := "季度" filePre := "season" startDate := time.Now().AddDate(-2, 0, 0).Format(utils.FormatDate) endDate := time.Now().Format(utils.FormatDate) err := indexMergeV2(frequency, startDate, endDate, filePre) if err != nil { fmt.Println("合并年度指标失败,err:", err) } } // IndexMonthMergeV2 月度指标合并 func IndexMonthMergeV2() { frequency := "月度" filePre := "month" startDate := time.Now().AddDate(0, -7, 0).Format(utils.FormatDate) endDate := time.Now().Format(utils.FormatDate) err := indexMergeV2(frequency, startDate, endDate, filePre) if err != nil { fmt.Println("合并年度指标失败,err:", err) } } // indexMergeV2 指标合并 // @params frequency string 频度 // @params filePre string 文件名前缀 // @params pageSize int 每个excel表中的指标数量 func indexMergeV2(frequency, startDate, endDate, filePre string) (err error) { firstIndexCount := IndexPageSize //第一个添加的excel表需要的指标数 frequencyReq := watch.GetMaxFileIndexReq{ Frequency: frequency, EndDate: endDate, TerminalCode: global.CONFIG.Serve.TerminalCode, } // 获取未合并的指标总数量 total, err := watch.GetIndexCountByFrequencyV2(frequencyReq) if err != nil { fmt.Println("GetIndexCountByFrequencyV2 Err:" + err.Error()) return } //未合并的指标总数量小于等于0,说明没有需要合并的指标 if total <= 0 { return } fmt.Println(frequency, "总共", total, "个指标") fileName := filePre + ".xlsx" //保存的文件名称 //templatePath := global.CONFIG.Serve.IndexSaveDir + "index_template.xlsx" //templateFile, tmpErr := excelize.OpenFile(templatePath) //if tmpErr != nil { // fmt.Println("打开文件失败,Err:" + tmpErr.Error()) // err = tmpErr // return //} // 判断目录是否存在,不存在则创建 if !utils.FileIsExist(global.CONFIG.Serve.IndexMergeSaveDir) { err = os.Mkdir(global.CONFIG.Serve.IndexMergeSaveDir, os.ModePerm) if err != nil { fmt.Println("os.Mkdir Err:" + err.Error()) return } } mergeIndexFilePath := global.CONFIG.Serve.IndexMergeSaveDir + fileName templateFile := excelize.NewFile() defer func() { if err := templateFile.Close(); err != nil { fmt.Println("templateFile.Close() err:" + err.Error()) } }() if utils.FileIsExist(mergeIndexFilePath) { //修改文件 //err = os.Remove(mergeIndexFilePath) //if err != nil { // fmt.Println("os.Remove Err:" + err.Error() + "; mergeIndexFilePath:" + mergeIndexFilePath) // return err //} os.Remove(mergeIndexFilePath) } // 除去第一个excel表的剩余需要添加的指标数量 nextTotal := int(total) - firstIndexCount // 总文件数 totalPage := paging.PageCount(nextTotal, IndexPageSize) totalPage = totalPage + 1 //需要加上除去在外面的指标(第一张excel) fmt.Println("total:", total) fmt.Println("totalPage:", totalPage) //sheetMap := make(map[string]string) for i := 1; i <= totalPage; i++ { //sheet startSize := paging.StartIndex(i, IndexPageSize) // 当下需要合并到excel的指标 pageList, tmpErr := watch.GetIndexPageByFrequency(frequency, endDate, startSize, IndexPageSize) if tmpErr != nil { fmt.Println("GetIndexLimitByFrequency Err:" + tmpErr.Error()) err = tmpErr return } commentResult, tmpErr := GetIndexCommentV3(mergeIndexFilePath, startDate, pageList) if tmpErr != nil { fmt.Println("GetIndexCommentV2 Err:" + tmpErr.Error()) err = tmpErr return } fmt.Println("Sheet" + strconv.Itoa(i)) sheetName := "Sheet" + strconv.Itoa(i) //sheetMap[sheetName] = sheetName templateFile.NewSheet(sheetName) templateFile.SetCellStr(sheetName, "A1", "钢联数据") templateFile.DeleteComment(sheetName, "A1") templateFile.AddComment(sheetName, "A1", commentResult) //刷新数据 //MysteelChemicalRefresh(mergeIndexFilePath) } //sheetList := templateFile.GetSheetList() //for _, v := range sheetList { // //if _, ok := sheetMap[v]; !ok { // // templateFile.DeleteSheet(v) // //} //} if err = templateFile.SaveAs(mergeIndexFilePath); err != nil { fmt.Println("保存失败,Err:" + err.Error()) templateFile.Close() return } defer templateFile.Close() time.Sleep(1 * time.Second) //MysteelChemicalRefresh(mergeIndexFilePath) AddIndexRefreshToLPush(mergeIndexFilePath) return }