enhance(enduser):终端用户抄表时可根据当前数据库中记录的数量决定是否读取上传表格中的上期表底数。

This commit is contained in:
徐涛 2022-09-02 19:43:26 +08:00
parent e1d55e4fc7
commit c9f8235339
3 changed files with 112 additions and 19 deletions

View File

@ -8,11 +8,15 @@ import (
var ( var (
endUserNonPVRecognizers = []*ColumnRecognizer{ endUserNonPVRecognizers = []*ColumnRecognizer{
{Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1, MustFill: true}, {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: "currentPeriodOverall", MatchIndex: -1, MustFill: true},
{Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1, MustFill: true}, {Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1, MustFill: true},
} }
endUserPVRecognizers = append( endUserPVRecognizers = append(
endUserNonPVRecognizers, 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: "currentPeriodCritical", MatchIndex: -1},
&ColumnRecognizer{Pattern: []string{"本期", "(峰)"}, Tag: "currentPeriodPeak", MatchIndex: -1}, &ColumnRecognizer{Pattern: []string{"本期", "(峰)"}, Tag: "currentPeriodPeak", MatchIndex: -1},
&ColumnRecognizer{Pattern: []string{"本期", "(谷)"}, Tag: "currentPeriodValley", MatchIndex: -1}, &ColumnRecognizer{Pattern: []string{"本期", "(谷)"}, Tag: "currentPeriodValley", MatchIndex: -1},

View File

@ -53,6 +53,7 @@ type EndUserDetail struct {
PublicConsumptionDiluted decimal.NullDecimal `xorm:"numeric(18,8)" json:"publicConsumptionDiluted"` PublicConsumptionDiluted decimal.NullDecimal `xorm:"numeric(18,8)" json:"publicConsumptionDiluted"`
FinalDiluted decimal.NullDecimal `xorm:"numeric(14,2)" json:"finalDiluted"` FinalDiluted decimal.NullDecimal `xorm:"numeric(14,2)" json:"finalDiluted"`
FinalCharge decimal.NullDecimal `xorm:"numeric(14,2)" json:"finalCharge"` FinalCharge decimal.NullDecimal `xorm:"numeric(14,2)" json:"finalCharge"`
Initialize bool `xorm:"-" json:"-"`
} }
func (EndUserDetail) TableName() string { func (EndUserDetail) TableName() string {
@ -82,7 +83,11 @@ func (d *EndUserDetail) CalculatePeriod() {
type EndUserImport struct { type EndUserImport struct {
MeterId string `excel:"meterId"` MeterId string `excel:"meterId"`
LastPeriodOverall decimal.Decimal `excel:"lastPeriodOverall"`
CurrentPeriodOverall decimal.Decimal `excel:"currentPeriodOverall"` 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"` CurrentPeriodCritical decimal.NullDecimal `excel:"currentPeriodCritical"`
CurrentPeriodPeak decimal.NullDecimal `excel:"currentPeriodPeak"` CurrentPeriodPeak decimal.NullDecimal `excel:"currentPeriodPeak"`
CurrentPeriodValley decimal.NullDecimal `excel:"currentPeriodValley"` CurrentPeriodValley decimal.NullDecimal `excel:"currentPeriodValley"`

View File

@ -7,6 +7,7 @@ import (
"electricity_bill_calc/exceptions" "electricity_bill_calc/exceptions"
"electricity_bill_calc/global" "electricity_bill_calc/global"
"electricity_bill_calc/model" "electricity_bill_calc/model"
"errors"
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
@ -20,6 +21,11 @@ import (
type _EndUserService struct{} type _EndUserService struct{}
type MeterAppears struct {
Meter string
Appears int64
}
var EndUserService _EndUserService var EndUserService _EndUserService
func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) ([]model.EndUserDetail, int64, error) { func (_EndUserService) SearchEndUserRecord(reportId, keyword string, page int) ([]model.EndUserDetail, int64, error) {
@ -93,6 +99,32 @@ func (_EndUserService) FetchSpecificEndUserRecord(reportId, parkId, meterId stri
func (_EndUserService) UpdateEndUserRegisterRecord(tx *xorm.Session, record model.EndUserDetail) (err error) { func (_EndUserService) UpdateEndUserRegisterRecord(tx *xorm.Session, record model.EndUserDetail) (err error) {
record.CalculatePeriod() record.CalculatePeriod()
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)). _, err = tx.ID(schemas.NewPK(record.ReportId, record.ParkId, record.MeterId)).
Cols( Cols(
"current_period_overall", "current_period_overall",
@ -112,6 +144,7 @@ func (_EndUserService) UpdateEndUserRegisterRecord(tx *xorm.Session, record mode
"valley", "valley",
). ).
Update(record) Update(record)
}
cache.AbolishRelation("end_user_detail") cache.AbolishRelation("end_user_detail")
return return
} }
@ -127,6 +160,22 @@ func (es _EndUserService) BatchImportNonPVRegister(reportId string, file io.Read
errs.AddError(es.newVirtualExcelAnalysisError(err)) errs.AddError(es.newVirtualExcelAnalysisError(err))
return errs 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( indexedUsers := lo.Reduce(
users, users,
func(acc map[string]model.EndUserDetail, elem model.EndUserDetail, index int) map[string]model.EndUserDetail { 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 { for _, im := range imports {
if elem, ok := indexedUsers[im.MeterId]; ok { 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.CurrentPeriodOverall = im.CurrentPeriodOverall
elem.AdjustOverall = im.AdjustOverall elem.AdjustOverall = im.AdjustOverall
elem.CurrentPeriodCritical = decimal.Zero elem.CurrentPeriodCritical = decimal.Zero
@ -193,6 +250,22 @@ func (es _EndUserService) BatchImportPVRegister(reportId string, file io.Reader)
errs.AddError(es.newVirtualExcelAnalysisError(err)) errs.AddError(es.newVirtualExcelAnalysisError(err))
return errs 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( indexedUsers := lo.Reduce(
users, users,
func(acc map[string]model.EndUserDetail, elem model.EndUserDetail, index int) map[string]model.EndUserDetail { 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 { for _, im := range imports {
if elem, ok := indexedUsers[im.MeterId]; ok { 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.CurrentPeriodOverall = im.CurrentPeriodOverall
elem.AdjustOverall = im.AdjustOverall elem.AdjustOverall = im.AdjustOverall
elem.CurrentPeriodCritical = im.CurrentPeriodCritical.Decimal elem.CurrentPeriodCritical = im.CurrentPeriodCritical.Decimal