add: return expression alias while get customized kpi list
This commit is contained in:
@@ -3,10 +3,12 @@ package kpi_c_title
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"be.ems/lib/dborm"
|
||||
"be.ems/lib/log"
|
||||
"be.ems/lib/services"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -16,6 +18,7 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
|
||||
var titles []KpiCTitle
|
||||
var conditions []string
|
||||
var params []any
|
||||
i18n := ctx.AcceptLanguage(c)
|
||||
|
||||
var querys KpiCTitleQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
@@ -64,6 +67,8 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
k.expressionAlias(titles, i18n)
|
||||
|
||||
c.JSON(http.StatusOK, services.TotalDataResp(titles, total))
|
||||
//c.JSON(http.StatusOK, titles)
|
||||
}
|
||||
@@ -72,6 +77,7 @@ func (k *KpiCTitle) Get(c *gin.Context) {
|
||||
var titles []KpiCTitle
|
||||
var conditions []string
|
||||
var params []any
|
||||
i18n := ctx.AcceptLanguage(c)
|
||||
|
||||
// construct condition to get
|
||||
if neType := c.Query("neType"); neType != "" {
|
||||
@@ -91,10 +97,37 @@ func (k *KpiCTitle) Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
k.expressionAlias(titles, i18n)
|
||||
|
||||
c.JSON(http.StatusOK, services.DataResp(titles))
|
||||
//c.JSON(http.StatusOK, titles)
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) expressionAlias(titles []KpiCTitle, i18n string) {
|
||||
var title *KpiCTitle
|
||||
for i := 0; i < len(titles); i++ {
|
||||
title = &titles[i]
|
||||
title.ExprAlias = *title.Expression
|
||||
re := regexp.MustCompile(`'([^']+)'`)
|
||||
matches := re.FindAllStringSubmatch(title.ExprAlias, -1)
|
||||
|
||||
for _, match := range matches {
|
||||
var alias, sql string
|
||||
if i18n == "zh" {
|
||||
sql = fmt.Sprintf("SELECT cn_title FROM kpi_title WHERE kpi_id='%s'", match[1])
|
||||
} else {
|
||||
sql = fmt.Sprintf("SELECT en_title FROM kpi_title WHERE kpi_id='%s'", match[1])
|
||||
}
|
||||
err := dborm.XCoreDB().QueryRow(sql).Scan(&alias)
|
||||
if err != nil {
|
||||
log.Warn("Failed to QueryRow:", err)
|
||||
continue
|
||||
}
|
||||
title.ExprAlias = regexp.MustCompile(match[1]).ReplaceAllString(title.ExprAlias, alias)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Total(c *gin.Context) {
|
||||
var conditions []string
|
||||
var params []any
|
||||
|
||||
@@ -12,6 +12,7 @@ type KpiCTitle struct {
|
||||
KpiID *string `gorm:"column:kpi_id;default:NULL," json:"kpiId,omitempty"`
|
||||
Title *string `gorm:"column:title;default:NULL," json:"title,omitempty"`
|
||||
Expression *string `gorm:"column:expression;default:NULL," json:"expression,omitempty"`
|
||||
ExprAlias string `gorm:"-" json:"exprAlias"`
|
||||
Status string `gorm:"column:status;default:'Active'" json:"status"`
|
||||
Unit *string `gorm:"column:unit" json:"unit,omitempty"`
|
||||
Description *string `gorm:"column:description;default:NULL," json:"description,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user