OfflineRechargeRecordModel.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // 自动写入时间戳字段
  30. protected $autoWriteTimestamp = 'int';
  31. // 定义时间戳字段名
  32. protected $createTime = 'create_time';
  33. protected $updateTime = 'update_time';
  34. protected $deleteTime = false;
  35. // 追加属性
  36. protected $append = [
  37. 'create_time_text',
  38. 'update_time_text'
  39. ];
  40. public function userInfo()
  41. {
  42. return $this->hasOne(UserModel::class, 'id', 'user_id');
  43. }
  44. /**
  45. * 创建购买(充值)记录
  46. * @param string $txHash
  47. * @param int $uid
  48. * @param string $fee
  49. * @param int $status
  50. * @return int|string
  51. * @throws Exception
  52. */
  53. public function createRecord(int $cashFrom, string $txHash, int $uid, string $fee, int $status, int $order_type = RechargeOrderType::RentalPower, int $block_number = 0, string $note = '')
  54. {
  55. $orderExist = $this->where('tx_hash', $txHash)->count();
  56. if ($orderExist > 0) {
  57. throw new Exception('Hash已存在');
  58. }
  59. $now = time();
  60. $result = $this->insertGetId([
  61. 'order_type' => $order_type,
  62. 'type' => $cashFrom,
  63. 'tx_hash' => $txHash,
  64. 'user_id' => $uid,
  65. 'symbol' => Asset::USDT,
  66. 'amount' => $fee,
  67. 'status' => $status,
  68. 'create_time' => $now,
  69. 'note' => $note,
  70. 'block_number' => $block_number,
  71. ]);
  72. if (empty($result)) {
  73. throw new Exception('创建购买记录失败');
  74. }
  75. return $result;
  76. }
  77. /**
  78. * 服务器算力和后续处理
  79. * @param int $uid
  80. * @param string $fee
  81. * @throws Exception
  82. */
  83. public function updateOrderStatus(int $orderID, int $status, int $block_number = 0, float $power = 0)
  84. {
  85. $update = [
  86. 'status' => $status,
  87. 'update_time' => time(),
  88. 'block_number' => $block_number,
  89. ];
  90. $affect = (new OfflineRechargeRecordModel())
  91. ->where('status', RechargeStatus::StatusUnAuth)
  92. ->where('id', $orderID)
  93. ->update($update);
  94. if (empty($affect)) {
  95. throw new Exception("处理充值订单状态的影响行数为0,orderID:$orderID,status:$status");
  96. }
  97. }
  98. public function getCreateTimeTextAttr($value, $data)
  99. {
  100. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  101. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  102. }
  103. public function getUpdateTimeTextAttr($value, $data)
  104. {
  105. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  106. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  107. }
  108. protected function setCreateTimeAttr($value)
  109. {
  110. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  111. }
  112. protected function setUpdateTimeAttr($value)
  113. {
  114. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  115. }
  116. }