diff --git a/features/pm/kpi_c_report/controller.go b/features/pm/kpi_c_report/controller.go index 38f122cf..9b9079c9 100644 --- a/features/pm/kpi_c_report/controller.go +++ b/features/pm/kpi_c_report/controller.go @@ -10,15 +10,6 @@ import ( "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 @@ -115,6 +106,15 @@ func (k *KpiCReport) GetTotalList(c *gin.Context) { whereSql += strings.Join(conditions, " and ") 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 if pageSize := querys.PageSize; pageSize > 0 { 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 := dborm.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) } diff --git a/features/pm/kpi_c_title/controller.go b/features/pm/kpi_c_title/controller.go index 99e03565..c9551bfb 100644 --- a/features/pm/kpi_c_title/controller.go +++ b/features/pm/kpi_c_title/controller.go @@ -1,6 +1,7 @@ package kpi_c_title import ( + "fmt" "net/http" "strings" @@ -23,29 +24,54 @@ func (k *KpiCTitle) GetToalList(c *gin.Context) { var conditions []string 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 - if neType := c.Query("neType"); neType != "" { + if neType := querys.NeType; neType != "" { conditions = append(conditions, "ne_type = ?") params = append(params, strings.ToUpper(neType)) } - if status := c.Query("status"); status != "" { + if status := querys.Status; status != "" { conditions = append(conditions, "status = ?") params = append(params, status) } whereSql := "" if len(conditions) > 0 { 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())) - return - } + + // Get total number 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())) 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) } @@ -122,7 +148,7 @@ 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{CodeKeyName: CodeError, MsgKeyName: "Title not found"}) + c.JSON(http.StatusNotFound, services.ErrResp("Title not found")) return } @@ -138,7 +164,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{CodeKeyName: CodeError, MsgKeyName: "Title not found"}) + c.JSON(http.StatusNotFound, services.ErrResp("Title not found")) return } diff --git a/features/pm/kpi_c_title/model.go b/features/pm/kpi_c_title/model.go index 068d77d6..f2e804ed 100644 --- a/features/pm/kpi_c_title/model.go +++ b/features/pm/kpi_c_title/model.go @@ -14,6 +14,16 @@ type KpiCTitle struct { 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 { return "kpi_c_title" }