refactor(stat):统计服务基本完成迁移。

This commit is contained in:
徐涛 2022-09-17 18:45:02 +08:00
parent df08c31278
commit 805911f72b

View File

@ -3,64 +3,77 @@ package service
import (
"electricity_bill_calc/cache"
"electricity_bill_calc/global"
"electricity_bill_calc/logger"
"electricity_bill_calc/model"
"github.com/samber/lo"
"xorm.io/builder"
"github.com/uptrace/bun"
"go.uber.org/zap"
)
type _StatisticsService struct{}
type _StatisticsService struct {
l *zap.Logger
}
var StatisticsService _StatisticsService
var StatisticsService = _StatisticsService{
l: logger.Named("Service", "Stat"),
}
func (_StatisticsService) EnabledEnterprises() (int64, error) {
if cachedCount, err := cache.RetreiveCount("enabled_ent"); cachedCount != -1 && err == nil {
return cachedCount, nil
}
c, err := global.DBConn.
Table(new(model.User)).
Where(builder.Eq{"type": 0, "enabled": true}).
Count()
ctx, cancel := global.TimeoutContext()
defer cancel()
c, err := global.DB.NewSelect().Model((*model.User)(nil)).
Where("type = ?", model.USER_TYPE_ENT).
Where("enabled = ?", true).
Count(ctx)
if err == nil {
cache.CacheCount([]string{"user"}, "enabled_ent", c)
cache.CacheCount([]string{"user"}, "enabled_ent", int64(c))
}
return c, err
return int64(c), err
}
func (_StatisticsService) EnabledParks(userIds ...string) (int64, error) {
if cachedParks, err := cache.RetreiveCount("enabled_parks", userIds...); cachedParks != -1 && err == nil {
return cachedParks, nil
}
cond := builder.NewCond().And(builder.Eq{"enabled": true})
ctx, cancel := global.TimeoutContext()
defer cancel()
query := global.DB.NewSelect().Model((*model.Park)(nil)).
Where("enabled = ?", true)
if len(userIds) > 0 {
cond = cond.And(builder.Eq{"user_id": userIds})
query = query.Where("user_id in (?)", bun.In(userIds))
}
c, err := global.DBConn.
Table(new(model.Park)).
Where(cond).
Count()
c, err := query.Count(ctx)
if err == nil {
cache.CacheCount([]string{"user", "park"}, "enabled_parks", c, userIds...)
cache.CacheCount([]string{"user", "park"}, "enabled_parks", int64(c), userIds...)
}
return c, err
return int64(c), err
}
func (_StatisticsService) ParksNewestState(userIds ...string) ([]model.ParkPeriodStatistics, error) {
if cachedState, _ := cache.RetreiveSearch[[]model.ParkPeriodStatistics]("park_period_stat", userIds...); cachedState != nil {
return *cachedState, nil
}
cond := builder.NewCond().And(builder.Eq{"p.enabled": true, "r.published": true})
ctx, cancel := global.TimeoutContext()
defer cancel()
query := global.DB.NewSelect().Model((*model.Report)(nil)).
Relation("Park", func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Column("id", "name")
}).
Where("p.enabled = ?", true).
Where("r.publidhed = ?", true)
if len(userIds) > 0 {
cond = cond.And(builder.Eq{"p.user_id": userIds})
query = query.Where("p.user_id in (?)", bun.In(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)
err := query.Column("period").Scan(ctx, &parks)
if err != nil {
return make([]model.ParkPeriodStatistics, 0), err
}