--[[ 版本: Version 2.1 创建日期: 2023-6-16 创建人: HAN WMS-Basis-Model-Version: V15.5 功能: wms_operation Lua程序包整合了一些【作业】对象相关的操作 -- GetInfo 根据作业编号获取作业属性 -- SetEndLoc 更新【作业】对象的终点货位和库区、仓库信息 -- SetStartLoc 更新【作业】对象的起点货位和库区、仓库信息 -- Update 更新【作业】的起点和终点货位和库区、仓库等信息 -- SetCancel 设置【作业】状态=取消 -- SetFinish 设置【作业】对象的状态=完成 -- Reset 设置【作业】对象的状态=执行,前提是原来的状态=错误 -- SetTaskState 设置作业中的任务的状态,0 -- 表示任务没下发, 1 -- 表示有任务已经推送给设备 2 -- 任务已经执行 3 -- 已经执行完成 -- Create 点到点创建作业 -- CanCancel 判断作业是否可以取消 更改说明: --]] wms_base = require ("wms_base") local wms_op = {_version = "0.1.1"} --/////////////////////////////////////////////////////////作业相关//////////////////////////////////////////////////////////// --[[ 根据作业编号获取作业属性 返回2个值: nRet 0 成功获取信息 1 不存在 2 错误 object 作业对象 --]] function wms_op.GetInfo( strLuaDEID, op_code ) if ( op_code == nil or op_code == '' ) then return 2, "调用 wms_op.GetInfo 函数时参数不正确,作业编码不能为空!" end local nRet, strRetInfo, id, attrs local strCondition = "S_CODE = '"..op_code.."'" nRet, id, attrs = mobox.getDataObjAttrByKeyAttr( strLuaDEID, "Operation", strCondition ) if ( nRet == 1 ) then return 1, "作业编码='"..op_code.."'的作业不存在!" end if ( nRet ~= 0 ) then return 2, "getDataObjAttrByKeyAttr 发生错误!"..id end nRet, strRetInfo = mobox.objAttrsToLuaJson( "Operation", attrs ) if ( nRet ~= 0 ) then return 2, "objAttrsToLuaJson Operation 失败!"..strRetInfo end local object, success success, object = pcall( json.decode, strRetInfo ) if ( success == false ) then return 1, "objAttrsToLuaJson('Operation') 返回的的JSON格式不合法!" end object.id = id return 0, object end -- 更新【作业】的终点货位和库区、仓库等信息 --[[ 参数: strOpCode -- 作业编码 strLocCode -- 货位编码 strAreaCode -- 库区编码 strWHCode -- 仓库编码 strCntrCode -- 容器编码/可以不输入 --]] function wms_op.SetEndLoc( strLuaDEID, strOpCode, strLocCode, strAreaCode, strWHCode, strCntrCode ) local nRet, strRetInfo -- 输入参数检查 if ( strOpCode == nil or strOpCode == '' ) then return 1, "调用 wms_op.SetEndLoc 函数时参数不正确, 作业编码不能为空!" end if ( strWHCode == nil or strWHCode == '' ) then return 1, "调用 wms_op.SetEndLoc 函数时参数不正确, 仓库编码不能为空!" end if ( strAreaCode == nil or strAreaCode == '' ) then return 1, "调用 wms_op.SetEndLoc 函数时参数不正确, 库区编码不能为空!" end if ( strLocCode == nil or strLocCode == '' ) then return 1, "调用 wms_op.SetEndLoc 函数时参数不正确, 货位编码不能为空!" end local strCondition strCondition = "S_CODE = '"..strOpCode.."'" strSetAttr = "S_END_WH='"..strWHCode.."', S_END_AREA='"..strAreaCode.."', S_END_LOC='"..strLocCode.."'" if (strCntrCode ~= nil) then strSetAttr = strSetAttr..", S_CNTR_CODE='"..strCntrCode.."'" end nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Operation", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end return 0,"" end -- 更新【作业】的起点货位和库区、仓库等信息 --[[ 参数: strOpCode -- 作业编码 strLocCode -- 货位编码 strAreaCode -- 库区编码 strWHCode -- 仓库编码 strCntrCode -- 容器编码/可以不输入 --]] function wms_op.SetStartLoc( strLuaDEID, strOpCode, strLocCode, strAreaCode, strWHCode, strCntrCode ) local nRet, strRetInfo -- 输入参数检查 if ( strOpCode == nil or strOpCode == '' ) then return 1, "调用 wms_op.SetStartLoc 函数时参数不正确, 作业ID不能为空!" end local strCondition strCondition = "S_CODE = '"..strOpCode.."'" strSetAttr = "S_START_WH='"..strWHCode.."', S_START_AREA='"..strAreaCode.."', S_START_LOC='"..strLocCode.."'" if (strCntrCode ~= nil) then strSetAttr = strSetAttr..", S_CNTR_CODE='"..strCntrCode.."'" end nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Operation", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end return 0,"" end -- 更新【作业】的起点和终点货位和库区、仓库等信息 --[[ 参数: strOpCode -- 作业编码 strLocCode -- 货位编码 strAreaCode -- 库区编码 strWHCode -- 仓库编码 strCntrCode -- 容器编码/可以不输入 --]] function wms_op.Update( strLuaDEID, operation ) local nRet, strRetInfo -- 输入参数检查 if ( operation == nil ) then return 1, "调用 wms_op.Update 函数时参数不正确, operation 必须有值!" end local strCondition strCondition = "S_CODE = '"..operation.code.."'" strSetAttr = "S_START_WH='"..operation.start_wh_code.."', S_START_AREA='"..operation.start_area_code.."', S_START_LOC='"..operation.start_loc_code.."'," strSetAttr = strSetAttr.."S_CNTR_CODE='"..operation.cntr_code.."',S_END_WH='"..operation.end_wh_code.."', S_END_AREA='"..operation.end_area_code.."', S_END_LOC='"..operation.end_loc_code.."'" nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Operation", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end return 0,"" end -- 设置【作业】状态=完成 --[[ 参数: operation -- 作业编码/ 或者 operation table --]] function wms_op.SetFinish( strLuaDEID, operation ) local nRet, strRetInfo local strOpCode = '' local strCNTRCode = '' local start_time = '' local carry_cb_no = '' local carry_cb_cls = '' if ( type(operation) == "string" ) then strOpCode = operation else strOpCode = operation.code strCNTRCode = operation.cntr_code -- 作业携带容器业务类型 carry_cb_cls = operation.carry_cb_cls carry_cb_no = operation.carry_cb_no if ( operation.run_time ~= nil ) then -- 说明作业有新增的 F_RUN_TIME 属性 start_time = operation.start_time end end -- 输入参数检查 if ( strOpCode == nil or strOpCode == '' ) then return 1, "调用 wms_op.SetFinish 函数时参数不正确, 作业编码不能为空!" end --local bs_state = wms_base.Get_nConst(strLuaDEID,"作业状态-完成") local bs_state = 2 local strCondition local str_b_state = wms_base.GetDictItemName( strLuaDEID, "WMS_OperationState", bs_state ) local curTime = os.date("%Y-%m-%d %H:%M:%S") strCondition = "S_CODE = '"..strOpCode.."'" strSetAttr = "N_B_STATE="..bs_state..", S_B_STATE='"..str_b_state.."', T_END_TIME='"..curTime.."'" if ( start_time ~= '' ) then local st = lua.toTimestamp( start_time ) local run_time = os.difftime( os.time(), st ) strSetAttr = strSetAttr..", F_RUN_TIME = "..run_time end nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Operation", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end -- 解锁由该作业造成的货位锁,逻辑库区锁都解除 nRet, strRetInfo = wms.wms_UnlockByOperation( strLuaDEID, strOpCode ) if ( nRet ~= 0 ) then return 1, "wms_UnlockByOperation 失败! "..strRetInfo end -- 容器解锁 if ( strCNTRCode ~= '' ) then -- 容器解锁 nRet, strRetInfo = wms.wms_UnlockCntr( strLuaDEID, strCNTRCode ) if ( nRet ~= 0 ) then return 1, "wms_UnlockCntr 失败!"..strRetInfo end end if ( carry_cb_cls == "Pre_Alloc_Container" ) then -- 如果作业携带的容器是【预分配容器】,需要把预分配容器的状态也设置为{完成} if ( carry_cb_no ~= '' ) then -- 配盘容器的状态 = 0,1 strCondition = "S_PAC_NO = '"..carry_cb_no.."' AND (N_B_STATE <= 1 )" strSetAttr = "N_B_STATE = 5" -- 完成 nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Pre_Alloc_Container", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end end elseif ( carry_cb_cls == "Distribution_CNTR" ) then -- 如果作业携带的容器是【预分配容器】,需要把预分配容器的状态也设置为完成 if ( carry_cb_no ~= '' ) then -- 配盘容器的状态 = 0,1,2 7 strCondition = "S_DC_NO = '"..carry_cb_no.."' AND (N_B_STATE <= 2 OR N_B_STATE = 7)" strSetAttr = "N_B_STATE = 6" nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Distribution_CNTR", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end end end return 0,"" end -- 设置【作业】状态=取消 --[[ 参数: operation -- 作业编码/ 或者 operation table --]] function wms_op.SetCancel( strLuaDEID, operation ) local nRet, strRetInfo local strOpCode = '' local strCNTRCode = '' if ( type(operation) == "string" ) then strOpCode = operation else strOpCode = operation.code strCNTRCode = operation.cntr_code end -- 输入参数检查 if ( strOpCode == nil or strOpCode == '' ) then return 1, "调用 wms_op.SetFinish 函数时参数不正确, 作业编码不能为空!" end local bs_state = 7 -- 取消 local strCondition local str_b_state = wms_base.GetDictItemName( strLuaDEID, "WMS_OperationState", bs_state ) local curTime = os.date("%Y-%m-%d %H:%M:%S") strCondition = "S_CODE = '"..strOpCode.."'" strSetAttr = "N_B_STATE="..bs_state..", S_B_STATE='"..str_b_state.."', T_END_TIME='"..curTime.."'" nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Operation", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end -- 解锁由该作业造成的货位锁,逻辑库区锁都解除 nRet, strRetInfo = wms.wms_UnlockByOperation( strLuaDEID, strOpCode ) if ( nRet ~= 0 ) then return 1, "wms_UnlockByOperation 失败! "..strRetInfo end -- 容器解锁 -- lua.Debug( strLuaDEID, debug.getinfo(1), "wms_op.SetFinish --> ", strCNTRCode ) if ( strCNTRCode ~= '' ) then -- 容器解锁 nRet, strRetInfo = wms.wms_UnlockCntr( strLuaDEID, strCNTRCode ) if ( nRet ~= 0 ) then return 1, "wms_UnlockCntr 失败!"..strRetInfo end end return 0,"" end -- 设置【作业】状态=执行,错误码清空 --[[ 参数: obj -- 作业对象 --]] function wms_op.Reset( strLuaDEID, obj ) local nRet, strRetInfo assert( type(obj) == "table", "wms_op.Reset 的输入参数 obj 必须是一个 table 类型" ) assert( obj.cls == "Operation", "wms_op.Reset 的输入参数 obj 必须是'Operation'类型的数据对象" ) local bs_state = obj.laste_bs_state local strCondition local str_b_state = wms_base.GetDictItemName( strLuaDEID, "WMS_OperationState", bs_state ) strCondition = "S_CODE = '"..obj.code.."' AND N_B_STATE >= 3" strSetAttr = "N_FAIL_COUNT = 0, N_B_STATE="..bs_state..", S_B_STATE='"..str_b_state.."', S_ERR=''" nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Operation", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end return 0,"" end -- 设置作业状态 function wms_op.SetTaskState( strLuaDEID, op_code, task_state ) local nRet, strRetInfo if ( op_code == nil or op_code == '') then return 1, "函数 wms_op.SetTaskState 中的 op_code 必须有值!" end if ( task_state == nil ) then return 1, "函数 wms_op.SetTaskState 中的 task_state 必须有值!" end -- 设置任务已经推送 local strCondition, strSetAttr if ( task_state == 1 ) then strCondition = "S_CODE = '"..op_code.."' AND N_TASK_STATE = 0" strSetAttr = "N_TASK_STATE = 1" -- 2 任务已经执行 elseif ( task_state == 2 ) then strCondition = "S_CODE = '"..op_code.."' AND N_TASK_STATE = 1" strSetAttr = "N_TASK_STATE = 2" else return 0 end nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Operation", strCondition, strSetAttr ) if ( nRet ~= 0 ) then return nRet, strRetInfo end return 0 end --[[ 点到点创建作业 输入参数: -- cntr_code 容器号 -- from_loc_code 起点 -- to_loc_code 终点 -- op_def_name 作业定义名称 -- ext_info = { lock_cntr 启动时是否锁容器, source_sys 来源系统, bs_type, bs_no } 返回: nRet operation ]] function wms_op.Create( strLuaDEID, cntr_code, from_loc_code, to_loc_code, op_type, op_def_name, ext_info ) local nRet, strRetInfo -- 输入参数验证 if ( lua.StrIsEmpty( to_loc_code ) ) then return 1, "wms_op.Create 函数参数 to_loc_code 非法!" end if ( lua.StrIsEmpty( cntr_code ) ) then return 1, "wms_op.Create 函数参数 cntr_code 非法!" end if ( lua.StrIsEmpty( from_loc_code ) ) then return 1, "wms_op.Create 函数参数 from_loc_code 非法!" end if ( lua.StrIsEmpty( op_def_name ) ) then return 1, "wms_op.Create 函数参数 op_def_name 非法!" end if ( op_type == nil ) then return 1, "wms_op.Create 函数参数 op_type 非法!" end local source_sys = "" local lock_cntr = 'Y' local bs_type = '' local bs_no = '' if ( ext_info ~= nil ) then source_sys = lua.Get_StrAttrValue( ext_info.source_sys ) bs_type = lua.Get_StrAttrValue( ext_info.bs_type ) bs_no = lua.Get_StrAttrValue( ext_info.bs_no ) lock_cntr = lua.Get_StrAttrValue( ext_info.lock_cntr ) end if ( lock_cntr == '' ) then lock_cntr = 'Y' end local to_loc nRet, to_loc = wms_wh.GetLocInfo( to_loc_code ) if ( nRet ~= 0 ) then return 1, '获取货位信息失败! '..to_loc end local from_loc nRet, from_loc = wms_wh.GetLocInfo( from_loc_code ) if ( nRet ~= 0 ) then return 1, '获取货位信息失败! '..from_loc end local operation = m3.AllocObject(strLuaDEID,"Operation") operation.source_sys = source_sys operation.start_wh_code = from_loc.wh_code operation.start_area_code = from_loc.area_code operation.start_loc_code = from_loc_code operation.end_wh_code = to_loc.wh_code operation.end_area_code = to_loc.area_code operation.end_loc_code = to_loc_code operation.op_type = op_type operation.op_def_name = op_def_name operation.cntr_code = cntr_code operation.lock_cntr = lock_cntr operation.bs_type = bs_type operation.bs_no = bs_no nRet, operation = m3.CreateDataObj( strLuaDEID, operation ) if ( nRet ~= 0 ) then return 2, '创建【作业】失败!'..operation end return 0, operation end -- 判断作业是否可以取消, 顺带返回作业对象 -- 返回3个参数 -- nRet = 0 函数执行正确 非1错误 -- can_cancel = false, true -- operation 作业对象 function wms_op.CanCancel( strLuaDEID, op_code ) local nRet, strRetInfo local operation nRet, operation = wms_op.GetInfo( strLuaDEID, op_code ) if ( nRet == 1 ) then -- 作业已经被删除,不存在 operation = nil return 0, true, operation end if ( nRet > 1 ) then return nRet, operation end -- 作业已经完成不能取消 if ( operation.bs_state == 2 ) then return 0, false, operation end -- 作业在执行中 if ( operation.bs_state == 1 ) then if ( operation.task_state == 2 ) then return 0, false, operation end end return 0, true, operation end return wms_op