forked from free-lancers/electricity_bill_calc_service
		
	enhance(meter):恢复对于表计类型的解析错误提示,并在表计类型解析出现错误的时候停下来。
This commit is contained in:
		| @@ -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) | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user