zoukankan      html  css  js  c++  java
  • 批量更新数据不同状态时 ,1.在更新前检测 2.更新后执行自己的逻辑

      今天在项目中做盘点管理,盘点单据有 编辑->提交->审批->撤销->退回->开盘->盘毕等状态。根据操作对盘点单据会不停的更新其状态。
    一般做法是根据这5.6种状态,编写5,6中ServiceImpl方法 分别去Update。若要是批量操作则会加上for循环,代码块冗余过多。自己便将Update抽离出来。
    抽离出来后,面临这一些问题。就是在有些状态更新操作时,需要做验证(如:退回只能操作审批状态。开盘只能操作审批状态)。这个时候就需要在update之前需
    要判断,
    看其当前状态是否满足条件。
    -------------------------------------------------------------
    批量更新数据不同状态时 ,1.在更新前检测 2.更新后执行自己的逻辑

    此时利用断言型接口
    Predicate<?> 处理【有点象JS中的callback】

    /**
    * 提交盘点单 */ @Override public Result submitInventoryPlan(List<InvlctInventory> entitys) throws FrameExecuteException { return changeEntityStatus(entitys,InventoryStatus.SUBMIT,e->true,"提交成功!"); //若不需要在更新前进行检测 直接return true; } /** * 功能:(编制中)撤销盘点单据 * @param entitys 盘点单实体List */ @Override public 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); }
  • 相关阅读:
    单例模式
    java笔记 chapter7 抽象类和数组
    java笔记 chapter6 StringBuffer类和String Bulider类,Math类 Date类,Calendar类
    设计上的若干问题
    Java中的二次分发
    关于抽象
    SSI框架下同一个Bean加载了2次问题解决
    Hello 2015
    关于window.location.href is not a function在FF,chrom报错问题
    使用Eclipse的一些小心得!
  • 原文地址:https://www.cnblogs.com/wang-yi/p/10025417.html
Copyright © 2011-2022 走看看