forked from free-lancers/electricity_bill_calc_service
enhance(enduser):基本完成终端用户表计在一个时间阶段内的统计功能。
This commit is contained in:
@@ -13,7 +13,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
@@ -353,3 +355,112 @@ func (es _EndUserService) BatchImportPVRegister(reportId string, file io.Reader)
|
||||
cache.AbolishRelation("end_user_detail")
|
||||
return errs
|
||||
}
|
||||
|
||||
func (es _EndUserService) StatEndUserRecordInPeriod(requestUser, requestPark, startDate, endDate string) ([]model.EndUserPeriodStat, error) {
|
||||
var (
|
||||
conditions = make([]string, 0)
|
||||
relations = []string{
|
||||
fmt.Sprintf("park:%s", requestPark),
|
||||
"end_user_detail",
|
||||
}
|
||||
cond = global.DB.NewSelect().
|
||||
Model((*model.EndUserDetail)(nil)).
|
||||
Relation("Report", func(sq *bun.SelectQuery) *bun.SelectQuery {
|
||||
return sq.ExcludeColumn("*")
|
||||
}).
|
||||
Relation("Park", func(sq *bun.SelectQuery) *bun.SelectQuery {
|
||||
return sq.ExcludeColumn("*")
|
||||
})
|
||||
)
|
||||
if len(requestUser) > 0 {
|
||||
cond = cond.Where("Park.user_id = ?", requestUser)
|
||||
conditions = append(conditions, requestUser)
|
||||
} else {
|
||||
conditions = append(conditions, "_")
|
||||
}
|
||||
if len(requestPark) > 0 {
|
||||
cond = cond.Where("eud.park_id = ?", requestPark)
|
||||
conditions = append(conditions, requestPark)
|
||||
} else {
|
||||
conditions = append(conditions, "_")
|
||||
}
|
||||
if len(startDate) > 0 {
|
||||
parseTime, err := time.Parse("2006-01", startDate)
|
||||
if err != nil {
|
||||
return make([]model.EndUserPeriodStat, 0), fmt.Errorf("不能解析给定的参数[startDate],%w", err)
|
||||
}
|
||||
start := model.NewDate(parseTime)
|
||||
cond = cond.Where("report.period >= ?::date", start.ToString())
|
||||
conditions = append(conditions, startDate)
|
||||
} else {
|
||||
conditions = append(conditions, "_")
|
||||
}
|
||||
if len(endDate) > 0 {
|
||||
parseTime, err := time.Parse("2006-01", endDate)
|
||||
if err != nil {
|
||||
return make([]model.EndUserPeriodStat, 0), fmt.Errorf("不能解析给定的参数[endDate],%w", err)
|
||||
}
|
||||
end := model.NewDate(parseTime)
|
||||
cond = cond.Where("report.period <= ?::date", end.ToString())
|
||||
conditions = append(conditions, endDate)
|
||||
}
|
||||
if cached, err := cache.RetreiveSearch[[]model.EndUserPeriodStat]("end_user_stat", conditions...); cached != nil && err == nil {
|
||||
return *cached, nil
|
||||
}
|
||||
ctx, cancel := global.TimeoutContext(120)
|
||||
defer cancel()
|
||||
var endUserSums []model.EndUserPeriodStat
|
||||
err := cond.Column("eud.meter_04kv_id").
|
||||
ColumnExpr("sum(?) as overall", bun.Ident("eud.overall")).
|
||||
ColumnExpr("sum(?) as overall_fee", bun.Ident("eud.overall_fee")).
|
||||
ColumnExpr("sum(?) as critical", bun.Ident("eud.critical")).
|
||||
ColumnExpr("sum(?) as critical_fee", bun.Ident("eud.critical_fee")).
|
||||
ColumnExpr("sum(?) as peak", bun.Ident("eud.peak")).
|
||||
ColumnExpr("sum(?) as peak_fee", bun.Ident("eud.peak_fee")).
|
||||
ColumnExpr("sum(?) as valley", bun.Ident("eud.valley")).
|
||||
ColumnExpr("sum(?) as valley_fee", bun.Ident("eud.valley_fee")).
|
||||
ColumnExpr("sum(?) as final_diluted", bun.Ident("eud.final_diluted")).
|
||||
Group("eud.meter_04kv_id").
|
||||
Scan(ctx, &endUserSums)
|
||||
if err != nil {
|
||||
return make([]model.EndUserPeriodStat, 0), fmt.Errorf("未能完成终端用户在指定期限内的统计,%w", err)
|
||||
}
|
||||
meterIds := lo.Reduce(
|
||||
endUserSums,
|
||||
func(acc mapset.Set[string], elem model.EndUserPeriodStat, _ int) mapset.Set[string] {
|
||||
acc.Add(elem.MeterId)
|
||||
return acc
|
||||
},
|
||||
mapset.NewSet[string](),
|
||||
)
|
||||
meterArchives := make([]model.Meter04KV, 0)
|
||||
if len(meterIds.ToSlice()) > 0 {
|
||||
err = global.DB.NewSelect().Model(&meterArchives).
|
||||
Where("code in (?)", bun.In(meterIds.ToSlice())).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return make([]model.EndUserPeriodStat, 0), fmt.Errorf("未能获取到终端表计的最新基础档案,%w", err)
|
||||
}
|
||||
}
|
||||
filledStats := lo.Map(
|
||||
endUserSums,
|
||||
func(elem model.EndUserPeriodStat, _ int) model.EndUserPeriodStat {
|
||||
archive, has := lo.Find(meterArchives, func(meter model.Meter04KV) bool {
|
||||
return meter.Code == elem.MeterId
|
||||
})
|
||||
if has {
|
||||
elem.Address = *archive.Address
|
||||
elem.CustomerName = *archive.CustomerName
|
||||
elem.IsPublicMeter = archive.IsPublicMeter
|
||||
}
|
||||
if !elem.Overall.Decimal.IsZero() {
|
||||
elem.AdjustProportion = decimal.NewNullDecimal(
|
||||
elem.AdjustFee.Decimal.Div(elem.OverallFee.Decimal).RoundBank(8),
|
||||
)
|
||||
}
|
||||
return elem
|
||||
},
|
||||
)
|
||||
cache.CacheSearch(filledStats, relations, "end_user_stat", conditions...)
|
||||
return filledStats, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user