index_merge_v2.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package services
  2. import (
  3. "eta/mysteel_watch/global"
  4. "eta/mysteel_watch/utils"
  5. "eta/mysteel_watch/watch"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "github.com/xuri/excelize/v2"
  9. "os"
  10. "strconv"
  11. "time"
  12. )
  13. const (
  14. IndexPageSize = 100 //单个Sheet页,指标个数
  15. )
  16. // MergeDayAndWeek 合并处理日度和周度的指标
  17. func MergeDayAndWeek() {
  18. fmt.Println("MergeDayAndWeek start")
  19. now := time.Now()
  20. week := int(now.Weekday())
  21. dayArr := []int{1, 2, 3, 4, 5, 6} // 日度默认1-6
  22. weekArr := []int{3, 4, 5, 6} // 周度默认3-6
  23. // 有配置则取配置中的时间
  24. if len(global.CONFIG.Serve.DayRefreshWeekdays) > 0 {
  25. dayArr = global.CONFIG.Serve.DayRefreshWeekdays
  26. }
  27. if len(global.CONFIG.Serve.WeekRefreshWeekdays) > 0 {
  28. weekArr = global.CONFIG.Serve.WeekRefreshWeekdays
  29. }
  30. //日度
  31. if utils.InArrayByInt(dayArr, week) {
  32. IndexDayMergeV2()
  33. }
  34. time.Sleep(1 * time.Minute)
  35. //周度
  36. if utils.InArrayByInt(weekArr, week) {
  37. IndexWeekMergeV2()
  38. }
  39. fmt.Println("MergeV2 end")
  40. }
  41. // MergeMonthSeasonYear 合并指标-月度,季度,年度
  42. func MergeMonthSeasonYear() {
  43. week := int(time.Now().Weekday())
  44. // 如果不是周日,那么就不处理
  45. if week != 0 {
  46. return
  47. }
  48. fmt.Println("MergeMonthSeasonYear start")
  49. // 旬度
  50. IndexTendanMergeV2()
  51. //月度
  52. IndexMonthMergeV2()
  53. //季度
  54. IndexSeasonMergeV2()
  55. //年度
  56. IndexYearMergeV2()
  57. fmt.Println("MergeV2 end")
  58. }
  59. // IndexDayMergeV2 日度指标合并
  60. func IndexDayMergeV2() {
  61. frequency := "日度"
  62. filePre := "day"
  63. startDate := time.Now().AddDate(0, 0, -7).Format(utils.FormatDate)
  64. endDate := time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  65. err := indexMergeV2(frequency, startDate, endDate, filePre)
  66. if err != nil {
  67. fmt.Println("合并日度指标失败,err:", err)
  68. }
  69. }
  70. // IndexWeekMergeV2 周度指标合并
  71. func IndexWeekMergeV2() {
  72. frequency := "周度"
  73. filePre := "week"
  74. startDate := time.Now().AddDate(0, 0, -49).Format(utils.FormatDate)
  75. endDate := utils.GetNowWeekMonday().Format(utils.FormatDate)
  76. err := indexMergeV2(frequency, startDate, endDate, filePre)
  77. if err != nil {
  78. fmt.Println("合并日度指标失败,err:", err)
  79. }
  80. }
  81. // IndexTendanMergeV2 月度指标合并
  82. func IndexTendanMergeV2() {
  83. frequency := "旬度"
  84. filePre := "tendan"
  85. // 7期的数据,就是2个月零10天嘛
  86. startDate := time.Now().AddDate(0, -2, -10).Format(utils.FormatDate)
  87. endDate := time.Now().Format(utils.FormatDate)
  88. err := indexMergeV2(frequency, startDate, endDate, filePre)
  89. if err != nil {
  90. fmt.Println("合并旬度指标失败,err:", err)
  91. }
  92. }
  93. // IndexYearMergeV2 年度指标合并
  94. func IndexYearMergeV2() {
  95. frequency := "年度"
  96. filePre := "year"
  97. startDate := time.Now().AddDate(-7, 0, 0).Format(utils.FormatDate)
  98. endDate := time.Now().Format(utils.FormatDate)
  99. err := indexMergeV2(frequency, startDate, endDate, filePre)
  100. if err != nil {
  101. fmt.Println("合并年度指标失败,err:", err)
  102. }
  103. }
  104. // IndexSeasonMergeV2 季度指标合并
  105. func IndexSeasonMergeV2() {
  106. frequency := "季度"
  107. filePre := "season"
  108. startDate := time.Now().AddDate(-2, 0, 0).Format(utils.FormatDate)
  109. endDate := time.Now().Format(utils.FormatDate)
  110. err := indexMergeV2(frequency, startDate, endDate, filePre)
  111. if err != nil {
  112. fmt.Println("合并年度指标失败,err:", err)
  113. }
  114. }
  115. // IndexMonthMergeV2 月度指标合并
  116. func IndexMonthMergeV2() {
  117. frequency := "月度"
  118. filePre := "month"
  119. startDate := time.Now().AddDate(0, -7, 0).Format(utils.FormatDate)
  120. endDate := time.Now().Format(utils.FormatDate)
  121. err := indexMergeV2(frequency, startDate, endDate, filePre)
  122. if err != nil {
  123. fmt.Println("合并年度指标失败,err:", err)
  124. }
  125. }
  126. // indexMergeV2 指标合并
  127. // @params frequency string 频度
  128. // @params filePre string 文件名前缀
  129. // @params pageSize int 每个excel表中的指标数量
  130. func indexMergeV2(frequency, startDate, endDate, filePre string) (err error) {
  131. firstIndexCount := IndexPageSize //第一个添加的excel表需要的指标数
  132. frequencyReq := watch.GetMaxFileIndexReq{
  133. Frequency: frequency,
  134. EndDate: endDate,
  135. TerminalCode: global.CONFIG.Serve.TerminalCode,
  136. }
  137. // 获取未合并的指标总数量
  138. total, err := watch.GetIndexCountByFrequencyV2(frequencyReq)
  139. if err != nil {
  140. fmt.Println("GetIndexCountByFrequencyV2 Err:" + err.Error())
  141. return
  142. }
  143. //未合并的指标总数量小于等于0,说明没有需要合并的指标
  144. if total <= 0 {
  145. return
  146. }
  147. fmt.Println(frequency, "总共", total, "个指标")
  148. fileName := filePre + ".xlsx" //保存的文件名称
  149. //templatePath := global.CONFIG.Serve.IndexSaveDir + "index_template.xlsx"
  150. //templateFile, tmpErr := excelize.OpenFile(templatePath)
  151. //if tmpErr != nil {
  152. // fmt.Println("打开文件失败,Err:" + tmpErr.Error())
  153. // err = tmpErr
  154. // return
  155. //}
  156. // 判断目录是否存在,不存在则创建
  157. if !utils.FileIsExist(global.CONFIG.Serve.IndexMergeSaveDir) {
  158. err = os.Mkdir(global.CONFIG.Serve.IndexMergeSaveDir, os.ModePerm)
  159. if err != nil {
  160. fmt.Println("os.Mkdir Err:" + err.Error())
  161. return
  162. }
  163. }
  164. mergeIndexFilePath := global.CONFIG.Serve.IndexMergeSaveDir + fileName
  165. templateFile := excelize.NewFile()
  166. defer func() {
  167. if err := templateFile.Close(); err != nil {
  168. fmt.Println("templateFile.Close() err:" + err.Error())
  169. }
  170. }()
  171. if utils.FileIsExist(mergeIndexFilePath) { //修改文件
  172. //err = os.Remove(mergeIndexFilePath)
  173. //if err != nil {
  174. // fmt.Println("os.Remove Err:" + err.Error() + "; mergeIndexFilePath:" + mergeIndexFilePath)
  175. // return err
  176. //}
  177. os.Remove(mergeIndexFilePath)
  178. }
  179. // 除去第一个excel表的剩余需要添加的指标数量
  180. nextTotal := int(total) - firstIndexCount
  181. // 总文件数
  182. totalPage := paging.PageCount(nextTotal, IndexPageSize)
  183. totalPage = totalPage + 1 //需要加上除去在外面的指标(第一张excel)
  184. fmt.Println("total:", total)
  185. fmt.Println("totalPage:", totalPage)
  186. //sheetMap := make(map[string]string)
  187. for i := 1; i <= totalPage; i++ { //sheet
  188. startSize := paging.StartIndex(i, IndexPageSize)
  189. // 当下需要合并到excel的指标
  190. pageList, tmpErr := watch.GetIndexPageByFrequency(frequency, endDate, startSize, IndexPageSize)
  191. if tmpErr != nil {
  192. fmt.Println("GetIndexLimitByFrequency Err:" + tmpErr.Error())
  193. err = tmpErr
  194. return
  195. }
  196. commentResult, tmpErr := GetIndexCommentV3(mergeIndexFilePath, startDate, pageList)
  197. if tmpErr != nil {
  198. fmt.Println("GetIndexCommentV2 Err:" + tmpErr.Error())
  199. err = tmpErr
  200. return
  201. }
  202. fmt.Println("Sheet" + strconv.Itoa(i))
  203. sheetName := "Sheet" + strconv.Itoa(i)
  204. //sheetMap[sheetName] = sheetName
  205. templateFile.NewSheet(sheetName)
  206. templateFile.SetCellStr(sheetName, "A1", "钢联数据")
  207. templateFile.DeleteComment(sheetName, "A1")
  208. templateFile.AddComment(sheetName, "A1", commentResult)
  209. //刷新数据
  210. //MysteelChemicalRefresh(mergeIndexFilePath)
  211. }
  212. //sheetList := templateFile.GetSheetList()
  213. //for _, v := range sheetList {
  214. // //if _, ok := sheetMap[v]; !ok {
  215. // // templateFile.DeleteSheet(v)
  216. // //}
  217. //}
  218. if err = templateFile.SaveAs(mergeIndexFilePath); err != nil {
  219. fmt.Println("保存失败,Err:" + err.Error())
  220. templateFile.Close()
  221. return
  222. }
  223. defer templateFile.Close()
  224. time.Sleep(1 * time.Second)
  225. //MysteelChemicalRefresh(mergeIndexFilePath)
  226. AddIndexRefreshToLPush(mergeIndexFilePath)
  227. return
  228. }