1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
--[[
    版本:    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")
wms_station = require( "wms_station" )
wms_putaway = require( "wms_putaway" )
 
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     
 
--[[ 
    创建一个配盘出库作业
    dist_cntr_data 配盘数据对象 { S_DC_NO ='',S_CNTR_CODE = '',...}
    parameter  创建作业附带参数 
    {
        op_def_name -- 出库作业的作业定义名称
        to_staion -- 出库到站台
        source_sys -- 来源业务系统(如果有多系统时需要对作业进行区分,一般为空)
    }
--]]
function wms_op.Create_Distribution_OutOperation ( strLuaDEID, dist_cntr_data, parameter ) 
    local nRet, strRetInfo
    local msg
 
    local dc_no = dist_cntr_data.S_DC_NO 
    local b_state = lua.Get_NumAttrValue( dist_cntr_data.N_B_STATE )
    local to_station = lua.Get_StrAttrValue( dist_cntr_data.S_STATION_NO )
    local cntr_code = dist_cntr_data.S_CNTR_CODE   
    local from_loc_code = lua.Get_StrAttrValue( dist_cntr_data.S_LOC_CODE )
    local to_loc_code = lua.Get_StrAttrValue( dist_cntr_data.S_EXIT_LOC_CODE )       -- 出库货位
    local bs_no = lua.Get_StrAttrValue( dist_cntr_data.S_BS_NO )                     -- 业务来源
    local bs_type = lua.Get_StrAttrValue( dist_cntr_data.S_BS_TYPE )                 -- 业务来源类型
 
    if parameter == nil or parameter == '' then
        return 1, "wms_op.Create_Distribution_OutOperation 函数中 parameter 必须有值!"
    end
    if to_station == '' then
        to_station = parameter.to_station or ''
    end
    local op_def_name = parameter.op_def_name or ''
    if op_def_name == '' then
        return 1, "wms_op.Create_Distribution_OutOperation 函数中 parameter 中必须有 op_def_name!"
    end
 
    -- 【配盘】数据对象属性判断,不合法的终止程序执行
    -- b_state 不是配货完成状态
    if ( b_state ~= DIST_CNTR_STATE.PrePickingOK ) then
        msg = "配盘号'"..dc_no.."'的状态不是配货完成状态,不能启动配盘出库作业!"
        lua.Warning( strLuaDEID, debug.getinfo(1), msg )
        return 1, msg
    end
 
    if ( from_loc_code == '') then
        from_loc_code = wms_wh.GetLocCodeByCNTR( strLuaDEID, cntr_code )
        if ( from_loc_code == nil or from_loc_code == '' ) then
            return 1, "配盘号'"..pac_no.."'中的容器'"..cntr_code.."'没有绑定货位!"
        end
    end
    
    local from_loc
    nRet, from_loc = wms_wh.GetLocInfo( from_loc_code )
    if ( nRet ~= 0 ) then 
        return 1, '获取货位信息失败! '..loc_code
    end      
 
    -- 获取站点货位,站点货位定义在常量中
    if ( to_loc_code == '' ) then 
        if ( to_station == '') then
            return 1, "配盘号'"..dc_no.."'的配盘没定义出库站台或货位!"          
        end
        nRet, to_loc_code = wms_station.Get_Station_Loc( strLuaDEID, to_station )
        if nRet ~= 0 then
            return 2, "wms_station.Get_Station_Loc失败! "..to_loc_code
        end
    end
    local to_loc
    nRet, to_loc = wms_wh.GetLocInfo( to_loc_code )
    if ( nRet ~= 0 ) then 
        return 1, '获取货位信息失败! '..to_loc
    end  
 
    -- 创建【货品出库】作业. 【配盘】状态改为2(出库中)
    local operation = m3.AllocObject(strLuaDEID,"Operation")
    operation.bs_state = 8                              -- 待启动前,这些作业有待后台脚本来设置为状态 0 
    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 = OPERATION_TYPE.Outbound
    operation.op_def_name = op_def_name
    operation.cntr_code = cntr_code
    
    -- 说明作业携带的容器的业务类型
    operation.carry_cb_cls = "Distribution_CNTR"
    operation.carry_cb_no = dc_no
 
    operation.source_sys = parameter.source_sys or ''
 
    operation.bs_type = bs_type
    operation.bs_no = bs_no
 
    nRet, operation = m3.CreateDataObj( strLuaDEID, operation )
    if ( nRet ~= 0 ) then 
        return 1, '创建【作业】失败!'..operation 
    end  
 
    -- 更新【配盘】对象属性
    -- DIST_CNTR_STATE.Out 表示配盘容器已经安排作业进行搬运
    local strUpdateSql = "S_LOC_CODE = '"..from_loc_code.."', N_B_STATE = "..DIST_CNTR_STATE.Out..", S_OUT_OP_NO = '"..operation.code.."'"
    strCondition = "S_DC_NO = '"..dc_no.."'"
    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Distribution_CNTR", strCondition, strUpdateSql )
    if ( nRet ~= 0 ) then  
        return 1, "更新【配盘】信息失败!"..strRetInfo  
    end   
    
    return 0
end
 
--[[
    根据站台位置创建一个入库到立库的入库作业,适合站台-->立库, 
    物流规划: 通过AGV或输送线把料箱从站台搬运到立库巷道堆垛机入库接驳位,然后堆垛机从接驳位取货搬运到立库
 
        -- 首先要计算出作业的入库货位,根据这个货位确定巷道接驳位
        -- 创建一个作业
 
 
    输入参数:
 
        -- station      站台位置
        -- cntr_code    容器号
        -- op_def_name  作业定义
        -- parameter    输入参数{
                        carry_cb_cls,carry_cb_no -- 作业携带的容器目前的业务类型,比如 预分配容器,配盘,。。。
                        bs_type,bs_no -- 入库作业相关的业务类型如入库单,入库波次
                        factory -- 工厂标识
                        op_def_name -- 入库作业类型定义
                        }
                        carry_cb_cls -- 作业携带容器的业务类型(预分配容器,配盘容器,盘点容器)
    返回:
        nRet
        operation
]] 
 
function wms_op.Create_Inbound_Operation( strLuaDEID, station, cntr_code, parameter )
    local nRet, strRetInfo
    local loc_code
    nRet, loc_code = wms_station.Get_Station_Loc( strLuaDEID, station )
    if ( nRet ~= 0 ) then
        return 1, loc_code
    end 
 
    local loc
    nRet, loc = wms_wh.GetLocInfo( loc_code )
    if ( nRet ~= 0 ) then return 1, '获取货位信息失败! '..loc_code end  
 
    --V2.0 创建作业前对容器进行判断,如容器已经存在 active 作业,不能创建
    nRet, can_usedin_op = wms_cntr.CanUsedInOperation( strLuaDEID, cntr_code )
    if ( nRet ~= 0 ) then return 2, can_usedin_op end
    if ( can_usedin_op == false ) then
        return 1, "料箱'"..cntr_code.."'存在未完成的作业,不能继续创建作业!"
    end
    
    local storage_arae 
    nRet, storage_arae = wms_base.Get_sConst2("料箱库存储区")
    if ( nRet ~= 0 ) then
        return 1 ,"系统无法获取常量'料箱库存储区'"
    end 
 
    -- 获取入库作业类型,通过作业类型里定义的上架策略来计算 入库货位
    local op_def
    local op_def_name = parameter.op_def_name or ''
    if op_def_name == '' then
        return 1, "在创建入库作业时失败,没定义入库作业类型!"
    end
    local factory = parameter.factory or ''
    if factory == '' then
        return 1, "在创建入库作业时失败, 在parameter参数中没定义工厂标识!"
    end
 
    nRet, op_def = wms_base.GetOpDefInfo( factory, op_def_name )
    if nRet ~= 0 then
        return 1, "系统无法获取名为'"..op_def_name.."'的作业类型! "..op_def
    end
 
    -- 计算料箱入库货位,首先计算到哪个输送线入库缓存区
    local str_end_loc_code = ''
    local aisle_in_loc = ''
    local end_loc = {}
    local end_wh_code = ''
    local end_area_code = ''
 
    -- -- [TP] 接驳位/转移点 -- 要求计算接驳位负载情况
    -- if op_def.hand_proc == "[Station]->[TP]->[Stacker-AS/RS]" then
    --     -- 模式1# 【站台】-->【巷道接驳位】-->【堆垛机立库】
    --     -- 需要考虑堆垛机的工作状态
    --     if op_def.putaway_rule == '' then
    --         -- 如果没定义上架策略就根据 站台对接的库区进行入库货位计算,对上架的容器没有位置控制要求
    --         nRet, aisle_in_loc, str_end_loc_code = wms_putaway.Get_StorageCache_Loc( strLuaDEID, station )
    --         if ( nRet ~= 0 ) then
    --             if ( nRet == 1 ) then
    --                 return 1, "prj_base.Get_StorageCache_Loc 失败!"..aisle_in_loc
    --             end
    --             return 2, aisle_in_loc
    --         end
 
    --         nRet, end_loc = wms_wh.GetLocInfo( str_end_loc_code )
    --         if ( nRet ~= 0 ) then 
    --             return 2, '获取货位信息失败! '..str_end_loc_code 
    --         end 
    --         end_wh_code = end_loc.wh_code
    --         end_area_code = end_loc.area_code
    --     else
    --         -- 通过上架策略来计算货位
    --         return 2, "程序还没完成..."
    --     end
    -- elseif op_def.hand_proc == "[Station]->[Picking-AGV-AS/RS]" then
    --     -- 特点:Picking-AGV 不一定要求计算到货位,大部分情况到货区即可
    -- end
 
    -- 计算料箱入库货位,首先计算到哪个输送线入库缓存区
    local strCondition = "C_ENABLE = 'Y' AND S_AREA_CODE = '" .. storage_arae .. "' AND N_CURRENT_NUM = 0 AND N_LOCK_STATE = 0"
    local strOrder = "S_CODE"
    local loc_objs = {}
    local aisle_connect_loc_set = {}
    -- 查询 Location 表
    local nRet
    nRet, loc_objs = m3.QueryDataObject(strLuaDEID, "Location", strCondition, strOrder)
    if (nRet ~= 0) then
        return 2, '获取货位信息失败! '..loc_objs 
    end
    -- 定义 END_CODE
    local str_end_loc_code = nil
    local item_set = {}
    local object_attr
    for n = 1, #loc_objs do
        object_attr = m3.KeyValueAttrsToObjAttr(loc_objs[n].attrs)
        table.insert( item_set, object_attr.S_CODE )
    
    end
    
    if #item_set > 0 then
        math.randomseed(os.time())
        local index = math.random(1, #item_set)
        str_end_loc_code = item_set[index]
    else
        return 2, '未找到可用的货位! ' 
    end
    local end_loc
    nRet, end_loc = wms_wh.GetLocInfo( str_end_loc_code )
    if ( nRet ~= 0 ) then 
        return 2, '获取货位信息失败! '..str_end_loc_code 
    end  
 
    -- 计算料箱入库货位,首先计算到哪个输送线入库缓存区
    local sqlstrCondition = "C_ENABLE = 'Y' AND S_AREA_CODE = '" .. storage_arae .. "' AND S_PURPOSE = '接驳' "
    -- 查询 Location 表
    local nRet
    nRet, ansib_objs = m3.QueryDataObject(strLuaDEID, "Location", sqlstrCondition, strOrder)
    if (nRet ~= 0) then
        return 2, '获取货位信息失败! '..loc_objs 
    end
    -- 定义 END_CODE
    local ansib_end_loc_code = nil
    local item_objs = {}
    local object_attr
    for n = 1, #ansib_objs do
        object_attr = m3.KeyValueAttrsToObjAttr(ansib_objs[n].attrs)
        table.insert( item_objs, object_attr.S_CODE )
    
    end
    if #item_objs > 0 then
        math.randomseed(os.time())
        local index = math.random(1, #item_objs)
        ansib_end_loc_code = item_objs[index]
    else
        return 2, '未找到可用的货位! ' 
    end
    
 
    local operation = m3.AllocObject(strLuaDEID,"Operation")
    operation.source_sys = op_def.source_sys
    operation.start_wh_code = loc.wh_code
    operation.start_area_code = loc.area_code
    operation.start_loc_code = loc_code
 
    operation.end_wh_code = end_loc.wh_code
    operation.end_area_code = end_loc.area_code
    operation.end_loc_code = str_end_loc_code
    operation.ext_data = ansib_end_loc_code            -- 巷道口入库缓存货位
 
    operation.op_type = OPERATION_TYPE.Inbound
    operation.op_def_name = op_def_name
    operation.cntr_code = cntr_code
    
    if ( parameter ~= nil and parameter ~= '') then
        operation.carry_cb_cls = lua.Get_StrAttrValue( parameter.carry_cb_cls )
        operation.carry_cb_no = lua.Get_StrAttrValue( parameter.carry_cb_no )
        operation.bs_type = lua.Get_StrAttrValue( parameter.bs_type )
        operation.bs_no = lua.Get_StrAttrValue( parameter.bs_no )
    end
 
    nRet, operation = m3.CreateDataObj( strLuaDEID, operation )
    if ( nRet ~= 0 ) then 
        return 2, '创建【作业】失败!'..operation 
    end 
   
    return 0, operation
end
 
return wms_op