CustomModuleCategoryLogic.php 9.7 KB
<?php
/**
 * @remark :
 * @name   :CustomModuleCategoryLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/12/4 16:07
 */

namespace App\Http\Logic\Bside\CustomModule;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\RouteMap\RouteMap;

class CustomModuleCategoryLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new CustomModuleCategory();
    }

    /**
     * @remark :添加/编辑获取分类列表
     * @name   :getCateList
     * @author :lyh
     * @method :post
     * @time   :2023/12/5 17:12
     */
    public function getCateList(){
        $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 $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;
    }

    /**
     * @remark :获取当前数据详情
     * @name   :getCustomModuleInfo
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 16:10
     */
    public function categoryInfo(){
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        return $this->success($info);
    }

    /**
     * @remark :保存数据
     * @name   :ModuleSave
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:47
     */
    public function categorySave(){
        $this->param = $this->handleParam($this->param);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $this->categoryEdit();
        }else{
            $this->categoryAdd();
        }
        return $this->success();
    }

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

    /**
     * @remark :添加分类
     * @name   :categoryAdd
     * @author :lyh
     * @method :post
     * @time   :2023/12/5 10:55
     */
    public function categoryAdd(){
        try {
            $id = $this->model->addReturnId($this->param);
            $route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_MODULE_CATE.$this->param['module_id'],
                $id, $this->user['project_id']);
            $this->handleAddSon($id);
            $this->addUpdateNotify(RouteMap::SOURCE_MODULE_CATE.$this->param['module_id'],$route);
            $this->curlDelRoute(['new_route'=>$route]);
            $this->edit(['route' => $route], ['id' => $id]);
            //处理上级分类商品
            $this->handleAddSon($id);
        }catch (\Exception $e){
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :编辑分类
     * @name   :categoryEdit
     * @author :lyh
     * @method :post
     * @time   :2023/12/5 10:55
     */
    public function categoryEdit(){
        $route = RouteMap::setRoute($this->param['route'], RouteMap::SOURCE_MODULE_CATE.$this->param['module_id'],
            $this->param['id'], $this->user['project_id']);
        $this->editHandleCategory($this->param['id'],$this->param['pid']);
        $this->editRoute($this->param['id'],$route);
        $rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
        if($rs === false){
            $this->fail('系统错误,请连续管理员');
        }
        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){
            //修改勒上级,先查看上级是否拥有产品
            $contentModel = new CustomModuleContent();
            $contentCount = $contentModel->formatQuery(['category_id'=>['like','%,'.$pid.',%']])->count();
            if($contentCount > 0){
                //随机获取最后一级id
                $replacement = $this->getLastId($id);
                //存在博客时,移动所有博客到当前分类最后一级
                $contentModel->where('category_id', 'like', '%,' . $pid . ',%')->where('category_id', 'like', '%,' . $replacement . ',%')
                    ->update(['category_id' => DB::raw("REPLACE(category_id, ',$pid,', ',')")]);
                $contentModel->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($info['id']);
        }else{
            return $id;
        }
    }

    /**
     * @remark :查看是否编辑路由
     * @name   :editCategoryRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 11:05
     */
    public function editRoute($id, $route)
    {
        //生成一条删除路由记录
        $info = $this->model->read(['id' => $id], ['id', 'route']);
        if ($info['route'] != $route) {
            $this->addUpdateNotify(RouteMap::SOURCE_MODULE_CATE.$this->param['module_id'],$route);
            $this->curlDelRoute(['route'=>$info['route'],'new_route'=>$route]);
        }
        return true;
    }

    /**
     * @name   :(添加分类时处理子集分类)addProcessingSon
     * @author :lyh
     * @method :post
     * @time   :2023/6/13 11:34
     */
    public function handleAddSon($cate_id){
        if(isset($this->param['pid']) && !empty($this->param['pid'])) {
            $this->param['pid'] = 0;
        }
        //判断为子分类时
        if($this->param['pid'] != 0) {
            //查看当前上级分类下是否有其他分类
            $cate_info = $this->model->read(['pid' => $this->param['pid'], 'id' => ['!=', $cate_id]],['id']);
            if ($cate_info === false) {
                //查看当前上一级分类下是否有关联模块内容
                $contentModel = new CustomModuleContent();
                $news_count = $contentModel->where('category_id','like', '%,' . $this->param['pid'] . ',%')->count();
                if ($news_count > 0) {
                    $replacement = $this->handleStr($cate_id);
                    $old = $this->handleStr($this->param['pid']);
                    //更新所有商品到当前分类
                    $contentModel->where('category_id', 'like', '%' . $old . '%')->update(['category_id' => DB::raw("REPLACE(category_id, '$old', '$replacement')")]);
                }
            }
        }
        return $this->success();
    }

    /**
     * @remark :处理字符串
     * @name   :handleStr
     * @author :lyh
     * @method :post
     * @time   :2023/12/5 18:03
     */
    public function handleStr($str){
        return ',' . $str . ',';
    }

    /**
     * @remark :删除数据
     * @name   :ModuleDel
     * @author :lyh
     * @method :post
     * @time   :2023/12/4 15:47
     */
    public function categoryDel(){
        $ids = $this->param['id'];
        foreach ($ids as $id){
            $info = $this->model->read(['pid'=>$id],['id']);
            if($info !== false){
                $this->fail('分类id:'.$id.'拥有子集不允许删除');
            }
            $contentModel = new CustomModuleContent();
            $contentInfo = $contentModel->read(['category_id'=>['like','%,'.$id.',%']]);
            if($contentInfo !== false){
                $this->fail('当前分类拥有产品不允许删除');
            }
            //删除路由
            $this->delRoute($id);
            $this->model->del(['id'=>$id]);
        }
        return $this->success();
    }

    /**
     * @remark :删除路由
     * @name   :delRoute
     * @author :lyh
     * @method :post
     * @time   :2023/9/7 10:50
     */
    public function delRoute($id)
    {
        RouteMap::delRoute(RouteMap::SOURCE_MODULE_CATE.$this->param['module_id'], $id, $this->user['project_id']);
        //通知
        $info = $this->model->read(['id' => $id], ['id', 'route']);
        $this->curlDelRoute(['route'=>$info['route']]);
        return $this->success();
    }
}