package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.OnlinePersonBean;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author : Pengxh
 * @time : 2021/4/15 11:21
 * @email : 290677893@qq.com
 **/
public class PersonOnlineModelImpl implements IPersonOnlineModel {

    private OnGetOnlinePersonListener listener;

    public PersonOnlineModelImpl(OnGetOnlinePersonListener onlinePersonListener) {
        this.listener = onlinePersonListener;
    }

    /**
     * 数据回调接口
     */
    public interface OnGetOnlinePersonListener {
        void onSuccess(OnlinePersonBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest(String deptid, String roleTips, String isOnLine) {
        Observable<OnlinePersonBean> observable = RetrofitServiceManager.getOnlinePersonResult(deptid, roleTips, isOnLine);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<OnlinePersonBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e.getCause() != null) {
                    listener.onFailure(e);
                }
            }

            @Override
            public void onNext(OnlinePersonBean resultBean) {
                if (resultBean != null) {
                    listener.onSuccess(resultBean);
                }
            }
        });
    }
}
