1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
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
--[[
   编码: JX-19-20
   名称: 
   作者:
   日期:2025-03-25
 
   函数: Query
   功能:
 
   更改记录:
 
--]]
json  = require ("json")
mobox = require ("OILua_JavelinExt")
m3 = require("oi_base_mobox")
lua   = require ("oi_base_func")
 
function Query(strLuaDEID)
    local nRet, strRetInfo
    local parameter = {}
    local attrs
 
    -- 获取查询面板里的输入属性
    nRet, attrs = m3.GetSysInputParameter(strLuaDEID)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "GetSysInputParameter 失败! " .. attrs)
    end
    parameter = m3.KeyValueAttrsToObjAttr(attrs)
 
    lua.Debug(strLuaDEID, debug.getinfo(1), "parameter", parameter)
    local item_code = lua.Get_StrAttrValue(parameter.S_ITEM_CODE)                   -- 物料编码
    local cntr_code = lua.Get_StrAttrValue(parameter.S_CNTR_CODE)                   -- 料箱编码
    local code = lua.Get_StrAttrValue(parameter.S_CODE)                             -- 拣料框号
    local item_name = lua.Get_StrAttrValue(parameter.S_ITEM_NAME)                   -- 商品名称
    
 
    -- 条件构建(重点修改部分)
    local conditions = {}
    
    -- 1. 处理Z开头条件(完美解决方案)
    if code == '有' then
        table.insert(conditions, "SUBSTR(S_CNTR_CODE,1,1) = 'Z'")  -- 更可靠的写法
    elseif code == '无' then
        table.insert(conditions, "(SUBSTR(S_CNTR_CODE,1,1) <> 'Z' OR S_CODE IS NULL)")
    end
 
    -- 2. 添加其他条件
    if cntr_code ~= "" then
        table.insert(conditions, "S_CNTR_CODE = '"..cntr_code.."'")
    end
    if item_code ~= "" then
        table.insert(conditions, "S_ITEM_CODE = '"..item_code.."'")
    end
    if item_name ~= "" then
        table.insert(conditions, "S_ITEM_NAME = '"..item_name.."'")
    end
 
    -- 3. 构建最终SQL(安全方式)
    local strSQL = ""
    if #conditions > 0 then
        strSQL = table.concat(conditions, " AND ")
        -- 特殊处理:确保语法兼容性
        strSQL = string.gsub(strSQL, "<>", "!=")  -- 统一不等号语法
    end
 
    -- 调试输出
    lua.Debug(strLuaDEID, debug.getinfo(1), "最终查询条件:", strSQL)
    
    -- 设置查询条件
    local action = {}
    action[1] = {
        action_type = "set_query_condition",
        value = {
            condition = strSQL,
            order = "T_CREATE DESC"
        }
    }
 
    nRet, strRetInfo = mobox.setAction(strLuaDEID, lua.table2str(action))
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "setAction失败! " .. strRetInfo .. ' action = ' .. lua.table2str(action))
    end
end