BookController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * ${CARET}
  4. * @file:BookController.php
  5. * @Created by gnitif
  6. * @Date: 2023/3/22
  7. * @Time: 13:36
  8. */
  9. namespace Modules\ContentManage\Http\Controllers;
  10. use Catch\Base\CatchController as Controller;
  11. use Catch\Exceptions\FailedException;
  12. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  13. use Illuminate\Database\Eloquent\Model;
  14. use Illuminate\Http\Request;
  15. use Modules\ContentManage\Enums\BookEnum;
  16. use Modules\ContentManage\Http\Requests\BookRequest;
  17. use Modules\ContentManage\Models\Book;
  18. use Modules\ContentManage\Models\BookCategories;
  19. use Modules\ContentManage\Services\Books\BooksService;
  20. use Modules\ContentManage\Services\Books\ChapterService;
  21. use Storage;
  22. class BookController extends Controller
  23. {
  24. /**
  25. * 书籍列表
  26. * name: list
  27. * @param Request $request
  28. * @return LengthAwarePaginator
  29. * date 2023/03/23 13:49
  30. */
  31. public function list(Request $request)
  32. {
  33. $where = $this->getCondition($request);
  34. $pageSize = $request->input('limit', 20);
  35. $list = BooksService::bookList($where, [], $pageSize);
  36. $settlementTypes = array_column($this->settlementTypes(), null, 'value');
  37. if (!$list->isEmpty()) {
  38. foreach ($list as $value) {
  39. $value->is_on_shelf = 0;
  40. if (!is_empty($value->end_date) && $value->end_date < date("Y-m-d")) {
  41. $value->is_on_shelf = 1;
  42. }
  43. $value->settlement_type_text = $settlementTypes[$value->settlement_type]['name'] ?? "";
  44. $value->status_text = $value->status == 1 ? "完本" : "连载";
  45. $value->channel_text = $value->channel == 1 ? "男频" : "女频";
  46. }
  47. }
  48. return $list;
  49. }
  50. /**
  51. * 结算方式
  52. * name: settlementTypes
  53. * @return \string[][]
  54. * date 2023/03/23 13:49
  55. */
  56. public function settlementTypes()
  57. {
  58. return [
  59. ['value' => 'buyout', 'name' => '买断'],
  60. ['value' => 'bottomline', 'name' => '保底'],
  61. ['value' => 'share', 'name' => '分成'],
  62. //['value' => 'zhuishuyun', 'name' => '追书云计算'],
  63. ];
  64. }
  65. // 查询条件拼接
  66. private function getCondition(Request $request): array
  67. {
  68. $where = [];
  69. if (!empty($request->input('name', ""))) {
  70. $where['name'] = $request->input('name');
  71. }
  72. if (!empty($request->input('cp_id', 0))) {
  73. $where['cp_id'] = $request->input('cp_id', 0);
  74. }
  75. if (!empty($request->input('settlement_type', ''))) {
  76. $where['settlement_type'] = $request->input('settlement_type', '');
  77. }
  78. if (!empty($request->input('bid', 0))) {
  79. $where['id'] = $request->input('bid', 0);
  80. }
  81. if (!empty($request->input('author', 0))) {
  82. $where['author'] = $request->input('author', 0);
  83. }
  84. $createStart = $request->input("create_start", "");
  85. $createEnd = $request->input("create_end", "");
  86. if (!empty($createStart) || !empty($createEnd)) {
  87. if ($createStart == $createEnd) {
  88. $createEnd = date("Y-m-d H:i:s", strtotime($createStart) + 86400);
  89. }
  90. if ($createStart > $createEnd) {
  91. $temp = $createEnd;
  92. $createEnd = $createStart;
  93. $createStart = $temp;
  94. }
  95. $where['create_start'] = $createStart;
  96. if (!empty($createEnd)) {
  97. $where['create_end'] = $createEnd;
  98. }
  99. }
  100. $createStart = $request->input("start_date", "");
  101. $createEnd = $request->input("end_date", "");
  102. if (!empty($createStart) || !empty($createEnd)) {
  103. if ($createStart == $createEnd) {
  104. $createEnd = date("Y-m-d", strtotime($createStart) + 86400);
  105. }
  106. if ($createStart > $createEnd) {
  107. $temp = $createEnd;
  108. $createEnd = $createStart;
  109. $createStart = $temp;
  110. }
  111. $where['start_date'] = $createStart;
  112. if (!empty($createEnd)) {
  113. $where['end_date'] = $createEnd;
  114. }
  115. }
  116. $where['is_export'] = $request->input('is_export', 0);
  117. return $where;
  118. }
  119. /**
  120. * 编辑版权时间和结算方式
  121. * name: editAuthorByBid
  122. * @param BookRequest $request
  123. * @return bool
  124. * date 2023/03/23 13:49
  125. */
  126. public function editAuthorByBid(BookRequest $request)
  127. {
  128. $bids = explode(',', $request->input('bid'));
  129. $param = [];
  130. if ($request->has("status")){
  131. $status = $request->input('status');
  132. if (!in_array($status,[0,1])){
  133. throw new FailedException('连载状态不正确');
  134. }
  135. $param['status'] = $status;
  136. }
  137. if ($request->has("settlement_type")){
  138. if (!in_array($request->input('settlement_type'), array_column($this->settlementTypes(), 'value'))) {
  139. throw new FailedException('结算方式不正确');
  140. }
  141. $param['settlement_type'] = $request->input('settlement_type');
  142. if ($param['settlement_type'] == 'bottomline') {
  143. if (!$request->has('bottomline')) {
  144. throw new FailedException('保底金额必填!');
  145. }
  146. $bottomline = $request->input('bottomline');
  147. if (!is_numeric($bottomline) || $bottomline < 0) {
  148. throw new FailedException('保底金额不正确!');
  149. }
  150. $param['bottomline'] = $bottomline;
  151. }
  152. }
  153. if (!empty($request->input('start_date', ''))) {
  154. $param['start_date'] = $request->input('start_date');
  155. }
  156. if (!empty($request->input('end_date', ''))) {
  157. $param['end_date'] = $request->input('end_date');
  158. }
  159. $distributePrivilege = $request->input('distribution_privilege');
  160. if($distributePrivilege) {
  161. foreach ($bids as $bid) {
  162. BooksService::distributeSubmit($bid, $distributePrivilege);
  163. }
  164. }
  165. if (!empty($param)){
  166. return BooksService::editAuthorByBids($bids, $param);
  167. }
  168. return true;
  169. }
  170. /**
  171. * 书籍编辑时获取书籍信息
  172. * name: bookInfo
  173. * @param $bid
  174. * @return Model|null
  175. * date 2023/03/23 15:35
  176. */
  177. public function bookInfo($bid)
  178. {
  179. return (new Book())->firstBy($bid);
  180. }
  181. /**
  182. * 获取书籍版权分库信息
  183. * name: distributeInfo
  184. * @param $bid
  185. * date 2023/03/23 15:51
  186. */
  187. public function distributeInfo($bid)
  188. {
  189. return BooksService::distributeInfo($bid);
  190. }
  191. /**
  192. * 获取书籍版权分库信息
  193. * name: distributeInfo
  194. * @param $bid
  195. * date 2023/03/23 15:51
  196. */
  197. public function distributeSubmit($bid, Request $request)
  198. {
  199. $param = $request->input('channels');
  200. return BooksService::distributeSubmit($bid, $param);
  201. }
  202. public function export($bid)
  203. {
  204. return BooksService::exportByBid($bid);
  205. }
  206. public function createBook(Request $request){
  207. $cp_id = $request->post('cp_id');
  208. $cp_name = $request->post('cp_name');
  209. $book_name = $request->post('book_name');
  210. $author = $request->post('author');
  211. $channel = $request->post('channel');
  212. $status = $request->post('status',1);
  213. $category_id = $request->post('category_id');
  214. $category_name = $request->post('category_name');
  215. $vip_start = $request->post('vip_start',10);
  216. $path = $request->post('path');
  217. \Log::info('import',['path'=>$path]);
  218. $result = BooksService::createBook([
  219. 'cp_name'=>$cp_name,'cp_id'=>$cp_id,'name'=>$book_name,'author'=>$author,'intro'=>'','cover'=>'','category_id'=>$category_id,
  220. 'category_name'=>$category_name,'status'=>$status,'channel'=>$channel
  221. ]);
  222. if(!$result){
  223. throw new FailedException('已经导过了');
  224. }
  225. $chapter_result = ChapterService::createChapterFromFile($result->id,1,$vip_start,storage_path('app/'.$path));
  226. Storage::delete($path);
  227. $result->size = $chapter_result['size'];
  228. $result->chapter_count = $chapter_result['chapter_count'];
  229. $result->first_cid = $chapter_result['first_cid'];
  230. $result->last_cid = $chapter_result['last_cid'];
  231. $result->last_chapter = $chapter_result['last_chapter'];
  232. $result->save();
  233. $chapter_result['bid'] = $result->id;
  234. return $chapter_result;
  235. }
  236. public function import(Request $request){
  237. if (!$request->hasFile('file')) {
  238. throw new FailedException('缺少文件哦');
  239. }
  240. $file = $request->file('file');
  241. $path = $file->path();
  242. \Log::info('import',['path'=>$path]);
  243. $extension = $request->file->extension();
  244. $name = \Str::random(40).'.'.$extension;
  245. $path = $request->file->storeAs('book',$name);
  246. return ['path'=>$path];
  247. }
  248. public function categoryList(Request $request){
  249. $bookCategories = new BookCategories();
  250. $category_list = $bookCategories->show()
  251. ->select('id','category_name','channel_id','channel_name','pid')->get();
  252. $result = [
  253. ['channel_id'=>1,'channel_name'=>'男频','list'=>[]],
  254. ['channel_id'=>2,'channel_name'=>'女频','list'=>[]],
  255. ];
  256. foreach ($category_list as $item){
  257. //if($item->pid == 0) continue;
  258. $temp = ['category_id'=>$item->id,'category_name'=>$item->category_name];
  259. if($item->channel_id == 1){
  260. $result[0]['list'][] = $temp;
  261. }
  262. if($item->channel_id == 2){
  263. $result[1]['list'][] = $temp;
  264. }
  265. }
  266. return $result;
  267. }
  268. }