<?php

namespace App\Http\Controllers\Channel\Book;

use App\Http\Controllers\Channel\BaseController;
use App\Modules\Book\Services\BookSubscribleChapterService;
use Hashids;
use Illuminate\Http\Request;

class BookSubScribleChapterController extends BaseController
{

    /**
     * @apiDefine Book 图书模块
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 设置强关章节
     * @api {post} book/setSubScribleChapter 设置强关章节
     * @apiGroup Book
     * @apiParam {Number} book_id 图书id
     * @apiParam {Number} subscribe_chapter_seq 强关章节序号
     * @apiName setSubScribleChapterId
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":[]
     *     }
     */
    function setSubScribleChapter(Request $request)
    {
        $channel_id = $this->getChannelId();
        $book_id = $request->has('book_id') ? $request->input('book_id') : '';
        $subscribe_chapter_id = $request->has('subscribe_chapter_seq') ? $request->input('subscribe_chapter_seq') : '';
        if (empty($book_id) || empty($subscribe_chapter_id)) {
            return response()->error("PARAM_EMPTY");
        }
        $book_id = Hashids::decode($book_id)[0];
        if (!is_numeric($book_id) || !is_numeric($subscribe_chapter_id)) {
            return response()->error("PARAM_ERROR");
        }
        $result = BookSubscribleChapterService::setSubcribleChapter(compact('channel_id', 'book_id', 'subscribe_chapter_id'));
        if ($result) {
            return response()->success();
        } else {
            return response()->error("HANDLE_FAILED");
        }
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 查询强关章节
     * @api {get} book/getSubScribleChapter 查询强关章节
     * @apiGroup Book
     * @apiParam {Number} book_id 图书id
     * @apiName getSubScribleChapter
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":11,
     *     }
     */
    function getSubScribleChapter(Request $request)
    {
        $book_id = $request->has('book_id') ? $request->input('book_id') : '';
        if (empty($book_id)) {
            return response()->error("PARAM_EMPTY");
        }

        $book_id = Hashids::decode($book_id)[0];
        if (!is_numeric($book_id)) {
            return response()->error("PARAM_ERROR");
        }
        $distribution_channel_id = $this->getChannelId();
        $result = BookSubscribleChapterService::getSubcribleChapter($book_id, $distribution_channel_id);
        return response()->success($result);
    }
}
