NavLogic.php 2.1 KB
<?php

namespace App\Http\Logic\Bside\Nav;


use App\Enums\Common\Code;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\BNav;


/**
 * @author:dc
 * @time 2023/5/11 16:51
 * Class NavLogic
 * @package App\Http\Logic\Bside
 */
class NavLogic extends  BaseLogic
{
    public function __construct()
    {
        parent::__construct();

        $this->model = new BNav();
    }

    /**
     * @return array
     * @author:dc
     * @time 2023/5/12 9:23
     */
    public function list(){
        $where = [];
        if(!empty($this->requestAll['location'])){
            $where[] = ['location','=',$this->requestAll['location']];
        }

        $lists = $this->getList($where,['sort'=>'asc'],['*'],false);

        $isTree = $this->requestAll['tree']??false;

        if($isTree){
            $lists = list_to_tree($lists);
        }

        return $lists;
    }


    public function save($data)
    {
        if($data['pid']){
            // 验证是否存在上级
            $all = BNav::_all($this->user['project_id'],$data['location']);
            if(!$all->where('id',$data['pid'])->count()){
                $this->fail('上级栏目不存在');
            }
            // 上级不允许是自己的下级
            if(!empty($data['id'])){
                $all = list_to_tree($all->toArray(),$data['id']);
                $all = tree_to_list($all);
                if(in_array($data['pid'],array_column($all,'id'))){
                    $this->fail('上级栏目不允许为本身的下级');
                }
            }
        }

        // 保存
        $id = parent::save($data);

        return $this->getInfo($id['id']);
    }

    /**
     * @param $ids
     * @return array
     * @throws \App\Exceptions\AsideGlobalException
     * @throws \App\Exceptions\BsideGlobalException
     * @author:dc
     * @time 2023/5/11 16:59
     */
    public function delete($ids,$map = [])
    {
        if(BNav::isChild($ids,$this->user['project_id'])){
            $this->fail('存在下级无法删除');
        }

        return parent::delete($ids,$map); // TODO: Change the autogenerated stub

    }

}