| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 | <?php/** * ${CARET} * @file:BookController.php * @Created by gnitif * @Date: 2023/3/22 * @Time: 13:36 */namespace Modules\ContentManage\Http\Controllers;use Catch\Base\CatchController as Controller;use Catch\Exceptions\FailedException;use Illuminate\Contracts\Pagination\LengthAwarePaginator;use Illuminate\Database\Eloquent\Model;use Illuminate\Http\Request;use Modules\ContentManage\Enums\BookEnum;use Modules\ContentManage\Http\Requests\BookRequest;use Modules\ContentManage\Models\Book;use Modules\ContentManage\Models\BookCategories;use Modules\ContentManage\Services\Books\BooksService;use Modules\ContentManage\Services\Books\ChapterService;use Storage;class BookController extends Controller{    /**     * 书籍列表     * name: list     * @param Request $request     * @return LengthAwarePaginator     * date 2023/03/23 13:49     */    public function list(Request $request)    {        $where = $this->getCondition($request);        $pageSize = $request->input('limit', 20);        $list = BooksService::bookList($where, [], $pageSize);        $settlementTypes = array_column($this->settlementTypes(), null, 'value');        if (!$list->isEmpty()) {            foreach ($list as $value) {                $value->is_on_shelf = 0;                if (!is_empty($value->end_date) && $value->end_date < date("Y-m-d")) {                    $value->is_on_shelf = 1;                }                $value->settlement_type_text = $settlementTypes[$value->settlement_type]['name'] ?? "";                $value->status_text = $value->status == 1 ? "完本" : "连载";                $value->channel_text = $value->channel == 1 ? "男频" : "女频";            }        }        return $list;    }    /**     *  结算方式     * name: settlementTypes     * @return \string[][]     * date 2023/03/23 13:49     */    public function settlementTypes()    {        return [            ['value' => 'buyout', 'name' => '买断'],            ['value' => 'bottomline', 'name' => '保底'],            ['value' => 'share', 'name' => '分成'],            //['value' => 'zhuishuyun', 'name' => '追书云计算'],        ];    }    // 查询条件拼接    private function getCondition(Request $request): array    {        $where = [];        if (!empty($request->input('name', ""))) {            $where['name'] = $request->input('name');        }        if (!empty($request->input('cp_id', 0))) {            $where['cp_id'] = $request->input('cp_id', 0);        }        if (!empty($request->input('settlement_type', ''))) {            $where['settlement_type'] = $request->input('settlement_type', '');        }        if (!empty($request->input('bid', 0))) {            $where['id'] = $request->input('bid', 0);        }        if (!empty($request->input('author', 0))) {            $where['author'] = $request->input('author', 0);        }        $createStart = $request->input("create_start", "");        $createEnd = $request->input("create_end", "");        if (!empty($createStart) || !empty($createEnd)) {            if ($createStart == $createEnd) {                $createEnd = date("Y-m-d H:i:s", strtotime($createStart) + 86400);            }            if ($createStart > $createEnd) {                $temp = $createEnd;                $createEnd = $createStart;                $createStart = $temp;            }            $where['create_start'] = $createStart;            if (!empty($createEnd)) {                $where['create_end'] = $createEnd;            }        }        $createStart = $request->input("start_date", "");        $createEnd = $request->input("end_date", "");        if (!empty($createStart) || !empty($createEnd)) {            if ($createStart == $createEnd) {                $createEnd = date("Y-m-d", strtotime($createStart) + 86400);            }            if ($createStart > $createEnd) {                $temp = $createEnd;                $createEnd = $createStart;                $createStart = $temp;            }            $where['start_date'] = $createStart;            if (!empty($createEnd)) {                $where['end_date'] = $createEnd;            }        }        $where['is_export'] = $request->input('is_export', 0);        return $where;    }    /**     *  编辑版权时间和结算方式     * name: editAuthorByBid     * @param BookRequest $request     * @return bool     * date 2023/03/23 13:49     */    public function editAuthorByBid(BookRequest $request)    {        $bids = explode(',', $request->input('bid'));        $param = [];        if ($request->has("status")){            $status = $request->input('status');            if (!in_array($status,[0,1])){                throw new FailedException('连载状态不正确');            }            $param['status'] = $status;        }        if ($request->has("settlement_type")){            if (!in_array($request->input('settlement_type'), array_column($this->settlementTypes(), 'value'))) {                throw new FailedException('结算方式不正确');            }            $param['settlement_type'] = $request->input('settlement_type');            if ($param['settlement_type'] == 'bottomline') {                if (!$request->has('bottomline')) {                    throw new FailedException('保底金额必填!');                }                $bottomline = $request->input('bottomline');                if (!is_numeric($bottomline) || $bottomline < 0) {                    throw new FailedException('保底金额不正确!');                }                $param['bottomline'] = $bottomline;            }        }        if (!empty($request->input('start_date', ''))) {            $param['start_date'] = $request->input('start_date');        }        if (!empty($request->input('end_date', ''))) {            $param['end_date'] = $request->input('end_date');        }        $distributePrivilege = $request->input('distribution_privilege');        if($distributePrivilege) {            foreach ($bids as $bid) {                BooksService::distributeSubmit($bid, $distributePrivilege);            }        }        if (!empty($param)){            return BooksService::editAuthorByBids($bids, $param);        }        return true;    }    /**     *   书籍编辑时获取书籍信息     * name: bookInfo     * @param $bid     * @return Model|null     * date 2023/03/23 15:35     */    public function bookInfo($bid)    {        return (new Book())->firstBy($bid);    }    /**     *  获取书籍版权分库信息     * name: distributeInfo     * @param $bid     * date 2023/03/23 15:51     */    public function distributeInfo($bid)    {        return BooksService::distributeInfo($bid);    }    /**     *  获取书籍版权分库信息     * name: distributeInfo     * @param $bid     * date 2023/03/23 15:51     */    public function distributeSubmit($bid, Request $request)    {        $param = $request->input('channels');        return BooksService::distributeSubmit($bid, $param);    }    public function export($bid)    {        return  BooksService::exportByBid($bid);    }    public function createBook(Request $request){        $cp_id = $request->post('cp_id');        $cp_name = $request->post('cp_name');        $book_name = $request->post('book_name');        $author  = $request->post('author');        $channel = $request->post('channel');        $status = $request->post('status',1);        $category_id = $request->post('category_id');        $category_name = $request->post('category_name');        $vip_start = $request->post('vip_start',10);        $path = $request->post('path');        \Log::info('import',['path'=>$path]);                $result = BooksService::createBook([            'cp_name'=>$cp_name,'cp_id'=>$cp_id,'name'=>$book_name,'author'=>$author,'intro'=>'','cover'=>'','category_id'=>$category_id,            'category_name'=>$category_name,'status'=>$status,'channel'=>$channel        ]);        if(!$result){            throw new FailedException('已经导过了');        }        $chapter_result = ChapterService::createChapterFromFile($result->id,1,$vip_start,storage_path('app/'.$path));        Storage::delete($path);        $result->size = $chapter_result['size'];        $result->chapter_count = $chapter_result['chapter_count'];        $result->first_cid = $chapter_result['first_cid'];        $result->last_cid = $chapter_result['last_cid'];        $result->last_chapter = $chapter_result['last_chapter'];        $result->save();        $chapter_result['bid'] = $result->id;        return $chapter_result;    }    public function import(Request $request){        if (!$request->hasFile('file')) {            throw new FailedException('缺少文件哦');        }        $file = $request->file('file');        $path = $file->path();        \Log::info('import',['path'=>$path]);        $extension = $request->file->extension();        $name = \Str::random(40).'.'.$extension;        $path = $request->file->storeAs('book',$name);        return ['path'=>$path];    }    public function categoryList(Request $request){        $bookCategories = new BookCategories();        $category_list = $bookCategories->show()        ->select('id','category_name','channel_id','channel_name','pid')->get();        $result = [            ['channel_id'=>1,'channel_name'=>'男频','list'=>[]],            ['channel_id'=>2,'channel_name'=>'女频','list'=>[]],        ];        foreach ($category_list as $item){            //if($item->pid == 0) continue;            $temp = ['category_id'=>$item->id,'category_name'=>$item->category_name];            if($item->channel_id == 1){                $result[0]['list'][] = $temp;            }            if($item->channel_id == 2){                $result[1]['list'][] = $temp;            }        }        return $result;    }}
 |