Scala 定义复杂枚举

通常我们需要枚举参数不止两个,Scala提供的枚举类最多定义两个参数:id:Int与name:String。不能满足我们通常要求。

1 object BaseEntryEnum extends Enumeration {
2   type BaseEntryEnum = Value
3   //item_base类别
4   val ITEM_TYPE_PURCHASE_CENTER = Value(31, "采购中心")
5   val ITEM_TYPE_COMPANY = Value(32, "公司")
6   val ITEM_TYPE_OI_ITEM = Value(33, "OI项")
7   val ITEM_TYPE_STORE_ITEM = Value(40, "门店白名单")
8   val ITEM_TYPE_STORE_BLACKLIST = Value(41, "门店黑名单")
9 }

 

如下enumeratum包可以满足我们需求,可以定义任意参数。

引入如下包

"com.beachape" %% "enumeratum-argonaut" % "1.5.13"

定义复杂枚举:

 1 package com.today.service.financetask.job.define
 2 
 3 import com.today.api.financetask.scala.enums.TReportTypeEnum
 4 import com.today.service.financetask.job._
 5 import enumeratum.EnumEntry
 6 import org.quartz.Job
 7 
 8 sealed abstract class JobEnum(
 9                       /**
10                       * ID
11                       */
12                       val jobId: String,
13                       /**
14                         * job名称
15                         */
16                       val jobName: String,
17                       /**
18                         * job定义类
19                         */
20                       val jobClass : Class[_ <: Job],
21                       /**
22                         * 定时任务cron表达式
23                         */
24                       val cron: String) extends EnumEntry
25 
26 /**
27   * 所有Job 枚举定义在此类, 不能重复
28   * jobId不能重复
29   * @author BarryWang create at 2018/5/12 10:45
30   * @version 0.0.1
31   */
32 case object JobEnum extends enumeratum.Enum[JobEnum]{
33   val values = findValues
34   case object DAY_TIME_PROCESS extends JobEnum("DAY_TIME_PROCESS",
35     "日次处理定时任务", classOf[DayTimeProcessJob], "0 30 2 * * ?")
36 
37   case object RETIREMENT_SYNC_PROCESS extends JobEnum("RETIREMENT_SYNC_PROCESS",
38     "采购系统同步报废单据数据定时任务", classOf[RetirementSyncProcessJob], "0 0 1 * * ?")
39 
40   case object PURCHASE_ORDER_2_SYNC_PROCESS extends JobEnum("PURCHASE_ORDER_2_SYNC_PROCESS",
41     "采购系统同步PO2数据定时任务", classOf[PurchaseOrder2SyncProcessJob], "0 0 5 * * ?")
42 
43   case object SEND_EMAIL_PROCESS extends JobEnum("SEND_EMAIL_PROCESS",
44     "计划付款通知和已付款通知定时任务", classOf[SendEmailProcessJob], "0 0 18 * * ?")
45 
46   case object CLOSE_ACCOUNT_BASE_DATA_PROCESS extends JobEnum("CLOSE_ACCOUNT_BASE_DATA_PROCESS",
47     "关账基础数据同步定时任务", classOf[CloseAccountBaseDataSyncJob], "0 30 23 L * ?")
48 
49   case object INCOME_DETAIL_REPORT extends JobEnum(TReportTypeEnum.INCOMEDETAIL_REPORT.id.toString,
50     "每天生成营收明细报表定时任务", classOf[DailyGenIncomeDetailJob], "0 10 0 * * ?")
51 
52   case object CHECK_ACCOUNT_PROCESS extends JobEnum("CHECK_ACCOUNT_PROCESS",
53     "对账系统自动对账定时任务", classOf[CheckAccountJob], "0 0 13 * * ?")
54 
55   case object MEMBER_ACCOUNT_PROCESS extends JobEnum("MEMBER_ACCOUNT_PROCESS",
56     "会员自动对账定时任务", classOf[MemberAccountJob], "0 1 0 * * ?")
57 
58   case object EXPORT_REPORT_LONG_HISTORY_STATUS extends JobEnum("EXPORT_REPORT_STATUS_PROCESS",
59     "清除90天前的财务报表定时任务", classOf[ExportReportStatusProcessJob], "0 15 1 ? * *")
60 
61   case object DING_TALK_LOGS_IMPORT extends JobEnum("DING_TALK_LOGS_IMPORT",
62     "导入钉钉审批记录定时任务", classOf[DingTalkImportProcessLogsJob], "0 10 1 ? * *")
63 
64   case object DING_TALK_DETAIL_IMPORT extends JobEnum("DING_TALK_DETAIL_IMPORT",
65     "导入钉钉审核通过审批单详情定时任务", classOf[DingTalkImportProcessDetailJob], "0 20 1 ? * *")
66 
67   /**
68     * jobId与jobName映射关系
69     * @return
70     */
71   def jobIdNameMap : Map[String, String]={
72     JobEnum.values.map(x=> Map(x.jobId -> x.jobName)).fold(Map())((i,j) => i++j)
73   }
74 
75   /**
76     * JobObject与JobEnum映射关系
77     * @return
78     */
79   def jobObjectEnumMap = {
80     JobEnum.values.map(x=> Map(x.jobClass.getName -> x)).fold(Map())((i,j) => i++j)
81   }
82 
83   /**
84     * jobId与JobEnum映射关系
85     * @return
86     */
87   def jobIdEnumMap = {
88     JobEnum.values.map(x=> Map(x.jobId -> x)).fold(Map())((i,j) => i++j)
89   }
90 
91 }

 

转载于:https://www.cnblogs.com/barrywxx/p/10807545.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值