InteractionStatusDataFetcher.java 11.5 KB
package com.wd.common.fetcher;

import android.text.TextUtils;


import com.wd.capability.layout.ui.follow.listener.BatchCallback;
import com.wd.common.listener.InteractionDataListener;
import com.wd.foundation.wdkit.utils.PDUtils;
import com.wd.capability.network.BaseObserver;
import com.wd.capability.network.constant.ParameterConstant;
import com.wd.capability.network.fetcher.BaseDataFetcher;
import com.wd.capability.network.response.BaseResponse;
import com.wd.common.api.RequestApi;
import com.wd.foundation.bean.comment.DisplayWorkInfoBean;
import com.wd.foundation.bean.custom.content.ContentBean;
import com.wd.foundation.bean.custom.content.ContentTypeConstant;
import com.wd.foundation.bean.live.RoomDataBean;
import com.wd.foundation.bean.mail.LiveInfo;
import com.wd.foundation.wdkit.constant.Constants;
import com.wd.foundation.wdkit.utils.CommonUtil;
import com.wd.foundation.wdkitcore.tools.ArrayUtils;
import com.wd.foundation.wdkitcore.tools.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * 描述:评论相关的数据基础接口,通用性很高,接口隔离原则
 *
 * @author : lvjinhui
 * @since: 2023/5/30
 */
public class InteractionStatusDataFetcher extends BaseDataFetcher<RequestApi> {


    private InteractionDataListener mListener;

    public InteractionStatusDataFetcher(InteractionDataListener mListener) {
        this.mListener = mListener;
    }

    public InteractionStatusDataFetcher() {
    }

    /**
     * 批量查询点赞和收藏状态不
     *
     * @param mTempList
     */
    public void sendBatchLikeAndCollectStatusRequest(List<ContentBean> mTempList) {
        //登录才有必要查询
        if (!PDUtils.isLogin()) {
            if (mListener != null) {
                mListener.onBatchDataFail();
            }
            return;
        }
        if (ArrayUtils.isEmpty(mTempList)) {
            if (mListener != null) {
                mListener.onBatchDataFail();
            }
            return;
        }
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> listMap = new ArrayList<>();
        for (ContentBean bean : mTempList) {
            if (null == bean) {
                continue;
            }
            Map<String, Object> mapT = new HashMap<>();
            String contentId = bean.getObjectId();
            // 内容 id 为空,不塞到数组里查询点赞收藏状态
            if (StringUtils.isBlank(contentId)) {
                continue;
            }
            mapT.put(ParameterConstant.CONTENTID, contentId);
            String contentType = bean.getObjectType();
            // 内容类型为空,不塞到数组里查询点赞收藏状态
            if (StringUtils.isBlank(contentType)) {
                continue;
            }
            mapT.put(ParameterConstant.CONTENT_TYPE, contentType);
            if (!TextUtils.isEmpty(bean.getContentRelId()) && !"0".equals(bean.getContentRelId())) {
                mapT.put(ParameterConstant.CONTENT_RELLD, bean.getContentRelId());
            }
            if (0 != bean.getRelType()) {
                mapT.put(ParameterConstant.CONTENT_REL_TYPE, bean.getRelType());
            }
            listMap.add(mapT);
        }

        // 要查询的点赞和收藏的内容集合为空,则不进行查询点赞收藏状态
        if (CommonUtil.isEmpty(listMap)) {
            if (mListener != null) {
                mListener.onBatchDataFail();
            }
            return;
        }

        map.put("contentList", listMap);
        Observable<BaseResponse<List<DisplayWorkInfoBean>>> codeData = getRetrofit().batchLikeAndCollectStatus(getBody(map));
        request(codeData, new BaseObserver<List<DisplayWorkInfoBean>>() {
            @Override
            protected void dealSpecialCode(int code, String message) {
                if (mListener != null) {
                    mListener.onBatchDataFail();
                }
            }

            @Override
            public void onSuccess(List<DisplayWorkInfoBean> backList) {

                for (ContentBean bean : mTempList) {
                    String id = bean.getObjectId();
                    if (TextUtils.isEmpty(id)) {
                        continue;
                    }
                    for (DisplayWorkInfoBean infoBean : backList) {
                        String tempId = infoBean.getContentId();
                        if (id.equals(tempId)) {
                            bean.setLikeStatus(infoBean.getLikeStatus());
                            bean.setCollectStatus(infoBean.getCollectStatus());
                            break;
                        }
                    }
                }

                if (mListener != null) {
                    mListener.onBatchDataSuccess(1);
                }
            }

            @Override
            public void _onError(String e) {
                if (mListener != null) {
                    mListener.onBatchDataFail();
                }
            }
        });

    }

    /**
     * 批量查询点赞和收藏状态不
     *
     * @param list
     */
    public void sendBatchLikeAndCollectStatusRequest(List<ContentBean> list, BatchCallback<List<DisplayWorkInfoBean>> batchCallback) {

        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> listMap = new ArrayList<>();
        for (ContentBean bean : list) {
            if (bean == null) {
                continue;
            }
            Map<String, Object> mapT = new HashMap<>();
            String contentId = bean.getObjectId();
            // 内容 id 为空,不塞到数组里查询点赞收藏状态
            if (StringUtils.isBlank(contentId)) {
                continue;
            }
            mapT.put(ParameterConstant.CONTENTID, contentId);
            String contentType = bean.getObjectType();
            // 内容类型为空,不塞到数组里查询点赞收藏状态
            if (StringUtils.isBlank(contentType)) {
                continue;
            }
            mapT.put(ParameterConstant.CONTENT_TYPE, contentType);
            if (!TextUtils.isEmpty(bean.getContentRelId()) && !"0".equals(bean.getContentRelId())) {
                mapT.put(ParameterConstant.CONTENT_RELLD, bean.getContentRelId());
            }
            if (0 != bean.getRelType()) {
                mapT.put(ParameterConstant.CONTENT_REL_TYPE, bean.getRelType());
            }
            listMap.add(mapT);
        }

        // 要查询的点赞和收藏的内容集合为空,则不进行查询点赞收藏状态
        if (CommonUtil.isEmpty(listMap)) {
            return;
        }
        map.put("contentList", listMap);

        if (batchCallback != null) {
            batchCallback.parameter(map.toString());
        }
        Observable<BaseResponse<List<DisplayWorkInfoBean>>> codeData = getRetrofit().batchLikeAndCollectStatus(getBody(map));
        request(codeData, new BaseObserver<List<DisplayWorkInfoBean>>() {
            @Override
            protected void dealSpecialCode(int code, String message) {

                if (batchCallback != null) {
                    batchCallback.error(message);
                }
            }

            @Override
            public void onSuccess(List<DisplayWorkInfoBean> backList) {

                for (ContentBean bean : list) {
                    String id = bean.getObjectId();
                    if (TextUtils.isEmpty(id)) {
                        continue;
                    }
                    for (DisplayWorkInfoBean infoBean : backList) {
                        String tempId = infoBean.getContentId();
                        if (id.equals(tempId)) {
                            bean.setLikeStatus(infoBean.getLikeStatus());
                            bean.setCollectStatus(infoBean.getCollectStatus());
                            break;
                        }
                    }
                }
                if (batchCallback != null) {
                    batchCallback.success(backList);
                }
            }

            @Override
            public void _onError(String e) {
                if (batchCallback != null) {
                    batchCallback.error(e);
                }
            }
        });
    }


    /**
     * 批量查C端查询直播信息(观看人次、点赞人数、评论数、订阅数)
     *
     * @param list
     */
    public void sendBatchLiveRoomDataRequest(List<ContentBean> list, BatchCallback<List<RoomDataBean>> batchCallback) {

        HashMap<String, Object> queryMap = new HashMap<>();
        List<String> listMap = new ArrayList<>();
        String liveType = String.valueOf(ContentTypeConstant.URL_TYPE_TWO);
        for (ContentBean bean : list) {
            if (bean == null) {
                continue;
            }
            // 直播结束和进行中的业务数据参与批查
            if (liveType.equals(bean.getObjectType())) {
                LiveInfo liveInfo = bean.getLiveInfo();
                if (liveInfo != null) {
                    String liveState = liveInfo.getLiveState();
                    if (Constants.LIVE_RUNNING.equals(liveState) || Constants.LIVE_END.equals(liveState)) {
                        if (!listMap.contains(bean.getObjectId())) {
                            listMap.add(bean.getObjectId());
                        }

                    }
                }
            }
        }

        if (CommonUtil.isEmpty(listMap)) {
            if (batchCallback != null) {
                batchCallback.success(new ArrayList<>());
            }
            return;
        }

        StringBuffer sb = new StringBuffer();

        int size = listMap.size();
        for (int a = 0; a < size; a++) {
            sb.append(listMap.get(a));
            if (a == size - 1) {
            } else {
                sb.append(",");
            }
        }

        queryMap.put("liveIdList", sb.toString());
        if (batchCallback != null) {
            batchCallback.parameter(queryMap.toString());
        }
        Observable<BaseResponse<List<RoomDataBean>>> codeData = getRetrofit().getRoomBatchData(queryMap);
        request(codeData, new BaseObserver<List<RoomDataBean>>() {
            @Override
            protected void dealSpecialCode(int code, String message) {

                if (batchCallback != null) {
                    batchCallback.error(message);
                }
            }

            @Override
            public void onSuccess(List<RoomDataBean> backList) {


                if (backList != null) {
                    // 内容信息匹配直播批量数据
                    for (ContentBean contentBean : list) {
                        if (contentBean == null) {
                            continue;
                        }
                        if (liveType.equals(contentBean.getObjectType()) && contentBean.getLiveInfo() != null) {
                            for (RoomDataBean roomDataBean : backList) {
                                if (contentBean.getObjectId().equals(roomDataBean.liveId)) {
                                    contentBean.getLiveInfo().infoBean = roomDataBean;
                                }
                            }
                        }
                    }
                }

                if (batchCallback != null) {
                    batchCallback.success(backList);
                }
            }

            @Override
            public void _onError(String e) {
                if (batchCallback != null) {
                    batchCallback.error(e);
                }
            }
        });
    }
}