package com.njzscloud.supervisory.resource.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.support.EndpointResource;
import com.njzscloud.common.security.support.MenuResource;
import com.njzscloud.common.security.support.Resource;
import com.njzscloud.supervisory.resource.mapper.SysResourceMapper;
import com.njzscloud.supervisory.resource.pojo.SysResourceEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统资源表
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysResourceService extends ServiceImpl<SysResourceMapper, SysResourceEntity> implements IService<SysResourceEntity> {

    /**
     * 新增
     *
     * @param sysResourceEntity 数据
     */

    public void add(SysResourceEntity sysResourceEntity) {
        this.save(sysResourceEntity);
    }

    /**
     * 修改
     *
     * @param sysResourceEntity 数据
     */

    public void modify(SysResourceEntity sysResourceEntity) {
        this.updateById(sysResourceEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */

    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysResourceEntity 结果
     */

    public SysResourceEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysResourceEntity 筛选条件
     * @param pageParam         分页参数
     * @return PageResult&lt;SysResourceEntity&gt; 分页结果
     */

    public PageResult<SysResourceEntity> paging(PageParam pageParam, SysResourceEntity sysResourceEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SysResourceEntity>lambdaQuery(sysResourceEntity)));
    }

    public Resource selectResourceByUserId(Long userId) {
        List<MenuResource> menuResources = baseMapper.listUserMenu(userId);
        List<EndpointResource> endpointResource = baseMapper.listUserEndpoint(userId);
        return new Resource()
                .setMenu(menuResources)
                .setEndpoint(endpointResource);
    }
}
