BlogCategoryLogic.php 11.5 KB
<?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\Com\AssociationCate;
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   :categoryTopList
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 11:05
     */
    public function getCateAllList(){
        $this->param['project_id'] = $this->user['project_id'];
        $this->param['status'] = 0;
        if(isset($this->param['id']) && !empty($this->param['id'])){
            //排序掉当前id下所有子集
            $str = $this->getAllSub($this->param['id'],$str);
            $this->param['id'] = ['!=',$str];
        }
        $list = $this->model->list($this->param);
        if(!empty($list)){
            foreach ($list as $k => $v){
                $v['sub'] = _get_child($v['id'],$list);
                $list[$k] = $v;
            }
        }
        $this->success($list);
    }

    /**
     * @remark :获取当前id下所有子集
     * @name   :getAllSub
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 15:10
     */
    public function getAllSub($id,&$str = []){
        $str[] = $id;
        $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 $this->success($str);
    }

    /**
     * @remark :获取数据详情
     * @name   :info
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 15:37
     */
    public function info(){
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('当前数据不存在或者已被删除');
        }
        $info['url'] = $this->user['domain'] . $info['alias'];
        return $this->success($info);
    }

    /**
     * @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);
    }

    /**
     * @remark :保存数据
     * @name   :categorySave
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 13:42
     */
    public function categorySave(){
        //验证名称是否存在
        $this->verifyParamName($this->param['name']);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $id = $this->cateEdit();
        }else{
            $id = $this->cateAdd();
        }
        //更新路由
        $route = RouteMap::setRoute(isset($this->param['alias']) ? $this->param['alias'] : $this->param['name'],
            RouteMap::SOURCE_BLOG_CATE, $id, $this->user['project_id']);
        $this->model->edit(['alias'=>$route],['id'=>$id]);
        //通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_BLOG_CATE, 'route'=>$route]);
        return $this->success();
    }

    /**
     * @remark :添加数据
     * @name   :add
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 11:07
     */
    public function cateAdd(){
        //拼接参数
        $this->param = $this->addParamProcessing($this->param);
        try {
            $id = $this->model->addReturnId($this->param);
            //非顶级菜单处理子集
            if($this->param['pid'] != 0){
                //把所有上级博客移动到下级
                $associationCateModel = new AssociationCate();
                $associationCateModel->edit(['category_id'=>$id],['category_id'=>$this->param['pid'],$associationCateModel::BLOG_CATE]);
            }
        }catch (\Exception $e){
            $this->fail('系统错误请联系管理员');
        }
        return $this->success($id);
    }

    /**
     * @remark :修改数据
     * @name   :edit
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 11:08
     */
    public function cateEdit(){
        $this->param['operator_id'] = $this->user['id'];
        try {
            $this->model->edit($this->param,['id'=>$this->param['id']]);
            if($this->param['pid'] != 0){
                //查看当前上级分类下是否有博客
                $associationCateModel = new AssociationCate();
                $num = $associationCateModel->where(['category_id'=>$this->param['pid'],'type'=>$associationCateModel::BLOG_CATE])->count();
                if($num > 0){
                    //获取当前分类下的最后一级
                    $lastId = $this->getLastCateId($this->param['id']);
                    //更新所有博客到当前分类下
                    $associationCateModel->edit(['category_id'=>$lastId],['category_id'=>$this->param['pid'],'type'=>$associationCateModel::BLOG_CATE]);
                }
            }
        }catch (\Exception $e){
            $this->fail('系统错误请联系管理员');
        }
        //查看路由是否更新
        $id = $this->editCategoryRoute($this->param['id'],$this->param['alias']);
        return $this->success($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();
    }

    /**
     * @remark :获取当前分类下最后一级分类的id
     * @name   :getLastCateId
     * @author :lyh
     * @method :post
     * @time   :2023/10/18 14:37
     */
    public function getLastCateId($id){
        $info = $this->model->read(['pid'=>$id],['id']);
        if($info === false){
            return $this->success($id);
        }else{
            $this->getLastCateId($info['id']);
        }
    }

    /**
     * @name :编辑状态与排序
     * @return void
     * @author :liyuhang
     * @method
     */
    public function cateStatus(){
        $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'=>$id],['id']);
        if($rs !== false){
            $this->response('当前分类拥有博客,不允许删除');
        }
        return $this->success();
    }


    /**
     * @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();
    }

    /**
     * @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();
    }
}