|
@@ -1,186 +0,0 @@
|
|
|
-package services
|
|
|
-
|
|
|
-import (
|
|
|
- "eta/eta_crawler/models"
|
|
|
- "eta/eta_crawler/utils"
|
|
|
- "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)
|
|
|
-
|
|
|
- indexDataList := make([]*models.BaseFromYongyiData, 0)
|
|
|
- indexList := make([]*models.BaseFromYongyiIndex, 0)
|
|
|
- dailyPriceSheet, ok := xlFile.Sheet["出栏价"]
|
|
|
- if ok {
|
|
|
- // todo 处理分类信息,一个sheet表示一个分类
|
|
|
- //classifyName := "日度-商品猪出栏价"
|
|
|
- classifyId := 1
|
|
|
- now := time.Now()
|
|
|
- frequency := "日度"
|
|
|
- unit := "元/公斤"
|
|
|
- namePrefix := "商品猪出栏价"
|
|
|
-
|
|
|
- sheet := dailyPriceSheet
|
|
|
- // 遍历行读取
|
|
|
- maxRow := sheet.MaxRow
|
|
|
- fmt.Println("最大行")
|
|
|
- fmt.Println(maxRow)
|
|
|
- // 指标名称
|
|
|
- indexMap := make(map[string]*models.BaseFromYongyiIndex)
|
|
|
- for i := 0; i < maxRow; i++ {
|
|
|
- fmt.Printf("当前第%d行 \n", 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.IsTime() {
|
|
|
- dateText, _ := cell.GetTime(false)
|
|
|
- text = dateText.Format(utils.FormatDate)
|
|
|
- }
|
|
|
- // 检查单元格是否为合并单元格
|
|
|
- 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 { //表示表头
|
|
|
- // 处理 index指标表
|
|
|
- row := sheet.Row(i)
|
|
|
- cells := row.Cells
|
|
|
- for k, cell := range cells {
|
|
|
- text := cell.String()
|
|
|
- nameMap[k] = text
|
|
|
- }
|
|
|
- } else { //数据列
|
|
|
- // 新增指标记录,并返回对应的ID
|
|
|
- existIndexMap := make(map[string]*models.BaseFromYongyiIndex)
|
|
|
-
|
|
|
- //获取所有指标信息
|
|
|
- allIndex, err := models.GetBaseFromYongyiIndexByClassifyId(classifyId)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- for _, v := range allIndex {
|
|
|
- indexKey := v.IndexName
|
|
|
- existIndexMap[indexKey] = v
|
|
|
- }
|
|
|
- row := sheet.Row(i)
|
|
|
- cells := row.Cells
|
|
|
- province := ""
|
|
|
- for k, cell := range cells {
|
|
|
- fmt.Printf("当前第%d列 \n", k)
|
|
|
- text := cell.String()
|
|
|
- if k == 0 {
|
|
|
- province = text
|
|
|
- continue
|
|
|
- } else if k == 1 {
|
|
|
- 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
|
|
|
- }
|
|
|
- // todo 放到对应的指标名称的下方
|
|
|
- if name != "规模场" && name != "小散户" && name != "均价" {
|
|
|
- // 只处理以上三个类型,其余过滤
|
|
|
- continue
|
|
|
- }
|
|
|
- fmt.Printf("当前第%d行第%d列, 当前省份%s \n", i, k, province)
|
|
|
- // 处理指标名称
|
|
|
- fullIndexName := fmt.Sprintf("%s/%s/%s", namePrefix, province, name)
|
|
|
- indexItem, okIndex := indexMap[fullIndexName]
|
|
|
-
|
|
|
- if !okIndex {
|
|
|
- // 新增指标
|
|
|
- indexItem = new(models.BaseFromYongyiIndex)
|
|
|
- indexItem.IndexName = fullIndexName
|
|
|
- indexItem.ClassifyId = classifyId
|
|
|
- // todo 处理indexCode
|
|
|
- indexItem.IndexCode = fullIndexName
|
|
|
- indexItem.Frequency = frequency
|
|
|
- indexItem.Unit = unit
|
|
|
- indexItem.ModifyTime = now
|
|
|
- indexItem.CreateTime = now
|
|
|
- // todo 查询是否已存在,如果已存在,则获取已存在的指标ID
|
|
|
- if exist, ok3 := existIndexMap[fullIndexName]; ok3 {
|
|
|
- indexItem.YongyiIndexId = exist.YongyiIndexId
|
|
|
- } else {
|
|
|
- indexList = append(indexList, indexItem)
|
|
|
- }
|
|
|
- indexMap[fullIndexName] = indexItem
|
|
|
- } else {
|
|
|
- // todo 查询是否已存在,如果已存在,则获取已存在的指标ID
|
|
|
- if exist, ok3 := existIndexMap[fullIndexName]; ok3 {
|
|
|
- indexItem.YongyiIndexId = exist.YongyiIndexId
|
|
|
- }
|
|
|
- }
|
|
|
- fmt.Printf("indexItem%s", indexItem.IndexCode)
|
|
|
-
|
|
|
- dataItem := new(models.BaseFromYongyiData)
|
|
|
- dataItem.IndexCode = indexItem.IndexCode
|
|
|
- dataItem.YongyiIndexId = indexItem.YongyiIndexId
|
|
|
- dataItem.DataTime = date
|
|
|
- dataItem.Value = text
|
|
|
- dataItem.CreateTime = now
|
|
|
- dataItem.ModifyTime = now
|
|
|
- // 判断指标是否已存在
|
|
|
- indexDataList = append(indexDataList, dataItem)
|
|
|
- continue
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if len(indexList) > 0 {
|
|
|
- index := new(models.BaseFromYongyiIndex)
|
|
|
- err = index.Add(indexList)
|
|
|
- if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
- // 处理最终的数据并入库
|
|
|
- if len(indexDataList) > 0 {
|
|
|
- pigData := new(models.BaseFromYongyiData)
|
|
|
- err = pigData.Add(indexDataList)
|
|
|
- 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))
|
|
|
-}
|