+ DataPreview
This commit is contained in:
parent
b7fce6c3ba
commit
4426b8bc12
2 changed files with 59 additions and 1 deletions
|
|
@ -160,6 +160,45 @@ func DataValidate(row []interface{}, value interface{}, rule string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// DataPreview 预览数据
|
||||
func (imp *Importer) DataPreview(src from.Source, page int, size int, mapping *Mapping) map[string]interface{} {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
data := []map[string]interface{}{}
|
||||
res := map[string]interface{}{
|
||||
"page": page,
|
||||
"pagesize": size,
|
||||
"pagecnt": 10,
|
||||
"next": page + 1,
|
||||
"prev": page - 1,
|
||||
}
|
||||
|
||||
if mapping == nil {
|
||||
mapping = imp.AutoMapping(src)
|
||||
}
|
||||
|
||||
columns, rows := imp.DataGet(src, page, size, mapping)
|
||||
for _, row := range rows {
|
||||
if len(row) != len(columns) {
|
||||
exception.New("数据异常, 请联系管理员", 500).Ctx(map[string]interface{}{"row": row, "columns": columns}).Throw()
|
||||
}
|
||||
|
||||
rs := map[string]interface{}{}
|
||||
for i := range row {
|
||||
key := columns[i]
|
||||
value := row[i]
|
||||
rs[key] = value
|
||||
}
|
||||
|
||||
data = append(data, rs)
|
||||
}
|
||||
|
||||
res["data"] = data
|
||||
return res
|
||||
}
|
||||
|
||||
// MappingPreview 预览字段映射关系
|
||||
func (imp *Importer) MappingPreview(src from.Source) *Mapping {
|
||||
|
||||
|
|
@ -242,9 +281,9 @@ func (imp *Importer) Run(src from.Source, mapping *Mapping) map[string]int {
|
|||
xlog.Printf("导入处理器未返回失败结果 %#v %d %d", response, line, length)
|
||||
})
|
||||
return map[string]int{
|
||||
"total": total,
|
||||
"success": total - failed - ignore,
|
||||
"failure": failed,
|
||||
"total": total,
|
||||
"ignore": ignore,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,3 +94,22 @@ func TestRunSimple(t *testing.T) {
|
|||
assert.Equal(t, 2, res["success"])
|
||||
assert.Equal(t, 4, res["total"])
|
||||
}
|
||||
|
||||
func TestDataPreviewSimple(t *testing.T) {
|
||||
simple := filepath.Join(config.Conf.Root, "imports", "assets", "simple.xlsx")
|
||||
file := xlsx.Open(simple)
|
||||
defer file.Close()
|
||||
imp := Select("order")
|
||||
mapping := imp.AutoMapping(file)
|
||||
|
||||
res := imp.DataPreview(file, 2, 3, mapping)
|
||||
data, ok := res["data"].([]map[string]interface{})
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 2, res["page"])
|
||||
assert.Equal(t, 3, res["next"])
|
||||
assert.Equal(t, 10, res["pagecnt"])
|
||||
assert.Equal(t, 3, res["pagesize"])
|
||||
assert.Equal(t, 1, res["prev"])
|
||||
assert.Equal(t, 1, len(data))
|
||||
assert.Equal(t, 11, len(data[0]))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue