feat: 添加KpiReport模型和SelectKPI方法,优化数据查询逻辑

This commit is contained in:
TsMask
2025-06-26 17:22:19 +08:00
parent 35fccddebf
commit 40a95184dd
4 changed files with 167 additions and 1 deletions

View File

@@ -91,6 +91,44 @@ func (r *PerfKPI) SelectGoldKPI(query model.GoldKPIQuery, kpiIds []string) []map
return results
}
// SelectGoldKPI 通过网元指标数据信息
func (r PerfKPI) SelectKPI(query model.GoldKPIQuery) []model.KpiReport {
rows := []model.KpiReport{}
if query.NeType == "" {
return rows
}
tx := datasource.DB("").Model(&model.KpiReport{})
// 表名
tableName := fmt.Sprintf("kpi_report_%s", strings.ToLower(query.NeType))
tx = tx.Table(tableName)
// 构建查询条件
if query.RmUID != "" {
rmUIDs := strings.Split(query.RmUID, ",")
tx = tx.Where("rm_uid in ?", rmUIDs)
}
if query.StartTime != "" {
tx = tx.Where("created_at >= ?", query.StartTime)
}
if query.EndTime != "" {
tx = tx.Where("created_at <= ?", query.EndTime)
}
// 排序
if query.SortField == "" || query.SortField == "timeGroup" {
query.SortField = "created_at"
}
if query.SortOrder == "" {
query.SortOrder = "desc"
}
tx = tx.Order(fmt.Sprintf("%s %s", query.SortField, query.SortOrder))
// 查询数据
if err := tx.Find(&rows).Error; err != nil {
logger.Errorf("query find err => %v", err.Error())
return rows
}
return rows
}
// SelectGoldKPITitle 网元对应的指标名称
func (r *PerfKPI) SelectGoldKPITitle(neType string) []model.GoldKPITitle {
result := []model.GoldKPITitle{}