OfflineRechargeRecordModel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\common\model;
  3. use Exception;
  4. use fast\Action;
  5. use fast\Asset;
  6. use fast\RechargeOrderType;
  7. use fast\RechargeStatus;
  8. use think\Db;
  9. use think\Model;
  10. class OfflineRechargeRecordModel extends Model
  11. {
  12. protected $name = "offline_recharge_record";
  13. const StatusDefault = 0;
  14. const StatusConfirm = 100;
  15. const StatusSuccess = 200;
  16. const StatusFail = 400;
  17. const StatusCancel = 500;
  18. /*
  19. * 客户端订单状态筛选
  20. */
  21. public $order_status = [
  22. '-1' => '全部',
  23. self::StatusDefault => '待支付',
  24. self::StatusConfirm => '待确认',
  25. self::StatusSuccess => '成功',
  26. self::StatusFail => '失败',
  27. self::StatusCancel => '取消'
  28. ];
  29. public function userInfo()
  30. {
  31. return $this->hasOne(UserModel::class, 'id', 'user_id');
  32. }
  33. /**
  34. * 创建购买(充值)记录
  35. * @param string $txHash
  36. * @param int $uid
  37. * @param string $fee
  38. * @param int $status
  39. * @return int|string
  40. * @throws Exception
  41. */
  42. public function createRecord(int $cashFrom, string $txHash, int $uid, string $fee, int $status, int $order_type = RechargeOrderType::RentalPower, int $block_number = 0, string $note = '')
  43. {
  44. $orderExist = $this->where('tx_hash', $txHash)->count();
  45. if ($orderExist > 0) {
  46. throw new Exception('Hash已存在');
  47. }
  48. $now = time();
  49. $result = $this->insertGetId([
  50. 'order_type' => $order_type,
  51. 'type' => $cashFrom,
  52. 'tx_hash' => $txHash,
  53. 'user_id' => $uid,
  54. 'symbol' => Asset::USDT,
  55. 'amount' => $fee,
  56. 'status' => $status,
  57. 'create_time' => $now,
  58. 'note' => $note,
  59. 'block_number' => $block_number,
  60. ]);
  61. if (empty($result)) {
  62. throw new Exception('创建购买记录失败');
  63. }
  64. return $result;
  65. }
  66. /**
  67. * 服务器算力和后续处理
  68. * @param int $uid
  69. * @param string $fee
  70. * @throws Exception
  71. */
  72. public function updateOrderStatus(int $orderID, int $status, int $block_number = 0, float $power = 0)
  73. {
  74. $update = [
  75. 'status' => $status,
  76. 'update_time' => time(),
  77. 'block_number' => $block_number,
  78. ];
  79. if($power > 0){
  80. $update['power'] = $power;
  81. }
  82. $affect = (new OfflineRechargeRecordModel())
  83. ->where('status', RechargeStatus::StatusUnAuth)
  84. ->where('id', $orderID)
  85. ->update($update);
  86. if (empty($affect)) {
  87. throw new Exception("处理充值订单状态的影响行数为0,orderID:$orderID,status:$status");
  88. }
  89. }
  90. }