lzh
2025-06-19 3a6436e0c88042c6ce8dca2fe8adb0109f0ad9e4
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
-- 定义函数用于拆分字符串并转换为数字集合
local function splitAndCollectNumbers(str)
    local numbers = {}
    for num in string.gmatch(str, "%d+") do
        numbers[tonumber(num)] = true
    end
    return numbers
end
 
-- 定义函数用于找出两个集合中重合的数字并拼接成字符串
local function findCommonNumbers(strA, strB)
    local setA = splitAndCollectNumbers(strA)
    local setB = splitAndCollectNumbers(strB)
    local commonNumbers = {}
    for num in pairs(setA) do
        if setB[num] then
            table.insert(commonNumbers, tostring(num))
        end
    end
    return table.concat(commonNumbers, ",")
end
-- [
-- StockInquiry  库存查询
-- 1、获取巷道货位总数
-- 2、获取满货位总数
-- 3、(满货位总数 / 巷道总数)* 100 = 满货位百分比
-- 4、判断满货位库存是否达到80%,没达到则返回可入库巷道
-- param:
--      str_roadway   巷到
-- ]
local function StockInquiry(strLuaDEID)
    -- 获取未冻结的巷道
    local strCondition = "S_AREA_CODE = 'LK' AND N_LOCK_STATE = 0"
    local nRet, roadway = m3.QueryDataObject(strLuaDEID, "Roadway", strCondition)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), '查询巷道信息失败' .. roadway)
    end
    if (roadway == nil or roadway == '') then
        lua.Error(strLuaDEID, debug.getinfo(1), '没有可入库的巷道!')
    end
 
    local str = ''
    for i = 1, #roadway do
        local attrs = roadway[i].attrs
        attrs = m3.KeyValueAttrsToObjAttr(attrs)
        if (attrs == nil) then
            goto coroutine
        end
 
        str = str .. attrs.N_ROADWAY .. ","
        ::coroutine::
    end
    -- 去除最后一个,
    str = lua.trim_laster_char(str)
    lua.Debug(strLuaDEID, debug.getinfo(1), "str", str)
    return 0, str
end
 
-- 创建任务
local function GT_CreateTask(strLuaDEID, operation, task, end_loc, type, schedule_type, roadway)
    local nRet, strRetInfo
    local new_task = m3.AllocObject(strLuaDEID, "Task")
    new_task.op_code = operation.code
    new_task.op_name = operation.op_def_name
    new_task.factory = operation.factory
    new_task.type = wms_base.Get_nConst(strLuaDEID, type)
    new_task.cntr_code = operation.cntr_code
    -- 起点为上一个任务的终点
    new_task.start_wh_code = task.end_wh_code
    new_task.start_area_code = task.end_area_code
    new_task.start_loc_code = task.end_loc_code
    new_task.end_wh_code = end_loc.wh_code
    new_task.end_area_code = end_loc.area_code
    new_task.end_loc_code = end_loc.code
    new_task.roadway = roadway
    new_task.schedule_type = wms_base.Get_nConst(strLuaDEID, schedule_type)
    nRet, new_task = m3.CreateDataObj(strLuaDEID, new_task)
    if (nRet ~= 0) then
        return 2, "创建任务失败!" .. new_task
    end
    return 0, new_task
end
--[[
 编码: GT-40-27
 名称: 动作监听
 作者:
 入口函数:TaskFinish
 功能说明:
 
 变更历史:
 --]]
wms_op = require("wms_operation")
m3 = require("oi_base_mobox")
function TaskFinish(strLuaDEID)
    local nRet, strRetInfo, objs
 
    local operation
    nRet, operation = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), operation)
    end
 
    -- 获取任务对象
    local task
    nRet, task = m3.SysInputParamToDataObj(strLuaDEID, "Task")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), task)
    end
 
    -- 解析作业中的扩展参数
    local ext_data, success
    success, ext_data = pcall(json.decode, operation.ext_data)
    if (success == false) then
        lua.Error(strLuaDEID, debug.getinfo(1), "operation_obj.ext_data 中内容JSON格式不合法!")
    end
    local type = ext_data.type -- 1 粉料调拨搬运 2 烘胶搬运
    lua.Debug(strLuaDEID, debug.getinfo(1), "type", type)
    lua.Debug(strLuaDEID, debug.getinfo(1), "task", task)
 
    -- 1 粉料调拨搬运 2 烘胶移库 3 同巷道移库
    if (tonumber(type) == 1) then
        local end_loc
        -- 任务类型 为AGV出库搬运 则创建二段国自任务
        if (task.type == wms_base.Get_nConst(strLuaDEID, "任务类型-AGV出库搬运")) then
            -- 通过任务的终点获取另一边的转运/回库口
            if (task.end_loc_code == wms_base.Get_sConst(strLuaDEID, "佳通-三楼半钢回库口")) then
                nRet, end_loc = wms_wh.Location_GetInfo(strLuaDEID, "THREE-QGCKK-QY")
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo失败! " .. end_loc)
                end
            else
                nRet, end_loc = wms_wh.Location_GetInfo(strLuaDEID, "THREE-BGCKK-QY")
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo失败! " .. end_loc)
                end
            end
 
            -- 获取可入库巷道
            local op_num, roadway
            local str
            nRet, str = StockInquiry(strLuaDEID)
            str = findCommonNumbers(str, "3,4")
            if (str == '') then
                lua.Error(strLuaDEID, debug.getinfo(1), "粉料调拨没有可分配的巷道!")
            end
            local roadway_list = lua.split(str, ",")
            for i = 1, #roadway_list do
                -- 获取巷道任务数量
                local strCondition = "N_ROADWAY = " .. roadway_list[i] .. " AND N_B_STATE = 1"
                nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "Task", strCondition)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "getDataObjCount 失败! " .. strRetInfo)
                end
                if (op_num == nil) then
                    op_num = lua.StrToNumber(strRetInfo)
                    roadway = roadway_list[i]
                elseif (tonumber(op_num) >= lua.StrToNumber(strRetInfo)) then
                    op_num = lua.StrToNumber(strRetInfo)
                    roadway = roadway_list[i]
                end
            end
            if (roadway == nil) then
                lua.Error(strLuaDEID, debug.getinfo(1), "粉料调拨没有可分配的巷道!")
            end
 
            -- 创建任务
            local new_task
            nRet, new_task = GT_CreateTask(strLuaDEID, operation, task, end_loc, "任务类型-立库入库搬运",
                "调度类型-国自", roadway)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), new_task)
            end
 
            -- 通过容器获取物料信息
            local cg_detail_list, cg_detail
            nRet, cg_detail_list = wms_cntr.Get_Container_Goods(strLuaDEID, new_task.cntr_code)
            nRet, cg_detail = m3.ObjAttrStrToLuaObj("CG_Detail", lua.table2str(cg_detail_list[1].attrs))
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), cg_detail)
            end
 
            -- 获取起点绑定的WCS站点
            local condition = "S_VALUE = '" .. new_task.start_loc_code .. "' AND S_NOTE NOT LIKE '%高度规格站台%'"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "获取【常量】信息失败! " .. strRetInfo)
            end
            local start_loc_code_zd = strRetInfo.name
            condition = "S_VALUE = '" .. new_task.end_loc_code .. "' AND S_NOTE NOT LIKE '%高度规格站台%'"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "获取【常量】信息失败! " .. strRetInfo)
            end
            local end_loc_code_zd = strRetInfo.name
 
            -- 调用国自的任务下发接口
            local strCode = lua.guid() -- 生产一个GUID字符串
            local str_day_time = os.date("%Y-%m-%d %H:%M:%S")
            local url = wms_base.Get_sConst(strLuaDEID, "WCS-url")
            local strurl = url .. "/create"
            local strHeader = ""
            local strBody = {}
            local data = {
                req_no = strCode,
                task_type = 5, -- 1=货物入库;2=货物出库;3=托盘组入库;4=托盘组出库;5=移动(不过库位);6=同巷道移库;7=不同巷道移库
                task_no = new_task.code,
                tunnel_no = roadway,
                from_pos = start_loc_code_zd,
                to_pos = end_loc_code_zd,
                mat_code = operation.cntr_code,
                mat_type = cg_detail.item_code,
                mat_memo = cg_detail.item_name,
                req_time = str_day_time
            }
            strBody[1] = data
            lua.Debug(strLuaDEID, debug.getinfo(1), 'strBody', strBody)
            nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "WCS", "任务创建")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "调用接口失败!" .. strRetInfo)
            end
            -- 设置状态未推送
            wms_task.SetStateByCode(strLuaDEID, new_task.code, "任务状态-已推送")
 
            -- 起点解绑
            nRet, strRetInfo = wms_wh.Loc_Container_Unbinding(strLuaDEID, task.start_loc_code, operation.cntr_code,
                "绑定解绑方法-系统", "完成")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), '货位容器解绑失败!' .. strRetInfo)
            end
 
            -- 起点解锁
            nRet, strRetInfo = wms.wms_UnlockLocation(strLuaDEID,task.start_loc_code ,operation.bs_no)
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_UnlockLocation 失败! " .. strRetInfo) end
 
 
            -- 任务类型 为AGV入库搬运 则设置任务完成和佳通任务完成
        elseif (task.type == wms_base.Get_nConst(strLuaDEID, "任务类型-AGV入库搬运")) then
            -- 设置作业完成
            nRet, strRetInfo = wms_op.SetFinish(strLuaDEID, operation.code)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1),
                    "设置作业编号='" .. operation.code .. "' 的作业完成失败!" .. strRetInfo)
            end
 
            -- 设置佳通任务完成
            local condition = " S_CNTR_CODE = '" .. operation.cntr_code .. "'"
            local update_sql = " N_B_STATE = 3,S_B_STATE = '完成'"
            nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Task", condition, update_sql)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "修改移库单明细状态失败!" .. strRetInfo)
            end
 
            -- 判断佳通任务的来源单号所在的佳通任务是否都已完成,完成则设置粉料调拨状态为完成
            strCondition = "S_B_STATE <> '完成' AND S_BS_NO = (SELECT S_BS_NO FROM TN_GT_Task where S_CODE = '" ..
                               operation.bs_no .. "')"
            nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "GT_Task", strCondition)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
            end
            lua.Debug(strLuaDEID, debug.getinfo(1), "nRet", nRet)
            if (tonumber(strRetInfo) == 0) then
                condition = "S_NO = (SELECT S_BS_NO FROM TN_GT_Task where S_CODE = '" .. operation.bs_no .. "')"
                update_sql = "S_B_STATE = 3"
                nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Allot_Order", condition, update_sql)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "修改粉料调拨单状态失败!" .. strRetInfo)
                end
            end
 
            -- 起点解绑
            nRet, strRetInfo = wms_wh.Loc_Container_Unbinding(strLuaDEID, task.start_loc_code, operation.cntr_code,
                "绑定解绑方法-系统", "完成")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), '货位容器解绑失败!' .. strRetInfo)
            end
            -- 终点绑定
            nRet, strRetInfo = wms_wh.Loc_Container_Binding(strLuaDEID, task.end_loc_code, operation.cntr_code,
                "绑定解绑方法-系统", "完成")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), '货位容器解绑失败!' .. strRetInfo)
            end
            -- 终点解锁
            nRet, strRetInfo = wms.wms_UnlockLocation(strLuaDEID,task.end_loc_code ,operation.bs_no)
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_UnlockLocation 失败! " .. strRetInfo) end
 
            -- 任务类型 任务类型-立库入库搬运 则创建三段AGV任务
        elseif (task.type == wms_base.Get_nConst(strLuaDEID, "任务类型-立库入库搬运")) then
            nRet, end_loc = wms_wh.Location_GetInfo(strLuaDEID, operation.end_loc_code)
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo失败! " .. end_loc)
            end
 
            -- 创建任务
            local new_task
            nRet, new_task = GT_CreateTask(strLuaDEID, operation, task, end_loc, "任务类型-AGV入库搬运",
                "调度类型-AGV")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), new_task)
            end
            -- 通过容器获取物料信息
            local cg_detail
            nRet, cg_detail = wms_cntr.Get_Container_Goods(strLuaDEID, new_task.cntr_code)
            nRet, cg_detail = m3.ObjAttrStrToLuaObj("CG_Detail", lua.table2str(cg_detail[1].attrs))
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), cg_detail)
            end
 
            local data = {
                taskData = {
                    taskNum = new_task.code,
                    pickStation = new_task.start_loc_code,
                    dropStation = new_task.end_loc_code,
                    taskType = 5,
                    carrierType = 1,
                    priority = 1,
                    source = "YCL"
                },
                partData = {
                    rfid = operation.cntr_code,
                    lotNumber = nil,
                    partNumber = cg_detail.item_code,
                    partDesc = cg_detail.item_code,
                    partType = nil,
                    weight = cg_detail.qty,
                    unit = cg_detail.wu,
                    maturityTime = nil,
                    productionTime = nil,
                    stewingTime = nil,
                    overdueTime = nil
                }
            }
            -- 调用AGV任务下发
            local url = wms_base.Get_sConst(strLuaDEID, "AGV-url")
            local strurl = url .. "/CreateTask"
            local strHeader = ""
            local strBody = data
            nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "AGV", "任务创建")
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1), "调用WCS接口失败!" .. strRetInfo)
            end
 
            -- 设置状态未推送
            wms_task.SetStateByCode(strLuaDEID, new_task.code, "任务状态-已推送")
        end
    elseif (tonumber(type) == 3) then
        -- 设置作业完成
        nRet, strRetInfo = wms_op.SetFinish(strLuaDEID, operation.code)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1),
                "设置作业编号='" .. operation.code .. "' 的作业完成失败!" .. strRetInfo)
        end
 
        -- 设置佳通任务完成
        local condition = " S_CNTR_CODE = '" .. operation.cntr_code .. "'"
        local update_sql = " N_B_STATE = 3,S_B_STATE = '完成'"
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Task", condition, update_sql)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "修改移库单明细状态失败!" .. strRetInfo)
        end
    end
end