package nckd.jimin.jyyy.hr.tsrsc.plugin.thirdapp.auth;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dc.api.model.Account;
import kd.bos.entity.plugin.support.util.StringUtils;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.login.thirdauth.app.AppAuthResult;
import kd.bos.login.thirdauth.app.ThirdAppAuthtication;
import kd.bos.login.thirdauth.app.UserType;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import nckd.jimin.jyyy.hr.tsrsc.plugin.util.MokaApiUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * Moka第三方单点登陆
 *
 * @author ：liuxiao
 * @since ：Created in 11:35 2024/12/24
 */
public class MokaThirdAppAuthtication extends ThirdAppAuthtication {
    private static final Log LOG = LogFactory.getLog(MokaThirdAppAuthtication.class);

    /**
     * 检查请求url是否需要本插件认证，如果是返回true，否返回false
     */
    @Override
    public boolean isNeedHandle(HttpServletRequest request, Account account) {
        // 是否需要通过第三方办公APP插件认证
        boolean isNeed = false;
        // 第三方用户身份标识 参数名根据实际情况而定
        String token = request.getParameter("token");
        LOG.info("MokaThirdAppAuthtication.isNeedHandle.token：{}", token);
        // 请求 url携带的判断第三方办公APP认证插件类型  示例：金蝶云使用jindieyun 可自行定义传参用于判断
        String thirdAppType = request.getParameter("thirdAppType");
        LOG.info("MokaThirdAppAuthtication.isNeedHandle.thirdAppType：{}", thirdAppType);
        if (!StringUtils.isEmpty(token) && "moka".equals(thirdAppType)) {
            isNeed = true;
        }
        return isNeed;
    }

    /**
     * 实现苍穹调用第三方办公APP系统的相关接口完成认证，返回认证结果。
     */
    @Override
    public AppAuthResult appAuthtication(HttpServletRequest request, Account account) {
        LOG.info("MokaThirdAppAuthtication.appAuthtication.start");
        LOG.info("MokaThirdAppAuthtication.appAuthtication.accountName : {}", account.getAccountName());
        AppAuthResult result = new AppAuthResult();
        result.setSucceed(false);
        //第三方用户身份标识 参数名根据实际情况而定
        String token = request.getParameter("token");
        // 根据token查email
        QFilter filter = new QFilter("nckd_accesstoken", QCP.equals, token);
        filter.and("createtime", QCP.large_equals, MokaApiUtil.offsetMinute(5));
        DynamicObject tokenObj = BusinessDataServiceHelper.loadSingle("nckd_mokatoken", new QFilter[]{filter});
        LOG.info("MokaThirdAppAuthtication.appAuthtication.filter: {}", filter);
        LOG.info("MokaThirdAppAuthtication.appAuthtication.tokenObj: {}", tokenObj);
        if (tokenObj == null) {
            return result;
        }
        String number = tokenObj.getString("nckd_email");
        LOG.info("MokaThirdAppAuthtication.appAuthtication.email：{}", number);
        // 系统中是否存在该邮箱用户
        DynamicObject user = BusinessDataServiceHelper.loadSingle(
                "bos_user",
                new QFilter[]{
                        // 操作人邮箱
                        new QFilter("number", QCP.equals, number),
                }
        );
        if (user == null) {
            return result;
        }

        result.setSucceed(true);
        // 用户标识，包括手机号、邮箱、用户名
        result.setUserFlag(number);
        // 用户类型 枚举类UserType 对应用户标识
        result.setUserType(UserType.WORKER_NUMBER);
        return result;
    }
}
