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
-- 定义函数用于拆分字符串并转换为数字集合
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
 
--[[
 编码: GT-40-37
 名称: 任务完成
 作者:
 入口函数:TaskFinish
 功能说明:
 变更历史:
 --]]
require("WMS-BASE")
wms_op = require("wms_operation")
wms_cntr = require("wms_container")
require("GT_InAndOutboundPolicies")
function FLTaskFinish(strLuaDEID)
    local nRet, strRetInfo
 
    -- 获取当前作业对象
    local operation
    nRet, operation = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), operation)
    end
 
    -- 获取扩展数据参数
    local ext_data = json.decode(operation.ext_data)
    local delivery_no = ext_data.delivery_no -- 业务单号为 出库单号
    lua.Debug(strLuaDEID, debug.getinfo(1), 'ext_data', ext_data)
 
    -- 获取任务对象
    local task
    nRet, task = m3.SysInputParamToDataObj(strLuaDEID, "Task")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), task)
    end
 
    -- 判断任务终点和作业终点是否一致,一致则设置作业任务完成并重置库存
    if (task.end_loc_code == operation.end_loc_code) then
        nRet, strRetInfo = wms_op.SetFinish(strLuaDEID, operation.code)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1),
                "设置作业编号='" .. operation.code .. "' 的作业完成失败!" .. strRetInfo)
        end
 
        -- 容器解锁
        local container
        nRet, container = wms_cntr.GetInfo(strLuaDEID, operation.cntr_code)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), '获取容器对象失败!' .. container)
        end
        nRet, strRetInfo = wms_cntr.SetLock(strLuaDEID, container, "锁类型-无", "无")
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), '托盘容器解锁失败!' .. strRetInfo)
        end
 
        -- 设置出库单明细状态为完成
        local strCondition = "S_DO_NO = '" .. delivery_no .. "'"
        local strSetSQL_update = " S_STATE = '完成'"
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_SO_Detail", strCondition, strSetSQL_update)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "修改出库单据状态失败!" .. strRetInfo)
        end
 
        -- 设置出库单状态为完成
        strCondition = "S_DO_NO = '" .. delivery_no .. "'"
        strSetSQL_update = " S_STATE = '完成'"
        nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "GT_Stock_Out", strCondition, strSetSQL_update)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "修改出库单据状态失败!" .. strRetInfo)
        end
 
        -- 删除容器货品明细记录
        strCondition = "S_CNTR_CODE = '" .. operation.cntr_code .. "'"
        nRet, strRetInfo = mobox.deleteDataObject(strLuaDEID, "CG_Detail", strCondition)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
        end
 
        -- 重置库区库存
        nRet, strRetInfo = wms.wms_ResetInventory("Area", task.end_area_code)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
        end
 
        -- 当任务的终点为三楼回库口则创建二段堆垛机任务
    elseif (task.end_loc_code == wms_base.Get_sConst(strLuaDEID, "佳通-三楼半钢回库口") or task.end_loc_code ==
        wms_base.Get_sConst(strLuaDEID, "佳通-三楼全钢回库口")) then
        -- 通过任务的终点获取另一边的转运/回库口
        local end_loc
        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 = 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, "任务类型-立库入库搬运")
        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, "调度类型-国自")
        nRet, new_task = m3.CreateDataObj(strLuaDEID, new_task)
        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
 
    elseif (tonumber(task.type) == tonumber(wms_base.Get_nConst(strLuaDEID, "任务类型-立库入库搬运"))) then
        local end_loc
        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 = 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, "任务类型-AGV入库搬运")
        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.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-AGV")
        nRet, new_task = m3.CreateDataObj(strLuaDEID, new_task)
        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
end