package com.ywt.controllers.mg.interceptors;

/**
 * 保存当前请求与业务相关的信息上下文
 * 由拦截器层提供信息初始化
 * Created by dyh2020
 */
public class WebAppContext {
    private static WebAppContext _webAppContext = null;
    private static Object _lockObj = new Object();

    private ThreadLocal<Long> _requestStartTime = new ThreadLocal<>();
    private ThreadLocal<String> _requestID = new ThreadLocal<>();
    private ThreadLocal<String> _ip = new ThreadLocal<>();
    private ThreadLocal<String> _tokenid = new ThreadLocal<>();
    private ThreadLocal<String> _unionId = new ThreadLocal<>();
    private ThreadLocal<String> _openId = new ThreadLocal<>();
    private ThreadLocal<Integer> _userId = new ThreadLocal<>();
    private ThreadLocal<Integer> _doctorId = new ThreadLocal<>();
    private ThreadLocal<String> _doctorName = new ThreadLocal<>();
    private ThreadLocal<Integer> _terminal = new ThreadLocal<>();
    private ThreadLocal<String> _gwProtocol = new ThreadLocal<>();
    private ThreadLocal<String> _gwAuthAppid = new ThreadLocal<>();
    private ThreadLocal<Integer> _shopId = new ThreadLocal<>();
    private ThreadLocal<Integer> _hospitalId = new ThreadLocal<>();
    private ThreadLocal<Integer> _adminId = new ThreadLocal<>();

    private WebAppContext() {
    }

    public long getRequestStartTime() {
        return _requestStartTime.get() == null ? 0L : _requestStartTime.get();
    }

    protected void setRequestStartTime(long time) {
        _requestStartTime.set(time);
    }

    public String getRequestID() {
        return _requestID.get() == null ? "" : _requestID.get();
    }

    protected void setRequestID(String id) {
        _requestID.set(id);
    }

    public String getIP() {
        return _ip.get() == null ? "" : _ip.get();
    }

    protected void setIP(String ip) {
        _ip.set(ip);
    }

    public String getUnionId() {
        return _unionId.get() == null ? "" : _unionId.get();
    }

    protected void setUnionId(String unionId) {
        _unionId.set(unionId);
    }

    public String getOpenId() {
        return _openId.get() == null ? "" : _openId.get();
    }

    protected void setOpenId(String openId) {
        _openId.set(openId);
    }

    public Integer getUserId() {
        return _userId.get() == null ? 0 : _userId.get();
    }

    protected void setUserId(Integer userId) {
        _userId.set(userId);
    }

    public Integer getDoctorId() {
        return _doctorId.get() == null ? 0 : _doctorId.get();
    }

    public void setDoctorId(Integer doctorId) {
        _doctorId.set(doctorId);
    }

    public String getTokenID() {
        return _tokenid.get() == null ? "" : _tokenid.get();
    }

    protected void setTokenID(String tokenID) {
        _tokenid.set(tokenID);
    }

    public String getDoctorName() {
        return _doctorName.get() == null ? "" : _doctorName.get();
    }

    protected void setDoctorName(String doctorName) {
        _doctorName.set(doctorName);
    }

    public Integer getTerminal() {
        return _terminal.get() == null ? 0 : _terminal.get();
    }

    protected void setTerminal(Integer terminal) {
        this._terminal.set(terminal);
    }

    public String getGWProtocol() {
        return _gwProtocol.get() == null ? "" : _gwProtocol.get();
    }

    protected void setGWProtocol(String protocol) {
        _gwProtocol.set(protocol);
    }

    protected void setGWAuthAppid(String appid) {
        this._gwAuthAppid.set(appid);
    }

    public String getGWAuthAppid() {
        return this._gwAuthAppid.get();
    }

    public Integer getShopId() {
        return this._shopId.get();
    }

    public void setShopId(Integer _shopId) {
        this._shopId.set(_shopId);
    }

    public Integer getHospitalId() {
        return this._hospitalId.get();
    }

    public void setHospitalId(Integer _hospitalId) {
        this._hospitalId.set(_hospitalId);
    }

    public Integer getAdminId() {
        return this._adminId.get();
    }

    public void setAdminId(Integer _adminId) {
        this._adminId.set(_adminId);
    }



    protected void init() {
        _requestStartTime.set(0L);
        _requestID.set("");
        _ip.set("");
        _tokenid.set("");
        _unionId.set("");
        _openId.set("");
        _userId.set(0);
        _doctorId.set(0);
        _doctorName.set("");
        _terminal.set(0);
        _gwProtocol.set("");
        _gwAuthAppid.set("");
        _shopId.set(0);
        _hospitalId.set(0);
        _adminId.set(0);
    }

    public static WebAppContext current() {
        if (_webAppContext == null) {
            synchronized (_lockObj) {
                if (_webAppContext == null) {
                    _webAppContext = new WebAppContext();
                }
            }
        }
        return _webAppContext;
    }
}
