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
--[[
     编码: GT-40-36
     名称: 作业启动-退料入库
     作者: LZH
     入口函数:OperationStart
     功能说明:
            -- 计算出入库终点货位
            -- 在作业下面创建任务类型 = <AGV入库搬运> 的任务(Task),调度系统,设备为“AGV”
            -- 入库终点货位加入库锁
 
    备注:目前已知只有 天然胶\粉料 需要AGV搬运,其他的按照正常入库流程走
     变更历史:
 --]]
wms_op = require("wms_operation")
wms_wh = require("wms_wh")
wms_task = require("wms_task")
require("GT_InAndOutboundPolicies")
require("GT-Base")
function OperationStart(strLuaDEID)
    local nRet, data, strRetInfo
 
    -- 获取作业对象
    local operation
    nRet, operation = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "获取作业对象属性失败!" .. operation)
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), '退料入库operation参数!', operation)
 
    -- 解析作业中的扩展参数
    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
    lua.Debug(strLuaDEID, debug.getinfo(1), '退料入库作业扩展参数!', ext_data)
    local item_type = ext_data.item_type   -- 物料类型
    local isothermal = ext_data.isothermal -- 是否恒温
 
    -- 创建搬运任务
    local task = m3.AllocObject(strLuaDEID, "Task")
    task.op_code = operation.code    -- 作业编码
    task.op_name = "退料入库"
    task.factory = operation.factory -- 工厂
    task.cntr_code = operation.cntr_code
    -- 起点
    task.start_wh_code = operation.start_wh_code
    task.start_area_code = operation.start_area_code
    task.start_loc_code = operation.start_loc_code
 
    -- 获取起点库区信息
    local area
    local strCondition = "S_CODE = '" .. operation.start_area_code .. "'"
    nRet, area = m3.GetDataObjByCondition(strLuaDEID, "Area", strCondition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. area) end
 
    -- 通过容器获取明细信息
    local cg_detail
    nRet, cg_detail = wms_cntr.Get_Container_Goods(strLuaDEID, operation.cntr_code)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取码盘信息失败!" .. cg_detail) end
    nRet, cg_detail = m3.ObjAttrStrToLuaObj("CG_Detail", lua.table2str(cg_detail[1].attrs))
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "ObjAttrStrToLuaObj失败! " .. cg_detail) end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'cg_detail!', cg_detail)
 
    local store_loc
    -- 目前已知需要AGV搬运的只有 物料类型为 天然胶\粉料
    -- 天然胶 二段AGV任务 从线边库到接驳位 从接驳位到立库
    task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-AGV入库搬运")
    task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-AGV") -- 设置调度类型
    if (item_type == '粉料' or item_type == '白炭黑') then
        -- 获取终点
        local new_area_code = ''
        if (area.note == '半钢') then
            if (isothermal == 'Y') then
                new_area_code = "HWPFL"
            else
                new_area_code = "PFL"
            end
        else
            if (isothermal == 'Y') then
                new_area_code = "HWTFL"
            else
                new_area_code = "TFL"
            end
        end
        -- 查询条件为库区为指定库区的空货位
        strCondition = "S_AREA_CODE = '" ..
            new_area_code .. "' AND N_CURRENT_NUM = 0 AND N_LOCK_STATE = 0 AND C_ENABLE = 'Y'"
        -- 根据终点库区获取终点货位 按照排列层排序
        local strOrder = "N_ROW,N_COL,N_LAYER"
        -- 最多获取 10 条
        nRet, strRetInfo = mobox.queryDataObjAttr3(strLuaDEID, "Location", strCondition, 10, strOrder)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "获取货位信息错误! " .. strRetInfo .. " SQL条件: " .. strCondition)
        end
 
        local loc
        if (strRetInfo ~= '') then
            local retObjs = json.decode(strRetInfo)
            nRet, loc = m3.ObjAttrStrToLuaObj("Location", lua.table2str(retObjs[1].attrs))
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "m3.ObjAttrStrToLuaObj(Location) 失败! " .. loc) end
        else
            lua.Error(strLuaDEID, debug.getinfo(1), "没有查询到可入库的空货位!")
        end
 
        -- 获取终点信息
        nRet, store_loc = wms_wh.GetLocInfo(loc.code)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), 'WMS_GetLocInfo失败!' .. store_loc) end
 
        -- 终点为作业的终点
        task.end_wh_code = store_loc.wh_code
        task.end_area_code = store_loc.area_code
        task.end_loc_code = store_loc.code
 
        nRet, task = m3.CreateDataObj(strLuaDEID, task)
        lua.Debug(strLuaDEID, debug.getinfo(1), 'task', task)
 
        nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, task.end_loc_code,
            wms_base.Get_nConst(strLuaDEID, "锁类型-入库锁"), operation.code, operation.code, operation.op_def_name)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_LockLocation 失败!" .. strRetInfo) end
    else
        -- 通过起点获取库区在几楼,获取库区对应的机台是在半钢还是全钢,根据这两个条件筛选出库口区域确定终点
        -- 通过楼层 + 库区位置 计算AGV的终点为哪个回库口库区
        -- 库区的备注为 半钢/全钢
        strCondition = "N_FLOOR = '" .. area.floor .. "' AND S_NAME LIKE '%" .. area.note .. "回库口%'"
        nRet, area = m3.GetDataObjByCondition(strLuaDEID, "Area", strCondition)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. area) end
 
        -- 获取终点接驳位
        local end_loc_list
        local end_loc_code = ''
        strCondition = "N_LOCK_STATE = 0 AND S_AREA_CODE = '" .. area.code .. "' AND N_CURRENT_NUM = 0"
        nRet, end_loc_list = m3.QueryDataObject(strLuaDEID, "Location", strCondition)
        if (nRet ~= 0 or end_loc_list == '') then
            local msg
            msg = "作业编码为=" .. operation.code .. " 创建任务失败, 没有可用接驳位"
            lua.Warning(strLuaDEID, debug.getinfo(1), msg)
            lua.Wait(strLuaDEID, msg)
            return
        end
 
        for i = 1, #end_loc_list do
            local attrs = end_loc_list[i].attrs
            attrs = m3.KeyValueAttrsToObjAttr(attrs)
            if (attrs == nil) then goto coroutine end
            end_loc_code = attrs.S_CODE
            -- 请求WCS获取货位是否可用,可用则返回货位(未处理)
            ::coroutine::
        end
 
        if (end_loc_code == '') then
            local msg
            msg = "作业编码为=" .. operation.code .. " 创建任务失败, 没有可用接驳位"
            lua.Warning(strLuaDEID, debug.getinfo(1), msg)
            lua.Wait(strLuaDEID, msg)
            return
        end
 
        -- 通过获取到的终点货位作为终点找到上一个退料入库作业,如果结束时间未达到一分钟则继续等待
        local condition = "S_OP_DEF_NAME = '退料入库' AND S_END_LOC = '" .. end_loc_code .. "'"
        nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Operation", condition)
        if (nRet == 0) then
            condition = "S_OP_DEF_NAME = '退料入库' AND S_END_LOC = '" ..
                end_loc_code .. "' AND T_END_TIME IS NOT NULL AND T_END_TIME + INTERVAL '1' MINUTE <= SYSDATE"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Operation", condition)
            if (nRet == 1) then
                local msg
                msg = "作业编码为=" .. operation.code .. " 创建任务失败, 没有可用接驳位"
                lua.Warning(strLuaDEID, debug.getinfo(1), msg)
                lua.Wait(strLuaDEID, msg)
                return
            elseif (nRet > 1) then
                lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. strRetInfo)
            end
        elseif (nRet > 1) then
            lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. strRetInfo)
        end
 
        -- 获取货位信息
        nRet, store_loc = wms_wh.GetLocInfo(end_loc_code)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), 'WMS_GetLocInfo失败!' .. store_loc) end
 
        -- 设置作业对象的终点货位信息
        nRet, strRetInfo = wms_op.SetEndLoc(strLuaDEID, operation.code, store_loc.code, store_loc.area_code,
            store_loc.wh_code)
        if (nRet ~= 0) then
            local strErr = "设置作业编码 = '" .. operation.code .. "' 的终点货位信息失败! " .. strRetInfo
            lua.Error(strLuaDEID, debug.getinfo(1), strErr)
        end
 
        -- 终点
        task.end_wh_code = store_loc.wh_code
        task.end_area_code = store_loc.area_code
        task.end_loc_code = store_loc.code
 
        nRet, task = m3.CreateDataObj(strLuaDEID, task)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '创建任务失败!' .. task) end
        lua.Debug(strLuaDEID, debug.getinfo(1), 'task', task)
 
        -- 如果是二楼回库口的话,需要给回库口货位上锁,在国自返回 放货完成允许离开 状态时解锁货位
        if (tonumber(area.floor) == 2) then
            nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, task.end_loc_code,
                wms_base.Get_nConst(strLuaDEID, "锁类型-入库锁"), operation.cntr_code, operation.cntr_code,
                operation.op_def_name)
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_LockLocation 失败!" .. strRetInfo) end
        end
    end
 
    local label_crad
    strCondition = "S_SERIAL_NO = '" .. cg_detail.serial_no .. "'"
    nRet, label_crad = m3.GetDataObjByCondition(strLuaDEID, "GT_ROM", strCondition, "T_CREATE DESC")
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取流水号信息失败!" .. label_crad) end
 
    -- 下发任务给AGV
    data = {
        taskData = {
            taskNum = task.code,
            pickStation = task.start_loc_code,
            dropStation = task.end_loc_code,
            taskType = 2,
            carrierType = 1,
            priority = 1,
            wmsTaskNo = "",                 -- 出库才给
            level = "",                     -- 入库等级是默认的
            produceTime = label_crad.product_date,
            tyreType = "",
            supplier = label_crad.vendor,
            receiveLot = cg_detail.batch_no,
            subpool = "",
            source = "YCL"
        },
        partData = {
            rfid = task.cntr_code,
            lotNumber = 1,
            partNumber = cg_detail.item_code,
            partDesc = cg_detail.item_code,
            partType = "",
            weight = cg_detail.qty,
            unit = cg_detail.wu,
            maturityTime = "",
            productionTime = "",
            stewingTime = "",
            overdueTime = ""
        }
    }
 
    -- 调用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), "调用接口失败!" .. strRetInfo)
    end
 
    -- 设置状态未推送
    wms_task.SetStateByCode(strLuaDEID, task.code, "任务状态-已推送")
end