BooksService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * ${CARET}
  4. * @file:BooksService.php
  5. * @Created by gnitif
  6. * @Date: 2023/3/22
  7. * @Time: 11:46
  8. */
  9. namespace Modules\ContentManage\Services\Books;
  10. use Catch\Exceptions\FailedException;
  11. use Illuminate\Support\Facades\Storage;
  12. use Modules\ContentManage\Models\Book;
  13. use Modules\ContentManage\Models\BookChapterContents;
  14. use Modules\ContentManage\Models\BookChapters;
  15. use Modules\ContentManage\Models\Output\OutputChannel;
  16. use Modules\ContentManage\Models\Output\OutputMapping;
  17. class BooksService
  18. {
  19. /***
  20. * 书籍列表分页
  21. * name: bookList
  22. * @param array $where
  23. * @param array $order
  24. * @param int $pageSize
  25. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  26. * date 2023/03/23 11:52
  27. */
  28. public static function bookList($where = [], $order = [], $pageSize = 20)
  29. {
  30. $list = Book::leftJoin('cps', 'cps.cp_id', 'books.cp_id')
  31. ->select("books.id",
  32. "books.name",
  33. "books.author",
  34. "books.intro",
  35. "books.cover",
  36. "books.size",
  37. "books.keyword",
  38. "books.status",
  39. "books.chapter_count",
  40. "books.category_name",
  41. "books.channel",
  42. "books.settlement_type",
  43. "books.bottomline",
  44. "books.start_date",
  45. "books.end_date",
  46. "books.created_at",
  47. "cps.cp_name")->orderBy('books.id','desc');
  48. $isExport = $where['is_export'] ?? 0;
  49. $where = self::getCondition($where);
  50. if (!is_empty($where)) {
  51. $list->where($where);
  52. }
  53. if($isExport) {
  54. return $list->get();
  55. } else {
  56. return $list->paginate($pageSize);
  57. }
  58. }
  59. /**
  60. * 查询条件拼接
  61. * name: getCondition
  62. * @param array $params
  63. * @return array
  64. * date 2023/03/23 11:52
  65. */
  66. private static function getCondition($params = [])
  67. {
  68. $where = [];
  69. if (isset($params['name']) && !empty(isset($params['name']))) {
  70. $where[] = ['books.name', 'like', "%{$params['name']}%"];
  71. }
  72. if (isset($params['id']) && !empty(isset($params['id']))) {
  73. $where[] = ['books.id', '=', $params['id']];
  74. }
  75. if (isset($params['author']) && !empty(isset($params['author']))) {
  76. $where[] = ['books.author', '=', $params['author']];
  77. }
  78. if (isset($params['settlement_type']) && !empty(isset($params['settlement_type']))) {
  79. $where[] = ['books.settlement_type', '=', $params['settlement_type']];
  80. }
  81. if (isset($params['cp_id']) && !empty(isset($params['cp_id']))) {
  82. $where[] = ['books.cp_id', '=', $params['cp_id']];
  83. }
  84. if (isset($params['create_start']) && !empty(isset($params['create_start']))) {
  85. $where[] = ['books.created_at', '>', $params['create_start']];
  86. }
  87. if (isset($params['create_end']) && !empty(isset($params['create_end']))) {
  88. $where[] = ['books.created_at', '<', $params['create_end']];
  89. }
  90. if (isset($params['start_date']) && !empty(isset($params['start_date']))) {
  91. $where[] = ['books.end_date', '>=', $params['start_date']];
  92. }
  93. if (isset($params['end_date']) && !empty(isset($params['end_date']))) {
  94. $where[] = ['books.end_date', '<=', $params['end_date']];
  95. }
  96. return $where;
  97. }
  98. /**
  99. * 编辑书籍版权日期,结算方式
  100. * name: editAuthorByBids
  101. * @param $bids
  102. * @param array $param
  103. * date 2023/03/23 11:52
  104. */
  105. public static function editAuthorByBids($bids, $param = [])
  106. {
  107. return Book::whereIn('id', $bids)->update($param);
  108. }
  109. /**
  110. * 保存书籍版权分库信息
  111. * name: distributeInfo
  112. * @param $bid
  113. * date 2023/03/23 15:51
  114. */
  115. public static function distributeSubmit($bid, $params)
  116. {
  117. foreach ($params as $value) {
  118. $data = [
  119. 'output_channel_id' => $value['channel_id'],
  120. 'is_enabled' => $value['is_enabled'] == 1 ? 1 : 0,
  121. 'bid' => $bid
  122. ];
  123. $info = OutputMapping::where('bid', $bid)->where('output_channel_id', $value['channel_id'])->first();
  124. if ($info) {
  125. $info->is_enabled = $value['is_enabled'] == 1 ? 1 : 0;
  126. $info->update($data);
  127. }else{
  128. OutputMapping::create($data);
  129. }
  130. }
  131. return true;
  132. }
  133. /**
  134. * 获取书籍版权分库信息
  135. * name: distributeInfo
  136. * @param $bid
  137. * date 2023/03/23 15:51
  138. */
  139. public static function distributeInfo($bid)
  140. {
  141. $channels = OutputChannel::where('is_enabled', 1)->select('id as channel_id', 'channel_name', 'remark')->get();
  142. if ($channels->isEmpty()) {
  143. return $channels;
  144. }
  145. $channelIds = array_column($channels->toArray(), 'channel_id');
  146. $auth = OutputMapping::where('bid', $bid)->whereIn('output_channel_id', $channelIds)->select()->get()->toArray();
  147. if (!empty($auth)) {
  148. $auth = array_column($auth, null, 'output_channel_id');
  149. }
  150. foreach ($channels as $val) {
  151. $info = $auth[$val->channel_id] ?? [];
  152. $val->is_enabled = $info['is_enabled'] ?? 0;
  153. }
  154. return $channels;
  155. }
  156. /**
  157. * 导出书籍
  158. * name: exportByBid
  159. * @param $bid
  160. * date 2023/03/24 13:30
  161. */
  162. public static function exportByBid($bid)
  163. {
  164. $bookInfo = Book::where('id', $bid)->select('name', 'id')->first();
  165. if (is_empty($bookInfo)) {
  166. throw new FailedException('请选择正确的书籍');
  167. }
  168. $chapters = BookChapters::where('bid', $bid)->select('chapter_content_id as cid', 'name')->orderBy('sequence',"asc")->get();
  169. $fileName = $bookInfo->name . '-' . $bookInfo->id . ".txt";
  170. $model = new BookChapterContents();
  171. $contents = $bookInfo->name . "\r\n";
  172. if (!empty($chapters)) {
  173. foreach ($chapters as $val ){
  174. $contents .= "###". $val->name."\r\n";
  175. $content = $model->where('bid',$bid)->where('id',$val->cid)->value('content');
  176. if ($content){
  177. $contents .= $content."\r\n";
  178. }
  179. }
  180. }
  181. return response()->streamDownload(function () use ($contents){
  182. echo $contents;
  183. },$fileName);
  184. }
  185. public static function createBook(array $data){
  186. $exists = Book::where('cp_id', $data['cp_id'])->where('name', $data['name'])->count();
  187. if( $exists){
  188. return null;
  189. }
  190. return Book::create($data);
  191. }
  192. }