BlogCategoryLogic.php 13.2 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
<?php

namespace App\Http\Logic\Bside\Blog;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\Blog as BlogModel;
use App\Models\Blog\BlogCategory as BlogCategoryModel;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;

class BlogCategoryLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new BlogCategoryModel();
        $this->param = $this->requestAll;
    }

    /**
     * @remark :保存数据
     * @name   :categorySave
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 13:42
     */
    public function categorySave(){
        //验证名称是否存在
        $this->verifyParamName($this->param['name']);
        DB::beginTransaction();
        try {
            if(isset($this->param['id']) && !empty($this->param['id'])){
                //查看路由是否更新
                $id = $this->editCategoryRoute($this->param['id'], $this->param['alias']);
                $this->editHandleCategory($this->param['id'],$this->param['pid']);
                $this->param['operator_id'] = $this->user['id'];
                $this->edit($this->param,['id'=>$this->param['id']]);
            }else{
                //拼接参数
                $this->param = $this->addParamProcessing($this->param);
                $id = $this->model->addReturnId($this->param);
                //处理子集
                $this->addProcessingSon($id);
            }
            $route = RouteMap::setRoute(isset($this->param['alias']) ? $this->param['alias'] : $this->param['name'],
                RouteMap::SOURCE_BLOG_CATE, $id, $this->user['project_id']);
            $this->edit(['alias'=>$route],['id'=>$id]);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('系统错误,请联系管理');
        }
        //通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_BLOG_CATE, 'route'=>$route]);
        return $this->success();
    }

    /**
     * @remark :编辑分类,处理博客数据
     * @name   :editCategory
     * @author :lyh
     * @method :post
     * @time   :2023/10/20 9:32
     */
    public function editHandleCategory($id,$pid){
        $info = $this->model->read(['id'=>$id],['id','pid']);
        if($info['pid'] != $pid){
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export(1111111, true) . PHP_EOL, FILE_APPEND);
            //修改勒上级,先查看上级是否拥有博客
            $blogModel = new BlogModel();
            $blogCount = $blogModel->formatQuery(['category_id'=>['like','%,'.$pid.',%']])->count();
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export(222222, true) . PHP_EOL, FILE_APPEND);
            //随机获取最后一级id
            $replacement = $this->getLastId($id);
            @file_put_contents(storage_path('logs/lyh_error.log'), var_export(33333, true) . PHP_EOL, FILE_APPEND);
            if($blogCount > 0){
                //存在博客时,移动所有博客到当前分类最后一级
                $blogModel->where('category_id', 'like', '%,' . $pid . ',%')->where('category_id', 'like', '%,' . $replacement . ',%')
                    ->update(['category_id' => DB::raw("REPLACE(category_id, ',$pid,', ',')")]);
                $blogModel->where('category_id', 'like', '%,' . $pid . ',%')
                    ->update(['category_id' => DB::raw("REPLACE(category_id, ',$pid,', ',$replacement,')")]);
            }
        }
        return $this->success();
    }

    /**
     * @remark :随机获取当前id下最后一级的id
     * @name   :getLastId
     * @author :lyh
     * @method :post
     * @time   :2023/10/20 9:45
     */
    public function getLastId($id){
        $info = $this->model->read(['pid'=>$id],['id']);
        if($info == false){
            return $this->getLastId($id);
        }else{
            return $id;
        }

    }

    /**
     * @remark :编辑路由时生成路由记录
     * @name   :editCategoryRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:51
     */
    public function editCategoryRoute($id,$route){
        //生成一条删除路由记录
        $info = $this->model->read(['id'=>$id],['id','alias']);
        if($info['alias'] != $route){
            $data = [
                'source'=>RouteMap::SOURCE_NEWS_CATE,
                'route'=>$info['alias'],
            ];
            $this->setRouteDeleteSave($data);
        }
        return $this->success($id);
    }


    /**
     * @remark :查看参数是否可编辑
     * @name   :verifyParamEdit
     * @author :lyh
     * @method :post
     * @time   :2023/7/31 16:31
     */
    public function verifyParamEdit($id,$pid){
        $info = $this->model->read(['id'=>$id]);
        if($info['pid'] != $pid){
            $info = $this->model->read(['pid'=>$id]);
            if($info !== false){
                $this->fail('当前分类拥有子分类不允许修改父级');
            }
            //查询当前分类下是否有商品
            $blogModel = new BlogModel();
            $info = $blogModel->read(['category_id'=>['like','%,'.$id.',%']],['id']);
            if($info !== false){
                $this->fail('当前分类下已存在博客,请先把博客移除当前分类,在修改');
            }
        }
        return $this->success();
    }


    /**
     * @name :详情
     * @return array
     * @author :liyuhang
     * @method
     */
    public function info_blog_category(){
        $info = $this->model->read($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 delBlogCategory(){
        foreach ($this->param['id'] as $id){
            $this->verifyIsDelete($id);
            //删除路由
            $this->delRoute($id);
            $this->model->del(['id'=>$id]);
        }
        return $this->success();
    }

    /**
     * @remark :验证是否可删除
     * @name   :VerifyIsDelete
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 14:40
     */
    public function verifyIsDelete($id){
        //查询是否有子分类
        $rs = $this->model->read(['pid'=>$id],['id']);
        if($rs !== false){
            $this->response('当前分类拥有子分类不允许删除');
        }
        //查看当前分内下是否有博客
        $blogModel = new BlogModel();
        $rs = $blogModel->read(['category_id'=>['like','%,'.$id.',%']],['id']);
        if($rs !== false){
            $this->response('当前分类拥有博客,不允许删除');
        }
        return $this->success();
    }

    /**
     * @name   :(添加分类时获取1级分类)categoryTopList
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 9:09
     */
    public function categoryTopList(){
        $this->param['project_id'] = $this->user['project_id'];
        $this->param['status'] = 0;
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $str = [];
            //排序掉当前id下所有子集
            $str = $this->getAllSub($this->param['id'],$str);
            $str[] = (int)$this->param['id'];
            $this->param['id'] = ['not in',$str];
        }
        $menu = array();
        $list = $this->model->list($this->param);
        if(!empty($list)){
            foreach ($list as $k => $v){
                if($v['pid'] == 0){
                    $v['sub'] = _get_child($v['id'],$list);
                    $menu[]   = $v;
                }
            }
        }
        return $this->success($menu);
    }

    /**
     * @remark :获取当前id下所有子集
     * @name   :getAllSub
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 15:10
     */
    public function getAllSub($id,&$str = []){
        $list = $this->model->list(['pid'=>$id,'status'=>0],['id','pid']);
        if(!empty($list)){
            foreach ($list as $v){
                $str[] = $v['id'];
                $this->getAllSub($v['id'],$str);
            }
        }
        return $str;
    }

    /**
     * @name   :(参数处理)paramProcessing
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:30
     */
    public function addParamProcessing($param){
        $param['project_id'] = $this->user['project_id'];
        $param['operator_id'] = $this->user['id'];
        $param['create_id'] = $this->user['id'];
        return $this->success($param);
    }

    /**
     * @name   :(验证名称是否存在)verifyParamName
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:41
     */
    public function verifyParamName($name){
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $condition = [
                'id'=>['!=',$this->param['id']],
                'name'=>$name,
            ];
        }else{
            $condition = ['name'=>$name];
        }
        $info = $this->model->read($condition);
        if($info !== false){
            $this->fail('当前分类名称已存在');
        }
        return $this->success();
    }

    /**
     * @param $cate_id
     * @name   :(处理子集)addProcessingSon
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:59
     */
    public function addProcessingSon($cate_id){
        if(!isset($this->param['pid'])){
            $this->param['pid'] = 0;
        }
        //判断为子分类时
        if($this->param['pid'] != 0){
            //查看当前上级分类下是否有其他子分类
            $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 = ',' . $cate_id . ',';
                    $old = ',' . $this->param['pid'] . ',';
                    //更新所有商品到当前分类
                    $blogModel->where('category_id', 'like', '%' . $old . '%')
                        ->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
                }
            }
        }
        return $this->success();
    }

    /**
     * @remark :删除路由
     * @name   :delRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:50
     */
    public function delRoute($id){
        //删除路由映射
        RouteMap::delRoute(RouteMap::SOURCE_BLOG_CATE, $id, $this->user['project_id']);
        //生成一条删除路由记录
        $info = $this->model->read(['id'=>$id],['id','alias']);
        $data = [
            'source'=>RouteMap::SOURCE_BLOG_CATE,
            'route'=>$info['alias'],
        ];
        $this->setRouteDeleteSave($data);
        return $this->success();
    }

    /**
     * 新闻导入:分类处理
     * @param $project_id
     * @param $user_id
     * @param $category
     * @return string
     * @throws \Exception
     * @author Akun
     * @date 2023/09/20 17:35
     */
    public function importBlogCategory($project_id,$user_id,$category){
        $return = [];

        $cate_arr = explode('/',$category);
        $p_cate = $cate_arr[0];
        $c_cate = $cate_arr[1];

        $p_category = $this->model->read(['name'=>$p_cate,'pid'=>0]);
        if(!$p_category){
            $p_id = $this->model->addReturnId(['name'=>$p_cate,'project_id'=>$project_id,'operator_id'=>$user_id,'create_id'=>$user_id]);

            $route = RouteMap::setRoute($p_cate, RouteMap::SOURCE_BLOG_CATE, $p_id, $project_id);
            $this->model->edit(['alias'=>$route],['id'=>$p_id]);
        }else{
            $p_id = $p_category['id'];
        }
        $return[] = $p_id;

        if($c_cate){
            $c_category = $this->model->read(['name'=>$c_cate,'pid'=>$p_id]);
            if(!$c_category){
                $c_id = $this->model->addReturnId(['name'=>$c_cate,'pid'=>$p_id,'project_id'=>$project_id,'operator_id'=>$user_id,'create_id'=>$user_id]);

                $route = RouteMap::setRoute($c_cate, RouteMap::SOURCE_BLOG_CATE, $c_id, $project_id);
                $this->model->edit(['alias'=>$route],['id'=>$c_id]);
            }else{
                $c_id = $c_category['id'];
            }
            $return[] = $c_id;
        }
        return ','.implode(',',$return).',';
    }

    /**
     * @remark :排序
     * @name   :setSort
     * @author :lyh
     * @method :post
     * @time   :2023/8/19 11:16
     */
    public function setSort(){
        $rs = $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }
}