BookService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Services\Book;
  3. use App\Consts\ErrorConst;
  4. use App\Facade\Site;
  5. use App\Libs\Utils;
  6. use GuzzleHttp\Client;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Redis;
  9. use OSS\Core\OssException;
  10. use OSS\OssClient;
  11. class BookService
  12. {
  13. public function __construct() {
  14. }
  15. public function getBookList($data) {
  16. $bid = getProp($data, 'bid');
  17. $book_name = getProp($data, 'book_name');
  18. $status = getProp($data, 'status');
  19. $query = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')
  20. ->whereIn('bc.cp_source', ['ycsd', 'yqsd'])->whereIn('bc.is_on_shelf', [1,2])->select('b.status', 'bc.*',
  21. DB::raw('(select count(id) from mp_book_version where bid = b.id) as version_count'));
  22. if ($bid) {
  23. $query->where('b.id', $bid);
  24. }
  25. if ($book_name) {
  26. $query->where('bc.book_name', 'like', "%{$book_name}%");
  27. }
  28. if ($status !== '') {
  29. $query->where('b.status', $status);
  30. }
  31. $result = $query->orderBy('b.id')->paginate();
  32. return $result;
  33. }
  34. public function getAllBooks($data) {
  35. $bid = getProp($data, 'bid');
  36. $book_name = getProp($data, 'book_name');
  37. $query = DB::table('book_configs')->whereIn('is_on_shelf', [1,2])->select('bid', 'book_name');
  38. if ($bid) {
  39. $query->where('bid', $bid);
  40. }
  41. if ($book_name) {
  42. $query->where('book_name', 'like', "%{$book_name}%");
  43. }
  44. return $query->orderBy('bid', 'desc')->get()->map(function($value) {
  45. return (array)$value;
  46. })->toArray();
  47. }
  48. public function getBookVersion($data) {
  49. $bid = getProp($data, 'bid');
  50. if (!$bid) Utils::throwError('20003:请选择书籍');
  51. return DB::table('mp_book_version')->where('bid', $bid)->orderBy('id', 'desc')->select('bid', 'id as version_id', 'version_name')
  52. ->get()->map(function($value) {
  53. return (array)$value;
  54. })->toArray();
  55. }
  56. public function addBookVersion($data) {
  57. $bid = getProp($data, 'bid');
  58. if (!$bid) Utils::throwError('20003:请选择书籍');
  59. $version_name = trim(getProp($data, 'version_name'));
  60. if (!$version_name) Utils::throwError('20003:请填写版本名');
  61. $book_name = DB::table('book_configs')->where('bid', $bid)->value('book_name');
  62. $version_name = $book_name."【{$version_name}】";
  63. if (DB::table('mp_book_version')->where('bid', $bid)->where('version_name', $version_name)->exists()) Utils::throwError('20003:版本名已存在');
  64. $all_chapters = DB::table('chapters')->where('bid', $bid)->where('is_check', 1)->where('is_deleted', 0)->select('id', 'name', 'sequence', 'size', 'chapter_content_id')->get();
  65. try {
  66. DB::beginTransaction();
  67. $version_id = DB::table('mp_book_version')->insertGetId([
  68. 'bid' => $bid,
  69. 'version_name' => $version_name,
  70. 'created_at' => date('Y-m-d H:i:s'),
  71. 'updated_at' => date('Y-m-d H:i:s'),
  72. ]);
  73. if (!$version_id) {
  74. DB::rollBack();
  75. Utils::throwError('20003:创建版本失败');
  76. }
  77. $insert_data = [];
  78. foreach ($all_chapters as $chapter) {
  79. $insert_data[] = [
  80. 'bid' => $bid,
  81. 'book_name' => $book_name,
  82. 'version_id' => $version_id,
  83. 'cid' => getProp($chapter, 'id'),
  84. 'chapter_name' => getProp($chapter, 'name'),
  85. 'sequence' => getProp($chapter, 'sequence'),
  86. 'size' => getProp($chapter, 'size'),
  87. 'generate_status' => '待制作',
  88. 'created_at' => date('Y-m-d H:i:s'),
  89. 'updated_at' => date('Y-m-d H:i:s'),
  90. ];
  91. }
  92. $boolen = DB::table('mp_chapter_audios')->insert($insert_data);
  93. if (!$boolen) {
  94. DB::rollBack();
  95. Utils::throwError('20003:带入章节失败');
  96. }
  97. }catch (\Exception $e) {
  98. DB::rollBack();
  99. Utils::throwError('20003:'.$e->getMessage());
  100. }
  101. DB::commit();
  102. return true;
  103. }
  104. public function getChapterList($data) {
  105. $bid = getProp($data, 'bid');
  106. $version_id = getProp($data, 'version_id');
  107. if (!$bid) Utils::throwError('20003:请选择书籍');
  108. if (!$version_id) Utils::throwError('20003:请选择版本');
  109. $book = DB::table('books as b')->leftJoin('book_configs as bc', 'b.id', '=', 'bc.bid')->where('b.id', $bid)->whereIn('bc.is_on_shelf', [1,2])
  110. ->select('bc.book_name', 'bc.cover', 'b.size', 'b.chapter_count', 'b.intro')->first();
  111. if (!$book) Utils::throwError('20003:书籍不存在');
  112. $header = (array)$book;
  113. $header['cover'] = addPrefix($header['cover']);
  114. $header['intro'] = filterIntro($header['intro']);
  115. $list = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->orderBy('sequence', 'asc')
  116. ->select('cid', 'chapter_name', 'sequence', 'size', 'generate_status', 'audio_url', 'remark')
  117. ->paginate();
  118. return ['header' => $header, 'list' => $list];
  119. }
  120. public function getChapterContent($data) {
  121. $cid = getProp($data, 'cid');
  122. if (!$cid) Utils::throwError('20003:请选择章节');
  123. $content = DB::table('chapters as c')->leftJoin('chapter_contents as cc', 'c.chapter_content_id', '=', 'cc.id')->where('c.id', $cid)->value('cc.content');
  124. if (!$content) Utils::throwError('20003:章节内容不存在');
  125. $content = filterContent($content);
  126. return $content;
  127. }
  128. }