feat(stat):增加首页使用的提示和统计接口。

This commit is contained in:
徐涛
2022-08-23 14:50:26 +08:00
parent 5b2e107325
commit e135d976cd
6 changed files with 177 additions and 0 deletions

62
service/statistics.go Normal file
View File

@@ -0,0 +1,62 @@
package service
import (
"electricity_bill_calc/global"
"electricity_bill_calc/model"
"github.com/samber/lo"
"xorm.io/builder"
)
type _StatisticsService struct{}
var StatisticsService _StatisticsService
func (_StatisticsService) EnabledEnterprises() (int64, error) {
return global.DBConn.
Table(new(model.User)).
Where(builder.Eq{"type": 0, "enabled": true}).
Count()
}
func (_StatisticsService) EnabledParks(userIds ...string) (int64, error) {
cond := builder.NewCond().And(builder.Eq{"enabled": true})
if len(userIds) > 0 {
cond = cond.And(builder.Eq{"user_id": userIds})
}
return global.DBConn.
Table(new(model.Park)).
Where(cond).
Count()
}
func (_StatisticsService) ParksNewestState(userIds ...string) ([]model.ParkPeriodStatistics, error) {
cond := builder.NewCond().And(builder.Eq{"p.enabled": true, "r.published": true})
if len(userIds) > 0 {
cond = cond.And(builder.Eq{"p.user_id": userIds})
}
parks := make([]model.ParkPeriodStatistics, 0)
groupedParks := make(map[string]model.ParkPeriodStatistics, 0)
err := global.DBConn.
Table("park").Alias("p").
Join("LEFT", []string{"report", "r"}, "r.park_id=p.id").
Where(cond).
Cols("p.id", "p.name", "r.period").
Find(&parks)
if err != nil {
return make([]model.ParkPeriodStatistics, 0), err
}
for _, p := range parks {
if c, ok := groupedParks[p.Id]; ok {
if c.Period != nil && p.Period != nil && p.Period.After(*c.Period) {
groupedParks[p.Id] = p
}
if c.Period == nil && p.Period != nil {
groupedParks[p.Id] = p
}
} else {
groupedParks[p.Id] = p
}
}
return lo.Values(groupedParks), nil
}

View File

@@ -92,6 +92,7 @@ func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]m
Find(&reports)
return reports, total, err
}
func (_WithdrawService) AuditWithdraw(reportId string, granted bool) error {
var report = new(model.Report)
has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(report)
@@ -109,3 +110,12 @@ func (_WithdrawService) AuditWithdraw(reportId string, granted bool) error {
_, err = global.DBConn.ID(report.Id).Cols("withdraw", "last_withdraw_audit_at", "published").Update(report)
return err
}
func (_WithdrawService) AuditWaits() (int64, error) {
cond := builder.NewCond()
cond = cond.And(builder.Eq{"withdraw": model.REPORT_WITHDRAW_APPLIED})
return global.DBConn.
Table(new(model.JoinedReportForWithdraw)).
Where(cond).
Count()
}