wsz
2025-06-10 4be5e6f4ae34bcb7cb47f05816421459bbfaedf1
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
--[[
    版本:    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