今天在项目中做盘点管理,盘点单据有 编辑->提交->审批->撤销->退回->开盘->盘毕等状态。根据操作对盘点单据会不停的更新其状态。
一般做法是根据这5.6种状态,编写5,6中ServiceImpl方法 分别去Update。若要是批量操作则会加上for循环,代码块冗余过多。自己便将Update抽离出来。
抽离出来后,面临这一些问题。就是在有些状态更新操作时,需要做验证(如:退回只能操作审批状态。开盘只能操作审批状态)。这个时候就需要在update之前需
要判断,看其当前状态是否满足条件。
-------------------------------------------------------------
批量更新数据不同状态时 ,1.在更新前检测 2.更新后执行自己的逻辑
此时利用断言型接口 Predicate<?> 处理【有点象JS中的callback】
/*** 提交盘点单*/@Overridepublic Result submitInventoryPlan(List<InvlctInventory> entitys) throws FrameExecuteException {return changeEntityStatus(entitys,InventoryStatus.SUBMIT,e->true,"提交成功!"); //若不需要在更新前进行检测 直接return true;}/*** 功能:(编制中)撤销盘点单据* @param entitys 盘点单实体List*/@Overridepublic Result retreatInventoryPlan(List<InvlctInventory> entitys) throws FrameExecuteException {return changeEntityStatus(entitys,InventoryStatus.EDIT,e->InventoryStatus.SUBMIT.equals(e.getSts()),"撤销成功!");}/*** 功能:改变盘点单据状态* @param entitys 盘点单据实体LIst* @param status 要设置成的状态* @param trueMessage 操作成功后的提示消息* @return Result*/private Result changeEntityStatus(List<InvlctInventory> entitys,InventoryStatus status,Predicate<InvlctInventory> pre,String trueMessage){List<InvlctInventory> list = new ArrayList<>();for (InvlctInventory invlctInventory : entitys) {InvlctInventory entity = get(invlctInventory.getCod());if(entity!=null){//检查是否满足 状态更新if(pre.test(entity)){//回调判断entity.setSts(status);entity.setEtim(new Date());update(entity);}}else{list.add(invlctInventory);}}String message = null;if(list.isEmpty() || list.size()==0){message = trueMessage;}else{message = "部分数据不存在,请刷新!";}return new Result(true,message);}