--- --- Created by wsz. --- DateTime: 2025/5/13 上午9:02 --- --[[ 编码:GK-API-03 名称: 商品批次同步接口 作者: 入口函数:Batch_Sync 功能说明:创建、覆盖GK_BATCH实体 同时更新CG_Detail相关自定义字段 变更历史: v0.0.2 格式验证不通过的返回报文 系统异常需要error抛出 resp v0.0.3 CG_Detail弃用,变更为 库存表 INV_Detail --]] local json = require("json") local mobox = require("OILua_JavelinExt") local m3 = require("oi_base_mobox") local xml = require("oi_base_xml") --- 实体标识-商品批次同步 TN_GK_BATCH local CLSID_GKbatch = "GK_BATCH" --- 实体标识-货位容器货品明细 TN_CG_Detail | 变更为 库存表 INV_Detail local CLSID_INVdetail = "INV_Detail" -- local apiDesc = "商品批次同步接口" --根标签 local RootTag = "response" --[[ desc:处理xml->table 部分空节点被转化为{} parameter: value-字符串或空table{} return: nil/字符串 -- 备 集中处理 --]] local function getStrHandle(value) --if (type(value) == "table" and #value == 0) then -- return nil --else -- return lua.Get_StrAttrValue(value) --end return lua.Get_StrAttrValue(value) end -- mergeTablesDeep辅助-递归 local function deepCopy(original) if type(original) ~= 'table' then return original end local copy = {} for key, value in pairs(original) do copy[key] = deepCopy(value) end return copy end -- 将tableb合并到tablea local function mergeTablesDeep(tablea, tableb) local bCopy = deepCopy(tableb) for key, value in pairs(bCopy) do tablea[key] = value end return tablea end --[[ -- 事务回滚-返回信息后终止执行 ]] local function result_transaction_back(strLuaDEID, msg) -- 回滚当次处理 lua.Stop(strLuaDEID, msg) local result = {} result.flag = "failure" -- success|failure result.code = 5 result.message = msg do local nRet = mobox.returnValue(strLuaDEID, 1, xml.json_to_xml(result, RootTag), result.code) if nRet ~= 0 then lua.Error(strLuaDEID, debug.getinfo(1), 'result_transaction_back-执行mobox.returnValue失败 ' .. nRet) end end lua.Error(strLuaDEID, debug.getinfo(1), msg) end --[[ -- 执行成功 ]] local function result_success(strLuaDEID) local result = {} result.flag = "success" -- success|failure result.code = 0 result.message = "" do local nRet = mobox.returnValue(strLuaDEID, 1, xml.json_to_xml(result, RootTag), result.code) if nRet ~= 0 then lua.Error(strLuaDEID, debug.getinfo(1), 'result_success-执行mobox.returnValue失败 ' .. nRet) end end end --[[ 组装临时对象 ]] local function buildTemp(dataSet) local temp = {} -- 放置待创建数据 -- 物料编码/商品代码 temp.storer_no = getStrHandle(dataSet.storerId)-- getStrHandle(dataSet.skuId) -- 货主编码 temp.item_code = getStrHandle(dataSet.skuId) --getStrHandle(dataSet.storerId) -- 物权 temp.owner_no = getStrHandle(dataSet.ownerId) -- 批次编码 temp.wms_bn = getStrHandle(dataSet.batchNo) -- 批号 temp.batch_no = getStrHandle(dataSet.produceCode) -- 生产日期 temp.product_date = getStrHandle(dataSet.productDate) -- 有效日期 temp.expiry_date = getStrHandle(dataSet.expiryDate) -- 注册证号 temp.register_no = getStrHandle(dataSet.registerNo) -- 生产企业 temp.company_name = getStrHandle(dataSet.companyName) -- 证件生产厂家 temp.cert_company_name = getStrHandle(dataSet.certCompanyName) -- 委托生产厂家 temp.consign_company_name = getStrHandle(dataSet.consignCompanyWTName) -- 灭菌日期 temp.sterilization_date = getStrHandle(dataSet.sterilizationData) -- 备注 temp.note = getStrHandle(dataSet.memo) -- 备注 temp.ex1 = getStrHandle(dataSet.ex1) -- 备注 temp.ex2 = getStrHandle(dataSet.ex2) -- 备注 temp.ex3 = getStrHandle(dataSet.ex3) return temp end --[[ strClsID: 数据类id dataObj:待创建的数据对象 --]] local function create_obj(strLuaDEID, strClsID, dataObj) -- 基础验证 if (nil == strClsID or strClsID == '') then result_transaction_back(strLuaDEID, "[商品批次同步接口]-创建对象失败,strClsID不可为空") end if type(dataObj) ~= 'table' then result_transaction_back(strLuaDEID, "[商品批次同步接口]-创建对象失败,dataObj必须为table类型") end -- local creatobj = m3.AllocObject(strLuaDEID, strClsID) mergeTablesDeep(creatobj, dataObj) local nRet, resultData = m3.CreateDataObj(strLuaDEID, creatobj) if (nRet ~= 0) then result_transaction_back(strLuaDEID, string.format("创建对象[%s]失败,msg:%s", strClsID, resultData)) end end --[[ desc:更新目标数据 ]] local function updateBussData(strLuaDEID, dataSet, id) local temp = buildTemp(dataSet) local attrValueObj do -- 取得 可用来更新数据库的 attr-value 的 table 类型 [attrValueObj] -- luaJsonToObjAttrs 执行后返回的str_data_attrset 是字符串,且attr 是数据库字符串 需反序列化处理为table local nRet, str_data_attrset = mobox.luaJsonToObjAttrs(CLSID_GKbatch, json.encode(temp)) if nRet ~= 0 then result_transaction_back(strLuaDEID, 'upd-goodsbatsh item 时luaJsonToObjAttrs执行失败,详细:' .. str_data_attrset) end -- 反序列化为目标格式 attrValueObj = json.decode(str_data_attrset) end -- 当程序可以运行以下时 - 认为 if nRet == 0 --- 执行upd逻辑 lua.Debug(strLuaDEID, debug.getinfo(1), "attrValueObj", attrValueObj) -- 组装批量更新的数据格式 local updateObj = {} local updateObj_item = {} updateObj_item.id = id updateObj_item.attrs = attrValueObj table.insert(updateObj, updateObj_item) --local updateObj = { -- { -- id = id, -- attrs = attrValueObj -- } --} local updateStrDataJson = lua.table2str(updateObj) lua.Debug(strLuaDEID, debug.getinfo(1), "goodsbatch-update准备覆盖已有数据", updateStrDataJson) local nRet, strRetInfo = mobox.updateDataObj(strLuaDEID, CLSID_GKbatch, updateStrDataJson) if (nRet ~= 0) then result_transaction_back(strLuaDEID, string.format("goodbatch更新操作失败。code:%s,msg:%s", nRet, strRetInfo)) end end --[[ desc:创建目标数据 ]] local function creatBussData(strLuaDEID, dataSet) local temp = buildTemp(dataSet) lua.Debug(strLuaDEID, debug.getinfo(1), "goodsbatch待新增数据-temp", temp) create_obj(strLuaDEID, CLSID_GKbatch, temp) end --[[ 单条 ]] local function bussHandle(strLuaDEID, dataSet) -- 对于 非数组表 返回0 认定仅单条??? lua.Debug(strLuaDEID, debug.getinfo(1), "dataSet", dataSet) -- 组装sql过滤条件 local strCondition do local filters = {} table.insert(filters, string.format([[ %s = '%s' ]], "S_STORER", getStrHandle(dataSet.storerId)))--getStrHandle(dataSet.skuId))) table.insert(filters, string.format([[ %s = '%s' ]], "S_ITEM_CODE", getStrHandle(dataSet.skuId)))--getStrHandle(dataSet.storerId))) table.insert(filters, string.format([[ %s = '%s' ]], "S_OWNER", getStrHandle(dataSet.ownerId))) table.insert(filters, string.format([[ %s = '%s' ]], "S_WMS_BN", getStrHandle(dataSet.batchNo))) strCondition = table.concat(filters, " and ") lua.Debug(strLuaDEID, debug.getinfo(1), "filters-组装的goodsbatch的where条件", strCondition) end local nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr(strLuaDEID, CLSID_GKbatch, strCondition) lua.Debug(strLuaDEID, debug.getinfo(1), "getDataObjAttrByKeyAttr", strRetInfo) if nRet == 0 then -- key已存在,更新原数据 lua.Debug(strLuaDEID, debug.getinfo(1), "add", "数据更新:" .. strRetInfo) updateBussData(strLuaDEID, dataSet, id) elseif nRet == 1 then -- key不存在,新增 lua.Debug(strLuaDEID, debug.getinfo(1), "upd", "数据新增") creatBussData(strLuaDEID, dataSet) else result_transaction_back(strLuaDEID, string.format("查询目标数据失败,code:%s,msg:%s", nRet, strRetInfo)) end end --[[ 组装数据key skuId+storerId+ownerId+batchNo唯一(必填) ]] local function getId(dataSet) return getStrHandle(dataSet.skuId) .. getStrHandle(dataSet.storerId) .. getStrHandle(dataSet.ownerId) .. getStrHandle(dataSet.batchNo) end --[[ 同时更新 CGDetail 字段 策略:查找 skuId+storerId+ownerId+batchNo 数据,然后的 ]] local function updateCG_Detail(strLuaDEID, dataSet) lua.Debug(strLuaDEID, debug.getinfo(1), "开始更新updateCG_Detail", '') -- 1. 组装查询条件 type:str local strCondition do local filters = {} table.insert(filters, string.format([[ %s = '%s' ]], "S_STORER", getStrHandle(dataSet.storerId))) table.insert(filters, string.format([[ %s = '%s' ]], "S_OWNER", getStrHandle(dataSet.ownerId))) table.insert(filters, string.format([[ %s = '%s' ]], "S_ITEM_CODE", getStrHandle(dataSet.skuId))) table.insert(filters, string.format([[ %s = '%s' ]], "S_WMS_BN", getStrHandle(dataSet.batchNo))) strCondition = table.concat(filters, " and ") lua.Debug(strLuaDEID, debug.getinfo(1), "filters-组装的cg-detail的where条件", strCondition) end -- 2. 组装待更新字段 type:str local strUpdateSql do local updater = {} table.insert(updater, string.format([[ %s = '%s' ]], "S_BATCH_NO", getStrHandle(dataSet.produceCode))) -- 批号 | 批号 table.insert(updater, string.format([[ %s = '%s' ]], "D_PRD_DATE", getStrHandle(dataSet.productDate))) -- 生产日期 | 生产日期 table.insert(updater, string.format([[ %s = '%s' ]], "D_EXP_DATE", getStrHandle(dataSet.expiryDate))) -- 有效日期 | 有效日期 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF01", getStrHandle(dataSet.registerNo))) -- 注册证号 | 自定义字段1 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF02", getStrHandle(dataSet.companyName))) -- 生产企业 | 自定义字段2 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF03", getStrHandle(dataSet.certCompanyName))) -- 证件生产厂家 | 自定义字段3 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF04", getStrHandle(dataSet.consignCompanyWTName))) -- 委托生产厂家 | 自定义字段4 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF05", getStrHandle(dataSet.sterilizationDate))) -- 灭菌日期 | 自定义字段5 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF06", getStrHandle(dataSet.ex1))) -- 扩展字段1 | 自定义字段6 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF07", getStrHandle(dataSet.ex2))) -- 扩展字段2 | 自定义字段7 table.insert(updater, string.format([[ %s = '%s' ]], "S_UDF08", getStrHandle(dataSet.ex3))) -- 扩展字段3 | 自定义字段8 strUpdateSql = table.concat(updater, " , ") lua.Debug(strLuaDEID, debug.getinfo(1), "filters-组装的cg-detail的set字段", strUpdateSql) end -- 3. 更新执行 do local nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, CLSID_INVdetail, strCondition, strUpdateSql) if (nRet ~= 0) then result_transaction_back(strLuaDEID, CLSID_INVdetail .. "更新信息失败!" .. strRetInfo) end end end function Batch_Sync(strLuaDEID) -- 1.1 getxml local soap_xml do local nRet, data = mobox.getCurEditDataPacket(strLuaDEID) if (nRet ~= 0) then result_transaction_back(strLuaDEID, "无法获取数据包 datajson !" .. data) end soap_xml = data lua.Debug(strLuaDEID, debug.getinfo(1), "GK-API-03xml报文", soap_xml) end -- 1.2 xml->luaobj local parsed_data do local nRet, data = xml.parse(soap_xml) if (nRet ~= 0) then result_transaction_back(strLuaDEID, "接口输入的XML格式非法!") end parsed_data = data end -- 1.3 取得商品批次表item的tableObj local dataSet = parsed_data["Envelope"]["Body"] ["InBatchGoodsReq"] ["BatchGoods_Input"] ["InputParameters"] ["BATCHGOODS_TB"] ["BATCHGOODS_TB_ITEM"] if nil == dataSet then -- wms_base.Warning( strLuaDEID, 1, 601, err_msg, "从GK-WMS系统同步SKU信息" ) --wms_base.Warning(strLuaDEID, 2, 201, "[商品批次同步接口]-未解析到soap商品批次节点!", json.encode(dataSet), "", "[商品批次同步接口]-dataSet") result_transaction_back(strLuaDEID, "[商品批次同步接口]-未解析到soap商品批次节点!") elseif #dataSet == 0 then -- 仅单条数据 lua.Debug(strLuaDEID, debug.getinfo(1), "单条", "") bussHandle(strLuaDEID, dataSet) updateCG_Detail(strLuaDEID, dataSet) else for i = 1, #dataSet do lua.Debug(strLuaDEID, debug.getinfo(1), "多条", "") bussHandle(strLuaDEID, dataSet[i]) updateCG_Detail(strLuaDEID, dataSet) end end result_success(strLuaDEID) end