SearchOrderViewModel.kt 2.76 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 7
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.miya.fastcashier.dao.DatabaseKeeper
8
import com.miya.fastcashier.utils.DateUtils
gaodapeng's avatar
gaodapeng committed
9
import com.miya.fastcashier.utils.MiYaPayPlantformPayWayEnum
10
import com.miya.fastcashier.utils.isEmpty
11
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse
gaodapeng's avatar
gaodapeng committed
12 13 14
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.Exception
15
import com.miya.fastcashier.beans.ViewPayOrderData as ViewPayOrderData1
16

gaodapeng's avatar
gaodapeng committed
17
class SearchOrderViewModel : ViewModel() {
18

19
    val payDataLiveData: MutableLiveData<Result<List<ViewPayOrderData1>>> = MutableLiveData()
20

gaodapeng's avatar
gaodapeng committed
21
    /**
22
     * 获取所有订单,分页处理
gaodapeng's avatar
gaodapeng committed
23 24 25 26 27 28 29 30 31 32 33 34
     */
    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))
            }
35

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

    /**
     * 根据时间、用户获取订单,不分页
     */
    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))
            }
        }
    }
79
}