package cn.stylefeng.guns.sys.modular.rest.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.oauth2.entity.OauthUserInfo;
import cn.stylefeng.guns.base.oauth2.service.OauthUserInfoService;
import cn.stylefeng.guns.base.pojo.node.MenuNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.util.SaltUtil;
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser;
import cn.stylefeng.guns.sys.modular.rest.entity.RestUserPos;
import cn.stylefeng.guns.sys.modular.rest.factory.RestUserFactory;
import cn.stylefeng.guns.sys.modular.rest.mapper.RestUserMapper;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.roses.core.datascope.DataScope;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 管理员表 服务实现类
*
*
* @author stylefeng
* @since 2018-12-07
*/
@Service
public class RestUserService extends ServiceImpl {
@Resource
private RestMenuService restMenuService;
@Resource
private RestUserPosService restUserPosService;
/**
* 添加用戶
*
* @author fengshuonan
* @Date 2018/12/24 22:51
*/
@Transactional(rollbackFor = Exception.class)
public void addUser(UserDto user) {
// 判断账号是否重复
RestUser theUser = this.getByAccount(user.getAccount());
if (theUser != null) {
throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
}
// 完善账号信息
String salt = SaltUtil.getRandomSalt();
String password = SaltUtil.md5Encrypt(user.getPassword(), salt);
RestUser newUser = RestUserFactory.createRestUser(user, password, salt);
this.save(newUser);
//添加职位关联
addPosition(user.getPosition(), newUser.getUserId());
}
/**
* 修改用户
*
* @author fengshuonan
* @Date 2018/12/24 22:53
*/
@Transactional(rollbackFor = Exception.class)
public void editUser(UserDto user) {
RestUser oldUser = this.getById(user.getUserId());
if (oldUser == null) {
throw new ServiceException(BizExceptionEnum.NO_THIS_USER);
}
if (LoginContextHolder.getContext().hasRole(Const.ADMIN_NAME)) {
this.updateById(RestUserFactory.editRestUser(user, oldUser));
} else {
this.assertAuth(user.getUserId());
LoginUser shiroUser = LoginContextHolder.getContext().getUser();
if (shiroUser.getId().equals(user.getUserId())) {
this.updateById(RestUserFactory.editRestUser(user, oldUser));
} else {
throw new ServiceException(BizExceptionEnum.NO_PERMITION);
}
}
//删除职位关联
restUserPosService.remove(new QueryWrapper().eq("user_id", user.getUserId()));
//添加职位关联
addPosition(user.getPosition(), user.getUserId());
}
/**
* 删除用户
*
* @author fengshuonan
* @Date 2018/12/24 22:54
*/
@Transactional(rollbackFor = Exception.class)
public void deleteUser(Long userId) {
//不能删除超级管理员
if (userId.equals(Const.ADMIN_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_DELETE_ADMIN);
}
//不是超级管理员校验权限
if (!LoginContextHolder.getContext().isAdmin()) {
this.assertAuth(userId);
}
this.setStatus(userId, ManagerStatus.DELETED.getCode());
//删除对应的oauth2绑定表
OauthUserInfoService oauthUserInfoService = null;
try {
oauthUserInfoService = SpringContextHolder.getBean(OauthUserInfoService.class);
oauthUserInfoService.remove(new QueryWrapper().eq("user_id", userId));
} catch (Exception e) {
//没有集成oauth2模块,不操作
}
//删除职位关联
restUserPosService.remove(new QueryWrapper().eq("user_id", userId));
}
/**
* 修改用户状态
*
* @author fengshuonan
* @Date 2018/12/24 22:45
*/
public int setStatus(Long userId, String status) {
return this.baseMapper.setStatus(userId, status);
}
/**
* 修改密码
*
* @author fengshuonan
* @Date 2018/12/24 22:45
*/
public void changePwd(String oldPassword, String newPassword) {
Long userId = LoginContextHolder.getContext().getUser().getId();
RestUser user = this.getById(userId);
String oldMd5 = SaltUtil.md5Encrypt(oldPassword, user.getSalt());
if (user.getPassword().equals(oldMd5)) {
String newMd5 = SaltUtil.md5Encrypt(newPassword, user.getSalt());
user.setPassword(newMd5);
this.updateById(user);
} else {
throw new ServiceException(BizExceptionEnum.OLD_PWD_NOT_RIGHT);
}
}
/**
* 根据条件查询用户列表
*
* @author fengshuonan
* @Date 2018/12/24 22:45
*/
public Page