package com.moral.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.ManageAccount;
import com.moral.api.entity.ManageAccountRole;
import com.moral.api.entity.ManageMenu;
import com.moral.api.entity.ManageRole;
import com.moral.api.mapper.ManageAccountMapper;
import com.moral.api.mapper.ManageAccountRoleMapper;
import com.moral.api.mapper.ManageMenuMapper;
import com.moral.api.mapper.ManageRoleMapper;
import com.moral.api.pojo.dto.account.*;
import com.moral.api.pojo.dto.login.AccountInfoDTO;
import com.moral.api.pojo.dto.login.LoginDTO;
import com.moral.api.pojo.form.account.AccountDeleteForm;
import com.moral.api.pojo.form.account.AccountInsertForm;
import com.moral.api.pojo.form.account.AccountQueryForm;
import com.moral.api.pojo.form.account.AccountUpdateForm;
import com.moral.api.pojo.form.login.LoginForm;
import com.moral.api.pojo.form.login.LogoutForm;
import com.moral.api.service.ManageAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.config.mybatis.wrapper.NullFilterWrapper;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.util.*;
/**
*
* 后台账户表 服务实现类
*
*
* @author moral
* @since 2021-03-09
*/
@Service
public class ManageAccountServiceImpl extends ServiceImpl implements ManageAccountService {
@Autowired
ManageAccountMapper manageAccountMapper;
@Autowired
ManageRoleMapper manageRoleMapper;
@Autowired
ManageMenuMapper manageMenuMapper;
@Autowired
ManageAccountRoleMapper manageAccountRoleMapper;
/**
* @Description: 登陆接口
* @Param: [loginForm]
* @return: com.moral.api.pojo.dto.login.LoginDTO
* @Author: 陈凯裕
* @Date: 2021/3/30
*/
@Override
public LoginDTO login(LoginForm loginForm) {
LoginDTO loginDTO = new LoginDTO();
//取参
String account = loginForm.getAccount();
String AESPassword = loginForm.getPassword();
//解密
String password = AESUtils.decrypt(AESPassword);
//查询是否存在
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("account", account);
List manageAccounts = manageAccountMapper.selectList(wrapper);
if (ObjectUtils.isEmpty(manageAccounts)) {
loginDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode());
loginDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
return loginDTO;
}
//查询是否逻辑删除
ManageAccount manageAccount = null;
for (ManageAccount value : manageAccounts) {
if (Constants.NOT_DELETE.equals(value.getIsDelete()))
manageAccount = value;
}
if (ObjectUtils.isEmpty(manageAccount)) {
loginDTO.setCode(ResponseCodeEnum.ACCOUNT_IS_DELETE.getCode());
loginDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
return loginDTO;
}
//校验密码
if (!MD5Utils.saltMD5Verify(password, manageAccount.getPassword())) {
loginDTO.setCode(ResponseCodeEnum.PASSWORD_ERROR.getCode());
loginDTO.setMsg(ResponseCodeEnum.PASSWORD_ERROR.getMsg());
return loginDTO;
}
//查询角色
List roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
//查询菜单
List menus = null;
if (!ObjectUtils.isEmpty(roles)) {
menus = manageMenuMapper.getParentChildrenMenusByRoles(roles);
}
//封装用户信息
AccountInfoDTO accountInfoDTO = new AccountInfoDTO();
accountInfoDTO.setAccount(manageAccount);
accountInfoDTO.setMenus(menus);
accountInfoDTO.setRoles(roles);
//获取token 并且存入缓存
String token = TokenUtils.getToken(String.valueOf(manageAccount.getId()), accountInfoDTO);
//封装返回结果
loginDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
loginDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
loginDTO.setAccountInfoDTO(accountInfoDTO);
loginDTO.setToken(token);
return loginDTO;
}
/**
* @Description: 注销
* @Param: [parameters]
* @return:
* @Author: 陈凯裕
* @Date: 2021/3/11
*/
@Override
public boolean logout(LogoutForm logoutForm) {
String accountId = logoutForm.getAccountId();
String token = logoutForm.getToken();
TokenUtils.destoryToken(accountId, token);
return true;
}
/**
* @Description: 添加后台账号
* @Param: [accountAddRequest]
* @return: com.moral.api.pojo.dto.AccountDTO
* @Author: 陈凯裕
* @Date: 2021/3/13
*/
@Override
@Transactional
public AccountDTO insertAccount(AccountInsertForm accountInsertForm) {
AccountDTO accountDTO = new AccountDTO();
//取参
ManageAccount manageAccount = accountInsertForm.formConvertEntity();
List roleIdsStr = accountInsertForm.getRoleIds();
/*判断账号是否存在*/
ManageAccount existAccount = new ManageAccount();
existAccount.setAccount(manageAccount.getAccount());
existAccount.setIsDelete(Constants.NOT_DELETE);
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.setEntity(existAccount);
ManageAccount exitAccountResult = manageAccountMapper.selectOne(wrapper);
if (!ObjectUtils.isEmpty(exitAccountResult)) {
accountDTO.setCode(ResponseCodeEnum.ACCOUNT_EXIST.getCode());
accountDTO.setMsg(ResponseCodeEnum.ACCOUNT_EXIST.getMsg());
return accountDTO;
}
//插入
manageAccountMapper.insert(manageAccount);
//封装account_role
/*String to Integer*/
List roles = null;
//如果新建账号没有分配角色则不进行操作
if (!ObjectUtils.isEmpty(roleIdsStr)) {
List roleIds = new ArrayList<>();
roleIdsStr.forEach(str -> roleIds.add(Integer.parseInt(str)));
Integer accountId = manageAccount.getId();
roleIdsStr.forEach(
value -> {
ManageAccountRole manageAccountRole = new ManageAccountRole();
manageAccountRole.setAccountId(accountId);
manageAccountRole.setRoleId(Integer.parseInt(value));
manageAccountRoleMapper.insert(manageAccountRole);
}
);
roles = manageRoleMapper.selectBatchIds(roleIds);
}
//封装返回结果
accountDTO.setAccount(manageAccount);
accountDTO.setRoles(roles);
accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
return accountDTO;
}
/**
* @Description: 查询后台账号
* @Param: [accountQueryRequest]
* @return: com.moral.api.pojo.dto.AccountDTO
* @Author: 陈凯裕
* @Date: 2021/3/15
*/
@Override
public AccountQueryDTO queryAccount(AccountQueryForm accountQueryForm) {
AccountQueryDTO accountQueryDTO = new AccountQueryDTO();
//取参
Integer pageCount = accountQueryForm.getPage();
Integer size = accountQueryForm.getSize();
String userName = accountQueryForm.getUserName();
String email = accountQueryForm.getEmail();
String mobile = accountQueryForm.getMobile();
String wechat = accountQueryForm.getWechat();
String isDelete = accountQueryForm.getIsDelete();
String order = accountQueryForm.getOrder();
String orderType = accountQueryForm.getOrderType();
Date createStartTime = accountQueryForm.getCreateStartTime();
Date createEndTime = DateUtils.getDateOfDay(accountQueryForm.getCreateEndTime(), 1);
//组装查询条件
Page page = new Page<>(pageCount, size);
NullFilterWrapper wrapper = new NullFilterWrapper<>();
wrapper.like("user_name", userName);
wrapper.like("email", email);
wrapper.like("mobile", mobile);
wrapper.like("wechat", wechat);
wrapper.between("create_time", createStartTime, createEndTime);
if (!ObjectUtils.isEmpty(order) && !ObjectUtils.isEmpty(orderType)) { //排序顺序条件构造
if (orderType.equals(Constants.ORDER_ASC))
wrapper.orderByAsc(ConvertUtils.toLine(order));
else
wrapper.orderByDesc(ConvertUtils.toLine(order));
}
if (!ObjectUtils.isEmpty(isDelete))//逻辑删除条件构造
wrapper.eq("is_delete", isDelete);
else
wrapper.eq("is_delete", Constants.NOT_DELETE);
//查询结果
Page resultPage = manageAccountMapper.selectPage(page, wrapper);
//查询用户对应的角色
List accounts = resultPage.getRecords();
List accountDTOS = new ArrayList<>();
if (!ObjectUtils.isEmpty(accounts)) {
for (ManageAccount manageAccount : accounts) {
AccountDTO accountDTO = new AccountDTO();
List roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
accountDTO.setRoles(roles);
accountDTO.setAccount(manageAccount);
accountDTOS.add(accountDTO);
}
}
//封装返回结果
accountQueryDTO.setAccountDTOS(accountDTOS);
accountQueryDTO.setCurrent(resultPage.getCurrent());
accountQueryDTO.setPages(resultPage.getPages());
accountQueryDTO.setSize(resultPage.getSize());
accountQueryDTO.setTotal(resultPage.getTotal());
accountQueryDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
accountQueryDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
return accountQueryDTO;
}
/**
* @Description: 更新后台账号
* @Param: [accountUpdateRequest]
* @return: com.moral.api.pojo.dto.account.AccountUpdateDTO
* @Author: 陈凯裕
* @Date: 2021/3/16
*/
@Override
@Transactional
public AccountDTO updateAccount(AccountUpdateForm accountUpdateForm) {
AccountDTO accountDTO = new AccountDTO();
//取参
ManageAccount manageAccount = accountUpdateForm.formConvertEntity();
List roleIds = accountUpdateForm.getRoleIds();
//判断要更新的用户是否存在
QueryWrapper exitWrapper = new QueryWrapper<>();
ManageAccount exitManageAccount = new ManageAccount();
exitManageAccount.setId(manageAccount.getId());
exitManageAccount.setIsDelete(Constants.NOT_DELETE);
exitWrapper.setEntity(exitManageAccount);
List manageAccounts = manageAccountMapper.selectList(exitWrapper);
if (ObjectUtils.isEmpty(manageAccounts)) {
accountDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode());
accountDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
return accountDTO;
}
//更新ManageAccount表
manageAccountMapper.updateById(manageAccount);
//如果角色有变动,则更新ManageAccountRole表
/*
* 如果roleIds为null,则是该账号角色没有发生改变
* 如果roleIds为空,则是该账号所有的角色都被移除
* 如果roleIds有元素,则是该账号的角色有更新
* */
List roles = null;
if (roleIds != null) {
UpdateWrapper deleteWrapper = new UpdateWrapper<>();
deleteWrapper.eq("account_id", manageAccount.getId()).set("is_delete", Constants.DELETE);
manageAccountRoleMapper.update(null, deleteWrapper);
/*重新添加角色*/
for (Integer roleId : roleIds) {
ManageAccountRole manageAccountRole = new ManageAccountRole();
manageAccountRole.setAccountId(manageAccount.getId());
manageAccountRole.setRoleId(roleId);
manageAccountRoleMapper.insert(manageAccountRole);
}
//获取用户所有角色
if (roleIds.size() != 0)
roles = manageRoleMapper.selectBatchIds(roleIds);
} else {
roles = manageRoleMapper.getManageRoleByAccountId(manageAccount.getId());
}
accountDTO.setRoles(roles);
//封装返回结果
accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
accountDTO.setAccount(manageAccountMapper.selectById(manageAccount.getId()));
return accountDTO;
}
/**
* @Description: 删除后台账号
* @Param: [accountDeleteRequest]
* @return: com.moral.api.pojo.dto.account.AccountDeleteDTO
* @Author: 陈凯裕
* @Date: 2021/3/16
*/
@Override
@Transactional
public AccountDTO deleteAccount(AccountDeleteForm accountDeleteForm) {
AccountDTO accountDTO = new AccountDTO();
//取参
Integer accountId = accountDeleteForm.getAccountId();
//查询是否存在
ManageAccount manageAccount = new ManageAccount();
manageAccount.setIsDelete(Constants.NOT_DELETE);
manageAccount.setId(accountId);
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.setEntity(manageAccount);
ManageAccount existManageAccount = manageAccountMapper.selectOne(queryWrapper);
if (ObjectUtils.isEmpty(existManageAccount)) {
accountDTO.setCode(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode());
accountDTO.setMsg(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
return accountDTO;
}
//逻辑删除账号
UpdateWrapper deleteAccountWrapper = new UpdateWrapper<>();
deleteAccountWrapper.eq("id", accountId);
deleteAccountWrapper.set("is_delete", Constants.DELETE);
manageAccountMapper.update(null, deleteAccountWrapper);
//逻辑删除账号相关角色
UpdateWrapper deleteManageAccountRoleWrapper = new UpdateWrapper<>();
deleteManageAccountRoleWrapper.set("is_delete", Constants.DELETE).eq("account_id", manageAccount.getId());
manageAccountRoleMapper.update(null, deleteManageAccountRoleWrapper);
//封装返回结果
accountDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
accountDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
accountDTO.setAccount(existManageAccount);
return accountDTO;
}
}