enhance(excel):增加解析日期与时间类型内容的功能。
This commit is contained in:
parent
ab2bcb3cf6
commit
48753eb3f0
|
@ -2,6 +2,7 @@ package excel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"electricity_bill_calc/tools"
|
"electricity_bill_calc/tools"
|
||||||
|
"electricity_bill_calc/types"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -21,7 +22,7 @@ type ExcelTemplateGenerator interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ColumnRecognizer struct {
|
type ColumnRecognizer struct {
|
||||||
Pattern []string
|
Pattern [][]string
|
||||||
Tag string
|
Tag string
|
||||||
MatchIndex int
|
MatchIndex int
|
||||||
MustFill bool
|
MustFill bool
|
||||||
|
@ -43,7 +44,7 @@ type ExcelAnalysisError struct {
|
||||||
Err AnalysisError `json:"error"`
|
Err AnalysisError `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewColumnRecognizer(tag string, patterns ...string) ColumnRecognizer {
|
func NewColumnRecognizer(tag string, patterns ...[]string) ColumnRecognizer {
|
||||||
return ColumnRecognizer{
|
return ColumnRecognizer{
|
||||||
Pattern: patterns,
|
Pattern: patterns,
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
|
@ -65,9 +66,17 @@ func (e ExcelAnalysisError) Error() string {
|
||||||
|
|
||||||
func (r *ColumnRecognizer) Recognize(cellValue string) bool {
|
func (r *ColumnRecognizer) Recognize(cellValue string) bool {
|
||||||
matches := make([]bool, 0)
|
matches := make([]bool, 0)
|
||||||
for _, p := range r.Pattern {
|
for _, pG := range r.Pattern {
|
||||||
matches = append(matches, strings.Contains(cellValue, p))
|
groupMatch := make([]bool, 0)
|
||||||
|
for _, p := range pG {
|
||||||
|
groupMatch = append(groupMatch, strings.Contains(cellValue, p))
|
||||||
}
|
}
|
||||||
|
// 这句表示在每一个匹配组中,只要有一个匹配项,就算匹配成功
|
||||||
|
matches = append(matches, lo.Reduce(groupMatch, func(acc, elem bool, index int) bool {
|
||||||
|
return acc || elem
|
||||||
|
}, false))
|
||||||
|
}
|
||||||
|
// 这句表示在尊有的匹配组中,必须全部的匹配组都完成匹配,才算匹配成功
|
||||||
return lo.Reduce(matches, func(acc, elem bool, index int) bool {
|
return lo.Reduce(matches, func(acc, elem bool, index int) bool {
|
||||||
return acc && elem
|
return acc && elem
|
||||||
}, true)
|
}, true)
|
||||||
|
@ -187,6 +196,54 @@ func (a *ExcelAnalyzer[T]) Analysis(bean T) ([]T, []ExcelAnalysisError) {
|
||||||
} else {
|
} else {
|
||||||
actualField.SetBool(false)
|
actualField.SetBool(false)
|
||||||
}
|
}
|
||||||
|
case "types.Date":
|
||||||
|
if len(matchValue) == 0 {
|
||||||
|
actualField.Set(reflect.ValueOf(types.NewEmptyDate()))
|
||||||
|
} else {
|
||||||
|
v, err := types.ParseDate(matchValue)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期格式。%w", err)}})
|
||||||
|
actualField.Set(reflect.ValueOf(types.NewEmptyDate()))
|
||||||
|
} else {
|
||||||
|
actualField.Set(reflect.ValueOf(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "*types.Date":
|
||||||
|
if len(matchValue) == 0 {
|
||||||
|
actualField.Set(reflect.ValueOf(nil))
|
||||||
|
} else {
|
||||||
|
v, err := types.ParseDate(matchValue)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期格式。%w", err)}})
|
||||||
|
actualField.Set(reflect.ValueOf(nil))
|
||||||
|
} else {
|
||||||
|
actualField.Set(reflect.ValueOf(&v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "types.DateTime":
|
||||||
|
if len(matchValue) == 0 {
|
||||||
|
actualField.Set(reflect.ValueOf(types.NewEmptyDateTime()))
|
||||||
|
} else {
|
||||||
|
v, err := types.ParseDateTime(matchValue)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期时间格式。%w", err)}})
|
||||||
|
actualField.Set(reflect.ValueOf(types.NewEmptyDateTime()))
|
||||||
|
} else {
|
||||||
|
actualField.Set(reflect.ValueOf(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "*types.DateTime":
|
||||||
|
if len(matchValue) == 0 {
|
||||||
|
actualField.Set(reflect.ValueOf(nil))
|
||||||
|
} else {
|
||||||
|
v, err := types.ParseDateTime(matchValue)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期时间格式。%w", err)}})
|
||||||
|
actualField.Set(reflect.ValueOf(nil))
|
||||||
|
} else {
|
||||||
|
actualField.Set(reflect.ValueOf(&v))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user