'全部', self::StatusDefault => '待支付', self::StatusConfirm => '待确认', self::StatusSuccess => '成功', self::StatusFail => '失败', self::StatusCancel => '取消' ]; // 自动写入时间戳字段 protected $autoWriteTimestamp = 'int'; // 定义时间戳字段名 protected $createTime = 'create_time'; protected $updateTime = 'update_time'; protected $deleteTime = false; // 追加属性 protected $append = [ 'create_time_text', 'update_time_text' ]; public function userInfo() { return $this->hasOne(UserModel::class, 'id', 'user_id'); } /** * 创建购买(充值)记录 * @param string $txHash * @param int $uid * @param string $fee * @param int $status * @return int|string * @throws Exception */ public function createRecord(int $cashFrom, string $txHash, int $uid, string $fee, int $status, int $order_type = RechargeOrderType::RentalPower, int $block_number = 0, string $note = '') { $orderExist = $this->where('tx_hash', $txHash)->count(); if ($orderExist > 0) { throw new Exception('Hash已存在'); } $now = time(); $result = $this->insertGetId([ 'order_type' => $order_type, 'type' => $cashFrom, 'tx_hash' => $txHash, 'user_id' => $uid, 'symbol' => Asset::USDT, 'amount' => $fee, 'status' => $status, 'create_time' => $now, 'note' => $note, 'block_number' => $block_number, ]); if (empty($result)) { throw new Exception('创建购买记录失败'); } return $result; } /** * 服务器算力和后续处理 * @param int $uid * @param string $fee * @throws Exception */ public function updateOrderStatus(int $orderID, int $status, int $block_number = 0, float $power = 0) { $update = [ 'status' => $status, 'update_time' => time(), 'block_number' => $block_number, ]; if($power > 0){ $update['power'] = $power; } $affect = (new OfflineRechargeRecordModel()) ->where('status', RechargeStatus::StatusUnAuth) ->where('id', $orderID) ->update($update); if (empty($affect)) { throw new Exception("处理充值订单状态的影响行数为0,orderID:$orderID,status:$status"); } } public function getCreateTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getUpdateTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } protected function setCreateTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } protected function setUpdateTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } }