| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\common\model;
- use Exception;
- use fast\Action;
- use fast\Asset;
- use fast\RechargeOrderType;
- use fast\RechargeStatus;
- use think\Db;
- use think\Model;
- class OfflineRechargeRecordModel extends Model
- {
- protected $name = "offline_recharge_record";
- const StatusDefault = 0;
- const StatusConfirm = 100;
- const StatusSuccess = 200;
- const StatusFail = 400;
- const StatusCancel = 500;
- /*
- * 客户端订单状态筛选
- */
- public $order_status = [
- '-1' => '全部',
- 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,
- ];
- $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);
- }
- }
|