|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
namespace App\Modules\Book\Services;
|
|
|
|
|
|
+use App\Modules\Book\Models\Book;
|
|
|
use App\Modules\Book\Models\Chapter;
|
|
|
use OSS\Core\OssException;
|
|
|
use OSS\OssClient;
|
|
@@ -287,4 +288,103 @@ class ChapterService
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ public static function splitContent($chapter_id)
|
|
|
+ {
|
|
|
+ $chapter = self::getChapterById($chapter_id);
|
|
|
+ if(!$chapter || $chapter->bid <0 || $chapter->size < 4500) return [];
|
|
|
+ $content_list = explode("\r\n",$chapter->content);
|
|
|
+ //print_r($content_list);
|
|
|
+ $count = 1;
|
|
|
+ $temp_size= 0;
|
|
|
+ $temp_content = '';
|
|
|
+ $data = [];
|
|
|
+ foreach ($content_list as $item){
|
|
|
+ if($temp_size >2500){
|
|
|
+ $count++;
|
|
|
+ $temp_content = '';
|
|
|
+ $temp_size = 0;
|
|
|
+ }
|
|
|
+ $temp_size += mb_strlen($item);
|
|
|
+ $temp_content .= $item.'\r\n';
|
|
|
+ $data[$count-1] = [
|
|
|
+ 'bid'=>$chapter->bid,
|
|
|
+ 'is_vip'=>$chapter->is_vip,
|
|
|
+ 'name'=>sprintf('%s(%s)',$chapter->name,$count),
|
|
|
+ 'sequence'=>$chapter->sequence+$count-1,
|
|
|
+ 'size'=>$temp_size,
|
|
|
+ 'prev_cid'=>0,
|
|
|
+ 'next_cid'=>0,
|
|
|
+ 'recent_update_at'=>$chapter->recent_update_at,
|
|
|
+ 'content'=>$temp_content,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ if($data && $data[$count-1]['size'] <1500){
|
|
|
+ $data[$count-2]['content'] = $data[$count-2]['content'].$data[$count-1]['content'];
|
|
|
+ $data[$count-2]['size'] = $data[$count-2]['size']+$data[$count-1]['size'];
|
|
|
+ unset($data[$count-1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function createSplitContent($chapter_id,$data)
|
|
|
+ {
|
|
|
+ if(count($data) == 1){
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ Chapter::where('bid',$data[0]['bid'])->where('sequence','>=',$data[0]['sequence']+1)->increment('sequence',count($data)-1);
|
|
|
+ Chapter::where('id',$chapter_id)->update([
|
|
|
+ 'content'=>$data[0]['content'],
|
|
|
+ 'size'=>$data[0]['size']
|
|
|
+ ]);
|
|
|
+ foreach ($data as $kye=>$item){
|
|
|
+ if($kye == 0) continue;
|
|
|
+ self::createChapter($item);
|
|
|
+ }
|
|
|
+ self::adjustSequent($data[0]['bid'],$data[0]['sequence'],$data[0]['sequence']+count($data));
|
|
|
+
|
|
|
+ if(self::getChapterFromOss($data[0]['bid'],$chapter_id)){
|
|
|
+ $oss = self::ossObject();
|
|
|
+ $bucket = env('OSS_BUCKET','zhuishuyun');
|
|
|
+ $path_format = 'book/chapters/%s/%s.json';
|
|
|
+ $oss->deleteObject($bucket,sprintf($path_format, $data[0]['bid'], $chapter_id));
|
|
|
+ }
|
|
|
+ $key = sprintf('book_chapter_%s_%s', $data[0]['bid'], $chapter_id);
|
|
|
+ //Redis::connection('chapter')
|
|
|
+ Redis::connection('chapter')->delete($key);
|
|
|
+
|
|
|
+ $last = Chapter::where('bid',$data[0]['bid'])
|
|
|
+ ->select('id','bid','name','sequence')
|
|
|
+ ->orderBy('sequence','desc')
|
|
|
+ ->limit(1)
|
|
|
+ ->first();
|
|
|
+ Book::where('id',$data[0]['bid'])->update([
|
|
|
+ 'chapter_count'=>$last->sequence,
|
|
|
+ 'last_chapter'=>$last->last_chapter,
|
|
|
+ 'last_cid'=>$last->id,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function adjustSequent($bid,$start_sequence,$end_sequence){
|
|
|
+ $chapter_list = Chapter::where('bid',$bid);
|
|
|
+ if($start_sequence){
|
|
|
+ $chapter_list->where('sequence','>=',$start_sequence);
|
|
|
+ }
|
|
|
+ if($end_sequence){
|
|
|
+ $chapter_list->where('sequence','<=',$end_sequence);
|
|
|
+ }
|
|
|
+ $chapter_list->orderBy('sequence')->select('id')->get();
|
|
|
+ $prev = 0;
|
|
|
+
|
|
|
+ foreach ($chapter_list as $chapter){
|
|
|
+ if($prev){
|
|
|
+ Chapter::where('id',$chapter->id)->update(['prev_cid'=>$prev]);
|
|
|
+ Chapter::where('id',$prev)->update(['next_cid'=>$chapter->id]);
|
|
|
+ }
|
|
|
+ $prev = $chapter->id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|