1
Jianw
10 天以前 88e26a2a960dbbc148332772448b79b9877102d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
   --[[ 
    编码: JX-115-10
    名称: 空料箱初始化-导入
    作者:HAN    
    日期:2025-1-29    
    入口函数: Import
 
    功能说明:
    
--]]
 
wms_base = require ("wms_base")
wms_cntr= require( "wms_container" )
 
 
function ImportLocation(strLuaDEID)
    local nRet, strRetInfo, strNo
    
    -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
    local row_data = {}
    nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0 or strRetInfo == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), "无法获取导入数据!")
    end
 
    local row_attrs
    local n, nCount, nRows, nIndex
    local cntr_type, max_cell_num
 
    -- 导入是可分多页分批导入,因此每次的起始行不一样
    nRet, strRetInfo = mobox.getGlobalAttrValue(strLuaDEID, "start_row")
    if (nRet ~= 0) then
        mobox.error(strLuaDEID, "获取批量导入全局参数 start_row 失败!" .. strRetInfo)
        return
    end
    local nStartRow = tonumber(strRetInfo)
    local err_msg_list = {} -- 用于写导入错误信息
    local str_err_msg
    local cntr
    local strCondition, strInsertSql
 
    -- 步骤1 获取从excel导入的一行数据
    nRow = nStartRow
    for row = 1, #row_data do
        row_attrs = row_data[row]
        lua.Debug(strLuaDEID, debug.getinfo(1), "row_attrs", row_attrs)
 
        -- 初始化空料箱对象
        str_err_msg = ''
        local cntr_code
 
        for n = 1, #row_attrs do
            strAttr = row_attrs[n].attr
            strValue = row_attrs[n].value
            
            if (strAttr ~= '') then
                if (strAttr == "料箱编码") then
                    if (strValue == '') then
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    cntr_code = strValue
                end
            end
        end
 
        if (str_err_msg == '') then
            -- 判断容器是否存在
            if (wms_cntr.Exist(strLuaDEID, cntr_code) == true) then
                -- 容器存在,记录错误信息并跳出当前循环
                str_err_msg = "料箱编码 '" .. cntr_code .. "' 已存在!"
                goto err_msg_process
            else
                -- 容器不存在,创建新的容器
                local cntr_obj = m3.AllocObject(strLuaDEID, "Container")
                cntr_obj.code = cntr_code
                cntr_obj.source = "巨星"
                cntr_obj.type = 2
                
                -- 创建新的容器记录
                nRet, cntr_obj = m3.CreateDataObj(strLuaDEID, cntr_obj)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "创建容器失败!" .. cntr_obj)
                end
            end
        end
 
        ::err_msg_process::
        if (str_err_msg ~= '') then
            local err_msg_row = {
                row_no = nRow,
                err_msg = str_err_msg
            }
            table.insert(err_msg_list, err_msg_row)
        end
        nRow = nRow + 1
    end
 
    -- 把导入过程中的错误信息返回到前端
    local import_fail = {
        result = {
            fail = err_msg_list
        }
    }
    mobox.returnValue(strLuaDEID, 1, lua.table2str(import_fail))
    lua.Debug(strLuaDEID, debug.getinfo(1), "import_fail", import_fail)
end