From b9ee9d9fbfcee64eec285b4b6f12e3a15c3888cf Mon Sep 17 00:00:00 2001 From: zchengo <1933757688@qq.com> Date: Thu, 8 Dec 2022 19:10:19 +0800 Subject: [PATCH] feat: select the range of days for statistics --- server/api/dashboard.go | 9 ++++-- server/models/dashboard.go | 12 ++++---- server/service/dashboard.go | 8 +++--- web/src/views/Dashboard.vue | 56 +++++++++++++++++++++++++++++-------- 4 files changed, 61 insertions(+), 24 deletions(-) diff --git a/server/api/dashboard.go b/server/api/dashboard.go index 463f99b..bf4094c 100644 --- a/server/api/dashboard.go +++ b/server/api/dashboard.go @@ -22,6 +22,11 @@ func NewDashboardApi() *DashboardApi { // 获取数据汇总 func (d *DashboardApi) Summary(context *gin.Context) { uid, _ := strconv.Atoi(context.Request.Header.Get("uid")) - sum := d.dashboardService.Summary(int64(uid)) + days, _ := strconv.Atoi(context.Query("daysRange")) + if days < 7 || days > 30 { + response.Result(response.ErrCodeParamInvalid, nil, context) + return + } + sum := d.dashboardService.Summary(int64(uid), days) response.Result(response.ErrCodeSuccess, sum, context) -} +} \ No newline at end of file diff --git a/server/models/dashboard.go b/server/models/dashboard.go index 884f49c..42cf68f 100644 --- a/server/models/dashboard.go +++ b/server/models/dashboard.go @@ -1,10 +1,10 @@ package models type DashboardSum struct { - Customers int `json:"customers"` - Contracts int `json:"contracts"` - ContractAmount float64 `json:"contractAmount"` - Products int `json:"products"` - Date [7]string `json:"date"` - Amount [7]float64 `json:"amount"` + Customers int `json:"customers"` + Contracts int `json:"contracts"` + ContractAmount float64 `json:"contractAmount"` + Products int `json:"products"` + Date []string `json:"date"` + Amount []float64 `json:"amount"` } diff --git a/server/service/dashboard.go b/server/service/dashboard.go index c18c5a6..386f0f8 100644 --- a/server/service/dashboard.go +++ b/server/service/dashboard.go @@ -3,7 +3,6 @@ package service import ( "crm/global" "crm/models" - "fmt" "time" ) @@ -11,7 +10,7 @@ type DashboardService struct { } // 数据汇总 -func (d *DashboardService) Summary(uid int64) models.DashboardSum { +func (d *DashboardService) Summary(uid int64, days int) models.DashboardSum { var ds models.DashboardSum global.Db.Raw("select count(*) from customer where creator = ?", uid).Scan(&ds.Customers) global.Db.Raw("select count(*) from contract where creator = ?", uid).Scan(&ds.Contracts) @@ -20,10 +19,11 @@ func (d *DashboardService) Summary(uid int64) models.DashboardSum { now := time.Now().Unix() con := "created > ? and created < ? and status = 1 and creator = ?" - for i, d := 0, 7; i < 7; i++ { + ds.Amount = make([]float64, days) + ds.Date = make([]string, days) + for i, d := 0, days; i < days; i++ { day := now - (int64(d) * 24 * 60 * 60) start, end := dayRange(day) - fmt.Println(start, end) global.Db.Table(CONTRACT).Where(con, start, end, uid).Pluck("amount", &ds.Amount[i]) ds.Date[i] = time.Unix(day, 0).Format("01-02") d-- diff --git a/web/src/views/Dashboard.vue b/web/src/views/Dashboard.vue index 9e0e37f..6ff2c42 100644 --- a/web/src/views/Dashboard.vue +++ b/web/src/views/Dashboard.vue @@ -65,7 +65,23 @@ -
+
+
+ 合同金额完成情况 + + + + +
+ + 最近7天 + 最近14天 + 最近30天 + +
+
@@ -75,7 +91,7 @@