<?php

namespace App\Rpc\v1;

use App\Model\AppNews;
use App\Model\AppNewsColumn;
use App\Model\AppNewsSecondColumn;
use App\Rpc\BaseService;
use Hyperf\RpcServer\Annotation\RpcService;

#[RpcService(name: "column", server: "jsonrpc-http", protocol: "jsonrpc-http")]
class ColumnService extends BaseService
{
    /**
     * 获取栏目
     * @url /column/index
     * @param int $app_id
     * @return array
     */
    public function index(int $app_id = 0): array
    {
        $resp = $this->getResponse();
        if ($app_id) {
            $firstColumn = AppNewsColumn::query()->where('website', $app_id)->get()->toArray();
            $res = [];
            foreach ($firstColumn as $index => &$column) {
                $filter = [];
                $filter[] = ['column_tag', $column['id']];
                $res[] = [
                    'filter' => $filter,
                    'desc' => $column['desc'],
                    'item' => [['title' => $column['name'], 'link' => $column['url']]],
                    'path' => $column['url'],
                ];
                $second = AppNewsSecondColumn::query()->where('rid', $column['id'])->get()->toArray();
                if ($second) {
                    foreach ($second as $secondCol) {
                        $appendArr = [];
                        $appendArr[] = ['title' => $column['name'], 'link' => $column['url']];
                        $appendArr[] = ['title' => $secondCol['name'], 'link' => $column['url'] . $secondCol['url']];
                        $filter = [
                            ['column_tag', $column['id']],
                            ['second_column', $secondCol['id']]
                        ];
                        $res[] = [
                            'filter' => $filter,
                            'path' => "{$column['url']}{$secondCol['url']}",
                            'desc' => $secondCol['desc'],
                            'item' => $appendArr,
                        ];
                    }
                }
            }
            return $resp->setCode(200)->setData($res)->send();
        }

        return $resp->setCode(200)->send();
    }
}