daily.go 14 KB

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