Newer
Older
CasicSmartTube / app / src / main / java / com / casic / smarttube / vm / WellViewModel.kt
package com.casic.smarttube.vm

import androidx.lifecycle.MutableLiveData
import com.casic.smarttube.base.BaseApplication
import com.casic.smarttube.extensions.separateResponseCode
import com.casic.smarttube.extensions.toErrorMessage
import com.casic.smarttube.model.WellDetailModel
import com.casic.smarttube.utils.retrofit.RetrofitServiceManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.extensions.launch
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.vm.BaseViewModel
import com.pengxh.kt.lite.vm.LoadState

class WellViewModel : BaseViewModel() {

    private val gson = Gson()
    val detailModel = MutableLiveData<WellDetailModel>()
//    val allWellModel = MutableLiveData<WellListModel>()
//    val countResultModel = MutableLiveData<HashMap<String, String>>()
//    val wellTypeAndCountModel = MutableLiveData<WellTypeAndCountModel>()
//    val wellListPageModel = MutableLiveData<WellListPageModel>()
//    val wellTypeModel = MutableLiveData<WellTypeModel>()
//    val deviceDataModel = MutableLiveData<DeviceDataModel>()
//    val commonResultModel = MutableLiveData<CommonResultModel>()

    fun obtainWellDetail(id: String) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.obtainWellDetail(id)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            detailModel.value = gson.fromJson<WellDetailModel>(
                response, object : TypeToken<WellDetailModel>() {}.type
            )
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

//    fun obtainAllWell() = launch({
//        val response = RetrofitServiceManager.obtainAllWell()
//        val responseCode = response.separateResponseCode()
//        if (responseCode == 200) {
//            allWellModel.value = gson.fromJson<WellListModel>(
//                response, object : TypeToken<WellListModel>() {}.type
//            )
//        } else {
//            response.toErrorMessage().show()
//        }
//    }, {
//        it.printStackTrace()
//    })
//
//    fun countWellByState() = launch({
//        val response = RetrofitServiceManager.countWellByState()
//        val responseCode = response.separateResponseCode()
//        if (responseCode == 200) {
//            val map = HashMap<String, String>(3)
//
//            val jsonObject = JSONObject(response)
//            val data = jsonObject.getJSONObject("data")
//            map["bfWell"] = data.getString("bfWell")
//            map["cfWell"] = data.getString("cfWell")
//            map["total"] = data.getString("total")
//            countResultModel.value = map
//        } else {
//            response.toErrorMessage().show()
//        }
//    }, {
//        it.printStackTrace()
//    })
//
//    fun obtainWellTypeAndCount() = launch({
//        val response = RetrofitServiceManager.obtainWellTypeAndCount()
//        val responseCode = response.separateResponseCode()
//        if (responseCode == 200) {
//            wellTypeAndCountModel.value = gson.fromJson<WellTypeAndCountModel>(
//                response, object : TypeToken<WellTypeAndCountModel>() {}.type
//            )
//        } else {
//            response.toErrorMessage().show()
//        }
//    }, {
//        it.printStackTrace()
//    })
//
//    fun obtainWellPageList(keywords: String?, wellType: String?, deptid: String?, offset: Int) =
//        launch({
//            val response =
//                RetrofitServiceManager.obtainWellPageList(keywords, wellType, deptid, offset)
//            val responseCode = response.separateResponseCode()
//            if (responseCode == 200) {
//                wellListPageModel.value = gson.fromJson<WellListPageModel>(
//                    response, object : TypeToken<WellListPageModel>() {}.type
//                )
//            } else {
//                response.toErrorMessage().show()
//            }
//        }, {
//            it.printStackTrace()
//        })
//
//    fun obtainWellType() = launch({
//        val response = RetrofitServiceManager.obtainWellType()
//        val responseCode = response.separateResponseCode()
//        if (responseCode == 200) {
//            wellTypeModel.value = gson.fromJson<WellTypeModel>(
//                response, object : TypeToken<WellTypeModel>() {}.type
//            )
//        } else {
//            response.toErrorMessage().show()
//        }
//    }, {
//        it.printStackTrace()
//    })
//
//    fun obtainDeviceDataByWell(id: String) = launch({
//        val response = RetrofitServiceManager.obtainDeviceDataByWell(id)
//        val responseCode = response.separateResponseCode()
//        if (responseCode == 200) {
//            deviceDataModel.value = gson.fromJson<DeviceDataModel>(
//                response, object : TypeToken<DeviceDataModel>() {}.type
//            )
//        } else {
//            response.toErrorMessage().show()
//        }
//    }, {
//        it.printStackTrace()
//    })
//
//    fun obtainOperationResult(id: String, state: String) = launch({
//        loadState.value = LoadState.Loading
//        val response = RetrofitServiceManager.obtainOperationResult(id, state)
//        val responseCode = response.separateResponseCode()
//        if (responseCode == 200) {
//            loadState.value = LoadState.Success
//            "操作成功".show()
//            commonResultModel.value = gson.fromJson<CommonResultModel>(
//                response, object : TypeToken<CommonResultModel>() {}.type
//            )
//        } else {
//            loadState.value = LoadState.Fail
//            response.toErrorMessage().show()
//        }
//    }, {
//        loadState.value = LoadState.Fail
//        it.printStackTrace()
//    })
//
//    /**
//     * 编辑更新井信息
//     * */
//    fun updateWellDetail(
//        id: String,
//        wellCode: String,
//        wellName: String,
//        wellType: String,
//        deptid: String,
//        deep: String,
//        position: String,
//        coordinateX: String,
//        coordinateY: String,
//        photos: String,
//        notes: String?,
//        qu: String,
//        area: String,
//        responsibleDept: String,
//        responsibleDeptName: String
//    ) = launch({
//        loadState.value = LoadState.Loading
//        val response = RetrofitServiceManager.updateWellDetail(
//            id,
//            wellCode,
//            wellName,
//            wellType,
//            deptid,
//            deep,
//            position,
//            coordinateX,
//            coordinateY,
//            photos,
//            notes,
//            qu,
//            area,
//            responsibleDept,
//            responsibleDeptName
//        )
//        val responseCode = response.separateResponseCode()
//        if (responseCode == 200) {
//            loadState.value = LoadState.Success
//            "更新成功".show()
//            commonResultModel.value = gson.fromJson<CommonResultModel>(
//                response, object : TypeToken<CommonResultModel>() {}.type
//            )
//        } else {
//            loadState.value = LoadState.Fail
//            response.toErrorMessage().show()
//        }
//    }, {
//        loadState.value = LoadState.Fail
//        it.printStackTrace()
//    })
}