package com.seeyon.apps.bip.ctrl;

import cn.hutool.http.HttpRequest;
import com.seeyon.apps.addressbook.manager.AddressBookCustomerFieldInfoManager;
import com.seeyon.apps.addressbook.po.AddressBook;
import com.seeyon.apps.bip.constants.BipConstants;
import com.seeyon.apps.bip.util.BipHttpUtil;
import com.seeyon.apps.bip.vo.BipResponse;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.plugin.vo.ConfigVo;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.taglibs.functions.Functions;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.manager.OrgManagerImpl;
import com.seeyon.ctp.util.Strings;
import org.springframework.web.servlet.ModelAndView;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述:<br>
 * <pre>
 *
 * </pre>
 *
 * @Author: FanGaowei
 * @Date: 2022/12/1
 */
public class NccSsoController extends BaseController {

    @Inject
    private ICstConfigApi cstConfigApi;
    @Inject
    private AddressBookCustomerFieldInfoManager addressBookCustomerFieldInfoManager;


    @Override
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ConfigVo config = cstConfigApi.getConfig(BipConstants.getPluginId());
        String url = config.getParamVal(BipConstants.ssoUrl.name());
//        获取当前登录人ID    AppContext.currentUserId()
//        OrgManager orgManager = new OrgManagerImpl();
//        orgManager.getMemberById(AppContext.currentUserId());根据人员ID获取人员信息
        AddressBook addressBook = addressBookCustomerFieldInfoManager.getByMemberId(AppContext.currentUserId());
        if(null == addressBook) {
            super.rendJavaScript(response, "alert('BIP账号未维护，无法单点登录！');");
            return null;
        }
        String code = addressBook.getExtAttr2();
        if(Strings.isBlank(code)) {
            super.rendJavaScript(response, "alert('BIP账号未维护，无法单点登录！');");
            return null;
        }
        // 获取token走内网地址
        String interUrl = config.getParamVal(BipConstants.bipUrl.name());
        String tokenUrl = interUrl + "/servlet/NCTokenServlet?dsname=" + config.getParamVal(BipConstants.dsname.name())
                + "&usercode=" + code + "&system=OA&busicentercode=" + config.getParamVal(BipConstants.busicentercode.name());
        try {
            String body = HttpRequest.get(tokenUrl).execute().body();
            BipResponse bipResponse = BipHttpUtil.parseResponse(body);
            if(bipResponse.isSuccess()) {
                String ssoUrl = url + "/nccloud/resources/uap/rbac/thirdpartylogin/main/index.htmlac?cesstoken=" + bipResponse.getData()
                    + "&redirect_uri=" + url + "/nccloud";
                response.sendRedirect(ssoUrl);
            } else {
                super.rendJavaScript(response, "alert('单点登录错误：" + bipResponse.getMsg() + "');");
            }
        } catch (Exception e) {
            logger.error("单点登录异常", e);
            super.rendJavaScript(response, "alert('后台发生错误');");
        }
        return null;
    }
}
