package com.kexue.skills.service.impl;

import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CachePenetrationProtect;
import com.alicp.jetcache.anno.Cached;
import com.kexue.skills.entity.SysRole;
import com.kexue.skills.entity.dto.SysRoleDto;
import com.kexue.skills.mapper.SysRoleMapper;
import com.kexue.skills.service.SysRoleService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

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

/**
 * (SysRole)表服务实现类
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

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

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    @Cached(name = "sysRole:", key = "#roleId", expire = 3600, cacheType = com.alicp.jetcache.anno.CacheType.BOTH)
    @CachePenetrationProtect
    public SysRole queryById(Long roleId) {
        return sysRoleMapper.queryById(roleId);
    }

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

    /**
     * 新增数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysRole insert(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
        return sysRole;
    }

    /**
     * 修改数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Override
    @CacheInvalidate(name = "sysRole:", key = "#sysRole.roleId")
    public SysRole update(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
        return queryById(sysRole.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    @CacheInvalidate(name = "sysRole:", key = "#roleId")
    public boolean deleteById(Long roleId) {
        return sysRoleMapper.deleteById(roleId) > 0;
    }
}
