package com.rc.httpcore.client.retrofit

import android.util.Log
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.rc.httpcore.HttpClient
import com.rc.httpcore.apk.AppInfo
import com.rc.httpcore.apk.ProcessedBean
import com.rc.httpcore.bean.BelongingPersonBean
import com.rc.httpcore.bean.ChemicalABean
import com.rc.httpcore.bean.ChemicalConfsBean
import com.rc.httpcore.bean.ChemicalInfoBean
import com.rc.httpcore.bean.ChemicalSearchBean
import com.rc.httpcore.bean.ChemicalTypeBean
import com.rc.httpcore.bean.ConfigBean
import com.rc.httpcore.bean.EarlyWarningBean
import com.rc.httpcore.bean.GiveBackBean
import com.rc.httpcore.bean.HxpChemicalVo
import com.rc.httpcore.bean.HxpControlConfig
import com.rc.httpcore.bean.HxpStockWaitModeBean
import com.rc.httpcore.bean.InventoryListBean
import com.rc.httpcore.bean.LockVoListBean
import com.rc.httpcore.bean.MsdsBean
import com.rc.httpcore.bean.MsdsListBean
import com.rc.httpcore.bean.PageHxpStockBean
import com.rc.httpcore.bean.QueryTwoBean
import com.rc.httpcore.bean.ReturnDetailsBean
import com.rc.httpcore.bean.ReturnGiveBackBean
import com.rc.httpcore.bean.ReturningChemicalsBean
import com.rc.httpcore.bean.RuleBean
import com.rc.httpcore.bean.SearchBean
import com.rc.httpcore.bean.SearchSpecBean
import com.rc.httpcore.bean.StockModeBean
import com.rc.httpcore.bean.StockModel
import com.rc.httpcore.bean.TopicDataBean
import com.rc.httpcore.bean.UnifiedVerBean
import com.rc.httpcore.bean.UseBean
import com.rc.httpcore.bean.UseLedBean
import com.rc.httpcore.bean.UseNoBean
import com.rc.httpcore.bean.UseNoTwoBean
import com.rc.httpcore.bean.UsePostDataBean
import com.rc.httpcore.bean.UserData
import com.rc.httpcore.bean.UserValidationBean
import com.rc.httpcore.bean.WasteChemicalsBean
import com.rc.httpcore.client.ChemicalClient
import com.rc.httpcore.exception.NetException
import com.rc.httpcore.vo.request.FaceCompareReq
import com.rc.httpcore.vo.response.ConfLogo
import io.reactivex.rxjava3.core.Observable
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


class ChemicalRetrofit : ChemicalClient {

    private val apiService by lazy {
        HttpClient.createRetrofitApi(ApiService::class.java)
    }

    /**
     * 查询LOGO配置列表
     */
    override fun queryLogoConfInfo(): Observable<ConfLogo> {
        return apiService.queryLogoConfInfo()
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map response.data!!
            }
    }

//    override fun apkVersion(
//        deviceNum: String,
//        type: Int,
//        version: Double
//    ): Observable<AppUpDateBean> {
//        return apiService.apkVersion(deviceNum, type, version)
//            .map { response ->
//                if (!response.isSuccess()) {
//                    throw NetException(response.code, response.message)
//                }
//                return@map response.data!!
//            }
//    }

    override fun getCheck(deviceNum: String, type: String, version: Double): Observable<AppInfo> {
        return apiService.getCheck(deviceNum, type, version)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun cardNum(cardNum: String): Observable<UserData> {
        return apiService.cardNum(cardNum)
            .map { response ->
                if (!response.isSuccess()) {
//                    throw NetException(response.code, response.message)
                    throw NetException(response.code, response.message)
                }

                HttpClient.token = response.data?.token!!
                LogUtils.json("token", response.data)
                return@map response.data!!
            }
    }

    override fun userCardValidation(userId: String, subId: String): Observable<UserValidationBean> {
        Log.d("====人脸登录-验证", "====人脸登录-验证")
        return apiService.userCardValidation(userId, subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun useCardVerify(map: Map<String, String>): Observable<UserValidationBean> {
        val gson = Gson()
        val json = gson.toJson(map)
        Log.d("=====q==", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.useCardVerify(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

//                HttpClient.token = response.data?.token
                return@map response.data!!
            }
    }

    override fun searchChemica(
        searchChemica: String,
        chemicalLevel: String?
    ): Observable<List<ChemicalSearchBean>> {
        return apiService.searchChemica(searchChemica, chemicalLevel)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map response.data!!
            }
    }

    override fun getOptions(typs: String): Observable<List<ChemicalTypeBean>> {
        return apiService.getOptions(typs)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map response.data!!
            }
    }

    override fun searchProducer(): Observable<List<SearchBean>> {
        return apiService.searchProducer()
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map response.data!!
            }
    }

    override fun searchSpec(specNum: String): Observable<SearchSpecBean> {
        return apiService.searchSpec(specNum)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map response.data!!
            }
    }

    override fun getSelects(searchValue: String): Observable<List<BelongingPersonBean>> {
        val json = "{\"searchValue\":\"$searchValue\"}"
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.getSelects(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map response.data!!
            }
    }

    override fun getTopicList(userId: String): Observable<List<TopicDataBean>> {
//        val json = "{}"
//        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.getTopicList(userId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map response.data!!
            }
    }

    override fun getCabinetList(subId: String): Observable<List<RuleBean>> {
        return apiService.getCabinetList(subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!!!
            }
    }

    override fun getScanCode(macId: String, code: String, subId: String): Observable<UserData> {
        return apiService.getScanCode(macId, code, subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                HttpClient.token = response.data?.token!!
                return@map response.data!!!!
            }
    }

    override fun certitude(model: ChemicalABean): Observable<ChemicalABean> {
        val gson = Gson()
        val json = gson.toJson(model)
        Log.d("=========新增确定====", "$json")
        return apiService.certitude(model)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun certitude(model: HxpChemicalVo): Observable<HxpChemicalVo> {
        val gson = Gson()
        val json = gson.toJson(model)
        Log.d("=========新增确定====", "$json")
        return apiService.certitude(model)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getRelList(subjectId: String): Observable<ChemicalInfoBean> {
        val json = "{\"subId\":$subjectId}"
        LogUtils.d("获取实验室信息", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.getRelList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!!![0]
            }
    }

    override fun getUserIds(subId: String): Observable<List<String>> {
        return apiService.getUserIds(subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data as List<String>
            }
    }

    override fun getCabinetBySubId(subId: String): Observable<List<RuleBean>> {
        return apiService.getCabinetBySubId(subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun stockWaitAdd(model: StockModeBean): Observable<Boolean> {
        val gson = Gson()
        val jsonArray = gson.toJson(model)
        Log.d("=========新增入库", jsonArray)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), jsonArray)
        return apiService.stockWaitAdd(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun addStockCheck(model: StockModel): Observable<Boolean> {
        val gson = Gson()
        val jsonArray = gson.toJson(model)
        Log.d("=========校验", jsonArray)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), jsonArray)
        return apiService.addStockCheck(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map true
            }
    }

    override fun getStockWaitDetails(waitId: String): Observable<UnifiedVerBean> {
        return apiService.getStockWaitDetails(waitId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun waitList(mutableMap: Map<String, Any>): Observable<PageHxpStockBean> {
        val gson = Gson()
        val jsonArray = gson.toJson(mutableMap)
        Log.d("=领用==", "$jsonArray")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), jsonArray)
        return apiService.waitList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getStockList(
        mutableMap: Map<String, Any>
    ): Observable<HxpStockWaitModeBean> {

        val gson = Gson()
        val jsonArray = gson.toJson(mutableMap)
        Log.d("=领用==", "$jsonArray")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), jsonArray)
        return apiService.getStockList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getByRfid(rfidCode: String): Observable<UseBean> {
        return apiService.getByRfid(rfidCode)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun indexDetailbyRfid(rfidCode: String): Observable<UseNoBean> {
        return apiService.indexDetailbyRfid(rfidCode)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getStockDetailsByCode(rfidCode: String): Observable<UseNoTwoBean> {
        Log.d("=rfidCode==", "$rfidCode")
        return apiService.getStockDetailsByCode(rfidCode)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun addUseRecord(model: UsePostDataBean): Observable<String> {
        val gson = Gson()
        val jsonArray = gson.toJson(model)
        Log.d("=领用==", "$jsonArray")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), jsonArray)
        return apiService.addUseRecord(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun useList(mutableMap: Map<String, String>): Observable<ReturningChemicalsBean> {
//        val json =
//            "{\"subId\":$subId,\"backStatusCollect\":[1,2],\"cabinetId\":$cabinetId,\"doorId\":$doorId,\"layers\":$layers,\"searchValue\":${searchValue},\"page\":${page},\"pageSize\":${pageSize}}"
//        Log.d("===", "$json")
        val gson = Gson()
        val json = gson.toJson(mutableMap)
        Log.d("=========查询信息======", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.useList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun updateRfid(
        ids: String,
        rfidCode: String?
    ): Observable<String> {
        val json =
            "{\"id\":$ids,\"rfidCode\":\"$rfidCode\"}"
        Log.d("==更换rfid=", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.updateRfid(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun backDetail(rfidCode: String, subId: String): Observable<ReturnDetailsBean> {
        return apiService.backDetail(rfidCode, subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun chemicalSearchBean(codes: String, subId: String): Observable<ChemicalSearchBean> {
        return apiService.scanCodeChemical(codes, subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun discardDetail(rfidCode: String): Observable<ReturnDetailsBean> {
        return apiService.discardDetail(rfidCode)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun giveBack(model: GiveBackBean): Observable<Boolean> {
        val gson = Gson()
        val jsonArray = gson.toJson(model)
        Log.d("==归还=", "$jsonArray")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), jsonArray)
        return apiService.giveBack(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map true
            }
    }

    override fun returnGiveBack(model: GiveBackBean): Observable<ReturnGiveBackBean> {
        val gson = Gson()
        val jsonArray = gson.toJson(model)
        Log.d("==归还=", "$jsonArray")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), jsonArray)
        return apiService.returnGiveBack(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun stockDetailsList(
        mutableMap: Map<String, String>
    ): Observable<WasteChemicalsBean> {
        val gson = Gson()
        val json = gson.toJson(mutableMap)
        Log.d("=========查询信息======", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.stockDetailsList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getCabinetLock(doorId: String): Observable<List<LockVoListBean>> {
        return apiService.getCabinetLock(doorId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun indexStockList(mutableMap: Map<String, String>): Observable<HxpStockWaitModeBean> {
        val gson = Gson()
        val json = gson.toJson(mutableMap)
        Log.d("=========查询信息======", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.indexStockList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun warningNoticeList(map: Map<String, String>): Observable<EarlyWarningBean> {
        val gson = Gson()
        val json = gson.toJson(map)
        Log.d("=========查询信息======", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.warningNoticeList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun warningNoticeDetail(id: String): Observable<ProcessedBean> {
        return apiService.warningNoticeDetail(id)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun handleMessage(noticeId: String, handleNotes: String): Observable<ProcessedBean> {
        val json = "{\"noticeId\":$noticeId,\"handleNotes\":\"$handleNotes\"}"
        Log.d("===", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.handleMessage(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun warningNoticeCount(subId: String): Observable<String> {
        return apiService.warningNoticeCount(subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun indexDetailsList(mutableMap: Map<String, String>): Observable<QueryTwoBean> {
        val gson = Gson()
        val json = gson.toJson(mutableMap)
        Log.d("================", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.indexDetailsList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun useValidation(
        cardNum: String,
        subId: String,
        doorId: String
    ): Observable<UserValidationBean> {
        var json = "{\"cardNum\":\"${cardNum}\",\"subId\":$subId,\"doorId\":$doorId}"
        Log.d("================", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.useValidation(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun aioScanLogin(
        subId: String,
        doorId: String,
        code: String
    ): Observable<UserValidationBean> {
//        var json = "{\"cardNum\":\"${cardNum}\",\"subId\":$subId,\"doorId\":$doorId}"
        var json = "{\"subId\":$subId,\"doorId\":$doorId,\"code\":\"$code\"}"
        Log.d("=====扫码参数", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.aioScanLogin(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun lockOperate(mutableMap: Map<String, Any>): Observable<Boolean> {
        val gson = Gson()
        val json = gson.toJson(mutableMap)
        Log.d("=====开锁参数:", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.lockOperate(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map true
            }
    }

    override fun outTimeWarn(map: Map<String, Any>): Observable<Boolean> {
        val gson = Gson()
        val json = gson.toJson(map)
        Log.d("=====预警上报:", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.outTimeWarn(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map true
            }
    }

    override fun getLocks(subId: String, lockNum: String): Observable<Boolean> {
        return apiService.getLocks(subId, lockNum)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun basicConfig(subId: String): Observable<ConfigBean> {
        return apiService.basicConfig(subId)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun useRecords(map: Map<String, Any>): Observable<UseLedBean> {
        val gson = Gson()
        val json = gson.toJson(map)
        Log.d("=======使用台账", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.useRecords(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun stockLedger(map: Map<String, Any>): Observable<InventoryListBean> {
        val gson = Gson()
        val json = gson.toJson(map)
        Log.d("=======库存台账", json)
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.stockLedger(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun useReturnList(subId: String): Observable<UseLedBean> {
        var json = "{\"subId\":$subId,\"backStatusCollect\":[1,2]}"
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.useReturnList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun tagDetailsList(map: Map<String, Any>): Observable<QueryTwoBean> {
        val gson = Gson()
        val json = gson.toJson(map)
        Log.d("===标签查询====", "$json")
        val requestBody = RequestBody.create("application/json".toMediaTypeOrNull(), json)
        return apiService.tagDetailsList(requestBody)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getDetailsById(id: String): Observable<ReturnDetailsBean> {
        return apiService.getDetailsById(id)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getMsdsDetails(searchValue: String?): Observable<MsdsBean> {
        return apiService.getMsdsDetails(searchValue)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun msdsDetails(id: String): Observable<MsdsListBean> {
        return apiService.msdsDetails(id)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun controlConfigs(chemicalLevel: Int): Observable<ChemicalConfsBean> {
        return apiService.controlConfigs(chemicalLevel)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun getControlConfigs(chemicalLevel: Int): Observable<HxpControlConfig> {
        return apiService.getControlConfigs(chemicalLevel)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }

    override fun checkRfids(rfid: String): Observable<Boolean> {
        return apiService.checkRfids(rfid)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }


    /**
     * 上传APK更新状态
     *
     * @param state 0:升级失败; 1:升级成功; 2:升级中
     */
    override fun onepcApkUpdate(id: String, state: String): Observable<Boolean> {
        return apiService.onepcApkUpdate(id, state)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map true
            }
    }


    /**
     * 退出登录
     */
    override fun loginOut(): Observable<Boolean> {
        return apiService.loginOut()
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }

                return@map true
            }
    }

    override fun monitor(deviceNo: String): Observable<Boolean> {
        return apiService.monitor(deviceNo)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map true
            }
    }

    /**
     * 人脸比对
     */
    override fun faceCompare(file: File, param: FaceCompareReq): Observable<UserData> {
        val fileBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, fileBody)
        val map = HashMap<String, RequestBody>()
        Log.d("====人脸登录-比对", "人脸登录-比对")
//        map["userId"] = RequestBody.create("text/plain".toMediaTypeOrNull(), param.userId)
        map["userIds"] = RequestBody.create("text/plain".toMediaTypeOrNull(), param.userIds)
//        return apiService.faceNewCompare(HttpConfig.BASE_PATH_FACE, filePart, map)
        return apiService.faceNewCompare(filePart, map)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                HttpClient.token = response.data?.token!!
                return@map response.data!!
            }
    }

    override fun checkUserFaceByPic(
        file: File,
        param: FaceCompareReq
    ): Observable<UserValidationBean> {
        val fileBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, fileBody)
        val map = HashMap<String, RequestBody>()
        Log.d("====人脸登录-登录", "${HttpClient.token}")
        map["subId"] = RequestBody.create("text/plain".toMediaTypeOrNull(), param.subId)
        if (param.doorId != null) {
            map["doorId"] = RequestBody.create("text/plain".toMediaTypeOrNull(), param.doorId)
        }
        if (param.stockId != null) {
            map["stockId"] = RequestBody.create("text/plain".toMediaTypeOrNull(), param.stockId)
        }
        if (param.waitId != null) {
            map["waitId"] = RequestBody.create("text/plain".toMediaTypeOrNull(), param.waitId)
        }
        return apiService.checkUserFaceByPic(filePart, map)
            .map { response ->
                if (!response.isSuccess()) {
                    throw NetException(response.code, response.message)
                }
                return@map response.data!!
            }
    }
}