KeywordController.php 11.8 KB
<?php

namespace App\Http\Controllers\Bside\Product;

use App\Enums\Common\Code;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Product\KeywordLogic;
use App\Http\Requests\Bside\Product\KeywordRequest;
use App\Models\Ai\AiCommand;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordPage;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\Project\AggregateKeywordComment;
use App\Rules\Ids;
use Illuminate\Http\Request;

/**
 * Class KeywordVideoController
 * @package App\Http\Controllers\Bside
 * @author zbj
 * @date 2023/4/15
 */
class KeywordController extends BaseController
{

    /**
     * @remark :关键字列表
     * @name   :index
     * @author :lyh
     * @method :post
     * @time   :2023/8/17 10:57
     */
    public function index(Keyword $keyword)
    {
        $this->map = $this->searchParam($this->map);
        $filed =  ['id', 'project_id', 'title', 'seo_title', 'seo_keywords', 'seo_description', 'status', 'created_at','route','keyword_title','is_video_keyword'];
        $data = $keyword->lists($this->map,$this->page,$this->row,$this->order,$filed);
        if(!empty($data)){
            foreach ($data['list'] as &$v){
                $v['product_num'] = KeywordRelated::where('keyword_id',$v['id'])->count();
                if(!empty($v['seo_title']) || !empty($v['seo_keywords']) || !empty($v['seo_description'])){
                    $v['tdk'] = 1;
                }else{
                    $v['tdk'] = 0;
                }
                $v = $this->handleReturnInfo($v);
                $v['url'] = $this->user['domain'] . $v['route'].'/';
            }
        }
        return $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :单独处理图片文件
     * @name   :HandleReturnInfo
     * @author :lyh
     * @method :post
     * @time   :2024/1/26 9:44
     */
    public function handleReturnInfo($param){
        if(isset($param['keyword_top_banner']) && !empty($param['keyword_top_banner'])){
            $param['keyword_top_banner'] = getImageUrl($param['keyword_top_banner'],$this->user['storage_type'],$this->user['project_location']);
        }
        if(isset($param['keyword_foot_banner']) && !empty($param['keyword_foot_banner'])){
            $param['keyword_foot_banner'] = getImageUrl($param['keyword_foot_banner'],$this->user['storage_type'],$this->user['project_location']);
        }
        return $this->success($param);
    }

    /**
     * @remark :搜索
     * @name   :searchParam
     * @author :lyh
     * @method :post
     * @time   :2023/12/21 17:40
     */
    public function searchParam($map){
        if(!empty($map['title'])){
            $map['title'] = ['like','%'.$map['title'].'%'];
        }
        if(!empty($map['keyword_title'])){
            $map['keyword_title'] = ['like','%'.$map['keyword_title'].'%'];
        }
        if(empty($map['is_video_keyword'])){
            unset($map['is_video_keyword']);
        }
        $map['route'] = ['<>',''];
        $map['project_id'] = $this->user['project_id'];
        $map['deleted_at'] = null;
        return $this->success($map);
    }

    /**
     * @remark :获取数据详情
     * @name   :info
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 16:57
     */
    public function info(KeywordLogic $logic){
        $data = $logic->getKeywordInfo();
        if($data !== false){
            $data = $this->handleReturnInfo($data);
        }
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :保存
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2023/8/28 14:24
     */
    public function save(KeywordRequest $request, KeywordLogic $logic)
    {
        $request->validated();
        $data = $logic->keywordSave();
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * 批量添加关键词
     * FIXME 添加通知, 异步处理任务
     * @param KeywordLogic $logic
     * @throws \App\Exceptions\AsideGlobalException
     * @throws \App\Exceptions\BsideGlobalException
     */
    public function batchAdd(KeywordLogic $logic){
        $this->request->validate([
            'title'=>['required','array', 'max:1000']
        ],[
            'title.required' => 'title不能为空',
            'title.array' => 'title为数组',
            'title.max' => '批量操作不能超过1000条数据'
        ]);
        $rs = $logic->batchAdd();
        if($rs === false){
            $this->response('创建任务添加关键词任务失败,请稍后重试!',Code::SYSTEM_ERROR);
        }
        $this->response('关键词后台异步添加中,请稍后刷新查看!');
    }

    /**
     * @remark :删除数据
     * @name   :delete
     * @author :lyh
     * @method :post
     * @time   :2023/8/23 17:44
     */
    public function delete(KeywordLogic $logic)
    {
        $this->request->validate([
            'ids'=>['required', new Ids()]
        ],[
            'ids.required' => 'ID不能为空'
        ]);
        $logic->keywordDelete();
        $this->response('success');
    }

    /**
     * 批量删除
     * @param KeywordLogic $logic
     * @author zbj
     * @date 2023/11/22
     */
    public function batchDel(KeywordLogic $logic){
        $this->request->validate([
            'title'=>['required','array', 'max:1000']
        ],[
            'title.required' => 'title不能为空',
            'title.array' => 'title为数组',
            'title.max' => '批量操作不能超过1000条数据'
        ]);
        $logic->batchDel();
        $this->response('success');
    }

    /**
     * @remark :批量清除关键词相关内容
     * @name   :batchKeywordFiled
     * @author :lyh
     * @method :post
     * @time   :2024/4/22 10:27
     */
    public function batchKeywordFiled(){
        $param = [];
        if(isset($this->param['seo_title'])){
            $param['seo_title'] = null;
        }
        if(isset($this->param['keyword'])){
            $param['seo_keywords'] = null;
        }
        if(isset($this->param['description'])){
            $param['seo_description'] = null;
        }
        if(isset($this->param['title'])){
            $param['keyword_title'] = null;
        }
        if(isset($this->param['content'])){
            $param['keyword_content'] = null;
        }
        $keywordModel = new Keyword();
        $rs = $keywordModel->edit($param,['id'=>['!=',0]]);
        if($rs === false){
            $this->fail('保存失败,请联系管理员');
        }
        $this->response('success');
    }

    /**
     * @remark :批量操作关键字是否展示视频
     * @name   :batchKeywordIsVideo
     * @author :lyh
     * @method :post
     * @time   :2024/5/30 14:29
     */
    public function batchKeywordIsVideo(){
        $this->request->validate([
            'title'=>['required','array', 'max:500']
        ],[
            'title.required' => 'title不能为空',
            'title.array' => 'title为数组',
            'title.max' => '批量操作不能超过500条数据'
        ]);
        $keywordModel = new Keyword();
        $rs = $keywordModel->edit(['is_video_keyword'=>$this->param['is_video_keyword'] ?? 0],['title'=>['in',$this->param['title']]]);
        if($rs === false){
            $this->fail('编辑失败,请联系管理员');
        }
        if($this->param['is_video_keyword'] == 1){
            $url = [];
            $keywordList = $keywordModel->list(['title'=>['in',$this->param['title']]],'id',['id','route','is_video_keyword']);
            foreach ($keywordList as $v){
                $url[] = $v['route'];
            }
            $this->sendHttpC($url);
        }
        $this->response('success');
    }

    /**
     * @remark :批量提交更新关键词
     * @name   :batchUpdateKeyword
     * @author :lyh
     * @method :post
     * @time   :2024/7/2 10:14
     */
    public function batchUpdateKeyword(){
        $this->request->validate([
            'text'=>'required|array',
            'update_object'=>'required|array',
            'update_method'=>'required'
        ],[
            'text.required' => '文件内容不能为空',
            'update_object.required' => '更新对象不为空',
            'update_object.array' => '更新对象为数组',
            'update_method.required' => '请求方式不为空'
        ]);
        $keywordPageModel = new KeywordPage();
        $this->param['text'] = json_encode($this->param['text']);
        $this->param['update_object'] = json_encode($this->param['update_object']);
        $this->param['project_id'] = $this->user['project_id'];
        $id = $keywordPageModel->addReturnId($this->param);
        $this->response('success',Code::SUCCESS,['id'=>$id]);
    }

    /**
     * @remark :删除关联关系
     * @name   :delRelated
     * @author :lyh
     * @method :post
     * @time   :2024/11/28 10:30
     */
    public function delRelated(KeywordLogic $logic){
        $this->request->validate([
            'keyword_id'=>'required',
            'product_id'=>'required',
        ],[
            'keyword_id.required' => '关键词id不能为空',
            'product_id.required' => '产品id不为空',
        ]);
        $logic->delRelated($this->param['keyword_id'],$this->param['product_id']);
        $this->response('success');
    }

    /**
     * @remark :清除当前项目所有关键字
     * @name   :delAllKeyword
     * @author :lyh
     * @method :post
     * @time   :2024/12/5 16:34
     */
    public function delAllKeyword(KeywordLogic $logic){
        $logic->delAllKeyword();
        $this->response('success');
    }

    /**
     * @remark :删除关键词所有关联产品
     * @name   :delRelatedProductId
     * @author :lyh
     * @method :post
     * @time   :2025/4/17 11:01
     */
    public function delRelatedProductId(KeywordLogic $logic){
        $this->request->validate([
            'keyword_id'=>'required',
        ],[
            'keyword_id.required' => '关键词ID不能为空',
        ]);
        $logic->delAllRelated($this->param['keyword_id']);
        $this->response('success');
    }

    /**
     * @remark :添加评论
     * @name   :saveComment
     * @author :lyh
     * @method :post
     * @time   :2025/6/9 14:27
     */
    public function saveComment(KeywordLogic $logic){
        $this->request->validate([
            'text'=>'required',
            'nickname'=>'required',
        ],[
            'text.required' => '评论内容不能为空',
            'nickname.required'=>'昵称不能为空',
        ]);
        $data = $logic->saveComment();
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :生成评论
     * @name   :sendComment
     * @author :lyh
     * @method :post
     * @time   :2025/6/9 11:10
     */
    public function sendComment(KeywordLogic $logic){
        $this->request->validate([
            'count'=>'required|max:10',
        ],[
            'count.required' => '生成条数不能为空',
            'count.max'=>'count最大10',
        ]);
        $data = $logic->sendComment();
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :获取评论
     * @name   :getComment
     * @author :lyh
     * @method :post
     * @time   :2025/6/9 11:45
     */
    public function getComment(KeywordLogic $logic){
        $data = $logic->getComment($this->map,$this->page,$this->row);
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :删除评论
     * @name   :getComment
     * @author :lyh
     * @method :post
     * @time   :2025/6/9 11:45
     */
    public function delComment(KeywordLogic $logic){
        $this->request->validate([
            'id'=>'required',
        ],[
            'id.required' => '主键不能为空',
        ]);
        $data = $logic->delComment();
        $this->response('success',Code::SUCCESS,$data);
    }
}