index.go 13 KB

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