@PostMapping("addList") public AjaxResult addList(@RequestBody List<TblSmtzInfo> tblSmtzInfo, HttpServletRequest request) { if (FskAppKey.equals(request.getHeader("appKey")) && FskAppSecret.equals(request.getHeader("appSecret"))) { for (TblSmtzInfo t : tblSmtzInfo) { t.setCreatedTime(new DateTime()); t.setAppSecret("福寿康"); } return AjaxResult.success(this.tblSmtzInfoService.saveBatch(tblSmtzInfo)); } else if (YjAppKey.equals(request.getHeader("appKey")) && YjAppSecret.equals(request.getHeader("appSecret"))) { for (TblSmtzInfo t : tblSmtzInfo) { t.setCreatedTime(new DateTime()); t.setAppSecret("颐家"); } return AjaxResult.success(this.tblSmtzInfoService.saveBatch(tblSmtzInfo)); } else { return AjaxResult.error(); } }优化代码
时间: 2024-03-20 07:44:45 浏览: 87
可以考虑将重复的代码抽取到一个方法中,减少代码冗余。例如:
```
@PostMapping("addList")
public AjaxResult addList(@RequestBody List<TblSmtzInfo> tblSmtzInfo, HttpServletRequest request) {
String appKey = request.getHeader("appKey");
String appSecret = request.getHeader("appSecret");
String name = "";
if (FskAppKey.equals(appKey) && FskAppSecret.equals(appSecret)) {
name = "福寿康";
} else if (YjAppKey.equals(appKey) && YjAppSecret.equals(appSecret)) {
name = "颐家";
} else {
return AjaxResult.error();
}
for (TblSmtzInfo t : tblSmtzInfo) {
t.setCreatedTime(new DateTime());
t.setAppSecret(name);
}
return AjaxResult.success(this.tblSmtzInfoService.saveBatch(tblSmtzInfo));
}
```
这样代码更加简洁,也减少了重复代码的出错风险。
阅读全文