MusicPlayedService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\MusicCollectModel;
  13. use App\Models\MusicPlayedModel;
  14. /**
  15. * 佛音播放记录管理-服务类
  16. * @author wesmiler
  17. * @since 2020/11/11
  18. * Class MusicPlayedService
  19. * @package App\Services
  20. */
  21. class MusicPlayedService extends BaseService
  22. {
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * MusicPlayedService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new MusicPlayedModel();
  32. }
  33. /**
  34. * 获取列表
  35. * @return array
  36. * @since 2020/11/11
  37. * @author wesmiler
  38. */
  39. public function getList()
  40. {
  41. $params = request()->all();
  42. return parent::getList();
  43. }
  44. /**
  45. * 获取列表
  46. * @return array
  47. * @since 2020/11/11
  48. * @author wesmiler
  49. */
  50. public function getDataList($params)
  51. {
  52. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  53. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  54. $dataList = $this->model::from('music_played as a')
  55. ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
  56. ->leftJoin('musics as ms', 'ms.id', '=', 'a.source_id')
  57. ->where(function ($query) use ($params) {
  58. $query->where(['a.mark'=>1,'a.status'=> 1]);
  59. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  60. if ($userId > 0) {
  61. $query->where('a.user_id', $userId);
  62. }
  63. })
  64. ->select(['a.source_id as id', 'ms.title','ms.file_url','ms.author','ms.play_num', 'a.user_id', 'm.nickname', 'm.avatar', 'ms.thumb', 'a.status', 'a.create_time', 'a.update_time'])
  65. ->orderBy('a.create_time', 'desc')
  66. ->paginate($pageSize);
  67. $dataList = $dataList ? $dataList->toArray() : [];
  68. if ($dataList) {
  69. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  70. foreach ($dataList['data'] as &$item) {
  71. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  72. $item['file_url'] = $item['file_url'] ? get_file_url($item['file_url']) : '';
  73. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  74. $isCollect = MusicCollectModel::where(['user_id'=> $userId,'source_id'=> $item['id'],'status'=> 1,'mark'=>1])->value('id');
  75. $item['is_collect'] =$isCollect? 1 : 0;
  76. }
  77. unset($item);
  78. }
  79. return [
  80. 'code' => 0,
  81. 'success'=> true,
  82. 'msg' => '操作成功',
  83. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  84. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  85. ];
  86. }
  87. /**
  88. * 添加或编辑
  89. * @return array
  90. * @since 2020/11/11
  91. * @author wesmiler
  92. */
  93. public function edit()
  94. {
  95. $data = request()->all();
  96. $data['update_time'] = time();
  97. return parent::edit($data); // TODO: Change the autogenerated stub
  98. }
  99. /**
  100. * 添加或编辑
  101. * @return array
  102. * @since 2020/11/11
  103. * @author wesmiler
  104. */
  105. public function save($userId)
  106. {
  107. $params = request()->all();
  108. $id = isset($params['id'])? $params['id'] : 0;
  109. $status = isset($params['status'])? $params['status'] : 1;
  110. if($id<=0){
  111. return message('参数错误', false);
  112. }
  113. $info = $this->model::where(['user_id'=> $userId, 'source_id'=> $id])->select(['id','status'])->first();
  114. $count = $this->model::where(['user_id'=> $userId, 'status'=> 1])
  115. ->where('create_time','<=',time() - 7 * 86400)
  116. ->count('id');
  117. $this->model::where(['user_id'=> $userId, 'status'=> 3])
  118. ->where('update_time','<=',time() - 3 * 86400)
  119. ->delete();
  120. if($count >= 20){
  121. $this->model::where(['user_id'=> $userId, 'status'=> 1])
  122. ->where('create_time','<=',time() - 7 * 86400)
  123. ->update(['status'=> 3,'update_time'=>time()]);
  124. }
  125. // 处理
  126. if($info){
  127. $info->status = $status;
  128. $info->create_time = time();
  129. $info->update_time = time();
  130. if($info->save()){
  131. return message("操作成功", true);
  132. }
  133. }else{
  134. $data = [
  135. 'user_id'=> $userId,
  136. 'source_id'=> $id,
  137. 'create_time'=> time(),
  138. 'status'=> 1,
  139. ];
  140. if($this->model::insertGetId($data)){
  141. return message("操作成功", true);
  142. }
  143. }
  144. return message('操作失败', false);
  145. }
  146. }