package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.entity.Permission;
import com.zhentao.entity.Role;
import com.zhentao.entity.RolePermission;
import com.zhentao.mapper.PermissionMapper;
import com.zhentao.mapper.RoleMapper;
import com.zhentao.mapper.RolePermissionMapper;
import com.zhentao.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 角色服务实现
 */
@Service
public class RoleServiceImpl implements RoleService {
    
    @Autowired
    private RoleMapper roleMapper;
    
    @Autowired
    private PermissionMapper permissionMapper;
    
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    
    @Override
    @Transactional
    public Role createRole(Role role) {
        // 检查角色编码是否已存在
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("role_code", role.getRoleCode());
        Role existRole = roleMapper.selectOne(wrapper);
        if (existRole != null) {
            throw new RuntimeException("角色编码已存在");
        }
        
        role.setStatus(1); // 默认启用
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        
        roleMapper.insert(role);
        return role;
    }
    
    @Override
    public Page<Role> getRoleList(Integer pageNum, Integer pageSize, String keyword) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        
        if (StringUtils.hasText(keyword)) {
            wrapper.and(w -> w.like("role_name", keyword)
                    .or().like("role_code", keyword));
        }
        
        wrapper.orderByDesc("create_time");
        
        return roleMapper.selectPage(page, wrapper);
    }
    
    @Override
    public Role getRoleById(Integer roleId) {
        return roleMapper.selectById(roleId);
    }
    
    @Override
    public boolean updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        return roleMapper.updateById(role) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteRole(Integer roleId) {
        // 删除角色权限关联
        rolePermissionMapper.deleteByRoleId(roleId);
        // 删除角色
        return roleMapper.deleteById(roleId) > 0;
    }
    
    @Override
    @Transactional
    public boolean assignPermissions(Integer roleId, List<Integer> permissionIds) {
        // 删除原有权限
        rolePermissionMapper.deleteByRoleId(roleId);
        
        // 分配新权限
        if (permissionIds != null && !permissionIds.isEmpty()) {
            LocalDateTime now = LocalDateTime.now();
            for (Integer permissionId : permissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                rolePermission.setCreateTime(now);
                rolePermissionMapper.insert(rolePermission);
            }
        }
        
        return true;
    }
    
    @Override
    public List<Permission> getRolePermissions(Integer roleId) {
        return permissionMapper.selectPermissionsByRoleId(roleId);
    }
}

