package cn.ey.biz.module.system.service.category.dynamic.storage;

import cn.ey.biz.common.enums.CommonStatusEnum;
import cn.ey.biz.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ey.biz.module.system.dal.dataobject.dynamicapi.DynamicApiDO;
import cn.ey.biz.module.system.dal.mapper.dynamicapi.DynamicApiMapper;
import cn.ey.biz.module.system.framework.dynamic_api.core.AbstractResourceStorage;
import cn.ey.biz.module.system.framework.dynamic_api.core.model.ApiInfo;
import cn.ey.biz.module.system.framework.dynamic_api.core.util.PathUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class CategoryResourceStorage extends AbstractResourceStorage {

    private final DynamicApiMapper dynamicApiMapper;

    private volatile List<ApiInfo> apiInfos;

    @Override
    public List<ApiInfo> getEntities() {
        return this.apiInfos;
    }

    @PostConstruct
    private void initDynamicApiData() {
        List<DynamicApiDO> dictApiList = dynamicApiMapper.selectList(new LambdaQueryWrapperX<DynamicApiDO>()
                .eq(DynamicApiDO::getApiGroup, "DICT")
                .eq(DynamicApiDO::getStatus, CommonStatusEnum.ENABLE.getStatus()));
        apiInfos = dictApiList.stream().map(item -> {
            ApiInfo apiInfo = new ApiInfo();
            apiInfo.setId(String.valueOf(item.getApiId()))
                    .setMethod(item.getRequestMethod())
                    .setName(item.getApiName())
                    .setPath(PathUtils.replaceSlash(item.getPathPrefix() + item.getPathUrl()));
            return apiInfo;
        }).collect(Collectors.toList());
    }
}
