daily.go 16 KB

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