package cn.flightfeather.thirdappmodule.module.inspection
|
|
import android.arch.lifecycle.MutableLiveData
|
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
|
import cn.flightfeather.thirdappmodule.model.bean.LedgerRecord
|
import cn.flightfeather.thirdappmodule.model.bean.UserMap
|
import cn.flightfeather.thirdappmodule.model.enumreation.SceneType
|
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
|
import cn.flightfeather.thirdappmodule.repository.LedgerRepository
|
import cn.flightfeather.thirdappmodule.repository.UserInfoRepository
|
|
/**
|
* @author riku
|
* Date:2022/4/12
|
*
|
*/
|
class PicEvidenceViewModel : BaseViewModel() {
|
|
private val userInfoRepository = UserInfoRepository.instance
|
private var ledgerRepository = LedgerRepository.instance
|
|
private var tzUserId: String? = null
|
|
// 台账记录
|
val ledgerTypes = MutableLiveData<LinkedHashMap<String, MutableList<LedgerRecord>>>()
|
|
// 用户选择的问题图片
|
val selectedImgUrls = MutableLiveData<MutableList<String>>().apply { value = mutableListOf() }
|
|
/**
|
* 根据飞羽监管的场景id,获取飞羽环境系统中对应的用户id
|
*/
|
private fun getTZUserId(sceneId: String, s: () -> Unit) {
|
userInfoRepository.getTZId(sceneId, object : ResultCallBack<UserMap> {
|
override fun onSuccess(result: UserMap?) {
|
result?.let {
|
this@PicEvidenceViewModel.tzUserId = it.tzUserId
|
s()
|
}
|
}
|
|
override fun onFailure() {
|
ledgerTypes.value = null
|
}
|
})
|
}
|
|
private fun getLedgerRecord(userId: String, sceneType: Int, time: String) {
|
ledgerRepository.getLedgerRecord(userId, sceneType, time, object : ResultCallBack<List<LedgerRecord>> {
|
override fun onSuccess(result: List<LedgerRecord>?) {
|
val map = linkedMapOf<String, MutableList<LedgerRecord>>()
|
result?.forEach {
|
if (!map.containsKey(it.ledgerType) && it.ledgerType != null) {
|
map[it.ledgerType!!] = mutableListOf()
|
}
|
map[it.ledgerType]?.add(it)
|
}
|
ledgerTypes.value = map
|
}
|
|
override fun onFailure() {
|
ledgerTypes.value = null
|
}
|
})
|
}
|
|
/**
|
* 获取用户某月台账记录
|
*/
|
fun searchLedgerRecord(sceneId: String, sceneType: Int, time: String) {
|
val tzSceneType = SceneType.typeMap(sceneType)
|
if (tzUserId == null) {
|
getTZUserId(sceneId) {
|
getLedgerRecord(tzUserId!!, tzSceneType, time)
|
}
|
} else {
|
getLedgerRecord(tzUserId!!, tzSceneType, time)
|
}
|
}
|
|
/**
|
* 选取图片
|
*/
|
fun chosePic(url: String, success: (isAdd: Boolean) -> Unit, fail: (err: String) -> Unit) {
|
if (selectedImgUrls.value?.contains(url) == true) {
|
selectedImgUrls.value?.remove(url)
|
success(false)
|
} else {
|
if (selectedImgUrls.value?.size == PicEvidenceActivity.MAX_PIC) {
|
fail("图片最多选取${PicEvidenceActivity.MAX_PIC}张")
|
} else {
|
selectedImgUrls.value?.add(url)
|
success(true)
|
}
|
}
|
selectedImgUrls.value = selectedImgUrls.value
|
}
|
|
/**
|
* 清空所选图片
|
*/
|
fun clearPic() {
|
selectedImgUrls.value?.clear()
|
selectedImgUrls.value = selectedImgUrls.value
|
}
|
}
|