ExpertSearchPresenter.kt 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package com.yidianling.consultant

import android.annotation.SuppressLint
import android.text.TextUtils
import com.google.gson.Gson
import com.ydl.ydlcommon.base.BaseApp
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.mvp.base.SimplePresenter
import com.ydl.ydlcommon.utils.RxLifecycleUtils
import com.ydl.ydlcommon.utils.YDLAsyncUtils
import com.ydl.ydlcommon.utils.YDLCacheUtils
import com.ydl.ydlcommon.utils.remind.HttpErrorUtils
import com.yidianling.consultant.http.ExpertSearchDataManager
import com.yidianling.consultant.model.SearchApi
import com.yidianling.consultant.model.bean.AllFilter
import com.yidianling.consultant.model.bean.ExpertSearchBean
18
import com.yidianling.consultant.modular.singlton.ConsultAssistantDialogUtils
19 20 21 22 23 24 25 26 27 28 29 30
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * 专家搜索页Presenter
 */
class ExpertSearchPresenter : SimplePresenter<IExpertSearchView>() {

    @SuppressLint("CheckResult")
    fun fetchListHead() {
        SearchApi.getSearchApi()
31 32 33 34 35 36 37 38 39 40 41
            .searchConditions(2)
            .compose(RxLifecycleUtils.bindToLifecycle(mView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
            .compose(RxUtils.resultJavaData())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ resp ->
                mView.onHeadFetched(resp)
            }, { t ->
                HttpErrorUtils.handleError(BaseApp.getApp(), t)
                mView.fetchFailed(t.message)
            })
42 43 44 45 46
    }

    /**
     * 加载缓存
     */
47 48
    fun localData(showType: Int) {
        YDLAsyncUtils.asyncAsResult(object : YDLAsyncUtils.AsyncObjecyerResult {
49
            override fun doAsyncAction(): Any {
50 51
                return when (showType) {
                    0 -> {//按专家
52 53
                        YDLCacheUtils.getDoctorListData()
                    }
54
                    1 -> {//按服务
55 56
                        YDLCacheUtils.getServerListData()
                    }
57
                    else -> {
58 59 60 61 62 63 64
                        YDLCacheUtils.getServerListData()
                    }
                }
            }

            override fun asyncResult(`object`: Any?) {
                //如果没有缓存数据,显示加载框
65
                if (`object` !is String || TextUtils.isEmpty(`object`)) {
66 67 68
                    mView.showRefreshLayout()
                }

69
                if (`object` is String) {
70
                    val gson = Gson()
71 72
                    val bean =
                        gson.fromJson<ExpertSearchBean>(`object`, ExpertSearchBean::class.java)
73
//                    val bean = gson.fromJson<ExpertSearchBean>(`object`, object : TypeToken<ExpertSearchBean>() {}.type)
74 75 76
                    if (bean?.list != null) {
                        when (showType) {
                            0 -> {
77 78
                                mView.onDoctorListFetched(bean.list, 1, 1)
                            }
79
                            1 -> {
80 81
                                mView.onServiceListFetched(bean.list, 1, 1)
                            }
82
                            else -> {
83 84 85 86 87 88 89 90 91 92
                                mView.onServiceListFetched(bean.list, 1, 1)
                            }
                        }

                    }
                }
            }
        })
    }

93
    fun updateCache(showType: Int, searchBean: ExpertSearchBean) {
94 95
        val gson = Gson()
        val json = gson.toJson(searchBean)
96 97
        when (showType) {
            0 -> {
98 99
                YDLCacheUtils.saveDoctorListData(json)
            }
100
            1 -> {
101 102 103 104 105
                YDLCacheUtils.saveServerListData(json)
            }
        }
    }

洪国微 committed
106
    @SuppressLint("CheckResult")
107
    fun fetchListData(allFilter: AllFilter, page: Int) {
108
        //是否亲子教育字段
109 110
        ConsultAssistantDialogUtils.REALATION_EDUCATION =
            allFilter.categories.size == 1 && allFilter.categories[0].cateId == "23"
111 112 113
        var showType = 0

        val sb = StringBuffer()
114 115
        sb.append("searchWord=")
            .append(if (TextUtils.isEmpty(allFilter.searchWord)) "" else allFilter.searchWord)
116 117 118 119 120
        if (allFilter.categories.isNotEmpty()) {
            var categorys = allFilter.categories.map { it.cateId }.joinToString(",")
            if ("0" == categorys) {
                categorys = ""
            }
YKai committed
121
            sb.append("&directionTags=").append(categorys)
122
        }
123 124 125 126 127 128 129 130 131
       if (allFilter.region.value == "海外"||allFilter.region.value=="全国") {
            sb.append("&country=").append(allFilter.sub.code)
        } else{
            if (allFilter.region.code != null) {
                sb.append("&province=").append(allFilter.region.code)
            }
            if (allFilter.sub.code != null) {
                sb.append("&city=").append(allFilter.sub.code)
            }
132 133 134 135 136 137 138 139 140 141
        }
        if (allFilter.reorder.key != null) {
            sb.append("&reorder=").append(allFilter.reorder.key)
        }
        if (allFilter.enquiries.isNotEmpty()) {
            sb.append("&enquirys=").append(allFilter.enquiries.map { it.key }.joinToString(","))
        }
        if (allFilter.ages.isNotEmpty()) {
            sb.append("&ages=").append(allFilter.ages.map { it.key }.joinToString(","))
        }
142
        // 擅长人群拼接
143 144 145
        if (allFilter.specialityCrowd.isNotEmpty()) {
            sb.append("&crowdsTags=")
                .append(allFilter.specialityCrowd.map { it.key }.joinToString(","))
146
        }
147 148 149
        if (allFilter.others.isNotEmpty()) {
            sb.append("&others=").append(allFilter.others.map { it.key }.joinToString(","))
        }
konghaorui committed
150 151 152 153
//        if (allFilter.showType.key != null) {
//            showType = allFilter.showType.key!!
//            sb.append("&showType=").append(allFilter.showType.key!!)
//        }
154
        if (allFilter.title.isNotEmpty()) {
155 156
            sb.append("&title=").append(allFilter.title.map { it.key }.joinToString(","))
        }
157
        if (allFilter.priceRanges != null) {
158 159 160 161 162
            sb.append("&minPrice=").append(allFilter.priceRanges?.minPrice)
            sb.append("&maxPrice=").append(allFilter.priceRanges?.maxPrice)
        }
        sb.append("&page=").append(page)
        ExpertSearchDataManager.getHttp().searchDoctor(sb.toString())
163
            .compose(RxLifecycleUtils.bindToLifecycle(mView))
洪国微 committed
164
            .compose(RxUtils.resultJavaData())
165 166 167
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
168
                if (null != it.list && it.list.isNotEmpty()) {
konghaorui committed
169 170 171 172 173
//                    if (showType == 0) {
//                        mView.onDoctorListFetched(it.list!!, page, it.pages)
//                    } else {
//                        mView.onServiceListFetched(it.list!!, page, it.pages)
//                    }
174
                    mView.onDoctorListFetched(it.list, page, it.pages)
175
                    //更新缓存 只更新第一页的缓存
176 177
                    if (page == 1) {
                        updateCache(showType, it)
178 179 180 181 182 183 184 185 186 187 188
                    }
                } else {
                    mView.fetchListEmpty("没有搜到相关信息,换个关键词看看吧")
                }
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    mView.fetchListFailed(msg)
                }
            })
    }
}