Jianw
2025-05-13 3b39fe3810c3ee2ec9ec97236c1769c5c85e062c
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
--[[
    编码: WMS-30-12
    名称: 货位组-检查
    版本: Version 1.0
    日期: 2025-1-29
    创建人:   HAN
 
    功能:
        WMS 一些基础数据直接会影响后面的业务逻辑,因此需要做一些数据校验
        检验数据包括:
        Location_Group
              -- N_CAPACITY, N_CURRENT_NUM, N_OUT_LOCK_NUM, S_ITEM_CODE,...
 
        -- WMS_DataCheck_Loaction_Group  货位组
 
--]]
wms_check = require("wms_datacheck")
 
local function upadte_location_group( strLuaDEID, id, strSetAttr, strCheckResult )
    local nRet, strRetInfo
 
    local curTime = os.date("%Y-%m-%d %H:%M:%S Checked -> ")
    if ( strCheckResult == '' ) then strCheckResult = "OK" end
    strCheckResult = curTime..strCheckResult
    strSetAttr = strSetAttr.."S_CHECK_RESULT = '"..strCheckResult.."'"
 
    strCondition = "S_ID = '"..id.."'"
    nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Location_Group", strCondition, strSetAttr )
    if ( nRet ~= 0 ) then  
        lua.Debug( strLuaDEID, debug.getinfo(1), "更新【货位组】失败!", strRetInfo )
        return 1, "更新【货位组】失败! 见调试日志"        
    end
    return 0, "ok"
end
 
--[[
    校验货位组属性的正确性
--]]
function WMS_DataCheck_Loaction_Group( strLuaDEID, id )
    local nRet, strValue
 
    if (id == '' or id == nil ) then
        return 1, "WMS_DataCheck_Loaction_Group 参数id必须有值!"
    end
    -- 把GUID {xxxx-xxxx} 改成 xxxx-xxxx
    id = trim_guid_str(id)
 
    local data_obj
    nRet, data_obj = m3.GetDataObject( strLuaDEID, "Location_Group", id ) 
    if ( nRet ~= 0 )  then 
        lua.Debug( strLuaDEID, debug.getinfo(1), "GetDataObject失败!", data_obj )
        return 1, "GetDataObject失败! 见调试日志" 
    end 
 
    -- 首先需要获取 货位组 有哪些货位组成
    local loc_set = {}
    local strOrder = ''
    local strLocCondition = "S_AREA_CODE ='"..data_obj.area_code.."' AND N_ROW_GROUP = "..data_obj.row_group.." AND N_COL = "..data_obj.col.." AND N_LAYER = "..data_obj.layer
    nRet, strRetInfo = mobox.queryDataObjAttr( strLuaDEID, "Location", strLocCondition, strOrder )
    if ( nRet ~= 0 ) then 
        lua.Debug( strLuaDEID, debug.getinfo(1), "获取【Location】失败!", strLocCondition )
        return 1, "获取【Location】失败! 见调试日志"
    end
 
    local ext_data = {}
    ext_data.id = id
    ext_data.cls = "Location_Group"
    local strSetAttr = ''
    local strCheckResult = ''           -- 校验结果
 
    if ( strRetInfo == '' ) then 
        -- 201 无效数据
        wms_base.Warning( strLuaDEID, 2, 201, "无效货位组,建议删除!", table2str(ext_data), "", "DataCheck" )
        nRet, strRetInfo = upadte_location_group( strLuaDEID, id, "", "无效货位组,建议删除!" )
        return nRet, strRetInfo
    end
 
    local retObjs = json.decode( strRetInfo )
    local n
    local capacity = 0
    local cur_num = 0
    local in_lock_num = 0
    local out_lock_num = 0
    local loc = {}
 
    -- 获取同一个货位组的货位信息,重新计算 capacity 等数值量
    for n = 1, #retObjs do
        nRet, loc = m3.ObjAttrStrToLuaObj( "Location", table2str(retObjs[n].attrs) )
        if ( nRet ~= 0 ) then 
            lua.Debug( strLuaDEID, debug.getinfo(1), "ObjAttrStrToLuaObj(Location) 失败!", loc )
            return 1, "ObjAttrStrToLuaObj(Location) 失败! 见调试日志"
        end  
        capacity = capacity + loc.capacity
        cur_num = cur_num + loc.cur_num
        -- 1 入库锁  2 出库锁
        if ( loc.lock_state == 1 ) then 
            in_lock_num = in_lock_num + 1
        elseif ( loc.lock_state == 2 ) then 
            out_lock_num = out_lock_num + 1
        end
    end   
 
    if ( data_obj.capacity ~= capacity ) then
        strSetAttr = strSetAttr.."N_CAPACITY = "..capacity..","
        strCheckResult = strCheckResult.."容量:("..data_obj.capacity.." -> "..capacity.."),"
    end
 
    if ( data_obj.cur_num ~= cur_num ) then
        strSetAttr = strSetAttr.."N_CURRENT_NUM = "..cur_num..","
        strCheckResult = strCheckResult.."容器数量:("..data_obj.cur_num.." -> "..cur_num.."),"
    end
    
    if ( data_obj.in_lock_num ~= in_lock_num ) then
        strSetAttr = strSetAttr.."N_IN_LOCK_NUM = "..in_lock_num..","
        strCheckResult = strCheckResult.."入库锁数量:("..data_obj.in_lock_num.." -> "..in_lock_num.."),"
    end
    
    if ( data_obj.out_lock_num ~= out_lock_num ) then
        strSetAttr = strSetAttr.."N_OUT_LOCK_NUM = "..out_lock_num..","
        strCheckResult = strCheckResult.."出库锁数量:("..data_obj.out_lock_num.." -> "..out_lock_num.."),"
    end    
 
    -- 获取货位组里的存储的货品信息
    local strCondition = "S_CNTR_CODE IN ( Select S_CNTR_CODE From TN_Loc_Container Where S_LOC_CODE In ( Select S_CODE From TN_Location "
    strCondition = strCondition.." Where "..strLocCondition.."))"
    local strOrder = "S_CNTR_CODE"
    nRet, strRetInfo = mobox.queryDataObjAttr( strLuaDEID, "CG_Detail", strCondition, strOrder )
    if ( nRet ~= 0 ) then  
        lua.Debug( strLuaDEID, debug.getinfo(1), "获取【CG_Detail】失败!", strCondition )
        return 1, "获取【CG_Detail】失败! 见调试日志"
    end 
 
    if ( strRetInfo ~= '' ) then
        local cg_detail
        local item_code = ''
        local item_name = ''
        local batch_no = ''
        local owner = ''
        local end_user = ''
        local supplier = ''
 
        retObjs = json.decode( strRetInfo )
        for n = 1, #retObjs do
            nRet, cg_detail = m3.ObjAttrStrToLuaObj( "CG_Detail", table2str(retObjs[n].attrs) )
            if ( nRet ~= 0 ) then 
                lua.Debug( strLuaDEID, debug.getinfo(1), "ObjAttrStrToLuaObj(CG_Detail) 失败!", loc )
                return 1, "ObjAttrStrToLuaObj(CG_Detail)) 失败! 见调试日志"
            end 
            if ( n == 1 ) then
                item_code = cg_detail.item_code
                item_name = cg_detail.item_name
                batch_no = cg_detail.batch_no
                owner = cg_detail.owner
                end_user = cg_detail.end_user
                supplier = cg_detail.supplier
            else
                if ( item_code ~= cg_detail.item_code ) then item_code = '' end
                if ( item_name ~= cg_detail.item_name ) then item_name = '' end
                if ( batch_no ~= cg_detail.batch_no ) then batch_no = '' end
                if ( owner ~= cg_detail.owner ) then owner = '' end
                if ( end_user ~= cg_detail.end_user ) then end_user = '' end
                if ( supplier ~= cg_detail.supplier ) then supplier = '' end
            end 
        end  
        
        if ( data_obj.item_code ~= item_code ) then
            strSetAttr = strSetAttr.."S_ITEM_CODE = '"..item_code.."',"
            strCheckResult = strCheckResult..'物料编码:("'..data_obj.item_code..'" -> "'..item_code..'"),'
        end
        if ( data_obj.item_name ~= item_name ) then
            strSetAttr = strSetAttr.."S_ITEM_NAME = '"..item_name.."',"
            strCheckResult = strCheckResult..'物料名称:("'..data_obj.item_name..'" -> "'..item_name..'"),'
        end
        if ( data_obj.batch_no ~= batch_no ) then
            strSetAttr = strSetAttr.."S_BATCH_NO = '"..batch_no.."',"
            strCheckResult = strCheckResult..'批次号:("'..data_obj.batch_no..'" -> "'..batch_no..'"),'
        end
        if ( data_obj.owner ~= owner ) then
            strSetAttr = strSetAttr.."S_OWNER = '"..owner.."',"
            strCheckResult = strCheckResult..'货主:("'..data_obj.owner..'" -> "'..owner..'"),'
        end
        if ( data_obj.end_user ~= end_user ) then
            strSetAttr = strSetAttr.."S_ITEM_CODE = '"..end_user.."',"
            strCheckResult = strCheckResult..'使用单位:("'..data_obj.end_user..'" -> "'..end_user..'"),'
        end
        if ( data_obj.supplier ~= supplier ) then
            strSetAttr = strSetAttr.."S_ITEM_CODE = '"..supplier.."',"
            strCheckResult = strCheckResult..'供应商:("'..data_obj.supplier..'" -> "'..supplier..'"),'
        end     
    end
 
    nRet, strRetInfo = upadte_location_group( strLuaDEID, id, strSetAttr, strCheckResult )
    return nRet, strRetInfo
end
 
function Test( strLuaDEID )
    local nRet, strRetInfo
 
    local strOrder = "S_CODE"
    nRet, strRetInfo = mobox.queryDataObjAttr( strLuaDEID, "Location_Group", "", strOrder )
    if ( nRet ~= 0 or strRetInfo == '') then 
        lua.Error( strLuaDEID, debug.getinfo(1), "获取【Location_Group】失败!"..strRetInfo )
    end 
    local n
    local retObjs
    retObjs = json.decode( strRetInfo )
 
    for n = 1, #retObjs do
        nRet, strRetInfo = wms_check.Loaction_Group( strLuaDEID, retObjs[n].id )
        if ( nRet ~= 0 ) then 
            lua.Error( strLuaDEID, debug.getinfo(1), "WMS_DataCheck_Loaction_Group 失败!"..strRetInfo )
        end
    end
end