正在显示
4 个修改的文件
包含
78 行增加
和
30 行删除
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | namespace App\Http\Controllers\Bside\Blog; | 3 | namespace App\Http\Controllers\Bside\Blog; |
| 4 | 4 | ||
| 5 | use App\Http\Controllers\Bside\BaseController; | 5 | use App\Http\Controllers\Bside\BaseController; |
| 6 | +use App\Http\Logic\Bside\Blog\BlogCategoryLogic; | ||
| 6 | use App\Http\Requests\Bside\Blog\BlogCategoryRequest; | 7 | use App\Http\Requests\Bside\Blog\BlogCategoryRequest; |
| 7 | use App\Models\Blog\Blog as BlogModel; | 8 | use App\Models\Blog\Blog as BlogModel; |
| 8 | use App\Models\Blog\BlogCategory as BlogCategoryModel; | 9 | use App\Models\Blog\BlogCategory as BlogCategoryModel; |
| @@ -46,36 +47,10 @@ class BlogCategoryController extends BaseController | @@ -46,36 +47,10 @@ class BlogCategoryController extends BaseController | ||
| 46 | * @author :liyuhang | 47 | * @author :liyuhang |
| 47 | * @method | 48 | * @method |
| 48 | */ | 49 | */ |
| 49 | - public function add(BlogCategoryRequest $request,BlogCategoryModel $blogCategoryModel,BlogModel $blogModel){ | 50 | + public function add(BlogCategoryRequest $request,BlogCategoryLogic $blogCategoryLogic){ |
| 50 | $request->validated(); | 51 | $request->validated(); |
| 51 | - $this->param['project_id'] = $this->user['project_id']; | ||
| 52 | - $this->param['operator_id'] = $this->uid; | ||
| 53 | - $this->param['create_id'] = $this->uid; | ||
| 54 | - DB::beginTransaction(); | ||
| 55 | - $rs = $blogCategoryModel->add($this->param); | ||
| 56 | - if($rs === false){ | ||
| 57 | - DB::rollBack(); | ||
| 58 | - $this->response('error',Code::USER_ERROR); | ||
| 59 | - } | ||
| 60 | - //TODO::判断当前分内是否为一级分类 | ||
| 61 | - if(isset($this->param['pid']) && !empty($this->param['pid'])){ | ||
| 62 | - //查看当前上级分类下是否有其他分类 | ||
| 63 | - $cate_info = $blogCategoryModel->read(['pid'=>$this->param['pid'],'id'=>['!=',$blogCategoryModel->id]]); | ||
| 64 | - if($cate_info === false){ | ||
| 65 | - //查看当前上一级分类下是否有商品 | ||
| 66 | - $news_info = $blogModel->read(['category_id'=>$this->param['pid'],'pid'=>0]); | ||
| 67 | - if($news_info !== false){ | ||
| 68 | - //更新所有商品到当前分类 | ||
| 69 | - $rs = $blogModel->edit(['category_id'=>$blogCategoryModel->id],['category_id'=>$this->param['pid']]); | ||
| 70 | - if($rs === false){ | ||
| 71 | - DB::rollBack(); | ||
| 72 | - $this->response('error',Code::USER_ERROR); | ||
| 73 | - } | ||
| 74 | - } | ||
| 75 | - } | ||
| 76 | - } | ||
| 77 | - //TODO::写入日志 | ||
| 78 | - DB::commit(); | 52 | + //添加时,验证分类上级分类是否有,有则更新到当前分类中,没有时直接添加 |
| 53 | + $blogCategoryLogic->add_blog_category(); | ||
| 79 | $this->response('success'); | 54 | $this->response('success'); |
| 80 | } | 55 | } |
| 81 | 56 |
| @@ -52,7 +52,7 @@ class NewsCategoryController extends BaseController | @@ -52,7 +52,7 @@ class NewsCategoryController extends BaseController | ||
| 52 | */ | 52 | */ |
| 53 | public function add(NewsCategoryRequest $request,NewsCategoryLogic $newsCategoryLogic){ | 53 | public function add(NewsCategoryRequest $request,NewsCategoryLogic $newsCategoryLogic){ |
| 54 | $request->validated(); | 54 | $request->validated(); |
| 55 | - //添加商品时,验证分类上级分类是否有商品,有则更新到当前分类中,没有时直接添加 | 55 | + //添加时,验证分类上级分类是否有,有则更新到当前分类中,没有时直接添加 |
| 56 | $newsCategoryLogic->add_news_category(); | 56 | $newsCategoryLogic->add_news_category(); |
| 57 | //TODO::写入日志 | 57 | //TODO::写入日志 |
| 58 | $this->response('success',Code::SUCCESS,[]); | 58 | $this->response('success',Code::SUCCESS,[]); |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Http\Logic\Bside\Blog; | ||
| 4 | + | ||
| 5 | +use App\Enums\Common\Code; | ||
| 6 | +use App\Http\Logic\Bside\BaseLogic; | ||
| 7 | +use App\Models\Blog\BlogCategory as BlogCategoryModel; | ||
| 8 | +use App\Models\Blog\Blog as BlogModel; | ||
| 9 | +use Illuminate\Support\Facades\DB; | ||
| 10 | + | ||
| 11 | +class BlogCategoryLogic extends BaseLogic | ||
| 12 | +{ | ||
| 13 | + public function __construct() | ||
| 14 | + { | ||
| 15 | + parent::__construct(); | ||
| 16 | + | ||
| 17 | + $this->model = new BlogCategoryModel(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * @name :添加时验证上级分类是否有商品,有则替换带当前分类下 | ||
| 22 | + * @return void | ||
| 23 | + * @author :liyuhang | ||
| 24 | + * @method | ||
| 25 | + */ | ||
| 26 | + public function add_blog_category(){ | ||
| 27 | + DB::beginTransaction(); | ||
| 28 | + $this->param = $this->requestAll; | ||
| 29 | + $this->param['project_id'] = $this->user['project_id']; | ||
| 30 | + $this->param['operator_id'] = $this->user['id']; | ||
| 31 | + $this->param['create_id'] = $this->user['id']; | ||
| 32 | + $this->param['created_at'] = date('Y-m-d H:i:s'); | ||
| 33 | + $this->param['updated_at'] = date('Y-m-d H:i:s'); | ||
| 34 | + $cate_id = $this->model->insertGetId($this->param); | ||
| 35 | + if($cate_id === false){ | ||
| 36 | + DB::rollBack(); | ||
| 37 | + $this->fail('error',Code::USER_ERROR); | ||
| 38 | + } | ||
| 39 | + //判断为子分类时 | ||
| 40 | + if(isset($this->param['pid']) && !empty($this->param['pid'])) { | ||
| 41 | + //查看当前上级分类下是否有其他分类 | ||
| 42 | + $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]]); | ||
| 43 | + if ($cate_info === false) { | ||
| 44 | + //查看当前上一级分类下是否有新闻 | ||
| 45 | + $blogModel = new BlogModel(); | ||
| 46 | + $news_info = $blogModel->read(['category_id' => ['like', ',' . $this->param['pid'] . ',']]); | ||
| 47 | + if ($news_info !== false) { | ||
| 48 | + $replacement = ',' . $cate_id . ','; | ||
| 49 | + $old = ',' . $this->param['pid'] . ','; | ||
| 50 | + //更新所有商品到当前分类 | ||
| 51 | + $rs = DB::table('gl_Blog')->where('category_id', 'like', '%' . $old . '%') | ||
| 52 | + ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]); | ||
| 53 | + if ($rs === false) { | ||
| 54 | + DB::rollBack(); | ||
| 55 | + $this->fail('error', Code::USER_ERROR); | ||
| 56 | + } | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + DB::commit(); | ||
| 61 | + return $this->success(); | ||
| 62 | + } | ||
| 63 | +} |
-
请 注册 或 登录 后发表评论