KeywordLogic.php 7.9 KB
<?php

namespace App\Http\Logic\Bside\Product;

use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\News\News;
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 getKeywordInfo()
    {
        $info = $this->model->read($this->param);
        $info['url'] = $this->user['domain'] . $info['route'];
        $info['related_news_info'] = News::whereIn('id',  $info['related_news_ids'])->select(['id', 'name'])->get();

        return $this->success($info);
    }

    /**
     * @remark :保存
     * @name   :keywordSave
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 16:50
     */
    public function keywordSave(){
        DB::beginTransaction();
        try {
            $this->param = $this->handleSaveParam($this->param);
            if(isset($this->param['id']) && !empty($this->param['id'])){
                //TODO::不能修改路由
                $this->model->edit($this->param,['id'=>$this->param['id']]);
                $route = $this->param['route'];
            }else{
                $this->param = $this->addHandleParam($this->param);
                $id = $this->model->insertGetId($this->param);
                //路由映射
                $route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
                $this->model->edit(['route'=>$route],['id'=>$id]);
            }
            //清除缓存
            Common::del_user_cache('product_keyword',$this->user['project_id']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('保存失败,请连续管理员');
        }
        $this->addUpdateNotify(RouteMap::SOURCE_PRODUCT_KEYWORD,$route);
        $this->curlDelRoute(['new_route'=>$route]);
        return $this->success();
    }

    /**
     * @remark :添加组装数据
     * @name   :addHandleParam
     * @author :lyh
     * @method :post
     * @time   :2023/11/30 15:00
     */
    public function addHandleParam($param){
        $param['project_id'] = $this->user['project_id'];
        $param['created_at'] = date('Y-m-d H:i:s');
        $param['updated_at'] = $param['created_at'];
        return $this->success($param);
    }

    /**
     * @remark :保存数据时参数处理
     * @name   :handleSaveParam
     * @author :lyh
     * @method :post
     * @time   :2023/10/23 14:47
     */
    public function handleSaveParam($param){
        if(isset($param['keyword_top_banner']) && !empty($param['keyword_top_banner'])){
            $param['keyword_top_banner'] = str_replace_url($param['keyword_top_banner']);
        }
        if(isset($param['keyword_foot_banner']) && !empty($param['keyword_foot_banner'])){
            $param['keyword_foot_banner'] = str_replace_url($param['keyword_foot_banner']);
        }
        if(isset($param['keyword_video']) && !empty($param['keyword_video'])){
            $param['keyword_video'] = Arr::a2s($param['keyword_video']);
        }
        if(!empty($param['related_news_ids'])){
            $param['related_news_ids'] = Arr::arrToSet($param['related_news_ids']);
        }
        if(!empty($param['related_blog_ids'])){
            $param['related_blog_ids'] = Arr::arrToSet($param['related_blog_ids']);
        }
        return $param;
    }

    /**
     * @remark :批量添加数据
     * @name   :batchAdd
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 14:03
     */
    public function batchAdd(){
        try {
            foreach ($this->param['title'] as $v){
                $this->model = new Keyword();
                $info = $this->model->read(['title'=>$v]);
                if($info === false){
                    $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);
                    $route = RouteMap::setRoute($v, RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
                    $this->model->edit(['route'=>$route],['id'=>$id]);
//                    $this->curlDelRoute(['new_route'=>$route]);
                }
            }
        }catch (\Exception $e){
            $this->fail('保存失败,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :删除标签
     * @name   :keywordDelete
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 11:28
     */
    public function keywordDelete(){
        $ids = $this->param['ids'];
        $productModel = new Product();
        foreach ($ids as $id){
            $product_info = $productModel->read(['keyword_id'=>['like','%,'.$id.',%']]);
            if($product_info !== false){
                $this->fail('当前关键词拥有产品不允许删除');
            }
            $this->delRoute($id);
            $this->model->del(['id'=>$id]);
        }
        //清除缓存
        Common::del_user_cache('product_keyword',$this->user['project_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_PRODUCT_KEYWORD, $id, $this->user['project_id']);
        //生成一条删除路由记录
        $info = $this->model->read(['id'=>$id],['id','route']);
        $this->curlDelRoute(['route'=>$info['route']]);
        return $this->success();
    }

    /**
     * 产品导入:关键词处理
     * @param $project_id
     * @param $keyword
     * @return string
     * @throws \Exception
     * @author Akun
     * @date 2023/09/21 14:55
     */
    public function importProductKeyword($project_id,$keyword){
        $return = [];
        $keyword_arr = explode('^v6sp$',$keyword);
        foreach ($keyword_arr as $v){
            $keyword_info = $this->model->read(['title'=>$v]);
            if(!$keyword_info){
                $k_id = $this->model->addReturnId(['title'=>$v,'project_id'=>$project_id]);
                $route = RouteMap::setRoute($v, RouteMap::SOURCE_PRODUCT_KEYWORD, $k_id, $project_id);
                $this->model->edit(['route'=>$route],['id'=>$k_id]);
            }else{
                $k_id = $keyword_info['id'];
            }
            $return[] = $k_id;
        }
        //清除缓存
        Common::del_user_cache('product_keyword',$project_id);
        return ','.implode(',',$return).',';
    }

    /**
     * 批量删除
     * @return array
     * @throws BsideGlobalException
     * @throws \App\Exceptions\AsideGlobalException
     * @author zbj
     * @date 2023/11/22
     */
    public function batchDel(){
        try {
            foreach ($this->param['title'] as $v){
                $info = $this->model->read(['title'=>$v]);
                if($info){
                    $this->delRoute($info['id']);
                    $this->model->del(['id'=>$info['id']]);
                }
            }
            //清除缓存
            Common::del_user_cache('product_keyword',$this->user['project_id']);
        }catch (\Exception $e){
            $this->fail('error');
        }
        return $this->success();
    }
}