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

use app\service\api\BaseApiService;
use app\service\RequestHelper;
use Applications\sample\modules\article\repository\ArticleRepository;
use Applications\sample\modules\order\core\CoreOrderService;
use Applications\sample\modules\order\repository\OrderRepository;
use think\Request;

/**
 * Order Service
 * @package Applications\sample\modules\order\service\api
 */
class OrderService extends BaseApiService
{
    /**
     * C端我的订单列表
     */
    public function list(int $uid, Request $request): array
    {
        $page = $this->repository()->paginateForApi($uid, [], RequestHelper::parseLimit($request, 'limit', 10, 100));
        return ['code' => 0, 'data' => $page];
    }

    /**
     * 创建订单
     */
    public function create(int $uid, int $articleId): array
    {
        $article = $this->app->make(ArticleRepository::class)->findById($articleId);
        if (!$article) {
            return ['code' => 1, 'msg' => '文章不存在'];
        }
        return $this->core()->create($uid, $articleId, (float)$article->price);
    }

    private function core(): CoreOrderService
    {
        return $this->app->make(CoreOrderService::class);
    }

    private function repository(): OrderRepository
    {
        return $this->app->make(OrderRepository::class);
    }
}
