Browse Source

优化逻辑判断写法

suntg 8 years ago
parent
commit
78f4694a51

+ 13 - 0
src/main/java/com/uas/platform/b2c/common/account/dao/RoleDao.java

@@ -12,9 +12,22 @@ import java.util.List;
 @Repository
 public interface RoleDao extends JpaRepository<Role, Long> {
 
+    /**
+     * 获取企业的角色列表
+     * @param enUU
+     * @return
+     */
     @Query("from Role r where r.enUU = :enUU order by id")
     public List<Role> findByEnUU(@Param("enUU") long enUU);
 
+    /**
+     * 获取企业的默认角色的数量
+     * @param enUU
+     * @return
+     */
+    @Query("select count(r) from Role r where r.isdefault = 1 and r.enUU = :enUU")
+    public Integer findIsDefaultCountByEnUU(@Param("enUU") long enUU);
+
     /**
      * 按角色描述查找角色
      *

+ 5 - 15
src/main/java/com/uas/platform/b2c/common/account/service/impl/RoleServiceImpl.java

@@ -59,23 +59,13 @@ public class RoleServiceImpl implements RoleService {
 
     @Override
     public List<Role> findByEnterprise(long enUU) {
-        List<Role> roles = roleDao.findByEnUU(enUU);
-        if (CollectionUtils.isEmpty(roles)) {
-            // 角色为空,说明资料未初始化或初始化失败,需重新init
+        // 默认角色个数
+        Integer defaultRoleCount = roleDao.findIsDefaultCountByEnUU(enUU);
+        if (defaultRoleCount < ALL_DEFAULT_ROLE) {
+            // 如果默认角色个数小于设定的值,进行初始化角色数据
             enterpriseDao.callInitProcedure(enUU);
-            roles = roleDao.findByEnUU(enUU);
-        } else {
-            int count = 0;
-            for (Role role : roles) {
-                if (role.getIsdefault().intValue() == DEFAULT_ROLE) {
-                    count++;
-                }
-            }
-            if (count < ALL_DEFAULT_ROLE) {
-                enterpriseDao.callInitProcedure(enUU);
-                roles = roleDao.findByEnUU(enUU);
-            }
         }
+        List<Role> roles = roleDao.findByEnUU(enUU);
         return roles;
     }