Explorar el Código

增加sql条件对parent的支持

zhouhao hace 6 años
padre
commit
a074300a79

+ 12 - 12
hsweb-system/hsweb-system-organizational/README.md

@@ -13,17 +13,17 @@ createQuery().where("orgId","org-child-in","1234").list();
 
 ```
 
-1. dist-child`(-not)`-in : 参数`(不)`在指定的行政区域以及子节点中
-2. org-child`(-not)`-in : 参数`(不)`在指定的机构以及子节点中
-3. dept-child`(-not)`-in: 参数`(不)`在指定的部门以及子节点中
-3. pos-child`(-not)`-in: 参数`(不)`在指定的岗位以及子节点中
-4. user`(-not)`-in-position`(-child)`: 用户ID`(不)`在岗位中`(包含子级岗位)`
-5. user`(-not)`-in-department`(-child)`: 用户ID`(不)`在部门中`(包含子级岗位)`
-6. user`(-not)`-in-org`(-child)`: 用户ID`(不)`在机构中`(包含子级岗位)`
-7. user`(-not)`-in-dist`(-child)`: 用户ID`(不)`在行政区域中`(包含子级岗位)`
-8. person`(-not)`-in-position`(-child)`: 人员ID`(不)`在岗位中`(包含子级岗位)`
-9. person`(-not)`-in-department`(-child)`: 人员ID`(不)`在部门中`(包含子级岗位)`
-10. person`(-not)`-in-org`(-child)`: 人员ID`(不)`在机构中`(包含子级岗位)`
-11. person`(-not)`-in-dist`(-child)`: 人员ID`(不)`在行政区域中`(包含子级岗位)`
+1. dist-child`(-parent)``(-not)`-in : 参数`(不)`在指定的行政区域以及子(父)节点中
+2. org-child`(-parent)``(-not)`-in : 参数`(不)`在指定的机构以及子(父)节点中
+3. dept-child`(-parent)``(-not)`-in: 参数`(不)`在指定的部门以及子(父)节点中
+3. pos-child`(-parent)``(-not)`-in: 参数`(不)`在指定的岗位以及子(父)节点中
+4. user`(-not)`-in-position`(-child)(-parent)`: 用户ID`(不)`在岗位中`(包含子级(父级)岗位)`
+5. user`(-not)`-in-department`(-child)(-parent)`: 用户ID`(不)`在部门中`(包含子级(父级)岗位)`
+6. user`(-not)`-in-org`(-child)(-parent)`: 用户ID`(不)`在机构中`(包含子级(父级)岗位)`
+7. user`(-not)`-in-dist`(-child)(-parent)`: 用户ID`(不)`在行政区域中`(包含子级(父级)岗位)`
+8. person`(-not)`-in-position`(-child)(-parent)`: 人员ID`(不)`在岗位中`(包含子级(父级)岗位)`
+9. person`(-not)`-in-department`(-child)(-parent)`: 人员ID`(不)`在部门中`(包含子级(父级)岗位)`
+10. person`(-not)`-in-org`(-child)(-parent)`: 人员ID`(不)`在机构中`(包含子级(父级)岗位)`
+11. person`(-not)`-in-dist`(-child)(-parent)`: 人员ID`(不)`在行政区域中`(包含子级(父级)岗位)`
 
 注意: 括号中的内容是可选的

+ 143 - 18
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/terms/CustomSqlTermConfiguration.java

@@ -14,70 +14,105 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class CustomSqlTermConfiguration {
 
+    //=======================================================================
     @Bean
     public InServiceTreeInSqlTerm<String> distInSqlTerm(DistrictService districtService) {
-        return new InServiceTreeInSqlTerm<>(districtService, "dist", "s_district", false);
+        return new InServiceTreeInSqlTerm<>(districtService, "dist", "s_district", false, false);
+    }
+
+    @Bean
+    public InServiceTreeInSqlTerm<String> distInSqlTermParent(DistrictService districtService) {
+        return new InServiceTreeInSqlTerm<>(districtService, "dist", "s_district", false, true);
     }
 
     @Bean
     public InServiceTreeInSqlTerm<String> distNotInSqlTerm(DistrictService districtService) {
-        return new InServiceTreeInSqlTerm<>(districtService, "dist", "s_district", true);
+        return new InServiceTreeInSqlTerm<>(districtService, "dist", "s_district", true, false);
+    }
+
+    @Bean
+    public InServiceTreeInSqlTerm<String> distNotInSqlTermParent(DistrictService districtService) {
+        return new InServiceTreeInSqlTerm<>(districtService, "dist", "s_district", true, true);
     }
 
+    //=======================================================================
     @Bean
     public InServiceTreeInSqlTerm<String> orgInSqlTerm(OrganizationalService organizationalService) {
-        return new InServiceTreeInSqlTerm<>(organizationalService, "org", "s_organization", false);
+        return new InServiceTreeInSqlTerm<>(organizationalService, "org", "s_organization", false, false);
     }
 
     @Bean
     public InServiceTreeInSqlTerm<String> orgNotInSqlTerm(OrganizationalService organizationalService) {
-        return new InServiceTreeInSqlTerm<>(organizationalService, "org", "s_organization", true);
+        return new InServiceTreeInSqlTerm<>(organizationalService, "org", "s_organization", true, false);
+    }
+
+    @Bean
+    public InServiceTreeInSqlTerm<String> orgInSqlTermParent(OrganizationalService organizationalService) {
+        return new InServiceTreeInSqlTerm<>(organizationalService, "org", "s_organization", false, true);
+    }
+
+    @Bean
+    public InServiceTreeInSqlTerm<String> orgNotInSqlTermParent(OrganizationalService organizationalService) {
+        return new InServiceTreeInSqlTerm<>(organizationalService, "org", "s_organization", true, true);
     }
 
+    //=======================================================================
     @Bean
     public InServiceTreeInSqlTerm<String> departmentInSqlTerm(DepartmentService departmentService) {
-        return new InServiceTreeInSqlTerm<>(departmentService, "dept", "s_department", false);
+        return new InServiceTreeInSqlTerm<>(departmentService, "dept", "s_department", false, false);
     }
 
     @Bean
     public InServiceTreeInSqlTerm<String> departmentNotInSqlTerm(DepartmentService departmentService) {
-        return new InServiceTreeInSqlTerm<>(departmentService, "dept", "s_department", true);
+        return new InServiceTreeInSqlTerm<>(departmentService, "dept", "s_department", true, false);
     }
 
     @Bean
-    public PersonInPositionSqlTerm personInPositionSqlTerm() {
-        return new PersonInPositionSqlTerm(false);
+    public InServiceTreeInSqlTerm<String> departmentInSqlTermParent(DepartmentService departmentService) {
+        return new InServiceTreeInSqlTerm<>(departmentService, "dept", "s_department", false, true);
     }
 
     @Bean
-    public PersonInPositionSqlTerm personNotInPositionSqlTerm() {
-        return new PersonInPositionSqlTerm(true);
+    public InServiceTreeInSqlTerm<String> departmentNotInSqlTermParent(DepartmentService departmentService) {
+        return new InServiceTreeInSqlTerm<>(departmentService, "dept", "s_department", true, true);
     }
 
+
     /*====================================================================================*/
 
     @Bean
-    public UserInPositionSqlTerm userInPositionSqlTerm(PositionService positionService) {
+    public UserInSqlTerm userInPositionSqlTerm(PositionService positionService) {
 
         return new UserInPositionSqlTerm(false, false, "user-in-position", positionService);
     }
 
     @Bean
-    public UserInPositionSqlTerm userNotInPositionSqlTerm(PositionService positionService) {
+    public UserInSqlTerm userNotInPositionSqlTerm(PositionService positionService) {
 
         return new UserInPositionSqlTerm(true, false, "user-not-in-position", positionService);
     }
 
     @Bean
-    public UserInPositionSqlTerm userInPositionChildSqlTerm(PositionService positionService) {
+    public UserInSqlTerm userInPositionChildSqlTerm(PositionService positionService) {
+
+        return new UserInPositionSqlTerm(false, true, "user-in-position-child", positionService).forChild();
+    }
+
+    @Bean
+    public UserInSqlTerm userNotInPositionChildSqlTerm(PositionService positionService) {
 
-        return new UserInPositionSqlTerm(false, true, "user-in-position-child", positionService);
+        return new UserInPositionSqlTerm(true, true, "user-not-in-position-child", positionService).forChild();
     }
 
     @Bean
-    public UserInPositionSqlTerm userNotInPositionChildSqlTerm(PositionService positionService) {
+    public UserInSqlTerm userInPositionParentSqlTerm(PositionService positionService) {
 
-        return new UserInPositionSqlTerm(true, true, "user-not-in-position-child", positionService);
+        return new UserInPositionSqlTerm(false, true, "user-in-position-parent", positionService).forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm userNotInPositionParentSqlTerm(PositionService positionService) {
+        return new UserInPositionSqlTerm(true, true, "user-not-in-position-parent", positionService).forParent();
     }
  /*====================================================================================*/
 
@@ -105,6 +140,18 @@ public class CustomSqlTermConfiguration {
         return new UserInPositionSqlTerm(true, true, "person-not-in-position-child", positionService).forPerson();
     }
 
+    @Bean
+    public UserInSqlTerm personInPositionParentSqlTerm(PositionService positionService) {
+
+        return new UserInPositionSqlTerm(false, true, "person-in-position-parent", positionService).forPerson().forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm personNotInPositionParentSqlTerm(PositionService positionService) {
+
+        return new UserInPositionSqlTerm(true, true, "person-not-in-position-parent", positionService).forPerson().forParent();
+    }
+
     /*====================================================================================*/
     @Bean
     public UserInSqlTerm userInDepartmentSqlTerm(DepartmentService departmentService) {
@@ -121,13 +168,26 @@ public class CustomSqlTermConfiguration {
     @Bean
     public UserInSqlTerm userInDepartmentChildSqlTerm(DepartmentService departmentService) {
 
-        return new UserInDepartmentSqlTerm(false, true, "user-in-department-child", departmentService);
+        return new UserInDepartmentSqlTerm(false, true, "user-in-department-child", departmentService).forChild();
     }
 
     @Bean
     public UserInSqlTerm userNotInDepartmentChildSqlTerm(DepartmentService departmentService) {
 
-        return new UserInDepartmentSqlTerm(true, true, "user-not-in-department-child", departmentService);
+        return new UserInDepartmentSqlTerm(true, true, "user-not-in-department-child", departmentService).forChild();
+    }
+
+
+    @Bean
+    public UserInSqlTerm userInDepartmentParentSqlTerm(DepartmentService departmentService) {
+
+        return new UserInDepartmentSqlTerm(false, true, "user-in-department-parent", departmentService).forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm userNotInDepartmentParentSqlTerm(DepartmentService departmentService) {
+
+        return new UserInDepartmentSqlTerm(true, true, "user-not-in-department-parent", departmentService).forParent();
     }
 
     /*====================================================================================*/
@@ -155,6 +215,23 @@ public class CustomSqlTermConfiguration {
         return new UserInDepartmentSqlTerm(true, true, "person-not-in-department-child", departmentService).forPerson();
     }
 
+    @Bean
+    public UserInSqlTerm personInDepartmentParentSqlTerm(DepartmentService departmentService) {
+
+        return new UserInDepartmentSqlTerm(false, true, "person-in-department-parent", departmentService)
+                .forPerson()
+                .forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm personNotInDepartmentParentSqlTerm(DepartmentService departmentService) {
+
+        return new UserInDepartmentSqlTerm(true, true, "person-not-in-department-parent", departmentService)
+                .forPerson()
+                .forParent();
+    }
+
+
     /*====================================================================================*/
     @Bean
     public UserInSqlTerm userInOrgSqlTerm(OrganizationalService organizationalService) {
@@ -181,6 +258,19 @@ public class CustomSqlTermConfiguration {
     }
 
 
+    @Bean
+    public UserInSqlTerm userInOrgParentSqlTerm(OrganizationalService organizationalService) {
+
+        return new UserInOrgSqlTerm(false, true, "user-in-org-parent", organizationalService).forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm userNotInOrgParentSqlTerm(OrganizationalService organizationalService) {
+
+        return new UserInOrgSqlTerm(true, true, "user-not-in-org-parent", organizationalService).forParent();
+    }
+
+
     /*====================================================================================*/
     @Bean
     public UserInSqlTerm personInOrgSqlTerm(OrganizationalService organizationalService) {
@@ -206,6 +296,17 @@ public class CustomSqlTermConfiguration {
         return new UserInOrgSqlTerm(true, true, "person-not-in-org-child", organizationalService).forPerson();
     }
 
+    @Bean
+    public UserInSqlTerm personInOrgParentSqlTerm(OrganizationalService organizationalService) {
+
+        return new UserInOrgSqlTerm(false, true, "person-in-org-parent", organizationalService).forPerson().forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm personNotInOrgParentSqlTerm(OrganizationalService organizationalService) {
+
+        return new UserInOrgSqlTerm(true, true, "person-not-in-org-parent", organizationalService).forPerson().forParent();
+    }
 
     /*====================================================================================*/
     @Bean
@@ -232,6 +333,18 @@ public class CustomSqlTermConfiguration {
         return new UserInDistSqlTerm(true, true, "user-not-in-dist-child", districtService);
     }
 
+    @Bean
+    public UserInSqlTerm userInDistParentSqlTerm(DistrictService districtService) {
+
+        return new UserInDistSqlTerm(false, true, "user-in-dist-parent", districtService).forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm userNotInDistParentSqlTerm(DistrictService districtService) {
+
+        return new UserInDistSqlTerm(true, true, "user-not-in-dist-parent", districtService).forParent();
+    }
+
     /*====================================================================================*/
     @Bean
     public UserInSqlTerm personInDistSqlTerm(DistrictService districtService) {
@@ -257,5 +370,17 @@ public class CustomSqlTermConfiguration {
         return new UserInDistSqlTerm(true, true, "person-not-in-dist-child", districtService).forPerson();
     }
 
+    @Bean
+    public UserInSqlTerm personInDistParentSqlTerm(DistrictService districtService) {
+
+        return new UserInDistSqlTerm(false, true, "person-in-dist-parent", districtService).forPerson().forParent();
+    }
+
+    @Bean
+    public UserInSqlTerm personNotInDistParentSqlTerm(DistrictService districtService) {
+
+        return new UserInDistSqlTerm(true, true, "person-not-in-dist-parent", districtService).forPerson().forParent();
+    }
+
 
 }

+ 5 - 2
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/terms/InServiceTreeInSqlTerm.java

@@ -18,8 +18,11 @@ public class InServiceTreeInSqlTerm<PK> extends TreeStructureSqlTermCustomer {
 
     private String tableName;
 
-    public InServiceTreeInSqlTerm(QueryService<? extends TreeSupportEntity<PK>, PK> service, String prefix, String tableName, boolean not) {
-        super(prefix + "-child-" + (not ? "not-" : "") + "in", not);
+    public InServiceTreeInSqlTerm(QueryService<? extends TreeSupportEntity<PK>, PK> service,
+                                  String prefix,
+                                  String tableName,
+                                  boolean not, boolean parent) {
+        super(prefix + "-" + (parent ? "parent" : "child") + "-" + (not ? "not-" : "") + "in", not, parent);
         this.treeService = service;
         this.tableName = tableName;
     }

+ 4 - 1
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/terms/UserInDepartmentSqlTerm.java

@@ -3,6 +3,7 @@ package org.hswebframework.web.service.organizational.simple.terms;
 import org.hswebframework.ezorm.core.param.Term;
 import org.hswebframework.ezorm.rdb.meta.RDBColumnMetaData;
 import org.hswebframework.ezorm.rdb.render.SqlAppender;
+import org.hswebframework.ezorm.rdb.render.dialect.Dialect;
 import org.hswebframework.ezorm.rdb.render.dialect.term.BoostTermTypeMapper;
 import org.hswebframework.web.dao.mybatis.mapper.ChangedTermValue;
 import org.hswebframework.web.service.organizational.DepartmentService;
@@ -35,6 +36,8 @@ public class UserInDepartmentSqlTerm extends UserInSqlTerm {
     public SqlAppender accept(String wherePrefix, Term term, RDBColumnMetaData column, String tableAlias) {
         ChangedTermValue termValue = createChangedTermValue(term);
 
+        Dialect dialect=column.getTableMetaData().getDatabaseMetaData().getDialect();
+
         SqlAppender appender = new SqlAppender();
         appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp,s_position _pos,s_person _person");
         if (isChild()) {
@@ -49,7 +52,7 @@ public class UserInDepartmentSqlTerm extends UserInSqlTerm {
         List<Object> positionIdList = BoostTermTypeMapper.convertList(column, termValue.getOld());
         if (!positionIdList.isEmpty()) {
             appender.addSpc("and");
-            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_pos.department_id"));
+            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_pos.department_id",dialect));
         }
 
         appender.add(")");

+ 3 - 2
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/terms/UserInDistSqlTerm.java

@@ -3,10 +3,10 @@ package org.hswebframework.web.service.organizational.simple.terms;
 import org.hswebframework.ezorm.core.param.Term;
 import org.hswebframework.ezorm.rdb.meta.RDBColumnMetaData;
 import org.hswebframework.ezorm.rdb.render.SqlAppender;
+import org.hswebframework.ezorm.rdb.render.dialect.Dialect;
 import org.hswebframework.ezorm.rdb.render.dialect.term.BoostTermTypeMapper;
 import org.hswebframework.web.dao.mybatis.mapper.ChangedTermValue;
 import org.hswebframework.web.service.organizational.DistrictService;
-import org.hswebframework.web.service.organizational.PositionService;
 
 import java.util.List;
 
@@ -35,6 +35,7 @@ public class UserInDistSqlTerm extends UserInSqlTerm {
     @Override
     public SqlAppender accept(String wherePrefix, Term term, RDBColumnMetaData column, String tableAlias) {
         ChangedTermValue termValue = createChangedTermValue(term);
+        Dialect dialect=column.getTableMetaData().getDatabaseMetaData().getDialect();
 
         SqlAppender appender = new SqlAppender();
         appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp,s_position _pos,s_person _person,s_department _dept,s_organization _org");
@@ -49,7 +50,7 @@ public class UserInDistSqlTerm extends UserInSqlTerm {
         List<Object> positionIdList = BoostTermTypeMapper.convertList(column, termValue.getOld());
         if (!positionIdList.isEmpty()) {
             appender.addSpc("and");
-            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_org.district_id"));
+            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_org.district_id",dialect));
         }
 
         appender.add(")");

+ 5 - 4
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/terms/UserInOrgSqlTerm.java

@@ -3,10 +3,10 @@ package org.hswebframework.web.service.organizational.simple.terms;
 import org.hswebframework.ezorm.core.param.Term;
 import org.hswebframework.ezorm.rdb.meta.RDBColumnMetaData;
 import org.hswebframework.ezorm.rdb.render.SqlAppender;
+import org.hswebframework.ezorm.rdb.render.dialect.Dialect;
 import org.hswebframework.ezorm.rdb.render.dialect.term.BoostTermTypeMapper;
 import org.hswebframework.web.dao.mybatis.mapper.ChangedTermValue;
 import org.hswebframework.web.service.organizational.OrganizationalService;
-import org.hswebframework.web.service.organizational.PositionService;
 
 import java.util.List;
 
@@ -21,7 +21,7 @@ public class UserInOrgSqlTerm extends UserInSqlTerm {
 
     private boolean not;
 
-    public UserInOrgSqlTerm(boolean not,boolean child, String term, OrganizationalService service) {
+    public UserInOrgSqlTerm(boolean not, boolean child, String term, OrganizationalService service) {
         super(term, service);
         setChild(child);
         this.not = not;
@@ -35,6 +35,7 @@ public class UserInOrgSqlTerm extends UserInSqlTerm {
     @Override
     public SqlAppender accept(String wherePrefix, Term term, RDBColumnMetaData column, String tableAlias) {
         ChangedTermValue termValue = createChangedTermValue(term);
+        Dialect dialect = column.getTableMetaData().getDatabaseMetaData().getDialect();
 
         SqlAppender appender = new SqlAppender();
         appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp,s_position _pos,s_department _dept,s_person _person");
@@ -42,14 +43,14 @@ public class UserInOrgSqlTerm extends UserInSqlTerm {
             appender.addSpc(",s_organization _org");
         }
         appender.addSpc("where _person.u_id=_tmp.person_id and _tmp.position_id = _pos.u_id and _person.u_id=_tmp.person_id and _dept.u_id=_pos.department_id"
-                , "and", createColumnName(column, tableAlias), "=",  isForPerson() ? "_tmp.person_id" : "_person.user_id");
+                , "and", createColumnName(column, tableAlias), "=", isForPerson() ? "_tmp.person_id" : "_person.user_id");
         if (isChild()) {
             appender.addSpc("and _org.u_id=_dept.org_id");
         }
         List<Object> positionIdList = BoostTermTypeMapper.convertList(column, termValue.getOld());
         if (!positionIdList.isEmpty()) {
             appender.addSpc("and");
-            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_dept.org_id"));
+            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_dept.org_id", dialect));
         }
 
         appender.add(")");

+ 3 - 1
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/terms/UserInPositionSqlTerm.java

@@ -3,6 +3,7 @@ package org.hswebframework.web.service.organizational.simple.terms;
 import org.hswebframework.ezorm.core.param.Term;
 import org.hswebframework.ezorm.rdb.meta.RDBColumnMetaData;
 import org.hswebframework.ezorm.rdb.render.SqlAppender;
+import org.hswebframework.ezorm.rdb.render.dialect.Dialect;
 import org.hswebframework.ezorm.rdb.render.dialect.term.BoostTermTypeMapper;
 import org.hswebframework.web.dao.mybatis.mapper.ChangedTermValue;
 import org.hswebframework.web.service.organizational.PositionService;
@@ -34,6 +35,7 @@ public class UserInPositionSqlTerm extends UserInSqlTerm {
     @Override
     public SqlAppender accept(String wherePrefix, Term term, RDBColumnMetaData column, String tableAlias) {
         ChangedTermValue termValue = createChangedTermValue(term);
+        Dialect dialect=column.getTableMetaData().getDatabaseMetaData().getDialect();
 
         SqlAppender appender = new SqlAppender();
         appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp");
@@ -55,7 +57,7 @@ public class UserInPositionSqlTerm extends UserInSqlTerm {
         List<Object> positionIdList = BoostTermTypeMapper.convertList(column, termValue.getOld());
         if (!positionIdList.isEmpty()) {
             appender.addSpc("and");
-            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_tmp.position_id"));
+            termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_tmp.position_id",dialect));
         }
 
         appender.add(")");

+ 39 - 6
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/terms/UserInSqlTerm.java

@@ -2,14 +2,16 @@ package org.hswebframework.web.service.organizational.simple.terms;
 
 import lombok.Getter;
 import lombok.Setter;
-import org.hswebframework.ezorm.core.param.Term;
-import org.hswebframework.ezorm.rdb.meta.RDBColumnMetaData;
+import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.ezorm.rdb.render.SqlAppender;
-import org.hswebframework.ezorm.rdb.render.dialect.term.BoostTermTypeMapper;
+import org.hswebframework.ezorm.rdb.render.dialect.Dialect;
+import org.hswebframework.ezorm.rdb.render.dialect.RenderPhase;
+import org.hswebframework.ezorm.rdb.render.dialect.function.SqlFunction;
 import org.hswebframework.web.commons.entity.TreeSupportEntity;
 import org.hswebframework.web.dao.mybatis.mapper.AbstractSqlTermCustomer;
 import org.hswebframework.web.service.QueryService;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
 import java.util.stream.Collectors;
@@ -21,12 +23,18 @@ import java.util.stream.Collectors;
  * @author zhouhao
  * @since 3.0.0-RC
  */
+@Slf4j
 public abstract class UserInSqlTerm<PK> extends AbstractSqlTermCustomer {
 
 
     @Setter
     @Getter
     private boolean child;
+
+    @Getter
+    @Setter
+    private boolean parent;
+
     @Getter
     @Setter
     private boolean forPerson;
@@ -34,6 +42,16 @@ public abstract class UserInSqlTerm<PK> extends AbstractSqlTermCustomer {
     QueryService<? extends TreeSupportEntity<PK>, PK> treeService;
 
 
+    public UserInSqlTerm<PK> forChild() {
+        setChild(true);
+        return this;
+    }
+
+    public UserInSqlTerm<PK> forParent() {
+       setParent(true);
+        return this;
+    }
+
     public UserInSqlTerm<PK> forPerson() {
         this.forPerson = true;
         return this;
@@ -46,14 +64,15 @@ public abstract class UserInSqlTerm<PK> extends AbstractSqlTermCustomer {
 
     public abstract String getTableName();
 
-    protected Object appendCondition(List<Object> values, String wherePrefix, SqlAppender appender, String column) {
+
+    protected Object appendCondition(List<Object> values, String wherePrefix, SqlAppender appender, String column, Dialect dialect) {
         if (!child) {
             appender.addSpc(column);
             return super.appendCondition(values, wherePrefix, appender);
         } else {
             List<String> paths = getTreePathByTerm(values)
                     .stream()
-                    .map(path -> path.concat("%"))
+                    .map(path -> parent ? path : path.concat("%"))
                     .collect(Collectors.toList());
             int len = paths.size();
             if (len == 0) {
@@ -64,7 +83,21 @@ public abstract class UserInSqlTerm<PK> extends AbstractSqlTermCustomer {
                     if (i > 0) {
                         appender.addSpc("or");
                     }
-                    appender.add(getTableName() + ".path like #{", wherePrefix, ".value.value[", i, "]}");
+                    if (parent) {
+                        SqlFunction function = dialect.getFunction(SqlFunction.concat);
+                        String concat;
+                        if (function == null) {
+                            concat = getTableName() + ".path";
+                            log.warn("数据库方言未支持concat函数,你可以调用Dialect.installFunction进行设置!");
+                        } else {
+                            concat = function.apply(SqlFunction.Param.of(RenderPhase.where, Arrays.asList(getTableName() + ".path", "'%'")));
+                        }
+                        // aaa-vvv-ccc like aaa%
+                        appender.add("#{", wherePrefix, ".value.value[", i, "]}", " like ", concat);
+                    } else {
+                        appender.add(getTableName(), ".path like #{", wherePrefix, ".value.value[", i, "]}");
+                    }
+
                 }
                 appender.add(")");
             }