找到第一个非空的部门代码后要拿下一个非空的部门代码然后拼接sql
时间: 2024-03-09 16:48:58 浏览: 21
好的,根据您的需求,可以在找到第一个非空的部门代码后,继续从下一个部门代码开始查找,直到找到第二个非空的部门代码,然后将两个部门代码拼接在一起作为查询innercode的条件。优化后的代码如下:
```
String innercode = null;
String[] deptCodes = {"DEPTCODE048", "DEPTCODE047", "DEPTCODE034", "DEPTCODE033", "DEPTCODE032"};
String shortName1 = null;
String shortName2 = null;
for (String deptCode : deptCodes) {
String shortName = nullStr(map.get(deptCode));
if (shortName != null) {
if (shortName1 == null) {
shortName1 = shortName;
} else {
shortName2 = shortName;
break;
}
}
}
if (shortName1 != null) {
if (shortName2 != null) {
innercode = "select innercode from bd_staff where shortname in ('" + shortName1 + "', '" + shortName2 + "') and tenantId = '" + tenantId + "' and dr = 0";
} else {
innercode = "select innercode from bd_staff where shortname = '" + shortName1 + "' and tenantId = '" + tenantId + "' and dr = 0";
}
} else {
innercode = "select innercode from bd_staff where code = '0' and tenantId = '" + tenantId + "' and dr = 0";
}
```
这里定义了两个变量shortName1和shortName2,用于存储第一个和第二个非空的部门代码对应的shortName。在找到第一个非空的部门代码后,判断shortName1是否为空,如果为空则将当前的shortName赋值给shortName1,否则将当前的shortName赋值给shortName2并跳出循环。然后根据shortName1和shortName2是否为空,拼接不同的innercode。如果shortName1为空,则使用默认的innercode。