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
--[[
    编码: WMS-40-12
    名称: 取消作业
    作者: HAN  
    日期:2025-1-29
    
    级别:固定 (说明本段代码在项目中不太会变化)
 
    函数: OperationCancel
 
    功能:
        把作业的状态设置为 取消
        -- 需要判断 什么 作业的任务状态
    变更记录:
 
--]]
 
prj_api = require ( "prj_api")
wms_op = require ( "wms_operation")
 
function OperationCancel( strLuaDEID )
    local nRet, strRetInfo, operation_objs,n,m
 
    nRet, operation_objs = m3.GetSysDataJson( strLuaDEID )
    if ( nRet ~=0 ) then 
        lua.Stop( strLuaDEID, objs ) 
        return
    end  
 
    local nCount = #operation_objs
    if (nCount == 0) then  return end
 
    local task_objs, wave_obj
    local task_code, cntr_code, task    
    local operation = {}
    local strCondition
    local prompt_msg, msg, can_cancel
    local cancel_count = 0                  -- 取消作业数量
 
    prompt_msg = ''
    for n = 1, nCount do
        nRet, operation = m3.ObjAttrStrToLuaObj( "Operation", lua.table2str(operation_objs[n].attrs) )
        if ( nRet ~= 0 ) then 
            lua.Stop( strLuaDEID, "m3.ObjAttrStrToLuaObj 失败! "..operation )
            return 
        end
 
        -- 只要是作业没完成的都可以取消 N_B_STATE = 2 (完成) 并且作业的任务状态(N_TASK_STATE) 不等于 2
        -- 作业的任务状态=2 表示作业下面的任务已经设备开始执行了,这个时候必须和设备端进行一些交互后才能取消
        if ( operation.bs_state ~= 2 and operation.task_state ~= 2 ) then
            can_cancel = true
            -- 查询作业下面的任务(状态 = 0等待 1已推送) 
            strCondition = "S_OP_CODE = '"..operation.code.."' AND ( N_B_STATE = 0 OR N_B_STATE = 1 )  "
            nRet, task_objs = m3.QueryDataObject(strLuaDEID, "Task",  strCondition)
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, "获取任务信息失败!" .. operation.code)
                return
            end
            if (task_objs ~= '') then
                for m = 1, #task_objs do
                    task = m3.KeyValueAttrsToObjAttr( task_objs[m].attrs )
                    task_code = lua.Get_StrAttrValue( task.S_CODE )
                    cntr_code = lua.Get_StrAttrValue( task.S_CNTR_CODE )
 
                    if ( lua.Get_NumAttrValue( task.N_B_STATE ) == 1 ) then
                        -- 1 任务已经已经推送给WCS,因此需要调用WCS接口进行取消
                        nRet, strRetInfo = prj_api.WCS_Cancel( strLuaDEID, cntr_code, task_code ) 
                        if ( nRet ~= 0 ) then
                            msg = "编号 = '"..operation.code.."'的作业无法取消, 原因是WCS无法取消料箱号 = "..cntr_code.." 的任! 原因:"..strRetInfo
                            prompt_msg = prompt_msg..msg.."\r\n"
                            can_cancel = false
                        end
                    end
                end
            end
 
            if ( can_cancel ) then
                -- 把作业下面的‘待启动’,‘已推送’任务的状态设置为 5/取消
                strCondition = "S_OP_CODE = '"..operation.code.."' AND ( N_B_STATE = 0 OR N_B_STATE = 1 )  "
                strUpdateSql = "N_B_STATE = 5"
                nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Task", strCondition, strUpdateSql )
                if ( nRet ~= 0 ) then  
                    lua.Stop( strLuaDEID, "更新【Task】信息失败!"..strRetInfo ) 
                    return
                end     
 
                nRet, strRetInfo = wms_op.SetCancel( strLuaDEID, operation )
                if ( nRet ~= 0 ) then  
                    lua.Stop( strLuaDEID, "更新【Operation】信息失败!"..strRetInfo ) 
                    return
                end    
                
                -- 检查作业来源,如果是入库波次,出库波次,他们的状态是关闭中 需要触发后台进行 强制完成操作
                if ( operation.bs_type == "Inbound_Wave" ) then 
                    nRet, wave_obj = m3.GetDataObjectByKey(strLuaDEID, operation.bs_type, "S_WAVE_NO", operation.bs_no )
                    if (nRet ~= 0 ) then 
                        lua.Stop( strLuaDEID, "获取'"..operation.bs_type.."'信息失败! " .. wave_obj ) 
                        return
                    end                    
                    -- 如果入库波次状态=6/关闭中,需要后台触发强制完成事件
                    if ( wave_obj.b_state == 6 ) then
                        local add_wfp = {
                            wfp_type = 1,                  -- 触发数据对象事件(指定数据对象标识)
                            cls = "Inbound_Wave",
                            obj_id = wave_obj.id,
                            obj_name = "入库波次号'"..operation.bs_no.."'-->强制完成",
                            trigger_event = "强制完成"
                        }
                        nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp )
                        if ( nRet ~= 0 ) then 
                            lua.Stop( strLuaDEID, "AddSysWFP失败!"..strRetInfo )  
                            return
                        end                        
                    end
                elseif ( operation.bs_type == "Outbound_Wave" ) then 
                    nRet, wave_obj = m3.GetDataObjectByKey(strLuaDEID, operation.bs_type, "S_WAVE_NO", operation.bs_no )
                    if (nRet ~= 0 ) then 
                        lua.Stop( strLuaDEID, "获取'"..operation.bs_type.."'信息失败! " .. wave_obj ) 
                        return 
                    end                    
                    -- 如果出库波次状态 = 8/关闭中,需要后台触发强制完成事件
                    if ( wave_obj.b_state == 8 ) then
                        local add_wfp = {
                            wfp_type = 1,                  -- 触发数据对象事件(指定数据对象标识)
                            cls = "Outbound_Wave",
                            obj_id = wave_obj.id,
                            obj_name = "出库波次号'"..operation.bs_no.."'-->强制完成",
                            trigger_event = "强制完成"
                        }
                        nRet, strRetInfo = m3.AddSysWFP( strLuaDEID, add_wfp )
                        if ( nRet ~= 0 ) then 
                            lua.Stop( strLuaDEID, "AddSysWFP失败!"..strRetInfo )  
                            return
                        end                        
                    end
                end
                cancel_count = cancel_count + 1
            end
        end
    end
 
    -- 如果中间有作业无法取消的,前端显示原因
    if ( prompt_msg ~= '' ) then
        mobox.setInfo( strLuaDEID, prompt_msg )
    end
    local action
    if ( cancel_count == 1 ) then
        action = {
                    {
                        action_type = "refresh_cur_row",
                        value = ""
                    }
                }
    else
        action = {
            {
                action_type = "refresh",
                value = ""
            }
        }        
    end
    nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action))
    if ( nRet ~= 0 ) then 
        lua.Stop( strLuaDEID, "setAction错误: "..strRetInfo) 
        return
    end      
 
end