package com.moral.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.moral.api.entity.ManageAccount;
import com.moral.api.entity.ManageAccountRole;
import com.moral.api.entity.ManageRole;
import com.moral.api.mapper.ManageAccountMapper;
import com.moral.api.mapper.ManageAccountRoleMapper;
import com.moral.api.mapper.ManageRoleMapper;
import com.moral.api.pojo.dto.accountRole.AccountRoleDTO;
import com.moral.api.pojo.form.accountRole.AccountRoleUpdateForm;
import com.moral.api.service.ManageAccountRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.ManageAccountService;
import com.moral.api.service.ManageRoleService;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
/**
*
* 后台账户角色关系表 服务实现类
*
*
* @author moral
* @since 2021-03-09
*/
@Service
public class ManageAccountRoleServiceImpl extends ServiceImpl implements ManageAccountRoleService {
@Autowired
ManageAccountRoleMapper manageAccountRoleMapper;
@Autowired
ManageRoleMapper manageRoleMapper;
@Autowired
ManageRoleService manageRoleService;
@Autowired
ManageAccountMapper manageAccountMapper;
@Override
@Transactional
public AccountRoleDTO updateAccountRole(AccountRoleUpdateForm form) {
//创建返回对象
AccountRoleDTO dto = new AccountRoleDTO();
//取参
Integer accountId = form.getAccountId();
Integer roleId = form.getRoleId();
//查询要更新的账号用于日志插入
ManageAccount account = manageAccountMapper.selectById(accountId);
//查询要更新的角色用于日志插入
ManageRole updateRole = null;
if (!ObjectUtils.isEmpty(roleId)) {
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("is_delete", Constants.NOT_DELETE);
queryWrapper.eq("id", roleId);
updateRole = manageRoleMapper.selectOne(queryWrapper);
}
//查询原有角色用于插入日志
ManageRole oldRole = manageRoleService.getRoleByAccountId(accountId);
//先删除原有角色再进行分配
UpdateWrapper updateWrapper = new UpdateWrapper();
updateWrapper.set("is_delete", Constants.DELETE);
updateWrapper.eq("account_id", accountId);
manageAccountRoleMapper.update(null, updateWrapper);
if (!ObjectUtils.isEmpty(roleId)) {
ManageAccountRole role = new ManageAccountRole();
role.setAccountId(accountId);
role.setRoleId(roleId);
manageAccountRoleMapper.insert(role);
}
//插入日志
insertUpdateLog(oldRole,updateRole,account);
//封装返回参数
dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
return dto;
}
private void insertUpdateLog(ManageRole oldRole, ManageRole updateRole, ManageAccount account) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
StringBuilder content = new StringBuilder();
content.append("修改了用户:").append(account.getUserName() + ";")
.append("账号:" + account.getAccount() + ";")
.append("角色:");
if(ObjectUtils.isEmpty(oldRole))
content.append("空->");
else
content.append(oldRole.getName()+"->");
if(ObjectUtils.isEmpty(updateRole))
content.append("空;");
else
content.append(updateRole.getName()+";");
LogUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
}
}