package com.gxwebsoft.common.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.system.entity.Role;
import com.gxwebsoft.common.system.mapper.UserRoleMapper;
import com.gxwebsoft.common.system.service.UserRoleService;
import com.gxwebsoft.common.system.entity.UserRole;
import com.gxwebsoft.common.system.param.UserRoleParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户角色Service实现
 *
 * @author 科技小王子
 * @since 2025-06-16 20:39:53
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

  @Override
  public int saveBatch(Integer userId, List<Integer> roleIds) {
    return baseMapper.insertBatch(userId, roleIds);
  }

  @Override
  public List<Role> listByUserId(Integer userId) {
    return baseMapper.selectByUserId(userId);
  }

  @Override
  public List<Role> listByUserIds(List<Integer> userIds) {
    return baseMapper.selectByUserIds(userIds);
  }

  @Override
  public List<UserRole> listByRoleId(Integer roleId) {
    return list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, roleId));
  }

  @Override
  public PageResult<UserRole> pageRel(UserRoleParam param) {
    PageParam<UserRole, UserRoleParam> page = new PageParam<>(param);
    page.setDefaultOrder("create_time desc");
    List<UserRole> list = baseMapper.selectPageRel(page, param);
    return new PageResult<>(list, page.getTotal());
  }

  @Override
  public List<UserRole> listRel(UserRoleParam param) {
    List<UserRole> list = baseMapper.selectListRel(param);
    // 排序
    PageParam<UserRole, UserRoleParam> page = new PageParam<>();
    page.setDefaultOrder("create_time desc");
    return page.sortRecords(list);
  }

  @Override
  public UserRole getByIdRel(Integer id) {
    UserRoleParam param = new UserRoleParam();
    param.setId(id);
    return param.getOne(baseMapper.selectListRel(param));
  }

}
