package com.kingdee.eas.custom.integrationWeb.apimgt.handler;

import com.kingdee.eas.custom.integration.apimgt.sync.ApiRegistrySyncRunner;
import com.kingdee.eas.custom.integration.base.util.SpringContextHolder;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * API 注册单列表界面 Handler。
 */
public class ApiRegistryBillListHandler extends ListHandler {

    private static final Logger logger = LoggerFactory.getLogger(ApiRegistryBillListHandler.class);

    /**
     * 手动重新扫描 API 接口并同步到 ApiRegistryBill 表。
     * 通过 SpringContextHolder 获取 ApiRegistrySyncRunner 调用其 rescan 方法。
     */
    public void rescanApiAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        try {
            ApiRegistrySyncRunner runner = SpringContextHolder.getBean(ApiRegistrySyncRunner.class);
            if (runner == null) {
                throw new SHRWebException("ApiRegistrySyncRunner 未初始化，请先启动 Spring 容器");
            }
            Map<String, Integer> result = runner.rescan();
            logger.info("手动重新扫描 API 完成: {}", result);
        } catch (Exception e) {
            logger.error("手动重新扫描 API 失败: {}", e.getMessage(), e);
            throw new ShrWebBizException("手动重新扫描 API 失败: " + e.getMessage(), e);
        }
    }
}
