SearchOrderViewModel.kt 2.68 KB
Newer Older
1 2
package com.miya.fastcashier.viewmodel

3
import android.text.TextUtils
4
import androidx.lifecycle.MutableLiveData
gaodapeng's avatar
gaodapeng committed
5 6
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
7 8
import com.fastcashier.lib_common.function.pay.MiYaPayPlantformPayWayEnum
import com.fastcashier.lib_common.util.DateUtils
9
import com.fastcashier.lib_common.util.isEmpty
gaodapeng's avatar
gaodapeng committed
10 11 12
import com.miya.fastcashier.dao.DatabaseKeeper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
赵鹏翔's avatar
赵鹏翔 committed
13
import com.miya.fastcashier.dao.ViewPayOrderData as ViewPayOrderData1
14

gaodapeng's avatar
gaodapeng committed
15
class SearchOrderViewModel : ViewModel() {
16

17
    val payDataLiveData: MutableLiveData<Result<List<ViewPayOrderData1>>> = MutableLiveData()
18

gaodapeng's avatar
gaodapeng committed
19
    /**
20
     * 获取所有订单,分页处理
gaodapeng's avatar
gaodapeng committed
21 22 23 24 25 26 27 28 29 30 31 32
     */
    fun getPayData(payType: MiYaPayPlantformPayWayEnum.MiyaPayType, currentSize: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val list =
                    DatabaseKeeper.payDatabase.payDataDao()
                        .queryWithType(payType = payType.code, currentSize)
                payDataLiveData.postValue(Result.success(list))
            } catch (e: Exception) {
                e.printStackTrace()
                payDataLiveData.postValue(Result.failure(e))
            }
33

gaodapeng's avatar
gaodapeng committed
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

    /**
     * 根据时间、用户获取订单,不分页
     */
    fun getPayDataByTimeAndUser(
        payType: MiYaPayPlantformPayWayEnum.MiyaPayType,
        userName: String,
        limitTime: Long
    ) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val list = DatabaseKeeper.payDatabase.payDataDao()
                    .queryAllWithTypeAndName(payType = payType.code, userName)

                if (limitTime == 0L) {
                    payDataLiveData.postValue(Result.success(list))
                    return@launch
                }

                val destList: MutableList<ViewPayOrderData1> = arrayListOf()
                if (!isEmpty(list)) {
                    for (order in list) {
                        val tradeDate: String = order.chanelOrderTradeTime
                        if (!TextUtils.isEmpty(tradeDate) &&
                            DateUtils.stringToLong(
                                tradeDate,
                                DateUtils.DF_YYYY_MM_DDHHMMSS
                            ) > limitTime
                        ) {
                            destList.add(order)
                        }
                    }
                }

                payDataLiveData.postValue(Result.success(destList))
            } catch (e: Exception) {
                e.printStackTrace()
                payDataLiveData.postValue(Result.failure(e))
            }
        }
    }
77
}