From 7f95192c7899bb58b058c4293220991062d1d623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 22 Aug 2022 15:57:04 +0800 Subject: [PATCH] =?UTF-8?q?enhance(report):=E6=96=B0=E4=B8=80=E6=9C=9F?= =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=EF=BC=8C=E5=8A=A0=E5=85=A5=E5=8F=AF=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=A1=A8=E8=AE=B0=E5=BD=95=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/report.go | 91 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 5 deletions(-) diff --git a/service/report.go b/service/report.go index f19a825..9a21d3a 100644 --- a/service/report.go +++ b/service/report.go @@ -10,6 +10,7 @@ import ( "github.com/fufuok/utils" "github.com/google/uuid" "github.com/samber/lo" + "github.com/shopspring/decimal" "xorm.io/builder" ) @@ -108,9 +109,61 @@ func (_ReportService) IsNewPeriodValid(uid string, period time.Time) (bool, erro } func (_ReportService) InitializeNewReport(parkId string, period time.Time) (err error) { + periods := make([]model.Report, 0) + err = global.DBConn. + Table("report"). + Where(builder.Eq{"park_id": parkId, "published": true}). + Asc("period"). + Find(&periods) + if err != nil { + return + } + // 获取上一期的报表索引信息 + maxPublishedReport := lo.Reduce( + periods, + func(acc *model.Report, elem model.Report, index int) *model.Report { + if acc == nil || (acc != nil && elem.Period.After(acc.Period)) { + return &elem + } + return acc + }, + nil, + ) + var indexedLastPeriodCustomers map[string]model.EndUserDetail + if maxPublishedReport != nil { + // 获取上一期的所有户表信息,并获取当前已启用的所有用户 + lastPeriodCustomers := make([]model.EndUserDetail, 0) + err = global.DBConn.Where(builder.Eq{"report_id": maxPublishedReport.Id}).Find(&lastPeriodCustomers) + if err != nil { + return + } + indexedLastPeriodCustomers = lo.Reduce( + lastPeriodCustomers, + func(acc map[string]model.EndUserDetail, elem model.EndUserDetail, index int) map[string]model.EndUserDetail { + acc[elem.MeterId] = elem + return acc + }, + make(map[string]model.EndUserDetail, 0), + ) + } else { + indexedLastPeriodCustomers = make(map[string]model.EndUserDetail, 0) + } + currentActivatedCustomers := make([]model.Meter04KV, 0) + err = global.DBConn.Where(builder.Eq{"park_id": parkId, "enabled": true}).Find(¤tActivatedCustomers) + if err != nil { + return + } + // 生成新一期的报表 + tx := global.DBConn.NewSession() + if err = tx.Begin(); err != nil { + return + } + defer tx.Close() + // 插入已经生成的报表索引信息和园区概况信息 newReport := model.Report{ Id: uuid.New().String(), ParkId: parkId, + Period: period, StepState: model.NewSteps(), Published: false, Withdraw: model.REPORT_NOT_WITHDRAW, @@ -118,16 +171,44 @@ func (_ReportService) InitializeNewReport(parkId string, period time.Time) (err newReportSummary := model.ReportSummary{ ReportId: newReport.Id, } - tx := global.DBConn.NewSession() - if err = tx.Begin(); err != nil { - return - } - defer tx.Close() _, err = tx.Insert(newReport, newReportSummary) if err != nil { tx.Rollback() return } + // 生成并插入户表信息 + for _, customer := range currentActivatedCustomers { + newEndUser := model.EndUserDetail{ + ReportId: newReport.Id, + ParkId: parkId, + MeterId: customer.Code, + Seq: customer.Seq, + Ratio: customer.Ratio, + Address: customer.Address, + CustomerName: customer.CustomerName, + ContactName: customer.ContactName, + ContactPhone: customer.ContactPhone, + IsPublicMeter: customer.IsPublicMeter, + WillDilute: customer.WillDilute, + LastPeriodOverall: decimal.Zero, + LastPeriodCritical: decimal.Zero, + LastPeriodPeak: decimal.Zero, + LastPeriodFlat: decimal.Zero, + LastPeriodValley: decimal.Zero, + } + if lastPeriod, ok := indexedLastPeriodCustomers[customer.Code]; ok { + newEndUser.LastPeriodOverall = lastPeriod.CurrentPeriodOverall + newEndUser.LastPeriodCritical = lastPeriod.CurrentPeriodCritical + newEndUser.LastPeriodPeak = lastPeriod.CurrentPeriodPeak + newEndUser.LastPeriodFlat = lastPeriod.CurrentPeriodFlat + newEndUser.LastPeriodValley = lastPeriod.CurrentPeriodValley + } + _, err = tx.Insert(newEndUser) + if err != nil { + tx.Rollback() + return + } + } err = tx.Commit() if err != nil { tx.Rollback()