From 805911f72b626d43b1505f574563b5f3b239ce51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 17 Sep 2022 18:45:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(stat):=E7=BB=9F=E8=AE=A1=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/statistics.go | 63 ++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/service/statistics.go b/service/statistics.go index 0b9a528..e779de0 100644 --- a/service/statistics.go +++ b/service/statistics.go @@ -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 }