1添加一个设备 PostMappingaddOneDeviceEntity public ResultObject addOneDeviceEntityRequestBody MapStringObject params HttpServletRequest request 权限验证 String token = StringrequestgetAttri
<p>以下是优化后的代码:</p>
<p>@PostMapping("/addOneDeviceEntity")
public Result<Object> addOneDeviceEntity(@RequestBody Map<String,Object> params, HttpServletRequest request) {
//权限验证
String token = (String)request.getAttribute("claims_admin");
if(token == null || "".equals(token)){
throw new RuntimeException("权限不足!");
}</p>
<pre><code>log.info("addOneDeviceEntity的请求报文:"+params);
//获取必要参数
Integer deviceModel= (Integer) params.get("deviceModel");
Long adminId = (Long) params.get("adminId");
String uniqueId = (String) params.get("uniqueId");
//检查必要参数是否为空
if (deviceModel == null || adminId == null || StringUtils.isEmpty(uniqueId)) {
log.info("必要参数为空!");
return new Result<>(ResultCode.FAIL);
}
//查询公司设备是否存在
DeviceExisting deviceExisting = deviceExistingService.queryOneDeviceExistingByUniqueID(uniqueId);
if (deviceExisting == null){
log.info("公司数据库没有此设备!");
return new Result<>(ResultCode.DEVICE_NULL_IN_COMPANY);
}
//查询设备是否已存在
DeviceEntity deviceEntity = deviceEntityService.queryOneDeviceEntityByUniqueIDByAll(uniqueId);
if (deviceEntity != null) {
//设备已存在
if (deviceEntity.getBindingId() == null || deviceEntity.getBindingId() == -1) {
//设备已被删除,重新添加
if (deviceModel == 1) {
//将设备设为基站
deviceEntity.setBindingId(0L);
deviceEntity.setAdminId(adminId);
deviceEntity.setDeviceModel(deviceModel);
deviceEntity.setDeviceName("基站");
int ret = deviceEntityService.updateOneDeviceEntityByID(deviceEntity);
if (ret > 0) {
log.info("设备增加成功!");
if (deviceEntity.getDeviceModel() == 1) {
//通知游戏端新增一个基站
springToGameService.reloadUpdateOneBaseDevice(deviceEntity);
}
return new Result<>(ResultCode.SUCCESS);
}
} else if (deviceModel == 0) {
//将设备设为流动站
Long deviceId = (Long) params.get("deviceId");
if (deviceId == null) {
log.info("增加流动站时,deviceId为空!");
return new Result<>(ResultCode.FAIL);
}
deviceEntity.setBindingId(deviceId);
deviceEntity.setAdminId(adminId);
deviceEntity.setDeviceModel(deviceModel);
deviceEntity.setDeviceName("流动站");
int ret = deviceEntityService.updateOneDeviceEntityByID(deviceEntity);
if (ret > 0) {
log.info("设备增加成功!");
if (deviceEntity.getDeviceModel() == 0) {
//通知游戏端新增一个流动站
springToGameService.reloadUpdateOneMobileDevice(deviceEntity);
}
return new Result<>(ResultCode.SUCCESS);
}
}
} else {
log.info("数据库中已存在该设备");
return new Result<>(ResultCode.DEVICE_HAS_EXISTED);
}
} else {
//设备不存在,新增设备
DeviceEntity newDevice = new DeviceEntity();
newDevice.setDeviceModel(deviceModel);
newDevice.setAdminId(adminId);
newDevice.setUniqueId(uniqueId);
newDevice.setIccid(deviceExisting.getIccid());
newDevice.setDeviceName(deviceModel == 1 ? "基站" : "流动站");
newDevice.setBindingId(deviceModel == 1 ? 0L : (Long) params.get("deviceId"));
newDevice.setDeviceModel(deviceModel);
newDevice.setDeviceName(deviceModel == 1 ? "基站" : "流动站");
newDevice.setCoachId(0L);
newDevice.setNumberplate("");
newDevice.setRegistrationDate(new Date());
newDevice.setArea("");
newDevice.setRegularId(deviceModel == 1 ? 0L : null);
newDevice.setCarModelAdminId(0L);
int ret = deviceEntityService.addOneDeviceEntity(newDevice);
if (ret > 0) {
log.info("新增设备成功!" + newDevice);
if (deviceModel == 1) {
//通知游戏端新增一个基站
springToGameService.reloadAddOneBaseDevice(newDevice);
} else if (deviceModel == 0) {
//通知游戏端新增一个流动站
springToGameService.reloadAddOneMobileDevice(newDevice);
}
return new Result<>(ResultCode.SUCCESS);
} else {
log.info("新增设备失败!");
return new Result<>(ResultCode.FAIL);
}
}
return new Result<>(ResultCode.FAIL);
</code></pre>
<p>}</p>
<p>主要的优化点如下:</p>
<p>1.将必要参数的检查提前,避免后面出现NullPointerException等异常。</p>
<p>2.将查询设备是否存在的代码提取出来,避免重复代码。</p>
<p>3.将新增设备的代码提取出来,避免重复代码。</p>
<p>4.将重复代码集中在一起,增加代码可读性。</p>
<p>5.将一些不必要的判断和日志记录删除,简化代码</p>
原文地址: https://www.cveoy.top/t/topic/gdZe 著作权归作者所有。请勿转载和采集!