POINT_TYPE::class, 'status' => EXTERNAL_STATUS::class, 'extra_data' => 'array', ]; // 订单类型常量 const TYPE_EXCHANGE = 'exchange'; // 积分兑换 const TYPE_CONSUME = 'consume'; // 积分消费 const TYPE_REWARD = 'reward'; // 积分奖励 const TYPE_REFUND = 'refund'; // 积分退款 /** * 创建积分订单 * * @param int $userId 用户ID * @param int $pointId 积分类型ID * @param int $amount 积分数量 * @param string $orderType 订单类型 * @param string $title 订单标题 * @param string $description 订单描述 * @param array $extraData 额外数据 * @return PointOrderModel|false 成功返回模型,失败返回false */ public static function createOrder( int $userId, int $pointId, int $amount, string $orderType, string $title, string $description = '', array $extraData = [] ) { $order = new self(); $order->order_no = self::generateOrderNo(); $order->user_id = $userId; $order->point_id = $pointId; $order->amount = $amount; $order->order_type = $orderType; $order->title = $title; $order->description = $description; $order->status = EXTERNAL_STATUS::PENDING; $order->extra_data = $extraData; $order->create_time = time(); $order->update_time = time(); if ($order->save()) { return $order; } return false; } /** * 生成订单号 * * @return string 订单号 */ private static function generateOrderNo(): string { return 'PO' . date('YmdHis') . mt_rand(1000, 9999); } /** * 根据订单号获取订单 * * @param string $orderNo 订单号 * @return PointOrderModel|null 订单模型 */ public static function getByOrderNo(string $orderNo): ?PointOrderModel { return self::where('order_no', $orderNo)->first(); } /** * 更新订单状态 * * @param EXTERNAL_STATUS $status 状态 * @return bool 是否成功 */ public function updateStatus(EXTERNAL_STATUS $status): bool { $this->status = $status; $this->update_time = time(); return $this->save(); } /** * 标记为已完成 * * @return bool 是否成功 */ public function markCompleted(): bool { return $this->updateStatus(EXTERNAL_STATUS::COMPLETED); } /** * 标记为已失败 * * @return bool 是否成功 */ public function markFailed(): bool { return $this->updateStatus(EXTERNAL_STATUS::FAILED); } /** * 标记为已取消 * * @return bool 是否成功 */ public function markCancelled(): bool { return $this->updateStatus(EXTERNAL_STATUS::CANCELLED); } /** * 获取用户订单 * * @param int $userId 用户ID * @param int $limit 限制数量 * @return \Illuminate\Database\Eloquent\Collection 订单集合 */ public static function getUserOrders(int $userId, int $limit = 50) { return self::where('user_id', $userId) ->orderBy('create_time', 'desc') ->limit($limit) ->get(); } /** * 获取指定类型的订单 * * @param int $userId 用户ID * @param string $orderType 订单类型 * @param int $limit 限制数量 * @return \Illuminate\Database\Eloquent\Collection 订单集合 */ public static function getUserOrdersByType(int $userId, string $orderType, int $limit = 50) { return self::where('user_id', $userId) ->where('order_type', $orderType) ->orderBy('create_time', 'desc') ->limit($limit) ->get(); } /** * 获取订单类型名称 * * @return string 订单类型名称 */ public function getOrderTypeName(): string { return match($this->order_type) { self::TYPE_EXCHANGE => '积分兑换', self::TYPE_CONSUME => '积分消费', self::TYPE_REWARD => '积分奖励', self::TYPE_REFUND => '积分退款', default => '未知类型', }; } /** * 获取状态名称 * * @return string 状态名称 */ public function getStatusName(): string { return $this->status->getStatusName(); } /** * 判断是否为最终状态 * * @return bool 是否为最终状态 */ public function isFinalStatus(): bool { return $this->status->isFinalStatus(); } /** * 获取积分类型名称 * * @return string 积分类型名称 */ public function getPointTypeName(): string { return $this->point_id->getTypeName(); } /** * 判断是否为积分支出订单 * * @return bool 是否为支出订单 */ public function isExpenseOrder(): bool { return in_array($this->order_type, [self::TYPE_EXCHANGE, self::TYPE_CONSUME]); } /** * 判断是否为积分收入订单 * * @return bool 是否为收入订单 */ public function isIncomeOrder(): bool { return in_array($this->order_type, [self::TYPE_REWARD, self::TYPE_REFUND]); } }