场景:四种情况的判断
问题代码:
/** * 修改拨测: * 状态:0->0 不用调拨测下发 * 状态:0->1 调用拨测下发schedulerSend(1)接口,若失败,把库中的状态改为否 * 状态:1->0 调用拨测下发schedulerSend(0)接口 * 状态:1->1 分两种: * 1.判断除了名称之外的内容,若有变化,先调schedulerSend(0)接口,再调schedulerSend(1)接口 * 2.若没有变化,则不调 */ public Result editor(TestMessageByTaskDetail testMessageByTask) {//新提交的数据 TestMessageByTask newTestMessageByTask = testMessageByTask.getNewTestMessageByTask(); //修改前的数据 TestMessageByTask oldTestMessageByTask = testMessageByTask.getOldTestMessageByTask(); //创建拨测下发参数 SchedulerSendTestVo schedulerSendTestVo = new SchedulerSendTestVo(); BeanUtils.copyProperties(newTestMessageByTask, schedulerSendTestVo); dateFormat(newTestMessageByTask, schedulerSendTestVo); Result result = null; switch (oldTestMessageByTask.getStatus()) { case 0: switch (newTestMessageByTask.getStatus()) { case 0: //状态:0->0 直接修改数据库 result = testMessageByTaskFeign.editor(newTestMessageByTask); break; case 1: //状态:0—>1 调用schedulerSend(1)接口,若调用失败,把数据库中的状态改为0 result = zeroToOne(newTestMessageByTask, schedulerSendTestVo); break;//这个地方执行完之后,继续往下执行 default: break; }
//这里忘了加break,导致程序继续往下执行;这个容易忽略,建议不要两个switch语句套用,改用if else比较安全 case 1: switch (newTestMessageByTask.getStatus()) { case 0: //状态:1->0,调用拨测服务参数状态设置为0,sch(0) result = oneToZero(testMessageByTask, schedulerSendTestVo); break; case 1: //状态:1——>1,判断除了名称之外,如果数据有变化,先调用拨测服务关闭,再开启 result = oneToOne(newTestMessageByTask, oldTestMessageByTask, schedulerSendTestVo);
break; default: break; } default: break; } return result; }
解决方案:
/** * 修改拨测: * 状态:0->0 不用调拨测下发 * 状态:0->1 调用拨测下发schedulerSend(1)接口,若失败,把库中的状态改为否 * 状态:1->0 调用拨测下发schedulerSend(0)接口 * 状态:1->1 分两种: * 1.判断除了名称之外的内容,若有变化,先调schedulerSend(0)接口,再调schedulerSend(1)接口 * 2.若没有变化,则不调 */ public Result editor(TestMessageByTaskDetail testMessageByTask) {//新提交的数据 TestMessageByTask newTestMessageByTask = testMessageByTask.getNewTestMessageByTask(); //修改前的数据 TestMessageByTask oldTestMessageByTask = testMessageByTask.getOldTestMessageByTask(); //创建拨测下发参数 SchedulerSendTestVo schedulerSendTestVo = new SchedulerSendTestVo(); BeanUtils.copyProperties(newTestMessageByTask, schedulerSendTestVo); dateFormat(newTestMessageByTask, schedulerSendTestVo); Result result = null; if (oldTestMessageByTask.getStatus() == 1) { switch (newTestMessageByTask.getStatus()) { case 0: //状态:1->0,调用拨测服务参数状态设置为0,sch(0) result = oneToZero(testMessageByTask, schedulerSendTestVo); break; case 1: //状态:1——>1,判断除了名称之外,如果数据有变化,先调用拨测服务关闭,再开启 result = oneToOne(newTestMessageByTask, oldTestMessageByTask, schedulerSendTestVo); default: break; } } else { switch (newTestMessageByTask.getStatus()) { case 0: //状态:0->0 直接修改数据库 result = testMessageByTaskFeign.editor(newTestMessageByTask); return result; case 1: //状态:0—>1 调用schedulerSend(1)接口,若调用失败,把数据库中的状态改为0 result = zeroToOne(newTestMessageByTask, schedulerSendTestVo); break; default: break; } } return result; }