123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308 |
- 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
- }
|