package com.kexue.skills.service.impl;

import com.kexue.skills.entity.SysUserRole;
import com.kexue.skills.entity.dto.SysUserRoleDto;
import com.kexue.skills.mapper.SysUserRoleMapper;
import com.kexue.skills.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

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

/**
 * (SysUserRole)表服务实现类
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 分页查询数据
     *
     * @param queryDto
     * @return 数据分页列表
     */
    @Override
    public PageInfo<SysUserRole> getPageList(SysUserRoleDto queryDto) {
        int pageNum = queryDto.getPageNum()==null?PAGENUM:queryDto.getPageNum();
        int pageSize = queryDto.getPageSize()==null? PAGESIZE :queryDto.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<SysUserRole> dataList = sysUserRoleMapper.getPageList(queryDto);
        return new PageInfo<>(dataList);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public SysUserRole queryById(Long roleId) {
        return sysUserRoleMapper.queryById(roleId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<SysUserRole> queryAllByLimit(int offset, int limit) {
        return sysUserRoleMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole insert(SysUserRole sysUserRole) {
        sysUserRoleMapper.insert(sysUserRole);
        return sysUserRole;
    }

    /**
     * 修改数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole update(SysUserRole sysUserRole) {
        sysUserRoleMapper.update(sysUserRole);
        return queryById(sysUserRole.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long roleId) {
        return sysUserRoleMapper.deleteById(roleId) > 0;
    }
}
