MusicRepository.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $post_music_category = $this->postMusicCategory->where('id', $request['id'])->first();
  76. $res = $post_music_category->delete();
  77. if (!$res){
  78. return Response::create([
  79. 'message' => '删除失败,请重试',
  80. 'status_code' => 500
  81. ]);
  82. }
  83. }
  84. public function musicList($request)
  85. {
  86. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  87. $where = [];
  88. if(isset($request['id'])){
  89. $where[] = ['post_music_category_rel.id', '=', $request['id']];
  90. }
  91. if(isset($request['name'])){
  92. $where[] = ['post_music.name', 'like', "%{$request['name']}%"];
  93. }
  94. if(isset($request['category_id'])){
  95. $where[] = ['post_music_category.id', '=', $request['category_id']];
  96. }
  97. $postMusicList = $this->postMusicCategoryRel
  98. ->join('post_music_category', 'post_music_category.id', '=', 'post_music_category_rel.music_category_id')
  99. ->join('post_music', 'post_music.id', '=', 'post_music_category_rel.mid')
  100. ->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')
  101. ->where($where)
  102. ->orderBy('id', 'desc')
  103. ->paginate($perPage);
  104. return $postMusicList;
  105. }
  106. public function musicCreate($request)
  107. {
  108. $url = $request['url'];
  109. $data = [
  110. 'name' => $request['name'],
  111. 'url' => $url,
  112. 'music_duration' => $request['music_duration'],
  113. ];
  114. $date = date('Y-m-d H:i:s');
  115. DB::beginTransaction();
  116. try {
  117. $res = $this->postMusic->create($data);
  118. if ($res) {
  119. $mid = $res->id;
  120. $create_category_rel = [
  121. 'mid' => $mid,
  122. 'music_category_id' => $request['category_id'],
  123. 'created_at' => $date,
  124. 'updated_at' => $date
  125. ];
  126. $result = $this->postMusicCategoryRel->insert($create_category_rel);
  127. if (!$result) {
  128. throw new HttpException(500, '音乐与分类关联-添加失败');
  129. }
  130. }
  131. DB::commit();
  132. return Response::create();
  133. } catch (QueryException $exception) {
  134. DB::rollBack();
  135. return Response::create([
  136. 'message' => '添加失败,请重试',
  137. 'error' => $exception->getMessage(),
  138. 'status_code' => 500
  139. ]);
  140. }
  141. }
  142. public function musicEdit($request)
  143. {
  144. $post_music_rel = $this->postMusicCategoryRel->select('mid')->where('id', $request['id'])->first();
  145. $post_music = $this->postMusic->where('id',$post_music_rel->mid)->first();
  146. $date = date('Y-m-d H:i:s');
  147. $post_music->name = $request['name'];
  148. $post_music->url = $request['url'];
  149. $post_music->music_duration = $request['music_duration'];
  150. $post_music->updated_at = $date;
  151. DB::beginTransaction();
  152. try{
  153. $res = $post_music->save();
  154. if ($res) {
  155. $create_category_rel = [
  156. 'mid' => $post_music->id,
  157. 'music_category_id' => $request['category_id'],
  158. 'created_at' => $date,
  159. 'updated_at' => $date,
  160. ];
  161. $result = $this->postMusicCategoryRel->where('id',$request['id'])->update($create_category_rel);
  162. if (!$result){
  163. throw new HttpException(500, '音乐与分类关联-修改失败');
  164. }
  165. }
  166. DB::commit();
  167. return Response::create();
  168. }catch (QueryException $exception){
  169. DB::rollBack();
  170. return Response::create([
  171. 'message' => '修改失败,请重试',
  172. 'error' => $exception->getMessage(),
  173. 'status_code' => 500
  174. ]);
  175. }
  176. }
  177. public function musicDelete($request)
  178. {
  179. $post_music_rel = $this->postMusicCategoryRel->where('id', $request['id'])->first();
  180. DB::beginTransaction();
  181. try {
  182. $res = $post_music_rel->delete();
  183. if ($res) {
  184. $post_music = $this->postMusic->where('id',$post_music_rel->mid)->delete();
  185. if (!$post_music){
  186. throw new HttpException(500, '音乐删除失败');
  187. }
  188. }
  189. DB::commit();
  190. return Response::create();
  191. } catch (QueryException $exception) {
  192. DB::rollBack();
  193. return Response::create([
  194. 'message' => '删除失败,请重试',
  195. 'error' => $exception->getMessage(),
  196. 'status_code' => 500
  197. ]);
  198. }
  199. }
  200. public function userMusic($request)
  201. {
  202. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  203. return $this->postMusicUser->orderBy('id', 'asc')->paginate($perPage);
  204. }
  205. }