daily.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. package base_from_yongyi_v2
  2. import (
  3. "eta/eta_data_analysis/models"
  4. "eta/eta_data_analysis/utils"
  5. "fmt"
  6. "github.com/xuri/excelize/v2"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // HandleYongyiExcelDaily1 日度-商品猪出栏价
  12. func HandleYongyiExcelDaily1(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  13. defer func() {
  14. if err != nil {
  15. fmt.Printf("HandleYongyiExcelDaily1 周度-商品猪出栏价 ErrMsg: %s\n", err.Error())
  16. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDaily1 周度-商品猪出栏价ErrMsg: %s", err.Error()))
  17. }
  18. }()
  19. rows, e := f.GetRows(sheetName)
  20. if e != nil {
  21. err = fmt.Errorf("f GetRows err: %s", e.Error())
  22. return
  23. }
  24. // 获取指标分类
  25. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  26. // 遍历行读取
  27. indexList = make([]*models.YongyiExcelIndex, 0)
  28. dateMap := make(map[int]string)
  29. nameMap := make(map[int]string)
  30. sort := 0
  31. // 指标名称
  32. indexMap := make(map[string]*models.YongyiExcelIndex)
  33. mergeCellMap, err := GetMergeCells(f, sheetName)
  34. if err != nil {
  35. err = fmt.Errorf("获取合并单元格失败, sheetName: %s", sheetName)
  36. return
  37. }
  38. for i, row := range rows {
  39. fmt.Printf("当前第%d行 \n", i)
  40. currentMergeCells, mergeOk := mergeCellMap[i]
  41. // 首行,表示时间
  42. if i == 0 {
  43. for k, text := range row {
  44. fmt.Printf("当前第%d列 \n", k)
  45. if k > 1 && text != "" {
  46. text = strings.TrimSpace(text)
  47. if mergeOk {
  48. for j, v := range currentMergeCells {
  49. // 日期
  50. v = strings.TrimSpace(v)
  51. var dateT time.Time
  52. dateT, e = time.ParseInLocation("01-2-06", v, time.Local)
  53. if e != nil {
  54. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  55. continue
  56. }
  57. dateMap[j] = dateT.Format(utils.FormatDate)
  58. }
  59. }
  60. // 日期
  61. text = strings.TrimSpace(text)
  62. var dateT time.Time
  63. dateT, e = time.ParseInLocation("01-2-06", text, time.Local)
  64. if e != nil {
  65. //utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  66. continue
  67. }
  68. dateMap[k] = dateT.Format(utils.FormatDate)
  69. }
  70. }
  71. } else if i == 1 { //表示表头
  72. // 处理 index指标表
  73. for k, text := range row {
  74. fmt.Printf("当前第%d列 \n", k)
  75. if text != "" {
  76. nameMap[k] = text
  77. }
  78. }
  79. } else { //数据列
  80. province := ""
  81. for k, text := range row {
  82. text = strings.TrimSpace(text)
  83. fmt.Printf("当前第%d列 \n", k)
  84. if k == 0 {
  85. province = text
  86. continue
  87. } else if k == 1 {
  88. continue
  89. } else {
  90. if text == "" {
  91. continue
  92. }
  93. if strings.Contains(text, "%") {
  94. text = strings.Replace(text, "%", "", 1)
  95. }
  96. _, e := strconv.ParseFloat(text, 64)
  97. if e != nil {
  98. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  99. continue
  100. }
  101. }
  102. date, ok1 := dateMap[k]
  103. if !ok1 {
  104. utils.FileLog.Info(fmt.Sprintf("找不到对应的日期,第%d行,第%d列, text:%s", i, k, text))
  105. continue
  106. }
  107. name, ok2 := nameMap[k]
  108. if !ok2 {
  109. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  110. return
  111. }
  112. if name == "全国均价" {
  113. name = "全国"
  114. }
  115. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  116. // 处理指标名称
  117. fullIndexName := fmt.Sprintf("%s/%s/%s", namePrefix, province, name)
  118. if name != "规模场" && name != "小散户" && name != "均价" {
  119. // 只处理以上三个类型,其余过滤
  120. continue
  121. }
  122. if fullIndexName == "商品猪出栏价/全国/均价" {
  123. continue
  124. }
  125. provincePingyin := utils.GetFullPingYin(province)
  126. namePingin := utils.GetFirstPingYin(name)
  127. fullIndexNamePingyin := fmt.Sprintf("%s%s%s", namePrefixPingin, provincePingyin, namePingin)
  128. indexItem, okIndex := indexMap[fullIndexName]
  129. if !okIndex {
  130. // 新增指标
  131. indexItem = new(models.YongyiExcelIndex)
  132. indexItem.IndexName = fullIndexName
  133. indexItem.ClassifyName = classifyName
  134. indexItem.ClassifySort = classifySort
  135. indexItem.IndexCode = fullIndexNamePingyin
  136. indexItem.Frequency = frequency
  137. indexItem.Sort = sort
  138. indexItem.Unit = unit
  139. indexItem.ExcelDataMap = make(map[string]string)
  140. sort++
  141. }
  142. indexItem.ExcelDataMap[date] = text
  143. indexMap[fullIndexName] = indexItem
  144. continue
  145. }
  146. }
  147. }
  148. for _, v := range indexMap {
  149. fmt.Printf("IndexName: %s \n", v.IndexName)
  150. fmt.Printf("IndexCode: %s \n", v.IndexCode)
  151. indexList = append(indexList, v)
  152. }
  153. return
  154. }
  155. // HandleYongyiExcelDaily2 日度-商品猪标肥价差
  156. func HandleYongyiExcelDaily2(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  157. defer func() {
  158. if err != nil {
  159. fmt.Printf("HandleYongyiExcelDaily2 日度-商品猪标肥价差 ErrMsg: %s\n", err.Error())
  160. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDaily2 日度-商品猪标肥价差 ErrMsg: %s", err.Error()))
  161. }
  162. }()
  163. rows, e := f.GetRows(sheetName)
  164. if e != nil {
  165. err = fmt.Errorf("f GetRows err: %s", e.Error())
  166. return
  167. }
  168. // 获取指标分类
  169. classifyName, classifySort, frequency, unit, _, namePrefixPingin := GetBaseInfo(sheetName)
  170. // 遍历行读取
  171. indexList = make([]*models.YongyiExcelIndex, 0)
  172. dateMap := make(map[int]string)
  173. nameMap := make(map[int]string)
  174. sort := 0
  175. // 指标名称
  176. indexMap := make(map[string]*models.YongyiExcelIndex)
  177. mergeCellMap, err := GetMergeCells(f, sheetName)
  178. if err != nil {
  179. err = fmt.Errorf("获取合并单元格失败, sheetName: %s", sheetName)
  180. return
  181. }
  182. for i, row := range rows {
  183. fmt.Printf("当前第%d行 \n", i)
  184. currentMergeCells, mergeOk := mergeCellMap[i]
  185. // 首行,表示时间
  186. if i == 0 {
  187. for k, text := range row {
  188. fmt.Printf("当前第%d列 \n", k)
  189. if text != "" {
  190. text = strings.TrimSpace(text)
  191. if mergeOk {
  192. for j, v := range currentMergeCells {
  193. // 日期
  194. v = strings.TrimSpace(v)
  195. var dateT time.Time
  196. dateT, e = time.ParseInLocation("01-2-06", v, time.Local)
  197. if e != nil {
  198. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  199. continue
  200. }
  201. dateMap[j] = dateT.Format(utils.FormatDate)
  202. }
  203. }
  204. // 日期
  205. text = strings.TrimSpace(text)
  206. var dateT time.Time
  207. dateT, e = time.ParseInLocation("01-2-06", text, time.Local)
  208. if e != nil {
  209. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  210. continue
  211. }
  212. dateMap[k] = dateT.Format(utils.FormatDate)
  213. }
  214. }
  215. } else if i == 1 { //表示表头
  216. // 处理 index指标表
  217. for k, text := range row {
  218. fmt.Printf("当前第%d列 \n", k)
  219. if text != "" {
  220. nameMap[k] = text
  221. }
  222. }
  223. } else { //数据列
  224. province := ""
  225. for k, text := range row {
  226. text = strings.TrimSpace(text)
  227. fmt.Printf("当前第%d列 \n", k)
  228. if k == 0 {
  229. province = text
  230. continue
  231. } else {
  232. if text == "" {
  233. continue
  234. }
  235. if strings.Contains(text, "%") {
  236. text = strings.Replace(text, "%", "", 1)
  237. }
  238. _, e := strconv.ParseFloat(text, 64)
  239. if e != nil {
  240. //utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  241. continue
  242. }
  243. }
  244. date, ok1 := dateMap[k]
  245. if !ok1 {
  246. utils.FileLog.Info(fmt.Sprintf("找不到对应的日期,第%d行,第%d列, text:%s", i, k, text))
  247. continue
  248. }
  249. name, ok2 := nameMap[k]
  250. if !ok2 {
  251. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  252. return
  253. }
  254. if province == "全国均价" {
  255. province = "全国"
  256. }
  257. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  258. // 处理指标名称
  259. // 过滤特殊的指标 放到对应的指标名称的下方
  260. if name != "市场标重猪" && name != "150公斤左右较标猪" && name != "175公斤左右较标猪" {
  261. // 只处理以上三个类型,其余过滤
  262. continue
  263. }
  264. if name == "150公斤左右较标猪" {
  265. name = "150公斤较标猪价差"
  266. } else if name == "175公斤左右较标猪" {
  267. name = "175公斤较标猪价差"
  268. } else if name == "市场标重猪" {
  269. name = "市场标重猪价"
  270. } else if name == "全国均价" {
  271. name = "全国"
  272. }
  273. fullIndexName := fmt.Sprintf("%s/%s", name, province)
  274. provincePingyin := utils.GetFullPingYin(province)
  275. namePingin := utils.GetFirstPingYin(name)
  276. fullIndexNamePingyin := fmt.Sprintf("%s%s%s", namePrefixPingin, namePingin, provincePingyin)
  277. indexItem, okIndex := indexMap[fullIndexName]
  278. if !okIndex {
  279. // 新增指标
  280. indexItem = new(models.YongyiExcelIndex)
  281. indexItem.IndexName = fullIndexName
  282. indexItem.ClassifyName = classifyName
  283. indexItem.ClassifySort = classifySort
  284. indexItem.IndexCode = fullIndexNamePingyin
  285. indexItem.Frequency = frequency
  286. indexItem.Sort = sort
  287. indexItem.Unit = unit
  288. indexItem.ExcelDataMap = make(map[string]string)
  289. sort++
  290. }
  291. indexItem.ExcelDataMap[date] = text
  292. indexMap[fullIndexName] = indexItem
  293. continue
  294. }
  295. }
  296. }
  297. for _, v := range indexMap {
  298. fmt.Printf("IndexName: %s \n", v.IndexName)
  299. fmt.Printf("IndexCode: %s \n", v.IndexCode)
  300. indexList = append(indexList, v)
  301. }
  302. return
  303. }
  304. // HandleYongyiExcelDaily3 日度-商品猪全国均价和宰量
  305. func HandleYongyiExcelDaily3(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  306. defer func() {
  307. if err != nil {
  308. fmt.Printf("HandleYongyiExcelDaily3 日度-商品猪全国均价和宰量 ErrMsg: %s\n", err.Error())
  309. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDaily3 日度-商品猪全国均价和宰量 ErrMsg: %s", err.Error()))
  310. }
  311. }()
  312. rows, e := f.GetRows(sheetName)
  313. if e != nil {
  314. err = fmt.Errorf("f GetRows err: %s", e.Error())
  315. return
  316. }
  317. // 获取指标分类
  318. classifyName, classifySort, frequency, unit, _, _ := GetBaseInfo(sheetName)
  319. // 遍历行读取
  320. indexList = make([]*models.YongyiExcelIndex, 0)
  321. nameMap := make(map[int]string)
  322. sort := 0
  323. // 指标名称
  324. indexMap := make(map[string]*models.YongyiExcelIndex)
  325. for i, row := range rows {
  326. fmt.Printf("当前第%d行 \n", i)
  327. // 首行,表示时间
  328. if i == 0 {
  329. for k, text := range row {
  330. fmt.Printf("当前第%d列 \n", k)
  331. text = strings.TrimSpace(text)
  332. if text != "" {
  333. // 日期
  334. nameMap[k] = text
  335. }
  336. }
  337. } else { //数据列
  338. date := ""
  339. for k, text := range row {
  340. text = strings.TrimSpace(text)
  341. fmt.Printf("当前第%d列 \n", k)
  342. if k == 0 {
  343. var dateT time.Time
  344. dateT, e = time.ParseInLocation("01-2-06", text, time.Local)
  345. if e != nil {
  346. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  347. continue
  348. }
  349. date = dateT.Format(utils.FormatDate)
  350. continue
  351. } else {
  352. if text == "" {
  353. continue
  354. }
  355. if strings.Contains(text, "%") {
  356. text = strings.Replace(text, "%", "", 1)
  357. }
  358. _, e := strconv.ParseFloat(text, 64)
  359. if e != nil {
  360. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  361. continue
  362. }
  363. }
  364. name, ok2 := nameMap[k]
  365. if !ok2 {
  366. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  367. return
  368. }
  369. if name != "全国均价" && name != "日屠宰量" {
  370. // 只处理以上三个类型,其余过滤
  371. continue
  372. }
  373. var fullIndexNamePingyin string
  374. if name == "全国均价" {
  375. name = "商品猪出拦价/全国/均价"
  376. fullIndexNamePingyin = "yyzxspzcljquanguojj"
  377. unit = "元/公斤"
  378. } else if name == "日屠宰量" {
  379. name = "商品猪日屠宰量/全国"
  380. fullIndexNamePingyin = "yyzxspzrtzlquanguo"
  381. unit = "头/日"
  382. }
  383. fullIndexName := name
  384. indexItem, okIndex := indexMap[fullIndexName]
  385. if !okIndex {
  386. // 新增指标
  387. indexItem = new(models.YongyiExcelIndex)
  388. indexItem.IndexName = fullIndexName
  389. indexItem.ClassifyName = classifyName
  390. indexItem.ClassifySort = classifySort
  391. indexItem.IndexCode = fullIndexNamePingyin
  392. indexItem.Frequency = frequency
  393. indexItem.Sort = sort
  394. indexItem.Unit = unit
  395. indexItem.ExcelDataMap = make(map[string]string)
  396. sort++
  397. }
  398. indexItem.ExcelDataMap[date] = text
  399. indexMap[fullIndexName] = indexItem
  400. continue
  401. }
  402. }
  403. }
  404. for _, v := range indexMap {
  405. fmt.Printf("IndexName: %s \n", v.IndexName)
  406. fmt.Printf("IndexCode: %s \n", v.IndexCode)
  407. indexList = append(indexList, v)
  408. }
  409. return
  410. }
  411. // HandleYongyiExcelDaily4 日度-屠宰企业屠宰量
  412. func HandleYongyiExcelDaily4(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  413. defer func() {
  414. if err != nil {
  415. fmt.Printf("HandleYongyiExcelDaily4 日度-屠宰企业屠宰量 ErrMsg: %s\n", err.Error())
  416. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelDaily4 日度-屠宰企业屠宰量 ErrMsg: %s", err.Error()))
  417. }
  418. }()
  419. rows, e := f.GetRows(sheetName)
  420. if e != nil {
  421. err = fmt.Errorf("f GetRows err: %s", e.Error())
  422. return
  423. }
  424. // 获取指标分类
  425. classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
  426. // 遍历行读取
  427. indexList = make([]*models.YongyiExcelIndex, 0)
  428. dateMap := make(map[int]string)
  429. sort := 0
  430. // 指标名称
  431. indexMap := make(map[string]*models.YongyiExcelIndex)
  432. for i, row := range rows {
  433. fmt.Printf("当前第%d行 \n", i)
  434. // 首行,表示时间
  435. if i == 0 {
  436. for k, text := range row {
  437. fmt.Printf("当前第%d列 \n", k)
  438. if text != "" {
  439. // 日期
  440. text = strings.TrimSpace(text)
  441. dateSlice := strings.Split(text, "(")
  442. if len(dateSlice) > 1 {
  443. text = dateSlice[0]
  444. }
  445. var dateT time.Time
  446. //判断是否是数字,如果是数字,另外单独处理
  447. if dateFloat, e := strconv.ParseFloat(text, 64); e == nil {
  448. dateT, e = excelize.ExcelDateToTime(dateFloat, false)
  449. if e != nil {
  450. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  451. continue
  452. }
  453. } else {
  454. dateT, e = time.ParseInLocation("01-2-06", text, time.Local)
  455. if e != nil {
  456. dateT, e = time.ParseInLocation("2006/1/2", text, time.Local)
  457. if e != nil {
  458. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  459. continue
  460. }
  461. }
  462. }
  463. dateMap[k] = dateT.Format(utils.FormatDate)
  464. }
  465. }
  466. } else { //数据列
  467. province := ""
  468. for k, text := range row {
  469. text = strings.TrimSpace(text)
  470. fmt.Printf("当前第%d列 \n", k)
  471. if k == 0 {
  472. province = text
  473. continue
  474. } else {
  475. if text == "" {
  476. continue
  477. }
  478. if strings.Contains(text, "%") {
  479. text = strings.Replace(text, "%", "", 1)
  480. }
  481. _, e := strconv.ParseFloat(text, 64)
  482. if e != nil {
  483. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  484. continue
  485. }
  486. }
  487. date, ok1 := dateMap[k]
  488. if !ok1 {
  489. utils.FileLog.Info(fmt.Sprintf("找不到对应的日期,第%d行,第%d列, text:%s", i, k, text))
  490. continue
  491. }
  492. if province == "合计(单位:头)" {
  493. continue
  494. }
  495. fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
  496. // 处理指标名称
  497. // 过滤特殊的指标 放到对应的指标名称的下方
  498. fullIndexName := fmt.Sprintf("%s/%s", namePrefix, province)
  499. provincePingyin := utils.GetFullPingYin(province)
  500. fullIndexNamePingyin := fmt.Sprintf("%s%s", namePrefixPingin, provincePingyin)
  501. indexItem, okIndex := indexMap[fullIndexName]
  502. if !okIndex {
  503. // 新增指标
  504. indexItem = new(models.YongyiExcelIndex)
  505. indexItem.IndexName = fullIndexName
  506. indexItem.ClassifyName = classifyName
  507. indexItem.ClassifySort = classifySort
  508. indexItem.IndexCode = fullIndexNamePingyin
  509. indexItem.Frequency = frequency
  510. indexItem.Sort = sort
  511. indexItem.Unit = unit
  512. indexItem.ExcelDataMap = make(map[string]string)
  513. sort++
  514. }
  515. indexItem.ExcelDataMap[date] = text
  516. indexMap[fullIndexName] = indexItem
  517. continue
  518. }
  519. }
  520. }
  521. for _, v := range indexMap {
  522. fmt.Printf("IndexName: %s \n", v.IndexName)
  523. fmt.Printf("IndexCode: %s \n", v.IndexCode)
  524. indexList = append(indexList, v)
  525. }
  526. return
  527. }