MusicRepository.php 7.5 KB

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