All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.api.ApiService Maven / Gradle / Ivy

The newest version!
package shz.api;

import org.springframework.boot.CommandLineRunner;
import shz.api.entity.SysApi;
import shz.api.vo.QueryApiVo;
import shz.api.vo.UpdateApiVo;
import shz.core.FieldSetter;
import shz.core.model.PageInfo;
import shz.core.msg.ClientFailureMsg;
import shz.core.msg.ServerFailureMsg;
import shz.core.structure.config.BigMapConfig;
import shz.core.structure.limiter.RateLimiter;
import shz.jdbc.JdbcService;
import shz.orm.Tnp;
import shz.spring.api.ApiInfo;
import shz.spring.model.PageVo;

import java.util.List;
import java.util.Set;

public class ApiService implements CommandLineRunner {
    private final ApiInfo apiDetail = ApiInfo.of();

    protected final JdbcService jdbcService;

    public ApiService(JdbcService jdbcService) {
        this.jdbcService = jdbcService;
    }

    @Override
    public void run(String... args) {
        jdbcService.query((Tnp) null, SysApi.class, null, null, entity -> {
            ApiDetail detail = new ApiDetail();
            setDetail(detail, entity);
            apiDetail.put(entity.getMethod(), entity.getPath(), detail);
            return false;
        }, null, 0, null, null);
    }

    private void setDetail(ApiDetail detail, SysApi sysApi) {
        detail.setId(sysApi.getId());
        detail.setLogin(sysApi.getLogin());
        detail.setEnabled(sysApi.getEnabled());

        if (sysApi.getRateLimit() != null && sysApi.getRateLimit() > 0) {
            RateLimiter rateLimiter = detail.getRateLimiter();
            if (rateLimiter == null) {
                rateLimiter = new RateLimiter();
                detail.setRateLimiter(rateLimiter);
            }
            rateLimiter.setPermits(sysApi.getRateLimit());
        } else detail.setRateLimiter(null);

        if (sysApi.getIntervalLimit() != null && sysApi.getIntervalLimit() > 0)
            detail.setIntervalLimit(sysApi.getIntervalLimit());
        else detail.setIntervalLimit(0);
    }

    public final BigMapConfig permissions(Set apiIds) {
        BigMapConfig config = new BigMapConfig();
        apiDetail.filter(d -> {
            if (apiIds.contains(d.getId())) config.add(d.getId());
            return false;
        });
        return config;
    }

    public final ApiDetail getDetail(String method, String path) {
        return apiDetail.get(method, path);
    }

    public final List list(QueryApiVo vo) {
        return jdbcService.selectList(SysApi.class, jdbcService.whereSql(jdbcService.nonNullClassInfo(SysApi.class), vo, Boolean.FALSE, true));
    }

    public final PageInfo page(PageVo pageVo) {
        return jdbcService.page(pageVo.map(), SysApi.class, jdbcService.whereSql(jdbcService.nonNullClassInfo(SysApi.class), pageVo.getData(), Boolean.FALSE, true));
    }

    public final void update(UpdateApiVo vo) {
        SysApi oldEntity = jdbcService.selectById(SysApi.class, vo.getId());
        ClientFailureMsg.requireNonNull(oldEntity, "接口不存在");

        ApiDetail detail = getDetail(oldEntity.getMethod(), oldEntity.getPath());
        ClientFailureMsg.requireNonNull(detail, "当前应用不存在该接口");

        int row = jdbcService.updateById(FieldSetter.copy(vo, new SysApi()));
        ServerFailureMsg.requireNon(jdbcService.fail(row), "更新接口失败");

        setDetail(detail, FieldSetter.copy(vo, oldEntity));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy