index.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/mysteel_watch/cache"
  5. "eta/mysteel_watch/global"
  6. "eta/mysteel_watch/services/alarm_msg"
  7. "eta/mysteel_watch/utils"
  8. "eta/mysteel_watch/watch"
  9. "fmt"
  10. "os"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/xuri/excelize/v2"
  16. )
  17. const (
  18. FormatTime = "15:04:05" //时间格式
  19. FormatTimeStr = "15:04" //时间格式
  20. )
  21. var checkLock sync.RWMutex
  22. func IndexCreateCheck() (err error) {
  23. checkLock.Lock()
  24. defer func() {
  25. checkLock.Unlock()
  26. }()
  27. fmt.Println("IndexCreateCheck")
  28. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  29. //list, err := indexObj.GetIndexCreate()
  30. list, err := watch.GetIndexCreate()
  31. if err != nil {
  32. return
  33. }
  34. fmt.Println("listLen:", len(list))
  35. time.Sleep(5 * time.Second)
  36. if len(list) <= 0 {
  37. return nil
  38. }
  39. var indexCode []string
  40. for _, v := range list {
  41. indexCode = append(indexCode, v.IndexCode)
  42. }
  43. indexCodeStr := strings.Join(indexCode, ";")
  44. fmt.Println("indexCodeStr:" + indexCodeStr)
  45. //go alarm_msg.SendAlarmMsg(utils.APPNAME+" 存在指标数据未生成:"+indexCodeStr, 3)
  46. for _, v := range list {
  47. fmt.Println("IndexCreateCheck start:", v.IndexCode, v.FilePath)
  48. if v.FilePath != "" && utils.FileIsExist(v.FilePath) {
  49. os.Remove(v.FilePath)
  50. }
  51. fmt.Println("IndexCreate:" + v.IndexCode)
  52. saveFilePath, err := IndexCreate(v.UpdateWeek, v.IndexCode)
  53. if err != nil {
  54. fmt.Println("IndexCreate Err:" + err.Error())
  55. go alarm_msg.SendAlarmMsg(utils.APPNAME+" 指标数据未生成检测失败:"+err.Error(), 3)
  56. }
  57. v.FilePath = saveFilePath
  58. fmt.Println("IndexCreate saveFilePath:" + v.FilePath)
  59. time.Sleep(1 * time.Second)
  60. if utils.FileIsExist(saveFilePath) {
  61. AddIndexRefreshToLPush(saveFilePath)
  62. }
  63. fmt.Println("MysteelChemicalRefresh end:" + v.IndexCode)
  64. }
  65. return nil
  66. }
  67. // func IndexCreate(item *index.BaseFromMysteelChemicalIndex) (saveFilePath string, err error) {
  68. func IndexCreate(updateWeek, indexCode string) (saveFilePath string, err error) {
  69. updateWeek = utils.GetUpdateWeekEn(updateWeek)
  70. global.LOG.Info("task IndexCreate:" + time.Now().Format(utils.FormatDateTime))
  71. runMode := "release"
  72. //fileName := req.IndexName + "_" + req.IndexCode + ".xlsx"
  73. var fileName string
  74. if updateWeek != "" {
  75. fileName = indexCode + "_" + updateWeek + "_" + runMode + ".xlsx" //保存的文件名称
  76. } else {
  77. fileName = indexCode + "_" + runMode + ".xlsx" //保存的文件名称
  78. }
  79. filePath := global.CONFIG.Serve.IndexSaveDir + fileName
  80. if utils.FileIsExist(filePath) {
  81. saveFilePath = filePath
  82. return
  83. os.Remove(filePath)
  84. }
  85. templatePath := global.CONFIG.Serve.IndexSaveDir + "index_template.xlsx"
  86. templateFile, err := excelize.OpenFile(templatePath)
  87. if err != nil {
  88. fmt.Println("OpenFile template err:" + err.Error())
  89. return
  90. }
  91. defer func() {
  92. templateFile.Close()
  93. }()
  94. sheetList := templateFile.GetSheetList()
  95. for k, v := range sheetList {
  96. if k > 0 {
  97. templateFile.DeleteSheet(v)
  98. }
  99. }
  100. startDate := "1990-01-01"
  101. 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}
  102. `
  103. commentMap := make(map[string]interface{})
  104. commentMap["author"] = "{"
  105. commentMap["text"] = commentStr
  106. commentJson, err := json.Marshal(commentMap)
  107. if err != nil {
  108. fmt.Println("json.Marshal err:" + err.Error())
  109. }
  110. fmt.Println("commentJson")
  111. fmt.Println(string(commentJson))
  112. templateFile.DeleteComment("Sheet1", "A1")
  113. templateFile.AddComment("Sheet1", "A1", string(commentJson))
  114. if err := templateFile.SaveAs(filePath); err != nil {
  115. fmt.Println(err)
  116. return "", err
  117. }
  118. saveFilePath = filePath
  119. return
  120. }
  121. func IndexRefreshAll() {
  122. fmt.Println("IndexCreateCheck")
  123. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  124. //list, err := indexObj.GetIndexRefreshAllByMergeFile()
  125. frequency := global.CONFIG.Serve.Frequency
  126. list, err := watch.GetIndexRefreshAllByMergeFile()
  127. if err != nil {
  128. fmt.Println("GetIndexRefreshAll Err:" + err.Error())
  129. return
  130. }
  131. fmt.Println("listLen:", len(list))
  132. if len(list) <= 0 {
  133. return
  134. }
  135. now := time.Now()
  136. month := int(now.Month())
  137. day := now.Day()
  138. week := int(now.Weekday())
  139. for _, v := range list {
  140. rn := utils.GetRandInt(1, 10)
  141. time.Sleep(time.Duration(rn) * time.Second)
  142. if v.Frequency == "年度" {
  143. if month == 1 && day == 1 {
  144. //MysteelChemicalRefresh(v.MergeFilePath)
  145. AddIndexRefreshToLPush(v.MergeFilePath)
  146. }
  147. } else if v.Frequency == "季度" {
  148. if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
  149. //MysteelChemicalRefresh(v.MergeFilePath)
  150. AddIndexRefreshToLPush(v.MergeFilePath)
  151. }
  152. } else if v.Frequency == "月度" {
  153. if day == 1 {
  154. //MysteelChemicalRefresh(v.MergeFilePath)
  155. AddIndexRefreshToLPush(v.MergeFilePath)
  156. }
  157. } else if v.Frequency == "周度" && frequency == "周度" {
  158. if week > 2 && week < 6 {
  159. //MysteelChemicalRefresh(v.MergeFilePath)
  160. AddIndexRefreshToLPush(v.MergeFilePath)
  161. }
  162. } else {
  163. if week < 6 && frequency == "" {
  164. //MysteelChemicalRefresh(v.MergeFilePath)
  165. AddIndexRefreshToLPush(v.MergeFilePath)
  166. }
  167. }
  168. }
  169. return
  170. }
  171. func IndexRefreshMethanol() {
  172. fmt.Println("IndexRefreshMethanol")
  173. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  174. //list, err := indexObj.GetIndexRefreshMethanolByMergeFile()
  175. list, err := watch.GetIndexRefreshMethanolByMergeFile()
  176. if err != nil {
  177. fmt.Println("GetIndexRefreshAll Err:" + err.Error())
  178. return
  179. }
  180. fmt.Println("listLen:", len(list))
  181. if len(list) <= 0 {
  182. return
  183. }
  184. now := time.Now()
  185. month := int(now.Month())
  186. day := now.Day()
  187. week := int(now.Weekday())
  188. for _, v := range list {
  189. rn := utils.GetRandInt(1, 10)
  190. time.Sleep(time.Duration(rn) * time.Second)
  191. time.Sleep(3 * time.Second)
  192. if v.Frequency == "年度" {
  193. if month == 1 && day == 1 {
  194. //MysteelChemicalRefresh(v.MergeFilePath)
  195. AddIndexRefreshToLPush(v.MergeFilePath)
  196. }
  197. } else if v.Frequency == "季度" {
  198. if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
  199. //MysteelChemicalRefresh(v.MergeFilePath)
  200. AddIndexRefreshToLPush(v.MergeFilePath)
  201. }
  202. } else if v.Frequency == "月度" {
  203. if day == 1 {
  204. //MysteelChemicalRefresh(v.MergeFilePath)
  205. AddIndexRefreshToLPush(v.MergeFilePath)
  206. }
  207. } else if v.Frequency == "周度" {
  208. if week > 2 && week < 6 {
  209. //MysteelChemicalRefresh(v.MergeFilePath)
  210. AddIndexRefreshToLPush(v.MergeFilePath)
  211. }
  212. } else {
  213. if week < 6 {
  214. //MysteelChemicalRefresh(v.MergeFilePath)
  215. AddIndexRefreshToLPush(v.MergeFilePath)
  216. }
  217. }
  218. }
  219. return
  220. }
  221. func IndexRefreshTimely() {
  222. fmt.Println("IndexRefreshTimely")
  223. listLen := 0
  224. defer func() {
  225. if listLen > 0 {
  226. go alarm_msg.SendAlarmMsg(utils.APPNAME+" 及时刷新指标, listLen: "+strconv.Itoa(listLen), 3)
  227. }
  228. }()
  229. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  230. //list, err := indexObj.GetIndexRefreshMethanolByTimely()
  231. list, err := watch.GetIndexRefreshMethanolByTimely()
  232. if err != nil {
  233. fmt.Println("GetIndexRefreshAll Err:" + err.Error())
  234. return
  235. }
  236. listLen = len(list)
  237. fmt.Println("listLen:", len(list))
  238. if listLen <= 0 {
  239. return
  240. }
  241. now := time.Now()
  242. month := int(now.Month())
  243. day := now.Day()
  244. week := int(now.Weekday())
  245. for _, v := range list {
  246. rn := utils.GetRandInt(1, 10)
  247. time.Sleep(time.Duration(rn) * time.Second)
  248. time.Sleep(3 * time.Second)
  249. if v.Frequency == "年度" {
  250. if month == 1 && day == 1 {
  251. //MysteelChemicalRefresh(v.MergeFilePath)
  252. AddIndexRefreshToLPush(v.MergeFilePath)
  253. }
  254. } else if v.Frequency == "季度" {
  255. if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
  256. //MysteelChemicalRefresh(v.MergeFilePath)
  257. AddIndexRefreshToLPush(v.MergeFilePath)
  258. }
  259. } else if v.Frequency == "月度" {
  260. if day == 1 {
  261. //MysteelChemicalRefresh(v.MergeFilePath)
  262. AddIndexRefreshToLPush(v.MergeFilePath)
  263. }
  264. } else if v.Frequency == "周度" {
  265. if week > 2 && week < 6 {
  266. //MysteelChemicalRefresh(v.MergeFilePath)
  267. AddIndexRefreshToLPush(v.MergeFilePath)
  268. }
  269. } else {
  270. if week < 6 {
  271. //MysteelChemicalRefresh(v.MergeFilePath)
  272. AddIndexRefreshToLPush(v.MergeFilePath)
  273. }
  274. }
  275. }
  276. return
  277. }
  278. // pushLock excel路径写入的读写锁
  279. var pushLock sync.RWMutex
  280. // AddIndexRefreshToLPush 添加到指标刷新
  281. func AddIndexRefreshToLPush(filePath string) {
  282. pushLock.Lock()
  283. //result := cache.IndexAutoRefresh(filePath)
  284. cache.IndexAutoRefresh(filePath)
  285. //fmt.Println("IndexAutoRefresh result:", result)
  286. pushLock.Unlock()
  287. return
  288. }
  289. func IndexUpdateCheck() {
  290. fmt.Println("开始检测")
  291. //day:周一至周六
  292. //week,周三至周五
  293. //month:每月月中和月底
  294. //season:每个季度的下一个月,月中和月底
  295. //year:每个月月初
  296. // 频度:日度,周度,月度,季度,年度
  297. frequencyBatch := []string{"日度"}
  298. //today, _ := time.Parse(utils.FormatDate, "2023-09-01")
  299. today := time.Now()
  300. todayDate := today.Format(utils.FormatDate)
  301. // 是否是周度指标更新的日子
  302. if today.Weekday() >= 3 {
  303. frequencyBatch = append(frequencyBatch, "周度")
  304. }
  305. // 判断是否是月度指标更新的日子
  306. /*tmpT, _ := time.ParseInLocation(utils.FormatDate, today.Format(utils.FormatYearMonth)+"-01", time.Local)
  307. lastDate := tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
  308. if lastDate == todayDate {
  309. frequencyBatch = append(frequencyBatch, "月度")
  310. }
  311. //判断是否是季度指标更新的日子
  312. if lastDate == todayDate && today.Month()%3 == 0 {
  313. frequencyBatch = append(frequencyBatch, "季度")
  314. }
  315. //判断是否是年度指标更新的日子
  316. tmpT, _ = time.ParseInLocation(utils.FormatDate, today.Format(utils.FormatYearMonth)+"-01", time.Local)
  317. if tmpT.Format(utils.FormatDate) == todayDate {
  318. frequencyBatch = append(frequencyBatch, "年度")
  319. }*/
  320. var req watch.RefreshCheckByDayReq
  321. req.Source = 34
  322. req.LatestDate = todayDate
  323. req.FrequencyBatch = strings.Join(frequencyBatch, ",")
  324. ret, err := watch.RefreshCheckByDay(req)
  325. if err != nil {
  326. global.LOG.Error(utils.APPNAME + " 钢联指标更新检查 出错" + time.Now().Format("2006-01-02 15:04:05") + ";Err:" + err.Error())
  327. go alarm_msg.SendAlarmMsg(utils.APPNAME+" 钢联指标更新检查 出错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3)
  328. return
  329. }
  330. fmt.Println(ret.UnUpdateNum)
  331. fmt.Println(ret.UpdateNum)
  332. halfNum := ret.UpdateNum / 2
  333. if ret.UnUpdateNum > halfNum {
  334. tip := fmt.Sprintf("%s 钢联指标更新检查:更新数据过少; %s :已更新指标个数:%d, 未更新指标个数%d", utils.APPNAME, todayDate, ret.UpdateNum, ret.UnUpdateNum)
  335. global.LOG.Info(tip)
  336. go alarm_msg.SendAlarmMsg(tip, 3)
  337. return
  338. }
  339. return
  340. }