<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\modules\order\repository;

use app\repository\BaseRepository;
use Applications\sample\modules\order\model\Order;

/**
 * Order Repository
 * @package Applications\sample\modules\order\repository
 */
class OrderRepository extends BaseRepository
{
    /**
     * 后台分页列表（关联文章标题）
     */
    public function paginateForAdmin(array $filters, int $limit): array
    {
        $query = Order::alias('o')
            ->join('sample_article a', 'o.article_id = a.id', 'left')
            ->field('o.*, o.pay_time as pay_time, a.title as article_title')
            ->order('o.id', 'desc');

        $orderNo = trim((string)($filters['order_no'] ?? ''));
        if ($orderNo !== '') {
            $query->where('o.order_no', 'like', "%{$orderNo}%");
        }

        $createdFrom = trim((string)($filters['created_from'] ?? ''));
        $createdTo = trim((string)($filters['created_to'] ?? ''));
        if ($createdFrom !== '' && $createdTo !== '') {
            $query->whereBetweenTime('o.create_time', $createdFrom . ' 00:00:00', $createdTo . ' 23:59:59');
        }

        return $query->paginate($limit)->toArray();
    }

    /**
     * C端订单列表
     */
    public function paginateForApi(int $uid, array $filters, int $limit): array
    {
        $query = Order::alias('o')
            ->join('sample_article a', 'o.article_id = a.id', 'left')
            ->field('o.*, a.title as article_title, a.cover_image')
            ->where('o.user_id', $uid)
            ->order('o.id', 'desc');

        return $query->paginate($limit)->toArray();
    }

    /**
     * 按订单号查询
     */
    public function findByOrderNo(string $orderNo): ?Order
    {
        $order = Order::where('order_no', $orderNo)->find();
        return $order instanceof Order ? $order : null;
    }

    /**
     * 用户是否已购买文章
     */
    public function hasPaid(int $uid, int $articleId): bool
    {
        return Order::where('user_id', $uid)
            ->where('article_id', $articleId)
            ->where('status', 1)
            ->count() > 0;
    }

    /**
     * 创建订单
     */
    public function createOrder(int $uid, int $articleId, string $orderNo, float $amount): int
    {
        $order = Order::create([
            'user_id' => $uid,
            'article_id' => $articleId,
            'order_no' => $orderNo,
            'amount' => $amount,
            'status' => 0,
        ]);
        return $order->id;
    }

    /**
     * 标记已支付
     */
    public function markPaid(string $orderNo): void
    {
        Order::where('order_no', $orderNo)
            ->where('status', 0)
            ->update([
                'status' => 1,
                'pay_time' => date('Y-m-d H:i:s'),
            ]);
    }
}
