enhance(excel):调整Excel分析,以避免因为单元格没有填写导致的panic。

This commit is contained in:
徐涛 2022-09-02 09:52:45 +08:00
parent 17bde54c7c
commit e1d55e4fc7
3 changed files with 21 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"electricity_bill_calc/model" "electricity_bill_calc/model"
"electricity_bill_calc/tools" "electricity_bill_calc/tools"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
@ -25,6 +26,7 @@ type ColumnRecognizer struct {
Pattern []string Pattern []string
Tag string Tag string
MatchIndex int MatchIndex int
MustFill bool
} }
type ExcelAnalyzer[T any] struct { type ExcelAnalyzer[T any] struct {
@ -126,8 +128,18 @@ func (a *ExcelAnalyzer[T]) Analysis(bean T) ([]T, []ExcelAnalysisError) {
if alias, ok := field.Tag.Lookup("excel"); ok { if alias, ok := field.Tag.Lookup("excel"); ok {
for _, recognizer := range a.Regconizers { for _, recognizer := range a.Regconizers {
if alias == recognizer.Tag && recognizer.MatchIndex != -1 { if alias == recognizer.Tag && recognizer.MatchIndex != -1 {
var matchValue string
actualField := instance.Elem().FieldByName(field.Name) actualField := instance.Elem().FieldByName(field.Name)
matchValue := cols[recognizer.MatchIndex] if recognizer.MatchIndex > len(cols)-1 {
if recognizer.MustFill {
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: errors.New("单元格内不能没有内容。")}})
continue
} else {
matchValue = ""
}
} else {
matchValue = cols[recognizer.MatchIndex]
}
switch field.Type.String() { switch field.Type.String() {
case "string": case "string":
actualField.Set(reflect.ValueOf(matchValue)) actualField.Set(reflect.ValueOf(matchValue))

View File

@ -7,9 +7,9 @@ import (
var ( var (
endUserNonPVRecognizers = []*ColumnRecognizer{ endUserNonPVRecognizers = []*ColumnRecognizer{
{Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1}, {Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1, MustFill: true},
{Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1}, {Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1, MustFill: true},
{Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1}, {Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1, MustFill: true},
} }
endUserPVRecognizers = append( endUserPVRecognizers = append(
endUserNonPVRecognizers, endUserNonPVRecognizers,

View File

@ -6,15 +6,15 @@ import (
) )
var meter04kVExcelRecognizers = []*ColumnRecognizer{ var meter04kVExcelRecognizers = []*ColumnRecognizer{
{Pattern: []string{"表号"}, Tag: "code", MatchIndex: -1}, {Pattern: []string{"表号"}, Tag: "code", MatchIndex: -1, MustFill: true},
{Pattern: []string{"户名"}, Tag: "name", MatchIndex: -1}, {Pattern: []string{"户名"}, Tag: "name", MatchIndex: -1},
{Pattern: []string{"户址"}, Tag: "address", MatchIndex: -1}, {Pattern: []string{"户址"}, Tag: "address", MatchIndex: -1},
{Pattern: []string{"联系人"}, Tag: "contact", MatchIndex: -1}, {Pattern: []string{"联系人"}, Tag: "contact", MatchIndex: -1},
{Pattern: []string{"电话"}, Tag: "phone", MatchIndex: -1}, {Pattern: []string{"电话"}, Tag: "phone", MatchIndex: -1},
{Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1}, {Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1, MustFill: true},
{Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1}, {Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1, MustFill: true},
{Pattern: []string{"公用设备"}, Tag: "public", MatchIndex: -1}, {Pattern: []string{"公用设备"}, Tag: "public", MatchIndex: -1, MustFill: true},
{Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1}, {Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1, MustFill: true},
} }
func NewMeterArchiveExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.Meter04KV], error) { func NewMeterArchiveExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.Meter04KV], error) {