package com.njzscloud.supervisory.sys.endpoint.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
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.supervisory.sys.endpoint.mapper.EndpointMapper;
import com.njzscloud.supervisory.sys.endpoint.pojo.entity.EndpointEntity;
import com.njzscloud.supervisory.sys.endpoint.pojo.param.EndpointSearchParam;
import com.njzscloud.supervisory.sys.endpoint.pojo.result.EndpointDetailResult;
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 EndpointService extends ServiceImpl<EndpointMapper, EndpointEntity> implements IService<EndpointEntity> {

    /**
     * 新增
     */
    public void add(EndpointEntity endpointEntity) {
        this.save(endpointEntity);
    }

    /**
     * 修改
     */
    public void modify(EndpointEntity endpointEntity) {
        this.updateById(endpointEntity);
    }

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

    /**
     * 详情
     */
    public EndpointEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 查询所有
     */
    public List<EndpointDetailResult> listAll(EndpointSearchParam endpointSearchParam) {
        return this.list(Wrappers.<EndpointEntity>lambdaQuery()
                        .like(StrUtil.isNotBlank(endpointSearchParam.getEndpointPath()), EndpointEntity::getEndpointPath, endpointSearchParam.getEndpointPath())
                        .orderByAsc(EndpointEntity::getRequestMethod, EndpointEntity::getEndpointPath, EndpointEntity::getCreateTime)
                )
                .stream()
                .map(it -> BeanUtil.copyProperties(it, EndpointDetailResult.class))
                .collect(Collectors.toList());
    }
}
