package com.lanyuanxiaoyao.service.web.controller.base;

import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.configuration.Constants;
import com.lanyuanxiaoyao.service.configuration.exception.TableNotFoundException;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.MutableMap;

/**
 * 放一些 Controller 的辅助方法
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-21
 */
public class BaseController {
    protected MutableMap<String, Object> buildQueryMap(
            Integer page,
            Integer count,
            String order,
            String direction,
            String searchFlinkJobId,
            String searchAlias
    ) {
        MutableMap<String, Object> queryMap = Maps.mutable.empty();
        queryMap.put("page", page);
        queryMap.put("count", count);
        if (StrUtil.isNotBlank(searchFlinkJobId)) {
            queryMap.put("flink_job_id", searchFlinkJobId);
        }
        if (StrUtil.isNotBlank(searchAlias)) {
            queryMap.put("alias", searchAlias);
        }
        if (StrUtil.isNotBlank(order) && StrUtil.isNotBlank(direction)) {
            queryMap.put("order", order);
            queryMap.put("direction", direction);
        }
        return queryMap;
    }

    protected void checkTableExists(InfoService infoService, Long flinkJobId, String alias) throws TableNotFoundException {
        if (infoService.nonExistsTable(flinkJobId, alias)) {
            throw new TableNotFoundException(Constants.SERVICE_NAME_INFO_QUERY, flinkJobId, alias);
        }
    }
}
