123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package services
- import (
- "eta/mysteel_watch/global"
- "eta/mysteel_watch/models/index"
- "eta/mysteel_watch/utils"
- "eta/mysteel_watch/watch"
- "fmt"
- "github.com/xuri/excelize/v2"
- "os"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- const (
- IndexCreatePageSize = 100 //单个Sheet页,指标个数
- )
- var indexCreateCheckLock sync.RWMutex
- // 有更新的指标
- var hasRefreshIndexMap = make(map[string]int)
- func IndexCreateCheckMerge() (err error) {
- indexCreateCheckLock.Lock()
- defer func() {
- indexCreateCheckLock.Unlock()
- }()
- fmt.Println("IndexCreateCheck")
- findList, err := watch.GetIndexCreate()
- if err != nil {
- return
- }
- lenFindList := len(findList)
- fmt.Println("查出来的指标数量:", lenFindList)
- time.Sleep(3 * time.Second)
- if lenFindList <= 0 {
- return nil
- }
- // 超限的指标编码
- codeList := make([]string, 0)
- // 需要去查询的指标编码
- list := make([]index.BaseFromMysteelChemicalIndex, 0)
- for _, v := range findList {
- if tmpV, ok := hasRefreshIndexMap[v.IndexCode]; ok {
- // 超过5次没刷到数据,就不处理了
- if tmpV >= 5 {
- codeList = append(codeList, v.IndexCode)
- continue
- }
- }
- list = append(list, v)
- }
- if len(codeList) > 0 {
- fmt.Println(strings.Join(codeList, " , "), ";刷新超限了")
- }
- lenList := len(list)
- fmt.Println("需要生成的指标数量:", lenList)
- if lenList <= 0 {
- return nil
- }
- var filePre string
- filePre = list[0].IndexCode
- startDate := "1990-01-01"
- fileName := "create_" + filePre + ".xlsx" //保存的文件名称
- mergeIndexCreateFilePath := global.CONFIG.Serve.IndexSaveDir + fileName
- if utils.FileIsExist(mergeIndexCreateFilePath) { //修改文件
- err = os.Remove(mergeIndexCreateFilePath)
- if err != nil {
- fmt.Println("os.Remove Err:" + err.Error() + "; mergeIndexCreateFilePath:" + mergeIndexCreateFilePath)
- return err
- }
- }
- // 将指标标记下,不重复刷新数据
- for _, v := range list {
- tmpV, ok := hasRefreshIndexMap[v.IndexCode]
- if !ok {
- tmpV = 0
- }
- hasRefreshIndexMap[v.IndexCode] = tmpV + 1
- }
- templateFile := excelize.NewFile()
- defer func() {
- if err := templateFile.Close(); err != nil {
- fmt.Println("templateFile.Close() err:" + err.Error())
- }
- }()
- // 总共需要生成的sheet数量
- num := lenList / IndexCreatePageSize
- if lenList%IndexCreatePageSize > 0 {
- num++
- }
- fmt.Println("需要生成的sheet页数量:", num)
- endIndex := num - 1
- for i := 0; i <= endIndex; i++ {
- pageList := make([]index.BaseFromMysteelChemicalIndex, 0)
- if i < endIndex {
- pageList = list[IndexCreatePageSize*i : IndexCreatePageSize*(i+1)]
- } else {
- pageList = list[IndexCreatePageSize*i:]
- }
- //fmt.Println(pageList)
- // 生成批注
- commentResult, tmpErr := GetIndexCommentV3(mergeIndexCreateFilePath, startDate, pageList)
- if tmpErr != nil {
- fmt.Println("GetIndexCommentV3 Err:" + tmpErr.Error())
- err = tmpErr
- return
- }
- // 写入到对应的sheet页
- sheetName := "Sheet" + strconv.Itoa(i+1)
- templateFile.NewSheet(sheetName)
- templateFile.DeleteComment(sheetName, "A1")
- templateFile.AddComment(sheetName, "A1", commentResult)
- //templateFile.SetSheetRow(sheetName, "A1", "钢联数据")
- templateFile.SetCellStr(sheetName, "A1", "钢联数据")
- }
- if err = templateFile.SaveAs(mergeIndexCreateFilePath); err != nil {
- fmt.Println("保存失败,Err:" + err.Error())
- templateFile.Close()
- return
- }
- defer templateFile.Close()
- time.Sleep(3 * time.Second)
- // 将待刷新的excel文件路径传入到list里面处理
- AddIndexRefreshToLPush(mergeIndexCreateFilePath)
- return nil
- }
|