Commit ac214743 authored by 王向伟's avatar 王向伟

用户管理和角色管理

parent d0c5016a
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Properties;
public class MavenWrapperDownloader {
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL =
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: : " + url);
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip
......@@ -4,6 +4,7 @@ import cn.quantgroup.cashloanflowboss.component.security.Authority;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Proxy;
import javax.persistence.*;
import java.util.List;
......@@ -16,6 +17,7 @@ import java.util.List;
@AllArgsConstructor
@Entity
@Table(name = "permission")
@Proxy(lazy = false)
public class Permission {
/**
......
package cn.quantgroup.cashloanflowboss.api.permissionmodule.repository;
import cn.quantgroup.cashloanflowboss.api.permissionmodule.entity.Permission;
import cn.quantgroup.cashloanflowboss.core.persistence.CashLoanFlowBossPubConfDataSource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author Wang Xiangwei
* @version 2020/3/2
*/
@CashLoanFlowBossPubConfDataSource
@Repository
public interface PermissionRepository extends JpaRepository<Permission,Long> {
}
package cn.quantgroup.cashloanflowboss.api.role.controller;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleModelVo;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleQueryModel;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleVO;
import cn.quantgroup.cashloanflowboss.api.role.service.RoleService;
import cn.quantgroup.cashloanflowboss.component.security.annotiation.Security;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* function:
......@@ -19,28 +23,34 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("role")
public class RoleController {
@Autowired
private RoleService roleService;
@GetMapping("list")
@Security(authorityId = "Role.roleList")
public Result roleList() {
return null;
//@Security(authorityId = "Role.roleList")
public Result<Page<Role>> roleList(@Valid RoleQueryModel roleQueryModel) {
return roleService.getRoleByName(roleQueryModel);
}
@GetMapping("delete")
@Security(authorityId = "Role.delete")
public Result deleteRole() {
return null;
@DeleteMapping("delete/{roleId}")
//@Security(authorityId = "Role.delete")
public Result<Boolean> deleteRole(@PathVariable Long roleId) {
return roleService.deleteRole(roleId);
}
@PostMapping("save")
@Security(authorityId = "Role.saveRole")
public Result saveRole(RoleVO role) {
//@Security(authorityId = "Role.saveRole")
public Result<Boolean> saveRole(@RequestBody @Valid RoleModelVo roleModelVo) {
return roleService.addRole(roleModelVo);
}
return null;
@PutMapping("update")
// @Security(authorityId = "Role.update")
public Result<Boolean> updateRole(@RequestBody @Valid RoleModelVo roleModelVo){
return roleService.modifyRole(roleModelVo);
}
......
......@@ -3,6 +3,7 @@ package cn.quantgroup.cashloanflowboss.api.role.entity;
import cn.quantgroup.cashloanflowboss.api.permissionmodule.entity.Permission;
import cn.quantgroup.cashloanflowboss.core.persistence.Primary;
import lombok.Data;
import org.hibernate.annotations.Proxy;
import javax.persistence.*;
import java.util.List;
......@@ -14,6 +15,7 @@ import java.util.function.UnaryOperator;
@Data
@Entity
@Table(name = "role")
@Proxy(lazy = false)
public class Role extends Primary implements UnaryOperator<Role> {
/**
......@@ -29,6 +31,12 @@ public class Role extends Primary implements UnaryOperator<Role> {
@Column(name = "name")
private String name;
/**
* 角色描述
*/
@Column(name = "[desc]")
private String desc;
/**
* 授权列表
*/
......
package cn.quantgroup.cashloanflowboss.api.role.model;
import cn.quantgroup.cashloanflowboss.component.validator.constraints.NotEmpty;
import lombok.Data;
import java.util.List;
/**
* @author Wang Xiangwei
* @version 2020/3/2
*/
@Data
public class RoleModelVo {
private Long id;
private Long parentId;
@NotEmpty(message = "角色名不能为空")
private String name;
private String desc;
private List<Long> permissions;
}
package cn.quantgroup.cashloanflowboss.api.role.model;
import cn.quantgroup.cashloanflowboss.core.base.Pagination;
import lombok.Data;
/**
* @author Wang Xiangwei
* @version 2020/3/2
*/
@Data
public class RoleQueryModel extends Pagination {
private String name;
}
......@@ -3,7 +3,11 @@ package cn.quantgroup.cashloanflowboss.api.role.repository;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.core.persistence.CashLoanFlowBossDataSource;
import cn.quantgroup.cashloanflowboss.core.persistence.CashLoanFlowBossPubConfDataSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
......@@ -11,5 +15,10 @@ import org.springframework.stereotype.Repository;
*/
@CashLoanFlowBossPubConfDataSource
@Repository
public interface RoleRepository extends JpaRepository<Role, Long> {
public interface RoleRepository extends JpaRepository<Role, Long> , JpaSpecificationExecutor<Role> {
Role getByName(String name);
}
package cn.quantgroup.cashloanflowboss.api.role.service;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.role.repository.RoleRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleModelVo;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleQueryModel;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import org.springframework.data.domain.Page;
import java.util.List;
/**
* @author Wang Xiangwei
* @version 2020/3/2
*/
public interface RoleService {
@Slf4j
@Service
public class RoleService {
Result<Boolean> addRole(RoleModelVo roleModelVo);
@Autowired
private RoleRepository roleRepository;
Result<Page<Role>> getRoleByName(RoleQueryModel roleQueryModel);
/**
* 获取用户角色列表
*
* @param ids 角色ID
* @return
*/
public List<Role> getRoles(List<Long> ids) {
return this.roleRepository.findAll(ids);
}
Result<Boolean> modifyRole(RoleModelVo roleModelVo);
Result<Boolean> deleteRole(Long roleId);
}
package cn.quantgroup.cashloanflowboss.api.role.service;
import cn.quantgroup.cashloanflowboss.api.permissionmodule.entity.Permission;
import cn.quantgroup.cashloanflowboss.api.permissionmodule.repository.PermissionRepository;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleModelVo;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleQueryModel;
import cn.quantgroup.cashloanflowboss.api.role.repository.RoleRepository;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleRepository roleRepository;
@Autowired
private PermissionRepository permissionRepository;
@Override
@Transactional(rollbackFor = Exception.class)
public Result<Boolean> addRole(RoleModelVo roleModelVo) {
final String LOG_PRE="RoleServiceImpl.addRole";
log.info("{} 添加角色 roleModelVo={}",LOG_PRE, roleModelVo);
String name = roleModelVo.getName();
Role exist = roleRepository.getByName(name);
if(exist != null){
log.error("{} 已存在角色 name={}",LOG_PRE,name);
return Result.buildFail(name+"已存在,请更换角色名");
}
Role role = new Role();
BeanUtils.copyProperties(roleModelVo,role);
Long parentId = roleModelVo.getParentId();
if(parentId != null){
log.info("{},父角色查询 parentId={}",LOG_PRE,parentId);
Role parent = roleRepository.getOne(parentId);
role.setParent(parent);
}
List<Long> permissions = roleModelVo.getPermissions();
if(CollectionUtils.isNotEmpty(permissions)){
List<Permission> all = permissionRepository.findAll(permissions);
log.info("{} 权限ids={} 对应的权限size={}",LOG_PRE,permissions,all.size());
role.setPermissions(all);
}
roleRepository.save(role);
return Result.buildSuccess(true);
}
@Override
public Result<Page<Role>> getRoleByName(RoleQueryModel roleQueryModel) {
final String LOG_PRE="RoleServiceImpl.getRoleByName";
log.info("{} 查询角色 roleQueryModel={}",LOG_PRE,roleQueryModel);
Page<Role> rolePage= roleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.isNotEmpty(roleQueryModel.getName())) {
predicates.add(criteriaBuilder.equal(root.get("name"),roleQueryModel.getName()));
}
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
// 指定排序
criteriaQuery.orderBy(criteriaBuilder.desc(root.get("id")));
return criteriaQuery.getRestriction();
}, new PageRequest(roleQueryModel.getPageNumber(), roleQueryModel.getPageSize()));
return Result.buildSuccess(rolePage);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Result<Boolean> modifyRole(RoleModelVo roleModelVo) {
final String LOG_PRE="RoleServiceImpl.modifyRole";
log.info("{} 更新角色 roleQueryModel={}",LOG_PRE, roleModelVo);
Long id = roleModelVo.getId();
if(id == null){
log.error("{} 更新的角色没有Id,无法更新 roleModelVo={}",LOG_PRE, roleModelVo);
return Result.buildFail("角色没有id,无法更新");
}
Role exist = roleRepository.getOne(id);
if(exist == null){
log.error("{},没有对应的角色 id={}",LOG_PRE,id);
return Result.buildFail("没有对应的角色");
}
BeanUtils.copyProperties(roleModelVo,exist);
Long parentId = roleModelVo.getParentId();
if(parentId != null){
Role parent = roleRepository.getOne(parentId);
log.info("{},id={},parentId={},父角色为{}",LOG_PRE,id,parentId,parent);
exist.setParent(parent);
}
List<Long> permissions = roleModelVo.getPermissions();
if(CollectionUtils.isEmpty(permissions)){
exist.setPermissions(null);
}else {
List<Permission> permissionList = permissionRepository.findAll(permissions);
exist.setPermissions(permissionList);
}
exist.setUpdateTime(new Date());
roleRepository.save(exist);
return Result.buildSuccess(true);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Result<Boolean> deleteRole(Long roleId) {
final String LOG_PRE="RoleServiceImpl.deleteRole";
log.info("{} 删除角色 roleId={}",LOG_PRE,roleId);
roleRepository.delete(roleId);
return Result.buildSuccess(true);
}
}
package cn.quantgroup.cashloanflowboss.api.user.controller;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserRank;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.model.QueryUserListModel;
import cn.quantgroup.cashloanflowboss.api.user.model.*;
import cn.quantgroup.cashloanflowboss.core.base.Pagination;
import cn.quantgroup.cashloanflowboss.api.user.model.RegisterUserFormModel;
import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo;
import cn.quantgroup.cashloanflowboss.api.user.model.UserInfoModel;
import cn.quantgroup.cashloanflowboss.api.user.service.UserService;
import cn.quantgroup.cashloanflowboss.component.security.annotiation.Security;
import cn.quantgroup.cashloanflowboss.component.validator.constraints.NotEmpty;
......@@ -18,6 +17,7 @@ import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -27,6 +27,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* Created by WeiWei on 2019/7/22.
*/
......@@ -147,4 +150,57 @@ public class UserController {
return true;
}
/**
* 添加用户
* @param userModelVo
* @return
*/
// @Security(authorityId = "User.add")
@PostMapping(value = "/add")
public Result<Boolean> addUser(@RequestBody @Valid UserModelVo userModelVo){
return userService.addUser(userModelVo);
}
@PutMapping("/modify")
// @Security(authorityId = "User.update")
public Result<Boolean> modifyUser(@RequestBody @Valid UserModelVo userModelVo){
return userService.modifyUser(userModelVo);
}
/**
* 根据用户名、角色分页查询
* @param queryUserListModel
* @return
*/
@GetMapping("/list/get")
//@Security(authorityId = "User.list")
public Result<Page<User>> UserList(@Valid QueryUserListModel queryUserListModel){
return userService.getUserList(queryUserListModel);
}
@GetMapping("/rank")
public Result<List<String>> userRank(){
List<String> list = new ArrayList<>();
for (UserRank value : UserRank.values()) {
if(value == UserRank.ADMINISTRATOR || value == UserRank.SUPER_ADMINISTRATOR){
continue;
}
list.add(value.name());
}
return Result.buildSuccess(list);
}
@GetMapping("/status")
public Result<List<String>> userStatus(){
List<String> list = new ArrayList<>();
for (UserStatus value : UserStatus.values()) {
list.add(value.name());
}
return Result.buildSuccess(list);
}
}
......@@ -3,13 +3,17 @@ package cn.quantgroup.cashloanflowboss.api.user.entity;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserRank;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.api.user.model.UserModelVo;
import cn.quantgroup.cashloanflowboss.core.persistence.Primary;
import cn.quantgroup.cashloanflowboss.utils.MD5Tools;
import lombok.Data;
import org.hibernate.annotations.Proxy;
import org.springframework.beans.BeanUtils;
import javax.persistence.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
/**
* Created by WeiWei on 2019/7/22.
......@@ -17,6 +21,7 @@ import java.util.Set;
@Data
@Entity
@Table(name = "user")
@Proxy(lazy = false)
public class User extends Primary {
/**
......@@ -70,4 +75,18 @@ public class User extends Primary {
@Column(name = "last_login_time")
private Date lastLoginTime;
public static User valueOf(UserModelVo userModelVo, Role role) {
if (userModelVo == null) {
return null;
}
User user = new User();
BeanUtils.copyProperties(userModelVo, user);
user.setPassword(MD5Tools.md5(userModelVo.getPassword()));
if (role != null) {
user.setRoles(Collections.singletonList(role));
}
return user;
}
}
......@@ -12,4 +12,6 @@ import lombok.Data;
@Data
public class QueryUserListModel extends Pagination {
private String nickname;
private Long roleId;
}
package cn.quantgroup.cashloanflowboss.api.user.model;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserRank;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.component.validator.constraints.NotEmpty;
import lombok.Data;
/**
* @author Wang Xiangwei
* @version 2020/3/2
*/
@Data
public class UserModelVo {
private Long id;
/**
* 用户名
*/
@NotEmpty(message = "用户名不能为空")
private String username;
/**
* 昵称
*/
@NotEmpty(message = "用户昵称不能为空")
private String nickname;
/**
* 密码
*/
@NotEmpty(message = "用户密码不能为空")
private String password;
/**
* 渠道ID
*/
private Long channelId;
/**
* 用户级别
*/
@NotEmpty(message = "用户级别不能为空")
private UserRank rank;
private Long roleId;
/**
* 用户状态
*/
@NotEmpty(message = "用户活跃状态不能为空")
private UserStatus status;
}
......@@ -3,8 +3,12 @@ package cn.quantgroup.cashloanflowboss.api.user.repository;
import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.core.persistence.CashLoanFlowBossDataSource;
import cn.quantgroup.cashloanflowboss.core.persistence.CashLoanFlowBossPubConfDataSource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
......@@ -23,4 +27,15 @@ public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificat
User getUserByUsername(String username);
@Query(value = "select user.* from user,user_role_mapping urm where urm.user_id= user.id "+
"and IF ( ?1 =\"\",1=1, user.nickname = ?1) " +
"and IF (?2 =\"\", 1=1, urm.role_id= ?2) "
+ "ORDER BY ?#{#pageable}",
countQuery = "select count(user.id) from user,user_role_mapping urm where urm.user_id= user.id "+
"and IF ( ?1 =\"\",1=1, user.nickname = ?1) " +
"and IF (?2 =\"\", 1=1, urm.role_id= ?2) ",
nativeQuery = true)
Page<User> getUserByNicknameAndRoleId( String nickname, String roleId, Pageable pageable);
}
package cn.quantgroup.cashloanflowboss.api.user.service;
import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.model.QueryUserListModel;
import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo;
import cn.quantgroup.cashloanflowboss.api.user.model.UserInfoModel;
import cn.quantgroup.cashloanflowboss.api.user.model.*;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import cn.quantgroup.cashloanflowboss.core.base.Tuple;
import org.springframework.data.domain.Page;
......@@ -33,4 +32,23 @@ public interface UserService {
User saveUserInfo(UserInfoModel user);
Tuple<Boolean,String> cleanUserActiveOrder(String mobile);
/**
* 添加用户,账号校验
* @param
* @return
*/
Result<Boolean> addUser(UserModelVo userModelVo);
/**
* 更新用户,账号不能更改
* @param
* @return
*/
Result<Boolean> modifyUser(UserModelVo userModelVo);
Result<Page<User>> getUserList(QueryUserListModel queryUserListModel);
}
package cn.quantgroup.cashloanflowboss.api.user.service;
import cn.quantgroup.cashloanflowboss.api.login.model.Principal;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.role.repository.RoleRepository;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.model.QueryUserListModel;
import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo;
import cn.quantgroup.cashloanflowboss.api.user.model.UserInfoModel;
import cn.quantgroup.cashloanflowboss.api.user.model.UserModelVo;
import cn.quantgroup.cashloanflowboss.api.user.repository.UserRepository;
import cn.quantgroup.cashloanflowboss.core.Application;
import cn.quantgroup.cashloanflowboss.core.asserts.Assert;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import cn.quantgroup.cashloanflowboss.core.base.Tuple;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus;
import cn.quantgroup.cashloanflowboss.spi.user.service.XyqbUserService;
......@@ -21,20 +25,20 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
* Created by WeiWei on 2019/7/22.
*/
@Slf4j
@Service
public class UserServiceImpl implements UserService{
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
......@@ -43,6 +47,8 @@ public class UserServiceImpl implements UserService{
@Autowired
private XyqbUserService xyqbUserService;
@Autowired
private RoleRepository roleRepository;
/**
......@@ -197,12 +203,93 @@ public class UserServiceImpl implements UserService{
@Async("commonAsyncExecutor")
//@OperationAnno(channelNo = "#this[0]", opt = OptEnumName.USER_ORDER_CLEAN, succSPEL = "#this.key", optDetailSPEL = "#this.value")
@Override
public Tuple<Boolean,String> cleanUserActiveOrder(String mobile) {
public Tuple<Boolean, String> cleanUserActiveOrder(String mobile) {
XUser user = xyqbUserService.findUserByPhoneNo(mobile);
if (user == null) {
log.info("cleanUserOrder,清除用户活跃订单失败,未找到用户 phoneNo={}", mobile);
return new Tuple<>(false,"清除用户活跃订单失败,未找到用户");
return new Tuple<>(false, "清除用户活跃订单失败,未找到用户");
}
return xyqbCenterService.cleanUserActiveOrder(user.getId());
}
@Override
@Transactional
public Result<Boolean> addUser(UserModelVo userModelVo) {
final String LOG_PRE = "UserServiceImpl.addUser";
log.info("{} 添加用户 rank={}", LOG_PRE, userModelVo);
String username = userModelVo.getUsername();
User exist = this.getUser(username);
if (exist != null) {
log.error("{},用户已存在 userName={}", LOG_PRE, username);
return Result.buildFail(username + "用户已存在");
}
Long roleId = userModelVo.getRoleId();
Role role = roleRepository.getOne(roleId);
if (role == null) {
log.error("{},不存在对应的角色 roleId={}", LOG_PRE, roleId);
return Result.buildFail("不存在对应的角色");
}
User user = User.valueOf(userModelVo, role);
userRepository.save(user);
return Result.buildSuccess(true);
}
@Override
@Transactional
public Result<Boolean> modifyUser(UserModelVo userModelVo) {
final String LOG_PRE = "UserServiceImpl.modifyUser";
Long id = userModelVo.getId();
if (id == null) {
log.error("{} 缺少主键id, userModelVo={}", LOG_PRE, userModelVo);
return Result.buildFail("缺少用户Id");
}
User exist = userRepository.getOne(id);
if (exist == null) {
log.error("{}不存在相应的用户,不能更新 userModelVo={}", LOG_PRE, userModelVo);
return Result.buildFail("不存在相应的用户,不能更新");
}
String existUsername = exist.getUsername();
String username = userModelVo.getUsername();
if (!existUsername.equals(username)) {
log.error("{}用户名不能修改,userModelVo={}", LOG_PRE, userModelVo);
return Result.buildFail("用户名不能修改");
}
Long roleId = userModelVo.getRoleId();
Role role = roleRepository.getOne(roleId);
if (role == null) {
log.error("{},不存在对应的角色 roleId={}", LOG_PRE, roleId);
return Result.buildFail("不存在对应的角色");
}
BeanUtils.copyProperties(userModelVo, exist);
exist.setPassword(MD5Tools.md5(userModelVo.getPassword()));
exist.setRoles(Collections.singletonList(role));
exist.setUpdateTime(new Date());
userRepository.save(exist);
return Result.buildSuccess(true);
}
@Override
public Result<Page<User>> getUserList(QueryUserListModel queryUserListModel) {
final String LOG_PRE = "UserServiceImpl.getUserList";
log.info("{},查询用户列表,queryUserListModel={}", LOG_PRE, queryUserListModel);
String nickname = queryUserListModel.getNickname();
Long roleId = queryUserListModel.getRoleId();
PageRequest pageRequest = new PageRequest(queryUserListModel.getPageNumber(), queryUserListModel.getPageSize(), new Sort(Sort.Direction.DESC, "id"));
Page<User> userPage = userRepository.getUserByNicknameAndRoleId(nickname == null ? "" : nickname, roleId == null ? "" : roleId.toString(),
pageRequest);
return Result.buildSuccess(userPage);
}
}
......@@ -55,6 +55,10 @@ public class ChannelApplyInfoStrategyTest extends CashLoanFlowBossApplicationTes
channelApplyInfoStrategy.setChannelUserId(true);
channelConfService.editChannelConfInfo(channelConf);
//数据恢复
channelApplyInfoStrategy.setChannelUserId(false);
channelConfService.editChannelConfInfo(channelConf);
}
......
package cn.quantgroup.cashloanflowboss.service.contract;
import cn.quantgroup.cashloanflowboss.CashLoanFlowBossApplicationTests;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.role.model.RoleQueryModel;
import cn.quantgroup.cashloanflowboss.api.role.service.RoleService;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
/**
* @author Wang Xiangwei
* @version 2020/3/2
*/
public class RoleTest extends CashLoanFlowBossApplicationTests {
@Autowired
private RoleService roleService;
@Test
public void roleTest(){
// RoleModelVo roleModel = new RoleModelVo();
// roleModel.setName("测试角色");
// roleModel.setDesc("测试");
// Result<Role> roleResult = roleService.addRole(roleModel);
RoleQueryModel roleQueryModel = new RoleQueryModel();
// roleQueryModel.setName("测试角色");
roleQueryModel.setPageNumber(1);
roleQueryModel.setPageSize(5);
Result<Page<Role>> roleByName = roleService.getRoleByName(roleQueryModel);
// roleModel.setName("测试");
// Long id = roleResult.getData().getId();
// roleModel.setId(id);
// roleService.modifyRole(roleModel);
//
// Result<Boolean> booleanResult = roleService.deleteRole(roleResult.getData().getId());
}
}
package cn.quantgroup.cashloanflowboss.service.contract;
import cn.quantgroup.cashloanflowboss.CashLoanFlowBossApplicationTests;
import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.model.QueryUserListModel;
import cn.quantgroup.cashloanflowboss.api.user.service.UserService;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
/**
* @author Wang Xiangwei
* @version 2020/3/2
*/
public class UserTest extends CashLoanFlowBossApplicationTests {
@Autowired
UserService userService;
@Test
public void testUser(){
// UserModelVo userModel = new UserModelVo();
//
// userModel.setNickname("测试账号");
// userModel.setUsername("test11111111111");
// userModel.setPassword("123456");
// userModel.setRank(UserRank.OPERATOR);
// userModel.setRoleId(9L);
// userModel.setStatus(UserStatus.ENABLED);
// Result<User> result = userService.addUser(userModel);
//
// User rank = result.getData();
//
// userModel.setId(rank.getId());
// userModel.setNickname("测试账号123");
//
// Result<User> result1 = userService.modifyUser(userModel);
QueryUserListModel model = new QueryUserListModel();
// model.setNickname("小丽");
model.setRoleId(9L);
model.setPageNumber(1);
model.setPageSize(4);
Result<Page<User>> userList = userService.getUserList(model);
System.out.println(userList.getData().getContent().size());
// userService.removeUser(rank.getId());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment