作者 lyh

gx

@@ -46,41 +46,15 @@ class BlogCategoryLogic extends BaseLogic @@ -46,41 +46,15 @@ class BlogCategoryLogic extends BaseLogic
46 * @method 46 * @method
47 */ 47 */
48 public function add_blog_category(){ 48 public function add_blog_category(){
49 - $condition = [  
50 - 'name'=>$this->param['name']  
51 - ];  
52 - $info = $this->model->read($condition);  
53 - if($info !== false){  
54 - $this->fail('当前分类名称已存在');  
55 - }  
56 - $this->param['project_id'] = $this->user['project_id'];  
57 - $this->param['operator_id'] = $this->user['id'];  
58 - $this->param['create_id'] = $this->user['id'];  
59 - $this->param['created_at'] = date('Y-m-d H:i:s');  
60 - $this->param['updated_at'] = date('Y-m-d H:i:s'); 49 + //验证名称是否存在
  50 + $this->verifyParamName();
  51 + //拼接参数
  52 + $this->param = $this->addParamProcessing($this->param);
61 DB::beginTransaction(); 53 DB::beginTransaction();
62 try { 54 try {
63 $cate_id = $this->model->insertGetId($this->param); 55 $cate_id = $this->model->insertGetId($this->param);
64 - if(!isset($this->param['pid'])){  
65 - $this->param['pid'] = 0;  
66 - }  
67 - //判断为子分类时  
68 - if($this->param['pid'] != 0){  
69 - //查看当前上级分类下是否有其他子分类  
70 - $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);  
71 - if ($cate_info === false) {  
72 - //查看当前上一级分类下是否有新闻  
73 - $blogModel = new BlogModel();  
74 - $blog_count = $blogModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();  
75 - if ($blog_count > 0) {  
76 - $replacement = ','. $this->param['pid'] . ',' . $cate_id . ',';  
77 - $old = ',' . $this->param['pid'] . ',';  
78 - //更新所有商品到当前分类  
79 - DB::table('gl_Blog')->where('category_id', 'like', '%' . $old . '%')  
80 - ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);  
81 - }  
82 - }  
83 - } 56 + //处理子集
  57 + $this->addProcessingSon($cate_id);
84 RouteMap::setRoute($this->param['alias'] ?: $this->param['name'], RouteMap::SOURCE_BLOG_CATE, $cate_id, $this->user['project_id']); 58 RouteMap::setRoute($this->param['alias'] ?: $this->param['name'], RouteMap::SOURCE_BLOG_CATE, $cate_id, $this->user['project_id']);
85 DB::commit(); 59 DB::commit();
86 }catch (\Exception $e){ 60 }catch (\Exception $e){
@@ -97,15 +71,8 @@ class BlogCategoryLogic extends BaseLogic @@ -97,15 +71,8 @@ class BlogCategoryLogic extends BaseLogic
97 * @method 71 * @method
98 */ 72 */
99 public function edit_blog_category(){ 73 public function edit_blog_category(){
100 - $condition = [  
101 - 'id'=>['!=',$this->param['id']],  
102 - 'name'=>$this->param['name']  
103 - ];  
104 - //查看当前分类名称是否存在  
105 - $info = $this->model->read($condition);  
106 - if($info !== false){  
107 - $this->fail('当前分类名称已存在');  
108 - } 74 + //验证名称是否存在
  75 + $this->verifyParamName();
109 $info = $this->model->read(['id'=>$this->param['id']]); 76 $info = $this->model->read(['id'=>$this->param['id']]);
110 if($info['id'] == $this->param['pid']){ 77 if($info['id'] == $this->param['pid']){
111 $this->fail('不能成为自己的上级'); 78 $this->fail('不能成为自己的上级');
@@ -167,7 +134,7 @@ class BlogCategoryLogic extends BaseLogic @@ -167,7 +134,7 @@ class BlogCategoryLogic extends BaseLogic
167 $blogModel = new BlogModel(); 134 $blogModel = new BlogModel();
168 $rs = $blogModel->read(['category_id'=>$v],['id']); 135 $rs = $blogModel->read(['category_id'=>$v],['id']);
169 if($rs !== false){ 136 if($rs !== false){
170 - $this->response('当前分类拥有博客'); 137 + $this->response('当前分类拥有博客,不允许删除');
171 } 138 }
172 RouteMap::delRoute(RouteMap::SOURCE_BLOG_CATE, $v, $this->user['project_id']); 139 RouteMap::delRoute(RouteMap::SOURCE_BLOG_CATE, $v, $this->user['project_id']);
173 } 140 }
@@ -191,4 +158,74 @@ class BlogCategoryLogic extends BaseLogic @@ -191,4 +158,74 @@ class BlogCategoryLogic extends BaseLogic
191 $list = $this->model->list($map); 158 $list = $this->model->list($map);
192 return $this->success($list); 159 return $this->success($list);
193 } 160 }
  161 +
  162 + /**
  163 + * @name :(参数处理)paramProcessing
  164 + * @author :lyh
  165 + * @method :post
  166 + * @time :2023/6/13 11:30
  167 + */
  168 + public function addParamProcessing($param){
  169 + $param['project_id'] = $this->user['project_id'];
  170 + $param['operator_id'] = $this->user['id'];
  171 + $param['create_id'] = $this->user['id'];
  172 + $param['created_at'] = date('Y-m-d H:i:s');
  173 + $param['updated_at'] = date('Y-m-d H:i:s');
  174 + return $this->success($param);
  175 + }
  176 +
  177 + /**
  178 + * @name :(验证名称是否存在)verifyParamName
  179 + * @author :lyh
  180 + * @method :post
  181 + * @time :2023/6/13 11:41
  182 + */
  183 + public function verifyParamName(){
  184 + if(isset($this->param['id'])){
  185 + $condition = [
  186 + 'id'=>['!=',$this->param['id']],
  187 + 'name'=>$this->param['name'],
  188 + ];
  189 + }else{
  190 + $condition = [
  191 + 'name'=>$this->param['name']
  192 + ];
  193 + }
  194 + $info = $this->model->read($condition);
  195 + if($info !== false){
  196 + $this->fail('当前分类名称已存在');
  197 + }
  198 + return $this->success();
  199 + }
  200 +
  201 + /**
  202 + * @param $cate_id
  203 + * @name :(处理子集)addProcessingSon
  204 + * @author :lyh
  205 + * @method :post
  206 + * @time :2023/6/13 11:59
  207 + */
  208 + public function addProcessingSon($cate_id){
  209 + if(!isset($this->param['pid'])){
  210 + $this->param['pid'] = 0;
  211 + }
  212 + //判断为子分类时
  213 + if($this->param['pid'] != 0){
  214 + //查看当前上级分类下是否有其他子分类
  215 + $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);
  216 + if ($cate_info === false) {
  217 + //查看当前上一级分类下是否有新闻
  218 + $blogModel = new BlogModel();
  219 + $blog_count = $blogModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
  220 + if ($blog_count > 0) {
  221 + $replacement = ','. $this->param['pid'] . ',' . $cate_id . ',';
  222 + $old = ',' . $this->param['pid'] . ',';
  223 + //更新所有商品到当前分类
  224 + DB::table('gl_Blog')->where('category_id', 'like', '%' . $old . '%')
  225 + ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
  226 + }
  227 + }
  228 + }
  229 + return $this->success();
  230 + }
194 } 231 }
@@ -55,31 +55,15 @@ class NewsCategoryLogic extends BaseLogic @@ -55,31 +55,15 @@ class NewsCategoryLogic extends BaseLogic
55 * @method 55 * @method
56 */ 56 */
57 public function add_news_category(){ 57 public function add_news_category(){
  58 + //验证名称是否存在
  59 + $this->verifyParamName();
58 //参数处理 60 //参数处理
59 $this->param = $this->addParamProcessing($this->param); 61 $this->param = $this->addParamProcessing($this->param);
60 DB::beginTransaction(); 62 DB::beginTransaction();
61 try { 63 try {
62 $cate_id = $this->model->insertGetId($this->param); 64 $cate_id = $this->model->insertGetId($this->param);
63 - if(isset($this->param['pid']) && !empty($this->param['pid'])) {  
64 - $this->param['pid'] = 0;  
65 - }  
66 - //判断为子分类时  
67 - if($this->param['pid'] != 0) {  
68 - //查看当前上级分类下是否有其他分类  
69 - $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);  
70 - if ($cate_info === false) {  
71 - //查看当前上一级分类下是否有新闻  
72 - $newsModel = new NewsModel();  
73 - $news_count = $newsModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();  
74 - if ($news_count > 0) {  
75 - $replacement = ','. $this->param['pid'] .','. $cate_id . ',';  
76 - $old = ',' . $this->param['pid'] . ',';  
77 - //更新所有商品到当前分类  
78 - DB::table('gl_news')->where('category_id', 'like', '%' . $old . '%')  
79 - ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);  
80 - }  
81 - }  
82 - } 65 + //当父级分类拥有产品时,处理产品
  66 + $this->addProcessingSon($cate_id);
83 RouteMap::setRoute($this->param['alias'] ?: $this->param['name'], RouteMap::SOURCE_NEWS_CATE, $cate_id, $this->user['project_id']); 67 RouteMap::setRoute($this->param['alias'] ?: $this->param['name'], RouteMap::SOURCE_NEWS_CATE, $cate_id, $this->user['project_id']);
84 DB::commit(); 68 DB::commit();
85 }catch (\Exception $e){ 69 }catch (\Exception $e){
@@ -96,15 +80,8 @@ class NewsCategoryLogic extends BaseLogic @@ -96,15 +80,8 @@ class NewsCategoryLogic extends BaseLogic
96 * @method 80 * @method
97 */ 81 */
98 public function edit_news_category(){ 82 public function edit_news_category(){
99 - $condition = [  
100 - 'id'=>['!=',$this->param['id']],  
101 - 'name'=>$this->param['name'],  
102 - ];  
103 - //查看当前分类名称是否存在  
104 - $info = $this->model->read($condition,['id']);  
105 - if($info !== false){  
106 - $this->fail('当前分类名称已存在');  
107 - } 83 + //验证名称是否存在
  84 + $this->verifyParamName();
108 $info = $this->model->read(['id'=>$this->param['id']]); 85 $info = $this->model->read(['id'=>$this->param['id']]);
109 if($info['id'] == $this->param['pid']){ 86 if($info['id'] == $this->param['pid']){
110 $this->fail('不能成为自己的上级'); 87 $this->fail('不能成为自己的上级');
@@ -224,7 +201,27 @@ class NewsCategoryLogic extends BaseLogic @@ -224,7 +201,27 @@ class NewsCategoryLogic extends BaseLogic
224 * @method :post 201 * @method :post
225 * @time :2023/6/13 11:34 202 * @time :2023/6/13 11:34
226 */ 203 */
227 - public function addProcessingSon(){  
228 - 204 + public function addProcessingSon($cate_id){
  205 + if(isset($this->param['pid']) && !empty($this->param['pid'])) {
  206 + $this->param['pid'] = 0;
  207 + }
  208 + //判断为子分类时
  209 + if($this->param['pid'] != 0) {
  210 + //查看当前上级分类下是否有其他分类
  211 + $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]);
  212 + if ($cate_info === false) {
  213 + //查看当前上一级分类下是否有新闻
  214 + $newsModel = new NewsModel();
  215 + $news_count = $newsModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
  216 + if ($news_count > 0) {
  217 + $replacement = ','. $this->param['pid'] .','. $cate_id . ',';
  218 + $old = ',' . $this->param['pid'] . ',';
  219 + //更新所有商品到当前分类
  220 + DB::table('gl_news')->where('category_id', 'like', '%' . $old . '%')
  221 + ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
  222 + }
  223 + }
  224 + }
  225 + return $this->success();
229 } 226 }
230 } 227 }