daily.go 16 KB

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