package base_from_yongyi_v2 import ( "eta/eta_data_analysis/models" "eta/eta_data_analysis/utils" "fmt" "github.com/xuri/excelize/v2" "strconv" "strings" "time" ) // HandleYongyiExcelWeekly11 月度-原种场二元后备母猪销量及出栏日龄 func HandleYongyiExcelWeekly11(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly11 月度-原种场二元后备母猪销量及出栏日龄 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly11 月度-原种场二元后备母猪销量及出栏日龄 ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) nameMap := make(map[int]string) sort := 0 // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) // 首行,表示时间 if i == 0 { continue } else if i == 1 { //表示表头 // 处理 index指标表 for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k >= 7 && text != "" { text = strings.TrimSpace(text) nameMap[k] = text } } } else { //数据列 date := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k <= 6 { continue } else if k == 7 { // 日期 text = strings.TrimSpace(text) var dateT time.Time dateT, e = time.ParseInLocation("2006年1月", text, time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } date = dateT.Format(utils.FormatDate) fmt.Println(date) continue } else { if i <= 6 { continue } if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } name, ok3 := nameMap[k] if !ok3 { err = fmt.Errorf("找不到对应的指标名称,第%d行,第%d列", i, k) return } if name != "数量" { continue } name = "二元母猪销量" //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, name) // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name) areaPingin := utils.GetFirstPingYin(name) fullIndexNamePingyin := namePrefixPingin + areaPingin indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("indexItem%s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly12 历史猪价 func HandleYongyiExcelWeekly12(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly12 月度-历史猪价 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly12 月度-历史猪价 ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) yearMap := make(map[int]string) sort := 0 // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) // 首行,表示时间 if i == 0 { continue } else if i == 1 { for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if text != "" { text = strings.TrimSpace(text) yearMap[k] = text } } } else { //数据列 month := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 { // 日期 month = strings.TrimSpace(text) continue } else { if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } year, ok := yearMap[k] if !ok { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, "年份不存在")) continue } var dateT time.Time dateT, e = time.ParseInLocation("2006年1月", fmt.Sprintf("%s%s", year, month), time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } date := dateT.Format(utils.FormatDate) fmt.Println(date) // 处理指标名称 fullIndexName := namePrefix fullIndexNamePingyin := namePrefixPingin indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("indexItem%s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly13 二育成本 func HandleYongyiExcelWeekly13(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly13 二育成本 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly13 二育成本 ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, _, _, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) nameMap := make(map[int]string) dateMap := make(map[int]string) dateNameMap := make(map[string]string) finalNameMap := make(map[string]string) unitMap := make(map[string]string) sort := 0 // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) // 首行,表示时间 if i == 0 || i == 1 { continue } else if i == 2 { for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if text != "" { text = strings.TrimSpace(text) nameMap[k] = text } } } else { //数据列 date := "" weight := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 { // 日期 text = strings.TrimSpace(text) var dateT time.Time if dateFloat, e := strconv.ParseFloat(text, 64); e == nil { dateT, e = excelize.ExcelDateToTime(dateFloat, false) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } } else { dateT, e = time.ParseInLocation("2006年1月2日", text, time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } } date = dateT.Format(utils.FormatDate) dateMap[i] = date fmt.Println(date) continue } else { if k == 1 { // 体重 weight = strings.TrimSpace(text) } if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } name, ok := nameMap[k] if !ok { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取 名称失败 Err:%s", sheetName, i, k, "名称不存在")) continue } dateName := fmt.Sprintf("%s_%s", date, name) dateNameWeight := fmt.Sprintf("%s_%s_%s", date, name, weight) fmt.Println(dateNameWeight) unitMap[dateNameWeight] = GetWeekly13IndexUnit(name) if v, ok1 := dateNameMap[dateName]; ok1 { vf, _ := strconv.ParseFloat(v, 64) wf, _ := strconv.ParseFloat(weight, 64) if vf > wf { dateNameWeightPrev := fmt.Sprintf("%s_%s_%s", date, name, v) finalNameMap[dateNameWeightPrev] = GetWeekly13IndexName(name, "高") finalNameMap[dateNameWeight] = GetWeekly13IndexName(name, "低") } else { finalNameMap[dateNameWeight] = GetWeekly13IndexName(name, "高") } } else { dateNameMap[dateName] = weight //默认是低 finalNameMap[dateNameWeight] = GetWeekly13IndexName(name, "低") } } } } for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) // 首行,表示时间 if i == 0 || i == 1 || i == 2 { continue } else { //数据列 date := dateMap[i] weight := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 { continue } else { if k == 1 { // 体重 weight = strings.TrimSpace(text) } if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } name, ok := nameMap[k] if !ok { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取 名称失败 Err:%s", sheetName, i, k, "名称不存在")) continue } finalName, ok := finalNameMap[fmt.Sprintf("%s_%s_%s", date, name, weight)] if !ok { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取 名称失败 Err:%s", sheetName, i, k, "最终名称不存在")) continue } unitNew, ok := unitMap[fmt.Sprintf("%s_%s_%s", date, name, weight)] if !ok { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取 名称失败 Err:%s", sheetName, i, k, "最终名称不存在")) continue } // 处理指标名称 fullIndexName := finalName fullIndexNamePingyin := namePrefixPingin + utils.GetFirstPingYin(fullIndexName) indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unitNew indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("indexItem%s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { //fmt.Printf("IndexName:%s\n", v.IndexName) //fmt.Printf("IndexCode:%s\n", v.IndexCode) indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly14 二育销量 func HandleYongyiExcelWeekly14(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly14 二育销量 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly14 二育销量ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) sort := 0 fmt.Println("最大行") // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) if i <= 2 { continue } else { //数据列 date := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 { if text != "" { text = strings.TrimSpace(text) var dateT time.Time dateSlice := strings.Split(text, "-") if len(dateSlice) <= 1 { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, text)) continue } dateT, e = time.ParseInLocation("2006年1月2日", dateSlice[0]+"日", time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } date = dateT.Format("2006年1月") + dateSlice[1] dateT, e = time.ParseInLocation("2006年1月2日", date, time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } date = dateT.Format(utils.FormatDate) fmt.Println("date: " + date) } continue } else { // 判断出不是字符的,则过滤 if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } name := "二育占实际销量(十天)" // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name) provincePingyin := utils.GetFirstPingYin(name) fullIndexNamePingyin := namePrefixPingin + provincePingyin indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("IndexCode: %s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly15 育肥栏舍利用率 func HandleYongyiExcelWeekly15(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly15 育肥栏舍利用率 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly15 育肥栏舍利用率ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) dateMap := make(map[int]string) sort := 0 fmt.Println("最大行") // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) if i == 0 { continue } else if i == 1 { //表示表头 // 处理 index指标表 for k, text := range row { if text != "" { text = strings.TrimSpace(text) var dateT time.Time dateT, e = time.ParseInLocation("01-2-06", text, time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } dateMap[k] = dateT.Format(utils.FormatDate) fmt.Println("date: " + dateMap[k]) } } } else { //数据列 province := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 || k == 2 || k == 3 { continue } else if k == 1 { province = strings.TrimSpace(text) continue } else { // 判断出不是字符的,则过滤 if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } date, ok2 := dateMap[k] if !ok2 { utils.FileLog.Info(fmt.Sprintf("找不到对应的时间,第%d行,第%d列", i, k)) continue } //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province) // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", namePrefix, province) provincePingyin := utils.GetFullPingYin(province) fullIndexNamePingyin := namePrefixPingin + provincePingyin indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("IndexCode: %s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly16 周度-养殖利润最新 func HandleYongyiExcelWeekly16(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly16 周度-养殖利润最新 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly16 周度-养殖利润最新ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) nameMap := make(map[int]string) sort := 0 fmt.Println("最大行") // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) mergeCellMap, err := GetMergeCells(f, sheetName) if err != nil { err = fmt.Errorf("获取合并单元格失败, sheetName: %s", sheetName) return } for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) currentMergeCells, mergeOk := mergeCellMap[i] if i == 0 { continue } else if i <= 1 || i == 2 { //表示表头 // 处理 index指标表 for k, text := range row { if text != "" { text = strings.TrimSpace(text) if mergeOk { for j, v := range currentMergeCells { nameMap[j] = v } } nameMap[k] = text } } } else { //数据列 date := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 || k == 2 { continue } else if k == 1 { text = strings.TrimSpace(text) var dateT time.Time dateT, e = time.ParseInLocation("2006/1/2", text, time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } date = dateT.Format(utils.FormatDate) fmt.Println(date) continue } else { // 判断出不是字符的,则过滤 if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } name, ok1 := nameMap[k] if !ok1 { err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k) return } if name != "外购仔猪育肥" && name != "合同农户\n(放养部分)" { if !strings.Contains(name, "母猪") { name = fmt.Sprintf("母猪%s", name) } } if name == "合同农户\n(放养部分)" { name = "合同农户(放养部分)" } // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name) areaPingyin := utils.GetFirstPingYin(name) fullIndexNamePingyin := namePrefixPingin + areaPingyin indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("IndexCode: %s\n", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly17 周度-当期、预期成本 func HandleYongyiExcelWeekly17(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly17 周度-当期、预期成本 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly17 周度-当期、预期成本 ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) dateMap := make(map[int]string) sort := 0 // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) if i > 22 { break } if i == 0 { namePrefix = "出栏肥猪成本" namePrefixPingin = "yyzx" + utils.GetFirstPingYin(namePrefix) continue } else if i == 11 { namePrefix = "断奶仔猪对应育肥至标猪出栏成本" namePrefixPingin = "yyzx" + utils.GetFirstPingYin(namePrefix) continue } else if i == 1 || i == 12 { //表示表头 // 处理 index指标表 for k, text := range row { if text != "" { text = strings.TrimSpace(text) //2024.1.12-2024.1.18 dateSlice := strings.Split(text, "-") if len(dateSlice) <= 1 { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } var dateT time.Time dateT, e = time.ParseInLocation("2006.1.2", dateSlice[1], time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } dateMap[k] = dateT.Format(utils.FormatDate) fmt.Println("date: " + dateMap[k]) } } } else { //数据列 name := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 { continue } else if k == 1 || k == 2 { text = strings.TrimSpace(text) if text != "" { name = text } continue } else { // 判断出不是字符的,则过滤 if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } date, ok2 := dateMap[k] if !ok2 { utils.FileLog.Info(fmt.Sprintf("找不到对应的时间,第%d行,第%d列", i, k)) continue } if name != "外购仔猪育肥" && name != "合同农户(放养部分)" { if !strings.Contains(name, "母猪") { name = fmt.Sprintf("母猪%s", name) } } // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name) provincePingyin := utils.GetFirstPingYin(name) fullIndexNamePingyin := namePrefixPingin + provincePingyin indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("IndexCode: %s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly18 育肥全价料出厂价 func HandleYongyiExcelWeekly18(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly18 育肥全价料出厂价 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly18 育肥全价料出厂价ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) provinceMap := make(map[int]string) sort := 0 // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) // 首行,表示时间 if i <= 2 { continue } else if i == 3 { //表示表头 // 处理 index指标表 for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if text != "" { provinceMap[k] = text } } } else { //数据列 date := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 { // 日期 text = strings.TrimSpace(text) var dateT time.Time dateT, e = time.ParseInLocation("01-2-06", text, time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } date = dateT.Format(utils.FormatDate) fmt.Println(date) continue } else { if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } province, ok2 := provinceMap[k] if !ok2 { err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k) return } //fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province) // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", namePrefix, province) provincePingyin := utils.GetFullPingYin(province) fullIndexNamePingyin := namePrefixPingin + provincePingyin if province == "均价" { province = "全国均价" fullIndexName = fmt.Sprintf("%s/%s", namePrefix, province) provincePingyin = utils.GetFirstPingYin(province) fullIndexNamePingyin = namePrefixPingin + provincePingyin } indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("indexItem%s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly19 周度-成本计算附件 func HandleYongyiExcelWeekly19(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly19 周度-成本计算附件 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly19 周度-成本计算附件 ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, _, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) dateMap := make(map[int]string) areaMap := make(map[int]string) valNameMap := make(map[int]string) sort := 0 mergeCellMap, err := GetMergeCells(f, sheetName) if err != nil { err = fmt.Errorf("获取合并单元格失败, sheetName: %s", sheetName) return } // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) name := "" for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) currentMergeCells, mergeOk := mergeCellMap[i] if i == 0 { //表示表头 // 处理 index指标表 for k, text := range row { if k > 9 && text != "" { var dateT time.Time text = strings.TrimSpace(text) //2024.1.12-2024.1.18 if mergeOk { for j, v := range currentMergeCells { if v == "" { continue } dateSlice := strings.Split(v, "-") if len(dateSlice) <= 1 { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败", sheetName, i, k)) continue } dateT, e = time.ParseInLocation("2006.1.2", dateSlice[1], time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } dateMap[j] = dateT.Format(utils.FormatDate) } } dateSlice := strings.Split(text, "-") if len(dateSlice) <= 1 { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } dateT, e = time.ParseInLocation("2006.1.2", dateSlice[1], time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } dateMap[k] = dateT.Format(utils.FormatDate) fmt.Println("date: " + dateMap[k]) } } } else if i == 1 { for k, text := range row { if text != "" { text = strings.TrimSpace(text) if mergeOk { for j, v := range currentMergeCells { areaMap[j] = v } } areaMap[k] = text } } } else if i == 2 { for k, text := range row { if text != "" { text = strings.TrimSpace(text) valNameMap[k] = text } } } else { //数据列 subName := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 || (k >= 5 && k <= 11) { continue } else if k == 1 || k == 2 { text = strings.TrimSpace(text) if text != "" { name = text } //fmt.Printf("name: %s \n", name) continue } else if k == 3 || k == 4 { text = strings.TrimSpace(text) if text != "" { subName = text } continue } else { // 判断出不是字符的,则过滤 if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } valName, ok1 := valNameMap[k] if !ok1 { utils.FileLog.Info(fmt.Sprintf("找不到对应的时间,第%d行,第%d列", i, k)) continue } area, ok2 := areaMap[k] if !ok2 { utils.FileLog.Info(fmt.Sprintf("找不到对应的地区,第%d行,第%d列", i, k)) continue } area = strings.Replace(area, "本周", "", 1) date, ok2 := dateMap[k] if !ok2 { utils.FileLog.Info(fmt.Sprintf("找不到对应的时间,第%d行,第%d列", i, k)) continue } if strings.Contains(name, "-") && !strings.Contains(name, "母猪") { name = "母猪" + name } // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", area, name) provincePingyin := utils.GetFirstPingYin(area + name) fullIndexNamePingyin := namePrefixPingin + provincePingyin if strings.Contains(valName, "成本") { if strings.Contains(area, "出栏肥猪") || name == "料肉比" || name == "上市均重(公斤)" || name == "育肥成活率(中高水平)" || name == "1周" { continue } else { fullIndexName = fmt.Sprintf("%s/%s/%s", area, name, "成本") provincePingyin = utils.GetFirstPingYin(area + name + "成本") fullIndexNamePingyin = namePrefixPingin + provincePingyin } } else { if subName != "" { fullIndexName = fmt.Sprintf("%s/%s/%s", area, name, subName) provincePingyin = utils.GetFirstPingYin(area + name + subName) fullIndexNamePingyin = namePrefixPingin + provincePingyin } } indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } ////fmt.Printf("IndexCode: %s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { //fmt.Printf("IndexName: %s \n", v.IndexName) //fmt.Printf("IndexCode: %s \n", v.IndexCode) indexList = append(indexList, v) } return } // HandleYongyiExcelWeekly20 周度-毛白价差 func HandleYongyiExcelWeekly20(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) { defer func() { if err != nil { //fmt.Printf("HandleYongyiExcelWeekly20 周度-毛白价差 ErrMsg: %s\n", err.Error()) utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly20 周度-毛白价差 ErrMsg: %s", err.Error())) } }() rows, e := f.GetRows(sheetName) if e != nil { err = fmt.Errorf("f GetRows err: %s", e.Error()) return } // 获取指标分类 classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName) // 遍历行读取 indexList = make([]*models.YongyiExcelIndex, 0) nameMap := make(map[int]string) sort := 0 // 指标名称 indexMap := make(map[string]*models.YongyiExcelIndex) for i, row := range rows { //fmt.Printf("当前第%d行 \n", i) // 首行,表示时间 if i == 0 { //表示表头 // 处理 index指标表 for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if text != "" { nameMap[k] = text } } } else { //数据列 date := "" for k, text := range row { //fmt.Printf("当前第%d列 \n", k) if k == 0 { // 日期 text = strings.TrimSpace(text) var dateT time.Time dateT, e = time.ParseInLocation("2006/1/2", text, time.Local) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 读取行,时间列失败 Err:%s", sheetName, i, k, e)) continue } date = dateT.Format(utils.FormatDate) fmt.Println(date) continue } else { if text == "" { continue } if strings.Contains(text, "%") { text = strings.Replace(text, "%", "", 1) } _, e := strconv.ParseFloat(text, 64) if e != nil { utils.FileLog.Info(fmt.Sprintf("sheet:%s 第%d行, 第%d列 strconv.ParseFloat Err:%s", sheetName, i, k, e)) continue } } name, ok2 := nameMap[k] if !ok2 { err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k) return } if name != "前三级别白条价" && name != "生猪出栏价" { continue } // 处理指标名称 fullIndexName := fmt.Sprintf("%s/%s", namePrefix, name) provincePingyin := utils.GetFirstPingYin(name) fullIndexNamePingyin := namePrefixPingin + provincePingyin indexItem, okIndex := indexMap[fullIndexName] if !okIndex { // 新增指标 indexItem = new(models.YongyiExcelIndex) indexItem.IndexName = fullIndexName indexItem.ClassifyName = classifyName indexItem.ClassifySort = classifySort indexItem.IndexCode = fullIndexNamePingyin indexItem.Frequency = frequency indexItem.Sort = sort indexItem.Unit = unit indexItem.ExcelDataMap = make(map[string]string) sort++ } //fmt.Printf("indexItem%s", indexItem.IndexCode) indexItem.ExcelDataMap[date] = text indexMap[fullIndexName] = indexItem continue } } } for _, v := range indexMap { indexList = append(indexList, v) } return }