watch.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package watch
  2. import (
  3. "fmt"
  4. "hongze/mysteel_watch/global"
  5. "hongze/mysteel_watch/models/index"
  6. "hongze/mysteel_watch/utils"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "sync"
  12. "time"
  13. "github.com/fsnotify/fsnotify"
  14. "github.com/xuri/excelize/v2"
  15. )
  16. func ListenFolderNew() {
  17. fmt.Println("-----文件夹监听-------")
  18. watcher, err := fsnotify.NewWatcher()
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. defer watcher.Close()
  23. done2 := make(chan bool)
  24. go func() {
  25. for {
  26. select {
  27. case event, ok := <-watcher.Events:
  28. if ok && event.Op == fsnotify.Create &&
  29. !strings.Contains(event.Name, "tmp") &&
  30. !strings.Contains(event.Name, ".TMP") &&
  31. !strings.Contains(event.Name, "~") &&
  32. (strings.Contains(event.Name, "xlsx") || strings.Contains(event.Name, "xls")) {
  33. WatchIndexFile(event.Name)
  34. }
  35. case err := <-watcher.Errors:
  36. log.Println("error:", err)
  37. case <-time.After(60 * time.Second):
  38. continue
  39. }
  40. }
  41. }()
  42. err = watcher.Add(utils.IndexSaveDir)
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. <-done2
  47. }
  48. //检测指标文件
  49. func WatchIndexFile(filePath string) {
  50. fmt.Println("filePath:", filePath)
  51. //filePath:D:\mysteel_data\CM0000568866_release.xlsx
  52. time.Sleep(10 * time.Second)
  53. if !utils.FileIsExist(filePath) {
  54. fmt.Println("filePath is not exist:" + filePath)
  55. return
  56. }
  57. //读取文件内容
  58. global.LOG.Info("WatchFile:" + filePath)
  59. f, err := excelize.OpenFile(filePath)
  60. global.LOG.Info("OpenFile:" + filePath)
  61. if err != nil {
  62. fmt.Println("OpenFile:" + filePath + ",Err:" + err.Error())
  63. return
  64. }
  65. var newFilePath string
  66. defer func() {
  67. if err := f.Close(); err != nil {
  68. fmt.Println("FileClose Err:" + err.Error())
  69. return
  70. }
  71. //重命名文件
  72. if filePath != newFilePath {
  73. err := os.Rename(filePath, newFilePath)
  74. if err != nil {
  75. fmt.Println("os.Rename Err:" + err.Error())
  76. }
  77. }
  78. }()
  79. var runMode string
  80. if strings.Contains(filePath, "debug") {
  81. runMode = "debug"
  82. } else {
  83. runMode = "release"
  84. }
  85. dir, fp := filepath.Split(filePath)
  86. var wg = sync.WaitGroup{}
  87. wg.Add(1)
  88. go func() {
  89. sheetList := f.GetSheetList()
  90. for _, sv := range sheetList {
  91. var indexName, indexCode, unit, source, frequency, startDate, endDate, describe string
  92. var indexId int64
  93. rows, err := f.GetRows(sv)
  94. if err != nil {
  95. fmt.Println("f.GetRows:err:" + err.Error())
  96. return
  97. }
  98. indexObj := new(index.BaseFromMysteelChemicalIndex)
  99. dataList := make([]index.BaseFromMysteelChemicalData, 0)
  100. dataMap := make(map[string]string)
  101. for rk, row := range rows {
  102. if rk > 0 {
  103. if rk < 10 {
  104. for ck, colCell := range row {
  105. if ck == 1 {
  106. if rk == 1 {
  107. indexName = colCell
  108. }
  109. if rk == 2 {
  110. unit = colCell
  111. }
  112. if rk == 3 {
  113. source = colCell
  114. }
  115. if rk == 4 {
  116. indexCode = colCell
  117. }
  118. if rk == 5 {
  119. frequency = colCell
  120. if !strings.Contains(frequency, "度") {
  121. frequency = frequency + "度"
  122. }
  123. }
  124. if rk == 6 {
  125. dateArr := strings.Split(colCell, "~")
  126. if len(dateArr) >= 2 {
  127. startDate = dateArr[0]
  128. endDate = dateArr[1]
  129. }
  130. }
  131. if rk == 7 {
  132. describe = colCell
  133. }
  134. }
  135. }
  136. if rk == 9 {
  137. if indexName == "" {
  138. global.LOG.Info("未刷新到指标数据:filePath:" + filePath)
  139. break
  140. }
  141. //判断指标是否存在
  142. var isAdd int
  143. item, err := indexObj.GetIndexItem(runMode, indexCode)
  144. if err != nil {
  145. if err.Error() == "record not found" {
  146. isAdd = 1
  147. } else {
  148. isAdd = -1
  149. fmt.Println("GetIndexItem Err:" + err.Error())
  150. return
  151. }
  152. }
  153. if item != nil && item.BaseFromMysteelChemicalIndexId > 0 {
  154. fmt.Println("item:", item)
  155. isAdd = 2
  156. } else {
  157. isAdd = 1
  158. }
  159. fmt.Println("isAdd:", isAdd)
  160. if !strings.Contains(frequency, "度") {
  161. frequency = frequency + "度"
  162. }
  163. var frequencyStr string
  164. if strings.Contains(frequency, "日") {
  165. frequencyStr = "day"
  166. } else if strings.Contains(frequency, "周") {
  167. frequencyStr = "week"
  168. } else if strings.Contains(frequency, "月") || strings.Contains(frequency, "旬") {
  169. frequencyStr = "month"
  170. } else if strings.Contains(frequency, "年") {
  171. frequencyStr = "year"
  172. }
  173. frequencyStr = "_" + frequencyStr
  174. if !strings.Contains(filePath, frequencyStr) {
  175. fpArr := strings.Split(fp, "_")
  176. for k, v := range fpArr {
  177. if k == 0 {
  178. newFilePath = v + frequencyStr
  179. } else {
  180. newFilePath = newFilePath + "_" + v
  181. }
  182. }
  183. newFilePath = dir + newFilePath
  184. } else {
  185. newFilePath = filePath
  186. }
  187. if isAdd == 1 {
  188. indexObj.IndexCode = indexCode
  189. indexObj.IndexName = indexName
  190. indexObj.Unit = unit
  191. indexObj.Source = source
  192. indexObj.Describe = describe
  193. indexObj.StartDate, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  194. indexObj.EndDate, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  195. indexObj.Frequency = frequency
  196. indexObj.FilePath = newFilePath
  197. err = indexObj.Add(runMode)
  198. if err != nil {
  199. fmt.Println("add err:" + err.Error())
  200. return
  201. }
  202. indexId = indexObj.BaseFromMysteelChemicalIndexId
  203. } else if isAdd == 2 {
  204. indexObj.IndexCode = indexCode
  205. indexObj.IndexName = indexName
  206. indexObj.Unit = unit
  207. indexObj.Source = source
  208. indexObj.Describe = describe
  209. indexObj.StartDate, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  210. indexObj.EndDate, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  211. indexObj.Frequency = frequency
  212. indexObj.FilePath = newFilePath
  213. indexObj.ModifyTime = time.Now()
  214. indexId = item.BaseFromMysteelChemicalIndexId
  215. //修改数据
  216. updateColsArr := make([]string, 0)
  217. updateColsArr = append(updateColsArr, "index_name")
  218. updateColsArr = append(updateColsArr, "unit")
  219. updateColsArr = append(updateColsArr, "source")
  220. updateColsArr = append(updateColsArr, "frequency")
  221. updateColsArr = append(updateColsArr, "start_date")
  222. updateColsArr = append(updateColsArr, "end_date")
  223. updateColsArr = append(updateColsArr, "describe")
  224. updateColsArr = append(updateColsArr, "end_date")
  225. updateColsArr = append(updateColsArr, "modify_time")
  226. updateColsArr = append(updateColsArr, "file_path")
  227. indexObj.Update(runMode, updateColsArr)
  228. dataObj := new(index.BaseFromMysteelChemicalData)
  229. //获取已存在的所有数据
  230. dataList, err := dataObj.GetIndexDataList(runMode, indexCode)
  231. if err != nil {
  232. fmt.Println("GetIndexDataList Err:" + err.Error())
  233. return
  234. }
  235. fmt.Println("dataListLen:", len(dataList))
  236. for _, v := range dataList {
  237. dateStr := v.DataTime.Format(utils.FormatDate)
  238. dataMap[dateStr] = v.Value
  239. }
  240. }
  241. }
  242. } else {
  243. var date, value string
  244. for ck, colCell := range row {
  245. if ck == 0 {
  246. date = colCell
  247. } else {
  248. value = colCell
  249. }
  250. }
  251. if _, ok := dataMap[date]; !ok {
  252. dateTime, err := time.ParseInLocation(utils.FormatDate, date, time.Local)
  253. if err != nil {
  254. fmt.Println("time.ParseInLocation Err:" + err.Error())
  255. return
  256. }
  257. dataItem := new(index.BaseFromMysteelChemicalData)
  258. dataItem.BaseFromMysteelChemicalIndexId = indexId
  259. dataItem.IndexCode = indexCode
  260. dataItem.DataTime = dateTime
  261. dataItem.Value = value
  262. dataItem.CreateTime = time.Now()
  263. dataItem.ModifyTime = time.Now()
  264. dataList = append(dataList, *dataItem)
  265. }
  266. }
  267. }
  268. }
  269. if len(dataList) > 0 {
  270. dataObj := new(index.BaseFromMysteelChemicalData)
  271. err = dataObj.Add(runMode, dataList)
  272. if err != nil {
  273. fmt.Println("dataObj.Add() Err:" + err.Error())
  274. }
  275. }
  276. }
  277. wg.Done()
  278. }()
  279. wg.Wait()
  280. }
  281. /*
  282. CREATE动作即临时文件的创建
  283. WRITE写文件动作
  284. CHMOD修改文件属性
  285. REMOVE删除临时文件。
  286. */