Sfoglia il codice sorgente

ADD: get user by groupId & fuzzy search user in group in UserRPC

zhangxin 7 anni fa
parent
commit
f4a7604f3e

+ 2 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/UserService.java

@@ -36,4 +36,6 @@ public interface UserService {
     Page<UserDTOForMT> getUserOfFuzzySearch(Map<String, String> condition, Pageable pageable);
 
     Page<UserDTOForMT> getByUserPackage(Long serviceId, Long packageId, Pageable pageable);
+
+    Page<UserDTOForMT> getUserPageByGroupId(Long groupId, String keyword, Pageable pageable);
 }

+ 31 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/impl/UserRPCServiceImpl.java

@@ -1,10 +1,12 @@
 package cn.iselab.mooctest.site.service.impl;
 
 import cn.iselab.mooctest.rpc.user.data.UserDTO;
+import cn.iselab.mooctest.site.dao.Group2WorkerDao;
 import cn.iselab.mooctest.site.dao.OpenId2UserIdDao;
 import cn.iselab.mooctest.site.dao.User2RoleDao;
 import cn.iselab.mooctest.site.dao.User2SalesPackageDao;
 import cn.iselab.mooctest.site.data.UserDTOForMT;
+import cn.iselab.mooctest.site.models.Group2Worker;
 import cn.iselab.mooctest.site.models.OpenId2UserId;
 import cn.iselab.mooctest.site.models.User2Role;
 import cn.iselab.mooctest.site.models.User2SalesPackage;
@@ -40,6 +42,9 @@ public class UserRPCServiceImpl implements UserService {
     @Autowired
     private User2SalesPackageDao user2SalesPackageDao;
 
+    @Autowired
+    private Group2WorkerDao group2WorkerDao;
+
     @Override
     public UserDTOForMT createUser(UserDTOForMT userDTOForMT) {
         UserDTO result = Converter.convert(UserDTO.class, userDTOForMT);
@@ -201,4 +206,30 @@ public class UserRPCServiceImpl implements UserService {
         Page<UserDTOForMT> userDTOForMTPage = new PageImpl<UserDTOForMT>(userDTOForMTList, pageable, user2SalesPackages.size());
         return userDTOForMTPage;
     }
+
+    @Override
+    public Page<UserDTOForMT> getUserPageByGroupId(Long groupId, String keyword, Pageable pageable) {
+        List<Group2Worker> group2Workers = group2WorkerDao.findByGroupId(groupId);
+        List<Long> userIds = group2Workers.stream().map(group2Worker -> group2Worker.getParticipantId()).collect(Collectors.toList());
+        List<UserDTOForMT> userDTOForMTs = new ArrayList<>();
+
+        for (Long userId : userIds) {
+            UserDTO userDTO = userService.getUserById(userId);
+            if (keyword.isEmpty() || keyword.equals("")) {
+                userDTOForMTs.add(Converter.convert(UserDTOForMT.class, userDTO));
+            } else {
+                if (userDTO.getEmail().contains(keyword) ||
+                        userDTO.getName().contains(keyword) ||
+                        userDTO.getSchool().contains(keyword) ||
+                        userDTO.getMobile().contains(keyword)) {
+                    userDTOForMTs.add(Converter.convert(UserDTOForMT.class, userDTO));
+                }
+            }
+        }
+        int activePage = pageable.getPageNumber();
+        int pageSize = pageable.getPageSize();
+        List<UserDTOForMT> userDTOForMTList = userDTOForMTs.subList(activePage * pageSize + 1, (activePage + 1) * pageSize);
+        Page<UserDTOForMT> userDTOForMTPage = new PageImpl<UserDTOForMT>(userDTOForMTList, pageable, userIds.size());
+        return userDTOForMTPage;
+    }
 }

+ 14 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/impl/UserServiceImpl.java

@@ -1,5 +1,6 @@
 package cn.iselab.mooctest.site.service.impl;
 
+import cn.iselab.mooctest.site.dao.GroupDao;
 import cn.iselab.mooctest.site.dao.OpenId2UserIdDao;
 import cn.iselab.mooctest.site.dao.UserDao;
 import cn.iselab.mooctest.site.data.UserDTOForMT;
@@ -40,6 +41,8 @@ public class UserServiceImpl extends BaseService implements UserService {
     @Autowired
     private OpenId2UserIdDao openId2UserIdDao;
 
+    @Autowired
+    private GroupDao groupDao;
 
     @Override
     public UserDTOForMT createUser(UserDTOForMT userDTOForMT) {
@@ -194,4 +197,15 @@ public class UserServiceImpl extends BaseService implements UserService {
             }
         });
     }
+
+    @Override
+    public Page<UserDTOForMT> getUserPageByGroupId(Long groupId, String keyword, Pageable pageable) {
+        Page<User> users = groupDao.findByIdAndKeyWord(groupId, keyword, pageable);
+        return users.map(new org.springframework.core.convert.converter.Converter<User, UserDTOForMT>() {
+            @Override
+            public UserDTOForMT convert(User source) {
+                return Converter.convert(UserDTOForMT.class, source);
+            }
+        });
+    }
 }

+ 52 - 53
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/impl/GroupLogicImpl.java

@@ -94,11 +94,11 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
 
     @Override
     public Page<GroupVO> getPageableGroups(long ownerId, Pageable pageable) {
-        Page<Group> groups = groupService.getPageableGroupsByOwnerId(ownerId,pageable);
-        Page<GroupVO> groupVOS = groups.map(new org.springframework.core.convert.converter.Converter<Group, GroupVO>(){
+        Page<Group> groups = groupService.getPageableGroupsByOwnerId(ownerId, pageable);
+        Page<GroupVO> groupVOS = groups.map(new org.springframework.core.convert.converter.Converter<Group, GroupVO>() {
             @Override
             public GroupVO convert(Group group) {
-                GroupVO groupVO = Converter.convert(GroupVO.class,group);
+                GroupVO groupVO = Converter.convert(GroupVO.class, group);
                 groupVO.setWorkerCount(groupService.getWorkerCount(group.getId()));
                 return groupVO;
             }
@@ -120,10 +120,10 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
 
     @Override
     public List<GroupVO> getUserGroups(long participantId) {
-        List<Group> groupList=groupService.getGroupsByParticipantId(participantId);
-        List<GroupVO> groupVOList=new ArrayList<GroupVO>();
+        List<Group> groupList = groupService.getGroupsByParticipantId(participantId);
+        List<GroupVO> groupVOList = new ArrayList<GroupVO>();
         groupList.stream().forEach(group -> {
-            if(group.getId()!=0) {
+            if (group.getId() != 0) {
                 groupVOList.add(Converter.convert(GroupVO.class, group));
             }
         });
@@ -149,10 +149,10 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
     @Override
     public GroupVO getGroupDetail(long groupId) {
         Group group = groupService.getGroup(groupId);
-        if(group==null){
+        if (group == null) {
             throw new HttpNotFoundException(String.format("Group[id=%s] doesn't exist", groupId));
         }
-        GroupVO groupVO = Converter.convert(GroupVO.class,group);
+        GroupVO groupVO = Converter.convert(GroupVO.class, group);
         groupVO.setWorkerCount(groupService.getWorkerCount(groupId));
         return groupVO;
     }
@@ -174,13 +174,13 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
     @Override
     public List<UserVO> getManagerWorkersInGroup(long groupId) {
         Group group = groupService.getGroup(groupId);
-        if(group==null){
+        if (group == null) {
             throw new HttpNotFoundException(String.format("Group[id=%s] doesn't exist", groupId));
         }
-        List<UserVO> UserVOs =new ArrayList<UserVO>();
-        List<User> users=groupService.getUserByGroupId(groupId);
+        List<UserVO> UserVOs = new ArrayList<UserVO>();
+        List<User> users = groupService.getUserByGroupId(groupId);
         users.stream().forEach(user -> {
-            UserVO uv = Converter.convert(UserVO.class,user);
+            UserVO uv = Converter.convert(UserVO.class, user);
             uv.setPassword("");
             UserVOs.add(uv);
         });
@@ -189,11 +189,11 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
 
     @Override
     public Page<UserVO> getManagerWorkersInGroupPageable(long groupId, String keyword, Pageable pageable) {
-        Page<User> users = groupService.getUserByGroupId(groupId,keyword,pageable);
-        Page<UserVO> userVOS = users.map(new org.springframework.core.convert.converter.Converter<User,UserVO>(){
+        Page<UserDTOForMT> users = userService.getUserPageByGroupId(groupId, keyword, pageable);
+        Page<UserVO> userVOS = users.map(new org.springframework.core.convert.converter.Converter<UserDTOForMT, UserVO>() {
             @Override
-            public UserVO convert(User source) {
-                return UserVOWrapper.wrap(source);
+            public UserVO convert(UserDTOForMT source) {
+                return Converter.convert(UserVO.class, source);
             }
         });
         return userVOS;
@@ -229,26 +229,25 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
 
     @Override
     public GroupVO createGroup(GroupVO groupVO) {
-        String name=groupVO.getName();
+        String name = groupVO.getName();
         Long userId = (Long) SecurityUtils.getSubject().getSession().getAttribute("userId");
         groupVO.setOwnerId(userId);
-        for(Group group:groupService.getGroupsByOwnerId(groupVO.getOwnerId())){
-            if(name.equals(group.getName())) {
+        for (Group group : groupService.getGroupsByOwnerId(groupVO.getOwnerId())) {
+            if (name.equals(group.getName())) {
                 throw new HttpBadRequestException("Group name already created by this manager");
             }
         }
-        if(name.length() > 25){
+        if (name.length() > 25) {
             throw new HttpBadRequestException("Group name exceed 25 chars");
-        }else if(name.length() < 1){
+        } else if (name.length() < 1) {
             throw new HttpBadRequestException("Group name cannot be empty.");
-        }
-        else{
-            Group g=Converter.convert(Group.class,groupVO);
+        } else {
+            Group g = Converter.convert(Group.class, groupVO);
             g.setManagerId(-1L);
             g.setIsActive(true);
             Group group = groupService.save(g);
-            groupPermissionService.createGroup(userId,group.getId());
-            return Converter.convert(GroupVO.class,group);
+            groupPermissionService.createGroup(userId, group.getId());
+            return Converter.convert(GroupVO.class, group);
         }
     }
 
@@ -256,17 +255,17 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
     @Override
     public GroupVO joinGroup(HttpServletRequest request, long userId, GroupVO groupVO) {
         Group group = groupService.getGroup(groupVO.getId());
-        if(group.getIsActive() == false) {
+        if (group.getIsActive() == false) {
             throw new HttpBadRequestException("班级已关闭");
         }
         long managerId = group.getOwnerId();
-        if(!groupService.checkManagerGroupSize(managerId)) {
+        if (!groupService.checkManagerGroupSize(managerId)) {
             throw new HttpBadRequestException("老师份额已使用完");
         }
-        if ( groupVO.getCaptcha()!=null && ( groupVO.getCaptcha().equals("summer") || CaptchaUtils.verifyPictureCaptcha(request, groupVO.getCaptcha()))) {
+        if (groupVO.getCaptcha() != null && (groupVO.getCaptcha().equals("summer") || CaptchaUtils.verifyPictureCaptcha(request, groupVO.getCaptcha()))) {
             String managerName = groupVO.getManagerName();
-            UserDTOForMT user= userService.findByUserId(group.getOwnerId());
-            if(!group.getAllowJoin()){
+            UserDTOForMT user = userService.findByUserId(group.getOwnerId());
+            if (!group.getAllowJoin()) {
                 throw new HttpForbiddenException("Group not allowed to join");
             }
 
@@ -276,7 +275,7 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
                         "] is not manager of Group[id=" + groupVO.getId() + "]");
             }
             return groupVOWrapper.wrap(groupService.joinGroup(userId, group));
-        }else{
+        } else {
             throw new HttpBadRequestException("Captcha error");
         }
     }
@@ -323,14 +322,14 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
         if (group == null) {
             throw new HttpNotFoundException("班级[id=" + groupId + "]不存在!");
         }
-        if(group.getIsActive() == false) {
+        if (group.getIsActive() == false) {
             throw new HttpBadRequestException("班级已关闭");
         }
-        if(!groupService.checkManagerGroupSize(group.getOwnerId())) {
+        if (!groupService.checkManagerGroupSize(group.getOwnerId())) {
             throw new HttpBadRequestException("老师份额已使用完");
         }
-        UserDTOForMT user= userService.findByUsername(userName);
-        if(user==null){
+        UserDTOForMT user = userService.findByUsername(userName);
+        if (user == null) {
             throw new HttpNotFoundException("用户[" + userName + "]不存在!");
         }
         if (groupService.checkUserExist(user.getId(), groupId)) {
@@ -339,7 +338,7 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
         if (group.getOwnerId() == user.getId()) {
             throw new IllegalOperationException("Cannot join Group YOU OWN!");
         }
-        UserVO uv=Converter.convert(UserVO.class,groupService.addUserIntoGroup(user.getId(),groupId));
+        UserVO uv = Converter.convert(UserVO.class, groupService.addUserIntoGroup(user.getId(), groupId));
         uv.setPassword("");
         return uv;
     }
@@ -376,17 +375,17 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
         if (group == null) {
             throw new HttpNotFoundException(String.format("Group[id=%s] not exist", groupId));
         }
-        UserDTOForMT user= userService.findByUserId(userId);
-        if(user==null){
-            throw new HttpNotFoundException(String.format("User[id=%s] not exist",userId));
+        UserDTOForMT user = userService.findByUserId(userId);
+        if (user == null) {
+            throw new HttpNotFoundException(String.format("User[id=%s] not exist", userId));
         }
-        if(!groupService.checkUserExist(userId,groupId)){
-            throw new HttpBadRequestException(String.format("User[id=%s] not exists in Group[id=%s]",user.getId(), groupId));
+        if (!groupService.checkUserExist(userId, groupId)) {
+            throw new HttpBadRequestException(String.format("User[id=%s] not exists in Group[id=%s]", user.getId(), groupId));
         }
-        UserVO UserVO =new UserVO();
-        UserVO =Converter.copy(UserVO,groupService.deleteUserFromGroup(userId,groupId));
+        UserVO UserVO = new UserVO();
+        UserVO = Converter.copy(UserVO, groupService.deleteUserFromGroup(userId, groupId));
         UserVO.setPassword("");
-        List<GroupPermission> groupPermissions = groupPermissionService.deleteWorkerPermission(userId,groupId);
+        List<GroupPermission> groupPermissions = groupPermissionService.deleteWorkerPermission(userId, groupId);
         return UserVO;
     }
 
@@ -407,7 +406,7 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
     @Override
     public GroupVO updateAllowJoin(long managerId, GroupVO groupVO) {
         Group group = groupService.getGroup(groupVO.getId());
-        if(managerId != group.getManagerId()){
+        if (managerId != group.getManagerId()) {
             throw new HttpForbiddenException("unauthorized for the task");
         }
 
@@ -416,17 +415,17 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
     }
 
     @Override
-    public GroupVO updateAllowJoin(long groupId){
-        Group group=groupService.getGroup(groupId);
+    public GroupVO updateAllowJoin(long groupId) {
+        Group group = groupService.getGroup(groupId);
         group.setAllowJoin(!group.getAllowJoin());
-        return Converter.convert(GroupVO.class,groupService.save(group));
+        return Converter.convert(GroupVO.class, groupService.save(group));
     }
 
     @Override
     public GroupVO updateGroupActive(Long groupId) {
         Group group = groupService.getGroup(groupId);
         group.setIsActive(false);
-        return Converter.convert(GroupVO.class,groupService.save(group));
+        return Converter.convert(GroupVO.class, groupService.save(group));
     }
 
     @Override
@@ -437,21 +436,21 @@ public class GroupLogicImpl extends BaseLogic implements GroupLogic {
     @Override
     public int getManagerGroupSize(long managerId) {
         ManagerProperty managerProperty = managerPropertyService.getManagerPropertyByUserId(managerId);
-        if(managerProperty == null) {
+        if (managerProperty == null) {
             throw new HttpBadRequestException("manager don't exist");
         }
         return managerProperty.getGroupSize();
     }
 
     @Override
-    public GroupVO joinGroupByQrCode(long userId,long groupId){
+    public GroupVO joinGroupByQrCode(long userId, long groupId) {
         Group group = groupService.getGroup(groupId);
 
         if (group == null) {
             throw new HttpNotFoundException("Group[id=" + groupId + "] doesn't exist");
         }
 
-        if(!group.getAllowJoin()){
+        if (!group.getAllowJoin()) {
             throw new HttpForbiddenException("Group not allowed to join");
         }