package com.miya.fastcashier.beans

import android.text.TextUtils
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.miya.fastcashier.dao.DatabaseKeeper
import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.utils.isEmpty
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse
import java.lang.RuntimeException

/**
 * 封装层的
 */
@Entity(tableName = "pay_data")
data class ViewPayOrderData(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,//主键
    val orderNo: String,//订单号
    val tradStatus: String? = null,
    val tradeNo: String? = null,//交易流水号
    val tradPrice: String? = null,//总价格
    val chanelTag: String? = null,//支付方式
    val buyerId: String? = null,//支付者id
    val couponMessage: String? = null,
    val chanelOrderTradeTime: String,//支付时间
    val miyaResponseTime: String? = null,//返回支付时间
    val miyaOrderDesc: String? = null,
    val vipShopTag: String? = null,
    val buyerAccount: String? = null,//支付者账户
    val memberCardNumber: String? = null,//会员卡
    val platformName: String? = null,
    val refundOrderNo: String? = null,//退款订单号
    val oriOrderPrice: String? = null,//订单原价
    val refundPrice: String? = null,//退款价格
    val payQrCode: String? = null,//支付条码
    val userName: String? = null
) {

    companion object {
        fun create(payServiceResponse: PayServiceResponse): ViewPayOrderData {
            if (isEmpty(payServiceResponse.chanelOrderTradeTime)) {
                throw RuntimeException("交易时间不可以为空")
            }
            return ViewPayOrderData(
                orderNo = payServiceResponse.shopTradeNo,
                tradStatus = payServiceResponse.tradStatus,
                tradeNo = payServiceResponse.chanelSerialNumber,
                tradPrice = payServiceResponse.tradPrice,
                chanelTag = payServiceResponse.chanelTag,
                buyerId = payServiceResponse.buyerId,
                couponMessage = payServiceResponse.couponMessage,
                chanelOrderTradeTime = payServiceResponse.chanelOrderTradeTime,
                miyaResponseTime = payServiceResponse.miyaResponseTime,
                miyaOrderDesc = payServiceResponse.miyaOrderDesc,
                vipShopTag = payServiceResponse.vipShopTag,
                buyerAccount = payServiceResponse.buyerAccount,
                memberCardNumber = payServiceResponse.memberCardNumber,
                platformName = payServiceResponse.platformName,
                refundOrderNo = payServiceResponse.refundOrderNo,
                oriOrderPrice = payServiceResponse.oriOrderPrice,
                refundPrice = payServiceResponse.refundPrice,
                payQrCode = payServiceResponse.payQrCode,
                userName = AccountService.getUserName(),
            )

        }

        fun transformPayServiceResponse(orderData: ViewPayOrderData): PayServiceResponse {
            var payServiceResponse = PayServiceResponse()
            payServiceResponse.shopTradeNo = orderData.orderNo
            payServiceResponse.tradStatus = orderData.tradStatus
            payServiceResponse.chanelSerialNumber = orderData.tradeNo
            payServiceResponse.tradPrice = orderData.tradPrice
            payServiceResponse.chanelTag = orderData.chanelTag
            payServiceResponse.buyerId = orderData.buyerId
            payServiceResponse.couponMessage = orderData.couponMessage
            payServiceResponse.chanelOrderTradeTime = orderData.chanelOrderTradeTime
            payServiceResponse.miyaResponseTime = orderData.miyaResponseTime
            payServiceResponse.miyaOrderDesc = orderData.miyaOrderDesc
            payServiceResponse.vipShopTag = orderData.vipShopTag
            payServiceResponse.buyerAccount = orderData.buyerAccount
            payServiceResponse.memberCardNumber = orderData.memberCardNumber
            payServiceResponse.platformName = orderData.platformName
            payServiceResponse.refundOrderNo = orderData.refundOrderNo
            payServiceResponse.oriOrderPrice = orderData.oriOrderPrice
            payServiceResponse.refundPrice = orderData.refundPrice
            payServiceResponse.payQrCode = orderData.payQrCode
            return payServiceResponse
        }

        fun insert(payServiceResponse: PayServiceResponse) {
            if (TextUtils.isEmpty(payServiceResponse.shopTradeNo)) {
                return
            }
            DatabaseKeeper.payDatabase.payDataDao()
                .insertOne(create(payServiceResponse))
        }

        fun update() {
            //TODO
        }
    }
}