<?php
/**
 * Created by PhpStorm.
 * User: Gold
 * Date: 2024/1/15
 * Time: 14:50
 */

namespace app\api\controller;

use app\admin\model\books\Books as BookModel; //教材
use app\admin\model\books\BooksLevel;  //教材级别
use app\admin\model\books\BooksArrangement; //教学层次
use app\admin\model\books\BooksSpeciality; //专业类型
use app\admin\model\books\BooksSeries; //教材系列
use app\admin\model\books\Contribute; //投稿
use app\common\controller\Api;

class Books extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 获取教材级别
     *
     * api/books/getBooksLevel
     */
    public function getBooksLevel(){
        $BooksLevel = new BooksLevel();
        $list = $BooksLevel->where('is_deleted',1)->where('status',0)->order('sort desc,id desc')->select();
        $this->success('操作成功',$list);
    }

    /**
     * 获取教学层次
     *
     * api/books/getBooksArrangement
     */
    public function getBooksArrangement(){
        $BooksArrangement = new BooksArrangement();
        $list = $BooksArrangement->where('is_deleted',1)->where('status',0)->order('sort desc,id desc')->select();
        $this->success('操作成功',$list);
    }

    /**
     * 获取专业类型
     *
     * api/books/getBooksSpeciality
     */
    public function getBooksSpeciality(){
        $BooksSpeciality = new BooksSpeciality();
        $list = $BooksSpeciality->where('is_deleted',1)->where('status',0)->order('sort desc,id desc')->select();
        $this->success('操作成功',$list);
    }

    /**
     * 教材列表
     *
     * api/books/getBooksList
     */
    public function getBooksList(){
        $data = input();
        $where = [];
        $BookModel = new BookModel();

        if(!isset($data['page']) || $data['page'] == '' || $data['page'] == null){
            $page = 1;
        }else{
            $page = $data['page'];
        }
        if(!isset($data['limit']) || $data['limit'] == '' || $data['limit'] == null){
            $limit = 20;
        }else{
            $limit = $data['limit'];
        }
        //教材级别
        if(isset($data['level_id']) && $data['level_id'] && $data['level_id']!= '' && $data['level_id'] != null){
            $where['level_id'] = $data['level_id'];
        }
        //教学层次
        if(isset($data['arrangement_id']) && $data['arrangement_id'] && $data['arrangement_id']!= '' && $data['arrangement_id'] != null){
            $where['arrangement_id'] = $data['arrangement_id'];
        }
        //专业类型
        if(isset($data['speciality_id']) && $data['speciality_id'] && $data['speciality_id']!= '' && $data['speciality_id'] != null){
            $where['speciality_id'] = $data['speciality_id'];
        }
        //教材类型 0纸质书 1电子书
        if(isset($data['book_type']) && $data['book_type'] && $data['book_type']!= '' && $data['book_type'] != null){
            $where['book_type'] = $data['book_type'];
        }
        //是否新书推荐 0是 1否
        if(isset($data['is_new_recommend']) && $data['is_new_recommend']!= '' && $data['is_new_recommend'] != null){
            $where['is_new_recommend'] = $data['is_new_recommend'];
        }
        //是否获奖教材 0是 1否
        if(isset($data['is_awards']) && $data['is_awards']!= '' && $data['is_awards'] != null){
            $where['is_awards'] = $data['is_awards'];
        }

        $where['is_deleted'] = 1;

        $list = $BookModel->where($where)->order('id desc')->field('id,title,image,book_type,is_new_recommend,author,is_awards,price,entity_price,createtime,updatetime,level_id,arrangement_id,speciality_id,series_id,is_series')->paginate([
            'list_rows'=> $limit,
            'page' => $page,
        ]);

        $this->success('操作成功',$list);
    }

    /**
     * 教材详情
     *
     * api/books/getBookInfo
     */
    public function getBookInfo(){
        $data = input();
        $BookModel = new BookModel();
        if(!isset($data['id']) || !$data['id']){
            $this->error('参数错误');
        }
        $id = $data['id'];
        $list = $BookModel->where('id',$id)->where('is_deleted',1)->with(['courseware_file','resource_file','package_file','specimen_file'])->find();
        if(!$list){
            $this->error('教材不存在或已删除');
        }
        $this->success('操作成功',$list);
    }

    /**
     * 获取系列相关教材
     *
     * api/books/getSeriesBooks
     */
    public function getSeriesBooks(){
        $data = input();
        $BookModel = new BookModel();
        if(!isset($data['series_id']) || !$data['series_id']){
            $this->error('参数错误');
        }
        $series_id = $data['series_id'];
        $list = $BookModel->where('series_id',$series_id)
            ->where('is_deleted',1)
            ->where('is_series',0)
            ->field('id,title,image,book_type,is_new_recommend,author,is_awards,price,entity_price,createtime,updatetime,level_id,arrangement_id,speciality_id,series_id,is_series')
            ->select();
        $this->success('操作成功',$list);
    }

}
