package com.securitycontrol.system.base.service.impl;

import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.entity.system.dto.RoleDto;
import com.securitycontrol.entity.system.vo.RoleVo;
import com.securitycontrol.system.base.mapper.IRoleMapper;
import com.securitycontrol.system.base.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author：cwchen
 * @date：2024-02-23-18:33
 * @version：1.0
 * @description：角色管理-业务逻辑层
 */
@Service(value = "IRoleService")
@Slf4j
public class RoleServiceImpl implements IRoleService {

    @Resource(name = "IRoleMapper")
    private IRoleMapper mapper;

    @Override
    public List<RoleVo> getRoleLists(RoleDto dto) {
        List<RoleVo> list = new ArrayList<>();
        try {
            list = mapper.getRoleLists(dto);
        } catch (Exception e) {
            log.error("获取角色列表",e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateRole(RoleVo vo) {
        try {
            int result = mapper.isRoleName(vo.getRoleName());
            if (vo.getRoleId() == null) {
                vo.setType(1);
                vo.setCreateTime(DateTimeHelper.getNowTime());
                if (result > 0) {
                    return AjaxResult.error("角色已存在");
                }
            } else {
                vo.setType(2);
                if (vo.isFlag()) {
                    if (result > 0) {
                        return AjaxResult.error("角色已存在");
                    }
                }
            }
            mapper.addOrUpdateRole(vo);
        } catch (Exception e) {
            log.error("角色", e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getRoleById(RoleDto dto) {
        RoleVo vo = new RoleVo();
        try {
            vo = mapper.getRoleById(dto);
        } catch (Exception e) {
            log.error("角色详情",e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delRole(RoleDto dto) {
        int result = mapper.isUserUse(dto);
        if (result > 0) {
            return AjaxResult.error("已有用户绑定该角色，请先删除用户");
        }
        mapper.deleteRole(dto);
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult authMenu(RoleVo vo) {
        if (vo.getRoleId() == null || vo.getMenuIds() == null) {
            return AjaxResult.error("参数不完整");
        }
        mapper.delRoleMenu(vo);
        mapper.addRoleMenu(vo);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getAuthMenu(RoleDto dto) {
        List<String> list = new ArrayList<>();
        try {
            list = mapper.getAuthMenu(dto);
        } catch (Exception e) {
            log.error("获取角色授权的菜单",e);
        }
        return AjaxResult.success(list);
    }
}
