package com.ema.admin.modules.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ema.admin.modules.system.entity.SysRole;
import com.ema.admin.modules.system.entity.SysRoleMenu;
import com.ema.admin.modules.system.entity.SysUserRole;
import com.ema.admin.modules.system.mapper.SysRoleMapper;
import com.ema.admin.modules.system.service.*;
import com.ema.admin.modules.system.vo.SysRoleQueryVo;
import com.ema.common.exceptions.MyAssert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统角色信息表 服务层实现。
 *
 * @author LIJIAN
 * @since 2026-03-23
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>  implements SysRoleService{

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysRole> getRoleByUserId(String userId) {
        MyAssert.notNull(userId, "用户id不能为空");
        //获取用户角色
        List<SysUserRole> userRoles =  sysUserRoleService.queryChain().eq(SysUserRole::getUserId, userId).list();
        if (CollUtil.isNotEmpty(userRoles)) {
            List<String> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
            return this.listByIds(CollUtil.distinct(roleIds));
        }
        return CollUtil.newArrayList();
    }

    @Override
    public Page<SysRole> page(SysRoleQueryVo queryVo) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.like(SysRole::getRoleName, queryVo.getRoleName(), StrUtil.isNotBlank(queryVo.getRoleName()));
        queryWrapper.like(SysRole::getRoleKey, queryVo.getRoleKey(), StrUtil.isNotBlank(queryVo.getRoleKey()));
        queryWrapper.eq(SysRole::getStatus, queryVo.getStatus(), StrUtil.isNotBlank(queryVo.getStatus()));
        return this.page(new Page<>(queryVo.getPageNumber(), queryVo.getPageSize()), queryWrapper);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignMenusToRole(String roleId, List<String> menuIds) {
        // 先删除角色菜单关联
        sysRoleMenuService.remove(sysRoleMenuService.queryChain()
                .eq(SysRoleMenu::getRoleId, roleId));

        // 批量创建角色菜单关联
        List<SysRoleMenu> roleMenus = menuIds.stream()
                .map(menuId -> SysRoleMenu.builder()
                        .roleId(roleId)
                        .menuId(menuId)
                        .build())
                .collect(Collectors.toList());
        sysRoleMenuService.saveBatch(roleMenus);
    }

}
