CreateKeywordLogic.php 6.1 KB
<?php
/**
 * @remark :
 * @name   :CreateKeywordLogic.php
 * @author :lyh
 * @method :post
 * @time   :2023/12/19 9:45
 */

namespace App\Http\Logic\Aside\Optimize;

use App\Http\Logic\Aside\BaseLogic;
use App\Models\Com\CreateKeyword;

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

    /**
     * @remark :保存关键字
     * @name   :saveCreateKeyword
     * @author :lyh
     * @method :post
     * @time   :2023/12/19 9:47
     */
    public function saveKeyword(){
        $data = $this->handleParam($this->param);
        try {
            if(isset($this->param['id']) && !empty($this->param['id'])){
                $this->model->edit($data,['id'=>$this->param['id']]);
            }else{
                $this->model->add($data);
            }
        }catch (\Exception $e){
            $this->fail('保存失败,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :请求参数处理
     * @name   :handleParam
     * @author :lyh
     * @method :post
     * @time   :2023/12/19 10:03
     */
    public function handleParam($param){
        $data = [
            'type'=>$param['type'],
            'name'=>$param['name'],
            'language_id'=>$param['language_id'] ?? 0
        ];
        if(!isset($param['id'])){
            $info = $this->model->read($data);
            if($info !== false){
                $this->fail('当前名称已存在');
            }
        }else{
            $data['id'] = ['!=',$param['id']];
            $info = $this->model->read($data);
            if($info !== false){
                $this->fail('当前名称已存在');
            }
        }
        return $this->success($data);
    }

    /**
     * @remark :创建关键词
     * @name   :createKeyword
     * @author :lyh
     * @method :post
     * @time   :2023/12/19 10:48
     */
    public function createKeyword(){
        $data = array();
        if(empty($this->param['keyword'])){
            return $this->success($data);
        }
        $except_k = ['Quality','Philippines','USA','UK','America','China','Wholesale','Hot Sale','Cheap','cheap','price','pricelist','hot sale','Price','Pricelist','With ','For ','And ','Oem','Odm','Supplier','Manufacturer','CE Certification','Factory','Exporters','Company','Companies','Suppliers','Manufacturers','Factories','Company','Companies','Exporters','Exporter','Buy ',' Buy','Where ','What ','When ','How ','Which ','Producer','Producers','Best Selling','Hot Selling','Near','Chinese','India','use','high quality','discount','online','custom','customized','Enterprise','Agent','Plant','Refinery','Foundry','Maker','Distributor'];
        $filterKeywords = [];
        foreach ($this->param['keyword'] as $k=>$v){
            if(in_array($v,$except_k)){
                unset($this->param['keyword'][$k]);
                $filterKeywords[] = $v;
            }
        }
        $prefix_keyword = $this->prefixKeyword($this->param['prefix'] ?? [],$this->param['keyword'],$except_k);
        $keyword_suffix = $this->keywordSuffix($this->param['suffix'] ?? [],$this->param['keyword'],$except_k);
        $prefix_keyword_suffix = $this->prefixKeywordSuffix($this->param['prefix'] ?? [],$this->param['suffix'] ?? [],$this->param['keyword'],$except_k);
        $data = [
            'prefix_keyword'=>$prefix_keyword,
            'prefix_keyword_count'=>count($prefix_keyword),
            'keyword_suffix'=>$keyword_suffix,
            'keyword_suffix_count'=>count($keyword_suffix),
            'prefix_keyword_suffix'=>$prefix_keyword_suffix,
            'prefix_keyword_suffix_count'=>count($prefix_keyword_suffix),
            'filterKeywords'=>$filterKeywords,
            'filterKeywords_count'=>count($filterKeywords),
        ];
        return $this->success($data);
    }

    /**
     * @remark :前缀+关键词
     * @name   :
     * @author :lyh
     * @method :post
     * @time   :2023/12/19 11:11
     */
    public function prefixKeyword($prefix,$keyword,$except_k){
        $prefix_keyword = array();
        if(!empty($prefix)){//前缀+关键词
            foreach ($keyword as $keywordItem){
                foreach ($prefix as $prefixItem) {
                    if (strpos($keywordItem, $prefixItem) === 0) {
                        continue;
                    }
                    $prefix_keyword[] =$prefixItem.' '.$keywordItem;
                }
            }
        }
        return $this->success($prefix_keyword);
    }

    /**
     * @remark :关键词+后缀
     * @name   :
     * @author :lyh
     * @method :post
     * @time   :2023/12/19 11:11
     */
    public function keywordSuffix($suffix,$keyword){
        $suffix_keyword = array();
        if(!empty($suffix)){//前缀+关键词
            foreach ($keyword as $keywordItem){
                foreach ($suffix as $suffixItem) {
                    if (str_ends_with($keywordItem, $suffixItem)) {
                        continue;
                    }
                    $suffix_keyword[] = $keywordItem.' '.$suffixItem;
                }
            }
        }
        return $this->success($suffix_keyword);
    }

    /**
     * @remark :前缀+关键词+后缀
     * @name   :prefixKeywordSuffix
     * @author :lyh
     * @method :post
     * @time   :2023/12/19 11:59
     */
    public function prefixKeywordSuffix($prefix,$suffix,$keyword){
        $prefix_keyword_suffix = array();
        if(!empty($prefix) && !empty($suffix)){
            foreach ($keyword as $keywordItem){
                foreach ($prefix as $prefixItem) {
                    if (strpos($keywordItem, $prefixItem) === 0) {
                        continue;
                    }
                    foreach ($suffix as $suffixItem) {
                        if (str_ends_with($keywordItem, $suffixItem)) {
                            continue;
                        }
                        $prefix_keyword_suffix[] = $prefixItem.' '.$keywordItem.' '.$suffixItem;
                    }
                }
            }
        }
        return $this->success($prefix_keyword_suffix);
    }
}