$orderNo, 'user_id' => $userId, 'chat_num' => $chatNum, 'price' => $price, 'status' => self::STATUS_PENDING, 'create_time' => time(), ]); } /** * 生成订单号 * @return string */ public static function generateOrderNo(): string { return 'C' . date('YmdHis') . rand(1000, 9999); } /** * 支付成功 * @param int $orderId 订单ID * @return bool */ public static function paySuccess(int $orderId): bool { return self::where('id', $orderId) ->where('status', self::STATUS_PENDING) ->update([ 'status' => self::STATUS_PAID, 'pay_time' => time(), 'update_time' => time(), ]) !== false; } /** * 取消订单 * @param int $orderId 订单ID * @return bool */ public static function cancelOrder(int $orderId): bool { return self::where('id', $orderId) ->where('status', self::STATUS_PENDING) ->update([ 'status' => self::STATUS_CANCEL, 'update_time' => time(), ]) !== false; } /** * 获取用户订单列表 * @param int $userId 用户ID * @param int $page 页码 * @param int $limit 每页数量 * @return array */ public static function getUserOrders(int $userId, int $page = 1, int $limit = 10): array { $where = ['user_id' => $userId]; $data = self::where($where) ->order('id desc') ->page($page, $limit) ->select(); $total = self::where($where)->count(); return [ 'list' => $data ? $data->toArray() : [], 'total' => $total, 'page' => $page, 'limit' => $limit, ]; } /** * 根据订单号查询 * @param string $orderNo 订单号 * @return self|null */ public static function findByOrderNo(string $orderNo): ?self { return self::where('order_no', $orderNo)->find(); } /** * 获取状态文本 * @param int $status 状态 * @return string */ public static function getStatusText(int $status): string { $statusMap = [ self::STATUS_PENDING => '待支付', self::STATUS_PAID => '已支付', self::STATUS_CANCEL => '已取消', ]; return $statusMap[$status] ?? '未知'; } }