diff --git a/importer/importer.go b/importer/importer.go index ad77f32a..c6602905 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -9,6 +9,7 @@ import ( jsoniter "github.com/json-iterator/go" "github.com/yaoapp/gou" + "github.com/yaoapp/kun/any" "github.com/yaoapp/kun/exception" "github.com/yaoapp/xiang/config" "github.com/yaoapp/xiang/importer/from" @@ -205,6 +206,7 @@ func (imp *Importer) Run(src from.Source, mapping *Mapping) map[string]int { total := 0 failed := 0 + ignore := 0 imp.Chunk(src, mapping, func(line int, data [][]interface{}) { length := len(data) total = total + length @@ -223,20 +225,27 @@ func (imp *Importer) Run(src from.Source, mapping *Mapping) map[string]int { return } - if res, ok := response.(int); ok { - failed = failed + res + if res, ok := response.([]int); ok && len(res) > 1 { + failed = failed + res[0] + ignore = ignore + res[1] return - } else if res, ok := response.(int64); ok { - failed = failed + int(res) + } else if res, ok := response.([]int64); ok && len(res) > 1 { + failed = failed + int(res[0]) + ignore = ignore + int(res[1]) + return + } else if res, ok := response.([]interface{}); ok && len(res) > 1 { + failed = failed + any.Of(res[0]).CInt() + ignore = ignore + any.Of(res[1]).CInt() return } xlog.Printf("导入处理器未返回失败结果 %#v %d %d", response, line, length) }) return map[string]int{ - "success": total - failed, + "success": total - failed - ignore, "failure": failed, "total": total, + "ignore": ignore, } } diff --git a/importer/importer_test.go b/importer/importer_test.go index 85381f02..94e3507d 100644 --- a/importer/importer_test.go +++ b/importer/importer_test.go @@ -89,7 +89,8 @@ func TestRunSimple(t *testing.T) { imp := Select("order") mapping := imp.AutoMapping(file) res := imp.Run(file, mapping) - assert.Equal(t, 2, res["failure"]) + assert.Equal(t, 1, res["ignore"]) + assert.Equal(t, 1, res["failure"]) assert.Equal(t, 2, res["success"]) assert.Equal(t, 4, res["total"]) } diff --git a/tests/libs/rules.js b/tests/libs/rules.js index e247713f..3e380f07 100644 --- a/tests/libs/rules.js +++ b/tests/libs/rules.js @@ -22,5 +22,16 @@ function mobile(value, row) { } function ImportData(columns, data) { - return 1; + last = columns.length - 1; + ignore = 0; + failure = 0; + if (data.length > 1) { + failure = 1; + } + for (var i in data) { + if (data[i][last] == false) { + ignore = ignore + 1; + } + } + return [failure, ignore]; }