chartWeek.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // HandleYongyiExcelWeekly36 涌溢样本测算
  12. func HandleYongyiExcelWeekly36(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
  13. defer func() {
  14. if err != nil {
  15. fmt.Printf("HandleYongyiExcelWeekly36 涌溢样本测算 ErrMsg: %s\n", err.Error())
  16. utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly36 涌溢样本测算 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. sort := 0
  29. nameMap := make(map[int]string)
  30. // 指标名称
  31. indexMap := make(map[string]*models.YongyiExcelIndex)
  32. for i, row := range rows {
  33. //fmt.Printf("当前第%d行 \n", i)
  34. if i == 0 {
  35. continue
  36. } else if i == 1 {
  37. for k, text := range row {
  38. if text != "" {
  39. text = strings.TrimSpace(text)
  40. nameMap[k] = text
  41. }
  42. }
  43. } else { //数据列
  44. date := ""
  45. for k, text := range row {
  46. //fmt.Printf("当前第%d列 \n", k)
  47. if k == 0 {
  48. text = strings.TrimSpace(text)
  49. var dateT time.Time
  50. dateT, e = time.ParseInLocation("2006年1月", text, time.Local)
  51. if e != nil {
  52. utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e))
  53. continue
  54. }
  55. // 查询当月的最后一天
  56. monthDate := dateT.Format(utils.FormatYearMonthDate)
  57. firstDayStr := monthDate + "-01"
  58. tmpT, _ := time.ParseInLocation(utils.FormatDate, firstDayStr, time.Local)
  59. date = tmpT.AddDate(0, 1, -1).Format(utils.FormatDate)
  60. fmt.Println(date)
  61. continue
  62. } else {
  63. // 判断出不是字符的,则过滤
  64. if text == "" {
  65. continue
  66. }
  67. if strings.Contains(text, "%") {
  68. text = strings.Replace(text, "%", "", 1)
  69. }
  70. _, e := strconv.ParseFloat(text, 64)
  71. if e != nil {
  72. //utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e))
  73. continue
  74. }
  75. }
  76. name, ok2 := nameMap[k]
  77. if !ok2 {
  78. err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
  79. return
  80. }
  81. //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, name)
  82. if strings.Contains(name, "时间") || name == "肉产量(吨)" || name == "肉产量(万吨)" || name == "断奶仔猪量" {
  83. continue
  84. }
  85. switch {
  86. case strings.Contains(name, "分娩母猪窝数"):
  87. unit = "窝"
  88. case strings.Contains(name, "窝均健仔数"):
  89. unit = "头"
  90. case strings.Contains(name, "次月断奶成活率"):
  91. unit = "%"
  92. case strings.Contains(name, "育肥出栏成活率"):
  93. unit = "%"
  94. case strings.Contains(name, "出栏量"):
  95. name = "出栏量"
  96. unit = "头"
  97. case strings.Contains(name, "出栏体重(公斤)"):
  98. name = "出栏体重"
  99. unit = "公斤"
  100. case strings.Contains(name, "出肉率"):
  101. unit = "%"
  102. case strings.Contains(name, "肉产量(公斤)"):
  103. name = "肉产量"
  104. unit = "公斤"
  105. case strings.Contains(name, "猪价(元/公斤)"):
  106. name = "猪价"
  107. unit = "元/公斤"
  108. }
  109. fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name)
  110. namePingin := utils.GetFirstPingYin(name)
  111. fullIndexNamePingyin := namePrefixPingin + namePingin
  112. indexItem, okIndex := indexMap[fullIndexName]
  113. if !okIndex {
  114. // 新增指标
  115. indexItem = new(models.YongyiExcelIndex)
  116. indexItem.IndexName = fullIndexName
  117. indexItem.ClassifyName = classifyName
  118. indexItem.ClassifySort = classifySort
  119. indexItem.IndexCode = fullIndexNamePingyin
  120. indexItem.Frequency = frequency
  121. indexItem.Sort = sort
  122. indexItem.Unit = unit
  123. indexItem.ExcelDataMap = make(map[string]string)
  124. sort++
  125. }
  126. //fmt.Printf("IndexCode: %s", indexItem.IndexCode)
  127. indexItem.ExcelDataMap[date] = text
  128. indexMap[fullIndexName] = indexItem
  129. continue
  130. }
  131. }
  132. }
  133. for _, v := range indexMap {
  134. indexList = append(indexList, v)
  135. }
  136. return
  137. }