package com.kingdee.eas.custom.integration.apimgt.auth.service;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.custom.integration.apimgt.*;
import com.kingdee.eas.custom.integration.base.util.SHRApiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository
public class BosAuthDao {

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

    private Context getContext() {
        return SHRApiUtils.getContext();
    }

    public OpenPlatformAuthBillInfo findByAppSecret(String appSecret) {
        if (appSecret == null || appSecret.isEmpty()) {
            return null;
        }
        try {
            Context ctx = getContext();
            IOpenPlatformAuthBill iBill = OpenPlatformAuthBillFactory.getLocalInstance(ctx);
            OpenPlatformAuthBillCollection col = iBill.getOpenPlatformAuthBillCollection(
                    "where appSecret = '" + escape(appSecret) + "'");
            if (col == null || col.isEmpty()) {
                return null;
            }
            return (OpenPlatformAuthBillInfo) col.get(0);
        } catch (BOSException e) {
            logger.error("查询平台授权单失败: {}", e.getMessage(), e);
            return null;
        }
    }

    /**
     * 根据接口路径和方法查询接口注册信息
     *
     * @param apiPath
     * @param httpMethod
     * @return
     */
    public ApiRegistryBillInfo findApiByPathAndMethod(String apiPath, String httpMethod) {
        if (apiPath == null || httpMethod == null) {
            return null;
        }
        try {
            Context ctx = getContext();
            IApiRegistryBill iApi = ApiRegistryBillFactory.getLocalInstance(ctx);
            ApiRegistryBillCollection col = iApi.getApiRegistryBillCollection(
                    "where apiPath = '" + escape(apiPath)
                            + "' and httpMethod = '" + escape(httpMethod) + "'");
            if (col == null || col.isEmpty()) {
                return null;
            }
            return col.get(0);
        } catch (BOSException e) {
            logger.error("查询接口注册信息失败: {}", e.getMessage(), e);
            return null;
        }
    }

    public boolean hasApiPermission(OpenPlatformAuthBillInfo platform, ApiRegistryBillInfo api) {
        if (platform == null || api == null) {
            return false;
        }
        try {
            Context ctx = getContext();
            IOpenPlatformAuthBillApiEntry iEntry =
                    OpenPlatformAuthBillApiEntryFactory.getLocalInstance(ctx);
            String oql = "where parent.id = '" + escape(platform.getId().toString())
                    + "' and api.id = '" + escape(api.getId().toString()) + "'";
            OpenPlatformAuthBillApiEntryCollection col =
                    iEntry.getOpenPlatformAuthBillApiEntryCollection(oql);
            return col != null && !col.isEmpty();
        } catch (BOSException e) {
            logger.error("查询接口权限失败: {}", e.getMessage(), e);
            return false;
        }
    }

    private String escape(String s) {
        return s == null ? "" : s.replace("'", "''");
    }
}
