VidiDin-Android/app/src/main/java/com/gamedog/vididin/manager/ZeroManager.kt

218 lines
7.6 KiB
Kotlin
Raw Normal View History

2025-12-25 08:24:33 +00:00
package com.gamedog.vididin.manager
import com.ama.core.architecture.util.AndroidUtil
import com.ama.core.architecture.util.DeviceUtil
import com.ama.core.architecture.util.MD5Util
import com.ama.core.architecture.util.SpUtil
import com.gamedog.vididin.VidiConst
import com.gamedog.vididin.VidiConst.ZEROBUY_SECRET
import com.gamedog.vididin.beans.ZeroBuyItem
import com.gamedog.vididin.beans.ZeroBuyResp
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_FAIL
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_ONGOING
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_SUCCESS
import com.gamedog.vididin.manager.WithdrawManager.Companion.TRANSACTION_STATE_UNSTART
import com.gamedog.vididin.netbase.NetworkUtil
import com.gamedog.vididin.netbase.Result
import com.gamedog.vididin.request.RequestUtil
import com.vididin.real.money.game.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlin.Int
class ZeroManager private constructor() {
companion object {
@Volatile
private var instance: ZeroManager? = null
fun instance(): ZeroManager {
return instance ?: synchronized(this) {
instance ?: ZeroManager().also {
instance = it
}
}
}
}
private val backgroundScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val mWinZeroList: MutableList<WinZeroWithdrawInfoItem> by lazy {
SpUtil.instance().getList<WinZeroWithdrawInfoItem>(SpUtil.KEY_ZEROBUY_WIN_ITEMS).toMutableList()
}
private fun saveWinWithdrawInfos() {
SpUtil.instance().putList(SpUtil.KEY_ZEROBUY_WIN_ITEMS, mWinZeroList)
}
fun getWinItemList() : List<WinZeroWithdrawInfoItem> {
return mWinZeroList
}
fun addOrUpdateWinItem() {
}
/**
* only add new one, if already exist - then do nothing
*/
fun addWinWithdrawItem(zeroItem: ZeroBuyItem) {
val userId = AccountManager.getAccount().userId
zeroItem.winners?.let {
if (it.contains(userId) && zeroItem.completed) {
var alreadyExist = false
mWinZeroList.forEach {
if (it.purchase_id == zeroItem.id) {
alreadyExist = true
return@forEach
}
}
if (!alreadyExist) {
mWinZeroList.add(WinZeroWithdrawInfoItem(zeroItem.id, zeroItem.title, zeroItem.price))
saveWinWithdrawInfos()
}
}
}
}
fun couldStartWithdraw(zeroItem: ZeroBuyItem): Boolean {
val userId = AccountManager.getAccount().userId
zeroItem.winners?.let {
if (it.contains(userId) && zeroItem.completed) {
mWinZeroList.forEach {
if (it.purchase_id == zeroItem.id) {
return it.withdrawState == TRANSACTION_STATE_UNSTART || it.withdrawState == TRANSACTION_STATE_FAIL
}
}
addWinWithdrawItem(zeroItem)
return true
}
}
return false
}
fun startWithdrawForWinPurchase(zeroItem: ZeroBuyItem) {
if (couldStartWithdraw(zeroItem)) {
/*when (it.withdrawState) {
TRANSACTION_STATE_UNSTART -> {
}
TRANSACTION_STATE_ONGOING -> {
}
TRANSACTION_STATE_SUCCESS -> {
}
TRANSACTION_STATE_FAIL -> {
}
}*/
}
}
private fun requestWithdrawZeroReward(zeroWithdrawItem: WinZeroWithdrawInfoItem) {
backgroundScope.launch {
// header
val operationVal = VidiConst.ZERO_WITHDRAW_OPERATION
val curTimeSec = System.currentTimeMillis()/1000
val signStr = RequestUtil.getZeroBuyRequestSign(curTimeSec, operationVal)
val requestHeaders = mapOf("Operation" to operationVal.toString(), "Timestamp" to curTimeSec.toString(), "Sign" to signStr)
// body param
val requestParams: MutableMap<String, String> = mutableMapOf("AppId" to AndroidUtil.getPackageId(), "DeviceId" to DeviceUtil.generateDeviceId())
val userId = AccountManager.getAccount().userId
if (userId > 0) {
requestParams.put("UserId", userId.toString())
}
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
requestParams.put("ActivityId", zeroWithdrawItem.purchase_id.toString())
// withdraw 相关参数:
val bankCPFAccount = AccountManager.getAccount().bankInfo?.bankAccount!!
val accountType = "CPF"
requestParams.put("Account", bankCPFAccount)
requestParams.put("AccountType", accountType)
requestParams.put("Country", Country.BR.name)
requestParams.put("DocumentType", accountType)
requestParams.put("DocumentId", bankCPFAccount)
requestParams.put("BankCode", AccountManager.getAccount().bankInfo?.bankAccount!!)
val signOriginStr = "${zeroWithdrawItem.purchase_id}-${userId}-${bankCPFAccount}-${accountType}-${bankCPFAccount}-${accountType}-${ZEROBUY_SECRET}"
requestParams.put("Sign", MD5Util.md5(signOriginStr)!!)
val result = NetworkUtil.post("${VidiConst.URL_ZERO_BUY}/any", requestHeaders, requestParams, joinZeroBuyItemIds)
when (result) {
is Result.Success -> {
val respObj = AndroidUtil.json2Object<ZeroBuyResp>(result.data.string())
respObj?.contentObj?.let {
if (respObj.code == 0 && respObj.Content.isNotEmpty()) {
zeroWithdrawItem.orderId = respObj.Content
zeroWithdrawItem.withdrawState = TRANSACTION_STATE_ONGOING
saveWinWithdrawInfos()
return@launch
} else {
zeroWithdrawItem.withdrawState = TRANSACTION_STATE_FAIL
zeroWithdrawItem.failReason = respObj.code
saveWinWithdrawInfos()
}
}
}
is Result.Error -> {
AndroidUtil.showToast(R.string.net_error)
}
is Result.Loading -> {
}
}
}
}
}
data class WinZeroWithdrawInfoItem(
val purchase_id: Int = 0,
val purchase_title: String? = "",
val winCashNumStr: String? = "",
var operateMs: Long = System.currentTimeMillis(),
var withdrawState: Int = TRANSACTION_STATE_UNSTART,
var failReason: Int = 0,
var hasShowResultDialog: Boolean = false,
var orderId: String = "",
)
enum class Country(val code: Int) {
ANY(0), // 任意国家
BR(1), // 巴西
TR(2), // 土耳其
PK(3) // 巴基斯坦
}
enum class Currency(val code: Int) {
ANY(0), // 任意国家货币
BRL(1), // 巴西雷亚尔
TRY(2), // 土耳其里拉
PKR(3) // 巴基斯坦卢比
}
enum class Language(val code: Int) {
ANY(0), // 任意国家语言
PT(1), // 葡萄牙语(巴西,葡萄牙)
TR(2), // 土耳其语(土耳其)
UR(3) // 乌尔都语(巴基斯坦)
}