From c9f82353394ae514383a3d2803e01ade6a742882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 2 Sep 2022 19:43:26 +0800 Subject: [PATCH] =?UTF-8?q?enhance(enduser):=E7=BB=88=E7=AB=AF=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=8A=84=E8=A1=A8=E6=97=B6=E5=8F=AF=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E7=9A=84=E6=95=B0=E9=87=8F=E5=86=B3=E5=AE=9A=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=AF=BB=E5=8F=96=E4=B8=8A=E4=BC=A0=E8=A1=A8=E6=A0=BC?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E4=B8=8A=E6=9C=9F=E8=A1=A8=E5=BA=95=E6=95=B0?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- excel/end_user.go | 4 ++ model/end_user_detail.go | 5 ++ service/end_user.go | 122 +++++++++++++++++++++++++++++++++------ 3 files changed, 112 insertions(+), 19 deletions(-) diff --git a/excel/end_user.go b/excel/end_user.go index 4e8bf1b..c051a33 100644 --- a/excel/end_user.go +++ b/excel/end_user.go @@ -8,11 +8,15 @@ import ( var ( endUserNonPVRecognizers = []*ColumnRecognizer{ {Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1, MustFill: true}, + {Pattern: []string{"上期", "(总)"}, Tag: "lastPeriodOverall", MatchIndex: -1, MustFill: true}, {Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1, MustFill: true}, {Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1, MustFill: true}, } endUserPVRecognizers = append( endUserNonPVRecognizers, + &ColumnRecognizer{Pattern: []string{"上期", "(尖峰)"}, Tag: "lastPeriodCritical", MatchIndex: -1}, + &ColumnRecognizer{Pattern: []string{"上期", "(峰)"}, Tag: "lastPeriodPeak", MatchIndex: -1}, + &ColumnRecognizer{Pattern: []string{"上期", "(谷)"}, Tag: "lastPeriodValley", MatchIndex: -1}, &ColumnRecognizer{Pattern: []string{"本期", "(尖峰)"}, Tag: "currentPeriodCritical", MatchIndex: -1}, &ColumnRecognizer{Pattern: []string{"本期", "(峰)"}, Tag: "currentPeriodPeak", MatchIndex: -1}, &ColumnRecognizer{Pattern: []string{"本期", "(谷)"}, Tag: "currentPeriodValley", MatchIndex: -1}, diff --git a/model/end_user_detail.go b/model/end_user_detail.go index 09c9ccc..7356fd6 100644 --- a/model/end_user_detail.go +++ b/model/end_user_detail.go @@ -53,6 +53,7 @@ type EndUserDetail struct { PublicConsumptionDiluted decimal.NullDecimal `xorm:"numeric(18,8)" json:"publicConsumptionDiluted"` FinalDiluted decimal.NullDecimal `xorm:"numeric(14,2)" json:"finalDiluted"` FinalCharge decimal.NullDecimal `xorm:"numeric(14,2)" json:"finalCharge"` + Initialize bool `xorm:"-" json:"-"` } func (EndUserDetail) TableName() string { @@ -82,7 +83,11 @@ func (d *EndUserDetail) CalculatePeriod() { type EndUserImport struct { MeterId string `excel:"meterId"` + LastPeriodOverall decimal.Decimal `excel:"lastPeriodOverall"` CurrentPeriodOverall decimal.Decimal `excel:"currentPeriodOverall"` + LastPeriodCritical decimal.NullDecimal `excel:"lastPeriodCritical"` + LastPeriodPeak decimal.NullDecimal `excel:"lastPeriodPeak"` + LastPeriodValley decimal.NullDecimal `excel:"lastPeriodValley"` CurrentPeriodCritical decimal.NullDecimal `excel:"currentPeriodCritical"` CurrentPeriodPeak decimal.NullDecimal `excel:"currentPeriodPeak"` CurrentPeriodValley decimal.NullDecimal `excel:"currentPeriodValley"` diff --git a/service/end_user.go b/service/end_user.go index ee3c46d..34bc83a 100644 --- a/service/end_user.go +++ b/service/end_user.go @@ -7,6 +7,7 @@ import ( "electricity_bill_calc/exceptions" "electricity_bill_calc/global" "electricity_bill_calc/model" + "errors" "fmt" "io" "strconv" @@ -20,6 +21,11 @@ import ( type _EndUserService struct{} +type MeterAppears struct { + Meter string + Appears int64 +} + var EndUserService _EndUserService func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) ([]model.EndUserDetail, int64, error) { @@ -93,25 +99,52 @@ func (_EndUserService) FetchSpecificEndUserRecord(reportId, parkId, meterId stri func (_EndUserService) UpdateEndUserRegisterRecord(tx *xorm.Session, record model.EndUserDetail) (err error) { record.CalculatePeriod() - _, err = tx.ID(schemas.NewPK(record.ReportId, record.ParkId, record.MeterId)). - Cols( - "current_period_overall", - "adjust_overall", - "current_period_critical", - "current_period_peak", - "current_period_flat", - "current_perios_valley", - "adjust_critical", - "adjust_peak", - "adjust_flat", - "adjust_valley", - "overall", - "critical", - "peak", - "flat", - "valley", - ). - Update(record) + if record.Initialize { + _, err = tx.ID(schemas.NewPK(record.ReportId, record.ParkId, record.MeterId)). + Cols( + "last_period_overall", + "current_period_overall", + "adjust_overall", + "last_period_critical", + "last_period_peak", + "last_period_flat", + "last_perios_valley", + "current_period_critical", + "current_period_peak", + "current_period_flat", + "current_perios_valley", + "adjust_critical", + "adjust_peak", + "adjust_flat", + "adjust_valley", + "overall", + "critical", + "peak", + "flat", + "valley", + ). + Update(record) + } else { + _, err = tx.ID(schemas.NewPK(record.ReportId, record.ParkId, record.MeterId)). + Cols( + "current_period_overall", + "adjust_overall", + "current_period_critical", + "current_period_peak", + "current_period_flat", + "current_perios_valley", + "adjust_critical", + "adjust_peak", + "adjust_flat", + "adjust_valley", + "overall", + "critical", + "peak", + "flat", + "valley", + ). + Update(record) + } cache.AbolishRelation("end_user_detail") return } @@ -127,6 +160,22 @@ func (es _EndUserService) BatchImportNonPVRegister(reportId string, file io.Read errs.AddError(es.newVirtualExcelAnalysisError(err)) return errs } + reportDetail := new(model.Report) + has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(reportDetail) + if err != nil { + errs.AddError(es.newVirtualExcelAnalysisError(err)) + return errs + } + if !has { + errs.AddError(es.newVirtualExcelAnalysisError(errors.New("未能找到相应的报表。"))) + return errs + } + meterAppers := make([]MeterAppears, 0) + err = global.DBConn.Table(new(model.EndUserDetail)).Where(builder.Eq{"park_id": reportDetail.ParkId}).Select("meter_04kv_id as meter, count(*) as appears").GroupBy("meter_04kv_id").Find(&meterAppers) + if err != nil { + errs.AddError(es.newVirtualExcelAnalysisError(err)) + return errs + } indexedUsers := lo.Reduce( users, func(acc map[string]model.EndUserDetail, elem model.EndUserDetail, index int) map[string]model.EndUserDetail { @@ -156,6 +205,14 @@ func (es _EndUserService) BatchImportNonPVRegister(reportId string, file io.Read for _, im := range imports { if elem, ok := indexedUsers[im.MeterId]; ok { + if appears, has := lo.Find(meterAppers, func(m MeterAppears) bool { + return m.Meter == elem.MeterId + }); has { + if appears.Appears <= 1 { + elem.LastPeriodOverall = im.LastPeriodOverall + elem.Initialize = true + } + } elem.CurrentPeriodOverall = im.CurrentPeriodOverall elem.AdjustOverall = im.AdjustOverall elem.CurrentPeriodCritical = decimal.Zero @@ -193,6 +250,22 @@ func (es _EndUserService) BatchImportPVRegister(reportId string, file io.Reader) errs.AddError(es.newVirtualExcelAnalysisError(err)) return errs } + reportDetail := new(model.Report) + has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(reportDetail) + if err != nil { + errs.AddError(es.newVirtualExcelAnalysisError(err)) + return errs + } + if !has { + errs.AddError(es.newVirtualExcelAnalysisError(errors.New("未能找到相应的报表。"))) + return errs + } + meterAppers := make([]MeterAppears, 0) + err = global.DBConn.Table(new(model.EndUserDetail)).Where(builder.Eq{"park_id": reportDetail.ParkId}).Select("meter_04kv_id as meter, count(*) as appears").GroupBy("meter_04kv_id").Find(&meterAppers) + if err != nil { + errs.AddError(es.newVirtualExcelAnalysisError(err)) + return errs + } indexedUsers := lo.Reduce( users, func(acc map[string]model.EndUserDetail, elem model.EndUserDetail, index int) map[string]model.EndUserDetail { @@ -222,6 +295,17 @@ func (es _EndUserService) BatchImportPVRegister(reportId string, file io.Reader) for _, im := range imports { if elem, ok := indexedUsers[im.MeterId]; ok { + if appears, has := lo.Find(meterAppers, func(m MeterAppears) bool { + return m.Meter == elem.MeterId + }); has { + if appears.Appears > 1 { + elem.LastPeriodOverall = im.LastPeriodOverall + elem.LastPeriodCritical = im.LastPeriodCritical.Decimal + elem.LastPeriodPeak = im.LastPeriodPeak.Decimal + elem.LastPeriodValley = im.LastPeriodValley.Decimal + elem.Initialize = true + } + } elem.CurrentPeriodOverall = im.CurrentPeriodOverall elem.AdjustOverall = im.AdjustOverall elem.CurrentPeriodCritical = im.CurrentPeriodCritical.Decimal