riku
2025-07-02 3013b813e5df6977c0be921928f73b1a3adde290
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
    }
}