daily.go 14 KB

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