daily.go 16 KB

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