diff --git a/excel/abstract.go b/excel/abstract.go index a16b583..7b1504d 100644 --- a/excel/abstract.go +++ b/excel/abstract.go @@ -4,6 +4,7 @@ import ( "electricity_bill_calc/model" "electricity_bill_calc/tools" "encoding/json" + "errors" "fmt" "io" "reflect" @@ -25,6 +26,7 @@ type ColumnRecognizer struct { Pattern []string Tag string MatchIndex int + MustFill bool } 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 { for _, recognizer := range a.Regconizers { if alias == recognizer.Tag && recognizer.MatchIndex != -1 { + var matchValue string 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() { case "string": actualField.Set(reflect.ValueOf(matchValue)) diff --git a/excel/end_user.go b/excel/end_user.go index d684e56..4e8bf1b 100644 --- a/excel/end_user.go +++ b/excel/end_user.go @@ -7,9 +7,9 @@ import ( var ( endUserNonPVRecognizers = []*ColumnRecognizer{ - {Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1}, - {Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1}, - {Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1}, + {Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1, MustFill: true}, + {Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1, MustFill: true}, + {Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1, MustFill: true}, } endUserPVRecognizers = append( endUserNonPVRecognizers, diff --git a/excel/meter_archive.go b/excel/meter_archive.go index 26296fb..9c03079 100644 --- a/excel/meter_archive.go +++ b/excel/meter_archive.go @@ -6,15 +6,15 @@ import ( ) 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: "address", MatchIndex: -1}, {Pattern: []string{"联系人"}, Tag: "contact", MatchIndex: -1}, {Pattern: []string{"电话"}, Tag: "phone", MatchIndex: -1}, - {Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1}, - {Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1}, - {Pattern: []string{"公用设备"}, Tag: "public", MatchIndex: -1}, - {Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1}, + {Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1, MustFill: true}, + {Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1, MustFill: true}, + {Pattern: []string{"公用设备"}, Tag: "public", MatchIndex: -1, MustFill: true}, + {Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1, MustFill: true}, } func NewMeterArchiveExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.Meter04KV], error) {