TopicRepository.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-10
  6. * Time: 18:12
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Category;
  10. use App\Models\CategoryTopic;
  11. use App\Models\MemberFollowTopic;
  12. use App\Models\Topic;
  13. use Dingo\Api\Http\Response;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Database\QueryException;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Redis;
  18. class TopicRepository {
  19. public function __construct(Topic $topic,CategoryTopic $categoryTopic,MemberFollowTopic $memberFollowTopic){
  20. $this->topic = $topic;
  21. $this->categoryTopic = $categoryTopic;
  22. $this->memberFollowTopic = $memberFollowTopic;
  23. }
  24. //列表
  25. public function index($request){
  26. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  27. $where = [];
  28. if(isset($request['name'])){
  29. $where[] = ['name', 'like', "%{$request['name']}%"];
  30. }
  31. if(isset($request['topic_status'])){
  32. if($request['topic_status'] == 'is_suggest'){
  33. $where[] = ['is_suggest', 1];
  34. }
  35. if( $request['topic_status'] == 'is_hot'){
  36. $where[] = ['is_hot', 1];
  37. }
  38. if($request['topic_status'] == 'is_open'){
  39. $where[] = ['is_open', 1];
  40. }
  41. if($request['topic_status'] == 'is_close'){
  42. $where[] = ['is_open', 0];
  43. }
  44. }
  45. return $this->topic
  46. ->where($where)
  47. ->orderBy('is_hot','desc')
  48. ->orderBy('is_suggest','desc')
  49. ->orderBy('is_open','desc')
  50. ->orderBy('id','desc')
  51. ->paginate($perPage);
  52. }
  53. //新增
  54. public function create($request){
  55. $topic = $this->topic->where(['name'=>$request['name']])->first();
  56. if($topic){
  57. return Response::create([
  58. 'message' => '该话题已存在',
  59. 'status_code' => 500
  60. ]);
  61. }
  62. $data = [
  63. 'name' => $request['name'],
  64. 'img' => $request['img'],
  65. 'desc' => $request['desc'],
  66. ];
  67. if(!empty($request['base_count'])){
  68. $data['base_count'] = $request['base_count'];
  69. }
  70. if(isset($request['category_ids'])){
  71. $category_ids = explode(',', $request['category_ids']);
  72. }
  73. DB::beginTransaction();
  74. try{
  75. $topicInfo = $this->topic->create($data);
  76. if($topicInfo){
  77. if($category_ids){
  78. $category_topic_data = [];
  79. foreach($category_ids as $value){
  80. $category_topic_data[] = [
  81. 'category_id' => $value,
  82. 'topic_id' => $topicInfo->id,
  83. ];
  84. }
  85. $this->categoryTopic->insert($category_topic_data);
  86. }
  87. }
  88. DB::commit();
  89. Redis::zadd('topic.name', $topicInfo->id, $topicInfo->name);
  90. return Response::create();
  91. }catch (QueryException $exception){
  92. DB::rollBack();
  93. Log::debug('新增话题:'.$exception->getMessage());
  94. return Response::create([
  95. 'message' => '新增话题,请重试',
  96. 'error' => $exception->getMessage(),
  97. 'status_code' => 500
  98. ]);
  99. }
  100. }
  101. public function edit($request){
  102. $topic = $this->topic->where(['id'=>$request['id']])->first();
  103. if(!$topic){
  104. return Response::create([
  105. 'message' => '该话题不存在',
  106. 'status_code' => 500
  107. ]);
  108. }
  109. $oldName = $topic->name;
  110. $_topic = $this->topic->where(['name'=>$request['name']])->where('id','<>',$topic->id)->first();
  111. if($_topic){
  112. return Response::create([
  113. 'message' => '该话题已存在',
  114. 'status_code' => 500
  115. ]);
  116. }
  117. $topic->name = $request['name'];
  118. $topic->img = $request['img'];
  119. $topic->desc = $request['desc'];
  120. if(!empty($request['base_count'])){
  121. $topic->base_count = $request['base_count'];
  122. }
  123. $category_ids = [];
  124. if(!empty($request['category_ids'])){
  125. $category_ids = explode(',', $request['category_ids']);
  126. }
  127. DB::beginTransaction();
  128. try{
  129. //保存
  130. $topicInfo = $topic->save();
  131. //删除原来关联关系
  132. $this->categoryTopic->where(['topic_id'=>$topic->id])->delete();
  133. if($topicInfo){
  134. if($category_ids){
  135. $category_topic_data = [];
  136. foreach($category_ids as $value){
  137. $category_topic_data[] = [
  138. 'category_id' => $value,
  139. 'topic_id' => $topic->id,
  140. ];
  141. }
  142. $this->categoryTopic->insert($category_topic_data);
  143. }
  144. }
  145. DB::commit();
  146. if($oldName != $topic->name){
  147. Redis::zrem('topic.name', $oldName);
  148. Redis::zadd('topic.name', $topic->id, $topic->name);
  149. }
  150. return Response::create();
  151. }catch (QueryException $exception){
  152. DB::rollBack();
  153. Log::debug('编辑话题:'.$exception->getMessage());
  154. return Response::create([
  155. 'message' => '编辑话题,请重试',
  156. 'error' => $exception->getMessage(),
  157. 'status_code' => 500
  158. ]);
  159. }
  160. }
  161. //修改
  162. public function update($request){
  163. $topic = $this->topic->where('id', $request['id'])->first();
  164. if(!$topic){
  165. return Response::create([
  166. 'message' => '该话题不存在',
  167. 'status_code' => 500
  168. ]);
  169. }
  170. if(isset($request['is_open']) && $request['is_open'] !== null){
  171. $topic->is_open = $request['is_open'];
  172. }
  173. if(!empty($request['type'])){
  174. if($topic->is_open == 0){
  175. return Response::create([
  176. 'message' => '该话题未开启',
  177. 'status_code' => 500
  178. ]);
  179. }
  180. if(isset($request['type']) && $request['type'] == 1){
  181. $topic->is_suggest = $request['status'];
  182. }
  183. if(isset($request['type']) && $request['type'] == 2){
  184. $topic->is_hot = $request['status'];
  185. }
  186. }
  187. $res = $topic->save();
  188. if($res){
  189. return Response::create();
  190. }
  191. }
  192. //详情
  193. public function view($request){
  194. return $this->topic->where('id', $request['id'])->first();
  195. }
  196. //获取多个话题
  197. public function getTopics($ids = []){
  198. return $this->topic->whereIn('id', $ids)->select('id','name')->get();
  199. }
  200. public function getMemberTopics($uid){
  201. return $this->memberFollowTopic
  202. ->leftJoin('topic','topic.id','=','member_follow_topic.topic_id')
  203. ->where('member_follow_topic.uid',$uid)
  204. ->select('topic.id','topic.name')
  205. ->get();
  206. }
  207. /**
  208. * 重置话题redis
  209. */
  210. public function resetRedis()
  211. {
  212. $TopicName = $this->topic->pluck('id', 'name')->toArray();
  213. $res = Redis::zadd('topic.name', $TopicName);
  214. if($res){
  215. return Response::create([
  216. 'message' => '重置话题成功',
  217. 'status_code' => 500
  218. ]);
  219. }else{
  220. return Response::create([
  221. 'message' => '重置话题失败',
  222. 'status_code' => 500
  223. ]);
  224. }
  225. }
  226. }