Files
LingXi-CRM/server/service/dashboard.go
T

42 lines
958 B
Go
Raw Normal View History

2022-11-28 16:38:30 +08:00
package service
import (
2023-01-25 16:16:36 +08:00
"crm/dao"
2022-11-28 16:38:30 +08:00
"crm/models"
"time"
)
type DashboardService struct {
2023-01-25 16:16:36 +08:00
dashboardDao *dao.DashboardDao
}
func NewDashboardService() *DashboardService {
return &DashboardService{
dashboardDao: dao.NewDashboardDao(),
}
2022-11-28 16:38:30 +08:00
}
// 数据汇总
func (d *DashboardService) Summary(uid int64, days int) models.DashboardSum {
2023-01-25 16:16:36 +08:00
ds := d.dashboardDao.Count(uid, days)
2022-11-28 16:38:30 +08:00
now := time.Now().Unix()
ds.Amount = make([]float64, days)
ds.Date = make([]string, days)
2023-01-25 16:16:36 +08:00
for i, dd := 0, days; i < days; i++ {
day := now - (int64(dd) * 24 * 60 * 60)
2022-11-28 16:38:30 +08:00
start, end := dayRange(day)
2023-01-25 16:16:36 +08:00
ds.Amount[i] = d.dashboardDao.AmountSum(start, end, uid)
2022-11-28 16:38:30 +08:00
ds.Date[i] = time.Unix(day, 0).Format("01-02")
2023-01-25 16:16:36 +08:00
dd--
2022-11-28 16:38:30 +08:00
}
return ds
}
// 获取某一天的时间范围
func dayRange(day int64) (int64, int64) {
t := time.Unix(day, 0)
start := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
end := start.AddDate(0, 0, 1)
return start.Unix(), end.Unix()
}