package com.yunfeiyun.agmp.common.core.domain.model;

import com.alibaba.fastjson2.JSONObject;
import com.yunfeiyun.agmp.common.core.domain.entity.PtsUserBind;

public class PortalUser  {
    private String userId;
    private String deptId;
    private String token;
    private Long loginTime;
    private Long expireTime;
    private String ipaddr;
    private String loginLocation;
    private String browser;
    private String os;
    private String userLoginname;
    private String userName;
    private String userEmail;
    private String userMobile;
    private String portalId;
    private String ssoTokenKey;

    private IBaseUserBind userBind;



    public String getUserId()
    {
        return userId;
    }

    public void setUserId(String userId)
    {
        this.userId = userId;
    }

    public String getDeptId()
    {
        return deptId;
    }

    public void setDeptId(String deptId)
    {
        this.deptId = deptId;
    }

    public String getToken()
    {
        return token;
    }

    public void setToken(String token)
    {
        this.token = token;
    }

    public PortalUser()
    {
    }


    public PortalUser(String userId, String deptId)
    {
        this.userId = userId;
        this.deptId = deptId;
    }


    public String getUsername()
    {
        return this.getUserLoginname();
    }


    public boolean isEnabled()
    {
        return true;
    }

    public Long getLoginTime()
    {
        return loginTime;
    }

    public void setLoginTime(Long loginTime)
    {
        this.loginTime = loginTime;
    }

    public String getIpaddr()
    {
        return ipaddr;
    }

    public void setIpaddr(String ipaddr)
    {
        this.ipaddr = ipaddr;
    }

    public String getLoginLocation()
    {
        return loginLocation;
    }

    public void setLoginLocation(String loginLocation)
    {
        this.loginLocation = loginLocation;
    }

    public String getBrowser()
    {
        return browser;
    }

    public void setBrowser(String browser)
    {
        this.browser = browser;
    }

    public String getOs()
    {
        return os;
    }

    public void setOs(String os)
    {
        this.os = os;
    }

    public Long getExpireTime()
    {
        return expireTime;
    }

    public void setExpireTime(Long expireTime)
    {
        this.expireTime = expireTime;
    }


    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getUserLoginname()
    {
        return userLoginname;
    }

    public void setUserLoginname(String userLoginname)
    {
        this.userLoginname = userLoginname;
    }

    public String getUserEmail()
    {
        return userEmail;
    }

    public void setUserEmail(String email)
    {
        this.userEmail = email;
    }

    public String getUserMobile()
    {
        return userMobile;
    }

    public void setUserMobile(String userMobile)
    {
        this.userMobile = userMobile;
    }

    public String getPortalId() {
        return portalId;
    }

    public void setPortalId(String portalId) {
        this.portalId = portalId;
    }


    public IBaseUserBind getUserBind()
    {
        return userBind;
    }
    public void setUserBind(IBaseUserBind userBind)
    {
        this.userBind = userBind;
    }

    public String getSsoTokenKey() {
        return ssoTokenKey;
    }

    public void setSsoTokenKey(String ssoTokenKey) {
        this.ssoTokenKey = ssoTokenKey;
    }


    public static PortalUser userBind(String portalId , String userSsoTokenKey , LoginUser ssoUser , IBaseUserBind userBind){
        PortalUser ssoUserBind = new PortalUser();
        ssoUserBind.setPortalId(portalId);
        ssoUserBind.setSsoTokenKey( userSsoTokenKey );  //回传作为子系统的token标志
        IBaseUserBind userBinder = new PtsUserBind();
        ssoUserBind.setDeptId( ssoUser.getDeptId() );
        ssoUserBind.setToken( ssoUser.getToken() );
        ssoUserBind.setBrowser( ssoUser.getBrowser() );
        ssoUserBind.setExpireTime( ssoUser.getExpireTime() );
        ssoUserBind.setIpaddr(ssoUser.getIpaddr());
        ssoUserBind.setLoginLocation(ssoUser.getLoginLocation());
        ssoUserBind.setLoginTime(ssoUser.getLoginTime());
        ssoUserBind.setOs(ssoUser.getOs());
        ssoUserBind.setUserId(ssoUser.getUserId());
        ssoUserBind.setUserName(ssoUser.getUser().getUserName());
        ssoUserBind.setUserLoginname(ssoUser.getUser().getUserLoginname());
        ssoUserBind.setUserMobile(ssoUser.getUser().getUserMobile());
        ssoUserBind.setUserEmail(ssoUser.getUser().getUserEmail());

        userBinder.setUserBindId(userBind.getUserBindId() );
        userBinder.setUserId(userBind.getUserId());
        userBinder.setUserBindSys(userBind.getUserBindSys());
        userBinder.setUserBindSysUser(userBind.getUserBindSysUser());
        ssoUserBind.setUserBind(userBinder);
        return ssoUserBind;
    }

    public static PortalUser toBind(JSONObject ssoUser){
        PortalUser ssoUserBind = new PortalUser();
        IBaseUserBind userBinder = new PtsUserBind();
        //ssoUserBind.setUser( ssoUser.get("user") );
        ssoUserBind.setPortalId( ssoUser.get("portalId")== null ? "0" : ssoUser.get("portalId").toString() );
        ssoUserBind.setSsoTokenKey(  ssoUser.get("ssoTokenKey") == null ? "" : ssoUser.get("ssoTokenKey").toString());
        ssoUserBind.setDeptId( ssoUser.get("deptId") == null ? "" : ssoUser.get("deptId").toString() );
        ssoUserBind.setToken( ssoUser.get("token") == null ? "" : ssoUser.get("token").toString()  );
        ssoUserBind.setBrowser( ssoUser.get("browser") == null ? "" : ssoUser.get("browser").toString() );
        ssoUserBind.setExpireTime( Long.parseLong(ssoUser.get("expireTime") == null ? "0" : ssoUser.get("expireTime").toString()) );
        ssoUserBind.setIpaddr(ssoUser.get("ipaddr") == null ? "" : ssoUser.get("ipaddr").toString());
        ssoUserBind.setLoginLocation(ssoUser.get("loginLocation") == null ? "" : ssoUser.get("loginLocation").toString());
        ssoUserBind.setLoginTime( Long.parseLong(ssoUser.get("loginTime") == null ? "" : ssoUser.get("loginTime").toString()) );
        ssoUserBind.setOs(ssoUser.get("os") == null ? "" : ssoUser.get("os").toString());
        ssoUserBind.setUserId(ssoUser.get("userId") == null ? "": ssoUser.get("userId").toString());
        ssoUserBind.setUserName(ssoUser.get("userName") == null ? "": ssoUser.get("userName").toString());
        ssoUserBind.setUserLoginname(ssoUser.get("userLoginname") == null ? "" :ssoUser.get("userLoginname").toString());
        ssoUserBind.setUserMobile(ssoUser.get("userMobile") == null ? "": ssoUser.get("userMobile").toString());
        ssoUserBind.setUserEmail(ssoUser.get("userEmail") == null ? "": ssoUser.get("userEmail").toString());

        if(ssoUser.get("userBind") == null){
            ssoUserBind.setUserBind(null);
        }else {
            JSONObject userBind = JSONObject.from(ssoUser.get("userBind"));
            userBinder.setUserId(userBind.get("userId") == null ? "" : userBind.get("userId").toString());
            userBinder.setUserBindSys(userBind.get("userBindSys") == null ? "" : userBind.get("userBindSys").toString());
            userBinder.setUserBindSysUser(userBind.get("userBindSysUser") == null ? "" : userBind.get("userBindSysUser").toString());
            userBinder.setUserBindId(userBind.get("userBindId") == null ? "" : userBind.get("userBindId").toString());
            ssoUserBind.setUserBind(userBinder);
        }
        return ssoUserBind;
    }

}
