优化一下代码 String DEPTCODE032 = nullStr(map.get("DEPTCODE032")); String DEPTCODE033 = nullStr(map.get("DEPTCODE033")); String DEPTCODE034 = nullStr(map.get("DEPTCODE034")); String DEPTCODE047 = nullStr(map.get("DEPTCODE047")); String DEPTCODE048 = nullStr(map.get("DEPTCODE048")); String innercode = null; if(DEPTCODE048 != null){ innercode = "select innercode from bd_staff where shortname = '"+DEPTCODE047+"' and tenantId = '"+tenantId+"' and dr = 0"; } else{ if(DEPTCODE047 != null){ innercode = "select innercode from bd_staff where shortname = '"+DEPTCODE034+"' and tenantId = '"+tenantId+"' and dr = 0"; }else{ if(DEPTCODE034 != null){ innercode = "select innercode from bd_staff where shortname = '"+DEPTCODE033+"' and tenantId = '"+tenantId+"' and dr = 0"; }else{ if(DEPTCODE033 != null){ innercode = "select innercode from bd_staff where shortname = '"+DEPTCODE032+"' and tenantId = '"+tenantId+"' and dr = 0"; }else{ if(DEPTCODE032 != null){ innercode = "select innercode from bd_staff where code = '0' and tenantId = '"+tenantId+"' and dr = 0"; }else{ innercode = RDLetters(); } } } } }
时间: 2024-02-10 19:10:37 浏览: 58
可以使用一个循环和一个变量来避免这样的长串if-else语句,优化后的代码如下:
```
String innercode = null;
String[] deptCodes = {"DEPTCODE048", "DEPTCODE047", "DEPTCODE034", "DEPTCODE033", "DEPTCODE032"};
for (String deptCode : deptCodes) {
String shortName = nullStr(map.get(deptCode));
if (shortName != null) {
innercode = "select innercode from bd_staff where shortname = '" + shortName + "' and tenantId = '" + tenantId + "' and dr = 0";
break;
}
}
if (innercode == null) {
innercode = "select innercode from bd_staff where code = '0' and tenantId = '" + tenantId + "' and dr = 0";
}
```
这里首先定义了一个字符串数组deptCodes,包含了需要判断的5个部门代码。然后使用一个for循环遍历这个数组,找到第一个非空的部门代码对应的innercode并跳出循环。如果都没有找到非空的部门代码,则使用默认的innercode。这样就可以避免冗长的if-else语句,提高了代码的可读性和可维护性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)