package com.uas.kanban.service.impl; import java.util.List; import java.util.Objects; import org.mongodb.morphia.query.Query; import org.mongodb.morphia.query.UpdateOperations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.uas.kanban.annotation.NotEmpty; import com.uas.kanban.base.BaseService; import com.uas.kanban.dao.ResourcePointDao; import com.uas.kanban.dao.UserDao; import com.uas.kanban.exception.OperationException; import com.uas.kanban.model.ResourcePoint; import com.uas.kanban.model.User; import com.uas.kanban.model.User.Role; import com.uas.kanban.service.UserService; import com.uas.kanban.support.SystemSession; import com.uas.kanban.util.CollectionUtils; /** * 用户 * * @author sunyj * @since 2017年9月2日 下午8:47:20 */ @Service public class UserServiceImpl extends BaseService implements UserService { @Autowired private UserDao userDao; @Autowired private ResourcePointDao resourcePointDao; @Override public User save(@NotEmpty("json") String json) { User user = userDao.parse(json); if (exist(user.getName())) { throw new IllegalStateException("用户已存在"); } if (user.getRole() == null) { user.setRole(Role.Default); } checkValid(user); return userDao.save(user); } @Override public User savePart(String json) { return save(json); } @Override public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException { User user = userDao.parse(json); String code = user.codeNotEmpty(); User rPoint = userDao.findOne(code); if (rPoint == null) { throw new IllegalStateException("用户不存在"); } if (rPoint.getRole() == Role.Admin) { if (!Objects.equals(code, SystemSession.checkUser().getCode())) { throw new OperationException("不允许修改其他管理员"); } } if (Objects.equals(user, rPoint)) { throw new IllegalStateException("未发现任何变更"); } String name = user.getName(); if (!Objects.equals(name, rPoint.getName()) && exist(name)) { throw new IllegalStateException("用户已存在"); } checkValid(user); return userDao.update(user); } @Override public int updatePart(String json) throws IllegalArgumentException, OperationException { return update(json); } /** * 对名称和密码长度进行校验 * * @param user */ private void checkValid(@NotEmpty("user") User user) { String name = user.getName(); String password = user.getPassword(); if (name != null && name.trim().length() < 3) { throw new IllegalArgumentException("名称过短"); } if (password != null && password.trim().length() < 3) { throw new IllegalArgumentException("密码过短"); } checkResourcePointCodes(user.getResourcePointCodes()); } /** * 检查资源点是否存在 * * @param resourcePointCodes * 资源点的 code * @throws IllegalArgumentException * 资源点不存在 */ private void checkResourcePointCodes(List resourcePointCodes) throws IllegalArgumentException { if (CollectionUtils.isEmpty(resourcePointCodes)) { return; } for (String resourcePointCode : resourcePointCodes) { ResourcePoint resourcePoint = resourcePointDao.findOne(resourcePointCode); if (resourcePoint == null) { throw new IllegalArgumentException("资源点不存在:" + resourcePointCode); } } } @Override public User login(@NotEmpty("name") String name, @NotEmpty("password") String password) { Query query = userDao.createQuery(); query.field("name").equal(name); query.field("password").equal(password); long count = query.count(); if (count == 0) { throw new IllegalStateException("名称不存在或密码错误"); } if (count > 1) { throw new IllegalStateException("用户重复"); } User user = query.get(); return user; } @Override public boolean resetPassword(@NotEmpty("password") String password, @NotEmpty("newPassword") String newPassword) { User rPoint = SystemSession.checkUser(); String code = rPoint.getCode(); User user = userDao.findOne(code); if (user == null) { throw new IllegalStateException("用户不存在:" + rPoint); } if (!Objects.equals(password, user.getPassword())) { throw new IllegalStateException("旧密码错误"); } if (Objects.equals(password, newPassword)) { throw new IllegalStateException("新密码与旧密码相同"); } UpdateOperations operations = userDao.createUpdateOperations(); operations.set("password", newPassword); userDao.update(code, operations); return true; } /** * 用户是否已存在 * * @param name * 名称 * @return 是否存在 */ public boolean exist(@NotEmpty("name") String name) { Query query = userDao.createQuery(); query.field("name").equal(name); return query.count() > 0; } @Override public int deleteByCodes(@NotEmpty("codes") List codes) { Query query = userDao.createQuery(); query.field("code").in(codes); return userDao.delete(query); } }