index.go 13 KB

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