Browse Source

fix:添加新的涌益三期指标

zqbao 6 months ago
parent
commit
d3421aac89
2 changed files with 373 additions and 2 deletions
  1. 80 1
      services/base_from_yongyi_v2/base.go
  2. 293 1
      services/base_from_yongyi_v2/week40.go

+ 80 - 1
services/base_from_yongyi_v2/base.go

@@ -4,8 +4,11 @@ import (
 	"eta/eta_data_analysis/models"
 	"eta/eta_data_analysis/utils"
 	"fmt"
-	"github.com/xuri/excelize/v2"
+	"strconv"
 	"strings"
+	"time"
+
+	"github.com/xuri/excelize/v2"
 )
 
 func getClassifySortMap() map[string]int {
@@ -57,6 +60,9 @@ func getClassifySortMap() map[string]int {
 		"月度-能繁母猪存栏量":           45,
 		"月度-原种场二元后备母猪销量及出栏日龄":  46,
 		"涌益样本测算":               47,
+		"周度-养殖利润":              48,
+		"周度-华东冻品价格":            49,
+		"运费":                   50,
 	}
 	return classifyMap
 }
@@ -133,6 +139,9 @@ func init() {
 		"月度-能繁母猪存栏量":           {"月度能繁母猪存栏量", "月度-能繁母猪存栏量", "月度", "头"},
 		"月度-原种场二元后备母猪销量及出栏日龄":  {"原种场二元后备母猪销量及出栏日龄", "月度-原种场二元后备母猪销量及出栏日龄", "月度", "头"},
 		"涌益样本测算":               {"涌益样本测算", "涌益样本测算", "月度", "窝"}, //多个单位:头,%,窝,公斤,元/公斤
+		"周度-养殖利润":              {"外购育肥", "周度-养殖利润", "周度", "元/头"},
+		"华东冻品价格":               {"华东冻品价格", "周度-华东冻品价格", "周度", "元/公斤"},
+		"运费":                   {"9.6米拉猪车主流运费", "运费", "周度", "元/公里"},
 	}
 }
 
@@ -238,3 +247,73 @@ func GetWeekly13IndexUnit(name string) string {
 	}
 	return name
 }
+
+func getSundayOfWeek(year int, week int) string {
+	t := time.Date(year, 1, 1, 0, 0, 0, 0, time.Local)
+	weekday := int(t.Weekday())
+	daysToFirstSunday := (7 - weekday) % 7
+	firstSunday := t.AddDate(0, 0, daysToFirstSunday)
+	daysToDesiredSunday := (week - 1) * 7
+	return firstSunday.AddDate(0, 0, daysToDesiredSunday).Format(utils.FormatDate)
+}
+
+func isValueValid(value string) bool {
+	if value == "" {
+		return false
+	}
+	_, err := strconv.ParseFloat(value, 64)
+	return err == nil
+}
+
+func excelDateToTimeDateStr(excelDate int) string {
+	baseDate := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)
+	return baseDate.AddDate(0, 0, excelDate).Format(utils.FormatDate)
+}
+
+func getMergeCellValMap(f *excelize.File, sheetName string) (mergeIndexDataMap map[string]string, e error) {
+	mergeIndexDataMap = make(map[string]string)
+
+	merges, e := f.GetMergeCells(sheetName)
+	if e != nil {
+		return
+	}
+	for _, merge := range merges {
+		xmin, ymin, e := excelize.CellNameToCoordinates(merge.GetStartAxis())
+		if e != nil {
+			continue
+		}
+		xmax, ymax, e := excelize.CellNameToCoordinates(merge.GetEndAxis())
+		if e != nil {
+			continue
+		}
+		for i := xmin; i <= xmax; i++ {
+			for j := ymin; j <= ymax; j++ {
+				mergeIndexDataMap[fmt.Sprintf("%d:%d", i, j)] = merge.GetCellValue()
+			}
+		}
+	}
+	return
+}
+
+func getCellValue(cell string) (val string, ok bool) {
+	valueArr := strings.Split(cell, "-")
+	switch len(valueArr) {
+	case 1:
+		if !isValueValid(valueArr[0]) {
+			return
+		}
+		return valueArr[0], true
+	case 2:
+		val1, err := strconv.ParseFloat(valueArr[0], 64)
+		if err != nil {
+			return
+		}
+		val2, err := strconv.ParseFloat(valueArr[1], 64)
+		if err != nil {
+			return
+		}
+		mean := (val1 + val2) / 2
+		return fmt.Sprintf("%.2f", mean), true
+	}
+	return "", false
+}

+ 293 - 1
services/base_from_yongyi_v2/week40.go

@@ -4,10 +4,12 @@ import (
 	"eta/eta_data_analysis/models"
 	"eta/eta_data_analysis/utils"
 	"fmt"
-	"github.com/xuri/excelize/v2"
+	"regexp"
 	"strconv"
 	"strings"
 	"time"
+
+	"github.com/xuri/excelize/v2"
 )
 
 // HandleYongyiExcelWeekly31 月度-屠宰厂公母比例
@@ -628,3 +630,293 @@ func HandleYongyiExcelWeekly35(f *excelize.File, sheetName string) (indexList []
 	}
 	return
 }
+
+// HandleYongyiExcelWeekly37 周度-养殖利润
+func HandleYongyiExcelWeekly37(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
+	defer func() {
+		if err != nil {
+			//fmt.Printf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s\n", err.Error())
+			utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly37 周度-养殖利润 ErrMsg: %s", err.Error()))
+		}
+	}()
+	col, e := f.GetCols(sheetName)
+	if e != nil {
+		err = fmt.Errorf("f GetCols err: %s", e.Error())
+		return
+	}
+	yearReg := regexp.MustCompile(`^\d{4}年$`)
+	// 获取指标信息
+	classifyName, classifySort, frequency, unit, namePrefix, namePrefixPingin := GetBaseInfo(sheetName)
+	// GetBaseInfo(sheetName)
+	indexList = make([]*models.YongyiExcelIndex, 0)
+
+	indexInfoMap := make(map[string]*models.YongyiExcelIndex)
+	var indexName string
+	var weekIndexMap = make(map[int]int)
+	var sort = 0
+	for i, c := range col {
+		if len(c) < 2 {
+			continue
+		}
+		if i == 0 {
+			// 记录周度索引
+			for j, v := range c {
+				vInt, e := strconv.Atoi(v)
+				if e != nil {
+					continue
+				}
+				weekIndexMap[j] = vInt
+			}
+		}
+		if strings.TrimSpace(c[1]) != "" {
+			indexName = strings.TrimSpace(c[1])
+		}
+		if indexName == "" {
+			continue
+		}
+		if len(c) < 4 {
+			continue
+		}
+		// 匹配 2021年 的格式
+		if ok := yearReg.MatchString(c[2]); !ok {
+			continue
+		}
+		yearStr := c[2][:4]
+		year, e := strconv.Atoi(yearStr)
+		if e != nil {
+			continue
+		}
+		if indexInfo, ok := indexInfoMap[indexName]; !ok {
+			tmpYongyiIndex := new(models.YongyiExcelIndex)
+			tmpYongyiIndex.IndexName = namePrefix + "/" + indexName
+			tmpYongyiIndex.ClassifyName = classifyName
+			tmpYongyiIndex.ClassifySort = classifySort
+			tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(indexName)
+			tmpYongyiIndex.Frequency = frequency
+			tmpYongyiIndex.Unit = unit
+			tmpYongyiIndex.Sort = sort
+			tmpYongyiIndex.ExcelDataMap = make(map[string]string)
+			for j, v := range c[3:] {
+				dataTime := getSundayOfWeek(year, weekIndexMap[j+3])
+				if isValueValid(v) {
+					tmpYongyiIndex.ExcelDataMap[dataTime] = v
+				}
+			}
+			indexInfoMap[indexName] = tmpYongyiIndex
+			indexList = append(indexList, tmpYongyiIndex)
+			sort++
+		} else {
+			for j, v := range c[3:] {
+				dataTime := getSundayOfWeek(year, weekIndexMap[j+3])
+				if isValueValid(v) {
+					indexInfo.ExcelDataMap[dataTime] = v
+				}
+			}
+		}
+	}
+	return
+}
+
+// HandleYongyiExcelWeekly38 华东冻品价格
+func HandleYongyiExcelWeekly38(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
+	defer func() {
+		if err != nil {
+			//fmt.Printf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s\n", err.Error())
+			utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly37 周度-养殖利润 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)
+	if len(rows) < 2 {
+		err = fmt.Errorf("sheet %s 行数小于2", sheetName)
+		return
+	}
+
+	mergeCellMap, e := getMergeCellValMap(f, sheetName)
+	if e != nil {
+		err = fmt.Errorf("getMergeCellValMap err: %s", e.Error())
+		return
+	}
+	sort := 0
+	var indexNamePart1, indexNamePart2 string
+	indexNamePart3 := make(map[int]string)
+	indexInfoMap := make(map[string]*models.YongyiExcelIndex)
+	dateTimeIndexMap := make(map[int]string)
+	maxLen := 0
+	for _, row := range rows {
+		if len(row) > maxLen {
+			maxLen = len(row)
+		}
+	}
+	for k, v := range mergeCellMap {
+		axis := strings.Split(k, ":")
+		if len(axis) != 2 {
+			continue
+		}
+		x, y := axis[0], axis[1]
+		xInt, e := strconv.Atoi(x)
+		if e != nil {
+			continue
+		}
+		yInt, e := strconv.Atoi(y)
+		if e != nil {
+			continue
+		}
+		tmpList := make([]string, maxLen)
+		copy(tmpList, rows[yInt-1])
+		tmpList[xInt-1] = v
+		rows[yInt-1] = tmpList
+	}
+
+	for i, row := range rows {
+		if i == 0 {
+			continue
+		}
+		// 日期列
+		if i == 1 {
+			for j, cell := range row {
+				if cell == "" {
+					continue
+				}
+				dateInt, err := strconv.Atoi(cell)
+				if err != nil {
+					continue
+				}
+				dateTimeIndexMap[j] = excelDateToTimeDateStr(dateInt)
+			}
+		}
+		// 指标名称列3
+		if i == 2 {
+			for j, cell := range row[2:] {
+				if cell == "" {
+					continue
+				}
+				indexNamePart3[j] = cell
+			}
+		}
+		if i > 2 {
+			for j, cell := range row {
+				if j == 0 && cell != "" {
+					indexNamePart1 = row[0]
+				}
+				if j == 1 && cell != "" {
+					indexNamePart2 = row[1]
+				}
+				if indexNamePart1 == "" || indexNamePart2 == "" {
+					continue
+				}
+				namePart3, ok := indexNamePart3[j-2]
+				if !ok {
+					continue
+				}
+				indexName := fmt.Sprintf("%s/%s/%s", indexNamePart1, indexNamePart2, namePart3)
+				if indexInfo, ok := indexInfoMap[indexName]; !ok {
+					tmpYongyiIndex := new(models.YongyiExcelIndex)
+					tmpYongyiIndex.IndexName = namePrefix + "/" + indexName
+					tmpYongyiIndex.ClassifyName = classifyName
+					tmpYongyiIndex.ClassifySort = classifySort
+					tmpIndexName := strings.ReplaceAll(indexName, "/", "")
+					tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(tmpIndexName)
+					tmpYongyiIndex.Frequency = frequency
+					tmpYongyiIndex.Unit = unit
+					tmpYongyiIndex.Sort = sort
+					tmpYongyiIndex.ExcelDataMap = make(map[string]string)
+					val, ok := getCellValue(cell)
+					if !ok {
+						continue
+					}
+					tmpYongyiIndex.ExcelDataMap[dateTimeIndexMap[j]] = val
+					indexInfoMap[indexName] = tmpYongyiIndex
+					indexList = append(indexList, tmpYongyiIndex)
+					sort++
+				} else {
+					val, ok := getCellValue(cell)
+					if !ok {
+						continue
+					}
+					indexInfo.ExcelDataMap[dateTimeIndexMap[j]] = val
+				}
+			}
+		}
+	}
+	fmt.Println(len(indexList))
+	return
+}
+
+// HandleYongyiExcelWeekly39 运费
+func HandleYongyiExcelWeekly39(f *excelize.File, sheetName string) (indexList []*models.YongyiExcelIndex, err error) {
+	defer func() {
+		if err != nil {
+			//fmt.Printf("HandleYongyiExcelWeekly35 月度-能繁母猪存栏量 ErrMsg: %s\n", err.Error())
+			utils.FileLog.Info(fmt.Sprintf("HandleYongyiExcelWeekly39 月度-能繁母猪存栏量 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)
+	if len(rows) < 2 {
+		err = fmt.Errorf("sheet %s 行数小于2", sheetName)
+		return
+	}
+	var dataTime string
+	indexInfoMap := make(map[string]*models.YongyiExcelIndex)
+	for i, row := range rows {
+		if i <= 1 {
+			continue
+		}
+		if len(row) <= 3 {
+			continue
+		}
+		tmpDate, err := time.Parse("01-02-06", row[2])
+		if err == nil {
+			dataTime = tmpDate.Format(utils.FormatDate)
+		}
+		var fromSrc, toDst string
+		if len([]rune(row[0])) == 2 && len([]rune(row[1])) == 2 {
+			fromSrc = row[0]
+			toDst = row[1]
+		}
+		if fromSrc == "" || toDst == "" {
+			continue
+		}
+		indexName := fmt.Sprintf("%s/%s至%s", namePrefix, fromSrc, toDst)
+		if indexInfo, ok := indexInfoMap[indexName]; !ok {
+			tmpYongyiIndex := new(models.YongyiExcelIndex)
+			tmpYongyiIndex.IndexName = indexName
+			tmpYongyiIndex.ClassifyName = classifyName
+			tmpYongyiIndex.ClassifySort = classifySort
+			tmpIndexName := strings.ReplaceAll(indexName, "/", "")
+			tmpYongyiIndex.IndexCode = namePrefixPingin + utils.GetFirstPingYin(tmpIndexName)
+			tmpYongyiIndex.Frequency = frequency
+			tmpYongyiIndex.Unit = unit
+			tmpYongyiIndex.Sort = 0
+			tmpYongyiIndex.ExcelDataMap = make(map[string]string)
+			val, ok := getCellValue(row[2])
+			if !ok {
+				continue
+			}
+			tmpYongyiIndex.ExcelDataMap[dataTime] = val
+			indexInfoMap[indexName] = tmpYongyiIndex
+			indexList = append(indexList, tmpYongyiIndex)
+		} else {
+			val, ok := getCellValue(row[2])
+			if !ok {
+				continue
+			}
+			indexInfo.ExcelDataMap[dataTime] = val
+		}
+	}
+	fmt.Println(len(indexList))
+	return
+}