KeywordLogic.php 3.9 KB
<?php

namespace App\Http\Logic\Bside\Product;

use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use Illuminate\Support\Facades\DB;

/**
 * Class KeywordLogic
 * @package App\Http\Logic\Bside\APublicModel
 * @author zbj
 * @date 2023/4/15
 */
class KeywordLogic extends  BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new Keyword();
    }

    /**
     * @remark :获取数据详情
     * @name   :getInfo
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 16:50
     */
    public function getInfo($id)
    {
        $info = parent::getInfo($id);
        $info['url'] = $this->user['domain'] . $info['route'];
        return $this->success($info);
    }

    /**
     * @remark :保存
     * @name   :keywordSave
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 16:50
     */
    public function keywordSave(){
        DB::beginTransaction();
        try {
            if(isset($this->param['id']) && !empty($this->param['id'])){
                $id = $this->param['id'];
                $this->model->edit($this->param,['id'=>$this->param['id']]);
            }else{
                $this->param['project_id'] = $this->user['project_id'];
                $this->param['created_at'] = date('Y-m-d H:i:s');
                $this->param['updated_at'] = $this->param['created_at'];
                $id = $this->model->insertGetId($this->param);
            }
           //路由映射
            $route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('保存失败');
        }
        //通知更新
        $this->updateNotify(['project_id'=>$this->user['project_id'], 'type'=>RouteMap::SOURCE_PRODUCT_KEYWORD, 'route'=>$route]);
        return $this->success();
    }

    /**
     * @remark :批量添加数据
     * @name   :batchAdd
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 14:03
     */
    public function batchAdd(){
        $ids = [];
        var_dump($this->param['title']);
        die();
        if(!empty($this->param['title']) && is_array($this->param['title'])){
            foreach ($this->param['title'] as $k => $v){
                $param['project_id'] = $this->user['project_id'];
                $param['created_at'] = date('Y-m-d H:i:s');
                $param['updated_at'] = $param['created_at'];
                $param['title'] = $v;
                $id = $this->model->insertGetId($param);
                $ids[] = $id;
                RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
            }
        }
        return $this->success($ids);
    }

    /**
     * @remark :删除标签
     * @name   :keywordDelete
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 11:28
     */
    public function keywordDelete(){
        $ids = $this->param['ids'];
        DB::beginTransaction();
        try {
            $productModel = new Product();
            foreach ($ids as $id){
                $product_info = $productModel->read(['category_id'=>['like','%,'.$id.',%']]);
                if($product_info !== false){
                    $this->fail('当前关键词拥有产品不允许删除');
                }
                RouteMap::delRoute(RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
            }
            $this->model->del(['id'=>['in',$ids]]);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('删除失败');
        }
        return $this->success();
    }

}