enhance(report):新一期报表初始化的时候,加入可用户表记录的初始化。

This commit is contained in:
徐涛 2022-08-22 15:57:04 +08:00
parent 5058f17eda
commit 7f95192c78

View File

@ -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(&currentActivatedCustomers)
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()