fy36
15 小时以前 f1a64ab92120ffc0f1b1cf2c4848528a0d6b5d18
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
wms_base = require("wms_base")
xml = require("oi_base_xml")
mobox = require("OILua_JavelinExt")
m3 = require("oi_base_mobox")
 
-- XML特殊字符转义函数
local function xml_escape(str)
    if not str then
        return ""
    end
    return tostring(str):gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;"):gsub("\"", "&quot;"):gsub("'", "&apos;")
end
 
-- 硬编码XML生成器
local function generate_raw_xml(result)
    local xml = [[<?xml version="1.0" encoding="UTF-8"?>
<response>
    <flag>]] .. xml_escape(result.flag) .. [[</flag>
    <code>]] .. xml_escape(result.code) .. [[</code>
    <message>]] .. xml_escape(result.message) .. [[</message>]]
 
    if result.data and #result.data > 0 then
        xml = xml .. [[
    <data>]]
        for _, item in ipairs(result.data) do
            xml = xml .. [[
        <item>
            <productLine>]] .. xml_escape(item.productLine) .. [[</productLine>
            <storerId>]] .. xml_escape(item.storerId) .. [[</storerId>
            <cid>]] .. xml_escape(item.cid) .. [[</cid>
            <ownerId>]] .. xml_escape(item.ownerId) .. [[</ownerId>
            <skuId>]] .. xml_escape(item.skuId) .. [[</skuId>
            <boxCode>]] .. xml_escape(item.boxCode) .. [[</boxCode>
            <qty>]] .. xml_escape(item.qty) .. [[</qty>
            <registerNo>]] .. xml_escape(item.registerNo) .. [[</registerNo>
            <skuStatus>]] .. xml_escape(item.skuStatus) .. [[</skuStatus>
            <expiryDate>]] .. xml_escape(item.expiryDate) .. [[</expiryDate>
            <batchNo>]] .. xml_escape(item.batchNo) .. [[</batchNo>
            <productDate>]] .. xml_escape(item.productDate) .. [[</productDate>
            <produceCode>]] .. xml_escape(item.produceCode) .. [[</produceCode>
            <locationId>]] .. xml_escape(item.locationId) .. [[</locationId>
        </item>]]
        end
        xml = xml .. [[
    </data>]]
    else
        xml = xml .. [[
    <data/>]]
    end
    xml = xml .. [[
</response>]]
    return xml
end
 
-- 创建统一返回结果
local function Create_result(flag, code, msg, data)
    return {
        flag = flag or "success",
        code = code or "0",
        message = msg or "",
        data = data or {}
    }
end
 
-- 深度遍历查找XML节点(最稳定的方法)
local function deep_find_xml_node(data, node_names)
    if type(data) ~= "table" then
        return nil
    end
 
    -- 先尝试直接路径查找
    local current = data
    for _, name in ipairs(node_names) do
        if type(current) ~= "table" then
            break
        end
 
        -- 尝试带v1命名空间和不带命名空间的节点
        current = current["v1:" .. name] or current[name]
        if not current then
            break
        end
    end
    if current then
        return current
    end
 
    -- 深度遍历整个XML树查找
    local function deep_search(t, names, index)
        if index > #names then
            return t
        end
        local current_name = names[index]
 
        for k, v in pairs(t) do
            if (k == current_name or k == "v1:" .. current_name) and type(v) == "table" then
                local result = deep_search(v, names, index + 1)
                if result then
                    return result
                end
            elseif type(v) == "table" then
                local result = deep_search(v, names, index)
                if result then
                    return result
                end
            end
        end
        return nil
    end
 
    return deep_search(data, node_names, 1)
end
 
-- 查询库存明细
local function query_inventory_detail(strLuaDEID, query_params)
    local strCondition = string.format(" S_STORER = '%s' AND S_OWNER = '%s' AND S_ITEM_CODE = '%s'",
        query_params.storerId, query_params.ownerId, query_params.skuId)
 
    if query_params.locationId and query_params.locationId ~= "" then
        strCondition = strCondition .. string.format(" AND S_LOC_CODE = '%s'", query_params.locationId)
    end
 
    if query_params.produceCode and query_params.produceCode ~= "" then
        strCondition = strCondition .. string.format(" AND S_BATCH_NO = '%s'", query_params.produceCode)
    end
 
    local nRet, data_objects = m3.QueryDataObject(strLuaDEID, "INV_Detail", strCondition)
    if nRet ~= 0 then
        return nRet, data_objects
    end
 
    local formatted_result = {}
    for n = 1, #data_objects do
        local obj_attrs = m3.KeyValueAttrsToObjAttr(data_objects[n].attrs)
        local stock = {
            storerId = obj_attrs.S_STORER or "",
            ownerId = obj_attrs.S_OWNER or "",
            skuId = obj_attrs.S_ITEM_CODE or "",
            skuStatus = obj_attrs.S_ITEM_STATE or "",
            qty = lua.Get_NumAttrValue(obj_attrs.F_QTY) or 0,
            locationId = obj_attrs.S_LOC_CODE or "",
            cid = obj_attrs.S_CNTR_CODE or "",
            boxCode = obj_attrs.S_CELL_NO or "",
            batchNo = obj_attrs.S_WMS_BN or "",
            produceCode = obj_attrs.S_BATCH_NO or "",
            productDate = obj_attrs.D_PRD_DATE or "",
            expiryDate = obj_attrs.D_EXP_DATE or "",
            productLine = obj_attrs.S_UDF01 or "",
            registerNo = obj_attrs.S_PROD_LINE or ""
        }
        table.insert(formatted_result, stock)
    end
 
    return 0, formatted_result
end
 
-- Main函数
function Stock_Query(strLuaDEID)
    m3.PrintLuaDEInfo(strLuaDEID)
    local FinalRes = Create_result()
    local xml_result = ""
 
    -- 使用pcall捕获所有可能的错误
    local status, err = pcall(function()
        -- 1. 获取xml数据包
        local nRet, soap_xml = mobox.getCurEditDataPacket(strLuaDEID)
        if nRet ~= 0 then
            FinalRes = Create_result("failure", "201", "无法获取数据包: " .. tostring(soap_xml))
            error(FinalRes.message)
        end
 
        -- 2. 解析xml
        local nRet, parsed_data = xml.parse(soap_xml)
        if nRet ~= 0 then
            FinalRes = Create_result("failure", "202", "xml格式非法")
            error(FinalRes.message)
        end
 
        -- 3. 使用深度遍历查找关键节点
        local inventory_input = deep_find_xml_node(parsed_data, {"Envelope", "Body", "InventoryReq", "Inventory_Input"})
        if not inventory_input then
            FinalRes = Create_result("failure", "206", "XML结构错误: 缺少Inventory_Input节点")
            error(FinalRes.message)
        end
 
        local input_params = deep_find_xml_node(inventory_input, {"InputParameters"})
        if not input_params then
            FinalRes = Create_result("failure", "207", "XML结构错误: 缺少InputParameters节点")
            error(FinalRes.message)
        end
 
        local inventory_tb = deep_find_xml_node(input_params, {"Inventory_TB"})
        if not inventory_tb then
            FinalRes = Create_result("failure", "208", "XML结构错误: 缺少Inventory_TB节点")
            error(FinalRes.message)
        end
 
        -- 4. 提取查询参数
        local query_params = {
            storerId = deep_find_xml_node(inventory_tb, {"storerId"}) or "",
            ownerId = deep_find_xml_node(inventory_tb, {"ownerId"}) or "",
            skuId = deep_find_xml_node(inventory_tb, {"skuId"}) or "",
            productLine = deep_find_xml_node(inventory_tb, {"productLine"}) or "",
            locationId = deep_find_xml_node(inventory_tb, {"locationId"}) or "",
            produceCode = deep_find_xml_node(inventory_tb, {"produceCode"}) or ""
        }
 
        -- 5. 参数校验
        if query_params.storerId == "" then
            FinalRes = Create_result("failure", "203", "storerId不能为空")
            error(FinalRes.message)
        end
 
        if query_params.skuId == "" then
            FinalRes = Create_result("failure", "204", "skuId不能为空")
            error(FinalRes.message)
        end
 
        -- 6. 查询库存明细
        local nRet, query_result = query_inventory_detail(strLuaDEID, query_params)
        if nRet ~= 0 then
            FinalRes = Create_result("failure", "205", "查询库存明细失败: " .. tostring(query_result))
            error(FinalRes.message)
        end
 
        -- 7. 处理查询结果
        if not query_result or #query_result == 0 then
            FinalRes = Create_result("success", "0", "未查询到符合条件的库存记录", {})
        else
            FinalRes = Create_result("success", "0", "查询成功!", query_result)
        end
    end)
 
    -- 处理错误情况
    if not status then
        if FinalRes.code == "0" then
            FinalRes = Create_result("failure", "299", "系统内部错误: " .. tostring(err))
        end
    end
 
    -- 生成XML结果
    xml_result = generate_raw_xml(FinalRes)
 
    -- 返回结果
    mobox.returnValue(strLuaDEID, 0, xml_result, 0)
 
    -- 记录错误日志
    if not status then
        lua.Stop(strLuaDEID, "处理过程中发生错误", FinalRes)
    end
end