package com.njzscloud.supervisory.endpoint.service;

import cn.hutool.core.bean.BeanUtil;
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.supervisory.endpoint.mapper.SysEndpointMapper;
import com.njzscloud.supervisory.endpoint.pojo.SysEndpointEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 端点信息表
 */
@Slf4j
@Service
public class SysEndpointService extends ServiceImpl<SysEndpointMapper, SysEndpointEntity> implements IService<SysEndpointEntity> {

    /**
     * 新增
     *
     * @param sysEndpointEntity 数据
     */
    public void add(SysEndpointEntity sysEndpointEntity) {
        this.save(sysEndpointEntity);
    }

    /**
     * 修改
     *
     * @param sysEndpointEntity 数据
     */
    public void modify(SysEndpointEntity sysEndpointEntity) {
        this.updateById(sysEndpointEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysEndpointEntity 结果
     */
    public SysEndpointEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysEndpointEntity 筛选条件
     * @param pageParam         分页参数
     * @return PageResult&lt;SysEndpointEntity&gt; 分页结果
     */
    public PageResult<SysEndpointEntity> paging(PageParam pageParam, SysEndpointEntity sysEndpointEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SysEndpointEntity>query(sysEndpointEntity)));
    }

    public List<EndpointResource> listUserEndpoint(Long userId) {
        List<SysEndpointEntity> sysMenuEntities = baseMapper.listUserEndpoint(userId);
        return sysMenuEntities.stream()
                .map(it -> BeanUtil.copyProperties(it, EndpointResource.class)
                        .setAccessModel(it.getAccessModel().getVal())
                        .setRequestMethod(it.getRequestMethod().getVal())

                )
                .collect(Collectors.toList());
    }
}
