All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rexsheng.springboot.faster.system.user.application.UserServiceImpl Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.user.application;

import com.github.rexsheng.springboot.faster.common.domain.PagedList;
import com.github.rexsheng.springboot.faster.spring.SpringContext;
import com.github.rexsheng.springboot.faster.system.modular.AppModuleProperties;
import com.github.rexsheng.springboot.faster.system.user.application.dto.*;
import com.github.rexsheng.springboot.faster.system.user.domain.DeptDomainService;
import com.github.rexsheng.springboot.faster.system.user.domain.SysUser;
import com.github.rexsheng.springboot.faster.system.user.domain.SysUserToken;
import com.github.rexsheng.springboot.faster.system.user.domain.UserStateChangedEvent;
import com.github.rexsheng.springboot.faster.system.user.domain.gateway.ChangePwdDO;
import com.github.rexsheng.springboot.faster.system.user.domain.gateway.QueryUserDO;
import com.github.rexsheng.springboot.faster.system.user.domain.gateway.QueryUserTokenDO;
import com.github.rexsheng.springboot.faster.system.user.domain.gateway.UserGateway;

import com.github.rexsheng.springboot.faster.util.DateUtil;
import com.github.rexsheng.springboot.faster.util.PasswordUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Lazy
public class UserServiceImpl implements UserService{

    private static final Logger logger= LoggerFactory.getLogger(UserServiceImpl.class);

    private UserGateway userGateway;

    private PasswordEncoder passwordEncoder;

    private DeptDomainService deptDomainService;

    private AppModuleProperties appModuleProperties;

    public UserServiceImpl(UserGateway userGateway, PasswordEncoder passwordEncoder, DeptDomainService deptDomainService, AppModuleProperties appModuleProperties) {
        this.userGateway = userGateway;
        this.passwordEncoder = passwordEncoder;
        this.deptDomainService = deptDomainService;
        this.appModuleProperties = appModuleProperties;
    }

    @Override
    @Transactional
    public String add(AddUserRequest request) {
        SysUser user=request.toUser(passwordEncoder);
        userGateway.insertUser(user);
        SpringContext.getApplicationContext().publishEvent(new UserStateChangedEvent(user.getUserId(),UserStateChangedEvent.UserState.ADD));
        return request.getAutoGeneratedPassword();
    }

    @Override
    public void add(List request) {
        List userList=request.stream().map(a->a.toUser(passwordEncoder)).collect(Collectors.toList());
        userGateway.insertUsers(userList);
        SpringContext.getApplicationContext().publishEvent(new UserStateChangedEvent(UserStateChangedEvent.UserState.ADD));
    }

    @Override
    public PagedList pagedList(QueryUserRequest request) {
        QueryUserDO query=new QueryUserDO();
        query.setKeyword(request.getKeyword());
        query.setAccount(request.getLoginAccount());
        if(request.getDeptId()!=null){
            query.setDeptIds(deptDomainService.queryDeptAndChildrenIds(request.getDeptId()));
        }
        query.setPageIndex(request.getPageIndex());
        query.setPageSize(request.getPageSize());
        PagedList dataList=userGateway.queryUsers(query);
        return PagedList.of(dataList,UserDetailResponse::of);
    }

    @Override
    public List queryList(QueryUserRequest request) {
        QueryUserDO query=new QueryUserDO();
        query.setKeyword(request.getKeyword());
        query.setAccount(request.getLoginAccount());
        query.setMail(request.getMail());
        PagedList dataList=userGateway.queryUsers(query);
        return dataList.getDataList().stream().map(UserDetailResponse::ofAll).collect(Collectors.toList());
    }

    @Override
    public UserDetailResponse get(Long id) {
        SysUser user= userGateway.getUser(id);
        Assert.notNull(user,"用户不存在");
        return UserDetailResponse.of(user);
    }

    @Override
    public UserDetailResponse getAllInfo(Long id) {
        SysUser user= userGateway.getUser(id);
        Assert.notNull(user,"用户不存在");
        return UserDetailResponse.ofAll(user);
    }

    @Override
    @Transactional
    public void update(UpdateUserRequest request) {
        SysUser user=request.toUser();
        userGateway.updateUserById(user);
    }

    @Override
    public void updateStatus(List request) {
        List userList=request.stream().map(UpdateUserRequest::toUser).collect(Collectors.toList());
        userGateway.updateUserStatus(userList);
        SpringContext.getApplicationContext().publishEvent(new UserStateChangedEvent(UserStateChangedEvent.UserState.STATUS_CHANGED));
    }

    @Override
    public void updateLoginTime(UpdateUserRequest request) {
        SysUser user=request.toUser();
        userGateway.updateUserLoginTime(user);
    }

    @Override
    public void updateAvatar(UpdateAvatarRequest request) {
        SysUser user=request.toUser();
        userGateway.updateUserAvatar(user);
    }

    @Override
    public void updatePersonal(UpdatePersonalRequest request) {
        SysUser user=request.toUser();
        userGateway.updatePersonal(user);
    }

    @Override
    public void delete(List ids) {
        userGateway.deleteUsers(SysUser.of(ids,true));
        SpringContext.getApplicationContext().publishEvent(new UserStateChangedEvent(ids,UserStateChangedEvent.UserState.DELETE));
    }

    @Override
    public Boolean validAccount(ValidateAccountRequest request) {
        SysUser user=request.toUser();
        return !user.isAccountDuplicated();
    }

    @Override
    public String resetPassword(ResetPasswordRequest request) {
        String autoGeneratedPassword=null;
        String newPassword=request.getPassword();
        if(ObjectUtils.isEmpty(newPassword)){
            newPassword= PasswordUtils.createRandomPassword();
            autoGeneratedPassword=newPassword;
        }

        ChangePwdDO query=new ChangePwdDO();
        query.setUserIds(request.getUserIds());
        query.setPassword(passwordEncoder.encode(newPassword));
        query.setLastPasswordTime(null);
        query.setPasswordExpiredTime(DateUtil.currentDateTime());
        userGateway.changePassword(query);
        return autoGeneratedPassword;
    }

    @Override
    public void changePassword(ChangePasswordRequest request) {
        SysUser user= userGateway.getUser(request.getUserId());
        Assert.notNull(user,"用户不存在");
        user.validateCurrentPassword(passwordEncoder,request.getOldPassword());
        ChangePwdDO query=new ChangePwdDO();
        query.setUserIds(Arrays.asList(request.getUserId()));
        query.setPassword(passwordEncoder.encode(request.getNewPassword()));
        query.setLastPasswordTime(DateUtil.currentDateTime());
        query.setPasswordExpiredTime(DateUtil.currentDateTime().plusDays(appModuleProperties.getManagement().getPasswordExpirationDays()));
        userGateway.changePassword(query);
    }

    @Override
    public void changeRole(ChangeUserRoleRequest request) {
        SysUser user=request.toUser();
        userGateway.changeRole(user);
    }

    @Override
    public List getRoles(Long id) {
        SysUser user=SysUser.of(id);
        return userGateway.getRoles(user).stream().map(a->a.getRoleId()).collect(Collectors.toList());
    }

    @Override
    public ValidateImportUserResponse validateImportList(List request) {
        List duplicateAccounts=request.stream()
                .collect(Collectors.groupingBy(a->a.getLoginAccount()==null?"":a.getLoginAccount()))
                .entrySet().stream()
                .filter(a->a.getValue().size()>1)
                .map(a->a.getKey()).collect(Collectors.toList());
        List successList=new ArrayList<>();
        List errorList=new ArrayList<>();
        for(ValidateImportUserRequest item:request){
            if(duplicateAccounts.contains(item.getLoginAccount())){
                String errorMessage="账号已存在";
                ValidateImportUserResponse.ValidateErrorDetailResponse user=ValidateImportUserResponse.ValidateErrorDetailResponse.of(item,errorMessage);
                errorList.add(user);
                continue;
            }
            try {
                SysUser user=item.toUser();
                successList.add(ValidateImportUserResponse.ValidateSuccessDetailResponse.of(user));
            }
            catch (Exception e){
                String errorMessage=e.getMessage();
                ValidateImportUserResponse.ValidateErrorDetailResponse user=ValidateImportUserResponse.ValidateErrorDetailResponse.of(item,errorMessage);
                errorList.add(user);
            }
        }
        ValidateImportUserResponse response=new ValidateImportUserResponse();
        response.setSuccessList(successList);
        response.setErrorList(errorList);
        return response;
    }

    @Override
    public PagedList viewUserByCondition(ViewUserRequest request) {
        QueryUserDO query=new QueryUserDO();
        if(!ObjectUtils.isEmpty(request.getUserIds())){
            query.setUserIds(Arrays.asList(request.getUserIds()));
        }
        if(!ObjectUtils.isEmpty(request.getDeptIds())){
            query.setDeptIds(Arrays.asList(request.getDeptIds()));
        }
        if(!ObjectUtils.isEmpty(request.getPostIds())){
            query.setPostIds(Arrays.asList(request.getPostIds()));
        }
        if(!ObjectUtils.isEmpty(request.getRoleIds())){
            query.setRoleIds(Arrays.asList(request.getRoleIds()));
        }
        query.setFetchDeleted(request.getFetchDeleted());
        query.setPageIndex(request.getPageIndex());
        query.setPageSize(request.getPageSize());
        PagedList dataList=userGateway.queryUsers(query);
        return PagedList.of(dataList,UserDetailResponse::of);
    }

    @Override
    public CreateTokenResponse createToken(CreateUserTokenRequest request) {
        SysUser sysUser=request.toDomain();
        if(sysUser.isBusinessKeyDuplicated()){
            throw new RuntimeException("不允许重复创建");
        }
        sysUser.createToken();
        userGateway.saveUserToken(sysUser);
        return CreateTokenResponse.of(sysUser);
    }

    @Override
    public List queryTokens(QueryTokenRequest request) {
        QueryUserTokenDO query=new QueryUserTokenDO();
        query.setUserId(request.getUserId());
        query.setTokenName(request.getTokenName());
        query.setTokenValue(request.getTokenValue());
        SysUser sysUser=userGateway.queryUserTokens(query);
        if(sysUser!=null && sysUser.getTokenList()!=null){
            return sysUser.getTokenList().stream().map(this::map).collect(Collectors.toList());
        }
        return List.of();
    }

    private UserTokenDetailResponse map(SysUserToken token){
        UserTokenDetailResponse response=new UserTokenDetailResponse();
        response.setTokenId(token.getTokenId());
        response.setBusinessKey(token.getBusinessKey());
        response.setTokenName(token.getTokenName());
        response.setTokenValue(token.getTokenValue());
        response.setExpireTime(token.getExpireTime());
        response.setCreateTime(token.getCreateTime());
        response.setUserId(token.getUserId());
        return response;
    }

    @Override
    public void deleteTokens(List ids) {
        userGateway.deleteUserTokens(SysUser.deleteTokens(ids));
    }

    @Override
    public Boolean validToken(ValidateUserTokenRequest request) {
        SysUser user=request.toDomain();
        return !user.isBusinessKeyDuplicated();
    }

    @Override
    public void changeExpiredTime(ChangeExpiredTimeRequest request) {
        SysUser user=request.toUser();
        userGateway.updateUserExpiredTime(user);
    }

    @Override
    public void changeLockedTime(ChangeLockedTimeRequest request) {
        SysUser user=request.toUser();
        userGateway.updateUserLockedTime(user);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy