<p>//2.根据ID删除一个设备
@PostMapping(&quot;/deleteOneDeviceEntityByID&quot;)
public Result<Object> deleteOneDeviceEntityByID(@RequestBody Map&lt;String,Object&gt; params, HttpServletRequest request) {</p>
<pre><code>//权限验证
String token = (String)request.getAttribute(&quot;claims_admin&quot;);
if(StringUtils.isEmpty(token)){
    throw new RuntimeException(&quot;权限不足!&quot;);
}

log.info(&quot;deleteOneDeviceEntityByID请求报文:&quot;+params);

//前端传递参数
Integer deviceModel = (Integer) params.get(&quot;deviceModel&quot;);//设备的RTK模式,0:流动站模式(默认)。1:基站模式
Long deviceId = (Long) params.get(&quot;deviceId&quot;);//待删除设备id
String phone = (String) params.get(&quot;phone&quot;);
String userCode = (String) params.get(&quot;code&quot;);

//参数校验
if (deviceModel == null){
    log.info(&quot;deviceModel为空!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}
if (deviceId == null){
    log.info(&quot;deviceId为空!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}
if (StringUtils.isEmpty(phone)) {
    log.info(&quot;phone为空!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}
if (StringUtils.isEmpty(userCode)) {
    log.info(&quot;userCode为空!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}

//通过缓存拿到自己生成的验证码
String myCode = redisTemplate.opsForValue().get(&quot;deviceDelete_&quot;+deviceId+phone);
log.info(&quot;myCode:&quot; + myCode);

if (StringUtils.isEmpty(myCode)){
    log.info(&quot;myCode为空!&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}

//用户验证码输入正确
if (!userCode.equals(myCode)) {
    log.info(&quot;验证码错误!&quot;);
    return new Result&lt;&gt;(ResultCode.CODE_FALSE);
}
log.info(&quot;验证码正确!&quot;);

//当前设备为基站时,删除本设备同时需要把其流动站都删除
if (deviceModel == 1) {
    //通过设备id查询出当前需要删除的基站
    DeviceEntity baseDevice = deviceEntityService.queryOneDeviceEntityByID(deviceId);
    if (baseDevice == null){
        log.info(&quot;当前设备为空!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }

    //查出此基站下所有流动站
    List&lt;DeviceEntity&gt; mobileDevices = deviceEntityService.queryMobileDevicesByBindingID(deviceId);
    List&lt;DeviceEntity&gt; mobileDeviceListByDelete = new ArrayList&lt;&gt;();

    //流动站存在
    if (!CollectionUtils.isEmpty(mobileDevices)) {
        log.info(&quot;设备为基站,存在对应流动站&quot;);
        //删除所有此基站下的流动站
        for (DeviceEntity mobileDevice : mobileDevices){
            mobileDevice.setBindingId(-1L);//删除流动站
            mobileDeviceListByDelete.add(mobileDevice);
        }
        boolean batch = deviceEntityService.updateBatchById(mobileDeviceListByDelete);
        if (!batch) {
            log.info(&quot;删除流动站失败!&quot;);
            return new Result&lt;&gt;(ResultCode.FAIL);
        }
        log.info(&quot;删除流动站成功!&quot;);

        //通知游戏端删除流动站
        springToGameService.reloadDeleteMobileDevice(mobileDeviceListByDelete);
    } else {
        log.info(&quot;设备为基站,无对应流动站&quot;);
    }

    //调用方法删除基站
    baseDevice.setBindingId(-1L);
    int delete = deviceEntityService.updateOneDeviceEntityByID(baseDevice);
    if (delete&lt;=0){
        log.info(&quot;删除基站失败!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }
    log.info(&quot;删除基站成功!&quot;);

    //通知游戏端删除一个基站
    springToGameService.reloadDeleteOneBaseDevice(baseDevice);

    //备份数据
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(&quot;baseDevice&quot;,baseDevice);
    jsonObject.put(&quot;mobileDevices&quot;,mobileDevices);
    String deleteMessage = jsonObject.toString();

    Backup backup = new Backup();
    backup.setBackupTypeId(1);//备份删除的设备数据
    backup.setDeleteMessage(deleteMessage);
    backup.setState(false);//表示被删除
    backup.setDeleteDate(new Date());
    backup.setAdminId(baseDevice.getAdminId());

    boolean save = backupService.save(backup);

    if (!save){
        log.info(&quot;备份数据更新失败!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }
    log.info(&quot;备份数据更新成功!&quot;);

    //恢复默认值
    //1.流动站恢复默认值
    List&lt;DeviceEntity&gt; mobileDeviceListByUpdate = new ArrayList&lt;&gt;();
    for (DeviceEntity mobileDevice : mobileDevices){
        //更新设备数据,恢复默认值
        mobileDevice.setAdminId(0L);//默认为0
        mobileDevice.setCoachId(0L);//默认为0
        mobileDevice.setNumberplate(&quot;&quot;);//默认为“”
        mobileDevice.setArea(&quot;&quot;);//默认为“”
        mobileDevice.setRegularId(0L);//规则只与基站相关 流动站默认为0
        mobileDevice.setCarModelAdminId(0L);//车辆模型id默认为0
        mobileDeviceListByUpdate.add(mobileDevice);
    }
    boolean update = deviceEntityService.updateBatchById(mobileDeviceListByUpdate);
    if (!update){
        log.info(&quot;流动站恢复默认值失败!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }
    log.info(&quot;流动站恢复默认值成功!&quot;);

    //2.基站恢复默认值
    baseDevice.setAdminId(0L);//默认为0
    baseDevice.setCoachId(0L);//默认为0
    baseDevice.setNumberplate(&quot;&quot;);//默认为0
    baseDevice.setArea(&quot;&quot;);//默认为0
    baseDevice.setRegularId(0L);//规则只与基站相关 流动站默认为0
    baseDevice.setCarModelAdminId(0L);//车辆模型id默认为0
    int updateBase = deviceEntityService.updateOneDeviceEntityByID(baseDevice);

    //操作成功
    if (updateBase &gt; 0) {
        log.info(&quot;基站恢复默认值成功!&quot;);
        return new Result&lt;&gt;(ResultCode.SUCCESS);
    } else {
        //操作失败
        log.info(&quot;基站恢复默认值失败!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }

} else if (deviceModel == 0) {
    log.info(&quot;设备为流动站&quot;);
    //通过设备id查询出当前需要删除的流动站
    DeviceEntity mobileDevice = deviceEntityService.queryOneDeviceEntityByID(deviceId);
    if (mobileDevice == null){
        log.info(&quot;当前设备为空!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }
    mobileDevice.setRemark(mobileDevice.getBindingId().toString());//把当前要删除的流动站的绑定id存放在设备名称字段里
    mobileDevice.setBindingId(-1L);//删除流动站
    int ret = deviceEntityService.updateOneDeviceEntityByID(mobileDevice)
</code></pre>

原文地址: http://www.cveoy.top/t/topic/gfnc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录