| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 | <?php/** * ${CARET} * @file:BooksService.php * @Created by gnitif * @Date: 2023/3/22 * @Time: 11:46 */namespace Modules\ContentManage\Services\Books;use Catch\Exceptions\FailedException;use Illuminate\Support\Facades\Storage;use Modules\ContentManage\Models\Book;use Modules\ContentManage\Models\BookChapterContents;use Modules\ContentManage\Models\BookChapters;use Modules\ContentManage\Models\Output\OutputChannel;use Modules\ContentManage\Models\Output\OutputMapping;class BooksService{    /***     *  书籍列表分页     * name: bookList     * @param array $where     * @param array $order     * @param int $pageSize     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator     * date 2023/03/23 11:52     */    public static function bookList($where = [], $order = [], $pageSize = 20)    {        $list = Book::leftJoin('cps', 'cps.cp_id', 'books.cp_id')            ->select("books.id",                "books.name",                "books.author",                "books.intro",                "books.cover",                "books.size",                "books.keyword",                "books.status",                "books.chapter_count",                "books.category_name",                "books.channel",                "books.settlement_type",                "books.bottomline",                "books.start_date",                "books.end_date",                "books.created_at",                "cps.cp_name")->orderBy('books.id','desc');        $isExport = $where['is_export'] ?? 0;        $where = self::getCondition($where);        if (!is_empty($where)) {            $list->where($where);        }        if($isExport) {            return $list->get();        } else {            return $list->paginate($pageSize);        }    }    /**     * 查询条件拼接     * name: getCondition     * @param array $params     * @return array     * date 2023/03/23 11:52     */    private static function getCondition($params = [])    {        $where = [];        if (isset($params['name']) && !empty(isset($params['name']))) {            $where[] = ['books.name', 'like', "%{$params['name']}%"];        }        if (isset($params['id']) && !empty(isset($params['id']))) {            $where[] = ['books.id', '=', $params['id']];        }        if (isset($params['author']) && !empty(isset($params['author']))) {            $where[] = ['books.author', '=', $params['author']];        }        if (isset($params['settlement_type']) && !empty(isset($params['settlement_type']))) {            $where[] = ['books.settlement_type', '=', $params['settlement_type']];        }        if (isset($params['cp_id']) && !empty(isset($params['cp_id']))) {            $where[] = ['books.cp_id', '=', $params['cp_id']];        }        if (isset($params['create_start']) && !empty(isset($params['create_start']))) {            $where[] = ['books.created_at', '>', $params['create_start']];        }        if (isset($params['create_end']) && !empty(isset($params['create_end']))) {            $where[] = ['books.created_at', '<', $params['create_end']];        }        if (isset($params['start_date']) && !empty(isset($params['start_date']))) {            $where[] = ['books.end_date', '>=', $params['start_date']];        }        if (isset($params['end_date']) && !empty(isset($params['end_date']))) {            $where[] = ['books.end_date', '<=', $params['end_date']];        }        return $where;    }    /**     *  编辑书籍版权日期,结算方式     * name: editAuthorByBids     * @param $bids     * @param array $param     * date 2023/03/23 11:52     */    public static function editAuthorByBids($bids, $param = [])    {        return Book::whereIn('id', $bids)->update($param);    }    /**     *  保存书籍版权分库信息     * name: distributeInfo     * @param $bid     * date 2023/03/23 15:51     */    public static function distributeSubmit($bid, $params)    {        foreach ($params as $value) {            $data = [                'output_channel_id' => $value['channel_id'],                'is_enabled' => $value['is_enabled'] == 1 ? 1 : 0,                'bid' => $bid            ];            $info = OutputMapping::where('bid', $bid)->where('output_channel_id', $value['channel_id'])->first();            if ($info) {                $info->is_enabled = $value['is_enabled'] == 1 ? 1 : 0;                $info->update($data);            }else{                OutputMapping::create($data);            }        }        return true;    }    /**     *  获取书籍版权分库信息     * name: distributeInfo     * @param $bid     * date 2023/03/23 15:51     */    public static function distributeInfo($bid)    {        $channels = OutputChannel::where('is_enabled', 1)->select('id as channel_id', 'channel_name', 'remark')->get();        if ($channels->isEmpty()) {            return $channels;        }        $channelIds = array_column($channels->toArray(), 'channel_id');        $auth = OutputMapping::where('bid', $bid)->whereIn('output_channel_id', $channelIds)->select()->get()->toArray();        if (!empty($auth)) {            $auth = array_column($auth, null, 'output_channel_id');        }        foreach ($channels as $val) {            $info = $auth[$val->channel_id] ?? [];            $val->is_enabled = $info['is_enabled'] ?? 0;        }        return $channels;    }    /**     *  导出书籍     * name: exportByBid     * @param $bid     * date 2023/03/24 13:30     */    public static function exportByBid($bid)    {        $bookInfo = Book::where('id', $bid)->select('name', 'id')->first();        if (is_empty($bookInfo)) {            throw new FailedException('请选择正确的书籍');        }        $chapters = BookChapters::where('bid', $bid)->select('chapter_content_id as cid', 'name')->orderBy('sequence',"asc")->get();        $fileName = $bookInfo->name . '-' . $bookInfo->id . ".txt";        $model = new  BookChapterContents();        $contents =  $bookInfo->name . "\r\n";        if (!empty($chapters)) {            foreach ($chapters as  $val ){                $contents .= "###". $val->name."\r\n";                $content = $model->where('bid',$bid)->where('id',$val->cid)->value('content');                if ($content){                    $contents .= $content."\r\n";                }            }        }        return  response()->streamDownload(function () use ($contents){            echo $contents;        },$fileName);    }    public static function createBook(array $data){        $exists = Book::where('cp_id', $data['cp_id'])->where('name', $data['name'])->count();        if( $exists){            return null;        }        return Book::create($data);    }}
 |