فهرست منبع

涌溢生猪excel 表格处理

xyxie 1 سال پیش
والد
کامیت
be879eee97
3فایلهای تغییر یافته به همراه158 افزوده شده و 0 حذف شده
  1. 26 0
      models/aaa.go
  2. 1 0
      models/db.go
  3. 131 0
      services/yongyi_pig.go

+ 26 - 0
models/aaa.go

@@ -0,0 +1,26 @@
+package models
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+)
+
+type BaseFromPigTest struct {
+	Id              int `orm:"column(id);pk"`
+	Date            string
+	ScaleField      string
+	Retail          string
+	AveragePrice    string
+	YesterdayChange string
+	LastYear        string
+	Yoy             string
+	Tomorrow        string
+	Province        string
+	Area            string
+}
+
+// Add 新增
+func (r *BaseFromPigTest) Add(list []BaseFromPigTest) (err error) {
+	o := orm.NewOrmUsingDB("data")
+	_, err = o.InsertMulti(len(list), list)
+	return
+}

+ 1 - 0
models/db.go

@@ -56,5 +56,6 @@ func init() {
 		new(BaseFromNationalStatisticsIndex),
 		new(BaseFromNationalStatisticsData),
 		new(MeetingProbabilities),
+		new(BaseFromPigTest),
 	)
 }

+ 131 - 0
services/yongyi_pig.go

@@ -0,0 +1,131 @@
+package services
+
+import (
+	"eta/eta_crawler/models"
+	"fmt"
+	"github.com/tealeg/xlsx"
+	"strconv"
+	"time"
+)
+
+func HandleYongyiExcelDaily(uploadPath string) {
+	var err error
+
+	xlFile, err := xlsx.OpenFile(uploadPath)
+	if err != nil {
+		err = fmt.Errorf("打开文件失败, Err: %s", err)
+		return
+	}
+
+	dateMap := make(map[int]string)
+	nameMap := make(map[int]string)
+
+	dataProvinceMap := make([]models.BaseFromPigTest, 0)
+	dailyPriceSheet, ok := xlFile.Sheet["出栏价"]
+	if ok {
+		sheet := dailyPriceSheet
+		// 遍历行读取
+		maxRow := sheet.MaxRow
+		fmt.Println("最大行")
+		fmt.Println(maxRow)
+		for i := 0; i < maxRow; i++ {
+			if i == 0 { // 首行,表示时间
+				row := sheet.Row(i)
+				cells := row.Cells
+				for k, cell := range cells {
+					text := cell.String()
+					if k > 1 && text != "" {
+						// 检查单元格是否为合并单元格
+						if cell.HMerge > 0 {
+							for j := 1; j <= cell.HMerge; j++ {
+								dateMap[k+j] = text
+							}
+						}
+						fmt.Printf("合并单元格开始列:%d \n", k)
+						dateMap[k] = text
+					}
+				}
+			} else if i == 1 { //表示表头
+				row := sheet.Row(i)
+				cells := row.Cells
+				for k, cell := range cells {
+					text := cell.String()
+					nameMap[k] = text
+				}
+			} else { //数据列
+				row := sheet.Row(i)
+				cells := row.Cells
+				province := ""
+				area := ""
+				dataMap := make(map[string]models.BaseFromPigTest)
+				for k, cell := range cells {
+					text := cell.String()
+					if k == 0 {
+						province = text
+						continue
+					} else if k == 1 {
+						area = text
+						continue
+					}
+
+					date, ok1 := dateMap[k]
+					if !ok1 {
+						err = fmt.Errorf("找不到对应的日期,第%d行,第%d列", i, k)
+						return
+					}
+					name, ok2 := nameMap[k]
+					if !ok2 {
+						err = fmt.Errorf("找不到对应的列名,第%d行,第%d列", i, k)
+						return
+					}
+					var item models.BaseFromPigTest
+					item, _ = dataMap[date]
+					switch name {
+					case "规模场":
+						item.ScaleField = text
+					case "小散户":
+						item.Retail = text
+					case "均价":
+						item.AveragePrice = text
+					case "较昨日涨跌":
+						item.YesterdayChange = text
+					case "去年同期":
+						item.LastYear = text
+					case "同比":
+						item.Yoy = text
+					case "明日预计":
+						item.Tomorrow = text
+					}
+					dataMap[date] = item
+				}
+
+				// 重新整理并汇总
+				for k, item := range dataMap {
+					item.Province = province
+					item.Area = area
+					item.Date = k
+					dataProvinceMap = append(dataProvinceMap, item)
+				}
+			}
+		}
+
+		// 处理最终的数据并入库
+		if len(dataProvinceMap) > 0 {
+			pig := new(models.BaseFromPigTest)
+			err = pig.Add(dataProvinceMap)
+			if err != nil {
+				fmt.Println(err)
+				return
+			}
+		}
+	} else {
+		fmt.Println("该文件不存在")
+	}
+
+}
+
+func excelDateToDate(excelDate string) time.Time {
+	excelTime := time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
+	var days, _ = strconv.Atoi(excelDate)
+	return excelTime.Add(time.Second * time.Duration(days*86400))
+}