add: custom kpi
This commit is contained in:
@@ -5,10 +5,20 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"be.ems/lib/services"
|
||||
"be.ems/src/framework/datasource"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
CodeKeyName = "code"
|
||||
MsgKeyName = "message"
|
||||
DataKeyName = "data"
|
||||
CodeError = 0
|
||||
CodeSuccess = 1
|
||||
TotalKeyName = "total"
|
||||
)
|
||||
|
||||
func (k *KpiCReport) Get(c *gin.Context) {
|
||||
var reports []KpiCReport
|
||||
var conditions []string
|
||||
@@ -16,7 +26,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
|
||||
|
||||
var querys KpiCReportQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -25,7 +35,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.ToUpper(querys.NeType))
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Not found NE type"})
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
|
||||
return
|
||||
}
|
||||
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
|
||||
@@ -62,20 +72,21 @@ func (k *KpiCReport) Get(c *gin.Context) {
|
||||
//err := datasource.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
err := dborm.Find(&reports).Error
|
||||
if 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, reports)
|
||||
c.JSON(http.StatusOK, services.DataResp(reports))
|
||||
//c.JSON(http.StatusOK, reports)
|
||||
}
|
||||
|
||||
func (k *KpiCReport) Total(c *gin.Context) {
|
||||
func (k *KpiCReport) GetTotalList(c *gin.Context) {
|
||||
var reports []KpiCReport
|
||||
var conditions []string
|
||||
var params []any
|
||||
|
||||
var querys KpiCReportQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -84,7 +95,73 @@ func (k *KpiCReport) Total(c *gin.Context) {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.ToUpper(querys.NeType))
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Not found NE type"})
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
|
||||
return
|
||||
}
|
||||
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
|
||||
dborm := datasource.DefaultDB().Table(tableName)
|
||||
|
||||
if querys.StartTime != "" {
|
||||
conditions = append(conditions, "created_at >= ?")
|
||||
params = append(params, querys.StartTime)
|
||||
}
|
||||
if querys.EndTime != "" {
|
||||
conditions = append(conditions, "created_at <= ?")
|
||||
params = append(params, querys.EndTime)
|
||||
}
|
||||
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += strings.Join(conditions, " and ")
|
||||
dborm = dborm.Where(whereSql, params...)
|
||||
}
|
||||
// page number and size
|
||||
if pageSize := querys.PageSize; pageSize > 0 {
|
||||
dborm = dborm.Limit(pageSize)
|
||||
if pageNum := querys.PageNum; pageNum > 0 {
|
||||
dborm = dborm.Offset((pageNum - 1) * pageSize)
|
||||
}
|
||||
}
|
||||
|
||||
// order by
|
||||
if sortField, sortOrder := querys.SortField, querys.SortOrder; sortField != "" && sortOrder != "" {
|
||||
orderBy := fmt.Sprintf("%s %s", sortField, sortOrder)
|
||||
dborm = dborm.Order(orderBy)
|
||||
}
|
||||
|
||||
//err := datasource.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||
err := dborm.Find(&reports).Error
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
var total int64 = 0
|
||||
err = dborm.Count(&total).Error
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, services.TotalDataResp(reports, total))
|
||||
//c.JSON(http.StatusOK, reports)
|
||||
}
|
||||
|
||||
func (k *KpiCReport) Total(c *gin.Context) {
|
||||
var conditions []string
|
||||
var params []any
|
||||
|
||||
var querys KpiCReportQuery
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// construct condition to get
|
||||
if querys.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.ToUpper(querys.NeType))
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp("Not found NE type"))
|
||||
return
|
||||
}
|
||||
tableName := TableName() + "_" + strings.ToLower(querys.NeType)
|
||||
@@ -107,25 +184,25 @@ func (k *KpiCReport) Total(c *gin.Context) {
|
||||
var total int64 = 0
|
||||
err := dborm.Count(&total).Error
|
||||
if 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 *KpiCReport) Post(c *gin.Context) {
|
||||
var report KpiCReport
|
||||
|
||||
if err := c.ShouldBindJSON(&report); 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(&report).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, report)
|
||||
c.JSON(http.StatusCreated, services.DataResp(report))
|
||||
}
|
||||
|
||||
func (k *KpiCReport) Put(c *gin.Context) {
|
||||
@@ -133,23 +210,23 @@ func (k *KpiCReport) Put(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if err := datasource.DefaultDB().First(&report, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "KPI report not found"})
|
||||
c.JSON(http.StatusNotFound, services.ErrResp("KPI report not found"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&report); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
|
||||
return
|
||||
}
|
||||
datasource.DefaultDB().Save(&report)
|
||||
c.JSON(http.StatusOK, report)
|
||||
c.JSON(http.StatusOK, services.DataResp(report))
|
||||
}
|
||||
|
||||
func (k *KpiCReport) Delete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if err := datasource.DefaultDB().Delete(&KpiCReport{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "KPI report not found"})
|
||||
c.JSON(http.StatusNotFound, services.ErrResp("KPI report not found"))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user