我們在巨星工藝系統里面遇到,用戶需要將產品工藝一次性導入的需求
軟件系統架構圖:
數據對象介紹
產品及工藝導入,我們提供了一個excel模板
定義導入腳本(自定義腳本)
–[[
??? 編碼: CappImport ??? 名稱: 產品及工藝信息導入 ??? 作者:whx??? ??? 入口函數:ImportCapp
??? 功能說明: ??????? 產品及工藝的導入,在導入的時候判斷一下產品是否存在?如果不存在要新創建 ??????? 導入格式見《產品-工藝-導入test.xlsx》 ??? 變更歷史: ??????? V1.0? whx??? 2023-4-12????? 新建
??? 數據結構說明 ???????? 產品 ?????????????????? 工藝1 ?????????????????? 工藝2 ?????????????????? 工藝… ???????? 其中,有數據對象《產品》 子類 《工藝》 (S_GDS_CODE 為關聯字段) ???????? 使用前,將2個對象的所有字段 對應的變量都定義一下? 《參考代碼里面的? 產品字段對應變量定義;工藝字段對應變量定義》 –]]
json? = require(“json”) mobox = require(“OILua_JavelinExt”) require(“oi_basestrfunc”)
function ImportCapp(strLuaDEID) ??? local nRet, strRetInfo ? ??— 獲取導入的數據, 返回 [{“attr”:”xx”,”value”:””},…] ??? ??? nRet, strRetInfo = mobox.getInputParameter(strLuaDEID) ??? –mobox.writeSysLog(“nRet”,nRet)
??? if (nRet ~= 0 or strRetInfo == ”) then ??????? mobox.error(strLuaDEID, “無法獲取導入數據!”) ??????? return ??? end ??? –mobox.writeSysLog(“strRetInfo”,strRetInfo) ??? local retJson = json.decode(strRetInfo) ??? ??? local input = retJson[“parameter”] ??? ??? local n, nCount, nValue ??? local strAddAttr = ”
??? — 產品字段對應變量定義 ??? local strGDSCode = ”
??? local strGDSName = ” ??? local strGDSClass = ” ??? local strGDSSeries = ” ??? local strGDSSpec = ” ??? local strGDSDescribe = ”
??? — 工藝字段對應變量定義 ??? local strMPName = ” ??? local strMPCode = ” ??? local strMPDescrive = ” ??? local strN_On = ”
??? — 步驟1 獲取從excel導入的一行數據,根據excel的產品規格定義進行屬性組合 strAddAttr ??? nCount = #input ??? for n = 1, nCount do ??????? strAttr = input[n].attr ??????? strValue = input[n].value ??????? if (strAttr ~= ” and strValue ~= ”) then
??????????? — 根據導入的excel產品規格頭名稱進行判斷 ??????????? — 關鍵屬性判斷 ??????????? if (strAttr == “產品編碼”) then ??????????????? if (strValue == ”) then ??????????????????? mobox.error(strLuaDEID, strAttr .. “不能為空!”) ??????????????????? return ??????????????? end ??????????????? strGDSCode = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_GDS_CODE”,”value”:”‘ .. strGDSCode .. ‘”},’
??????????? elseif (strAttr == “產品名稱”) then ??????????????? if (strValue == ”) then ??????????????????? mobox.error(strLuaDEID, strAttr .. “不能為空!”) ??????? ????????????return ??????????????? end ??????????????? strGDSName = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_GDS_NAME”,”value”:”‘ .. strGDSName .. ‘”},’
??????????? elseif (strAttr == “過程名稱”) then ??????????????? if (strValue == ”) then ??????????????????? mobox.error(strLuaDEID, strAttr .. “不能為空!”) ??????????????????? return ??????????????? end ??????????????? strMPName = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_MP_NAME”,”value”:”‘ .. strMPName .. ‘”},’ ??????????????? ??????????? elseif (strAttr == “過程編號”) then ??????????????? if (strValue == ”) then ??????????????????? mobox.error(strLuaDEID, strAttr .. “不能為空!”) ??????????????????? return ??????????????? end ??????????????? strMPCode = strValue ????? ??????????strAddAttr = strAddAttr .. ‘{“attr”:”S_MP_CODE”,”value”:”‘ .. strMPCode .. ‘”},’ ??????????????? ??????????? — 常規屬性
??????????? elseif (strAttr == “產品分類”) then ??????????????? strGDSClass = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_GDS_CLASS”,”value”:”‘ .. strGDSClass .. ‘”},’
??????????? elseif (strAttr == “產品系列”) then ??????????????? strGDSSeries = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_GDS_SERIES”,”value”:”‘ .. strGDSSeries .. ‘”},’
????????? ??elseif (strAttr == “產品規格”) then ??????????????? strGDSSpec = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_GDS_SPEC”,”value”:”‘ .. strGDSSpec .. ‘”},’
??????????? elseif (strAttr == “產品描述”) then ??????????????? strGDSDescribe = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_GDS_DESCRIBE”,”value”:”‘ .. strGDSDescribe .. ‘”},’
??????????? elseif (strAttr == “序號”) then ??????????????? strN_On = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”N_ON”,”value”:”‘ .. strN_On .. ‘”},’
??????????? elseif (strAttr == “過程描述”) then ??????????????? strMPDescrive = strValue ??????????????? strAddAttr = strAddAttr .. ‘{“attr”:”S_MP_DESCRIBE”,”value”:”‘ .. strMPDescrive .. ‘”},’ ??????????? end ??????? end
??? end ??? –去除最后一個, ??? local strAddAttr1 = trim_laster_char(strAddAttr)
??? — 步驟2 根據(產品編碼 + 過程名稱) 來判斷導入的工藝步驟是否已經存在 ??? —?????? 如果已經存在,根據導入的數據進行覆蓋 ??? —?????? 如果不存在需要創建 ??? local attrs ??? local strCondition = “S_GDS_CODE='” .. strGDSCode .. “‘ and S_MP_NAME = ‘” .. strMPName .. “‘ “ ??? nRet, strRetInfo = mobox.existThisData(strLuaDEID, “工藝”, strCondition) ??? if (nRet ~= 0) then ??????? mobox.error(strLuaDEID, “在檢查工藝是否存在時失敗! ” .. strRetInfo) ??? ????return ??? end
??? if (strRetInfo == ‘yes’) then ??????? — 已經存在,根據導入的數據進行覆蓋 ??????? strCondition = “S_GDS_CODE='” .. strGDSCode .. “‘ and S_MP_NAME = ‘” .. strMPName .. “‘ “
??????? strSetSQL = “S_GDS_CODE = ‘” ..strGDSCode ..”‘ , S_MP_DESCRIBE ='” ..strMPDescrive .. ??????????? “‘ , S_MP_NAME = ‘” .. strMPName .. “‘ , S_MP_CODE = ‘” .. strMPCode .. “‘ , N_ON = ‘” .. strN_On ..”‘ “
??????? nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, “工藝”, strCondition, strSetSQL) ??????? if (nRet ~= 0) then ??????????? mobox.error(strLuaDEID, strRetInfo) ??????????? return ??????? end ??????? return
???? elseif (strRetInfo == ‘no’) then ???? — 新增工藝 ???????? attrs = ‘[{“attr”:”S_GDS_CODE”,”value”:”‘ .. strGDSCode .. ‘”},’ ???????? .. ‘{“attr”:”S_MP_NAME”,”value”:”‘ .. strMPName .. ‘”},’ ???????? .. ‘{“attr”:”N_ON”,”value”:”‘ .. strN_On .. ‘”},’ ???????? .. ‘{“attr”:”S_MP_CODE”,”value”:”‘ .. strMPCode .. ‘”},’ ???????? .. ‘{“attr”:”S_MP_DESCRIBE”,”value”:”‘ ..? strMPDescrive .. ‘”}]’
??? — mobox.writeSysLog(“attrs”,attrs) ??? ???????? nRet, strRetInfo = mobox.createDataObj(strLuaDEID, “工藝”, attrs) ???????? if (nRet ~= 0) then ?????????????????? mobox.error(strLuaDEID, “創建工藝失敗! ” .. strRetInfo) ?????????????????? return ???????? end
???????? ?— 導入的工藝不存在的時候,就需要判斷一下 產品是否存在 ???????? — 如果不存在需要創建 ???????? — 判斷產品是否存在 ???????? strCondition = “S_GDS_CODE='” .. strGDSCode .. “‘” ???????? nRet, strRetInfo = mobox.existThisData(strLuaDEID, “產品”, strCondition) ???????? if (nRet ~= 0) then ?????????????????? mobox.error(strLuaDEID, “在檢查產品是否存在時失敗! ” .. strRetInfo) ?????????????????? return ???????? end
???????? if (strRetInfo == ‘no’) then ???????? — 新增產品
?????????????????? attrs = ‘[{“attr”:”S_GDS_CODE”,”value”:”‘ .. strGDSCode .. ‘”},’ ?????????????????? .. ‘{“attr”:”S_GDS_NAME”,”value”:”‘ .. strGDSName .. ‘”},’ ?????????????????? .. ‘{“attr”:”S_GDS_CLASS”,”value”:”‘ .. strGDSClass .. ‘”},’ ?????????????????? .. ‘{“attr”:”S_GDS_SERIES”,”value”:”‘ .. strGDSSeries .. ‘”},’ ?????????????????? .. ‘{“attr”:”S_GDS_SPEC”,”value”:”‘ .. strGDSSpec .. ‘”},’ ?????????????????? .. ‘{“attr”:”S_GDS_DESCRIBE”,”value”:”‘ .. strGDSDescribe .. ‘”}]’
?????????????????? –mobox.writeSysLog(“attrs”,attrs) ?????????????????? nRet, strRetInfo = mobox.createDataObj(strLuaDEID, “產品”, attrs) ?????????????????? if (nRet ~= 0) then ??????????????????????????? mobox.error(strLuaDEID, “創建產品失敗! ” .. strRetInfo) ??????????????????????????? return ?????????????????? end ?? ? end
??? end
end |
定義導入命令
設置導入命令的相關參數
我們可以通過excel批量導入企業的工藝信息