2ed5dfa6a1
iotlcm-app:小程序端 + 用户端服务。 - app/:uni-app 小程序前端(基于 unibest,MIT) - 微信 AppID → YOUR_WX_APPID,uni-app AppID → __UNI__YOURAPPID - 内网后端地址 → admin.iotlcm.example.com - 应用标题与 unibest 模板 README 重写(保留上游致谢与 MIT 声明) - server/:GoFrame 用户端服务(基于 XiuAdmin,Apache-2.0) - module xiuadmin → git.5iot.com/iotlcm-public/iotlcm-app(360 文件) - 支付回调地址 → app.iotlcm.example.com,商户名与应用名泛化 - 真实手机号 → 合法 11 位编造号(保留号段前 3 位 + 后 4 位) - 依赖号码尾部的断言同步更新 - 补 server/LICENSE(Apache-2.0) - 移除内部资产:CI 触发器 - 补根 README,LF 行尾 + .gitattributes 已知:TestSysDept_RefreshDeptAncestors 在源仓同一提交上同样失败,属既有缺陷。
203 lines
4.6 KiB
Go
203 lines
4.6 KiB
Go
package iot_excelize
|
|
|
|
import (
|
|
"io"
|
|
"reflect"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"github.com/xuri/excelize/v2"
|
|
)
|
|
|
|
// ImportByReader parses an uploaded Excel file into typed rows. The first row is
|
|
// treated as the header row and matched by excel/dc/description/json tags.
|
|
func ImportByReader[T any](r io.Reader, sheetName string) (*ImportResult[T], error) {
|
|
f, err := excelize.OpenReader(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
if sheetName == "" {
|
|
sheetName = firstSheetName(f)
|
|
}
|
|
rows, err := f.Rows(sheetName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
schemas := parseSchema(reflect.TypeOf((*T)(nil)).Elem())
|
|
schemaByHeader := make(map[string]fieldSchema, len(schemas))
|
|
for _, schema := range schemas {
|
|
schemaByHeader[normalizeHeader(schema.Header)] = schema
|
|
}
|
|
if len(schemaByHeader) == 0 {
|
|
return nil, gerror.New("导入模型没有可读取字段")
|
|
}
|
|
|
|
result := &ImportResult[T]{Rows: make([]T, 0)}
|
|
headerIndex := make(map[int]fieldSchema)
|
|
rowIndex := 0
|
|
hasHeader := false
|
|
|
|
for rows.Next() {
|
|
rowIndex++
|
|
cols, err := rows.Columns()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rowIndex == 1 {
|
|
hasHeader = true
|
|
for i, header := range cols {
|
|
if schema, ok := schemaByHeader[normalizeHeader(header)]; ok {
|
|
headerIndex[i] = schema
|
|
}
|
|
}
|
|
if len(headerIndex) == 0 {
|
|
return nil, gerror.New("Excel表头与导入模型不匹配")
|
|
}
|
|
continue
|
|
}
|
|
if isEmptyRow(cols) {
|
|
continue
|
|
}
|
|
|
|
result.TotalRows++
|
|
item, rowErrors := parseImportRow[T](rowIndex, cols, headerIndex, schemas)
|
|
if len(rowErrors) > 0 {
|
|
result.Errors = append(result.Errors, rowErrors...)
|
|
result.FailedRows++
|
|
continue
|
|
}
|
|
result.Rows = append(result.Rows, item)
|
|
result.SuccessRows++
|
|
}
|
|
if err = rows.Error(); err != nil {
|
|
return nil, err
|
|
}
|
|
if !hasHeader {
|
|
return nil, gerror.New("Excel文件缺少表头")
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func parseImportRow[T any](rowIndex int, cols []string, headerIndex map[int]fieldSchema, schemas []fieldSchema) (T, []RowError) {
|
|
var item T
|
|
var errors []RowError
|
|
value := reflect.ValueOf(&item).Elem()
|
|
if value.Kind() == reflect.Ptr {
|
|
value.Set(reflect.New(value.Type().Elem()))
|
|
value = value.Elem()
|
|
}
|
|
|
|
seen := make(map[string]bool)
|
|
for colIndex, schema := range headerIndex {
|
|
raw := ""
|
|
if colIndex < len(cols) {
|
|
raw = normalizeHeader(cols[colIndex])
|
|
}
|
|
seen[schema.Name] = true
|
|
if raw == "" {
|
|
if schema.Required {
|
|
errors = append(errors, RowError{
|
|
Row: rowIndex,
|
|
Column: schema.Header,
|
|
Field: schema.Name,
|
|
Message: requiredMessage(schema.Field, schema.Header),
|
|
})
|
|
}
|
|
continue
|
|
}
|
|
if err := setFieldValue(value, schema, raw); err != nil {
|
|
errors = append(errors, RowError{
|
|
Row: rowIndex,
|
|
Column: schema.Header,
|
|
Field: schema.Name,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
}
|
|
|
|
for _, schema := range schemas {
|
|
if schema.Required && !seen[schema.Name] {
|
|
errors = append(errors, RowError{
|
|
Row: rowIndex,
|
|
Column: schema.Header,
|
|
Field: schema.Name,
|
|
Message: "缺少必要列:" + schema.Header,
|
|
})
|
|
}
|
|
}
|
|
return item, errors
|
|
}
|
|
|
|
func setFieldValue(root reflect.Value, schema fieldSchema, raw string) error {
|
|
field := fieldByIndexAlloc(root, schema.Index)
|
|
if field.Kind() == reflect.Ptr {
|
|
elemType := field.Type().Elem()
|
|
elemValue := reflect.New(elemType).Elem()
|
|
if err := setValue(elemValue, raw); err != nil {
|
|
return err
|
|
}
|
|
field.Set(elemValue.Addr())
|
|
return nil
|
|
}
|
|
return setValue(field, raw)
|
|
}
|
|
|
|
func fieldByIndexAlloc(root reflect.Value, index []int) reflect.Value {
|
|
field := root
|
|
for _, i := range index {
|
|
if field.Kind() == reflect.Ptr {
|
|
if field.IsNil() {
|
|
field.Set(reflect.New(field.Type().Elem()))
|
|
}
|
|
field = field.Elem()
|
|
}
|
|
field = field.Field(i)
|
|
}
|
|
return field
|
|
}
|
|
|
|
func setValue(field reflect.Value, raw string) error {
|
|
if !field.CanSet() {
|
|
return nil
|
|
}
|
|
if field.Type() == reflect.TypeOf(gtime.Time{}) {
|
|
t := gtime.NewFromStr(raw)
|
|
if t == nil {
|
|
return gconv.Scan(raw, field.Addr().Interface())
|
|
}
|
|
field.Set(reflect.ValueOf(*t))
|
|
return nil
|
|
}
|
|
if field.Type() == reflect.TypeOf(>ime.Time{}) {
|
|
t := gtime.NewFromStr(raw)
|
|
if t == nil {
|
|
return gconv.Scan(raw, field.Addr().Interface())
|
|
}
|
|
field.Set(reflect.ValueOf(t))
|
|
return nil
|
|
}
|
|
return gconv.Scan(raw, field.Addr().Interface())
|
|
}
|
|
|
|
func firstSheetName(f *excelize.File) string {
|
|
sheets := f.GetSheetList()
|
|
if len(sheets) == 0 {
|
|
return "Sheet1"
|
|
}
|
|
return sheets[0]
|
|
}
|
|
|
|
func isEmptyRow(cols []string) bool {
|
|
for _, col := range cols {
|
|
if normalizeHeader(col) != "" {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|