--[[
|
编码: 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
|