From 6d242ae973f968a9fa58b654cd48b2e54f57ca2b Mon Sep 17 00:00:00 2001 From: cano Date: Sun, 10 Mar 2024 00:44:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=B4=A6=E5=8F=98=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E9=92=B1=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/Order/CustomerUserPostOrderBean.php | 201 +++++ .../Api/Wallet/CustomerWalletBalanceBean.php | 121 +++ .../CustomerWalletBalanceTransactionBean.php | 212 ++++++ app/Bean/Model/Base/BaseBean.php | 13 + app/Bean/Model/Base/TmplBean.php | 9 + .../CustomerUserPostOrderDisputeModel.php | 32 + .../Api/Order/CustomerUserPostOrderModel.php | 53 ++ .../CustomerPostOrderWalletCheckbookModel.php | 27 + .../Api/Wallet/CustomerWalletBalanceModel.php | 130 ++++ .../CustomerWalletBalanceTransactionModel.php | 712 ++++++++++++++++++ .../Wallet/CustomerWalletCurrencyModel.php | 29 + .../WalletPlatformBalanceTransactionModel.php | 30 +- .../WalletPlatformUserTransactionModel.php | 15 +- app/Models/Wallet/Wallet/WalletAddrModel.php | 9 +- .../Wallet/Wallet/WalletCurrencyModel.php | 6 + app/Tools/Logs.php | 25 + app/Tools/Times.php | 9 +- 17 files changed, 1617 insertions(+), 16 deletions(-) create mode 100644 app/Bean/Model/Api/Order/CustomerUserPostOrderBean.php create mode 100644 app/Bean/Model/Api/Wallet/CustomerWalletBalanceBean.php create mode 100644 app/Bean/Model/Api/Wallet/CustomerWalletBalanceTransactionBean.php create mode 100644 app/Bean/Model/Base/BaseBean.php create mode 100644 app/Bean/Model/Base/TmplBean.php create mode 100644 app/Models/Api/Order/CustomerUserPostOrderDisputeModel.php create mode 100644 app/Models/Api/Order/CustomerUserPostOrderModel.php create mode 100644 app/Models/Api/Wallet/CustomerPostOrderWalletCheckbookModel.php create mode 100644 app/Models/Api/Wallet/CustomerWalletBalanceModel.php create mode 100644 app/Models/Api/Wallet/CustomerWalletBalanceTransactionModel.php create mode 100644 app/Models/Api/Wallet/CustomerWalletCurrencyModel.php create mode 100644 app/Tools/Logs.php diff --git a/app/Bean/Model/Api/Order/CustomerUserPostOrderBean.php b/app/Bean/Model/Api/Order/CustomerUserPostOrderBean.php new file mode 100644 index 0000000..cb98c78 --- /dev/null +++ b/app/Bean/Model/Api/Order/CustomerUserPostOrderBean.php @@ -0,0 +1,201 @@ +buyer_currency_code; + } + + public function setBuyerCurrencyCode($buyer_currency_code): void + { + $this->buyer_currency_code = $buyer_currency_code; + } + + public function getSellerCurrencyCode() + { + return $this->seller_currency_code; + } + + public function setSellerCurrencyCode($seller_currency_code): void + { + $this->seller_currency_code = $seller_currency_code; + } + + + public function getId() + { + return $this->id; + } + + public function setId($id): void + { + $this->id = $id; + } + + public function getSn() + { + return $this->sn; + } + + public function setSn($sn): void + { + $this->sn = $sn; + } + + public function getPayStatus() + { + return $this->pay_status; + } + + public function setPayStatus($pay_status): void + { + $this->pay_status = $pay_status; + } + + public function getBuyerUid() + { + return $this->buyer_uid; + } + + public function setBuyerUid($buyer_uid): void + { + $this->buyer_uid = $buyer_uid; + } + + public function getSellerUid() + { + return $this->seller_uid; + } + + public function setSellerUid($seller_uid): void + { + $this->seller_uid = $seller_uid; + } + + public function getPid() + { + return $this->pid; + } + + public function setPid($pid): void + { + $this->pid = $pid; + } + + public function getPostHistoryId() + { + return $this->post_history_id; + } + + public function setPostHistoryId($post_history_id): void + { + $this->post_history_id = $post_history_id; + } + + public function getAmount() + { + return $this->amount; + } + + public function setAmount($amount): void + { + $this->amount = $amount; + } + + public function getBuyerWalletTransactionId() + { + return $this->buyer_wallet_transaction_id; + } + + public function setBuyerWalletTransactionId($buyer_wallet_transaction_id): void + { + $this->buyer_wallet_transaction_id = $buyer_wallet_transaction_id; + } + + public function getSellerWalletTransactionId() + { + return $this->seller_wallet_transaction_id; + } + + public function setSellerWalletTransactionId($seller_wallet_transaction_id): void + { + $this->seller_wallet_transaction_id = $seller_wallet_transaction_id; + } + + public function getWalletCheckbookId() + { + return $this->wallet_checkbook_id; + } + + public function setWalletCheckbookId($wallet_checkbook_id): void + { + $this->wallet_checkbook_id = $wallet_checkbook_id; + } + + public function getDisputeStatus() + { + return $this->dispute_status; + } + + public function setDisputeStatus($dispute_status): void + { + $this->dispute_status = $dispute_status; + } + + public function getDisputeResultStatus() + { + return $this->dispute_result_status; + } + + public function setDisputeResultStatus($dispute_result_status): void + { + $this->dispute_result_status = $dispute_result_status; + } + + public function getCreatedAt() + { + return $this->created_at; + } + + public function setCreatedAt($created_at): void + { + $this->created_at = $created_at; + } + + public function getUpdatedAt() + { + return $this->updated_at; + } + + public function setUpdatedAt($updated_at): void + { + $this->updated_at = $updated_at; + } + + + + +} diff --git a/app/Bean/Model/Api/Wallet/CustomerWalletBalanceBean.php b/app/Bean/Model/Api/Wallet/CustomerWalletBalanceBean.php new file mode 100644 index 0000000..4f262f0 --- /dev/null +++ b/app/Bean/Model/Api/Wallet/CustomerWalletBalanceBean.php @@ -0,0 +1,121 @@ +id; + } + + public function setId($id): void + { + $this->id = $id; + } + + public function getUid() + { + return $this->uid; + } + + public function setUid($uid): void + { + $this->uid = $uid; + } + + public function getCurrencyCode() + { + return $this->currency_code; + } + + public function setCurrencyCode($currency_code): void + { + $this->currency_code = $currency_code; + } + + public function getTotalAmount() + { + return $this->total_amount; + } + + public function setTotalAmount($total_amount): void + { + $this->total_amount = $total_amount; + } + + public function getFrozenAmount() + { + return $this->frozen_amount; + } + + public function setFrozenAmount($frozen_amount): void + { + $this->frozen_amount = $frozen_amount; + } + + public function getAvailableAmount() + { + return $this->available_amount; + } + + public function setAvailableAmount($available_amount): void + { + $this->available_amount = $available_amount; + } + + public function getSecurityAmount() + { + return $this->security_amount; + } + + public function setSecurityAmount($security_amount): void + { + $this->security_amount = $security_amount; + } + + public function getRechargeWalletAddr() + { + return $this->recharge_wallet_addr; + } + + public function setRechargeWalletAddr($recharge_wallet_addr): void + { + $this->recharge_wallet_addr = $recharge_wallet_addr; + } + + public function getCreatedAt() + { + return $this->created_at; + } + + public function setCreatedAt($created_at): void + { + $this->created_at = $created_at; + } + + public function getUpdatedAt() + { + return $this->updated_at; + } + + public function setUpdatedAt($updated_at): void + { + $this->updated_at = $updated_at; + } + + +} diff --git a/app/Bean/Model/Api/Wallet/CustomerWalletBalanceTransactionBean.php b/app/Bean/Model/Api/Wallet/CustomerWalletBalanceTransactionBean.php new file mode 100644 index 0000000..25221fa --- /dev/null +++ b/app/Bean/Model/Api/Wallet/CustomerWalletBalanceTransactionBean.php @@ -0,0 +1,212 @@ +id; + } + + public function setId($id): void + { + $this->id = $id; + } + + public function getType() + { + return $this->type; + } + + public function setType($type): void + { + $this->type = $type; + } + + public function getStatus(): int + { + return $this->status; + } + + public function setStatus(int $status): void + { + $this->status = $status; + } + + public function getWalletId() + { + return $this->wallet_id; + } + + public function setWalletId($wallet_id): void + { + $this->wallet_id = $wallet_id; + } + + public function getUid() + { + return $this->uid; + } + + public function setUid($uid): void + { + $this->uid = $uid; + } + + public function getCurrencyCode(): string + { + return $this->currency_code; + } + + public function setCurrencyCode(string $currency_code): void + { + $this->currency_code = $currency_code; + } + + public function getAmount(): int + { + return $this->amount; + } + + public function setAmount(int $amount): void + { + $this->amount = $amount; + } + + public function getBeforeTotalAmount() + { + return $this->before_total_amount; + } + + public function setBeforeTotalAmount($before_total_amount): void + { + $this->before_total_amount = $before_total_amount; + } + + public function getAfterTotalAmount() + { + return $this->after_total_amount; + } + + public function setAfterTotalAmount($after_total_amount): void + { + $this->after_total_amount = $after_total_amount; + } + + public function getSourceParams() + { + return $this->source_params; + } + + public function setSourceParams($source_params): void + { + $this->source_params = $source_params; + } + + public function getRemark() + { + return $this->remark; + } + + public function setRemark($remark): void + { + $this->remark = $remark; + } + + public function getTargetUid() + { + return $this->target_uid; + } + + public function setTargetUid($target_uid): void + { + $this->target_uid = $target_uid; + } + + public function getTargetPostOrderId() + { + return $this->target_post_order_id; + } + + public function setTargetPostOrderId($target_post_order_id): void + { + $this->target_post_order_id = $target_post_order_id; + } + + public function getSign() + { + return $this->sign; + } + + public function setSign($sign): void + { + $this->sign = $sign; + } + + public function getCallbackTime() + { + return $this->callback_time; + } + + public function setCallbackTime($callback_time): void + { + $this->callback_time = $callback_time; + } + + public function getDelayPaymentTime() + { + return $this->delay_payment_time; + } + + public function setDelayPaymentTime($delay_payment_time): void + { + $this->delay_payment_time = $delay_payment_time; + } + + public function getCreatedAt() + { + return $this->created_at; + } + + public function setCreatedAt($created_at): void + { + $this->created_at = $created_at; + } + + public function getUpdatedAt() + { + return $this->updated_at; + } + + public function setUpdatedAt($updated_at): void + { + $this->updated_at = $updated_at; + } + + + + +} diff --git a/app/Bean/Model/Base/BaseBean.php b/app/Bean/Model/Base/BaseBean.php new file mode 100644 index 0000000..684e309 --- /dev/null +++ b/app/Bean/Model/Base/BaseBean.php @@ -0,0 +1,13 @@ +toArray(self::FILTER_NOT_NULL); + } + +} diff --git a/app/Bean/Model/Base/TmplBean.php b/app/Bean/Model/Base/TmplBean.php new file mode 100644 index 0000000..714f75b --- /dev/null +++ b/app/Bean/Model/Base/TmplBean.php @@ -0,0 +1,9 @@ + '未申诉', + self::DISPUTE_STATUS_WAITING => '申诉待处理', + self::DISPUTE_STATUS_PROCESSING => '申诉处理中', + self::DISPUTE_STATUS_FINISH => '申诉完成', + ]; + + const DISPUTE_RESULT_STATUS_NO_MISTAKE = 1; + const DISPUTE_RESULT_STATUS_HAVE_MISTAKE = 2; + const DISPUTE_RESULT_STATUS = [ + self::DISPUTE_RESULT_STATUS_NO_MISTAKE => '无过错', + self::DISPUTE_RESULT_STATUS_HAVE_MISTAKE => '有过错', + ]; + + +} diff --git a/app/Models/Api/Wallet/CustomerPostOrderWalletCheckbookModel.php b/app/Models/Api/Wallet/CustomerPostOrderWalletCheckbookModel.php new file mode 100644 index 0000000..72d3e9a --- /dev/null +++ b/app/Models/Api/Wallet/CustomerPostOrderWalletCheckbookModel.php @@ -0,0 +1,27 @@ + $id, + 'frozen_amount' => DB::raw('frozen_amount + '. $amount), + 'available_amount' => DB::raw('available_amount - '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function unFrozenAmount($id,$amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'frozen_amount' => DB::raw('frozen_amount - '. $amount), + 'available_amount' => DB::raw('available_amount + '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function subFrozenAmount($id, $amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'total_amount' => DB::raw('total_amount - '. $amount), + 'frozen_amount' => DB::raw('frozen_amount - '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function addFrozenAmount($id, $amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'total_amount' => DB::raw('total_amount + '. $amount), + 'frozen_amount' => DB::raw('frozen_amount + '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function subAvailableAmount($id, $amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'total_amount' => DB::raw('total_amount - '. $amount), + 'available_amount' => DB::raw('available_amount - '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function addAvailableAmount($id, $amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'total_amount' => DB::raw('total_amount + '. $amount), + 'available_amount' => DB::raw('available_amount + '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function balanceToSecurity($id, $amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'total_amount' => DB::raw('total_amount - '. $amount), + 'available_amount' => DB::raw('available_amount - '. $amount), + 'security_amount' => DB::raw('security_amount + '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function securityToBalance($id, $amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'total_amount' => DB::raw('total_amount + '. $amount), + 'available_amount' => DB::raw('available_amount + '. $amount), + 'security_amount' => DB::raw('security_amount - '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function subSecurityAmount($id, $amount): bool|int + { + $amount = abs($amount); + $updateItem = [ + 'id' => $id, + 'security_amount' => DB::raw('security_amount - '. $amount), + ]; + return $this->updateItem($updateItem); + } + + function findByUidCurrencyCode($uid, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null + { + return $this->findItemByWhere(['uid' => $uid, 'currency_code' => $currency_code]); + } + +} diff --git a/app/Models/Api/Wallet/CustomerWalletBalanceTransactionModel.php b/app/Models/Api/Wallet/CustomerWalletBalanceTransactionModel.php new file mode 100644 index 0000000..e6127f5 --- /dev/null +++ b/app/Models/Api/Wallet/CustomerWalletBalanceTransactionModel.php @@ -0,0 +1,712 @@ + '充值', + self::TYPE_WITHDRAW_DEC => '提现', + self::TYPE_ORDER_PAY_DEC => '订单支付', + self::TYPE_ORDER_RECEIVE_ADD => '订单收款', + self::TYPE_ORDER_REFUND_SUB => '订单退款', + self::TYPE_BALANCE_TO_SECURITY => '余额转保证金', + self::TYPE_SECURITY_TO_BALANCE => '保证金转余额', + self::TYPE_DISPUTE_ADD => '纠纷加款', + self::TYPE_DISPUTE_SUB => '纠纷扣款', + self::TYPE_TRANSACTION_FEE_SUB => '交易手续费扣除', + self::TYPE_TRANSACTION_FEE_ADD => '交易手续费增加', + self::TYPE_ADMIN_ADD => '管理员加款', + self::TYPE_ADMIN_SUB => '管理员扣款', + self::TYPE_TRANSFER_TO_USER_DEC => '转账给用户', + self::TYPE_TRANSFER_RECEIVE_ADD => '转账收款', + ]; + + const STATUS_WAIT = 1; + const STATUS_PROCESSING = 2; + const STATUS_SUCCESS = 3; + const STATUS_FAIL = 4; + const STATUS_DELAY_PAYMENT = 5; + const STATUS = [ + self::STATUS_WAIT => '等待处理', + self::STATUS_PROCESSING => '处理中', + self::STATUS_SUCCESS => '成功', + self::STATUS_FAIL => '失败', + self::STATUS_DELAY_PAYMENT => '延期到账', + ]; + + const SOURCE_PARAMS_BALANCE = 1; + const SOURCE_PARAMS_SECURITY = 2; + const SOURCE_PARAMS = [ + self::SOURCE_PARAMS_BALANCE => '余额', + self::SOURCE_PARAMS_SECURITY => '保证金', + ]; + + //转账给用户 + function typeTransferToUser($uid, $target_uid, $currency_code, $amount, $remark = null): bool + { + $senderWalletTransactionBean = new CustomerWalletBalanceTransactionBean(); + $receiverWalletTransactionBean = new CustomerWalletBalanceTransactionBean(); + + $senderWalletTransactionBean->setUid($uid); + $senderWalletTransactionBean->setTargetUid($target_uid); + $senderWalletTransactionBean->setCurrencyCode($currency_code); + $senderWalletTransactionBean->setAmount(-abs($amount)); //账变负数 + $senderWalletTransactionBean->setType(self::TYPE_TRANSFER_TO_USER_DEC); + $senderWalletTransactionBean->setStatus(self::STATUS_SUCCESS); + $senderWalletTransactionBean->setRemark($remark); + + $receiverWalletTransactionBean->setUid($target_uid); + $receiverWalletTransactionBean->setTargetUid($uid); + $receiverWalletTransactionBean->setCurrencyCode($currency_code); + $receiverWalletTransactionBean->setAmount(abs($amount)); //账变正数 + $receiverWalletTransactionBean->setType(self::TYPE_TRANSFER_RECEIVE_ADD); + $receiverWalletTransactionBean->setStatus(self::STATUS_SUCCESS); + $receiverWalletTransactionBean->setRemark($remark); + + try { + DB::beginTransaction(); + //查询发送人余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resSenderWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($senderWalletTransactionBean->getUid(), $senderWalletTransactionBean->getCurrencyCode()); + if (!$resSenderWalletBalanceModel) throw new ModelException('sender wallet not found'); + //查询接收人余额 + $resReceiverWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($receiverWalletTransactionBean->getUid(), $receiverWalletTransactionBean->getCurrencyCode()); + if (!$resReceiverWalletBalanceModel) throw new ModelException('receiver wallet not found'); + + //检查发送人余额是否足够 + if (Math::bcComp($resSenderWalletBalanceModel->available_amount, abs($senderWalletTransactionBean->getAmount())) != -1) throw new ModelException('balance not enough'); + $senderWalletTransactionBean->setWalletId($resSenderWalletBalanceModel->id); + $senderWalletTransactionBean->setBeforeTotalAmount($resSenderWalletBalanceModel->total_amount); + $senderWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resSenderWalletBalanceModel->total_amount, $senderWalletTransactionBean->getAmount())); + + //发送人新增账变 + $res = $this->addTransaction($senderWalletTransactionBean); + if (!$res) throw new ModelException('addTransaction fail'); + //发送人变更余额 + $res = $oCustomerWalletBalanceModel->subAvailableAmount($senderWalletTransactionBean->getWalletId(), $senderWalletTransactionBean->getAmount()); + if (!$res) throw new ModelException('transfer to user dec fail'); + + //接收人账变 + $receiverWalletTransactionBean->setWalletId($resReceiverWalletBalanceModel->id); + $receiverWalletTransactionBean->setBeforeTotalAmount($resReceiverWalletBalanceModel->total_amount); + $receiverWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resReceiverWalletBalanceModel->total_amount, $receiverWalletTransactionBean->getAmount())); + //接收人新增账变 + $res = $this->addTransaction($receiverWalletTransactionBean); + if (!$res) throw new ModelException('addTransaction fail'); + //接收人变更余额 + $res = $oCustomerWalletBalanceModel->addAvailableAmount($receiverWalletTransactionBean->getWalletId(), $receiverWalletTransactionBean->getAmount()); + if (!$res) throw new ModelException('transfer receive add fail'); + + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + + //管理员扣除 + function typeAdminSub($uid, $currency_code, $amount, $remark = null): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(-abs($amount)); //账变负数 + $bean->setType(self::TYPE_ADMIN_SUB); + $bean->setStatus(self::STATUS_SUCCESS); + $bean->setRemark($remark); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) throw new ModelException('balance not enough'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + $res = $oCustomerWalletBalanceModel->subAvailableAmount($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('admin sub fail'); + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //管理员增加 + function typeAdminAdd($uid, $currency_code, $amount, $remark = null): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(abs($amount)); //正数 + $bean->setType(self::TYPE_ADMIN_ADD); + $bean->setStatus(self::STATUS_SUCCESS); + $bean->setRemark($remark); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + $res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('admin add fail'); + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //手续费增加 + function typeTransactionFeeAdd($uid, $currency_code, $amount, $remark = null): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(abs($amount)); //正数 + $bean->setType(self::TYPE_TRANSACTION_FEE_ADD); + $bean->setStatus(self::STATUS_SUCCESS); + $bean->setRemark($remark); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + $res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('transaction fee add fail'); + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //手续费扣除 + function typeTransactionFeeSub($uid, $currency_code, $amount, $remark = null): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(-abs($amount)); //账变负数 + $bean->setType(self::TYPE_TRANSACTION_FEE_SUB); + $bean->setStatus(self::STATUS_SUCCESS); + $bean->setRemark($remark); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + //检查余额是否足够 + if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) { //从余额中扣除 + $bean->setSourceParams(self::SOURCE_PARAMS_BALANCE); + $res = $oCustomerWalletBalanceModel->subAvailableAmount($bean->getWalletId(), $bean->getAmount()); + } else if (Math::bcComp($resWalletBalanceModel->security_amount, abs($bean->getAmount())) != -1) { //从保证金中扣除 + $bean->setSourceParams(self::SOURCE_PARAMS_SECURITY); + $res = $oCustomerWalletBalanceModel->subSecurityAmount($bean->getWalletId(), $bean->getAmount()); + } else { + throw new ModelException('balance and security not enough'); + } + if (!$res) throw new ModelException('transaction fee sub fail'); + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //仲裁判罚扣除 + function typeDisputeSub($uid, $currency_code, $amount, $remark = null): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(-abs($amount)); //账变负数 + $bean->setType(self::TYPE_DISPUTE_SUB); + $bean->setStatus(self::STATUS_SUCCESS); + $bean->setRemark($remark); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + //检查余额是否足够 + if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) != -1) { //从余额中扣除 + $bean->setSourceParams(self::SOURCE_PARAMS_BALANCE); + $res = $oCustomerWalletBalanceModel->subAvailableAmount($bean->getWalletId(), $bean->getAmount()); + } else if (Math::bcComp($resWalletBalanceModel->security_amount, abs($bean->getAmount())) != -1) { //从保证金中扣除 + $bean->setSourceParams(self::SOURCE_PARAMS_SECURITY); + $res = $oCustomerWalletBalanceModel->subSecurityAmount($bean->getWalletId(), $bean->getAmount()); + } else { + throw new ModelException('balance and security not enough'); + } + if (!$res) throw new ModelException('dispute sub fail'); + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + + } + + //仲裁判罚增加 + function typeDisputeAdd($uid, $currency_code, $amount, $remark = null): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(abs($amount)); //正数 + $bean->setType(self::TYPE_DISPUTE_ADD); + $bean->setStatus(self::STATUS_SUCCESS); + $bean->setRemark($remark); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('dispute add fail'); + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + + } + + //押金转余额 + function typeSecurityToBalance($uid, $currency_code, $amount): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(abs($amount)); //正数 + $bean->setType(self::TYPE_SECURITY_TO_BALANCE); + $bean->setStatus(self::STATUS_SUCCESS); + + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + + //检查保证金是否足够 + if (Math::bcComp($resWalletBalanceModel->security_amount, abs($bean->getAmount())) == -1) throw new ModelException('security not enough'); + + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + + //变更余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $res = $oCustomerWalletBalanceModel->securityToBalance($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('securityToBalance fail'); + + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + + } + + //余额转押金 + function typeBalanceToSecurity($uid, $currency_code, $amount): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(-abs($amount)); //账变负数 + $bean->setType(self::TYPE_BALANCE_TO_SECURITY); + $bean->setStatus(self::STATUS_SUCCESS); + + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + + //检查余额是否足够 + if (Math::bcComp($resWalletBalanceModel->available_amount, abs($bean->getAmount())) == -1) throw new ModelException('balance not enough'); + + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + + //变更余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $res = $oCustomerWalletBalanceModel->balanceToSecurity($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('balanceToSecurity fail'); + + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //订单支付 + function typeOrderPay(CustomerUserPostOrderBean $orderBean): bool + { + //初始检测 + if ($orderBean->getBuyerCurrencyCode() != $orderBean->getSellerCurrencyCode()) throw new ModelException('currency_code not match'); + if ($orderBean->getBuyerUid() == $orderBean->getSellerUid()) throw new ModelException('buyer_uid can not match seller_uid'); + if ($orderBean->getAmount() <= 0) throw new ModelException('amount error'); + $orderBean->setAmount(abs($orderBean->getAmount())); //限制正数 + + $buyerWalletTransactionBean = new CustomerWalletBalanceTransactionBean(); + $sellerWalletTransactionBean = new CustomerWalletBalanceTransactionBean(); + //买家初始化 + $buyerWalletTransactionBean->setType(self::TYPE_ORDER_PAY_DEC); + $buyerWalletTransactionBean->setStatus(self::STATUS_SUCCESS); + $buyerWalletTransactionBean->setAmount(-$orderBean->getAmount()); //负数 + $buyerWalletTransactionBean->setUid($orderBean->getBuyerUid()); + $buyerWalletTransactionBean->setCurrencyCode($orderBean->getBuyerCurrencyCode()); + $buyerWalletTransactionBean->setTargetPostOrderId($orderBean->getId()); + //卖家初始化 + $sellerWalletTransactionBean->setType(self::TYPE_ORDER_RECEIVE_ADD); + $sellerWalletTransactionBean->setStatus(self::STATUS_DELAY_PAYMENT); + $sellerWalletTransactionBean->setAmount($orderBean->getAmount()); //正数 + $sellerWalletTransactionBean->setUid($orderBean->getSellerUid()); + $sellerWalletTransactionBean->setCurrencyCode($orderBean->getSellerCurrencyCode()); + $sellerWalletTransactionBean->setTargetPostOrderId($orderBean->getId()); + //@@计算订单延期到账时间 + $delay_payment_time = Times::getNowDateTimeAddDays(1); + $sellerWalletTransactionBean->setDelayPaymentTime($delay_payment_time); + + try { + DB::beginTransaction(); + //查询买家余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resBuyerWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($buyerWalletTransactionBean->getUid(), $buyerWalletTransactionBean->getCurrencyCode()); + //检查买家余额是否足够 + if (Math::bcComp($resBuyerWalletBalanceModel->available_amount, abs($orderBean->getAmount())) == -1) throw new ModelException('buyer balance not enough'); + + //设置买家卖家钱包id + $buyerWalletTransactionBean->setWalletId($resBuyerWalletBalanceModel->id); + $buyerWalletTransactionBean->setBeforeTotalAmount($resBuyerWalletBalanceModel->total_amount); + $buyerWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resBuyerWalletBalanceModel->total_amount, $buyerWalletTransactionBean->getAmount())); + //设置卖家卖家钱包id + $resSellerWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($sellerWalletTransactionBean->getUid(), $sellerWalletTransactionBean->getCurrencyCode()); + if (!$resSellerWalletBalanceModel) throw new ModelException('seller wallet not found'); + $sellerWalletTransactionBean->setWalletId($resSellerWalletBalanceModel->id); + $sellerWalletTransactionBean->setBeforeTotalAmount($resSellerWalletBalanceModel->total_amount); + $sellerWalletTransactionBean->setAfterTotalAmount(Math::bcAdd($resSellerWalletBalanceModel->total_amount, $sellerWalletTransactionBean->getAmount())); + + //买家新增账变 + $resBuyerTransaction = $this->addTransaction($buyerWalletTransactionBean); + if (!$resBuyerTransaction) throw new ModelException('buyer addTransaction fail'); + $buyerWalletTransactionBean->setId($resBuyerTransaction->id); + //买家变更余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $res = $oCustomerWalletBalanceModel->subAvailableAmount($buyerWalletTransactionBean->getWalletId(), $buyerWalletTransactionBean->getAmount()); + if (!$res) throw new ModelException('buyer order pay fail'); + + //卖家新增账变 + $resSellerTransaction = $this->addTransaction($sellerWalletTransactionBean); + if (!$resSellerTransaction) throw new ModelException('seller addTransaction fail'); + $sellerWalletTransactionBean->setId($resSellerTransaction->id); + //卖家变更余额 + $res = $oCustomerWalletBalanceModel->addFrozenAmount($sellerWalletTransactionBean->getWalletId(), $sellerWalletTransactionBean->getAmount()); + if (!$res) throw new ModelException('seller order receive fail'); + + //更新订单状态 + $oCustomerUserPostOrderModel = new CustomerUserPostOrderModel(); + $updateItem = [ + 'id' => $orderBean->getId(), + 'pay_status' => CustomerUserPostOrderModel::PAY_STATUS_PAID, + 'buyer_wallet_transaction_id' => $buyerWalletTransactionBean->getId(), + 'seller_wallet_transaction_id' => $sellerWalletTransactionBean->getId(), + 'updated_at' => Times::getNowDateTime(), + ]; + $res = $oCustomerUserPostOrderModel->updateItem($updateItem); + if (!$res) throw new ModelException('updateItem fail'); + + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //发起提现 + function typeWithdrawFirst($uid, $currency_code, $amount): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(-abs($amount)); //负数 + $bean->setUid($uid); + $bean->setType(self::TYPE_WITHDRAW_DEC); + $bean->setStatus(self::STATUS_PROCESSING); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount($resWalletBalanceModel->total_amount); + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + //先冻结,等回调成功再扣减 + $res = $oCustomerWalletBalanceModel->frozenAmount($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('withdraw fail'); + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //提现回调 + function typeWithdrawSecondCallback($id, $status, $remark = null): bool + { + try { + DB::beginTransaction(); + $resModel = $this->findItem($id); + if (!$resModel) throw new ModelException('transaction not found'); + if ($resModel->status != self::STATUS_PROCESSING) throw new ModelException('transaction status error'); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findItem($resModel->wallet_id); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + + if ($status == self::STATUS_SUCCESS) { + //扣减冻结金额 + $res = $oCustomerWalletBalanceModel->subFrozenAmount($resModel->wallet_id, $resModel->amount); + if (!$res) throw new ModelException('withdraw fail'); + //更新账变状态 + $updateItem = [ + 'id' => $id, + 'status' => self::STATUS_SUCCESS, + 'updated_at' => Times::getNowDateTime(), + 'before_total_amount' => $resWalletBalanceModel->total_amount, + 'after_total_amount' => Math::bcAdd($resWalletBalanceModel->total_amount, $resModel->amount) + ]; + if (!empty($remark)) $updateItem['remark'] = $remark; + $res = $this->updateItem($updateItem); + if (!$res) throw new ModelException('updateItem fail'); + } else { //失败 + //解冻 + $res = $oCustomerWalletBalanceModel->unFrozenAmount($resModel->wallet_id, $resModel->amount); + if (!$res) throw new ModelException('withdraw fail'); + //更新账变状态 + $updateItem = [ + 'id' => $id, + 'status' => self::STATUS_FAIL, + 'updated_at' => Times::getNowDateTime(), + ]; + if (!empty($remark)) $updateItem['remark'] = $remark; + $res = $this->updateItem($updateItem); + if (!$res) throw new ModelException('updateItem fail'); + } + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog(__FUNCTION__ . ' ' . 'rollBack', $e, func_get_args()); + return false; + } + } + + //充值 + function typeRecharge($uid, $currency_code, $amount): bool + { + $bean = new CustomerWalletBalanceTransactionBean(); + $bean->setUid($uid); + $bean->setCurrencyCode($currency_code); + $bean->setAmount(abs($amount)); //正数 + $bean->setUid($uid); + $bean->setType(self::TYPE_RECHARGE_ADD); + $bean->setStatus(self::STATUS_SUCCESS); + try { + DB::beginTransaction(); + //查询用户余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oCustomerWalletBalanceModel->findByUidCurrencyCode($bean->getUid(), $bean->getCurrencyCode()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setWalletId($resWalletBalanceModel->id); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + $bean->setAfterTotalAmount(Math::bcAdd($resWalletBalanceModel->total_amount, $bean->getAmount())); + //新增账变 + $res = $this->addTransaction($bean); + if (!$res) throw new ModelException('addTransaction fail'); + //变更余额 + $oCustomerWalletBalanceModel = new CustomerWalletBalanceModel(); + $res = $oCustomerWalletBalanceModel->addAvailableAmount($bean->getWalletId(), $bean->getAmount()); + if (!$res) throw new ModelException('recharge fail'); + + DB::commit(); + Logs::SuccLog(__FUNCTION__, func_get_args()); + return true; + } catch (\Exception $e) { + DB::rollBack(); + Logs::ErrLog('typeRecharge error rollBack', $e); + return false; + } + + } + + function addTransaction(CustomerWalletBalanceTransactionBean &$bean) + { + if (!$bean->getWalletId()) throw new ModelException('wallet_id is required'); + if (!$bean->getType()) throw new ModelException('type is required'); + if (!$bean->getStatus()) $bean->setStatus(self::STATUS_WAIT); + if (empty($bean->getUid()) || empty($bean->getBeforeTotalAmount())) { + $oWalletBalanceModel = new CustomerWalletBalanceModel(); + $resWalletBalanceModel = $oWalletBalanceModel->findItem($bean->getWalletId()); + if (!$resWalletBalanceModel) throw new ModelException('wallet not found'); + $bean->setUid($resWalletBalanceModel->uid); + $bean->setBeforeTotalAmount($resWalletBalanceModel->total_amount); + } + $bean->setCreatedAt(Times::getNowDateTime()); + $bean->setUpdatedAt(Times::getNowDateTime()); + return $this->addItem($bean->toArrayNotNull()); + } + + +} diff --git a/app/Models/Api/Wallet/CustomerWalletCurrencyModel.php b/app/Models/Api/Wallet/CustomerWalletCurrencyModel.php new file mode 100644 index 0000000..9a93058 --- /dev/null +++ b/app/Models/Api/Wallet/CustomerWalletCurrencyModel.php @@ -0,0 +1,29 @@ + '现金', + self::TYPE_CRYPTO_COIN => '加密货币', + ]; + const CODE_USDT_TRC20 = 'USDT_TRC20'; +} diff --git a/app/Models/Wallet/Platform/WalletPlatformBalanceTransactionModel.php b/app/Models/Wallet/Platform/WalletPlatformBalanceTransactionModel.php index 0c0d30e..a6354e7 100644 --- a/app/Models/Wallet/Platform/WalletPlatformBalanceTransactionModel.php +++ b/app/Models/Wallet/Platform/WalletPlatformBalanceTransactionModel.php @@ -10,6 +10,7 @@ use App\Tools\Times; use App\Tools\Tools; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; +use Symfony\Contracts\Service; class WalletPlatformBalanceTransactionModel extends WalletBaseModel { @@ -156,17 +157,22 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel } //成功提现处理 - function withdrawSuccByUserTransactionId($user_transaction_id, $remark = ''): bool + function withdrawSuccByUserTransactionId($user_transaction_id,$entered_amount, $remark = ''): bool { try{ Db::beginTransaction(); $resModel = $this->findItemByWhere(['from_user_transaction_id' => $user_transaction_id, 'type' => self::TYPE_USER_WITHDRAW]); - if (!$resModel) return false; - if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) return false; - $resModel->status = self::STATUS_SUCCESS; - if(!empty($remark)) $resModel->remark = $remark; - $resModel->updated_at = Times::getNowDateTime(); - $res = $resModel->save(); + if (!$resModel) throw new ModelException('user transaction not found'); + if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) throw new ModelException('status error'); + $updateItems = [ + 'id' => $resModel->id, + 'status' => self::STATUS_SUCCESS, + 'updated_at' => Times::getNowDateTime(), + 'entered_amount' => $entered_amount, + 'fee_amount' => Db::raw('received_amount - '.$entered_amount), + ]; + if(!empty($remark)) $updateItems['remark'] = $remark; + $res = $this->updateItem($updateItems); if(!$res) throw new ModelException('save error'); //扣除平台冻结余额 @@ -196,9 +202,13 @@ class WalletPlatformBalanceTransactionModel extends WalletBaseModel if (!$resModel) throw new ModelException('user transaction not found'); if (!in_array($resModel->status, [self::STATUS_WAITING_QUEUE, self::STATUS_CHAIN_WAITING_CALLBACK])) throw new ModelException('status error'); //更改账变状态 - $resModel->status = self::STATUS_FAIL; - if(!empty($remark)) $resModel->remark = $remark; - $res = $resModel->save(); + $updateItems = [ + 'id' => $resModel->id, + 'status' => self::STATUS_FAIL, + 'updated_at' => Times::getNowDateTime(), + ]; + if(!empty($remark)) $updateItems['remark'] = $remark; + $res = $this->updateItem($updateItems); if (!$res) throw new ModelException('save error'); //解冻平台余额 diff --git a/app/Models/Wallet/PlatformUser/WalletPlatformUserTransactionModel.php b/app/Models/Wallet/PlatformUser/WalletPlatformUserTransactionModel.php index 1080458..5ab171c 100644 --- a/app/Models/Wallet/PlatformUser/WalletPlatformUserTransactionModel.php +++ b/app/Models/Wallet/PlatformUser/WalletPlatformUserTransactionModel.php @@ -303,13 +303,22 @@ class WalletPlatformUserTransactionModel extends WalletBaseModel if ($resWalletPlatformUserTransactionModel->type != self::TYPE_WITHDRAW) throw new ModelException('type error'); if ($resWalletPlatformUserTransactionModel->status != self::STATUS_CHAIN_WAITING) throw new ModelException('status error'); //用户账变处理 - $resWalletPlatformUserTransactionModel->status = $status; - $resWalletPlatformUserTransactionModel->save(); + $updateItems = [ + 'id' => $resWalletPlatformUserTransactionModel->id, + 'status' => $status, + 'entered_amount' => $resWalletAddrTransactionModel->entered_amount, + 'fee_amount' => Db::raw('received_amount - '.$resWalletAddrTransactionModel->entered_amount), + ]; + $res = $this->updateItem($updateItems); + if(!$res) { + Log::error('listenWithdrawCallbackErr', $updateItems); + return; + } //平台余额和账变处理 $oWalletPlatformBalanceTransactionModel = new WalletPlatformBalanceTransactionModel(); if ($status == self::STATUS_SUCCESS) { //成功 - $oWalletPlatformBalanceTransactionModel->withdrawSuccByUserTransactionId($id); + $oWalletPlatformBalanceTransactionModel->withdrawSuccByUserTransactionId($id,$resWalletAddrTransactionModel->entered_amount); } else { //失败 $oWalletPlatformBalanceTransactionModel->withdrawErrByUserTransactionId($id); } diff --git a/app/Models/Wallet/Wallet/WalletAddrModel.php b/app/Models/Wallet/Wallet/WalletAddrModel.php index 29af06f..e22bb29 100644 --- a/app/Models/Wallet/Wallet/WalletAddrModel.php +++ b/app/Models/Wallet/Wallet/WalletAddrModel.php @@ -247,10 +247,15 @@ class WalletAddrModel extends WalletBaseModel } if ($type == WalletAddrTransactionModel::TYPE_TRANSFER && $resWalletAddrTransactionModel) { //提现已有账变,修改账变状态即可 - $res = $oWalletAddrTransactionModel->updateItem([ + $updateItems = [ 'id' => $resWalletAddrTransactionModel->id, 'status' => $status, - ]); + ]; + if($status == WalletAddrTransactionModel::STATUS_SUCCESS){ + $updateItems['entered_amount'] = $amount; + $updateItems['fee_amount'] = Db::raw('received_amount - '.$amount); + } + $res = $oWalletAddrTransactionModel->updateItem($updateItems); if (!$res) { Log::error('listenWalletAddrCallbackErr', ['token' => $token, 'addr' => $addr, 'amount' => $amount, 'status' => $status, 'wallet_addr_id' => $resModel->id, 'wallet_transaction_id' => $resWalletAddrTransactionModel->id, 'error' => '修改账变状态失败']); throw new ModelException('修改账变状态失败'); diff --git a/app/Models/Wallet/Wallet/WalletCurrencyModel.php b/app/Models/Wallet/Wallet/WalletCurrencyModel.php index f10549a..b746c94 100644 --- a/app/Models/Wallet/Wallet/WalletCurrencyModel.php +++ b/app/Models/Wallet/Wallet/WalletCurrencyModel.php @@ -19,6 +19,12 @@ class WalletCurrencyModel extends WalletBaseModel 'created_at', ]; + const TYPE_CASH = 1; + const TYPE_CRYPTO_COIN = 2; + const TYPE = [ + self::TYPE_CASH => '现金', + self::TYPE_CRYPTO_COIN => '加密货币', + ]; const CODE_USDT_TRC20 = 'USDT_TRC20'; function findByCode($code,$columns = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null diff --git a/app/Tools/Logs.php b/app/Tools/Logs.php new file mode 100644 index 0000000..13e704f --- /dev/null +++ b/app/Tools/Logs.php @@ -0,0 +1,25 @@ + $e->getMessage(), 'trace' => $e->getTrace(), 'params' => $params]); + } + + static function SuccLog($title, $params = []): void + { + Log::error(self::getTitle($title,'error:'), $params); + } + + static function getTitle($title,$prefix=''): string + { + return $title.' '.$prefix; + } + + +} diff --git a/app/Tools/Times.php b/app/Tools/Times.php index c1e9ab8..6015754 100644 --- a/app/Tools/Times.php +++ b/app/Tools/Times.php @@ -1,12 +1,19 @@ addDays($days)->format($format); + } + }