|
@@ -18,49 +18,55 @@ use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
-class TopicRepository {
|
|
|
|
- public function __construct(Topic $topic,CategoryTopic $categoryTopic,MemberFollowTopic $memberFollowTopic){
|
|
|
|
|
|
+class TopicRepository
|
|
|
|
+{
|
|
|
|
+ public function __construct(Topic $topic, CategoryTopic $categoryTopic, MemberFollowTopic $memberFollowTopic)
|
|
|
|
+ {
|
|
$this->topic = $topic;
|
|
$this->topic = $topic;
|
|
$this->categoryTopic = $categoryTopic;
|
|
$this->categoryTopic = $categoryTopic;
|
|
$this->memberFollowTopic = $memberFollowTopic;
|
|
$this->memberFollowTopic = $memberFollowTopic;
|
|
}
|
|
}
|
|
|
|
+
|
|
//列表
|
|
//列表
|
|
- public function index($request){
|
|
|
|
|
|
+ public function index($request)
|
|
|
|
+ {
|
|
$perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
$perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
$where = [];
|
|
$where = [];
|
|
- if(isset($request['name'])){
|
|
|
|
|
|
+ if (isset($request['name'])) {
|
|
$where[] = ['name', 'like', "%{$request['name']}%"];
|
|
$where[] = ['name', 'like', "%{$request['name']}%"];
|
|
}
|
|
}
|
|
- if(isset($request['topic_status'])){
|
|
|
|
- if($request['topic_status'] == 'is_suggest'){
|
|
|
|
|
|
+ if (isset($request['topic_status'])) {
|
|
|
|
+ if ($request['topic_status'] == 'is_suggest') {
|
|
$where[] = ['is_suggest', 1];
|
|
$where[] = ['is_suggest', 1];
|
|
}
|
|
}
|
|
- if( $request['topic_status'] == 'is_hot'){
|
|
|
|
|
|
+ if ($request['topic_status'] == 'is_hot') {
|
|
$where[] = ['is_hot', 1];
|
|
$where[] = ['is_hot', 1];
|
|
}
|
|
}
|
|
- if($request['topic_status'] == 'is_open'){
|
|
|
|
|
|
+ if ($request['topic_status'] == 'is_open') {
|
|
$where[] = ['is_open', 1];
|
|
$where[] = ['is_open', 1];
|
|
}
|
|
}
|
|
- if($request['topic_status'] == 'is_close'){
|
|
|
|
|
|
+ if ($request['topic_status'] == 'is_close') {
|
|
$where[] = ['is_open', 0];
|
|
$where[] = ['is_open', 0];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $this->topic
|
|
return $this->topic
|
|
->where($where)
|
|
->where($where)
|
|
- ->orderBy('is_hot','desc')
|
|
|
|
- ->orderBy('is_suggest','desc')
|
|
|
|
- ->orderBy('is_open','desc')
|
|
|
|
- ->orderBy('id','desc')
|
|
|
|
|
|
+ ->orderBy('is_hot', 'desc')
|
|
|
|
+ ->orderBy('is_suggest', 'desc')
|
|
|
|
+ ->orderBy('is_open', 'desc')
|
|
|
|
+ ->orderBy('id', 'desc')
|
|
->paginate($perPage);
|
|
->paginate($perPage);
|
|
}
|
|
}
|
|
|
|
+
|
|
//新增
|
|
//新增
|
|
- public function create($request){
|
|
|
|
- $topic = $this->topic->where(['name'=>$request['name']])->first();
|
|
|
|
- if($topic){
|
|
|
|
|
|
+ public function create($request)
|
|
|
|
+ {
|
|
|
|
+ $topic = $this->topic->where(['name' => $request['name']])->first();
|
|
|
|
+ if ($topic) {
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '该话题已存在',
|
|
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'message' => '该话题已存在',
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -68,20 +74,21 @@ class TopicRepository {
|
|
'name' => $request['name'],
|
|
'name' => $request['name'],
|
|
'img' => $request['img'],
|
|
'img' => $request['img'],
|
|
'desc' => $request['desc'],
|
|
'desc' => $request['desc'],
|
|
|
|
+ 'circle_id' => $request['desc'] ?? 0,
|
|
];
|
|
];
|
|
- if(!empty($request['base_count'])){
|
|
|
|
|
|
+ if (!empty($request['base_count'])) {
|
|
$data['base_count'] = $request['base_count'];
|
|
$data['base_count'] = $request['base_count'];
|
|
}
|
|
}
|
|
- if(isset($request['category_ids'])){
|
|
|
|
|
|
+ if (isset($request['category_ids'])) {
|
|
$category_ids = explode(',', $request['category_ids']);
|
|
$category_ids = explode(',', $request['category_ids']);
|
|
}
|
|
}
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
- try{
|
|
|
|
|
|
+ try {
|
|
$topicInfo = $this->topic->create($data);
|
|
$topicInfo = $this->topic->create($data);
|
|
- if($topicInfo){
|
|
|
|
- if($category_ids){
|
|
|
|
|
|
+ if ($topicInfo) {
|
|
|
|
+ if ($category_ids) {
|
|
$category_topic_data = [];
|
|
$category_topic_data = [];
|
|
- foreach($category_ids as $value){
|
|
|
|
|
|
+ foreach ($category_ids as $value) {
|
|
$category_topic_data[] = [
|
|
$category_topic_data[] = [
|
|
'category_id' => $value,
|
|
'category_id' => $value,
|
|
'topic_id' => $topicInfo->id,
|
|
'topic_id' => $topicInfo->id,
|
|
@@ -93,54 +100,56 @@ class TopicRepository {
|
|
DB::commit();
|
|
DB::commit();
|
|
Redis::zadd('topic.name', $topicInfo->id, $topicInfo->name);
|
|
Redis::zadd('topic.name', $topicInfo->id, $topicInfo->name);
|
|
return Response::create();
|
|
return Response::create();
|
|
- }catch (QueryException $exception){
|
|
|
|
|
|
+ } catch (QueryException $exception) {
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
- Log::debug('新增话题:'.$exception->getMessage());
|
|
|
|
|
|
+ Log::debug('新增话题:' . $exception->getMessage());
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '新增话题,请重试',
|
|
|
|
|
|
+ 'message' => '新增话题,请重试',
|
|
'error' => $exception->getMessage(),
|
|
'error' => $exception->getMessage(),
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public function edit($request){
|
|
|
|
- $topic = $this->topic->where(['id'=>$request['id']])->first();
|
|
|
|
- if(!$topic){
|
|
|
|
|
|
+ public function edit($request)
|
|
|
|
+ {
|
|
|
|
+ $topic = $this->topic->where(['id' => $request['id']])->first();
|
|
|
|
+ if (!$topic) {
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '该话题不存在',
|
|
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'message' => '该话题不存在',
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
$oldName = $topic->name;
|
|
$oldName = $topic->name;
|
|
- $_topic = $this->topic->where(['name'=>$request['name']])->where('id','<>',$topic->id)->first();
|
|
|
|
- if($_topic){
|
|
|
|
|
|
+ $_topic = $this->topic->where(['name' => $request['name']])->where('id', '<>', $topic->id)->first();
|
|
|
|
+ if ($_topic) {
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '该话题已存在',
|
|
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'message' => '该话题已存在',
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
$topic->name = $request['name'];
|
|
$topic->name = $request['name'];
|
|
$topic->img = $request['img'];
|
|
$topic->img = $request['img'];
|
|
$topic->desc = $request['desc'];
|
|
$topic->desc = $request['desc'];
|
|
- if(!empty($request['base_count'])){
|
|
|
|
|
|
+ $topic->circle_id = $request['circle_id'] ?? 0;
|
|
|
|
+ if (!empty($request['base_count'])) {
|
|
$topic->base_count = $request['base_count'];
|
|
$topic->base_count = $request['base_count'];
|
|
}
|
|
}
|
|
$category_ids = [];
|
|
$category_ids = [];
|
|
- if(!empty($request['category_ids'])){
|
|
|
|
|
|
+ if (!empty($request['category_ids'])) {
|
|
$category_ids = explode(',', $request['category_ids']);
|
|
$category_ids = explode(',', $request['category_ids']);
|
|
}
|
|
}
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
- try{
|
|
|
|
|
|
+ try {
|
|
//保存
|
|
//保存
|
|
$topicInfo = $topic->save();
|
|
$topicInfo = $topic->save();
|
|
//删除原来关联关系
|
|
//删除原来关联关系
|
|
- $this->categoryTopic->where(['topic_id'=>$topic->id])->delete();
|
|
|
|
- if($topicInfo){
|
|
|
|
- if($category_ids){
|
|
|
|
|
|
+ $this->categoryTopic->where(['topic_id' => $topic->id])->delete();
|
|
|
|
+ if ($topicInfo) {
|
|
|
|
+ if ($category_ids) {
|
|
$category_topic_data = [];
|
|
$category_topic_data = [];
|
|
- foreach($category_ids as $value){
|
|
|
|
|
|
+ foreach ($category_ids as $value) {
|
|
$category_topic_data[] = [
|
|
$category_topic_data[] = [
|
|
'category_id' => $value,
|
|
'category_id' => $value,
|
|
'topic_id' => $topic->id,
|
|
'topic_id' => $topic->id,
|
|
@@ -150,68 +159,77 @@ class TopicRepository {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
DB::commit();
|
|
DB::commit();
|
|
- if($oldName != $topic->name){
|
|
|
|
|
|
+ if ($oldName != $topic->name) {
|
|
Redis::zrem('topic.name', $oldName);
|
|
Redis::zrem('topic.name', $oldName);
|
|
Redis::zadd('topic.name', $topic->id, $topic->name);
|
|
Redis::zadd('topic.name', $topic->id, $topic->name);
|
|
}
|
|
}
|
|
return Response::create();
|
|
return Response::create();
|
|
- }catch (QueryException $exception){
|
|
|
|
|
|
+ } catch (QueryException $exception) {
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
- Log::debug('编辑话题:'.$exception->getMessage());
|
|
|
|
|
|
+ Log::debug('编辑话题:' . $exception->getMessage());
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '编辑话题,请重试',
|
|
|
|
|
|
+ 'message' => '编辑话题,请重试',
|
|
'error' => $exception->getMessage(),
|
|
'error' => $exception->getMessage(),
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
//修改
|
|
//修改
|
|
- public function update($request){
|
|
|
|
|
|
+ public function update($request)
|
|
|
|
+ {
|
|
$topic = $this->topic->where('id', $request['id'])->first();
|
|
$topic = $this->topic->where('id', $request['id'])->first();
|
|
- if(!$topic){
|
|
|
|
|
|
+ if (!$topic) {
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '该话题不存在',
|
|
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'message' => '该话题不存在',
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
- if(isset($request['is_open']) && $request['is_open'] !== null){
|
|
|
|
|
|
+ if (isset($request['is_open']) && $request['is_open'] !== null) {
|
|
$topic->is_open = $request['is_open'];
|
|
$topic->is_open = $request['is_open'];
|
|
}
|
|
}
|
|
- if(!empty($request['type'])){
|
|
|
|
- if($topic->is_open == 0){
|
|
|
|
|
|
+ if (!empty($request['type'])) {
|
|
|
|
+ if ($topic->is_open == 0) {
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '该话题未开启',
|
|
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'message' => '该话题未开启',
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
- if(isset($request['type']) && $request['type'] == 1){
|
|
|
|
|
|
+ if (isset($request['type']) && $request['type'] == 1) {
|
|
$topic->is_suggest = $request['status'];
|
|
$topic->is_suggest = $request['status'];
|
|
}
|
|
}
|
|
- if(isset($request['type']) && $request['type'] == 2){
|
|
|
|
|
|
+ if (isset($request['type']) && $request['type'] == 2) {
|
|
$topic->is_hot = $request['status'];
|
|
$topic->is_hot = $request['status'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$res = $topic->save();
|
|
$res = $topic->save();
|
|
- if($res){
|
|
|
|
|
|
+ if ($res) {
|
|
return Response::create();
|
|
return Response::create();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
//详情
|
|
//详情
|
|
- public function view($request){
|
|
|
|
|
|
+ public function view($request)
|
|
|
|
+ {
|
|
return $this->topic->where('id', $request['id'])->first();
|
|
return $this->topic->where('id', $request['id'])->first();
|
|
}
|
|
}
|
|
|
|
+
|
|
//获取多个话题
|
|
//获取多个话题
|
|
- public function getTopics($ids = []){
|
|
|
|
- return $this->topic->whereIn('id', $ids)->select('id','name')->get();
|
|
|
|
|
|
+ public function getTopics($ids = [])
|
|
|
|
+ {
|
|
|
|
+ return $this->topic->whereIn('id', $ids)->select('id', 'name')->get();
|
|
}
|
|
}
|
|
- public function getMemberTopics($uid){
|
|
|
|
- return $this->memberFollowTopic
|
|
|
|
- ->leftJoin('topic','topic.id','=','member_follow_topic.topic_id')
|
|
|
|
- ->where('member_follow_topic.uid',$uid)
|
|
|
|
- ->select('topic.id','topic.name')
|
|
|
|
|
|
+
|
|
|
|
+ public function getMemberTopics($uid)
|
|
|
|
+ {
|
|
|
|
+ return $this->memberFollowTopic
|
|
|
|
+ ->leftJoin('topic', 'topic.id', '=', 'member_follow_topic.topic_id')
|
|
|
|
+ ->where('member_follow_topic.uid', $uid)
|
|
|
|
+ ->select('topic.id', 'topic.name')
|
|
->get();
|
|
->get();
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 重置话题redis
|
|
* 重置话题redis
|
|
*/
|
|
*/
|
|
@@ -220,15 +238,15 @@ class TopicRepository {
|
|
$TopicName = $this->topic->pluck('id', 'name')->toArray();
|
|
$TopicName = $this->topic->pluck('id', 'name')->toArray();
|
|
|
|
|
|
$res = Redis::zadd('topic.name', $TopicName);
|
|
$res = Redis::zadd('topic.name', $TopicName);
|
|
- if($res){
|
|
|
|
|
|
+ if ($res) {
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '重置话题成功',
|
|
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'message' => '重置话题成功',
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
return Response::create([
|
|
return Response::create([
|
|
- 'message' => '重置话题失败',
|
|
|
|
- 'status_code' => 500
|
|
|
|
|
|
+ 'message' => '重置话题失败',
|
|
|
|
+ 'status_code' => 500
|
|
]);
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|