index.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/mysteel_watch/cache"
  6. "hongze/mysteel_watch/global"
  7. "hongze/mysteel_watch/services/alarm_msg"
  8. "hongze/mysteel_watch/utils"
  9. "hongze/mysteel_watch/watch"
  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. fmt.Println("Refresh:" + v.IndexCode)
  50. err = UpdateComment(v.FilePath)
  51. if err != nil {
  52. fmt.Println("UpdateComment Err:" + err.Error())
  53. go alarm_msg.SendAlarmMsg(utils.APPNAME+" 指标数据未生成检测失败:"+err.Error()+";file:"+v.FilePath, 3)
  54. }
  55. time.Sleep(1 * time.Second)
  56. //MysteelChemicalRefresh(v.FilePath)
  57. AddIndexRefreshToLpush(v.FilePath)
  58. } else {
  59. fmt.Println("IndexCreate:" + v.IndexCode)
  60. //saveFilePath, err := IndexCreate(v)
  61. saveFilePath, err := IndexCreate(v.UpdateWeek, v.IndexCode)
  62. if err != nil {
  63. fmt.Println("IndexCreate Err:" + err.Error())
  64. go alarm_msg.SendAlarmMsg(utils.APPNAME+" 指标数据未生成检测失败:"+err.Error(), 3)
  65. }
  66. v.FilePath = saveFilePath
  67. fmt.Println("IndexCreate saveFilePath:" + v.FilePath)
  68. time.Sleep(1 * time.Second)
  69. if utils.FileIsExist(saveFilePath) {
  70. //MysteelChemicalRefresh(saveFilePath)
  71. AddIndexRefreshToLpush(saveFilePath)
  72. }
  73. fmt.Println("MysteelChemicalRefresh end:" + v.IndexCode)
  74. }
  75. fmt.Println("IndexCreateCheck end:" + v.IndexCode)
  76. }
  77. return nil
  78. }
  79. // func IndexCreate(item *index.BaseFromMysteelChemicalIndex) (saveFilePath string, err error) {
  80. func IndexCreate(updateWeek, indexCode string) (saveFilePath string, err error) {
  81. updateWeek = utils.GetUpdateWeekEn(updateWeek)
  82. global.LOG.Info("task IndexCreate:" + time.Now().Format(utils.FormatDateTime))
  83. runMode := "release"
  84. //fileName := req.IndexName + "_" + req.IndexCode + ".xlsx"
  85. var fileName string
  86. if updateWeek != "" {
  87. fileName = indexCode + "_" + updateWeek + "_" + runMode + ".xlsx" //保存的文件名称
  88. } else {
  89. fileName = indexCode + "_" + runMode + ".xlsx" //保存的文件名称
  90. }
  91. filePath := global.CONFIG.Serve.IndexSaveDir + fileName
  92. if utils.FileIsExist(filePath) {
  93. saveFilePath = filePath
  94. return
  95. os.Remove(filePath)
  96. }
  97. templatePath := global.CONFIG.Serve.IndexSaveDir + "index_template.xlsx"
  98. templateFile, err := excelize.OpenFile(templatePath)
  99. if err != nil {
  100. fmt.Println("OpenFile template err:" + err.Error())
  101. return
  102. }
  103. defer func() {
  104. templateFile.Close()
  105. }()
  106. startDate := "1990-01-01"
  107. 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}
  108. `
  109. commentMap := make(map[string]interface{})
  110. commentMap["author"] = "{"
  111. commentMap["text"] = commentStr
  112. commentJson, err := json.Marshal(commentMap)
  113. if err != nil {
  114. fmt.Println("json.Marshal err:" + err.Error())
  115. }
  116. fmt.Println("commentJson")
  117. fmt.Println(string(commentJson))
  118. templateFile.DeleteComment("Sheet1", "A1")
  119. templateFile.AddComment("Sheet1", "A1", string(commentJson))
  120. if err := templateFile.SaveAs(filePath); err != nil {
  121. fmt.Println(err)
  122. return "", err
  123. }
  124. saveFilePath = filePath
  125. return
  126. }
  127. //func IndexRefreshAll() {
  128. // fmt.Println("IndexCreateCheck")
  129. // indexObj := new(index.BaseFromMysteelChemicalIndex)
  130. // list, err := indexObj.GetIndexRefreshAll()
  131. // if err != nil {
  132. // fmt.Println("GetIndexRefreshAll Err:" + err.Error())
  133. // return
  134. // }
  135. // fmt.Println("listLen:", len(list))
  136. // if len(list) <= 0 {
  137. // return
  138. // }
  139. // nowDate := time.Now().Format(utils.FormatDate)
  140. // monthLastDay := utils.GetNowMonthLastDay()
  141. // monthLastDayStr := monthLastDay.Format(utils.FormatDate)
  142. // yearLast := strconv.Itoa(time.Now().Year()) + "12-31"
  143. // for _, v := range list {
  144. // err = UpdateComment(v.FilePath)
  145. // if err != nil {
  146. // go alarm_msg.SendAlarmMsg(utils.APPNAME+" 指标数据未生成检测失败:"+err.Error()+";file:"+v.FilePath, 3)
  147. // }
  148. // time.Sleep(1 * time.Second)
  149. // if v.Frequency == "周度" {
  150. // if v.UpdateWeek == "" && v.UpdateTime == "" && v.UpdateTime2 == "" {
  151. // //判断本周的数据是否已经存在
  152. // MysteelChemicalRefresh(v.FilePath)
  153. // }
  154. // } else if v.Frequency == "月度" || v.Frequency == "旬度" {
  155. // if nowDate == monthLastDayStr {
  156. // MysteelChemicalRefresh(v.FilePath)
  157. // }
  158. // } else if v.Frequency == "年度" && nowDate == yearLast {
  159. // if nowDate == yearLast {
  160. // MysteelChemicalRefresh(v.FilePath)
  161. // }
  162. // } else if v.Frequency == "日度" {
  163. // if v.EndDate.Format(utils.FormatDate) != nowDate {
  164. // MysteelChemicalRefresh(v.FilePath)
  165. // }
  166. // } else {
  167. // global.LOG.Info("无效频度:" + v.IndexCode + ";" + v.Frequency)
  168. // }
  169. // }
  170. // return
  171. //}
  172. func IndexRefreshAll() {
  173. fmt.Println("IndexCreateCheck")
  174. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  175. //list, err := indexObj.GetIndexRefreshAllByMergeFile()
  176. frequency := global.CONFIG.Serve.Frequency
  177. list, err := watch.GetIndexRefreshAllByMergeFile()
  178. if err != nil {
  179. fmt.Println("GetIndexRefreshAll Err:" + err.Error())
  180. return
  181. }
  182. fmt.Println("listLen:", len(list))
  183. if len(list) <= 0 {
  184. return
  185. }
  186. now := time.Now()
  187. month := int(now.Month())
  188. day := now.Day()
  189. week := int(now.Weekday())
  190. for _, v := range list {
  191. rn := utils.GetRandInt(1, 10)
  192. time.Sleep(time.Duration(rn) * time.Second)
  193. if v.Frequency == "年度" {
  194. if month == 1 && day == 1 {
  195. //MysteelChemicalRefresh(v.MergeFilePath)
  196. AddIndexRefreshToLpush(v.MergeFilePath)
  197. }
  198. } else if v.Frequency == "季度" {
  199. if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
  200. //MysteelChemicalRefresh(v.MergeFilePath)
  201. AddIndexRefreshToLpush(v.MergeFilePath)
  202. }
  203. } else if v.Frequency == "月度" {
  204. if day == 1 {
  205. //MysteelChemicalRefresh(v.MergeFilePath)
  206. AddIndexRefreshToLpush(v.MergeFilePath)
  207. }
  208. } else if v.Frequency == "周度" && frequency == "周度" {
  209. if week > 2 && week < 6 {
  210. //MysteelChemicalRefresh(v.MergeFilePath)
  211. AddIndexRefreshToLpush(v.MergeFilePath)
  212. }
  213. } else {
  214. if week < 6 && frequency == "" {
  215. //MysteelChemicalRefresh(v.MergeFilePath)
  216. AddIndexRefreshToLpush(v.MergeFilePath)
  217. }
  218. }
  219. }
  220. return
  221. }
  222. func IndexRefreshMethanol() {
  223. fmt.Println("IndexRefreshMethanol")
  224. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  225. //list, err := indexObj.GetIndexRefreshMethanolByMergeFile()
  226. list, err := watch.GetIndexRefreshMethanolByMergeFile()
  227. if err != nil {
  228. fmt.Println("GetIndexRefreshAll Err:" + err.Error())
  229. return
  230. }
  231. fmt.Println("listLen:", len(list))
  232. if len(list) <= 0 {
  233. return
  234. }
  235. now := time.Now()
  236. month := int(now.Month())
  237. day := now.Day()
  238. week := int(now.Weekday())
  239. for _, v := range list {
  240. rn := utils.GetRandInt(1, 10)
  241. time.Sleep(time.Duration(rn) * time.Second)
  242. time.Sleep(3 * time.Second)
  243. if v.Frequency == "年度" {
  244. if month == 1 && day == 1 {
  245. //MysteelChemicalRefresh(v.MergeFilePath)
  246. AddIndexRefreshToLpush(v.MergeFilePath)
  247. }
  248. } else if v.Frequency == "季度" {
  249. if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
  250. //MysteelChemicalRefresh(v.MergeFilePath)
  251. AddIndexRefreshToLpush(v.MergeFilePath)
  252. }
  253. } else if v.Frequency == "月度" {
  254. if day == 1 {
  255. //MysteelChemicalRefresh(v.MergeFilePath)
  256. AddIndexRefreshToLpush(v.MergeFilePath)
  257. }
  258. } else if v.Frequency == "周度" {
  259. if week > 2 && week < 6 {
  260. //MysteelChemicalRefresh(v.MergeFilePath)
  261. AddIndexRefreshToLpush(v.MergeFilePath)
  262. }
  263. } else {
  264. if week < 6 {
  265. //MysteelChemicalRefresh(v.MergeFilePath)
  266. AddIndexRefreshToLpush(v.MergeFilePath)
  267. }
  268. }
  269. }
  270. return
  271. }
  272. func IndexRefreshTimely() {
  273. fmt.Println("IndexRefreshTimely")
  274. listLen := 0
  275. defer func() {
  276. if listLen > 0 {
  277. go alarm_msg.SendAlarmMsg(utils.APPNAME+" 及时刷新指标, listLen: "+strconv.Itoa(listLen), 3)
  278. }
  279. }()
  280. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  281. //list, err := indexObj.GetIndexRefreshMethanolByTimely()
  282. list, err := watch.GetIndexRefreshMethanolByTimely()
  283. if err != nil {
  284. fmt.Println("GetIndexRefreshAll Err:" + err.Error())
  285. return
  286. }
  287. listLen = len(list)
  288. fmt.Println("listLen:", len(list))
  289. if listLen <= 0 {
  290. return
  291. }
  292. now := time.Now()
  293. month := int(now.Month())
  294. day := now.Day()
  295. week := int(now.Weekday())
  296. for _, v := range list {
  297. rn := utils.GetRandInt(1, 10)
  298. time.Sleep(time.Duration(rn) * time.Second)
  299. time.Sleep(3 * time.Second)
  300. if v.Frequency == "年度" {
  301. if month == 1 && day == 1 {
  302. //MysteelChemicalRefresh(v.MergeFilePath)
  303. AddIndexRefreshToLpush(v.MergeFilePath)
  304. }
  305. } else if v.Frequency == "季度" {
  306. if (month == 1 || month == 4 || month == 7 || month == 10) && day == 1 {
  307. //MysteelChemicalRefresh(v.MergeFilePath)
  308. AddIndexRefreshToLpush(v.MergeFilePath)
  309. }
  310. } else if v.Frequency == "月度" {
  311. if day == 1 {
  312. //MysteelChemicalRefresh(v.MergeFilePath)
  313. AddIndexRefreshToLpush(v.MergeFilePath)
  314. }
  315. } else if v.Frequency == "周度" {
  316. if week > 2 && week < 6 {
  317. //MysteelChemicalRefresh(v.MergeFilePath)
  318. AddIndexRefreshToLpush(v.MergeFilePath)
  319. }
  320. } else {
  321. if week < 6 {
  322. //MysteelChemicalRefresh(v.MergeFilePath)
  323. AddIndexRefreshToLpush(v.MergeFilePath)
  324. }
  325. }
  326. }
  327. return
  328. }
  329. var lock sync.RWMutex
  330. // 刷新周度指标数据
  331. func IndexRefreshWeek() {
  332. lock.Lock()
  333. nowWeek := time.Now().Weekday().String()
  334. nowWeekZn := utils.GetWeekZn(nowWeek)
  335. fmt.Println("nowWeekZn:" + nowWeekZn)
  336. //indexObj := new(index.BaseFromMysteelChemicalIndex)
  337. startTime := time.Now().Add(-1 * time.Minute).Format(FormatTimeStr)
  338. endTime := time.Now().Add(1 * time.Minute).Format(FormatTimeStr)
  339. //list, err := indexObj.GetIndexRefreshWeek(nowWeekZn, startTime, endTime)
  340. list, err := watch.GetIndexRefreshWeek(nowWeekZn, startTime, endTime)
  341. if err != nil {
  342. fmt.Println("GetIndexRefreshWeek Err:" + err.Error())
  343. return
  344. }
  345. fmt.Println("listLen:", len(list))
  346. if len(list) <= 0 {
  347. return
  348. }
  349. for _, v := range list {
  350. if global.Re == nil && global.Rc != nil {
  351. key := utils.REFRESH_INDEX_CODE + v.IndexCode
  352. if global.Rc.SetNX(key, v.FilePath, 5*time.Hour) {
  353. cache.IndexAutoRefresh(v.FilePath)
  354. }
  355. }
  356. }
  357. lock.Unlock()
  358. return
  359. }
  360. // 修改指标备注指标
  361. func UpdateComment(filePath string) (err error) {
  362. templateFile, err := excelize.OpenFile(filePath)
  363. if err != nil {
  364. fmt.Println("OpenFile Err:" + err.Error())
  365. return
  366. }
  367. defer func() {
  368. templateFile.Close()
  369. }()
  370. commentArr := templateFile.GetComments()
  371. for k, v := range commentArr {
  372. fmt.Println(k, v)
  373. for _, sv := range v {
  374. fmt.Println("text:", sv.Text)
  375. if strings.Contains(sv.Text, `"UpdateMode":0`) {
  376. newText := strings.Replace(sv.Text, `"UpdateMode":0`, `"UpdateMode":1`, -1)
  377. newText = strings.Trim(newText, "{")
  378. commentMap := make(map[string]interface{})
  379. commentMap["author"] = "{"
  380. commentMap["text"] = newText
  381. commentJson, err := json.Marshal(commentMap)
  382. if err != nil {
  383. fmt.Println("json.Marshal err:" + err.Error())
  384. }
  385. fmt.Println("add text:" + string(commentJson))
  386. templateFile.DeleteComment("Sheet1", "A1")
  387. err = templateFile.AddComment("Sheet1", "A1", string(commentJson))
  388. if err != nil {
  389. fmt.Println("AddComment Err:" + err.Error())
  390. return err
  391. }
  392. }
  393. }
  394. }
  395. if err := templateFile.SaveAs(filePath); err != nil {
  396. fmt.Println("templateFile.SaveAs Err:", err)
  397. return err
  398. }
  399. return
  400. }
  401. // 获取指标备注指标
  402. func GetComment(filePath string) string {
  403. excelFile, err := excelize.OpenFile(filePath)
  404. if err != nil {
  405. fmt.Println("OpenFile Err:" + err.Error())
  406. return ""
  407. }
  408. defer func() {
  409. excelFile.Close()
  410. }()
  411. commentArr := excelFile.GetComments()
  412. for _, v := range commentArr {
  413. //fmt.Println(k, v)
  414. for _, sv := range v {
  415. //fmt.Println("text:", sv.Text)
  416. if sv.Text != "" {
  417. return sv.Text
  418. }
  419. }
  420. }
  421. return ""
  422. }
  423. var pushLock sync.RWMutex
  424. // 刷新周度指标数据
  425. func AddIndexRefreshToLpush(filePath string) {
  426. pushLock.Lock()
  427. result := cache.IndexAutoRefresh(filePath)
  428. fmt.Println("IndexAutoRefresh result:", result)
  429. pushLock.Unlock()
  430. return
  431. }