ref: v3变更,,同步v2.2508.4
This commit is contained in:
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -28,8 +29,11 @@ func (r KpiReport) SelectKPI(query model.KPIQuery) []model.KpiReport {
|
||||
tableName := fmt.Sprintf("kpi_report_%s", strings.ToLower(query.NeType))
|
||||
tx = tx.Table(tableName)
|
||||
// 构建查询条件
|
||||
if query.RmUID != "" {
|
||||
tx = tx.Where("rm_uid = ?", query.RmUID)
|
||||
if query.NeUID != "" {
|
||||
tx = tx.Where("ne_uid = ?", query.NeUID)
|
||||
}
|
||||
if query.CoreUID != "" {
|
||||
tx = tx.Where("core_uid = ?", query.CoreUID)
|
||||
}
|
||||
if query.BeginTime != 0 {
|
||||
tx = tx.Where("created_at >= ?", query.BeginTime)
|
||||
@@ -37,25 +41,58 @@ func (r KpiReport) SelectKPI(query model.KPIQuery) []model.KpiReport {
|
||||
if query.EndTime != 0 {
|
||||
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
|
||||
}
|
||||
// 排序
|
||||
if query.SortField != "" {
|
||||
sortField := query.SortField
|
||||
sortOrder := query.SortOrder
|
||||
sort.SliceStable(rows, func(i, j int) bool {
|
||||
// 支持的排序字段映射
|
||||
fieldGetters := map[string]func(*model.KpiReport) any{
|
||||
"id": func(row *model.KpiReport) any { return row.ID },
|
||||
"timeGroup": func(row *model.KpiReport) any { return row.CreatedAt },
|
||||
"createdAt": func(row *model.KpiReport) any { return row.CreatedAt },
|
||||
// 可添加更多支持的字段
|
||||
}
|
||||
|
||||
// 获取字段 getter 函数
|
||||
getter, ok := fieldGetters[sortField]
|
||||
if !ok {
|
||||
// 非法字段,使用默认排序(id升序)
|
||||
return rows[i].ID < rows[j].ID
|
||||
}
|
||||
|
||||
// 获取比较值
|
||||
valI, valJ := getter(&rows[i]), getter(&rows[j])
|
||||
|
||||
// 根据字段类型进行比较
|
||||
switch v := valI.(type) {
|
||||
case int64:
|
||||
if sortOrder == "desc" {
|
||||
return v > valJ.(int64)
|
||||
}
|
||||
return v < valJ.(int64)
|
||||
case string:
|
||||
if sortOrder == "desc" {
|
||||
return v > valJ.(string)
|
||||
}
|
||||
return v < valJ.(string)
|
||||
default:
|
||||
// 不支持的字段类型,使用默认排序
|
||||
return rows[i].ID < rows[j].ID
|
||||
}
|
||||
})
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Insert 新增信息 返回新增数据ID
|
||||
func (r KpiReport) Insert(param model.KpiReport) int64 {
|
||||
if param.NeType == "" {
|
||||
if param.CoreUID == "" || param.NeUID == "" || param.NeType == "" {
|
||||
return 0
|
||||
}
|
||||
if param.CreatedAt == 0 {
|
||||
@@ -72,11 +109,11 @@ func (r KpiReport) Insert(param model.KpiReport) int64 {
|
||||
}
|
||||
|
||||
// SelectUPF 查询UPF数据 N3上行,N6下行
|
||||
func (r KpiReport) SelectUPF(rmUID string, beginTime, endTime int64) []model.KpiReport {
|
||||
func (r KpiReport) SelectUPF(coreUid, neUid string, beginTime, endTime int64) []model.KpiReport {
|
||||
tx := db.DB("").Model(&model.KpiReport{})
|
||||
// 表名
|
||||
tx = tx.Table("kpi_report_upf")
|
||||
tx = tx.Where("rm_uid = ?", rmUID)
|
||||
tx = tx.Where("core_uid = ? and ne_uid = ?", coreUid, neUid)
|
||||
tx = tx.Where("created_at >= ?", beginTime)
|
||||
tx = tx.Where("created_at <= ?", endTime)
|
||||
// 查询数据
|
||||
|
||||
Reference in New Issue
Block a user