|
@@ -4,6 +4,8 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"hongze/hongze_yb/global"
|
|
|
+ "hongze/hongze_yb/models/request"
|
|
|
+ "hongze/hongze_yb/models/response/chart_info"
|
|
|
chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
|
|
|
chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
|
|
|
company2 "hongze/hongze_yb/models/tables/company"
|
|
@@ -400,8 +402,10 @@ func RefreshChart(chartInfoId int) (err error) {
|
|
|
}
|
|
|
|
|
|
// GetChartEdbData 获取图表的指标数据
|
|
|
-func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping) (edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, sourceArr []string, err error) {
|
|
|
+func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, barChartInfoDateList []request.BarChartInfoDateReq, barChartInfoSort request.BarChartInfoSortReq) (edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, xEdbIdValue []int, yDataList []chart_info.YData, sourceArr []string, err error) {
|
|
|
edbList = make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
|
|
|
+ // 指标对应的所有数据
|
|
|
+ edbDataListMap := make(map[int][]*edbDataModel.EdbDataList)
|
|
|
sourceArr = make([]string, 0)
|
|
|
|
|
|
for _, v := range mappingList {
|
|
@@ -506,6 +510,7 @@ func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate st
|
|
|
}
|
|
|
dataList := make([]*edbDataModel.EdbDataList, 0)
|
|
|
//fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate, ";EdbInfoCategoryType:", v.EdbInfoCategoryType)
|
|
|
+ edbDataListMap[v.EdbInfoId] = dataList
|
|
|
|
|
|
//var newEdbInfo *edbInfoModel.EdbInfo
|
|
|
switch v.EdbInfoCategoryType {
|
|
@@ -639,11 +644,215 @@ func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate st
|
|
|
}
|
|
|
item.DataList = quarterDataList
|
|
|
}
|
|
|
+ } else if chartType == 7 { //柱方图
|
|
|
+ //item.DataList = dataList
|
|
|
} else {
|
|
|
item.DataList = dataList
|
|
|
}
|
|
|
edbList = append(edbList, item)
|
|
|
}
|
|
|
+
|
|
|
+ // 柱方图
|
|
|
+ if chartType == 7 {
|
|
|
+ xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartInfoDateList, barChartInfoSort)
|
|
|
+ } else {
|
|
|
+ xEdbIdValue = make([]int, 0)
|
|
|
+ yDataList = make([]chart_info.YData, 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// BarChartData 柱方图的数据处理
|
|
|
+func BarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDataListMap map[int][]*edbDataModel.EdbDataList, barChartInfoDateList []request.BarChartInfoDateReq, barChartInfoSort request.BarChartInfoSortReq) (edbIdList []int, yDataList []chart_info.YData, err error) {
|
|
|
+ // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
|
|
|
+ edbDataMap := make(map[int]map[string]float64)
|
|
|
+ for edbInfoId, edbDataList := range edbDataListMap {
|
|
|
+ edbDateData := make(map[string]float64)
|
|
|
+ for _, edbData := range edbDataList {
|
|
|
+ edbDateData[edbData.DataTime] = edbData.Value
|
|
|
+ }
|
|
|
+ edbDataMap[edbInfoId] = edbDateData
|
|
|
+ }
|
|
|
+
|
|
|
+ // edbIdList 指标展示顺序;x轴的指标顺序
|
|
|
+ edbIdList = make([]int, 0)
|
|
|
+ //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
|
|
|
+ dateData := make(map[int]float64)
|
|
|
+ if barChartInfoSort.Sort == 0 {
|
|
|
+ for _, v := range mappingList {
|
|
|
+ edbIdList = append(edbIdList, v.EdbInfoId)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ lenBarChartInfoDateList := len(barChartInfoDateList)
|
|
|
+ if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
|
|
|
+ err = errors.New("排序日期异常")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ notDataEdbIdList := make([]int, 0) //没有数据的指标id
|
|
|
+ // 日期配置
|
|
|
+ barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
|
|
|
+ for edbInfoId, dataList := range edbDataListMap {
|
|
|
+ findDate := barChartInfoDate.Date
|
|
|
+ switch barChartInfoDate.Type {
|
|
|
+ case 1: //最新值
|
|
|
+ findDate = dataList[len(dataList)-1].DataTime
|
|
|
+ case 2: //近期几天
|
|
|
+ findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
|
|
|
+
|
|
|
+ lenData := len(dataList) - 1
|
|
|
+
|
|
|
+ for i := lenData; i >= 0; i-- {
|
|
|
+ currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if utils.GetTimeSubDay(currDateTime, findDateTime) >= 5 {
|
|
|
+ findDate = dataList[i].DataTime
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case 3: // 固定日期
|
|
|
+ //最早的日期
|
|
|
+ minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //寻找固定日期的数据
|
|
|
+ findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime.AddDate(0, 0, -1) {
|
|
|
+ tmpDate := tmpDateTime.Format(utils.FormatDate)
|
|
|
+ if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
|
|
|
+ findDate = tmpDate
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
|
|
|
+ dateData[edbInfoId] = tmpValue
|
|
|
+ } else {
|
|
|
+ // 没有数据的指标id
|
|
|
+ notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
|
|
|
+ // 排序
|
|
|
+ dateDataSort := utils.NewMapSorter(dateData)
|
|
|
+ sort.Sort(dateDataSort)
|
|
|
+ if barChartInfoSort.Sort == 1 {
|
|
|
+ // 先将没有数据的指标id放在最前面
|
|
|
+ if len(notDataEdbIdList) > 0 {
|
|
|
+ edbIdList = append(edbIdList, notDataEdbIdList...)
|
|
|
+ }
|
|
|
+ for _, v := range dateDataSort {
|
|
|
+ edbIdList = append(edbIdList, v.Key)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for i := len(dateDataSort) - 1; i >= 0; i-- {
|
|
|
+ edbIdList = append(edbIdList, dateDataSort[i].Key)
|
|
|
+ }
|
|
|
+ // 再将没有数据的指标id放在最后面
|
|
|
+ if len(notDataEdbIdList) > 0 {
|
|
|
+ edbIdList = append(edbIdList, notDataEdbIdList...)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ yDataList = make([]chart_info.YData, 0) //y轴的数据列表
|
|
|
+
|
|
|
+ for _, barChartInfoDate := range barChartInfoDateList {
|
|
|
+ var maxDate time.Time
|
|
|
+
|
|
|
+ findDataList := make([]float64, 0) // 当前日期的数据值
|
|
|
+ for _, edbInfoId := range edbIdList {
|
|
|
+ findDate := barChartInfoDate.Date //需要的日期值
|
|
|
+ dataList := edbDataListMap[edbInfoId] //指标的所有数据值
|
|
|
+ switch barChartInfoDate.Type {
|
|
|
+ case 1: //最新值
|
|
|
+ dataList := edbDataListMap[edbInfoId]
|
|
|
+ findDate = dataList[len(dataList)-1].DataTime
|
|
|
+ case 2: //近期几天
|
|
|
+ findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
|
|
|
+
|
|
|
+ lenData := len(dataList) - 1
|
|
|
+ for i := lenData; i >= 0; i-- {
|
|
|
+ currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if utils.GetTimeSubDay(currDateTime, findDateTime) >= 5 {
|
|
|
+ findDate = dataList[i].DataTime
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case 3: // 固定日期
|
|
|
+ //最早的日期
|
|
|
+ minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //寻找固定日期的数据
|
|
|
+ findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime.AddDate(0, 0, -1) {
|
|
|
+ tmpDate := tmpDateTime.Format(utils.FormatDate)
|
|
|
+ if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
|
|
|
+ findDate = tmpDate
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
|
|
|
+ if maxDate.IsZero() {
|
|
|
+ maxDate = findDateTime
|
|
|
+ } else {
|
|
|
+ if findDateTime.After(maxDate) {
|
|
|
+ maxDate = findDateTime
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
|
|
|
+ findDataList = append(findDataList, tmpValue)
|
|
|
+ } else {
|
|
|
+ findDataList = append(findDataList, 0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ yDataList = append(yDataList, chart_info.YData{
|
|
|
+ Date: maxDate.Format(utils.FormatDate),
|
|
|
+ Value: findDataList,
|
|
|
+ Color: barChartInfoDate.Color,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
return
|
|
|
}
|
|
|
|