|
|
@@ -1,18 +1,42 @@
|
|
|
package com.zswl.dataservice.service.user.impl;
|
|
|
|
|
|
import com.zswl.dataservice.config.SystemDefaultConfig;
|
|
|
+import com.zswl.dataservice.dao.DepartmentDao;
|
|
|
+import com.zswl.dataservice.dao.RoleDao;
|
|
|
import com.zswl.dataservice.dao.UserDao;
|
|
|
+import com.zswl.dataservice.dao.mqtt.ProjectInfoDao;
|
|
|
+import com.zswl.dataservice.dataConfig.ResultMessage;
|
|
|
+import com.zswl.dataservice.domain.mqtt.ProjectInfo;
|
|
|
+import com.zswl.dataservice.domain.user.Department;
|
|
|
+import com.zswl.dataservice.domain.user.Role;
|
|
|
import com.zswl.dataservice.domain.user.User;
|
|
|
+import com.zswl.dataservice.model.mqtt.ProjectInfoModel;
|
|
|
+import com.zswl.dataservice.model.user.*;
|
|
|
import com.zswl.dataservice.service.base.SuperService;
|
|
|
+import com.zswl.dataservice.service.mqtt.ProjectInfoService;
|
|
|
import com.zswl.dataservice.type.ResultState;
|
|
|
+import com.zswl.dataservice.type.UserState;
|
|
|
+import com.zswl.dataservice.type.UserType;
|
|
|
+import com.zswl.dataservice.utils.ValidateResult;
|
|
|
+import com.zswl.dataservice.utils.ValidateUtils;
|
|
|
+import com.zswl.dataservice.utils.bean.BeanUtils;
|
|
|
+import com.zswl.dataservice.utils.page.PageEntityUtil;
|
|
|
import com.zswl.dataservice.utils.result.ResultContent;
|
|
|
+import io.swagger.v3.oas.annotations.media.Schema;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
/**
|
|
|
* 用户管理
|
|
|
*
|
|
|
@@ -26,6 +50,144 @@ public class UserManagerServiceImpl extends SuperService {
|
|
|
@Autowired
|
|
|
private UserDao userDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ RoleDao roleDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProjectInfoDao projectInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DepartmentDao departmentDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DepartmentServiceImpl departmentService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProjectInfoService projectInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RoleServiceImpl roleService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加编辑用户
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent addUser(UserCountParam param) {
|
|
|
+ initDefaultUser(param);
|
|
|
+ String loginName = param.getPhone();
|
|
|
+ boolean b = ValidateUtils.isPhoneNumber(param.getPhone());
|
|
|
+ if (!b) {
|
|
|
+ return ResultContent.buildFail("电话号码格式不正确");
|
|
|
+ }
|
|
|
+ if (!param.getNewPassWord().equals(param.getConfirmPass())) {
|
|
|
+ return ResultContent.buildFail("密码和验证密码不一致");
|
|
|
+ }
|
|
|
+ ValidateResult validateResult = ValidateUtils.validatePassWord(param.getNewPassWord());
|
|
|
+ if (!validateResult.isSuccess()) {
|
|
|
+ return ResultContent.buildFail(validateResult.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ User user = null;
|
|
|
+ User temp = userDao.findByLoginName(loginName);
|
|
|
+ if (StringUtils.isNotEmpty(param.getId())) {
|
|
|
+ user = userDao.findTopById(param.getId());
|
|
|
+ if (ObjectUtils.isEmpty(user)) {
|
|
|
+ return ResultContent.buildFail(String.format("用户ID不存在:%s", param.getId()));
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(user.getId())) {
|
|
|
+ return ResultContent.buildFail(String.format("该账号已存在:%s", loginName));
|
|
|
+ }
|
|
|
+ BeanUtils.copyProperties(param, user);
|
|
|
+ } else {
|
|
|
+ if (ObjectUtils.isNotEmpty(temp)) {
|
|
|
+ return ResultContent.buildFail(String.format("该账号已存在:%s", loginName));
|
|
|
+ }
|
|
|
+ PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
|
+ user = new User();
|
|
|
+ BeanUtils.copyProperties(param, user);
|
|
|
+ user.setLoginName(loginName);
|
|
|
+ user.setPassWord(passwordEncoder.encode(param.getNewPassWord()));
|
|
|
+ user.setUserType(UserType.Consumer);
|
|
|
+ user.setIsAdmin(Boolean.FALSE);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 项目 角色 部门
|
|
|
+ List<ProjectInfo> projectInfos = new ArrayList<>();
|
|
|
+ List<String> projectInfoIds = param.getProjectInfos();
|
|
|
+ if (ObjectUtils.isNotEmpty(projectInfoIds)) {
|
|
|
+ projectInfos = projectInfoDao.findAllById(projectInfoIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 角色
|
|
|
+ List<Role> roles = new ArrayList<>();
|
|
|
+ List<String> roleIds = param.getRoles();
|
|
|
+ if (ObjectUtils.isNotEmpty(roleIds)) {
|
|
|
+ roles = roleDao.findAllById(roleIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 部门
|
|
|
+ Department department = null;
|
|
|
+ String departmentId = param.getDepartmentId();
|
|
|
+ if (StringUtils.isNotEmpty(departmentId)) {
|
|
|
+ department = departmentDao.findTopById(departmentId);
|
|
|
+ }
|
|
|
+ user.setProjectInfos(projectInfos);
|
|
|
+ user.setRoles(roles);
|
|
|
+ user.setDepartment(department);
|
|
|
+ userDao.save(user);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户列表
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @param pageable
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<Page<UserInfoModel>> page(UserSearchParams param, Pageable pageable) {
|
|
|
+ initSearchParam(param);
|
|
|
+ Page<User> page = userDao.page(pageable, param);
|
|
|
+ return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户详情
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent<UserInfoModel> getUserInfo(String id) {
|
|
|
+ User user = userDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(user)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ UserInfoModel model = toModel(user);
|
|
|
+ return ResultContent.buildSuccess(model);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除用户
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent delUser(String id) {
|
|
|
+ User user = userDao.findTopById(id);
|
|
|
+ if (ObjectUtils.isEmpty(user)) {
|
|
|
+ return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
|
|
|
+ }
|
|
|
+ if (user.getIsAdmin() != null && user.getIsAdmin()) {
|
|
|
+ return ResultContent.buildFail(String.format("该用户不能删除"));
|
|
|
+ }
|
|
|
+ if (user.getId().equals(getCurrentUserId())) {
|
|
|
+ return ResultContent.buildFail("不能删除当前用户");
|
|
|
+ }
|
|
|
+ userDao.delete(user);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 重置用户密码
|
|
|
@@ -43,4 +205,45 @@ public class UserManagerServiceImpl extends SuperService {
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
+ public UserInfoModel toModel(User user) {
|
|
|
+ UserInfoModel model = new UserInfoModel();
|
|
|
+ if (ObjectUtils.isNotEmpty(user)) {
|
|
|
+ BeanUtils.copyProperties(user, model);
|
|
|
+
|
|
|
+ // 部门信息
|
|
|
+ Department department = user.getDepartment();
|
|
|
+ if (ObjectUtils.isNotEmpty(department)) {
|
|
|
+ DepartmentModel departmentModel = departmentService.toModel(department);
|
|
|
+ model.setDepartment(departmentModel);
|
|
|
+ model.setDepartments(departmentService.getParents(department.getId()));
|
|
|
+ }
|
|
|
+ // 角色信息
|
|
|
+ List<Role> roles = user.getRoles();
|
|
|
+ if (ObjectUtils.isNotEmpty(roles)) {
|
|
|
+ List<RoleModel> roleModels = new ArrayList<>();
|
|
|
+ roles.stream().forEach(it -> {
|
|
|
+ RoleModel roleModel = roleService.toModel(it);
|
|
|
+ if (roleModel != null) {
|
|
|
+ roleModels.add(roleModel);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ model.setRoles(roleModels);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 项目信息
|
|
|
+ List<ProjectInfo> projectInfos = user.getProjectInfos();
|
|
|
+ if (ObjectUtils.isNotEmpty(projectInfos)) {
|
|
|
+ List<ProjectInfoModel> projectInfoModels = new ArrayList<>();
|
|
|
+ projectInfos.stream().forEach(it -> {
|
|
|
+ ProjectInfoModel projectInfoModel = projectInfoService.toModel(it);
|
|
|
+ if (projectInfoModel != null) {
|
|
|
+ projectInfoModels.add(projectInfoModel);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ model.setProjectInfos(projectInfoModels);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
}
|