add: custom kpi
This commit is contained in:
@@ -4,10 +4,42 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"be.ems/lib/services"
|
||||
"be.ems/src/framework/datasource"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (k *KpiCTitle) GetToalList(c *gin.Context) {
|
||||
var titles []KpiCTitle
|
||||
var conditions []string
|
||||
var params []any
|
||||
|
||||
// construct condition to get
|
||||
if neType := c.Query("neType"); neType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.ToUpper(neType))
|
||||
}
|
||||
if status := c.Query("status"); status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, status)
|
||||
}
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += strings.Join(conditions, " and ")
|
||||
}
|
||||
if err := datasource.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
var total int64 = 0
|
||||
if err := datasource.DefaultDB().Table(k.TableName()).Where(whereSql, params...).Count(&total).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, services.TotalDataResp(titles, total))
|
||||
//c.JSON(http.StatusOK, titles)
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Get(c *gin.Context) {
|
||||
var titles []KpiCTitle
|
||||
var conditions []string
|
||||
@@ -27,11 +59,12 @@ func (k *KpiCTitle) Get(c *gin.Context) {
|
||||
whereSql += strings.Join(conditions, " and ")
|
||||
}
|
||||
if err := datasource.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
//c.JSON(http.StatusOK, map[string]any{"data": titles})
|
||||
c.JSON(http.StatusOK, titles)
|
||||
|
||||
c.JSON(http.StatusOK, services.DataResp(titles))
|
||||
//c.JSON(http.StatusOK, titles)
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Total(c *gin.Context) {
|
||||
@@ -53,25 +86,26 @@ func (k *KpiCTitle) Total(c *gin.Context) {
|
||||
}
|
||||
var total int64 = 0
|
||||
if err := datasource.DefaultDB().Table(k.TableName()).Where(whereSql, params...).Count(&total).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, map[string]any{"total": total})
|
||||
c.JSON(http.StatusOK, services.TotalResp(total))
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Post(c *gin.Context) {
|
||||
var title KpiCTitle
|
||||
|
||||
if err := c.ShouldBindJSON(&title); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := datasource.DefaultDB().Create(&title).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, title)
|
||||
|
||||
c.JSON(http.StatusCreated, services.DataResp(title))
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) Put(c *gin.Context) {
|
||||
@@ -79,12 +113,12 @@ func (k *KpiCTitle) Put(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if err := datasource.DefaultDB().First(&title, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Title not found"})
|
||||
c.JSON(http.StatusNotFound, services.ErrResp("Title not found"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&title); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
datasource.DefaultDB().Save(&title)
|
||||
@@ -95,7 +129,7 @@ func (k *KpiCTitle) Delete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if err := datasource.DefaultDB().Delete(&KpiCTitle{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Title not found"})
|
||||
c.JSON(http.StatusNotFound, services.ErrResp("Title not found"))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -4,14 +4,14 @@ import "time"
|
||||
|
||||
type KpiCTitle struct {
|
||||
ID int `gorm:"column:id;primary_key;auto_increment" json:"id"`
|
||||
NeType *string `gorm:"column:ne_type;default:NULL," json:"ne_type,omitempty"`
|
||||
KpiID *string `gorm:"column:kpi_id;default:NULL," json:"kpi_id,omitempty"`
|
||||
NeType *string `gorm:"column:ne_type;default:NULL," json:"neType,omitempty"`
|
||||
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"`
|
||||
Status *string `gorm:"column:status" json:"status,omitempty"`
|
||||
Description *string `gorm:"column:description;default:NULL," json:"description,omitempty"`
|
||||
CreatedBy *string `gorm:"column:created_by;default:NULL," json:"created_by,omitempty"`
|
||||
UpdatedAt *time.Time `gorm:"column:updated_at;default:current_timestamp()," json:"updated_at,omitempty"`
|
||||
CreatedBy *string `gorm:"column:created_by;default:NULL," json:"createdBy,omitempty"`
|
||||
UpdatedAt *time.Time `gorm:"column:updated_at;default:current_timestamp()," json:"updatedAt,omitempty"`
|
||||
}
|
||||
|
||||
func (k *KpiCTitle) TableName() string {
|
||||
|
||||
@@ -19,6 +19,10 @@ func Register(r *gin.RouterGroup) {
|
||||
middleware.PreAuthorize(nil),
|
||||
k.Total,
|
||||
)
|
||||
pmKPIC.GET("/title/totalList",
|
||||
middleware.PreAuthorize(nil),
|
||||
k.GetToalList,
|
||||
)
|
||||
pmKPIC.POST("/title",
|
||||
middleware.PreAuthorize(nil),
|
||||
k.Post,
|
||||
|
||||
Reference in New Issue
Block a user