refactor: 重构更新多个文件中的相关调用
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
package kpi_c_report
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"be.ems/lib/dborm"
|
||||
"be.ems/lib/services"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -31,7 +33,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
|
||||
dbg := dborm.DefaultDB().Table(tableName)
|
||||
dbg := db.DB("").Model(&KpiCReport{}).Table(tableName)
|
||||
|
||||
if querys.NeID != "" {
|
||||
conditions = append(conditions, "rm_uid = (select n.rm_uid from ne_info n where n.ne_type=? and n.ne_id=? and n.status=1)")
|
||||
@@ -69,7 +71,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
|
||||
dbg = dbg.Order(orderBy)
|
||||
}
|
||||
|
||||
//err := dborm.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
//err := db.DB("").Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
err := dbg.Find(&reports).Error
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||
@@ -98,7 +100,7 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
|
||||
dbg := dborm.DefaultDB().Table(tableName)
|
||||
dbg := db.DB("").Model(&KpiCReport{}).Table(tableName)
|
||||
|
||||
if querys.NeID != "" {
|
||||
conditions = append(conditions, "rm_uid = (select n.rm_uid from ne_info n where n.ne_type=? and n.ne_id=? and n.status=1)")
|
||||
@@ -108,11 +110,11 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if querys.StartTime != "" {
|
||||
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
|
||||
conditions = append(conditions, "created_at >= ?")
|
||||
params = append(params, querys.StartTime)
|
||||
}
|
||||
if querys.EndTime != "" {
|
||||
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
|
||||
conditions = append(conditions, "created_at <= ?")
|
||||
params = append(params, querys.EndTime)
|
||||
}
|
||||
conditions = append(conditions, "kpi_values != 'null'")
|
||||
@@ -136,7 +138,7 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
|
||||
dbg = dbg.Order(orderBy)
|
||||
}
|
||||
|
||||
//err := dborm.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
//err := db.DB("").Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
err := dbg.Find(&results).Error
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||
@@ -156,14 +158,24 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
|
||||
"createdAt": r.CreatedAt,
|
||||
"granularity": r.Granularity,
|
||||
}
|
||||
// 解析 JSON 字符串为 map
|
||||
var kpiValues []map[string]any
|
||||
err := json.Unmarshal([]byte(r.KpiValues), &kpiValues)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, k := range r.KpiValues {
|
||||
formatted := fmt.Sprintf("%.3f", k.Value)
|
||||
// 遍历 kpiValues 数组
|
||||
for _, k := range kpiValues {
|
||||
kpiId := fmt.Sprint(k["kpiId"])
|
||||
value := parse.Number(k["value"])
|
||||
|
||||
formatted := fmt.Sprintf("%.3d", value)
|
||||
formattedFloat, err := strconv.ParseFloat(formatted, 64)
|
||||
if err != nil {
|
||||
formattedFloat = 0
|
||||
}
|
||||
report[k.KPIID] = formattedFloat
|
||||
report[kpiId] = formattedFloat
|
||||
}
|
||||
reports = append(reports, report)
|
||||
}
|
||||
@@ -190,14 +202,14 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
|
||||
dbg := dborm.DefaultDB().Table(tableName)
|
||||
dbg := db.DB("").Model(&KpiCReport{}).Table(tableName)
|
||||
|
||||
if querys.StartTime != "" {
|
||||
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
|
||||
conditions = append(conditions, "created_at >= ?")
|
||||
params = append(params, querys.StartTime)
|
||||
}
|
||||
if querys.EndTime != "" {
|
||||
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) <= ?")
|
||||
conditions = append(conditions, "created_at <= ?")
|
||||
params = append(params, querys.EndTime)
|
||||
}
|
||||
conditions = append(conditions, "kpi_values != 'null'")
|
||||
@@ -230,7 +242,7 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
|
||||
dbg = dbg.Order(orderBy)
|
||||
}
|
||||
|
||||
//err := dborm.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
//err := db.DB("").Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
err = dbg.Find(&reports).Error
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||
@@ -259,8 +271,7 @@ func (k *KpiCReport) Total(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
|
||||
dbg := dborm.DefaultDB().Table(tableName)
|
||||
|
||||
dbg := db.DB("").Model(&KpiCReport{}).Table(tableName)
|
||||
if querys.StartTime != "" {
|
||||
conditions = append(conditions, "(UNIX_TIMESTAMP(created_at) * 1000) >= ?")
|
||||
params = append(params, querys.StartTime)
|
||||
@@ -293,7 +304,8 @@ func (k *KpiCReport) Post(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := dborm.DefaultDB().Create(&report).Error; err != nil {
|
||||
dbg := db.DB("").Model(&KpiCReport{})
|
||||
if err := dbg.Create(&report).Error; err != nil {
|
||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
@@ -303,8 +315,8 @@ func (k *KpiCReport) Post(c *gin.Context) {
|
||||
func (k *KpiCReport) Put(c *gin.Context) {
|
||||
var report KpiCReport
|
||||
id := c.Param("id")
|
||||
|
||||
if err := dborm.DefaultDB().First(&report, id).Error; err != nil {
|
||||
dbg := db.DB("").Model(&KpiCReport{})
|
||||
if err := dbg.First(&report, id).Error; err != nil {
|
||||
c.JSON(http.StatusOK, services.ErrResp("custom indicator report not found"))
|
||||
return
|
||||
}
|
||||
@@ -313,14 +325,14 @@ func (k *KpiCReport) Put(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
dborm.DefaultDB().Save(&report)
|
||||
db.DB("").Model(&KpiCReport{}).Save(&report)
|
||||
c.JSON(http.StatusOK, services.DataResp(report))
|
||||
}
|
||||
|
||||
func (k *KpiCReport) Delete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if err := dborm.DefaultDB().Delete(&KpiCReport{}, id).Error; err != nil {
|
||||
if err := db.DB("").Delete(&KpiCReport{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusOK, services.ErrResp("custom indicator report not found"))
|
||||
return
|
||||
}
|
||||
@@ -330,7 +342,8 @@ func (k *KpiCReport) Delete(c *gin.Context) {
|
||||
|
||||
func InsertKpiCReport(neType string, report KpiCReport) {
|
||||
tableName := TableName() + "_" + strings.ToLower(neType)
|
||||
if err := dborm.DefaultDB().Table(tableName).Create(&report).Error; err != nil {
|
||||
dbg := db.DB("").Model(&KpiCReport{})
|
||||
if err := dbg.Table(tableName).Create(&report).Error; err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,17 @@
|
||||
package kpi_c_report
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type KpiCVal struct {
|
||||
KPIID string `json:"kpi_id" gorm:"column:kpi_id"`
|
||||
Value float64 `json:"value" gorm:"column:value"`
|
||||
Err string `json:"err" gorm:"column:err"`
|
||||
}
|
||||
|
||||
type KpiCValues []KpiCVal
|
||||
|
||||
type KpiCReport struct {
|
||||
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
|
||||
NeType *string `gorm:"column:ne_type;default:NULL" json:"neType,omitempty"`
|
||||
NeName *string `gorm:"column:ne_name;default:" json:"neName,omitempty"`
|
||||
RmUID *string `gorm:"column:rm_uid;default:NULL" json:"rmUid,omitempty"`
|
||||
Date string `gorm:"column:date" json:"date"` // time.Time `gorm:"column:date" json:"date"`
|
||||
StartTime *string `gorm:"column:start_time;default:NULL" json:"startTime,omitempty"`
|
||||
EndTime *string `gorm:"column:end_time;default:NULL" json:"endTime,omitempty"`
|
||||
Index int16 `gorm:"column:index" json:"index"`
|
||||
Granularity *int8 `gorm:"column:granularity;default:60" json:"granularity,omitempty"` //Time granualarity: 5/10/.../60/300 (second)
|
||||
KpiValues KpiCValues `gorm:"column:kpi_values;type:json" json:"kpiValues,omitempty"`
|
||||
CreatedAt *time.Time `gorm:"column:created_at;default:current_timestamp()" json:"createdAt,omitempty"`
|
||||
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
|
||||
NeType *string `gorm:"column:ne_type;default:NULL" json:"neType,omitempty"`
|
||||
NeName *string `gorm:"column:ne_name;default:" json:"neName,omitempty"`
|
||||
RmUID *string `gorm:"column:rm_uid;" json:"rmUid,omitempty"`
|
||||
Date string `gorm:"column:date" json:"date"` // time.Time `gorm:"column:date" json:"date"`
|
||||
StartTime *string `gorm:"column:start_time" json:"startTime,omitempty"`
|
||||
EndTime *string `gorm:"column:end_time" json:"endTime,omitempty"`
|
||||
Index int64 `gorm:"column:index" json:"index"`
|
||||
Granularity *int64 `gorm:"column:granularity" json:"granularity,omitempty"` //Time granualarity: 5/10/.../60/300 (second)
|
||||
KpiValues string `gorm:"column:kpi_values" json:"kpiValues,omitempty"`
|
||||
CreatedAt *int64 `gorm:"column:created_at" json:"createdAt,omitempty"`
|
||||
}
|
||||
|
||||
type KpiCReportQuery struct {
|
||||
@@ -55,17 +40,3 @@ type KpiCReport2FE struct {
|
||||
func TableName() string {
|
||||
return "kpi_c_report"
|
||||
}
|
||||
|
||||
// 将 KpiCValues 转换为 JSON 字节
|
||||
func (k KpiCValues) Value() (driver.Value, error) {
|
||||
return json.Marshal(k)
|
||||
}
|
||||
|
||||
// 从字节中扫描 KpiCValues
|
||||
func (k *KpiCValues) Scan(value interface{}) error {
|
||||
b, ok := value.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to scan value: %v", value)
|
||||
}
|
||||
return json.Unmarshal(b, k)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user