BuddhistCollectService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\BuddhistCollectModel;
  13. /**
  14. * 佛音歌单管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class BuddhistCollectService
  18. * @package App\Services
  19. */
  20. class BuddhistCollectService extends BaseService
  21. {
  22. /**
  23. * 构造函数
  24. * @author wesmiler
  25. * @since 2020/11/11
  26. * MusicCollectService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $this->model = new BuddhistCollectModel();
  31. }
  32. /**
  33. * 获取列表
  34. * @return array
  35. * @since 2020/11/11
  36. * @author wesmiler
  37. */
  38. public function getList()
  39. {
  40. $params = request()->all();
  41. return parent::getList();
  42. }
  43. /**
  44. * 获取列表
  45. * @return array
  46. * @since 2020/11/11
  47. * @author wesmiler
  48. */
  49. public function getDataList($params)
  50. {
  51. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  52. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  53. $dataList = $this->model::from('buddhist_collect as a')
  54. ->leftJoin('buddhists as b', 'b.id', '=', 'a.source_id')
  55. ->leftJoin('buddhist_cates as bc', 'bc.id', '=', 'b.cate_id')
  56. ->where(function ($query) use ($params) {
  57. $query->where(['a.mark'=>1,'a.status'=> 1]);
  58. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  59. if ($userId > 0) {
  60. $query->where('a.user_id', $userId);
  61. }
  62. })
  63. ->select(['a.id', 'a.source_id', 'b.title','b.thumb','bc.name as cate_name','b.view_num', 'a.user_id', 'a.status', 'a.create_time', 'a.update_time'])
  64. ->orderBy('a.create_time', 'desc')
  65. ->paginate($pageSize);
  66. $dataList = $dataList ? $dataList->toArray() : [];
  67. if ($dataList) {
  68. foreach ($dataList['data'] as &$item) {
  69. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  70. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  71. }
  72. unset($item);
  73. }
  74. return [
  75. 'code' => 0,
  76. 'success'=> true,
  77. 'msg' => '操作成功',
  78. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  79. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  80. ];
  81. }
  82. /**
  83. * 添加或编辑
  84. * @return array
  85. * @since 2020/11/11
  86. * @author wesmiler
  87. */
  88. public function edit()
  89. {
  90. $data = request()->all();
  91. $data['update_time'] = time();
  92. return parent::edit($data); // TODO: Change the autogenerated stub
  93. }
  94. /**
  95. * 添加或编辑
  96. * @return array
  97. * @since 2020/11/11
  98. * @author wesmiler
  99. */
  100. public function save($userId)
  101. {
  102. $params = request()->all();
  103. $id = isset($params['id'])? $params['id'] : 0;
  104. $status = isset($params['status'])? $params['status'] : 1;
  105. if($id<=0){
  106. return message('参数错误', false);
  107. }
  108. if(!in_array($status, [1,2])){
  109. return message('参数错误', false);
  110. }
  111. $info = $this->model::where(['user_id'=> $userId, 'source_id'=> $id])->select(['id','status'])->first();
  112. if($info && $info->status == 1 && $status == 1){
  113. return message("您已收藏过", false);
  114. }else if($info && $info->status == 2 && $status == 2){
  115. return message("您已取消收藏", false);
  116. }else if(!$info && $status == 2){
  117. return message("您未收藏过", false);
  118. }
  119. // 处理
  120. if($info){
  121. $info->status = $status;
  122. $info->create_time = time();
  123. if($info->save()){
  124. return message($status == 1? "收藏成功":"取消收藏成功", true);
  125. }
  126. }else{
  127. $data = [
  128. 'user_id'=> $userId,
  129. 'source_id'=> $id,
  130. 'create_time'=> time(),
  131. 'status'=> 1,
  132. ];
  133. if($this->model::insertGetId($data)){
  134. return message("收藏成功", true);
  135. }
  136. }
  137. return message('操作失败', false);
  138. }
  139. }