diff --git a/features/pm/kpi_c_title/controller.go b/features/pm/kpi_c_title/controller.go index 32c4eb32..490c3b02 100644 --- a/features/pm/kpi_c_title/controller.go +++ b/features/pm/kpi_c_title/controller.go @@ -5,6 +5,7 @@ import ( "strings" "be.ems/src/framework/datasource" + "be.ems/src/framework/vo/result" "github.com/gin-gonic/gin" ) @@ -12,7 +13,7 @@ func (k *KpiCTitle) Get(c *gin.Context) { var titles []KpiCTitle var conditions []string var params []any - + var total int64 // construct condition to get if neType := c.Query("neType"); neType != "" { conditions = append(conditions, "ne_type = ?") @@ -23,15 +24,32 @@ func (k *KpiCTitle) Get(c *gin.Context) { params = append(params, status) } whereSql := "" + totalSql := "" + if len(conditions) > 0 { whereSql += strings.Join(conditions, " and ") + totalSql += strings.Join(conditions, " and ") } + + data := map[string]any{ + "total": 0, + "rows": []KpiCTitle{}, + } + if err := datasource.DefaultDB().Where(whereSql, params...).Find(&titles).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } + data["rows"] = titles + + if err := datasource.DefaultDB().Table(k.TableName()).Where(totalSql, params...).Count(&total).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + data["total"] = total + //c.JSON(http.StatusOK, map[string]any{"data": titles}) - c.JSON(http.StatusOK, titles) + c.JSON(http.StatusOK, result.Ok(data)) } func (k *KpiCTitle) Total(c *gin.Context) {