Java教程

JAVA ENUM枚举类实现接口类有什么好处?

本文主要是介绍JAVA ENUM枚举类实现接口类有什么好处?,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

经过网络查找的解答就是为了单例。
枚举类可以实现接口,但要求枚举中的每个对象都必须覆写接口中的方法。
枚举类中可以定义抽象方法,但要求枚举中的每个对象都必须覆写方法。
以下是示例:

public interface CustomerPushService {
    /**
     * 获取需要推送的用户ID集合
     * @param custId
     * @return
     */
    Set<Integer> getPushUserArchiveIds(Integer custId,Integer cityId,Integer compId);
}
public enum CustomerPushServiceEnum implements CustomerPushService {
    /**
     * 发起报备
     **/
    START_REPORT(Const.ML_FUN_CUST_TRACK_0, Const.ML_FUN_CUST_PUSH_TITLE_0, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_0) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            // 发起报备只需要发送给驻场 项目负责人以及权限人
            return getManageReceiverUserIds(custId, cityId, compId);
        }
    },
    CANCEL_REPORT(Const.ML_FUN_CUST_TRACK_9, Const.ML_FUN_CUST_PUSH_TITLE_9, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_9) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 取消报备 发送给报备人
             */
            return getManageReceiverUserIds(custId, cityId, compId);
        }
    },
    REJECT_REPORT(Const.ML_FUN_CUST_TRACK_10, Const.ML_FUN_CUST_PUSH_TITLE_10, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_10) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 驳回报备 发送给报备人
             */
            return getReporterUserId(custId);
        }
    },
    CONFIRM_REPORT(Const.ML_FUN_CUST_TRACK_1, Const.ML_FUN_CUST_PUSH_TITLE_1, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_1) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 确认报备 发送给报备人
             */
            return getReporterUserId(custId);
        }
    },
    ARRIVAL(Const.ML_FUN_CUST_TRACK_2, Const.ML_FUN_CUST_PUSH_TITLE_2, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_2) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 到场 发送给报备人
             */
            return getReporterUserId(custId);
        }
    },
    VISIT(Const.ML_FUN_CUST_TRACK_3, Const.ML_FUN_CUST_PUSH_TITLE_3, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_3) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 复访 发送给报备人
             */
            return getReporterUserId(custId);
        }
    },
    MAKE_DEAL(Const.ML_FUN_CUST_TRACK_5, Const.ML_FUN_CUST_PUSH_TITLE_5, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_5) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 认购 发送给报备人 以及相关责任人
             */
            Set<Integer> ids = getReporterUserId(custId);
            ids.addAll(getManageReceiverUserIds(custId, cityId, compId));
            return ids;
        }
    },
    CANCEL_DEAL(Const.ML_FUN_CUST_TRACK_11, Const.ML_FUN_CUST_PUSH_TITLE_11, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_11) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 取消认购 发送给报备人 以及相关责任人
             */
            Set<Integer> ids = getReporterUserId(custId);
            ids.addAll(getManageReceiverUserIds(custId, cityId, compId));
            return ids;
        }
    },
    PROMOTION_EXPIRE(Const.ML_FUN_CUST_TRACK_12, Const.ML_FUN_CUST_PUSH_TITLE_12, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_12) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 超过项目推广期 发送给报备人 以及相关责任人
             */
            Set<Integer> ids = getReporterUserId(custId);
            ids.addAll(getManageReceiverUserIds(custId, cityId, compId));
            return ids;
        }
    },
    REPORT_VALIDITY_EXPIRE(Const.ML_FUN_CUST_TRACK_13, Const.ML_FUN_CUST_PUSH_TITLE_12, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_13) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 超过报备有效期 发送给报备人 以及相关责任人
             */
            Set<Integer> ids = getReporterUserId(custId);
            ids.addAll(getManageReceiverUserIds(custId, cityId, compId));
            return ids;
        }
    },
    CONFIRM_REPORT_VALIDITY_EXPIRE(Const.ML_FUN_CUST_TRACK_14, Const.ML_FUN_CUST_PUSH_TITLE_12, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_14) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 超过报备确认有效期 发送给报备人 以及相关责任人
             */
            Set<Integer> ids = getReporterUserId(custId);
            ids.addAll(getManageReceiverUserIds(custId, cityId, compId));
            return ids;
        }
    },
    ARRIVAL_PROTECTED_EXPIRE(Const.ML_FUN_CUST_TRACK_15, Const.ML_FUN_CUST_PUSH_TITLE_12, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_15) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            /**
             * 超过到场保护期 发送给报备人 以及相关责任人
             */
            Set<Integer> ids = getReporterUserId(custId);
            ids.addAll(getManageReceiverUserIds(custId, cityId, compId));
            return ids;
        }
    },
    DEFAULT(Const.ML_FUN_CUST_TRACK_0, Const.ML_FUN_CUST_PUSH_TITLE_0, Const.ML_FUN_CUST_PUSH_SUBJECT_CONTENT_0) {
        @Override
        public Set<Integer> getPushUserArchiveIds(Integer custId, Integer cityId, Integer compId) {
            return new HashSet<>();
        }
    };

    protected AgencyFunCustMapper agencyFunCustMapper;

    protected AgencyPropertyRelatedPersonMapper agencyPropertyRelatedPersonMapper;

    private ErpFunUsersMapper erpFunUsersMapper;

    private ErpFunTaskService erpFunTaskService;

    private PushBuildWebFeignClient pushBuildWebFeignClient;

    protected static final Logger LOG = LoggerFactory.getLogger(CustomerPushServiceEnum.class);


    private byte trackType;
    private String title;
    private String subjectContent;
    private Executor taskExecutor;

    CustomerPushServiceEnum(byte trackType, String title, String subjectContent) {
        this.trackType = trackType;
        this.title = title;
        this.subjectContent = subjectContent;
    }

    private byte getTrackType() {
        return trackType;
    }

    private String getSubjectContent() {
        return subjectContent;
    }

    private String getTitle() {
        return title;
    }

    private Integer getTemplateId() {
        return 137;
    }

    public void setAgencyFunCustMapper(AgencyFunCustMapper agencyFunCustMapper) {
        this.agencyFunCustMapper = agencyFunCustMapper;
    }

    public void setAgencyPropertyRelatedPersonMapper(AgencyPropertyRelatedPersonMapper agencyPropertyRelatedPersonMapper) {
        this.agencyPropertyRelatedPersonMapper = agencyPropertyRelatedPersonMapper;
    }

    public void setErpFunUsersMapper(ErpFunUsersMapper erpFunUsersMapper) {
        this.erpFunUsersMapper = erpFunUsersMapper;
    }

    private void setErpFunTaskService(ErpFunTaskService erpFunTaskService) {
        this.erpFunTaskService = erpFunTaskService;
    }

    public void setPushBuildWebFeignClient(PushBuildWebFeignClient pushBuildWebFeignClient){
        this.pushBuildWebFeignClient = pushBuildWebFeignClient;
    }

    public static CustomerPushServiceEnum getServiceByTrackType(byte trackType) {
        for (CustomerPushServiceEnum pushService : CustomerPushServiceEnum.values()) {
            if (pushService.getTrackType() == trackType) {
                return pushService;
            }
        }
        return DEFAULT;
    }

    public void pushMsg(Integer custId, byte custStatus, String isShare, BrokerOperator operator) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    /** 遍历需要发送的用户 **/
                    getPushUserArchiveIds(custId, operator.getCityId(), operator.getCompId()).stream().forEach(var -> {
                        ErpFunUsers user = erpFunUsersMapper.selectByUserId(operator.getCityId(), var);

                        if (user != null) {
                            String content = getPushSubjectContent(custId);

                            ErpFunTask erpFunTask = new ErpFunTask();
                            erpFunTask.setCaseType((byte) 0);
                            erpFunTask.setCaseId(0);
                            erpFunTask.setShardCityId(operator.getCityId());
                            erpFunTask.setTrackType(Const.DIC_TASK_TYPE_BAOBEI);
                            erpFunTask.setTaskType(Const.DIC_TASK_TYPE_BAOBEI);
                            erpFunTask.setTaskSubject(content);
                            erpFunTask.setTaskDesc(content);
                            erpFunTask.setCompId(operator.getCompId());
                            erpFunTask.setTypeFlag(Const.DIC_TASK_TYPE_FLAG_0);
                            erpFunTask.setIsRead(false);
                            erpFunTask.setTaskStatus(Const.DIC_TASK_STATUS_NEW);
                            erpFunTask.setCreatorUid(operator.getUserId());//发送人
                            erpFunTask.setCreationTime(DateTimeHelper.formatDateTimetoString(new Date()));
                            erpFunTask.setDeptId(operator.getDeptId());
                            erpFunTask.setTrackWarm(true);
                            erpFunTask.setTaskOwner(var);//收信人
                            erpFunTask.setWarmTime(DateUtil.DateToString(new Date()));
                            erpFunTaskService.insertTask(operator.getCityId(), erpFunTask);

                            execPushMsg(getTemplateId(), user.getArchiveId(), getTitle(), content, custId, custStatus, isShare);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    LOG.error("pushMsgForSystem error , customer id is " + custId);
                }
            }
        });
    }

    public void pushMsgForSystem(AgencyFunCust funCust) {
        try {
            /** 遍历需要发送的用户 **/
            getPushUserArchiveIds(funCust.getCustId(), funCust.getCityId().intValue(), funCust.getCompId()).stream().forEach(var -> {
                ErpFunUsers user = erpFunUsersMapper.selectByUserId(funCust.getCityId().intValue(), var);

                if (user != null) {
                    String content = getPushSubjectContent(funCust.getCustId());

                    ErpFunTask erpFunTask = new ErpFunTask();
                    erpFunTask.setCaseType((byte) 0);
                    erpFunTask.setCaseId(0);
                    erpFunTask.setShardCityId(funCust.getCityId().intValue());
                    erpFunTask.setTrackType(Const.DIC_TASK_TYPE_BAOBEI);
                    erpFunTask.setTaskType(Const.DIC_TASK_TYPE_BAOBEI);
                    erpFunTask.setTaskSubject(content);
                    erpFunTask.setTaskDesc(content);
                    erpFunTask.setCompId(funCust.getCompId());
                    erpFunTask.setTypeFlag(Const.DIC_TASK_TYPE_FLAG_0);
                    erpFunTask.setIsRead(false);
                    erpFunTask.setTaskStatus(Const.DIC_TASK_STATUS_NEW);
                    erpFunTask.setCreatorUid(Const.SYSTEM_UID);//发送人
                    erpFunTask.setCreationTime(DateTimeHelper.formatDateTimetoString(new Date()));
                    erpFunTask.setDeptId(funCust.getDeptId());
                    erpFunTask.setTrackWarm(true);
                    erpFunTask.setTaskOwner(var);//收信人
                    erpFunTask.setDeptId(0);
                    erpFunTask.setWarmTime(DateUtil.DateToString(new Date()));
                    erpFunTaskService.insertTask(funCust.getCityId().intValue(), erpFunTask);

                    execPushMsg(getTemplateId(), user.getArchiveId(), getTitle(), content, funCust.getCustId(), Const.ML_FUN_CUST_STATUS_0, StrUtil.EMPTY);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("pushMsgForSystem error , customer id is " + funCust.getCustId());
        }
    }

    private void execPushMsg(Integer templateId, Integer receiver, String title, String content, Integer custId, int status, String isShare) {
        JSONObject pushParam = new JSONObject();
        pushParam.put("templateId", templateId);
        pushParam.put("receiver", receiver);
        // 通知标题
        pushParam.put("title", title);
        pushParam.put("content", content);
        //类别 同城报备“” 跨城报备“3”
        pushParam.put("projectType", isShare);
        //当前客户状态 客户状态 -3 取消认购 -2 报备驳回 -1 报备取消 0 数据失效 1报备中 2已确认 3已到场 4已复访 5已认购
        pushParam.put("status", status);
        pushParam.put("custId", custId);

        String iosRouterUrl = "zshft://router.method?targetclass=FXBuildTargetAction&methodkey=mlCustomerDetailVcWithPara:&custId=%s&isShare=%s";
        iosRouterUrl = String.format(iosRouterUrl, custId, Objects.equals("3", isShare) ? 1 : 0);
        // 苹果跳转地址
        pushParam.put("ios_router_url", iosRouterUrl);
        Map<String, Object> paramExt = new HashMap<String, Object>();
        paramExt.put("paramExt", pushParam.toJSONString());

        String respResult = null;
        try{
            respResult = HttpUtil.postJson(AppConfig.getProperty("common.pushWeb.path") + "openApi/push/pushMsg", paramExt);
            LOG.info("----------------->>请求pushWeb 的url:{},参数:{}", AppConfig.getProperty("common.pushWeb.path") + "openApi/push/pushMsg", paramExt);
            LOG.info("----------------->>请求pushWeb结果:{}", respResult);
        }catch(Exception ex){
            LOG.error(" push mag erro. HttpUtil.postJson" + ex.toString());
        }

        if(respResult == null || !respResult.contains("200")){
            LOG.info("----------------->>请求pushWebFeignClient start......}");
            PushMsgParam extParam = new PushMsgParam();
            extParam.setParamExt(pushParam.toJSONString());
            try{
                pushBuildWebFeignClient.pushMsg(extParam);
                LOG.info("----------------->>请求pushWebFeignClient结果:{}", respResult);
            }catch(Exception ex){
                LOG.error("pushBuildWebFeignClient push mag erro." + ex.toString());
            }
        }
    }

    protected Set<Integer> getManageReceiverUserIds(Integer custId, Integer cityId, Integer compId) {
        Set<Integer> receiverIds = new HashSet<>();
        /**
         * 发起报备需要发送信息的人员 ID
         * 驻场经理
         * 项目负责人
         * 权限人
         */
        AgencyFunCust funCust = agencyFunCustMapper.selectByOnlyCustId(custId);

        if (funCust.getSaleUid() == null) {
            /**
             * 当获取的指定驻场经理人员为空时 就系统提醒该项目合同期内所有驻场人员
             */
            List<AgencyPropertyRelatedPerson> propertyRelatedPersonList = agencyPropertyRelatedPersonMapper.selectByBuildIdAndPersonType(funCust.getBuildId(), 2, null);
            if (propertyRelatedPersonList.isEmpty()) {
                return receiverIds;
            }
            List<Integer> userIds = propertyRelatedPersonList.stream().map(AgencyPropertyRelatedPerson::getUserId).distinct().collect(Collectors.toList());
            receiverIds.addAll(userIds);
        } else {
            // 过滤掉不在有效期内的驻场人员
            List<AgencyPropertyRelatedPerson> propertyRelatedPersonList = agencyPropertyRelatedPersonMapper.selectByBuildIdAndPersonType(funCust.getBuildId(), 2, funCust.getSaleUid());
            if (!propertyRelatedPersonList.isEmpty()) {
                propertyRelatedPersonList.stream().forEach(var -> {
                    receiverIds.add(var.getUserId());
                });
            }
        }

        /** 项目负责人 **/
        List<AgencyPropertyRelatedPerson> personList = agencyPropertyRelatedPersonMapper.selectByBuildIdAndPersonType(funCust.getBuildId(), 1, null);
        List<Integer> buildUserIdList = Optional.ofNullable(personList).orElseGet(() -> new ArrayList<>()).parallelStream()
                .filter(p -> Objects.nonNull(p.getUserId())).map(AgencyPropertyRelatedPerson::getUserId).distinct().collect(Collectors.toList());
        receiverIds.addAll(buildUserIdList);

        return receiverIds;
    }

    protected Set<Integer> getReporterUserId(Integer custId) {
        AgencyFunCust funCust = agencyFunCustMapper.selectByOnlyCustId(custId);
        Set<Integer> set = new HashSet<>(1);
        set.add(funCust.getUserId() == null ? 0 : funCust.getUserId());
        return set;
    }

    private String getPushSubjectContent(Integer custId) {
        AgencyFunCust funCust = agencyFunCustMapper.selectByOnlyCustId(custId);

        String phone = "";
        if (StringUtils.isNotBlank(funCust.getCustMobile())) {
            if (funCust.getCustMobile().length() > 8) {
                phone = funCust.getCustMobile().substring(0, 3) + "****" + funCust.getCustMobile().substring(7);
            } else {
                phone = "***";
            }
        } else {
            phone = "空";
        }

        return String.format(getSubjectContent(), funCust.getBuildName(), funCust.getCustName(), phone) + DateUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
    }


    @Component
    public static class ReportTypeServiceInjector {
        @Autowired
        protected AgencyFunCustMapper agencyFunCustMapper;
        @Autowired
        protected AgencyPropertyRelatedPersonMapper agencyPropertyRelatedPersonMapper;
        @Autowired
        private ErpFunUsersMapper erpFunUsersMapper;
        @Autowired
        private ErpFunTaskService erpFunTaskService;
        @Autowired
        private Executor taskExecutor;
        @Autowired
        private PushBuildWebFeignClient pushBuildWebFeignClient;

        @PostConstruct
        public void postConstruct() {
            for (CustomerPushServiceEnum tempEnum : EnumSet.allOf(CustomerPushServiceEnum.class)) {
                tempEnum.setAgencyFunCustMapper(agencyFunCustMapper);
                tempEnum.setAgencyPropertyRelatedPersonMapper(agencyPropertyRelatedPersonMapper);
                tempEnum.setErpFunUsersMapper(erpFunUsersMapper);
                tempEnum.setErpFunTaskService(erpFunTaskService);
                tempEnum.setTaskExecutor(taskExecutor);
                tempEnum.setPushBuildWebFeignClient(pushBuildWebFeignClient);
            }
        }
    }

    private void setTaskExecutor(Executor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }
}
这篇关于JAVA ENUM枚举类实现接口类有什么好处?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!