index.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. "hongze/hongtao3_watch/utils"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. FormatTime = "15:04:05" //时间格式
  11. FormatTimeStr = "15:04" //时间格式
  12. )
  13. //var checkLock sync.RWMutex
  14. // ReadExcel 读取Excel
  15. func ReadExcelV2(filePath string) {
  16. time.Sleep(5 * time.Second)
  17. excelInfo, err := excelize.OpenFile(filePath)
  18. if err != nil {
  19. fmt.Println("OpenFile excel err:" + err.Error())
  20. return
  21. }
  22. fmt.Println("开始读取EXCEL了:", filePath)
  23. defer func() {
  24. excelInfo.Close()
  25. }()
  26. sheetList := excelInfo.GetSheetList()
  27. indexNameList := make([]string, 0)
  28. thirdIndexIdList := make([]string, 0)
  29. frequencyList := make([]string, 0)
  30. unitList := make([]string, 0)
  31. //indexIdList := make([]int, 0)
  32. dataMap := make(map[string]map[string]string, 0)
  33. for _, sheet := range sheetList {
  34. //fmt.Println(sheet)
  35. sheetThirdIndexIdList := make([]string, 0)
  36. rows, err := excelInfo.GetRows(sheet)
  37. if err != nil {
  38. fmt.Println("err:", err)
  39. continue
  40. }
  41. // excel异常
  42. if len(rows) <= 13 {
  43. fmt.Println("错误的excel表")
  44. continue
  45. }
  46. indexLen := len(rows[0])
  47. //fmt.Println(indexLen)
  48. // 没有指标
  49. if indexLen <= 1 {
  50. fmt.Println("没有指标")
  51. continue
  52. }
  53. for k, row := range rows {
  54. //fmt.Println(row)
  55. // 指标信息处理
  56. {
  57. // 指标名称
  58. if k == 0 {
  59. for colIndex, col := range row {
  60. if colIndex == 0 {
  61. continue
  62. }
  63. indexNameList = append(indexNameList, col)
  64. }
  65. }
  66. //第三方指标id
  67. if k == 1 {
  68. for colIndex, col := range row {
  69. if colIndex == 0 {
  70. continue
  71. }
  72. thirdIndexIdList = append(thirdIndexIdList, col)
  73. sheetThirdIndexIdList = append(sheetThirdIndexIdList, col)
  74. }
  75. }
  76. //频度
  77. if k == 8 {
  78. for colIndex, col := range row {
  79. if colIndex == 0 {
  80. continue
  81. }
  82. if strings.Contains(col, "日度") {
  83. frequencyList = append(frequencyList, "日度")
  84. } else if strings.Contains(col, "周度") {
  85. frequencyList = append(frequencyList, "周度")
  86. } else if strings.Contains(col, "旬度") {
  87. frequencyList = append(frequencyList, "旬度")
  88. } else if strings.Contains(col, "月度") {
  89. frequencyList = append(frequencyList, "月度")
  90. } else if strings.Contains(col, "季度") {
  91. frequencyList = append(frequencyList, "季度")
  92. } else if strings.Contains(col, "年度") {
  93. frequencyList = append(frequencyList, "年度")
  94. } else {
  95. frequencyList = append(frequencyList, col)
  96. }
  97. }
  98. }
  99. //单位
  100. if k == 10 {
  101. for colIndex, col := range row {
  102. if colIndex == 0 {
  103. continue
  104. }
  105. unitList = append(unitList, col)
  106. }
  107. }
  108. }
  109. if k <= 12 {
  110. continue
  111. }
  112. // 数据处理
  113. var currDate string
  114. for colIndex, col := range row {
  115. // 数据日期
  116. if colIndex == 0 {
  117. currDate = col
  118. continue
  119. }
  120. // 没有数据的情况
  121. if col == `` {
  122. continue
  123. }
  124. // 校验数据长度
  125. lenSheetThirdIndexIdList := len(sheetThirdIndexIdList)
  126. if lenSheetThirdIndexIdList != colIndex {
  127. fmt.Println("长度不一致,sheetThirdIndexIdList:", lenSheetThirdIndexIdList, ";colIndex:", colIndex)
  128. continue
  129. }
  130. thirdIndexId := sheetThirdIndexIdList[colIndex-1]
  131. if _, ok := dataMap[thirdIndexId]; !ok {
  132. tmpDataMap := make(map[string]string)
  133. dataMap[thirdIndexId] = tmpDataMap
  134. }
  135. dataMap[thirdIndexId][currDate] = col
  136. }
  137. }
  138. }
  139. // 调用指标库公共服务处理该数据
  140. HandleExcelDataByEdbLib(dataMap, indexNameList, thirdIndexIdList, frequencyList, unitList, filePath)
  141. }
  142. // ReadExcel 读取Excel
  143. func ReadExcel(filePath string) {
  144. var err error
  145. defer func() {
  146. if err != nil {
  147. tips := fmt.Sprintf("ReadExcel, err: %s", err.Error())
  148. fmt.Println(tips)
  149. }
  150. }()
  151. time.Sleep(5 * time.Second)
  152. excelInfo, e := excelize.OpenFile(filePath)
  153. if e != nil {
  154. err = fmt.Errorf("OpenFile err: %s", e.Error())
  155. return
  156. }
  157. defer func() {
  158. _ = excelInfo.Close()
  159. }()
  160. fmt.Println("读取excel:", filePath)
  161. indexNameList := make([]string, 0)
  162. thirdIndexIdList := make([]string, 0)
  163. frequencyList := make([]string, 0)
  164. unitList := make([]string, 0)
  165. dataMap := make(map[string]map[string]string, 0)
  166. type IndexItem struct {
  167. IndexName string
  168. IndexCode string
  169. Frequency string
  170. Unit string
  171. DataList map[string]string
  172. }
  173. indexes := make([]*IndexItem, 0)
  174. startName := "数据项名称"
  175. sheetList := excelInfo.GetSheetList()
  176. for _, sheet := range sheetList {
  177. rows, e := excelInfo.GetRows(sheet)
  178. if e != nil {
  179. fmt.Println("GetRows: ", e.Error())
  180. continue
  181. }
  182. if len(rows) <= 13 {
  183. fmt.Println("错误的excel表")
  184. continue
  185. }
  186. colIndexMap := make(map[int]*IndexItem) // 列对应的指标信息
  187. colTitle := make(map[int]bool) // 列对应的是否为表头
  188. colDateCols := make(map[int][]int) // 日期列对应的指标列集合
  189. colYes := make(map[int]bool) // 列是否对应指标列
  190. // 遍历行
  191. for rk, row := range rows {
  192. // 遍历列
  193. lastDate := "" // 上一个日期
  194. for ck, col := range row {
  195. col = strings.TrimSpace(col)
  196. // 指标名称行
  197. if rk == 0 {
  198. // 读取到"数据项名称"或空时跳过
  199. if col == startName {
  200. colTitle[ck] = true
  201. colDateCols[ck] = make([]int, 0)
  202. continue
  203. }
  204. if col == "" {
  205. continue
  206. }
  207. colYes[ck] = true
  208. if colIndexMap[ck] == nil {
  209. colIndexMap[ck] = new(IndexItem)
  210. }
  211. colIndexMap[ck].IndexName = col
  212. colIndexMap[ck].DataList = make(map[string]string, 0)
  213. }
  214. // 指标ID
  215. if rk == 1 && colYes[ck] {
  216. colIndexMap[ck].IndexCode = col
  217. }
  218. // 频度
  219. if rk == 8 && colYes[ck] {
  220. if strings.Contains(col, "日度") {
  221. colIndexMap[ck].Frequency = "日度"
  222. } else if strings.Contains(col, "周度") {
  223. colIndexMap[ck].Frequency = "周度"
  224. } else if strings.Contains(col, "旬度") {
  225. colIndexMap[ck].Frequency = "旬度"
  226. } else if strings.Contains(col, "月度") {
  227. colIndexMap[ck].Frequency = "月度"
  228. } else if strings.Contains(col, "季度") {
  229. colIndexMap[ck].Frequency = "季度"
  230. } else if strings.Contains(col, "年度") {
  231. colIndexMap[ck].Frequency = "年度"
  232. } else {
  233. colIndexMap[ck].Frequency = col
  234. }
  235. }
  236. // 单位
  237. if rk == 10 && colYes[ck] {
  238. colIndexMap[ck].Unit = col
  239. }
  240. if rk <= 12 {
  241. continue
  242. }
  243. // 标题列取日期
  244. if colTitle[ck] {
  245. if col == "" {
  246. continue
  247. }
  248. _, e = time.Parse(utils.FormatDate, col)
  249. if e != nil {
  250. continue
  251. }
  252. lastDate = col
  253. }
  254. // 指标列-即值列
  255. if colYes[ck] {
  256. if col == "" || col == "--" || col == "#N/A" {
  257. continue
  258. }
  259. // 取上一个日期列的日期
  260. if lastDate != "" {
  261. colIndexMap[ck].DataList[lastDate] = col
  262. }
  263. }
  264. }
  265. }
  266. // 每一页读取后赋值给indexes
  267. for _, v := range colIndexMap {
  268. indexes = append(indexes, v)
  269. }
  270. }
  271. // 转为指定格式返回
  272. if len(indexes) == 0 {
  273. return
  274. }
  275. for _, v := range indexes {
  276. indexNameList = append(indexNameList, v.IndexName)
  277. thirdIndexIdList = append(thirdIndexIdList, v.IndexCode)
  278. frequencyList = append(frequencyList, v.Frequency)
  279. unitList = append(unitList, v.Unit)
  280. dataMap[v.IndexCode] = v.DataList
  281. }
  282. // 调用指标库公共服务处理该数据
  283. HandleExcelDataByEdbLib(dataMap, indexNameList, thirdIndexIdList, frequencyList, unitList, filePath)
  284. }