From 23e9e2ec4dc467ae86b62e650c5c02b38936476b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 13 Jun 2023 11:11:10 +0800 Subject: [PATCH] =?UTF-8?q?enhance(meter):=E6=81=A2=E5=A4=8D=E5=AF=B9?= =?UTF-8?q?=E4=BA=8E=E8=A1=A8=E8=AE=A1=E7=B1=BB=E5=9E=8B=E7=9A=84=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=9C=A8=E8=A1=A8=E8=AE=A1=E7=B1=BB=E5=9E=8B=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E9=94=99=E8=AF=AF=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E5=81=9C=E4=B8=8B=E6=9D=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/enums.go | 12 ++++----- service/meter.go | 68 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/model/enums.go b/model/enums.go index fd8b0db..1e8faf4 100644 --- a/model/enums.go +++ b/model/enums.go @@ -1,6 +1,7 @@ package model import ( + "fmt" "strings" ) @@ -19,19 +20,18 @@ const ( METER_INSTALLATION_TENEMENT int16 = iota METER_INSTALLATION_PARK METER_INSTALLATION_POOLING - METER_INSTALLATION_UNDEFINED = 99 ) -func ParseMeterInstallationType(s string) int16 { +func ParseMeterInstallationType(s string) (int16, error) { switch { case strings.Contains(s, "商户"): - return METER_INSTALLATION_TENEMENT + return METER_INSTALLATION_TENEMENT, nil case strings.Contains(s, "公共"): - return METER_INSTALLATION_PARK + return METER_INSTALLATION_PARK, nil case strings.Contains(s, "楼道"): - return METER_INSTALLATION_POOLING + return METER_INSTALLATION_POOLING, nil default: - return METER_INSTALLATION_UNDEFINED + return -1, fmt.Errorf("提供了一个无法识别的表计类型: %s", s) } } diff --git a/service/meter.go b/service/meter.go index 182c987..b08515d 100644 --- a/service/meter.go +++ b/service/meter.go @@ -204,27 +204,55 @@ func (ms _MeterService) BatchImportMeters(pid string, file *multipart.FileHeader ms.log.Error("无法启动数据插入阶段的数据库事务。", zap.Error(err)) return make([]excel.ExcelAnalysisError, 0), fmt.Errorf("无法启动数据插入阶段的数据库事务,%w", err) } - meterCreationForms := lo.Map(records, func(element model.MeterImportRow, _ int) vo.MeterCreationForm { - return vo.MeterCreationForm{ - Code: element.Code, - Address: element.Address, - MeterType: model.ParseMeterInstallationType(*element.MeterType), - Ratio: element.Ratio, - Seq: element.Seq, - Enabled: true, - Building: element.Building, - OnFloor: element.OnFloor, - Area: element.Area, - MeterReadingForm: vo.MeterReadingForm{ - ReadAt: &element.ReadAt, - Overall: element.Overall, - Critical: element.Critical.Decimal, - Peak: element.Peak.Decimal, - Flat: element.Flat.Decimal, - Valley: element.Valley.Decimal, - }, + var meterCreationForms = make([]vo.MeterCreationForm, 0) + for row, element := range records { + if element.MeterType != nil { + meterType, err := model.ParseMeterInstallationType(*element.MeterType) + if err != nil { + ms.log.Error("无法识别表计类型。", zap.Int("record_index", row), zap.Error(err)) + errs = append(errs, excel.ExcelAnalysisError{ + Row: row + 1, + Col: 3, + Err: excel.AnalysisError{ + Err: fmt.Errorf("表计类型无法识别"), + }, + }) + } + meterCreationForms = append(meterCreationForms, vo.MeterCreationForm{ + Code: element.Code, + Address: element.Address, + MeterType: meterType, + Ratio: element.Ratio, + Seq: element.Seq, + Enabled: true, + Building: element.Building, + OnFloor: element.OnFloor, + Area: element.Area, + MeterReadingForm: vo.MeterReadingForm{ + ReadAt: &element.ReadAt, + Overall: element.Overall, + Critical: element.Critical.Decimal, + Peak: element.Peak.Decimal, + Flat: element.Flat.Decimal, + Valley: element.Valley.Decimal, + }, + }) + } else { + ms.log.Error("表计类型不能为空。", zap.Int("record_index", row)) + errs = append(errs, excel.ExcelAnalysisError{ + Row: row + 1, + Col: 3, + Err: excel.AnalysisError{ + Err: fmt.Errorf("表计类型不能为空"), + }, + }) } - }) + } + if len(errs) > 0 { + ms.log.Error("表计档案分析器在解析上传的 Excel 文件时发生错误。", zap.Int("error count", len(errs))) + tx.Rollback(ctx) + return errs, fmt.Errorf("表计档案分析器在解析上传的 Excel 文件时发生错误。") + } for _, record := range meterCreationForms { _, err := repository.MeterRepository.CreateOrUpdateMeter(tx, ctx, pid, record) if err != nil {