fy36
2025-07-01 350eb5ec9163d3ea21416b1525bb80191e958071
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
--[[
    版本:    Version 2.1
    创建日期: 2023-6-16
    创建人:   HAN
    WMS-Basis-Model-Version: V15.5
 
    功能:
        Task Lua程序包整合了一些【任务】对象相关的操作
 
        -- GetPushedCount 获取调度系统已经推送的任务数量
        -- SetErrState 设置任务错误状态
        -- SetStateByCode 设置任务状态
        -- GetInfo 根据任务号获取任务属性
        -- Update 更新任务数据对象属性
        -- Action_Exist 任务下面是否存在某个动作码
        -- SetRunState 设置任务状态为"执行"
        -- GetAreaCount 获取物理库区任务数量
        -- After_TaskFinish 任务完成后的标准处理流程
    更改说明:
--]]
 
wms_base = require ("wms_base")
wms_wh   = require ("wms_wh")
 
local wms_task = {_version = "0.2.1"}
 
-- 获取调度系统已经推送的任务数量
-- 输入参数: strSecheduleType 调度类型
-- 返回: nRet,nCount   
function wms_task.GetPushedCount( strLuaDEID, strSecheduleType )
    local nRet, strRetInfo
    local strCondition
 
    local nSecheduleType
    if ( type(strSecheduleType) == "string" ) then
        nSecheduleType = wms_base.Get_nConst( strLuaDEID, strSecheduleType )
    else
        nSecheduleType = strSecheduleType
    end
    -- 获取某种调度类型的任务数量
    -- 1 已推送 2 -- 执行
    strCondition = "N_SCHEDULE_TYPE = "..nSecheduleType.." AND ( N_B_STATE = 1 OR N_B_STATE = 2 )"
    nRet, strRetInfo = mobox.getDataObjCount( strLuaDEID, "Task", strCondition )
    if ( nRet ~= 0 ) then
        return nRet, strRetInfo
    end
    return 0, tonumber( strRetInfo )
end
 
-- 设置任务错误状态
function wms_task.SetErrState( strLuaDEID, task, nErrCode, strErr )
    if ( task.id == nil or task.id == '' ) then
        return 1, "调用 wms_task.SetErrState 函数时参数不正确, 任务ID不能为空!"
    end
 
    -- 根据字典获取 N_B_STATE 的显示名称
    local str_b_state = wms_base.GetDictItemName( strLuaDEID, "WMS_TaskState", 4 )           -- 4 表示错误状态
    local condition = "S_ID = '"..task.id.."'"
    strSetAttr = "N_B_STATE = 4, S_B_STATE = '"..str_b_state.."', S_ERR='"..strErr.."', N_ERR = "..nErrCode
    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, task.cls, condition, strSetAttr )
    if ( nRet ~= 0 ) then
       return nRet, "更新任务对象失败!"..strRetInfo
    end
    return 0, "ok"
end
 
-- 设置任务状态为"执行"
-- HAN 2024-3-15 新增 executor_id, executor_name 可以不输入
function wms_task.SetRunState( strLuaDEID, task, executor_id, executor_name )
 
    --local nBState = wms_base.Get_nConst(strLuaDEID,"任务状态-执行")
    local nBState = 2
    -- 根据字典获取 N_B_STATE 的显示名称
    local str_b_state = wms_base.GetDictItemName( strLuaDEID, "WMS_TaskState", nBState ) 
    local condition = "S_CODE = '"..task.code.."'"
    local strSetAttr
 
    strSetAttr = "N_B_STATE = "..nBState..", S_B_STATE = '"..str_b_state.."', T_START_TIME = '"..os.date("%Y-%m-%d %H:%M:%S").."'"
    if ( executor_id ~= nil and executor_id ~= '' and executor_name ~= nil and executor_name ~= '') then
        strSetAttr = strSetAttr..",S_EXECUTOR_ID = '"..executor_id.."', S_EXECUTOR_NAME = '"..executor_name.."'"
    end
    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, task.cls, condition, strSetAttr )
    if ( nRet ~= 0 ) then return 1, "设置任务状态失败!"..strRetInfo end
    return 0
end
 
-- 设置任务状态
-- strState 任务状态( 已推送 执行 完成 )
function wms_task.SetStateByCode( strLuaDEID, task_code, strState, strErr )
    local nBState
 
    if ( task_code == nil or task_code == '' ) then
        return 1, "调用 wms_task.SetStateByCode 函数时参数不正确, 任务编码不能为空!"
    end  
    if ( strState == nil or strState == '' ) then
        return 1, "调用 wms_task.SetStateByCode 函数时参数不正确, 状态不能为空!"
    end    
 
    if ( strErr == nil ) then strErr = '' end
    if ( type(strState) == "string" ) then
        nBState = wms_base.Get_nConst(strLuaDEID, strState)
    else
        nBState = strState
    end
 
    -- 根据字典获取 N_B_STATE 的显示名称
    local str_b_state = wms_base.GetDictItemName( strLuaDEID, "WMS_TaskState", nBState ) 
    local condition = "S_CODE = '"..task_code.."'"
    strSetAttr = "N_B_STATE = "..nBState..", S_B_STATE = '"..str_b_state.."', S_ERR = '"..strErr.."'"
 
    -- 设置任务为执行状态
    if ( nBState == 2 ) then
        local curTime = os.date("%Y-%m-%d %H:%M:%S")
        strSetAttr = strSetAttr..", T_START_TIME = '"..curTime.."'"
    end
 
    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Task", condition, strSetAttr )
    if ( nRet ~= 0 ) then
       return nRet, "设置任务状态失败!"..strRetInfo
    end
    return 0, "ok"
end
 
-- 根据任务号获取任务属性,如果不存在返回非0
-- 0 获取任务信息 1 不存在 2 发错误
function wms_task.GetInfo( strLuaDEID, task_code )
    if ( task_code == nil or task_code == '' ) then
        return 2, "调用 WMS_Task_GetBaseInfo 函数时参数不正确,任务编码不能为空!"
    end
    local nRet, strRetInfo, id
    local strCondition = "S_CODE = '"..task_code.."'"
    nRet, id, strRetInfo = mobox.getDataObjAttrByKeyAttr( strLuaDEID, "Task", strCondition )
    if ( nRet == 1 ) then
        return 1, "任务编码='"..task_code.."'的任务不存在!"
    end
    if ( nRet ~= 0  ) then
        return 2, "getDataObjAttrByKeyAttr 发生错误!"..id
    end
 
    nRet, strRetInfo = mobox.objAttrsToLuaJson( "Task", strRetInfo )
    if ( nRet ~= 0  ) then
        return 2, "objAttrsToLuaJson Task 失败!"..strRetInfo
    end
 
    local object, success
    success, object = pcall( json.decode, strRetInfo )
    if ( success == false ) then
        return 2, "objAttrsToLuaJson('Task') 返回的的JSON格式不合法!"
    end
    object.id = id
    return 0, object
end
 
-- 更新任务数据对象,需要有任务ID
-- 返回: 成功 = 0 错误 非0
function wms_task.Update( strLuaDEID, task )
    if ( task.id == nil or task.id == '' ) then
        return 1, "调用 wms_task.Update 函数时参数不正确, 任务ID不能为空!"
    end
    local nRet, strAttrs
    nRet, strAttrs = mobox.luaJsonToObjAttrs(task.cls, lua.table2str(task))
    if ( nRet ~= 0 ) then return nRet, strAttrs end
 
    local strUpdate = '[{"id":"'..task.id..'","attrs":'..strAttrs..'}]'
    local strRetInfo
 
    nRet, strRetInfo = mobox.updateDataObj( strLuaDEID, task.cls, strUpdate, 1 )
    if ( nRet ~= 0 ) then
        return nRet, strRetInfo
    end
    return 0, "ok"
end
 
-- 任务下面是否存在某个动作码
-- task_code 任务编码
-- action_code 动作码
function wms_task.Action_Exist( strLuaDEID, task_code, action_code )
    local nRet, strRetInfo
 
    if ( task_code == '' or  task_code == nil) then 
        return 1, "wms_task.Action_Exist 任务编码不能为空!"
    end
    local strCondition = "S_TASK_CODE ='"..task_code.."' AND N_ACTION_CODE = "..action_code
    nRet, strRetInfo = mobox.existThisData( strLuaDEID, "Task_Action", strCondition )
    if ( nRet ~= 0 ) then
        return 1, "在【任务动作】是否存在时失败! "..strRetInfo
    end
    if ( strRetInfo == 'no' ) then return 0,"no" end
    return 0, "yes"
end
 
--[[
    通过任务对象给任务的两个货位加锁
    task -- 任务对象
    nStartLockType -- 开始货位的锁类型值
    nEndLockType -- 结束货位的锁类型值
--]]
function wms_task.LockLocation( strLuaDEID, task, startLockType, endLockType )
    local nRet, strRetInfo
 
    if ( task == nil or type(task) ~= "table" ) then 
        return 1, "wms_task.LockLocation  函数中 task不能为空而且必须是table类型!"
    end
    if ( startLockType == nil or startLockType == "" ) then 
        return 1, "wms_task.LockLocation  函数中 startLockType 不能为空而且必须有值!"
    end
    if ( endLockType == nil or endLockType == "" ) then 
        return 1, "wms_task.LockLocation  函数中 endLockType 不能为空而且必须有值!"
    end
 
    local nStartLockType, nEndLockType
    if (type(startLockType) == "string") then
        nStartLockType = wms_base.Get_nConst( strLuaDEID, startLockType )
    else
        nStartLockType = startLockType
    end
    if (type(endLockType) == "string") then
        nEndLockType = wms_base.Get_nConst( strLuaDEID, endLockType )
    else
        nEndLockType = endLockType
    end
 
    nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, task.start_loc_code, nStartLockType, task.code, task.op_code, task.op_name )
    if (nRet ~= 0) then return 1, "wms_LockLocation 失败! 开始货位='"..task.start_loc_code.."'  "..strRetInfo end
    nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, task.end_loc_code, nEndLockType, task.code, task.op_code, task.op_name )
    if (nRet ~= 0) then return 1, "wms_LockLocation 失败! 终止货位='"..task.end_loc_code.."'  "..strRetInfo end 
 
    return 0
end
 
-- 获取物理库区中任务数量
-- area_code  物理库区  str_task_type  任务类型 (可以不输入)
-- 返回: nRet, nCount   
function wms_task.GetEndLocInAreaCount( strLuaDEID, area_code, str_task_type )
    local nRet, strRetInfo
    local strCondition
 
    if ( area_code == nil or area_code == '' ) then 
        return 1, "WMS_Task_GetAreaCount 函数arae_code 不能为空!"
    end
    
    -- 获取物理库区某种任务类型数量
    -- N_B_STATE 0等待/1已推送/2执行中/3完成/4错误
    strCondition = "S_END_AREA = '"..area_code.."' AND  N_B_STATE <= 2 "
    if ( str_task_type ~= nil and str_task_type ~= '') then
        local nType
        if ( type(str_task_type) == "string") then
            nType = wms_base.Get_nConst( strLuaDEID, str_task_type )
        else
            nType = str_task_type
        end
        strCondition = strCondition.." AND N_TYPE = "..nType
    end
 
    nRet, strRetInfo = mobox.getDataObjCount( strLuaDEID, "Task", strCondition )
    if ( nRet ~= 0 ) then
        return nRet, strRetInfo
    end
    return 0, lua.StrToNumber( strRetInfo )
end
 
-- 标准的任务完成后的处理逻辑,适用于大部分的任务完成后处理
function wms_task.After_TaskFinish(strLuaDEID) 
    local nRet, strRetInfo
 
    -- 获取当前触发脚本的任务信息(Task)
    local task
    nRet, task = m3.GetSysCurEditDataObj( strLuaDEID, "Task" )
    if ( nRet ~= 0 ) then return 1, task end
 
    -- 解绑起点货位
    nRet, strRetInfo = wms_wh.Loc_Container_Unbinding( strLuaDEID, task.start_loc_code, task.cntr_code, "绑定解绑方法-系统",  
                                                    task.op_code.." "..task.op_name )
    if ( nRet ~= 0 ) then return 1, '货位容器解绑失败!'..strRetInfo end 
 
    -- 绑定终点货位  
    nRet, strRetInfo = wms_wh.Loc_Container_Binding( strLuaDEID, task.end_loc_code, task.cntr_code, "绑定解绑方法-系统",  
                                                  task.op_code.." "..task.op_name )
    if ( nRet ~= 0 ) then return 1, '货位容器绑定失败!'..strRetInfo end 
 
    -- 解锁由该任务造成的货位锁,逻辑库区锁都解除
    nRet, strRetInfo = wms.wms_UnlockByTask( strLuaDEID, task.code )
    if ( nRet ~= 0 )  then return 1, "wms_UnlockByTask 失败! "..strRetInfo end 
 
    return 0
end
 
return wms_task