|
@@ -1,16 +1,22 @@
|
|
|
package com.mooctest.crowd.domain.repository;
|
|
|
|
|
|
-import com.mooctest.crowd.domain.dao.UserDao;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.mooctest.crowd.domain.dao.*;
|
|
|
+import com.mooctest.crowd.domain.domainobject.Role;
|
|
|
import com.mooctest.crowd.domain.domainobject.User;
|
|
|
import com.mooctest.crowd.domain.exception.UserNotExistException;
|
|
|
-import com.mooctest.crowd.domain.factory.UserFactory;
|
|
|
+import com.mooctest.crowd.domain.model.RolePO;
|
|
|
import com.mooctest.crowd.domain.model.UserPO;
|
|
|
+import com.mooctest.crowd.domain.model.UserToRole;
|
|
|
import com.mooctest.crowd.domain.util.Converter;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.management.relation.RoleNotFoundException;
|
|
|
+import java.sql.Timestamp;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
/**
|
|
|
* @Author: xuexb
|
|
@@ -23,69 +29,111 @@ public class UserRepo implements IUserRepo {
|
|
|
@Autowired
|
|
|
private UserDao userDao;
|
|
|
|
|
|
- @Override
|
|
|
- public User getByMobileNum(String mobileNum) throws UserNotExistException {
|
|
|
+ @Autowired
|
|
|
+ private RoleDao roleDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PermissionDao permissionDao;
|
|
|
|
|
|
- UserPO userPo = userDao.findByMobile(mobileNum);
|
|
|
- System.out.println("user " + userPo);
|
|
|
- if (userPo != null) {
|
|
|
- return Converter.convert(User.class,userPo);
|
|
|
+ @Autowired
|
|
|
+ private UserToRoleDao userToRoleDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RoleToPermissionDao roleToPermissionDao;
|
|
|
+
|
|
|
+// private UserToRole userToRole = new UserToRole();
|
|
|
+
|
|
|
+// private RoleToPermission roleToPermission = new RoleToPermission();
|
|
|
+
|
|
|
+ private Timestamp currentTime = new Timestamp(System.currentTimeMillis());
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public User getByID(Long userId) throws UserNotExistException, RoleNotFoundException {
|
|
|
+ Optional<UserPO> userPOOptional = userDao.findById(userId);
|
|
|
+ if (!userPOOptional.isPresent()) {
|
|
|
+ throw new UserNotExistException();
|
|
|
+ }else{
|
|
|
+ User user = getUserAndRoleByUser(userPOOptional.get());
|
|
|
+ return user;
|
|
|
}
|
|
|
- throw new UserNotExistException();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public User getByID(Long id) throws UserNotExistException {
|
|
|
- if (id.equals(123L)) {
|
|
|
- User user = new User();
|
|
|
- user.setId(id);
|
|
|
- user.setMobile("EXIST_ACCOUNT");
|
|
|
- user.setPassword("PASSWORD");
|
|
|
+ public User getByMobileNum(String mobileNum) throws UserNotExistException, RoleNotFoundException {
|
|
|
+ UserPO userPO = userDao.findByMobile(mobileNum);
|
|
|
+ if (userPO == null) {
|
|
|
+ throw new UserNotExistException();
|
|
|
+ }else {
|
|
|
+ User user = getUserAndRoleByUser(userPO);
|
|
|
return user;
|
|
|
}
|
|
|
- throw new UserNotExistException();
|
|
|
+ }
|
|
|
+
|
|
|
+ private User getUserAndRoleByUser(UserPO userPO) throws RoleNotFoundException {
|
|
|
+ User user = Converter.convert(User.class, userPO);
|
|
|
+ List<UserToRole> userToRoleList = userToRoleDao.findByUserId(user.getId());
|
|
|
+ List<Role> roleList = new ArrayList<>();
|
|
|
+ for (UserToRole userToRole : userToRoleList) {
|
|
|
+ Optional<RolePO> rolePOOptional = roleDao.findById(userToRole.getRoleId());
|
|
|
+ if(rolePOOptional.isPresent()){
|
|
|
+ RolePO rolePO = rolePOOptional.get();
|
|
|
+ roleList.add(Converter.convert(Role.class, rolePO));
|
|
|
+ }else{
|
|
|
+ throw new RoleNotFoundException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ user.setRoleList(roleList);
|
|
|
+ return user;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<User> getByIDList(Long[] ids) throws UserNotExistException {
|
|
|
+ public User saveUserAndRole(User user) {
|
|
|
+ UserPO userPO = userDao.save(Converter.convert(UserPO.class, user));
|
|
|
+ List<Role> roleList = user.getRoleList();
|
|
|
|
|
|
- List<User> userList = new ArrayList<User>();
|
|
|
- for (Long id: ids) {
|
|
|
- User user = getByID(id);
|
|
|
- userList.add(user);
|
|
|
+ for(Role role : roleList){
|
|
|
+ RolePO rolePO = roleDao.findByName(role.getName());
|
|
|
+ UserToRole userToRole = new UserToRole();
|
|
|
+ userToRole.setRoleId(rolePO.getId());
|
|
|
+ userToRole.setUserId(userPO.getId());
|
|
|
+ userToRole.setCreateTime(currentTime);
|
|
|
+ userToRoleDao.save(userToRole);
|
|
|
+// List<Permission> permissionList = role.getPermissionList();
|
|
|
+// for(Permission permission : permissionList){
|
|
|
+// PermissionPO permissionPO = permissionDao.findByName(permission.getName());
|
|
|
+// roleToPermission.setRoleId(rolePO.getId());
|
|
|
+// roleToPermission.setPermissionId(permissionPO.getId());
|
|
|
+// roleToPermission.setCreateTime(currentTime);
|
|
|
+// roleToPermissionDao.save(roleToPermission);
|
|
|
+// }
|
|
|
}
|
|
|
- return userList;
|
|
|
+
|
|
|
+ return Converter.convert(User.class,userPO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public User addUser(User user){
|
|
|
- UserPO userPo = new UserPO();
|
|
|
- userPo.setMobile(user.getMobile());
|
|
|
- userPo.setPassword(user.getPassword());
|
|
|
- UserPO saveUserPO = userDao.save(userPo);
|
|
|
- User returnUser = UserFactory.createUser();
|
|
|
- returnUser.setMobile(saveUserPO.getMobile());
|
|
|
- returnUser.setPassword(saveUserPO.getPassword());
|
|
|
- return returnUser;
|
|
|
+ public User saveUser(User user) {
|
|
|
+ UserPO userPO = userDao.save(Converter.convert(UserPO.class, user));
|
|
|
+ return Converter.convert(User.class,userPO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean removeUser(User user) throws UserNotExistException {
|
|
|
- User byMobileNum = this.getByMobileNum(user.getMobile());
|
|
|
- if(byMobileNum != null){
|
|
|
- return true;
|
|
|
- }
|
|
|
- throw new UserNotExistException();
|
|
|
+ public List<UserPO> getByIDList(List<Long> ids) {
|
|
|
+ Iterable<UserPO> allUserPOById = userDao.findAllById(ids);
|
|
|
+ List<UserPO> userPOList = Lists.newArrayList(allUserPOById);
|
|
|
+ return userPOList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public User updateUser(User user) throws UserNotExistException {
|
|
|
- UserPO saveResultUser = userDao.save(Converter.convert(UserPO.class, user));
|
|
|
- return Converter.convert(User.class,saveResultUser);
|
|
|
+ public void removeUser(User user) {
|
|
|
+ UserPO userPO = Converter.convert(UserPO.class, user);
|
|
|
+ userDao.delete(userPO);
|
|
|
}
|
|
|
|
|
|
- public User save(User user) {
|
|
|
- UserPO userPO = userDao.save(Converter.convert(UserPO.class, user));
|
|
|
- return Converter.convert(User.class,userPO);
|
|
|
- }
|
|
|
+// @Override
|
|
|
+// public User updateUser(User user) {
|
|
|
+// UserPO userPO = userDao.save(Converter.convert(UserPO.class, user));
|
|
|
+// return Converter.convert(User.class,userPO);
|
|
|
+// }
|
|
|
+
|
|
|
}
|