Browse Source

fix:钢联excel导出格式

zqbao 5 months ago
parent
commit
eebfbc3ec2

+ 60 - 72
controllers/data_manage/mysteel_chemical_data.go

@@ -666,23 +666,20 @@ func (this *EdbClassifyController) MysteelChemicalExport() {
 	}
 
 	var frequencies []string
+	frequencieSet := make(map[string]struct{})
 	frequencyList, err := data_manage.GetMysteelChemicalFrequency(condition, pars)
-	for _, v := range frequencyList {
-		frequencies = append(frequencies, v.Frequency)
+	if err != nil {
+		br.Msg = "获取频度失败"
+		br.ErrMsg = "获取频度失败,Err:" + err.Error()
+		return
 	}
-
-	fileName := `钢联化工数据`
-	if frequencies == nil {
-		sheet, err := xlsxFile.AddSheet("无数据")
-		if err != nil {
-			br.Msg = "新增Sheet失败"
-			br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
-			return
+	for _, v := range frequencyList {
+		if _, ok := frequencieSet[v.Frequency]; !ok {
+			frequencies = append(frequencies, v.Frequency)
+			frequencieSet[v.Frequency] = struct{}{}
 		}
-		rowSecName := sheet.AddRow()
-		celSecName := rowSecName.AddCell()
-		celSecName.SetValue("")
 	}
+	secNameList := make([]*data_manage.MysteelChemicalList, 0)
 	for _, frequency := range frequencies {
 		//获取指标
 		var tmpCondition string
@@ -706,7 +703,7 @@ func (this *EdbClassifyController) MysteelChemicalExport() {
 			pars = utils.GetLikeKeywordPars(pars, keyword, 2)
 		}
 		//获取指标
-		secNameList, err := data_manage.GetMysteelChemicalIndex(tmpCondition, tmpPars)
+		secNameList, err = data_manage.GetMysteelChemicalIndex(tmpCondition, tmpPars)
 		if err != nil {
 			fmt.Println("获取数据失败,Err:" + err.Error())
 			return
@@ -730,14 +727,31 @@ func (this *EdbClassifyController) MysteelChemicalExport() {
 		frequencyRow := sheetNew.AddRow()
 		unitRow := sheetNew.AddRow()
 		lastModifyDateRow := sheetNew.AddRow()
-		//获取分类下指标最大数据量
-		dataMax, err := data_manage.GetBaseFromMysteelChemicalDataMaxCount(classifyId)
+
+		var indexIdList []int
+		for _, sv := range secNameList {
+			indexIdList = append(indexIdList, sv.Id)
+		}
+		dataTimeList, err := data_manage.GetBaseFromMysteelChemicalDataTimeByIndexId(indexIdList)
 		if err != nil {
-			fmt.Println("获取指标最大数据量失败", err.Error())
+			fmt.Println("获取数据时间失败", err.Error())
 			return
 		}
-		fmt.Println("dataMax:", dataMax)
+
+		//获取分类下指标最大数据量
 		setRowIndex := 6
+		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.GetMysteelChemicalIndexDataByCode(sv.IndexCode)
@@ -746,7 +760,7 @@ func (this *EdbClassifyController) MysteelChemicalExport() {
 				br.ErrMsg = "获取数据失败,Err:" + err.Error()
 				return
 			}
-			if len(dataList) > 0 {
+			if k == 0 {
 				windRow.AddCell().SetValue("钢联")
 				secNameRow.AddCell().SetValue("指标名称")
 				indexCodeRow.AddCell().SetValue("指标ID")
@@ -754,58 +768,29 @@ func (this *EdbClassifyController) MysteelChemicalExport() {
 				unitRow.AddCell().SetValue("单位")
 				lastModifyDateRow.AddCell().SetValue("更新时间")
 
-				secNameRow.AddCell().SetValue(sv.IndexName)
-				indexCodeRow.AddCell().SetValue(sv.IndexCode)
-				frequencyRow.AddCell().SetValue(sv.FrequencyName)
-
-				unitRow.AddCell().SetValue(sv.UnitName)
-				lastModifyDateRow.AddCell().SetValue(sv.UpdateTime)
-
-				windRow.AddCell()
-				windRow.AddCell()
-				secNameRow.AddCell()
-				indexCodeRow.AddCell()
-				frequencyRow.AddCell()
-				unitRow.AddCell()
-				lastModifyDateRow.AddCell()
-				min := k * 3
-				sheetNew.SetColWidth(min, min, 15)
-
-				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.InputValue)
-						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()
-						}
-					}
+			}
+			if len(dataList) == 0 {
+				continue
+
+			}
+			secNameRow.AddCell().SetValue(sv.IndexName)
+			indexCodeRow.AddCell().SetValue(sv.IndexCode)
+			frequencyRow.AddCell().SetValue(sv.FrequencyName)
+			unitRow.AddCell().SetValue(sv.UnitName)
+			lastModifyDateRow.AddCell().SetValue(sv.UpdateTime)
+
+			dataInfoMap := make(map[string]*data_manage.MysteelChemicalData)
+			for _, dv := range dataList {
+				dataInfoMap[dv.DataTime] = dv
+			}
+
+			for rk, dtv := range dataTimeList {
+				rowIndex := setRowIndex + rk
+				row := sheetNew.Row(rowIndex)
+				displayDateCell := row.AddCell()
+				tmpData, ok := dataInfoMap[dtv]
+				if ok {
+					displayDateCell.SetValue(tmpData.InputValue)
 				}
 			}
 		}
@@ -830,7 +815,10 @@ func (this *EdbClassifyController) MysteelChemicalExport() {
 			return
 		}
 	}
-
+	fileName := "上海钢联"
+	if len(secNameList) == 1 {
+		fileName = secNameList[0].IndexName
+	}
 	fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
 	this.Ctx.Output.Download(downLoadnFilePath, fileName)
 	defer func() {

+ 11 - 0
models/data_manage/mysteel_chemical_index.go

@@ -316,6 +316,17 @@ func GetBaseFromMysteelChemicalDataMaxCount(classifyId int) (count int, err erro
 	return
 }
 
+// GetBaseFromMysteelChemicalDataTimeByIndexId 根据指标id获取指标数据的日期列表
+func GetBaseFromMysteelChemicalDataTimeByIndexId(indexIdList []int) (items []string, err error) {
+	if len(indexIdList) == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("data")
+	sql := ` SELECT DISTINCT data_time FROM base_from_mysteel_chemical_data WHERE base_from_mysteel_chemical_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
+	_, err = o.Raw(sql, indexIdList).QueryRows(&items)
+	return
+}
+
 // GetMysteelChemicalIndexDataByCode 通过钢联化工指标code获取所有数据列表
 func GetMysteelChemicalIndexDataByCode(indexCode string) (items []*MysteelChemicalData, err error) {
 	sql := ` SELECT * FROM (