作者 lyh

gx

@@ -28,17 +28,11 @@ class BlogCategoryLogic extends BaseLogic @@ -28,17 +28,11 @@ class BlogCategoryLogic extends BaseLogic
28 public function categorySave(){ 28 public function categorySave(){
29 //验证名称是否存在 29 //验证名称是否存在
30 $this->verifyParamName($this->param['name']); 30 $this->verifyParamName($this->param['name']);
31 - if(isset($this->param['id']) && !empty($this->param['id'])) {  
32 - //验证参数是否可编辑  
33 - $this->verifyParamEdit($this->param['id'], $this->param['pid']);  
34 - }  
35 DB::beginTransaction(); 31 DB::beginTransaction();
36 try { 32 try {
37 if(isset($this->param['id']) && !empty($this->param['id'])){ 33 if(isset($this->param['id']) && !empty($this->param['id'])){
38 //查看路由是否更新 34 //查看路由是否更新
39 - $id = $this->editCategoryRoute($this->param['id'],  
40 - isset($this->param['alias']) ?? RouteMap::setRoute(isset($this->param['alias']) ? $this->param['alias'] : $this->param['name'],  
41 - RouteMap::SOURCE_BLOG_CATE, $this->param['id'], $this->user['project_id'])); 35 + $id = $this->editCategoryRoute($this->param['id'], $this->param['alias']);
42 $this->param['operator_id'] = $this->user['id']; 36 $this->param['operator_id'] = $this->user['id'];
43 $this->edit($this->param,['id'=>$this->param['id']]); 37 $this->edit($this->param,['id'=>$this->param['id']]);
44 }else{ 38 }else{
@@ -62,6 +56,46 @@ class BlogCategoryLogic extends BaseLogic @@ -62,6 +56,46 @@ class BlogCategoryLogic extends BaseLogic
62 } 56 }
63 57
64 /** 58 /**
  59 + * @remark :编辑分类,处理博客数据
  60 + * @name :editCategory
  61 + * @author :lyh
  62 + * @method :post
  63 + * @time :2023/10/20 9:32
  64 + */
  65 + public function editHandleCategory($id,$pid){
  66 + $info = $this->model->read(['id'=>$id],['id','pid']);
  67 + if($info['pid'] != $pid){
  68 + //修改勒上级,先查看上级是否拥有博客
  69 + $blogModel = new BlogModel();
  70 + $blogList = $blogModel->list(['category_id'=>['like','%,'.$pid.',%']],['id']);
  71 + //随机获取最后一级id
  72 + $replacement = $this->getLastId($id);
  73 + if(!empty($blogList)){
  74 + //存在博客时,移动所有博客到当前分类最后一级
  75 + $blogModel->where('category_id', 'like', '%,' . $pid . ',%')
  76 + ->update(['category_id' => DB::raw("REPLACE(category_id, '$pid', '$replacement')")]);
  77 + }
  78 + }
  79 + return $this->success();
  80 + }
  81 +
  82 + /**
  83 + * @remark :随机获取当前id下最后一级的id
  84 + * @name :getLastId
  85 + * @author :lyh
  86 + * @method :post
  87 + * @time :2023/10/20 9:45
  88 + */
  89 + public function getLastId($id){
  90 + $info = $this->model->read(['pid'=>$id],['id']);
  91 + if($info !== false){
  92 + $this->getLastId($id);
  93 + }else{
  94 + return $id;
  95 + }
  96 + }
  97 +
  98 + /**
65 * @remark :编辑路由时生成路由记录 99 * @remark :编辑路由时生成路由记录
66 * @name :editCategoryRoute 100 * @name :editCategoryRoute
67 * @author :lyh 101 * @author :lyh
@@ -106,6 +140,7 @@ class BlogCategoryLogic extends BaseLogic @@ -106,6 +140,7 @@ class BlogCategoryLogic extends BaseLogic
106 return $this->success(); 140 return $this->success();
107 } 141 }
108 142
  143 +
109 /** 144 /**
110 * @name :详情 145 * @name :详情
111 * @return array 146 * @return array
@@ -271,7 +306,7 @@ class BlogCategoryLogic extends BaseLogic @@ -271,7 +306,7 @@ class BlogCategoryLogic extends BaseLogic
271 $blogModel = new BlogModel(); 306 $blogModel = new BlogModel();
272 $blog_count = $blogModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count(); 307 $blog_count = $blogModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
273 if ($blog_count > 0) { 308 if ($blog_count > 0) {
274 - $replacement = ','. $this->param['pid'] . ',' . $cate_id . ','; 309 + $replacement = ',' . $cate_id . ',';
275 $old = ',' . $this->param['pid'] . ','; 310 $old = ',' . $this->param['pid'] . ',';
276 //更新所有商品到当前分类 311 //更新所有商品到当前分类
277 $blogModel->where('category_id', 'like', '%' . $old . '%') 312 $blogModel->where('category_id', 'like', '%' . $old . '%')
@@ -102,7 +102,12 @@ class BlogLogic extends BaseLogic @@ -102,7 +102,12 @@ class BlogLogic extends BaseLogic
102 if($info === false){ 102 if($info === false){
103 $this->fail('error'); 103 $this->fail('error');
104 } 104 }
105 - $info['category_id'] = explode(',',trim($info['category_id'],',')); 105 + $category = explode(',',trim($info['category_id'],','));
  106 + $str = [];
  107 + foreach ($category as $v){
  108 + $this->getAllFather($v,$str);
  109 + }
  110 + $info['category_id'] = array_unique($str);
106 //获取标签名称 111 //获取标签名称
107 $blogLabelLogic = new BlogLabelLogic(); 112 $blogLabelLogic = new BlogLabelLogic();
108 $info['label_name'] = $blogLabelLogic->getLabelName($info['label_id']); 113 $info['label_name'] = $blogLabelLogic->getLabelName($info['label_id']);
@@ -111,6 +116,22 @@ class BlogLogic extends BaseLogic @@ -111,6 +116,22 @@ class BlogLogic extends BaseLogic
111 } 116 }
112 117
113 /** 118 /**
  119 + * @remark :获取分类的所有上级
  120 + * @name :getAllFather
  121 + * @author :lyh
  122 + * @method :post
  123 + * @time :2023/10/20 9:08
  124 + */
  125 + public function getAllFather($id,&$str = []){
  126 + $cateModel = new BlogCategoryModel();
  127 + $info = $cateModel->read(['id'=>$id],['id','pid']);
  128 + if($info !== false){
  129 + $str[] = $id;
  130 + $this->getAllFather($info['pid'],$str);
  131 + }
  132 + }
  133 +
  134 + /**
114 * @name :修改状态 135 * @name :修改状态
115 * @return array 136 * @return array
116 * @throws \App\Exceptions\BsideGlobalException 137 * @throws \App\Exceptions\BsideGlobalException
@@ -179,22 +200,39 @@ class BlogLogic extends BaseLogic @@ -179,22 +200,39 @@ class BlogLogic extends BaseLogic
179 if(isset($this->param['id'])){ 200 if(isset($this->param['id'])){
180 $param['operator_id'] = $this->user['id']; 201 $param['operator_id'] = $this->user['id'];
181 if(isset($param['category_id']) && !empty($param['category_id'])){ 202 if(isset($param['category_id']) && !empty($param['category_id'])){
182 - $param['category_id'] = implode(',',$param['category_id']);  
183 - $param['category_id'] = ','.trim($param['category_id'],',').','; 203 + $param['category_id'] = $this->getLastCategory($param['category_id']);
184 } 204 }
185 }else{ 205 }else{
186 $param['create_id'] = $this->user['id']; 206 $param['create_id'] = $this->user['id'];
187 $param['operator_id'] = $this->user['id']; 207 $param['operator_id'] = $this->user['id'];
188 $param['project_id'] = $this->user['project_id']; 208 $param['project_id'] = $this->user['project_id'];
189 if(isset($param['category_id']) && !empty($param['category_id'])){ 209 if(isset($param['category_id']) && !empty($param['category_id'])){
190 - $param['category_id'] = implode(',',$param['category_id']);  
191 - $param['category_id'] = ','.$param['category_id'].','; 210 + $param['category_id'] = $this->getLastCategory($param['category_id']);
192 } 211 }
193 } 212 }
194 return $this->success($param); 213 return $this->success($param);
195 } 214 }
196 215
197 /** 216 /**
  217 + * @remark :获取最后一级分类id
  218 + * @name :getLastCategory
  219 + * @author :lyh
  220 + * @method :post
  221 + * @time :2023/10/20 9:02
  222 + */
  223 + public function getLastCategory($category){
  224 + $str = '';
  225 + $cateModel = new BlogCategoryModel();
  226 + foreach ($category as $v){
  227 + $info = $cateModel->read(['pid'=>$v]);
  228 + if($info === false){
  229 + $str .= $v.',';
  230 + }
  231 + }
  232 + return trim($str,',');
  233 + }
  234 +
  235 + /**
198 * @remark :根据状态获取数量 236 * @remark :根据状态获取数量
199 * @name :getStatusNumber 237 * @name :getStatusNumber
200 * @author :lyh 238 * @author :lyh