fy36
2025-07-01 350eb5ec9163d3ea21416b1525bb80191e958071
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
--[[
    编码: JX-115-10
    名称: 空料箱初始化-导入
    作者:HAN    
    日期:2025-1-29    
    入口函数: Import
 
    功能说明:
 
    变更历史:
 
--]]
wms_base = require ("wms_base")
wms_cntr= require( "wms_container" )
 
function Import(strLuaDEID)
    local nRet, strRetInfo, strNo
    local cntr_spec = {"A","B","C","D","","E"}
    -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...]
    local row_data = {}
    nRet, row_data = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0 or strRetInfo == '') then lua.Error( strLuaDEID, debug.getinfo(1), "无法获取导入数据!") end
 
    local row_attrs
    local n, nCount, nRows, nIndex
    local cntr_type, max_cell_num
 
    -- 导入是可分多页分批导入, 因此每次的起始行不一样
    nRet, strRetInfo = mobox.getGlobalAttrValue( strLuaDEID, "start_row" )
    if ( nRet ~= 0 ) then
        mobox.error( strLuaDEID, "获取批量导入全局参数 start_row 失败!"..strRetInfo )
        return
    end    
    local nStartRow = tonumber( strRetInfo )
    local err_msg_list = {}                          -- 用于写导入错误信息
    local str_err_msg
    local empty_box, cntr
    local strCondition, strUpdateSql
 
    -- 步骤1 获取从excel导入的一行数据
    nRow = nStartRow
    for row = 1, #row_data do
        row_attrs = row_data[row]
 
        -- 初始化空料箱对象
        empty_box = m3.AllocObject(strLuaDEID,"JX_ASRS_EmptyBox_In")
        str_err_msg = ''
        for n = 1, #row_attrs do
            strAttr = row_attrs[n].attr
            strValue = row_attrs[n].value
            if (strAttr ~= '') then
                if (strAttr == "容器编码") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end
                    empty_box.cntr_code = strValue
                elseif (strAttr == "来源系统") then
                    if (strValue == '') then 
                        str_err_msg = strAttr .. "不能为空!"
                        goto err_msg_process
                    end                  
                    empty_box.source_sys = strValue
                elseif (strAttr == "料格数") then
                    empty_box.max_cell_num = lua.StrToNumber( strValue )  
                                
                end
            end
        end
        -- 判断导入的数据是否完整
        if ( empty_box.source_sys == "巨沃" ) then
            if ( empty_box.max_cell_num < 1 or empty_box.max_cell_num > 6 or empty_box.max_cell_num == 5 ) then  
                str_err_msg = "料格数量不合法!"
                goto err_msg_process
            end  
        end        
 
        if ( str_err_msg == '' ) then
            -- 判断料箱是否存在
            if ( wms_cntr.Exist( strLuaDEID, empty_box.cntr_code ) == false ) then
                -- 没有容器需要创建
                nRet, empty_box = m3.CreateDataObj(strLuaDEID, empty_box)
                if (nRet ~= 0 ) then 
                    str_err_msg = "创建空料箱初始化对象失败!"..empty_box 
                    goto err_msg_process
                end  
 
                cntr = m3.AllocObject(strLuaDEID,"Container")
                cntr.code = empty_box.cntr_code
                if ( empty_box.source_sys == "巨沃" ) then
                    cntr.type = 3           -- 料箱
                    cntr.spec = cntr_spec[empty_box.max_cell_num]
                    cntr.max_cell_num = empty_box.max_cell_num
                    cntr.source = "巨沃"
                else
                    cntr.type = 2
                    cntr.source = "巨星"
                end
                nRet, cntr = m3.CreateDataObj(strLuaDEID, cntr)
                if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "创建料箱对象失败!"..cntr ) end                  
            else
                -- 容器已经存在需要判断容器是否和货位有绑定
                strCondition = "S_CNTR_CODE = '"..empty_box.cntr_code.."'"
                nRet, strRetInfo = mobox.existThisData( strLuaDEID, "CG_Detail", strCondition )
                if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo )  end
                -- 如果该车辆编码的动作已经在队列,返回,不做处理
                if ( strRetInfo ~= "no" ) then 
                    str_err_msg = "料箱'"..empty_box.cntr_code.."'还存在绑定的货品!" 
                    goto err_msg_process                    
                end      
                nRet, strRetInfo = mobox.existThisData( strLuaDEID, "Loc_Container", strCondition )
                if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), strRetInfo )  end
                -- 如果该车辆编码的动作已经在队列,返回,不做处理
                if ( strRetInfo ~= "no" ) then 
                    str_err_msg = "料箱'"..empty_box.cntr_code.."'还存在绑定的货位!" 
                    goto err_msg_process                    
                end 
                
                nRet, empty_box = m3.CreateDataObj(strLuaDEID, empty_box)
                if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "创建空料箱初始化对象失败!"..empty_box ) end
 
                -- 更新容器基本信息
                if ( empty_box.source_sys == "巨沃" ) then
                    cntr_type = 3           -- 料箱
                    cntr_spec = cntr_spec[empty_box.max_cell_num]
                    max_cell_num = empty_box.max_cell_num
                else
                    cntr_type = 2
                    cntr_spec = ""
                    max_cell_num = 0
                end                
                strUpdateSql = "N_TYPE = "..cntr_type..", S_SPEC = '"..cntr_spec.."', N_MAX_CELL_NUM = "..max_cell_num..", "..
                               "N_EMPTY_CELL_NUM = "..max_cell_num..", N_ALLOC_CELL_NUM = 0, S_POSITION = '', S_SOURCE = '"..empty_box.source_sys.."',"..
                               "N_EMPTY_FULL = 0, N_LOCK_STATE = 0, N_B_STATE = 0, C_ENABLE = 'Y', F_GOOD_WEIGHT = 0"
                strCondition = "S_CODE = '"..empty_box.cntr_code.."'"
                nRet, strRetInfo = mobox.updateDataAttrByCondition( strLuaDEID, "Container", strCondition, strUpdateSql )
                if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1),  "更新料箱信息失败!"..strRetInfo ) end                   
            end            
        end
 
        ::err_msg_process::
        if ( str_err_msg ~= '' ) then
            local err_msg_row = {
                row_no = nRow,
                err_msg = str_err_msg
            }
            table.insert( err_msg_list, err_msg_row )            
        end
        nRow = nRow + 1
    end
    -- 把导入过程中的错误信息返回到前端
    local import_fail = {
        result = {
            fail = err_msg_list
        }
    }
    mobox.returnValue( strLuaDEID,1, lua.table2str(import_fail) )
    lua.Debug( strLuaDEID, debug.getinfo(1), "import_fail", import_fail )    
end