JDK1.8中的新特性函数接口Consumer以及accept方法的实例使用。

JDK1.8中的新特性函数接口Consumer以及accept方法的实例使用。

举例使用场景:通过定时任务对数据库中的很多表中的某一字段进行自动添加内容。1.首先将不同表的的表名以及该表的标识,其他信息进行每局enum中实例存储配置。2.通过项目启动@PostConstruct提前将具体的统一填充字段内容方法作为一个Consumer填充到一个HashMap中,hashmap中的key为表的唯一标识。3.遍历循环所有的表,通过表标识获取map中的对应表customer行为参数,将表信息传给行为Consumer中的方法,通过行为方法accept执行。

1.多张表enum实例信息
public enum BillTypeEnum {
  
    task_notice("task_notice", "RW", "工程任务通知书", "BO_EU_TASK_NOTICE"), 
    project_visa("project_visa", "QZ", "工程签证单", "BO_EU_PROJECT_VISA"), 
    settlement_application("settlement_application", "JS", "工程结算申请单", "BO_EU_SETTLEMENT_APPLICATION"),
    record_review("record_review", "HS", "图纸会审记录", "BO_EU_RECORD_REVIEW"),
    default_notice("default_notice", "WY", "工程违约通知单", "BO_EU_DEFAULT_NOTICE"),
    construction_drawings("construction_drawings", "SG", "施工图纸", "BO_EU_CONSTRUCTION_DRAWINGS"),
    contact_list("contact_list", "LX", "工程联系单", "BO_EU_CONTACT_LIST"),
    changed_drawings("changed_drawings", "BG", "图纸变更申请", "BO_EU_CHANGED_DRAWINGS"),
    visa_apply("visa_apply", "QZS", "工程签证申请单", "BO_EU_VISA_APPLY"),
    onsite_measurement("onsite_measurement", "SF", "现场收方计量单", "BO_EU_ONSITE_MEASUREMENT"),
    completed_drawings("completed_drawings", "JG", "竣工图纸", "BO_EU_COMPLETED_DRAWINGS"),
    user_apply("user_apply", "YHJSSQ", "用户角色申请", "BO_EU_USER_ROLE_APPLY"),
    acceptance_completion("acceptance_completion", "JGYS", "工程竣工验收单", "BO_EU_ACCEPTANCE_COMPLETION"),
    acceptance_sporadic("acceptance_sporadic", "LXJGYS", "零星工程竣工验收单", "BO_EU_ACCEPTANCE_SPORADIC"),
    acceptance_bill("acceptance_bill", "GCYS", "工程验收单", "BO_EU_ACCEPTANCE_BILL"),
    acceptance_bill_hide("acceptance_bill_hide", "YBGCYS", "隐蔽工程验收单", "BO_EU_ACCEPTANCE_BILL_HIDE"),
    acceptance_bill_proc_hide("acceptance_bill_proc_hide", "GCYBGCYS", "过程隐蔽工程验收单", "BO_EU_ACCEPTANCE_PROC_HIDE"),
    project_statement("project_statement", "GCJSS", "工程结算书", "BO_EU_PROJECT_STATEMENT"),
    supervision_notice("supervision_notice", "XCJL", "现场监理通知单", "BO_EU_SITE_SUPERVISION_NOTICE"),
    construction_report("construction_report", "TJFABS", "土建方案报审", "BO_EU_CONSTRUCTION_REPORT"),

    ;

    private String value;
    private String code;
    private String name;
    private String tablename;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
    
    public String getTablename() {
        return tablename;
    }

    public void setTablename(String tablename) {
        this.tablename = tablename;
    }

    private BillTypeEnum(String value, String code, String name, String tablename) {
        this.value = value;
        this.code = code;
        this.name = name;
        this.tablename = tablename;
    }

    public static BillTypeEnum getBillTypeEnum(String value) {  
        BillTypeEnum[] billTypeEnumEnums = values();  
        for (BillTypeEnum sillTypeEnum : billTypeEnumEnums) {  
            if (sillTypeEnum.value.equals(value)) {  
                return sillTypeEnum;  
            }  
        }  
        return null;  
    } 
   
2.@PostConstruct将具体的统一填充字段内容方法作为一个Consumer填充到一个HashMap中

通过Consumer接收统一的业务处理逻辑方法,将其放入到Map中。

    private Map> strategyConditionMap=new HashMap();
    
    @PostConstruct
    public void init(){
        strategyConditionMap.put("baseBillNo", this::billNoGenerateByProject);
        strategyConditionMap.put("acceptance_completion", this::billNoGenerateByCommon);
        strategyConditionMap.put("acceptance_sporadic", this::billNoGenerateByCommon);
        strategyConditionMap.put("acceptance_bill", this::billNoGenerateByCommon);
        strategyConditionMap.put("acceptance_bill_hide", this::billNoGenerateByCommon);
        strategyConditionMap.put("acceptance_bill_proc_hide", this::billNoGenerateByCommon);
        strategyConditionMap.put("supervision_notice", this::billNoGenerateByCommon);
        strategyConditionMap.put("construction_report", this::billNoGenerateByCommon);
    }
}
3.循环遍历所有的表,表信息传给Consumer通过行为方法accept执行。

循环遍历所有的表,通过表标识获取map中的对应表Consumer行为参数,将表信息传给行为Consumer中的方法,通过行为方法accept执行。

 public int billNoGenerate() {
        long time = System.currentTimeMillis();
        boolean result = redisClientTemplate.setnx("billNoGenerate",3,String.valueOf(time));
        if(!result){
            return 0;
        }
        //b遍历所有的表
        for (BillTypeEnum billTypeEnum : EnumSet.allOf(BillTypeEnum.class)) {
            try {
                String value = billTypeEnum.getValue();
                if(value.equals("user_apply")){
                    continue;
                }
				//从hashmap中获取对应唯一的表行为Customer
                Consumer consumer = strategyConditionMap.get(value);
                if(null==consumer){
                    consumer = strategyConditionMap.get("baseBillNo");
                }
                //将表信息billTypeEnum传递给consumer,通过accept接收参数,执行对应表行为方法
                consumer.accept(billTypeEnum);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
4.具体业务处理方法,其中JDK1.8中的lambda表达式过滤筛选用相关应用
    private void billNoGenerateByCommon(BillTypeEnum billTypeEnum){
        List billInfoGenerateDtoList = formQueryMapper.selectBillInfoGenerateDto(billTypeEnum.getTablename());
        /** 过滤流程正常结束的数据 **/
        List bindIds = billInfoGenerateDtoList
                .stream()
                .map(BillInfoGenerateDto::getBindid)
                .collect(Collectors.toList());

        ProcessQueryModel processQueryModel = new ProcessQueryModel();
        processQueryModel.setIds(bindIds);
        List processInstances = bpmService.processQuery(processQueryModel);
        List bindids1 = processInstances
                .stream()
                .filter(s->s.getControlState().equals("end"))
                .map(ProcessInstance::getId)
                .collect(Collectors.toList());


        for (BillInfoGenerateDto billInfoGenerateDto : billInfoGenerateDtoList) {
            if(!bindids1.contains(billInfoGenerateDto.getBindid())){
                continue;
            }
            String dateMonth = DateUtil.getShortDays();
            String ncPkOrg = billInfoGenerateDto.getNcPkOrg();
            String key = billTypeEnum.getValue()+"_"+ncPkOrg+"_"+dateMonth;
            String billNoCnt = redisClientTemplate.get(key);
            Long requestNo;
            if(StringUtils.isBlank(billNoCnt)){
                /**读取数据库获取*/
                Date nowDate = new Date();
                long cnt=formQueryMapper.selectBillNoCountByNcAndDate(billTypeEnum.getTablename(),ncPkOrg,nowDate);
                redisClientTemplate.setnx(key,Constants.EXPIRE_DAY,String.valueOf(cnt));
            }
            requestNo=redisClientTemplate.incr(key);
            NcOrgOrgs ncOrgOrgs = ncOrgOrgsService.getNcOrgOrgsListByPkNcOrg(ncPkOrg);
            if(null==ncOrgOrgs){
                continue;
            }
            String mark = ncOrgOrgs.getMark();
            if(StringUtils.isBlank(mark)){
                continue;
            }
            DecimalFormat df3 = new DecimalFormat("00000");
            String billNo = billTypeEnum.getCode()+mark+dateMonth+df3.format(requestNo);
            bpmService.boFieldUpdate(billTypeEnum.getTablename(), billInfoGenerateDto.getBindid(), "BILL_NO", billNo);
        }
    }

在以后的java开发中函数编程会更加的广泛,简化程序开发,参数传递的可以是具体的方法,这种行为可以简单称为函数行为。以上是个人方法的使用以及相关见解。

你可能感兴趣的:(笔记,java,spring)