BlogLabelLogic.php 4.2 KB
<?php

namespace App\Http\Logic\Bside\Blog;

use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Blog\Blog as BlogModel;
use App\Models\Blog\BlogLabel as BlogLabelModel;

class BlogLabelLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new BlogLabelModel();
        $this->param = $this->requestAll;
    }
    /**
     * @param $v
     * @name :获取分类名称
     * @return void
     * @author :liyuhang
     * @method
     */
    public function getLabelName($label_id){
        $label_name = '';
        if(!empty($label_id)){
            $label_arr = $this->model->formatQuery(['id'=>['in',explode(',',trim($label_id,','))]])->pluck('name')->toArray();
            $label_name = explode(',',$label_arr);
        }
        return $this->success($label_name);
    }
    /**
     * @name :新增标签
     * @return void
     * @author :liyuhang
     * @method
     */
    public function add_blog_label(){
        $this->verifyParamName($this->param['name']);
        $this->param['create_id'] = $this->user['id'];
        $this->param['operator_id'] = $this->user['id'];
        $this->param['project_id'] = $this->user['project_id'];
        $rs = $this->model->add($this->param);
        if($rs ===  false){
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name :编辑标签
     * @return void
     * @author :liyuhang
     * @method
     */
    public function edit_blog_label(){
        $this->verifyParamName($this->param['name'],$this->param['id']);
        $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 array
     * @author :liyuhang
     * @method
     */
    public function status_blog_label(){
        $this->param['operator_id'] = $this->user['id'];
        $rs = $this->edit($this->param,['id'=>$this->param['id']]);
        if($rs ===  false){
            $this->fail('error');
        }
        return $this->success();
    }
    /**
     * @name :删除标签
     * @return array
     * @author :liyuhang
     * @method
     */
    public function del_blog_label(){
        $ids = $this->param['id'];
        foreach ($ids as $v){
            //查看当前分内下是否有博客
            $blogModel = new BlogModel();
            $rs = $blogModel->read(['label_id'=>$v],['id']);
            if($rs !== false){
                $this->response('当前标签拥有博客,不允许删除');
            }
        }
        $this->param['id'] = ['in',$this->param['id']];
        $rs = $this->model->del($this->param);
        if($rs ===  false){
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @remark :批量添加数据
     * @name   :batchAdd
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 14:03
     */
    public function batchAdd(){
        if(!empty($this->param['name']) && is_array($this->param['name'])){
            foreach ($this->param['name'] as $v){
                $this->verifyParamName($v);
                $param['create_id'] = $this->user['id'];
                $param['operator_id'] = $this->user['id'];
                $param['project_id'] = $this->user['project_id'];
                $param['name'] = $v;
                $this->model->add($param);
            }
        }
        return $this->success();
    }

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