add: custom kpi feature
This commit is contained in:
@@ -10,15 +10,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"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) {
|
func (k *KpiCReport) Get(c *gin.Context) {
|
||||||
var reports []KpiCReport
|
var reports []KpiCReport
|
||||||
var conditions []string
|
var conditions []string
|
||||||
@@ -115,6 +106,15 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
|
|||||||
whereSql += strings.Join(conditions, " and ")
|
whereSql += strings.Join(conditions, " and ")
|
||||||
dborm = dborm.Where(whereSql, params...)
|
dborm = dborm.Where(whereSql, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get total number
|
||||||
|
var total int64 = 0
|
||||||
|
err := dborm.Count(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// page number and size
|
// page number and size
|
||||||
if pageSize := querys.PageSize; pageSize > 0 {
|
if pageSize := querys.PageSize; pageSize > 0 {
|
||||||
dborm = dborm.Limit(pageSize)
|
dborm = dborm.Limit(pageSize)
|
||||||
@@ -130,18 +130,12 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//err := datasource.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
//err := datasource.DefaultDB().Table(tableName).Where(whereSql, params...).Find(&reports).Error
|
||||||
err := dborm.Find(&reports).Error
|
err = dborm.Find(&reports).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||||
return
|
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, services.TotalDataResp(reports, total))
|
||||||
//c.JSON(http.StatusOK, reports)
|
//c.JSON(http.StatusOK, reports)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package kpi_c_title
|
package kpi_c_title
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -23,29 +24,54 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) {
|
|||||||
var conditions []string
|
var conditions []string
|
||||||
var params []any
|
var params []any
|
||||||
|
|
||||||
|
var querys KpiCTitleQuery
|
||||||
|
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, services.ErrResp(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dborm := datasource.DefaultDB().Table(k.TableName())
|
||||||
// construct condition to get
|
// construct condition to get
|
||||||
if neType := c.Query("neType"); neType != "" {
|
if neType := querys.NeType; neType != "" {
|
||||||
conditions = append(conditions, "ne_type = ?")
|
conditions = append(conditions, "ne_type = ?")
|
||||||
params = append(params, strings.ToUpper(neType))
|
params = append(params, strings.ToUpper(neType))
|
||||||
}
|
}
|
||||||
if status := c.Query("status"); status != "" {
|
if status := querys.Status; status != "" {
|
||||||
conditions = append(conditions, "status = ?")
|
conditions = append(conditions, "status = ?")
|
||||||
params = append(params, status)
|
params = append(params, status)
|
||||||
}
|
}
|
||||||
whereSql := ""
|
whereSql := ""
|
||||||
if len(conditions) > 0 {
|
if len(conditions) > 0 {
|
||||||
whereSql += strings.Join(conditions, " and ")
|
whereSql += strings.Join(conditions, " and ")
|
||||||
|
dborm = dborm.Where(whereSql, params...)
|
||||||
}
|
}
|
||||||
if err := datasource.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil {
|
|
||||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
// Get total number
|
||||||
return
|
|
||||||
}
|
|
||||||
var total int64 = 0
|
var total int64 = 0
|
||||||
if err := datasource.DefaultDB().Table(k.TableName()).Where(whereSql, params...).Count(&total).Error; err != nil {
|
if err := dborm.Count(&total).Error; err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, services.TotalDataResp(titles, total)
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
if err := dborm.Find(&titles).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)
|
//c.JSON(http.StatusOK, titles)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +148,7 @@ func (k *KpiCTitle) Put(c *gin.Context) {
|
|||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
if err := datasource.DefaultDB().First(&title, id).Error; err != nil {
|
if err := datasource.DefaultDB().First(&title, id).Error; err != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{CodeKeyName: CodeError, MsgKeyName: "Title not found"})
|
c.JSON(http.StatusNotFound, services.ErrResp("Title not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +164,7 @@ func (k *KpiCTitle) Delete(c *gin.Context) {
|
|||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
if err := datasource.DefaultDB().Delete(&KpiCTitle{}, id).Error; err != nil {
|
if err := datasource.DefaultDB().Delete(&KpiCTitle{}, id).Error; err != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{CodeKeyName: CodeError, MsgKeyName: "Title not found"})
|
c.JSON(http.StatusNotFound, services.ErrResp("Title not found"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,16 @@ type KpiCTitle struct {
|
|||||||
UpdatedAt *time.Time `gorm:"column:updated_at;default:current_timestamp()," json:"updatedAt,omitempty"`
|
UpdatedAt *time.Time `gorm:"column:updated_at;default:current_timestamp()," json:"updatedAt,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type KpiCTitleQuery struct {
|
||||||
|
ID int `json:"id" form:"id"`
|
||||||
|
NeType string `json:"neType" form:"neType"`
|
||||||
|
Status string `json:"status" form:"status"`
|
||||||
|
SortField string `json:"sortField" form:"sortField" binding:"omitempty,oneof=created_at"` // 排序字段,填写结果字段
|
||||||
|
SortOrder string `json:"sortOrder" form:"sortOrder" binding:"omitempty,oneof=asc desc"` // 排序升降序,asc desc
|
||||||
|
PageNum int `json:"pageNum" form:"pageNum"`
|
||||||
|
PageSize int `json:"pageSize" form:"pageSize"`
|
||||||
|
}
|
||||||
|
|
||||||
func (k *KpiCTitle) TableName() string {
|
func (k *KpiCTitle) TableName() string {
|
||||||
return "kpi_c_title"
|
return "kpi_c_title"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user