1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
--[[
    编码: WMS-07-10
    名称: 导入
    作者:HAN   
    日期:2025-7-2 
    入口函数: Import
 
 
    功能说明:
        通过excel文件导入WMS常量
 
    变更历史:
 
--]]
wms_base = require ("wms_base")
 
local function value_sort( a, b ) return a < b end
 
function Import(strLuaDEID)
    local nRet, strRetInfo, strNo
    -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
    local row_data = {}
    nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0 or strRetInfo == '') then 
        lua.Stop( strLuaDEID, "无法获取导入数据!")
        return
    end
 
    local row_attrs
    local n, nCount, nRows, nIndex, const
  
    -- 导入是可分多页分批导入, 因此每次的起始行不一样
    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
 
    -- 步骤1 获取从excel导入的一行数据
    nRow = nStartRow
    for row = 1, #row_data do
        row_attrs = row_data[row]
        nCount = #row_attrs
        -- 初始化 货位 对象
        const = m3.AllocObject(strLuaDEID,"WMS_Const")
        nIndex = 1
        value_array = {}
        strNo = ''
        str_err_msg = ''
        for n = 1, nCount 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
                    const.group = strValue
                elseif (strAttr == "常量名称") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end                  
                    const.name = strValue
                elseif (strAttr == "值") then
                    const.value = strValue         
                elseif (strAttr == "系统固有") then
                    if (strValue == '') then 
                        strValue = 'N'
                    end
                    const.sys = strValue
                elseif (strAttr == "备注") then
                    const.note = strValue
                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 )
        else
             -- 判断物料是否已经存在,如果已经存在刷新新的属性
            -- 本项目为单一货主,因此直接用 S_ITEM_CODE 判断
            strCondition = "S_NAME = '"..const.name.."'"
            nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr( strLuaDEID, "WMS_Const", strCondition )
 
            if ( nRet > 1 ) then 
                local err_msg_row = {
                    row_no = nRow,
                    err_msg = "导入物料信息失败: 在检查 inco = '"..sku.item_code.."'是否存在时失败!"..strRetInfo
                }
                table.insert( err_msg_list, err_msg_row )
            end   
            if ( nRet == 1 ) then
                -- 不存在
                nRet, sku = m3.CreateDataObj(strLuaDEID, const )
                if (nRet ~= 0 ) then 
                    local err_msg_row = {
                        row_no = nRow,
                        err_msg = "序号='"..strNo.."'导入行在创建【物料】时失败!"..sku
                    }
                    table.insert( err_msg_list, err_msg_row )
                end              
            end
        end
        nRow = nRow + 1
    end
    -- 把导入过程中的错误信息返回到前端
    local import_fail = {
        result = {
            fail = err_msg_list
        }
    }
    mobox.returnValue( strLuaDEID, RETSTR_TYPE.Json, lua.table2str(import_fail) )
end