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 }

 

posted @ 2019-05-04 09:28  BarryW  阅读(1876)  评论(1编辑  收藏  举报