MusicRepository.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: durong
  5. * Date: 2019/7/8
  6. * Time: 下午23:50
  7. */
  8. namespace App\Repositories;
  9. use Acekyd\LaravelMP3\LaravelMP3;
  10. use App\Models\PostMusic;
  11. use App\Models\PostMusicCategory;
  12. use App\Models\PostMusicCategoryRel;
  13. use App\Models\PostMusicUser;
  14. use Illuminate\Support\Facades\DB;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. use Dingo\Api\Http\Response;
  17. use Illuminate\Database\QueryException;
  18. class MusicRepository
  19. {
  20. public function __construct(PostMusicCategoryRel $postMusicCategoryRel,PostMusicCategory $postMusicCategory,PostMusic $postMusic,PostMusicUser $postMusicUser)
  21. {
  22. $this->postMusicCategoryRel = $postMusicCategoryRel;
  23. $this->postMusicCategory = $postMusicCategory;
  24. $this->postMusic = $postMusic;
  25. $this->postMusicUser = $postMusicUser;
  26. }
  27. public function category_list($request)
  28. {
  29. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  30. $where = [];
  31. if(isset($request['id'])){
  32. $where[] = ['id', '=', $request['id']];
  33. }
  34. return $this->postMusicCategory->where($where)->orderBy('id', 'asc')->paginate($perPage);
  35. }
  36. public function categoryCreate($request)
  37. {
  38. if($this->postMusicCategory->where('name', $request['name'])->exists()){
  39. throw new HttpException(500, '该分类已经存在');
  40. }
  41. $data = [
  42. 'name' => $request['name'],
  43. 'is_open' => $request['is_open'] ?? 0,
  44. ];
  45. if (!$this->postMusicCategory->create($data)) {
  46. throw new HttpException(500, '添加失败');
  47. }
  48. }
  49. public function categoryEdit($request)
  50. {
  51. if($this->postMusicCategory->where('name', $request['name'])->exists()){
  52. throw new HttpException(500, '该分类已经存在');
  53. }
  54. $post_music_category = $this->postMusicCategory->where('id', $request['id'])->first();
  55. $post_music_category->name = $request['name'];
  56. $post_music_category->is_open = $request['is_open'] ?? 0;
  57. $post_music_category->updated_at = date('Y-m-d H:i:s');
  58. $res = $post_music_category->save();
  59. if (!$res) {
  60. throw new HttpException(500, '音乐分类更新失败');
  61. }
  62. }
  63. public function editStatus($request)
  64. {
  65. $post_music_category = $this->postMusicCategory->find($request['id']);
  66. $post_music_category->is_open = $request['is_open'];
  67. $post_music_category->updated_at = date('Y-m-d H:i:s');
  68. $res = $post_music_category->save();
  69. if (!$res) {
  70. throw new HttpException(500, '修改状态失败');
  71. }
  72. }
  73. public function categoryDelete($request)
  74. {
  75. if ($this->postMusicCategoryRel->where('music_category_id',$request['id'])->exists()) {
  76. throw new HttpException(500, '当前分类下含有音乐,不能删除');
  77. }
  78. $post_music_category = $this->postMusicCategory->where('id', $request['id'])->first();
  79. $res = $post_music_category->delete();
  80. if (!$res){
  81. return Response::create([
  82. 'message' => '删除失败,请重试',
  83. 'status_code' => 500
  84. ]);
  85. }
  86. }
  87. public function musicList($request)
  88. {
  89. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  90. $where = [];
  91. if(isset($request['id'])){
  92. $where[] = ['post_music_category_rel.id', '=', $request['id']];
  93. }
  94. if(isset($request['name'])){
  95. $where[] = ['post_music.name', 'like', "%{$request['name']}%"];
  96. }
  97. if(isset($request['category_id'])){
  98. $where[] = ['post_music_category.id', '=', $request['category_id']];
  99. }
  100. $postMusicList = $this->postMusicCategoryRel
  101. ->join('post_music_category', 'post_music_category.id', '=', 'post_music_category_rel.music_category_id')
  102. ->join('post_music', 'post_music.id', '=', 'post_music_category_rel.mid')
  103. ->select('post_music_category_rel.id','post_music.name','post_music.url','post_music.music_duration','post_music.created_at','post_music_category.name as category_name','post_music_category.id as category_id')
  104. ->where($where)
  105. ->orderBy('id', 'desc')
  106. ->paginate($perPage);
  107. return $postMusicList;
  108. }
  109. public function musicCreate($request)
  110. {
  111. $url = $request['url'];
  112. $data = [
  113. 'name' => $request['name'],
  114. 'url' => $url,
  115. 'music_duration' => $request['music_duration'],
  116. ];
  117. $date = date('Y-m-d H:i:s');
  118. DB::beginTransaction();
  119. try {
  120. $res = $this->postMusic->create($data);
  121. if ($res) {
  122. $mid = $res->id;
  123. $create_category_rel = [
  124. 'mid' => $mid,
  125. 'music_category_id' => $request['category_id'],
  126. 'created_at' => $date,
  127. 'updated_at' => $date
  128. ];
  129. $result = $this->postMusicCategoryRel->insert($create_category_rel);
  130. if (!$result) {
  131. throw new HttpException(500, '音乐与分类关联-添加失败');
  132. }
  133. }
  134. DB::commit();
  135. return Response::create();
  136. } catch (QueryException $exception) {
  137. DB::rollBack();
  138. return Response::create([
  139. 'message' => '添加失败,请重试',
  140. 'error' => $exception->getMessage(),
  141. 'status_code' => 500
  142. ]);
  143. }
  144. }
  145. public function musicEdit($request)
  146. {
  147. $post_music_rel = $this->postMusicCategoryRel->select('mid')->where('id', $request['id'])->first();
  148. $post_music = $this->postMusic->where('id',$post_music_rel->mid)->first();
  149. $date = date('Y-m-d H:i:s');
  150. $post_music->name = $request['name'];
  151. $post_music->url = $request['url'];
  152. $post_music->music_duration = $request['music_duration'];
  153. $post_music->updated_at = $date;
  154. DB::beginTransaction();
  155. try{
  156. $res = $post_music->save();
  157. if ($res) {
  158. $create_category_rel = [
  159. 'mid' => $post_music->id,
  160. 'music_category_id' => $request['category_id'],
  161. 'created_at' => $date,
  162. 'updated_at' => $date,
  163. ];
  164. $result = $this->postMusicCategoryRel->where('id',$request['id'])->update($create_category_rel);
  165. if (!$result){
  166. throw new HttpException(500, '音乐与分类关联-修改失败');
  167. }
  168. }
  169. DB::commit();
  170. return Response::create();
  171. }catch (QueryException $exception){
  172. DB::rollBack();
  173. return Response::create([
  174. 'message' => '修改失败,请重试',
  175. 'error' => $exception->getMessage(),
  176. 'status_code' => 500
  177. ]);
  178. }
  179. }
  180. public function musicDelete($request)
  181. {
  182. $post_music_rel = $this->postMusicCategoryRel->where('id', $request['id'])->first();
  183. DB::beginTransaction();
  184. try {
  185. $res = $post_music_rel->delete();
  186. if ($res) {
  187. $post_music = $this->postMusic->where('id',$post_music_rel->mid)->delete();
  188. if (!$post_music){
  189. throw new HttpException(500, '音乐删除失败');
  190. }
  191. }
  192. DB::commit();
  193. return Response::create();
  194. } catch (QueryException $exception) {
  195. DB::rollBack();
  196. return Response::create([
  197. 'message' => '删除失败,请重试',
  198. 'error' => $exception->getMessage(),
  199. 'status_code' => 500
  200. ]);
  201. }
  202. }
  203. public function userMusic($request)
  204. {
  205. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  206. return $this->postMusicUser->orderBy('id', 'asc')->paginate($perPage);
  207. }
  208. }