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
-- 定义函数用于拆分字符串并转换为数字集合
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-122-27
   名称:
   作者:
   日期:2025-03-26
 
   函数: ClickOk
   功能:
 
   更改记录:
 
--]]
require("WMS-Equipment")
wms_cntr = require("wms_container")
wms_wh = require("wms_wh")
require("GT-Base")
require("GT_InAndOutboundPolicies")
 
function ClickOk(strLuaDEID)
    local nRet, strRetInfo
    -- 获取PDA参数
    nRet, strRetInfo = mobox.getCurEditDataObjAttr(strLuaDEID, "end_loc_code")
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "调用方法GetSysDataJson出错") end
    local obj_attrs = json.decode(strRetInfo)
    lua.Debug(strLuaDEID, debug.getinfo(1), 'obj_attrs', obj_attrs)
 
 
    local end_loc_code = obj_attrs[1].value
    if (end_loc_code == nil or end_loc_code == '') then
        mobox.setInfo(strLuaDEID, "终点不能为空!")
        return
    end
    
    -- 获取该巷道的 空货箱/托盘组 优先外深位其次内深位
    -- local str_cntr
    -- if (station_no == 'KQ_KTPPullCache1') then
    --     str_cntr = '106EGP'
    -- else
    --     str_cntr = '106EGR' -- 胶框前6个字符 托盘是106EGP
    -- end
            
    -- 通过站台位置获取终点
    end_loc_code        = wms_base.Get_sConst(strLuaDEID, end_loc_code)
 
    -- 计算起点
    local data
    local loc_Container = {}
    lua.Debug(strLuaDEID, debug.getinfo(1), '#loc_Container', #loc_Container)
    local flag       = 0
 
    local str
    nRet, str = StockInquiry(strLuaDEID)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), str) end
 
    -- 获取可出库巷道
    if (end_loc_code == wms_base.Get_sConst(strLuaDEID, "KL_ReturnRgvStation1") or
    end_loc_code == wms_base.Get_sConst(strLuaDEID, "F3L_InWeight1") or
    end_loc_code == wms_base.Get_sConst(strLuaDEID, "F3R_InWeight1")) then
        local b = wms_base.Get_sConst(strLuaDEID, "佳通-一楼左侧回库口巷道")
        str = findCommonNumbers(str, b)
    end
 
    local roadway_list = lua.split(str, ",")
 
    for i = 1, #roadway_list do
        local roadway
        -- 获取当前巷道信息
        local strCondition = "N_ROADWAY = '" .. roadway_list[i] .. "'"
        nRet, roadway = m3.GetDataObjByCondition(strLuaDEID, "Roadway", strCondition)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取【巷道】信息失败! " .. roadway) end
 
        local strOrder = "T_CREATE"
 
        -- 获取外深位的空工装
        strCondition =
            "S_LOC_CODE IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE = 'LK' AND N_ROADWAY = " ..
            roadway_list[i] .. " AND N_LOCK_STATE = 0 AND C_ENABLE = 'Y' AND N_POS = 1)"
        strCondition = strCondition ..
            " AND S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = 'KGZ')"
                -- AND S_CNTR_CODE LIKE '" .. str_cntr .. "%')"
        -- 最多获取 10 条
        nRet, strRetInfo = mobox.queryDataObjAttr3(strLuaDEID, "Loc_Container", strCondition, 1, strOrder)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1),
                "获取货位信息错误! " .. strRetInfo .. " SQL条件: " .. strCondition)
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), 'strCondition1', strCondition)
        lua.Debug(strLuaDEID, debug.getinfo(1), "strRetInfo1", strRetInfo)
        if (strRetInfo ~= '') then
            local attrs
            local retObjs = json.decode(strRetInfo)
            nRet, attrs = m3.ObjAttrStrToLuaObj("Loc_Container", lua.table2str(retObjs[1].attrs))
            if (nRet ~= 0) then
                lua.Error(strLuaDEID, debug.getinfo(1),
                    "m3.ObjAttrStrToLuaObj(CG_Detail) 失败! " .. attrs)
            end
            data = {
                loc_code = attrs.loc_code,
                cntr_code = attrs.cntr_code,
                roadway = roadway.roadway
            }
            break
        end
 
        -- 获取内深位的空工装
        strCondition =
            "S_LOC_CODE IN (SELECT S_CODE FROM TN_Location WHERE S_AREA_CODE = 'LK' AND N_ROADWAY = " ..
            roadway_list[i] .. "  AND N_LOCK_STATE = 0 AND C_ENABLE = 'Y' AND N_POS = 2)"
        strCondition = strCondition ..
            " AND S_CNTR_CODE IN (SELECT S_CNTR_CODE FROM TN_CG_Detail WHERE S_ITEM_CODE = 'KGZ')"
                --AND S_CNTR_CODE LIKE '" ..str_cntr .. "%')"
        -- 最多获取 10 条
        nRet, strRetInfo = mobox.queryDataObjAttr3(strLuaDEID, "Loc_Container", strCondition, 1, strOrder)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1),
                "获取货位信息错误! " .. strRetInfo .. " SQL条件: " .. strCondition)
        end
        lua.Debug(strLuaDEID, debug.getinfo(1), 'strCondition2', strCondition)
        lua.Debug(strLuaDEID, debug.getinfo(1), "strRetInfo2", strRetInfo)
        if (strRetInfo ~= '') then
            local attrs
            local retObjs = json.decode(strRetInfo)
            for j = 1, #retObjs do
                flag = tonumber(flag) + 1
 
                nRet, attrs = m3.ObjAttrStrToLuaObj("Loc_Container", lua.table2str(retObjs[j].attrs))
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1),
                        "m3.ObjAttrStrToLuaObj(CG_Detail) 失败! " .. attrs)
                end
 
                loc_Container[flag] = attrs
            end
        end
        ::coroutine::
    end
 
    local cntr_code
    local start_loc
    flag = 0
    lua.Debug(strLuaDEID, debug.getinfo(1), '#loc_Container2', #loc_Container)
    if (data ~= nil) then
        lua.Debug(strLuaDEID, debug.getinfo(1), "data", data)
        cntr_code = data.cntr_code
        start_loc = data.loc_code
    elseif (tonumber(#loc_Container) > tonumber(0)) then
        lua.Debug(strLuaDEID, debug.getinfo(1), "loc_Container", loc_Container)
        for i = 1, #loc_Container do
            -- 内深位校验
            local success, loc_code = GetPosLoc(strLuaDEID, loc_Container[i].loc_code)
            if (success == true) then
                -- 判断内深位是否有货物
                local strCondition = "S_CODE = '" .. loc_code .. "' AND N_LOCK_STATE = 0 AND N_CURRENT_NUM = 0"
                nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Location", strCondition)
                if (nRet == 2) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. strRetInfo)
                elseif (nRet == 1) then
                    goto coroutine
                end
 
                cntr_code = loc_Container[i].cntr_code
                start_loc = loc_Container[i].loc_code
                break
            end
            ::coroutine::
        end
        if (start_loc == nil or start_loc == '') then
            lua.Error(strLuaDEID, debug.getinfo(1), '巷道没有可出库的空工装!')
        end
    else
        lua.Error(strLuaDEID, debug.getinfo(1), '巷道没有可出库的空工装!')
    end
    nRet, start_loc = wms_wh.GetLocInfo(start_loc)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), 'WMS_GetLocInfo失败!' .. start_loc) end
 
    -- 获取终点
    local end_loc
    nRet, end_loc = wms.wms_GetLocBaseInfo(end_loc_code)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取货位基本信息失败!") end
    end_loc = json.decode(end_loc)
    lua.Debug(strLuaDEID, debug.getinfo(1), '终点货位信息', end_loc)
 
    -- step4: 创建搬运作业
    local operation           = m3.AllocObject(strLuaDEID, "Operation")
    operation.start_wh_code   = start_loc.wh_code
    operation.start_area_code = start_loc.area_code
    operation.start_loc_code  = start_loc.code
 
    -- 终点是佳通WMS传过来的
    operation.end_wh_code     = end_loc.wh_code
    operation.end_area_code   = end_loc.area_code
    operation.end_loc_code    = end_loc.code
 
    operation.cntr_code       = cntr_code
    operation.op_def_name     = "呼叫空托"
    operation.op_type         = wms_base.Get_nConst(strLuaDEID, "作业类型-出库")
    nRet, operation           = m3.CreateDataObj(strLuaDEID, operation)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '创建【作业】失败!' .. operation) end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'operation', operation)
 
    mobox.setInfo(strLuaDEID, "操作成功!")
end