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
--[[
     编码: GT-40-59
     名称: 作业启动-采购退货
     作者: LZH
     入口函数:OperationStart
     功能说明: 采购退货是直接出库到 固定机台ABC编码维护的 出库库区或货位,如果是立库出库则创建一段国自任务
            如果是粉料退购则创建一段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, strRetInfo, store_loc
 
    -- 获取作业对象
    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 task_no = ext_data.task_no         -- 任务号
    local delivery_no = ext_data.delivery_no -- 业务单号为 出库单号
    local mes_task_no = ext_data.mes_task_no
    local wmsLot = ext_data.wmsLot
 
    -- 根据作业的起点库区判断作业类型
    local schedule_type
    if (operation.start_area_code == 'LK') then
        schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-AGV") -- 设置调度类型
    else
        schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-国自") -- 设置调度类型
    end
 
    -- 判断该巷道的任务是否超过阙值,如果超过则将作业设置为等待状态并将错误信息设置到页面
    nRet, store_loc = wms_wh.GetLocInfo(operation.start_loc_code)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), store_loc) end
 
    -- 获取某种调度类型的任务数量
    local strCondition = "N_SCHEDULE_TYPE = " ..
        schedule_type .. " AND ( N_B_STATE = " .. wms_base.Get_nConst(strLuaDEID, "任务状态-已推送")
    strCondition = strCondition ..
        " OR N_B_STATE = " .. wms_base.Get_nConst(strLuaDEID, "任务状态-执行") .. ") AND N_ROADWAY =" .. store_loc.roadway
    nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "Task", strCondition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo) end
    if (tonumber(strRetInfo) > tonumber(wms_base.Get_sConst(strLuaDEID, "Task-最大任务数"))) then
        local msg
        msg = "作业编码为=" .. operation.code .. " 创建任务失败, 巷道" .. store_loc.roadway .. "的任务数量已超过2个"
        lua.Warning(strLuaDEID, debug.getinfo(1), msg)
        lua.Wait(strLuaDEID, msg)
        return
    end
 
    -- 创建搬运作业
    local task = m3.AllocObject(strLuaDEID, "Task")
    task.op_code = operation.code    -- 作业编码
    task.op_name = operation.op_def_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
 
    -- 设置搬运类型和调度类型
    if (operation.start_area_code ~= 'LK') then
        -- 获取起点对应的回库口
        local end_loc
        if (operation.start_area_code == 'PFL' or operation.start_area_code == 'HWPFL') then
            nRet, end_loc = wms_wh.GetLocInfo("THREE-BGHHK-01")
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取终点信息失败!" .. end_loc) end
        elseif (operation.start_area_code == 'TFL' or operation.start_area_code == 'HWTFL') then
            nRet, end_loc = wms_wh.GetLocInfo("THREE-QGHHK-01")
            if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取终点信息失败!" .. end_loc) end
        else
            lua.Error(strLuaDEID, debug.getinfo(1), "未定义的采购退货流程!")
        end
        -- 终点
        task.end_wh_code = end_loc.wh_code
        task.end_area_code = end_loc.area_code
        task.end_loc_code = end_loc.code
        task.roadway = end_loc.roadway -- 添加巷道
        task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-AGV出库搬运")
        task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-AGV") -- 设置调度类型
    else
        -- 终点
        task.end_wh_code = operation.end_wh_code
        task.end_area_code = operation.end_area_code
        task.end_loc_code = operation.end_loc_code
        task.roadway = store_loc.roadway -- 添加巷道
        task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-立库出库搬运")
        task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-国自") -- 设置调度类型
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), "任务创建前信息", task)
    nRet, task = m3.CreateDataObj(strLuaDEID, task)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "创建AGV入库任务失败!" .. task)
    end
 
 
    local data = {}
    local strBody = {}
    local url, strHeader
    -- 通过容器获取物料信息
    local cg_detail_list, cg_detail
    nRet, cg_detail_list = wms_cntr.Get_Container_Goods(strLuaDEID, 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
 
    local label_crad
    strCondition = "S_SERIAL_NO = '" .. cg_detail.serial_no .. "'"
    if (tonumber(cg_detail.is_tl) == 1) then
        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
    else
        nRet, label_crad = m3.GetDataObjByCondition(strLuaDEID, "GT_Label_Crad", strCondition, "T_CREATE DESC")
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取流水号信息失败!" .. label_crad) end
    end
 
    -- 获取入库单据头信息
    local incoming_Info
    strCondition = "S_DELIVERY_NO = '" ..
        label_crad.delivery_no .. "' AND N_DELIVERY_ROW_NO = '" .. label_crad.delivery_row_no .. "'"
    nRet, incoming_Info = m3.GetDataObjByCondition(strLuaDEID, "GT_Incoming_Info", strCondition, "T_CREATE DESC")
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取入库单据头信息失败!") end
 
    --根据搬运任务类型组装任务下发参数
    if (task.type == wms_base.Get_nConst(strLuaDEID, "任务类型-AGV出库搬运")) then
        -- 下发任务给AGV
        data = {
            taskData = {
                taskNum = task.code,
                pickStation = task.start_loc_code,
                dropStation = task.end_area_code,
                taskType = 2,
                carrierType = 1,
                priority = 1,
                wmsTaskNo = mes_task_no,
                level = incoming_Info.level,
                produceTime = incoming_Info.product_date,
                tyreType = "",
                supplier = incoming_Info.vendor,
                receiveLot = cg_detail.batch_no,
                subpool = "",
                source = "YCL",
                wmsLot = wmsLot
            },
            partData = {
                rfid = task.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
            }
        }
        lua.Debug(strLuaDEID, debug.getinfo(1), "data", data)
        -- 调用AGV任务下发
        url = wms_base.Get_sConst(strLuaDEID, "AGV-url")
        url = url .. "/CreateTask"
        strBody = data
        nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, url, strHeader, strBody, "AGV", "任务创建")
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "调用WCS接口失败!" .. strRetInfo)
        end
    else
        -- 出库不指定具体终点,有WCS反馈终点,获取出库口库区绑定的WCS站点
        local condition = "S_VALUE = '" .. task.end_area_code .. "-01" .. "'"
        nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取【常量】信息失败! " .. strRetInfo) end
        local loc_code_zd = strRetInfo.name
 
        -- 判断起点是否是内深位,如果起点是内深位则获取对应的外深位
        local pre_task_no = ''
        local success, loc_code = GetPosLoc(strLuaDEID, operation.start_loc_code)
        if (success == true) then
            -- 判断外深位是否存在未完成的任务,如果有则将这个任务号输入到 WCS参数中的前置任务号中
            strCondition = "S_START_LOC = '" .. loc_code .. "' AND N_B_STATE IN (0,1,2)"
            nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Task", strCondition)
            if (nRet == 2) then
                lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. strRetInfo)
            elseif (nRet == 0) then
                pre_task_no = strRetInfo.code
            end
        end
 
        local strCode = lua.guid() -- 生产一个GUID字符串
        local str_day_time = os.date("%Y-%m-%d %H:%M:%S")
        data = {
            req_no = strCode,
            task_type = 2, -- 1=货物入库;2=货物出库;3=托盘组入库;4=托盘组出库;5=移动(不过库位);6=移库
            task_no = task.code,
            tunnel_no = store_loc.roadway,
            from_pos = task.start_loc_code,
            to_pos = loc_code_zd,
            mat_code = task.cntr_code,
            mat_type = cg_detail.item_code,
            mat_memo = cg_detail.item_name,
            req_time = str_day_time,
            pre_task_no = pre_task_no
        }
 
        url = wms_base.Get_sConst(strLuaDEID, "WCS-url")
        url = url .. "/create"
        strBody[1] = data
        nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, url, strHeader, strBody, "WCS", "任务创建")
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "调用WCS接口失败!" .. strRetInfo)
        end
    end
 
    -- 设置状态未推送
    wms_task.SetStateByCode(strLuaDEID, task.code, "任务状态-已推送")
    -- 任务的起点加上锁
    nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, task.start_loc_code,
        wms_base.Get_nConst(strLuaDEID, "锁类型-出库锁"),
        task.code, operation.code, operation.op_def_name)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_LockLocation 失败!" .. strRetInfo) end
end