BlogCategoryLogic.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace App\Http\Logic\Bside\Blog;
use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Http\Logic\Bside\User\UserLogic;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\Blog\Blog as BlogModel;
use App\Models\News\NewsCategory as NewsCategoryModel;
use Illuminate\Support\Facades\DB;
class BlogCategoryLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new BlogCategoryModel();
$this->param = $this->requestAll;
}
/**
* @param $v
* @name :获取分类名称
* @return void
* @author :liyuhang
* @method
*/
public function get_category_name($v){
//获取用户已读还是未读
$category_info = $this->model->list(['id'=>['in',explode(',',trim($v['category_id'],','))]],'id',['name']);
$str = '';
foreach ($category_info as $v1){
$str .= $v1['name'].',';
}
$v['category_name'] = trim($str,',');
return $this->success($v);
}
/**
* @name :添加时验证上级分类是否有商品,有则替换带当前分类下
* @return void
* @author :liyuhang
* @method
*/
public function add_blog_category(){
$condition = [
'name'=>$this->param['name']
];
$info = $this->model->read($condition);
if($info !== false){
$this->fail('当前分类名称已存在');
}
$this->param['project_id'] = $this->user['project_id'];
$this->param['operator_id'] = $this->user['id'];
$this->param['create_id'] = $this->user['id'];
$this->param['created_at'] = date('Y-m-d H:i:s');
$this->param['updated_at'] = date('Y-m-d H:i:s');
DB::beginTransaction();
try {
$cate_id = $this->model->insertGetId($this->param);
//判断为子分类时
if(isset($this->param['pid']) && !empty($this->param['pid'])) {
//查看当前上级分类下是否有其他分类
$cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);
if ($cate_info === false) {
//查看当前上一级分类下是否有新闻
$blogModel = new BlogModel();
$blog_count = $blogModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
if ($blog_count > 0) {
$replacement = ','. $this->param['pid'] . ',' . $cate_id . ',';
$old = ',' . $this->param['pid'] . ',';
//更新所有商品到当前分类
DB::table('gl_Blog')->where('category_id', 'like', '%' . $old . '%')
->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
}
}
}
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->fail('error');
}
return $this->success();
}
/**
* @name :编辑分类
* @return void
* @author :liyuhang
* @method
*/
public function edit_blog_category(){
$condition = [
'id'=>['!=',$this->param['id']],
'name'=>$this->param['name']
];
//查看当前分类名称是否存在
$info = $this->model->read($condition);
if($info !== false){
$this->fail('当前分类名称已存在');
}
$this->param['operator_id'] = $this->user['id'];
$this->edit($this->param,$this->param['id']);
return $this->success();
}
/**
* @name :详情
* @return array
* @author :liyuhang
* @method
*/
public function info_blog_category(){
$info = $this->info($this->param);
return $this->success($info);
}
/**
* @name :编辑状态与排序
* @return void
* @author :liyuhang
* @method
*/
public function status_blog_category(){
$this->param['operator_id'] = $this->user['id'];
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* @name :删除博客分类
* @return void
* @author :liyuhang
* @method
*/
public function del_blog_category(){
$ids = $this->param['id'];
foreach ($ids as $v){
//查询是否有子分类
$rs = $this->model->read(['pid'=>$v],['id']);
if($rs !== false){
$this->response('当前分类拥有子分类不允许删除');
}
//查看当前分内下是否有博客
$blogModel = new BlogModel();
$rs = $blogModel->read(['category_id'=>$v],['id']);
if($rs !== false){
$this->response('当前分类拥有博客');
}
}
$this->param['id'] = ['in',$this->param['id']];
$this->del($this->param,$ids);
return $this->success();
}
}