package data import ( "encoding/json" "errors" "eta/eta_mobile/models/company" "eta/eta_mobile/models/data_manage" "eta/eta_mobile/models/system" "eta/eta_mobile/services/alarm_msg" "eta/eta_mobile/utils" "fmt" "github.com/shopspring/decimal" "math" "sort" "strconv" "strings" "time" ) type ChartInfoReq struct { ChartInfoId int `description:"图表id,新增时传0"` } // DeleteChartInfoDataRedis 清除图表缓存 func DeleteChartInfoDataRedis(bodyByte []byte) (err error) { var req ChartInfoReq err = json.Unmarshal(bodyByte, &req) if err != nil { return } if req.ChartInfoId > 0 { err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId)) } return } // DeleteChartClassifyRedis 清除图表分类缓存 func DeleteChartClassifyRedis(bodyByte []byte) (err error) { err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY) return } // GetChartInfoDataKey 获取图表缓存的key func GetChartInfoDataKey(chartInfoId int) string { key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId) return key } // CheckOpChartPermission 判断图表操作权限 func CheckOpChartPermission(sysUser *system.Admin, createUserId int) (ok bool) { if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN { ok = true } // 如果图表创建人与当前操作人相同的话,那么就是允许操作 if ok == false && createUserId == sysUser.AdminId { ok = true } // 如果图表权限id 是 1 ,那么允许编辑 if ok == false && sysUser.ChartPermission == 1 { ok = true } return } // GetChartEdbData 获取图表的指标数据 func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) { edbList = make([]*data_manage.ChartEdbInfoMapping, 0) xEdbIdValue = make([]int, 0) yDataList = make([]data_manage.YData, 0) var extraConfig interface{} switch chartType { case 7: // 柱形图 var barConfig data_manage.BarChartInfoReq if extraConfigStr == `` { errMsg = "柱方图未配置" err = errors.New(errMsg) return } err = json.Unmarshal([]byte(extraConfigStr), &barConfig) if err != nil { errMsg = "柱方图配置异常" err = errors.New(errMsg) return } extraConfig = barConfig case 10: // 截面散点图 var tmpExtraConfig data_manage.SectionScatterReq if extraConfigStr == `` { errMsg = "截面散点图未配置" err = errors.New(errMsg) return } err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig) if err != nil { errMsg = "截面散点配置异常" err = errors.New(errMsg) return } extraConfig = tmpExtraConfig default: xEdbIdValue = make([]int, 0) yDataList = make([]data_manage.YData, 0) } // 指标对应的所有数据 edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList) if err != nil { return } // 特殊图形数据处理 switch chartType { case 7: // 柱形图 barChartConf := extraConfig.(data_manage.BarChartInfoReq) xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort) for _, v := range edbList { // 指标别名 if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 { for _, reqEdb := range barChartConf.EdbInfoIdList { if v.EdbInfoId == reqEdb.EdbInfoId { v.EdbAliasName = reqEdb.Name } } } } for k := range yDataList { yDataList[k].Unit = barChartConf.Unit yDataList[k].UnitEn = barChartConf.UnitEn } case 10: // 截面散点图 sectionScatterConf := extraConfig.(data_manage.SectionScatterReq) xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf) // 这个数据没有必要返回给前端 for _, v := range edbList { v.DataList = nil } } return } // GetEdbDataMapList 获取指标最后的基础数据 func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) { edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList) return } // getEdbDataMapList 获取指标最后的基础数据 func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) { // 指标对应的所有数据 edbDataListMap = make(map[int][]*data_manage.EdbDataList) for _, v := range mappingList { //fmt.Println("v:", v.EdbInfoId) item := new(data_manage.ChartEdbInfoMapping) item.EdbInfoId = v.EdbInfoId item.SourceName = v.SourceName item.Source = v.Source item.EdbCode = v.EdbCode item.EdbName = v.EdbName item.EdbNameEn = v.EdbNameEn item.Frequency = v.Frequency item.EdbType = v.EdbType item.FrequencyEn = GetFrequencyEn(v.Frequency) if v.Unit != `无` { item.Unit = v.Unit } item.UnitEn = v.UnitEn item.StartDate = v.StartDate item.EndDate = v.EndDate item.ModifyTime = v.ModifyTime item.EdbInfoCategoryType = v.EdbInfoCategoryType item.PredictChartColor = v.PredictChartColor item.ClassifyId = v.ClassifyId if chartInfoId <= 0 { item.IsAxis = 1 item.LeadValue = 0 item.LeadUnit = "" item.ChartEdbMappingId = 0 item.ChartInfoId = 0 item.IsOrder = false item.EdbInfoType = 1 item.ChartStyle = "" item.ChartColor = "" item.ChartWidth = 0 item.MaxData = v.MaxValue item.MinData = v.MinValue } else { item.IsAxis = v.IsAxis item.EdbInfoType = v.EdbInfoType item.LeadValue = v.LeadValue item.LeadUnit = v.LeadUnit item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit) item.ChartEdbMappingId = v.ChartEdbMappingId item.ChartInfoId = v.ChartInfoId item.ChartStyle = v.ChartStyle item.ChartColor = v.ChartColor item.ChartWidth = v.ChartWidth item.IsOrder = v.IsOrder item.MaxData = v.MaxData item.MinData = v.MinData } item.LatestValue = v.LatestValue item.LatestDate = v.LatestDate item.UniqueCode = v.UniqueCode item.MoveLatestDate = v.LatestDate var startDateReal string var diffSeconds int64 if chartType == 2 { //季节性图 startDateReal = startDate } else { if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标 var startTimeRealTemp time.Time startDateParse, _ := time.Parse(utils.FormatDate, startDate) switch v.LeadUnit { case "天": startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue) case "月": startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0) case "季": startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0) case "周": startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue) case "年": startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0) } if startTimeRealTemp.Before(startDateParse) { startDateReal = startTimeRealTemp.Format(utils.FormatDate) diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6 } else { startDateReal = startDate diffSeconds = 0 } // 预测指标的开始日期也要偏移 { day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal) if tmpErr != nil { err = tmpErr return } latestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.LatestDate, time.Local) if tmpErr != nil { err = tmpErr return } item.LatestDate = latestDateTime.AddDate(0, 0, day).Format(utils.FormatDate) } } else { startDateReal = startDate } } //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate) calendarPreYear := 0 if calendar == "农历" { newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal) if err != nil { fmt.Println("time.Parse:" + err.Error()) } calendarPreYear = newStartDateReal.Year() - 1 newStartDateReal = newStartDateReal.AddDate(-1, 0, 0) startDateReal = newStartDateReal.Format(utils.FormatDate) } dataList := make([]*data_manage.EdbDataList, 0) //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate) //var newEdbInfo *data_manage.EdbInfo switch v.EdbInfoCategoryType { case 0: dataList, err = data_manage.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate) case 1: _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false) default: err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType)) } if err != nil { return } edbDataListMap[v.EdbInfoId] = dataList if diffSeconds != 0 && v.EdbInfoType == 0 { dataListLen := len(dataList) for i := 0; i < dataListLen; i++ { dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds } } if chartType == 2 { latestDateStr := v.LatestDate //实际数据的截止日期 latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate) if tmpErr != nil { //item.DataList = dataList item.IsNullData = true edbList = append(edbList, item) continue err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate)) return } latestDateYear := latestDate.Year() //实际数据截止年份 if calendar == "农历" { if len(dataList) <= 0 { result := new(data_manage.EdbDataResult) item.DataList = result } else { result, tmpErr := data_manage.AddCalculateQuarterV4(dataList) if tmpErr != nil { err = errors.New("获取农历数据失败,Err:" + tmpErr.Error()) return } // 处理季节图的截止日期 for k, edbDataItems := range result.List { var cuttingDataTimestamp int64 // 切割的日期时间字符串 cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate) //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳 if edbDataItems.Year >= latestDateYear { for _, tmpData := range edbDataItems.Items { if tmpData.DataTime == cuttingDataTimeStr { cuttingDataTimestamp = tmpData.DataTimestamp break } } } edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp result.List[k] = edbDataItems } if result.List[0].Year != calendarPreYear { itemList := make([]*data_manage.EdbDataList, 0) items := new(data_manage.EdbDataItems) //items.Year = calendarPreYear items.Items = itemList newResult := new(data_manage.EdbDataResult) newResult.List = append(newResult.List, items) newResult.List = append(newResult.List, result.List...) item.DataList = newResult } else { item.DataList = result } } } else { currentYear := time.Now().Year() quarterDataList := make([]*data_manage.QuarterData, 0) quarterMap := make(map[int][]*data_manage.EdbDataList) var quarterArr []int for _, v := range dataList { itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime) if tmpErr != nil { err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime) return } year := itemDate.Year() newItemDate := itemDate.AddDate(currentYear-year, 0, 0) timestamp := newItemDate.UnixNano() / 1e6 v.DataTimestamp = timestamp if findVal, ok := quarterMap[year]; !ok { quarterArr = append(quarterArr, year) findVal = append(findVal, v) quarterMap[year] = findVal } else { findVal = append(findVal, v) quarterMap[year] = findVal } } for _, v := range quarterArr { itemList := quarterMap[v] quarterItem := new(data_manage.QuarterData) quarterItem.Year = v quarterItem.DataList = itemList //如果等于最后的实际日期,那么将切割时间戳记录 if v == latestDateYear { var cuttingDataTimestamp int64 for _, tmpData := range itemList { if tmpData.DataTime == latestDateStr { cuttingDataTimestamp = tmpData.DataTimestamp break } } quarterItem.CuttingDataTimestamp = cuttingDataTimestamp } else if v > latestDateYear { //如果大于最后的实际日期,那么第一个点就是切割的时间戳 if len(itemList) > 0 { quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100 } } quarterDataList = append(quarterDataList, quarterItem) } item.DataList = quarterDataList } } else if chartType == 7 { //柱方图 //item.DataList = dataList } else { item.DataList = dataList } edbList = append(edbList, item) } return } // BarChartData 柱方图的数据处理 func BarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, barChartInfoSort data_manage.BarChartInfoSortReq) (edbIdList []int, yDataList []data_manage.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 { if len(dataList) <= 0 { // 没有数据的指标id notDataEdbIdList = append(notDataEdbIdList, edbInfoId) continue } 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 currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) { 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 = 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([]data_manage.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] //指标的所有数据值 if len(dataList) <= 0 { // 没有数据的指标id findDataList = append(findDataList, 0) continue } 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 currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) { 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 = 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 { tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64() findDataList = append(findDataList, tmpValue) } else { findDataList = append(findDataList, 0) } } yDate := "0000-00-00" if !maxDate.IsZero() { yDate = maxDate.Format(utils.FormatDate) } yDataList = append(yDataList, data_manage.YData{ Date: yDate, Value: findDataList, Color: barChartInfoDate.Color, Name: barChartInfoDate.Name, }) } return } func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool { // 相关性图表不判断指标 if source == utils.CHART_SOURCE_CORRELATION && chartNameEn != "" { return true } if chartNameEn == "" { return false } // 截面散点图的话,肯定是有英文配置的 if chartType == utils.CHART_TYPE_SECTION_SCATTER { return true } for _, v := range edbList { if v.EdbNameEn == "" { return false } if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" { return false } } return true } func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool { if edbNameEn == "" { return false } if unit != "无" && unit != "" && unitEn == "" { return false } return true } // CheckIsDisableChart 根据指标Id判断是否是禁用图表 // return int 0:启用,1:禁用 func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) { crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb") if err != nil { return } //没有配置,立马返回 if crmConfig.ConfigValue == `` { return } confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",") for _, edbInfoId := range edbInfoIdArr { for _, confEdbInfoId := range confEdbInfoIdList { if strconv.Itoa(edbInfoId) == confEdbInfoId { isDisable = 1 return } } } return } // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) { switch chartType { case 10: //截面散点 var extraConfig data_manage.SectionScatterReq err = json.Unmarshal([]byte(extraConfigStr), &extraConfig) if err != nil { return } // 判断是否有配置日期序列 if len(extraConfig.SeriesList) <= 0 { errMsg = `请配置序列` err = errors.New(errMsg) return } // 判断是否有填写指标 if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 { errMsg = `请选择指标` err = errors.New(errMsg) return } // 遍历指标列表获取指标id edbIdMap := make(map[int]int) for _, v := range extraConfig.SeriesList[0].EdbInfoList { // X 轴的指标id if _, ok := edbIdMap[v.XEdbInfoId]; !ok { edbIdMap[v.XEdbInfoId] = v.XEdbInfoId edbIdList = append(edbIdList, v.XEdbInfoId) } // Y 轴的指标id if _, ok := edbIdMap[v.YEdbInfoId]; !ok { edbIdMap[v.YEdbInfoId] = v.YEdbInfoId edbIdList = append(edbIdList, v.YEdbInfoId) } } } return } // GetSectionScatterChartData 截面散点图的数据处理 func GetSectionScatterChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, 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) edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping) for _, v := range mappingList { edbIdList = append(edbIdList, v.EdbInfoId) edbMappingMap[v.EdbInfoId] = v } //SectionScatterSeriesInfoResp dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表 for _, seriesItem := range extraConfig.SeriesList { var maxDate time.Time // 系列中的指标数据 tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0) var minXVal, maxXVal, minYVal, maxYVal float64 for _, edbConf := range seriesItem.EdbInfoList { tmpItem := data_manage.SectionScatterEdbItemResp{ IsShow: edbConf.IsShow, Name: edbConf.Name, NameEn: edbConf.NameEn, } //单个坐标点的数据 //X轴的数据 { edbInfoId := edbConf.XEdbInfoId //X轴的指标 edbMappingInfo, ok := edbMappingMap[edbInfoId] if !ok { continue } findDate := edbConf.XDate //需要的日期值 dataList := edbDataListMap[edbInfoId] //指标的所有数据值 if len(dataList) <= 0 { // 没有数据的指标id //findDataList = append(findDataList, 0) continue } tmpItem.XEdbInfoId = edbInfoId tmpItem.XName = edbMappingInfo.EdbName tmpItem.XNameEn = edbMappingInfo.EdbNameEn switch edbConf.XDateType { 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, -edbConf.XDateValue) 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 currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) { 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, edbConf.XDate, time.Local) if tmpErr != nil { err = tmpErr return } for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = 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:", edbConf.XDate)) 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 { tmpItem.XDate = findDate tmpItem.XValue = tmpValue } else { continue } } //Y轴的数据 { edbInfoId := edbConf.YEdbInfoId //Y轴的指标 edbMappingInfo, ok := edbMappingMap[edbInfoId] if !ok { continue } findDate := edbConf.YDate //需要的日期值 dataList := edbDataListMap[edbInfoId] //指标的所有数据值 if len(dataList) <= 0 { // 没有数据的指标id //findDataList = append(findDataList, 0) continue } tmpItem.YEdbInfoId = edbInfoId tmpItem.YName = edbMappingInfo.EdbName tmpItem.YNameEn = edbMappingInfo.EdbNameEn switch edbConf.YDateType { 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, -edbConf.YDateValue) 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 currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) { 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, edbConf.YDate, time.Local) if tmpErr != nil { err = tmpErr return } for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = 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:", edbConf.YDate)) 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 { tmpItem.YDate = findDate tmpItem.YValue = tmpValue } else { continue } } // 获取当前系列的X轴的最大最小值 { if tmpItem.XValue < minXVal { minXVal = tmpItem.XValue } if tmpItem.XValue > maxXVal { maxXVal = tmpItem.XValue } if tmpItem.YValue < minYVal { minYVal = tmpItem.YValue } if tmpItem.YValue > maxYVal { maxYVal = tmpItem.YValue } } tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem) } trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点 var trendLine, rSquare string // 生成线性方程式 var a, b float64 { coordinateData := make([]utils.Coordinate, 0) for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList { tmpCoordinate1 := utils.Coordinate{ X: tmpSeriesEdbInfo.XValue, Y: tmpSeriesEdbInfo.YValue, } coordinateData = append(coordinateData, tmpCoordinate1) } // 只有存在两个坐标点的时候,才能去计算线性方程和R平方 if len(coordinateData) >= 2 { a, b = utils.GetLinearResult(coordinateData) if !math.IsNaN(a) && !math.IsNaN(b) { if b > 0 { trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4)) } else { trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4)) } minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64() maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64() } // 计算R平方 rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData)) } trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{ X: minXVal, Y: minYVal, }, data_manage.CoordinatePoint{ X: maxXVal, Y: maxYVal, }) } dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{ Name: seriesItem.Name, NameEn: seriesItem.NameEn, Color: seriesItem.Color, EdbInfoList: tmpSeriesEdbInfoList, ShowTrendLine: seriesItem.ShowTrendLine, ShowFitEquation: seriesItem.ShowFitEquation, ShowRSquare: seriesItem.ShowRSquare, TrendLine: trendLine, RSquare: rSquare, TrendLimitData: trendLimitData, }) } chartDataResp = data_manage.SectionScatterInfoResp{ XName: extraConfig.XName, XNameEn: extraConfig.XNameEn, XUnitName: extraConfig.XUnitName, XUnitNameEn: extraConfig.XUnitNameEn, YName: extraConfig.YName, YNameEn: extraConfig.YNameEn, YUnitName: extraConfig.YUnitName, YUnitNameEn: extraConfig.YUnitNameEn, XMinValue: extraConfig.XMinValue, XMaxValue: extraConfig.XMaxValue, YMinValue: extraConfig.YMinValue, YMaxValue: extraConfig.YMaxValue, DataList: dataListResp, } return } // ChartInfoRefreshV2 图表刷新 // @author Roc // @datetime 2022-09-16 10:15:38 // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务” func ChartInfoRefreshV2(chartInfoId int) (err error) { var errmsg string defer func() { if err != nil { go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3) //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers) fmt.Println("ChartInfoRefresh Err:" + errmsg) } }() edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId) if err != nil { errmsg = "获取需要刷新的指标失败:Err:" + err.Error() return } edbIdList := make([]int, 0) for _, v := range edbMappingList { edbIdList = append(edbIdList, v.EdbInfoId) } // 批量刷新 err, _ = EdbInfoRefreshAllFromBaseV3(edbIdList, false, true) if err != nil { return } return } // BatchChartInfoRefreshV2 图表批量刷新 // @author Roc // @datetime 2022-09-16 10:15:38 // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务” func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo) (err error) { if len(chartInfoList) <= 0 { return } chartInfoIdSlice := make([]int, 0) for _, chartInfo := range chartInfoList { chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId) } var errmsg string defer func() { if err != nil { go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3) //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers) fmt.Println("ChartInfoRefresh Err:" + errmsg) } }() edbIdList := make([]int, 0) edbInfoIdMap := make(map[int]int) edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice) for _, v := range edbMappingList { if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok { edbIdList = append(edbIdList, v.EdbInfoId) edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId } } // 批量刷新 err, _ = EdbInfoRefreshAllFromBaseV3(edbIdList, false, true) if err != nil { return } return }