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

shz.api.ApiGather Maven / Gradle / Ivy

The newest version!
package shz.api;

import com.alibaba.fastjson2.JSON;
import org.springframework.boot.CommandLineRunner;
import shz.api.entity.SysApi;
import shz.core.FieldSetter;
import shz.core.msg.ServerFailureMsg;
import shz.core.structure.UnInfo;
import shz.jdbc.JdbcService;
import shz.orm.annotation.Transactional;
import shz.orm.enums.Condition;
import shz.spring.BeanContainer;
import shz.spring.api.ApiInfo;

import java.util.List;

public class ApiGather implements CommandLineRunner {
    final String appName;
    final JdbcService jdbcService;

    public ApiGather(String appName, JdbcService jdbcService) {
        this.appName = appName;
        this.jdbcService = jdbcService;
    }

    @Override
    public void run(String... args) {
        if ("dev".equalsIgnoreCase(BeanContainer.getProperty("spring.profiles.active"))) collect();
    }

    /**
     * 收集项目api接口
     */
    @Transactional
    protected final void collect() {
        List newDataset = ApiInfo.generate(dto -> {
            SysApi sysApi = FieldSetter.copy(dto, new SysApi());
            sysApi.setAppName(appName);
            sysApi.setRequestModel(JSON.toJSONString(dto.getReqVo()));
            sysApi.setResponseModel(JSON.toJSONString(dto.getResVo()));
            return sysApi;
        });
        List oldDataset = jdbcService.selectListByColumn(SysApi.class, null, "appName", appName, Condition.EQ, Boolean.FALSE);
        UnInfo unInfo = UnInfo.of(newDataset, oldDataset, SysApi::getId, SysApi::setId, SysApi::getPath, SysApi::getMethod);
        ServerFailureMsg.requireNon(
                jdbcService.batchFail(jdbcService.batchDeleteById(unInfo.getDelete()))
                        || jdbcService.batchFail(jdbcService.batchUpdateById(unInfo.getUpdate()))
                        || jdbcService.batchFail(jdbcService.batchInsert(unInfo.getInsert())),
                "更新API失败"
        );
        afterDelete(unInfo.getDelete());
        afterUpdate(unInfo.getUpdate());
        afterInsert(unInfo.getInsert());
    }

    protected void afterDelete(List entities) {
    }

    protected void afterUpdate(List entities) {
    }

    protected void afterInsert(List entities) {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy