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); } }