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
--[[ 
    编码: GK-API-01
    名称: 货主信息同步接口
    作者: PMN
    日期: 2025-5-20
 
    入口函数:Storer_Sync
    功能说明:
        1. 接收货主信息同步请求
        2. 判断货主是否存在
        3. 执行新增或修改
        4. 返回处理结果
 
    输入XML示例:    
    <soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    </soap:Header>
    <soapenv:Body>
        <v1:InCompanyReq
            xmlns:v1="http://www.gkht.com/Information/INV/Ebs/Schemas/InCompany/V1.0">
            <v1:Company_Input>
                <v1:RESTHeader>
                    <v1:Responsibility/>
                    <v1:RespApplication/>
                    <v1:SecurityGroup/>
                    <v1:NLSLanguage>SIMPLIFIED CHINESE</v1:NLSLanguage>
                    <v1:Org_Id>0</v1:Org_Id>
                </v1:RESTHeader>
                <v1:InputParameters>
                    <v1:COMPANY_TB>
                        <!--1 or more repetitions:-->
                        <v1:COMPANY_TB_ITEM>
                            <v1:companyCode>CBJJT1</v1:companyCode>
                            <v1:companyName>北京京泰日晟科技有限公司</v1:companyName>
                            <v1:contactName></v1:contactName>
                            <v1:contactPhone></v1:contactPhone>
                            <v1:contactAddress></v1:contactAddress>
                            <v1:memo>个</v1:memo>
                        </v1:COMPANY_TB_ITEM>
                             <v1:COMPANY_TB_ITEM>
                            <v1:companyCode>CBJJXK</v1:companyCode>
                            <v1:companyName>北京吉鑫康商贸有限公司</v1:companyName>
                            <v1:contactName></v1:contactName>
                            <v1:contactPhone></v1:contactPhone>
                            <v1:contactAddress></v1:contactAddress>
                            <v1:memo>个</v1:memo>
                        </v1:COMPANY_TB_ITEM>
                    </v1:COMPANY_TB>
                </v1:InputParameters>
            </v1:Company_Input>
        </v1:InCompanyReq>
    </soapenv:Body>
</soapenv:Envelope>
--]]
 
wms_base = require("wms_base")
xml = require("oi_base_xml")
 
-- 检查货主是否存在
local function check_storer_exists(strLuaDEID, storer_item)
    local storerId = storer_item.storerId
 
    local strCondition = string.format("S_STORER = '%s' ", storerId)
    local nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, "STORER", strCondition)
    
    if nRet > 1 then
        return nRet, "检查货主是否存在时出错: " .. strRetInfo
    elseif nRet == 1 then
        return 1, "货主不存在"  -- 不存在
    else
        return 0, id  -- 存在
    end
end
 
-- 新增货主
local function create_storer(strLuaDEID, storer_data)
    local storer = m3.AllocObject(strLuaDEID, "STORER")
    local nRet1, CONST_WH = wms_base.Get_sConst2(strLuaDEID, "WMS_Default_Warehouse")
    local storerCode = storer_data.storerId
    -- 字段映射
    storer.storer = storerCode
    storer.storer_name = storerCode
    storer.company_code = storer_data.companyCode
    storer.company_name = storer_data.companyName
    storer.contact_name = storer_data.contactName or ""
    storer.contact_phone = storer_data.contactPhone or ""
    storer.contact_address = storer_data.contactAddress or ""
    storer.note = storer_data.memo or ""
    storer.default_wh_code = CONST_WH
    
    local nRet, result = m3.CreateDataObj(strLuaDEID, storer)
    return nRet, result
end
 
-- 更新货主信息
local function update_storer(strLuaDEID, storer_id, storer_data)
    local update_storer_obj = {
            {
                id = storer_id,
                attrs = {
                    { attr = "S_COMPANY_CODE", value = storer_data.companyCode },
                    { attr = "S_COMPANY_NAME", value = storer_data.companyName },
                    { attr = "S_STORER_NAME", value = storer_data.companyName },
                    { attr = "S_CONTACT_NAME", value = storer_data.contactName },
                    { attr = "S_CONTACT_PHONE", value = storer_data.contactPhone },
                    { attr = "S_CONTACT_ADDRESS", value = storer_data.contactAddress },
                    { attr = "S_NOTE", value = storer_data.memo },
                }
            }
        }
    
    local nRet, result = mobox.updateDataObj( strLuaDEID, "STORER", lua.table2str(update_storer_obj) )
    return nRet, result
end
 
-- 处理单个货主记录
local function process_storer_item(strLuaDEID, storer_item)
    
    -- 1. 检查货主是否存在
    local nRet, result = check_storer_exists(strLuaDEID, storer_item)
    if nRet > 1 then
        return nRet, result
    end
    
    -- 2. 存在则更新,不存在则新增
    if nRet == 0 then
        local storer_id = result
        -- 货主存在,更新信息
        nRet, result = update_storer(strLuaDEID, storer_id, storer_item)
        if nRet ~= 0 then
            return nRet, "更新货主信息失败: " .. tostring(result)
        end
        return 0, {action = "update", id = storer_id}
    else
        -- 货主不存在,新增
        nRet, result = create_storer(strLuaDEID, storer_item)
        if nRet ~= 0 then
            return nRet, "新增货主失败: " .. tostring(result)
        end
        return 0, {action = "create", id = result}
    end
end
 
function Storer_Sync(strLuaDEID)
    local nRet, strRetInfo, nErr
    local err_msg
    nErr = 0
    local isStop = 0
    local err = {}
    
    -- m3.PrintLuaDEInfo(strLuaDEID)
    
    -- 1. 获取接口输入数据
    local soap_xml
    nRet, soap_xml = mobox.getCurEditDataPacket(strLuaDEID)
    if nRet ~= 0 then 
        lua.Stop(strLuaDEID, "无法获取数据包: " .. soap_xml)
        isStop = 3
        return
    end
    
    -- 2. 解析XML
    local parsed_data
    nRet, parsed_data = xml.parse(soap_xml)
    if nRet ~= 0 then
        lua.Stop(strLuaDEID, "接口输入的XML格式非法!")
        isStop = 3
        return
    end
    
    -- 3. 提取货主数据
    local company_data = parsed_data.Envelope.Body.InCompanyReq.Company_Input
    local input_params = company_data.InputParameters
    local company_tb = input_params.COMPANY_TB
    
    -- 处理单条或多条货主数据
    local storer_items = {}
    if company_tb.COMPANY_TB_ITEM[1] ~= nil then
        storer_items = company_tb.COMPANY_TB_ITEM
    else
        storer_items = {company_tb.COMPANY_TB_ITEM}
    end
    
    -- 4. 处理每条货主记录
    for i, item in ipairs(storer_items) do
        local storer_data = {
            storerId = item.storerId,
            companyCode = item.companyCode,
            companyName = item.companyName,
            contactName = item.contactName or "",
            contactPhone = item.contactPhone or "",
            contactAddress = item.contactAddress or "",
            memo = item.memo or ""
        }
        
        -- 必填字段校验
        if not storer_data.companyCode or storer_data.companyCode == "" then
            err_msg = string.format("第%d条记录: companyCode不能为空", i)
            wms_base.Warning(strLuaDEID, 1, 601, err_msg, "从GK-WMS系统同步货主信息")
            nErr = nErr + 1
            err[nErr] = err_msg
            lua.Stop(strLuaDEID, err_msg)
            isStop = 5
        end
        
        if not storer_data.companyName or storer_data.companyName == "" then
            err_msg = string.format("第%d条记录: companyName不能为空", i)
            wms_base.Warning(strLuaDEID, 1, 601, err_msg, "从GK-WMS系统同步货主信息")
            nErr = nErr + 1
            err[nErr] = err_msg
            lua.Stop(strLuaDEID, err_msg)
            isStop = 5
        end
 
        if not storer_data.storerId or storer_data.storerId == "" then
            err_msg = string.format("第%d条记录: storerId不能为空", i)
            wms_base.Warning(strLuaDEID, 1, 601, err_msg, "从GK-WMS系统同步货主信息")
            nErr = nErr + 1
            err[nErr] = err_msg
            lua.Stop(strLuaDEID, err_msg)
            isStop = 5
        end
        
        -- 处理货主记录
        nRet, result = process_storer_item(strLuaDEID, storer_data)
        if nRet ~= 0 then
            err_msg = result
            wms_base.Warning(strLuaDEID, 1, 601, err_msg, "从GK-WMS系统同步货主信息")
            nErr = nErr + 1
            err[nErr] = err_msg
            lua.Stop(strLuaDEID, err_msg)
            isStop = 5
        end
    end
    
    -- 5. 返回响应
    local response = {
        flag = nErr > 0 and "failure" or "success",
        code = nErr,
        message = nErr > 0 and table.concat(err, "; ") or "处理成功",
    }
    
    local xml_result = xml.json_to_xml(response, "response")
    mobox.returnValue(strLuaDEID, 0, xml_result, isStop)
end