index.go 13 KB

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