@@ -106,6 +106,8 @@ public class LogicalDeletePlugin extends BasePlugin {
106106 * 逻辑删除枚举
107107 */
108108 private InnerEnum logicalDeleteEnum ;
109+ private int logicalUnDeleteEnumIndex = 0 ;
110+ private int logicalDeleteEnumIndex = 1 ;
109111
110112 /**
111113 * 初始化阶段
@@ -116,6 +118,9 @@ public class LogicalDeletePlugin extends BasePlugin {
116118 @ Override
117119 public void initialized (IntrospectedTable introspectedTable ) {
118120 super .initialized (introspectedTable );
121+ this .logicalUnDeleteEnumIndex = 0 ;
122+ this .logicalDeleteEnumIndex = 1 ;
123+
119124 // 1. 获取配置的逻辑删除列
120125 Properties properties = getProperties ();
121126 String logicalDeleteColumn = properties .getProperty (PRO_LOGICAL_DELETE_COLUMN );
@@ -139,7 +144,21 @@ public void initialized(IntrospectedTable introspectedTable) {
139144 }
140145 }
141146
142- // 3. 优先借助 EnumTypeStatusPlugin 插件,去注解里面解析枚举
147+ // 3.判断逻辑删除值是否配置了
148+ this .logicalDeleteValue = properties .getProperty (PRO_LOGICAL_DELETE_VALUE );
149+ this .logicalUnDeleteValue = properties .getProperty (PRO_LOGICAL_UN_DELETE_VALUE );
150+ if (introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_DELETE_VALUE ) != null ) {
151+ this .logicalDeleteValue = introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_DELETE_VALUE );
152+ }
153+ if (introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_UN_DELETE_VALUE ) != null ) {
154+ this .logicalUnDeleteValue = introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_UN_DELETE_VALUE );
155+ }
156+ if (this .logicalDeleteValue == null || this .logicalUnDeleteValue == null ) {
157+ this .logicalDeleteColumn = null ;
158+ warnings .add ("itfsw(逻辑删除插件):" + introspectedTable .getFullyQualifiedTable () + "没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!" );
159+ }
160+
161+ // 4. 优先借助 EnumTypeStatusPlugin 插件,去注解里面解析枚举
143162 if (this .logicalDeleteColumn != null ) {
144163 EnumTypeStatusPlugin .EnumInfo enumInfo = null ;
145164 try {
@@ -155,40 +174,45 @@ public void initialized(IntrospectedTable introspectedTable) {
155174
156175 if (enumInfo != null ) {
157176 // 这个是注释里配置了枚举
158- if (enumInfo .hasItems () && enumInfo .getItems ().size () >= 2 ) {
159- this .logicalUnDeleteValue = enumInfo .getItems ().get (0 ).getOriginalValue ();
160- this .logicalUnDeleteConstName = enumInfo .getItems ().get (0 ).getName ();
161- this .logicalDeleteValue = enumInfo .getItems ().get (1 ).getOriginalValue ();
162- this .logicalDeleteConstName = enumInfo .getItems ().get (1 ).getName ();
163- this .logicalDeleteEnum = enumInfo .generateEnum (commentGenerator , introspectedTable );
164- } else {
165- // 没有在注释里配置读取xml中配置的
166- this .logicalDeleteValue = properties .getProperty (PRO_LOGICAL_DELETE_VALUE );
167- this .logicalUnDeleteValue = properties .getProperty (PRO_LOGICAL_UN_DELETE_VALUE );
168- if (introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_DELETE_VALUE ) != null ) {
169- this .logicalDeleteValue = introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_DELETE_VALUE );
170- }
171- if (introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_UN_DELETE_VALUE ) != null ) {
172- this .logicalUnDeleteValue = introspectedTable .getTableConfigurationProperty (PRO_LOGICAL_UN_DELETE_VALUE );
173- }
174- // 4. 判断逻辑删除值是否配置了
175- if (this .logicalDeleteValue == null || this .logicalUnDeleteValue == null ) {
177+ if (enumInfo .hasItems ()) {
178+ if (enumInfo .getItems ().size () < 2 ) {
176179 this .logicalDeleteColumn = null ;
177- warnings .add ("itfsw(逻辑删除插件):" + introspectedTable .getFullyQualifiedTable () + "没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值 !" );
180+ warnings .add ("itfsw(逻辑删除插件):" + introspectedTable .getFullyQualifiedTable () + "在配合EnumTypeStatusPlugin插件使用时,枚举数量必须大于等于2 !" );
178181 } else {
179- // 兼容处理以前一些老用户配置的Long 和 Float配置问题
180- if (this .logicalDeleteColumn .getFullyQualifiedJavaType ().getFullyQualifiedName ().equals (Long .class .getName ())) {
181- this .logicalUnDeleteValue = this .logicalUnDeleteValue .replaceAll ("L|l" , "" );
182- this .logicalDeleteValue = this .logicalDeleteValue .replaceAll ("L|l" , "" );
183- } else if (this .logicalDeleteColumn .getFullyQualifiedJavaType ().getFullyQualifiedName ().equals (Float .class .getName ())) {
184- this .logicalUnDeleteValue = this .logicalUnDeleteValue .replaceAll ("F|f" , "" );
185- this .logicalDeleteValue = this .logicalDeleteValue .replaceAll ("F|f" , "" );
182+ this .logicalDeleteEnumIndex = -1 ;
183+ this .logicalUnDeleteEnumIndex = -1 ;
184+ for (int i = 0 ; i < enumInfo .getItems ().size (); i ++) {
185+ EnumTypeStatusPlugin .EnumInfo .EnumItemInfo enumItemInfo = enumInfo .getItems ().get (i );
186+ if (this .logicalDeleteValue .equals (enumItemInfo .getOriginalValue ())) {
187+ this .logicalDeleteEnumIndex = i ;
188+ } else if (this .logicalUnDeleteValue .equals (enumItemInfo .getOriginalValue ())) {
189+ this .logicalUnDeleteEnumIndex = i ;
190+ }
191+ }
192+ if (this .logicalDeleteEnumIndex == -1 ) {
193+ this .logicalDeleteColumn = null ;
194+ warnings .add ("itfsw(逻辑删除插件):" + introspectedTable .getFullyQualifiedTable () + "在配合EnumTypeStatusPlugin插件使用时,逻辑删除值“" + this .logicalDeleteValue + "”未在枚举中找到!" );
195+ } else if (this .logicalDeleteEnumIndex == -1 ) {
196+ this .logicalDeleteColumn = null ;
197+ warnings .add ("itfsw(逻辑删除插件):" + introspectedTable .getFullyQualifiedTable () + "在配合EnumTypeStatusPlugin插件使用时,逻辑未删除值“" + this .logicalUnDeleteValue + "”未在枚举中找到!" );
198+ } else {
199+ this .logicalDeleteEnum = enumInfo .generateEnum (commentGenerator , introspectedTable );
186200 }
187201
188- enumInfo .addItem (this .logicalUnDeleteConstName , "未删除" , this .logicalUnDeleteValue );
189- enumInfo .addItem (this .logicalDeleteConstName , "已删除" , this .logicalDeleteValue );
190- this .logicalDeleteEnum = enumInfo .generateEnum (commentGenerator , introspectedTable );
191202 }
203+ } else {
204+ // 兼容处理以前一些老用户配置的Long 和 Float配置问题
205+ if (this .logicalDeleteColumn .getFullyQualifiedJavaType ().getFullyQualifiedName ().equals (Long .class .getName ())) {
206+ this .logicalUnDeleteValue = this .logicalUnDeleteValue .replaceAll ("L|l" , "" );
207+ this .logicalDeleteValue = this .logicalDeleteValue .replaceAll ("L|l" , "" );
208+ } else if (this .logicalDeleteColumn .getFullyQualifiedJavaType ().getFullyQualifiedName ().equals (Float .class .getName ())) {
209+ this .logicalUnDeleteValue = this .logicalUnDeleteValue .replaceAll ("F|f" , "" );
210+ this .logicalDeleteValue = this .logicalDeleteValue .replaceAll ("F|f" , "" );
211+ }
212+
213+ enumInfo .addItem (this .logicalUnDeleteConstName , "未删除" , this .logicalUnDeleteValue );
214+ enumInfo .addItem (this .logicalDeleteConstName , "已删除" , this .logicalDeleteValue );
215+ this .logicalDeleteEnum = enumInfo .generateEnum (commentGenerator , introspectedTable );
192216 }
193217 }
194218 }
@@ -455,7 +479,9 @@ public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, Int
455479 // 常量、枚举和逻辑删除方法跟随 逻辑删除列走
456480 if (this .logicalDeleteColumn .getJavaProperty ().equals (field .getName ())) {
457481 // 1. 添加枚举
458- topLevelClass .addInnerEnum (this .logicalDeleteEnum );
482+ if (PluginTools .getHook (ILogicalDeletePluginHook .class ).logicalDeleteEnumGenerated (this .logicalDeleteColumn ) == false ) {
483+ topLevelClass .addInnerEnum (this .logicalDeleteEnum );
484+ }
459485 // 2. andLogicalDeleted 方法
460486 Method mAndLogicalDeleted = JavaElementGeneratorTools .generateMethod (
461487 METHOD_LOGICAL_DELETED ,
@@ -559,7 +585,7 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec
559585 */
560586 private String getEnumConstantValue (boolean delete ) {
561587 if (this .logicalDeleteEnum != null ) {
562- String enumConstant = this .logicalDeleteEnum .getEnumConstants ().get (delete ? 1 : 0 );
588+ String enumConstant = this .logicalDeleteEnum .getEnumConstants ().get (delete ? this . logicalDeleteEnumIndex : this . logicalUnDeleteEnumIndex );
563589 enumConstant = enumConstant .split ("\\ (" )[0 ];
564590 return this .logicalDeleteEnum .getType ().getShortName () + "." + enumConstant + ".value()" ;
565591 }
0 commit comments