zwxi 6 ay önce
ebeveyn
işleme
42d45791a7

+ 48 - 59
controllers/oilchem_data.go

@@ -656,7 +656,7 @@ func (this *TradeCommonController) ExportoilchemList() {
 		br.ErrMsg = "参数解析失败,Err:" + e.Error()
 		return
 	}
-	secNameList := make([]*models.EdbdataExportList, 0)
+	secNameList := make([]*data_manage.BaseFromOilchemIndex, 0)
 
 	dir, _ := os.Executable()
 	exPath := filepath.Dir(dir)
@@ -692,9 +692,8 @@ func (this *TradeCommonController) ExportoilchemList() {
 		return
 	}
 	for _, frequency := range frequencies {
-
 		//获取指标
-		secNameList, err := data_manage.GetOilchemIndexByConditionAndFrequency(condition, *frequency, pars)
+		secNameList, err = data_manage.GetOilchemIndexByConditionAndFrequency(condition, *frequency, pars)
 		if err != nil {
 			fmt.Println("获取数据失败,Err:" + err.Error())
 			return
@@ -703,26 +702,42 @@ func (this *TradeCommonController) ExportoilchemList() {
 			fmt.Println("secNameList长度为0")
 			return
 		}
-		sheetNew, err := xlsxFile.AddSheet(*frequency)
 
+		sheetNew, err := xlsxFile.AddSheet(*frequency)
 		if err != nil {
 			fmt.Println("新增Sheet失败", err.Error())
 			return
 		}
-		//sheetNew.SetColWidth()
-		//获取指标数据
+
 		secNameRow := sheetNew.AddRow()
 		frequencyRow := sheetNew.AddRow()
 		unitRow := sheetNew.AddRow()
 		lastModifyDateRow := sheetNew.AddRow()
-		//获取分类下指标最大数据量
-		dataMax, err := data_manage.GetOilchemDataMaxCount(req.ClassifyId)
+
+		var indexIdList []int
+		for _, sv := range secNameList {
+			indexIdList = append(indexIdList, sv.BaseFromOilchemIndexId)
+		}
+		dataTimeList, err := data_manage.GetOilchemDataDataTimeByIndexId(indexIdList)
 		if err != nil {
-			fmt.Println("获取指标最大数据量失败", err.Error())
+			fmt.Println("获取数据时间失败", err.Error())
 			return
 		}
-		fmt.Println("dataMax:", dataMax)
-		setRowIndex := 6
+
+		// 添加excel左侧指标日期
+		setRowIndex := 4
+		for rk, dv := range dataTimeList {
+			rowIndex := setRowIndex + rk
+			row := sheetNew.Row(rowIndex)
+			displayDate, _ := time.Parse(utils.FormatDate, dv)
+			displayDateCell := row.AddCell()
+			style := new(xlsx.Style)
+			style.ApplyAlignment = true
+			style.Alignment.WrapText = true
+			displayDateCell.SetStyle(style)
+			displayDateCell.SetDate(displayDate)
+
+		}
 		for k, sv := range secNameList {
 			//获取数据
 			dataList, err := data_manage.GetOilchemIndexDataByCode(sv.IndexCode)
@@ -731,60 +746,34 @@ func (this *TradeCommonController) ExportoilchemList() {
 				br.ErrMsg = "获取数据失败,Err:" + err.Error()
 				return
 			}
-			if len(dataList) > 0 {
+			if k == 0 {
 				secNameRow.AddCell().SetValue("指标名称/Metric Name")
 				frequencyRow.AddCell().SetValue("频度/Frequency")
 				unitRow.AddCell().SetValue("单位/Unit")
 				lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
-
-				secNameRow.AddCell().SetValue(sv.IndexName)
-				frequencyRow.AddCell().SetValue(sv.Frequency)
-
-				unitRow.AddCell().SetValue(sv.Unit)
-				lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
-
-				secNameRow.AddCell()
-				frequencyRow.AddCell()
-				unitRow.AddCell()
-				lastModifyDateRow.AddCell()
 				min := k * 3
 				sheetNew.SetColWidth(min, min, 15)
+			}
+			if len(dataList) == 0 {
+				continue
+			}
+			secNameRow.AddCell().SetValue(sv.IndexName)
+			frequencyRow.AddCell().SetValue(sv.Frequency)
+			unitRow.AddCell().SetValue(sv.Unit)
+
+			lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
+			dataInfoMap := make(map[string]*data_manage.BaseFromOilchemData)
+			for _, v := range dataList {
+				dataInfoMap[v.DataTime] = v
+			}
 
-				if len(dataList) <= 0 {
-					for n := 0; n < dataMax; n++ {
-						rowIndex := setRowIndex + n
-						row := sheetNew.Row(rowIndex)
-						row.AddCell()
-						row.AddCell()
-						row.AddCell()
-					}
-				} else {
-					endRowIndex := 0
-					for rk, dv := range dataList {
-						rowIndex := setRowIndex + rk
-						row := sheetNew.Row(rowIndex)
-						displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
-						displayDateCell := row.AddCell()
-						style := new(xlsx.Style)
-						style.ApplyAlignment = true
-						style.Alignment.WrapText = true
-						displayDateCell.SetStyle(style)
-						displayDateCell.SetDate(displayDate)
-
-						row.AddCell().SetValue(dv.Value)
-						row.AddCell()
-						endRowIndex = rowIndex
-					}
-					if len(dataList) < dataMax {
-						dataLen := dataMax - len(dataList)
-						for n := 0; n < dataLen; n++ {
-							rowIndex := (endRowIndex + 1) + n
-							row := sheetNew.Row(rowIndex)
-							row.AddCell()
-							row.AddCell()
-							row.AddCell()
-						}
-					}
+			for rk, dtv := range dataTimeList {
+				rowIndex := setRowIndex + rk
+				row := sheetNew.Row(rowIndex)
+				displayDateCell := row.AddCell()
+				tmpData, ok := dataInfoMap[dtv]
+				if ok {
+					displayDateCell.SetValue(tmpData.Value)
 				}
 			}
 		}
@@ -811,7 +800,7 @@ func (this *TradeCommonController) ExportoilchemList() {
 	}
 	fileName := `隆众资讯`
 	if len(secNameList) > 0 {
-		fileName = secNameList[0].ClassifyName
+		fileName = secNameList[0].IndexName
 	}
 	fileName += time.Now().Format("2006.01.02") + `.xlsx` //文件名称
 	this.Ctx.Output.Download(downLoadnFilePath, fileName)

+ 11 - 0
models/data_manage/base_from_oilchem.go

@@ -285,3 +285,14 @@ func GetOilchemDataViewList(indexIds []int) (items []*BaseFromOilchemData, err e
 	_, err = o.Raw(sql, indexIds).QueryRows(&items)
 	return
 }
+
+// GetOilchemDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
+func GetOilchemDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
+	if len(indexIdList) == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("data")
+	sql := ` SELECT DISTINCT data_time FROM base_from_oilchem_data WHERE base_from_oilchem_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
+	_, err = o.Raw(sql, indexIdList).QueryRows(&items)
+	return
+}