KeywordVideoController.php 6.9 KB
<?php
/**
 * @remark :
 * @name   :KeywordVideoController.php
 * @author :lyh
 * @method :post
 * @time   :2024/2/26 9:23
 */

namespace App\Http\Controllers\Aside\KeywordVideo;

use App\Enums\Common\Code;
use App\Http\Controllers\Aside\BaseController;
use App\Models\Com\KeywordVideoTask;
use App\Models\Com\KeywordVideoTaskLog;
use App\Models\Domain\DomainInfo;
use Illuminate\Support\Facades\Cache;

class KeywordVideoController extends BaseController
{
    /**
     * @remark :任务列表
     * @name   :lists
     * @author :lyh
     * @method :post
     * @time   :2024/2/26 11:36
     */
    public function lists(){
        $keywordModel  = new KeywordVideoTask();
        $query = $keywordModel->leftJoin('gl_project', 'gl_keyword_video_task.project_id', '=', 'gl_project.id');
        $query = $this->searchParam($query);
        $query = $this->orderByList($query);
        $lists = $query->paginate($this->row, $this->selectParam(), 'page', $this->page)->toArray();
        $this->response('success',Code::SUCCESS,$lists);
    }
    /**
     * 需要查询的字段
     * @return array
     */
    public function selectParam(){
        $select = [
            'gl_keyword_video_task.id AS id',
            'gl_keyword_video_task.project_id AS project_id',
            'gl_keyword_video_task.number AS number',
            'gl_keyword_video_task.status AS status',
            'gl_keyword_video_task.keywords AS keywords',
            'gl_keyword_video_task.sort AS sort',
            'gl_keyword_video_task.created_at AS created_at',
            'gl_keyword_video_task.updated_at AS updated_at',
            'gl_project.title AS title',
            'gl_project.company AS company',
        ];
        return $select;
    }
    /**
     * @remark :搜索参数处理
     * @name   :searchParam
     * @author :lyh
     * @method :post
     * @time   :2023/8/18 10:58
     */
    public function searchParam(&$query){
        if(isset($this->map['title'])){
            $query->where('gl_project.title','like','%'.$this->map['title'].'%');
        }
        if(isset($this->map['status'])){
            $query->where('gl_keyword_video_task.status',$this->map['status']);
        }
        return $query;
    }

    /**
     * @remark :排序
     * @name   :orderByList
     * @author :lyh
     * @method :post
     * @time   :2023/12/29 17:14
     */
    public function orderByList(&$query){
        $query = $query->orderBy('gl_keyword_video_task.sort', 'desc')->orderBy('gl_keyword_video_task.id', 'desc');
        return $query;
    }

    /**
     * @remark :创建关键字任务池子
     * @name   :saveKeyword
     * @author :lyh
     * @method :post
     * @time   :2024/2/26 9:24
     */
    public function createKeywordTask(){
        $this->request->validate([
            'project_id'=>'required',
            'number'=>'required'
        ], [
            'project_id.required' => '项目唯一标识不为空',
            'number.required' => 'number不为空',
        ]);
        //查看当前项目是否已添加
        $keywordModel  = new KeywordVideoTask();
        $keywordInfo = $keywordModel->read(['project_id'=>$this->param['project_id']]);
        if($keywordInfo !== false){
            $this->response('当前项目已添加');
        }
        //查看当前项目是否有正式域名
        $domainModel = new DomainInfo();
        $info = $domainModel->read(['project_id'=>$this->param['project_id']]);
        if($info === false){
            $this->response('请先设置域名',Code::SYSTEM_ERROR);
        }
        //组装数据
        $data = [
            'project_id'=>$this->param['project_id'],
            'number'=>$this->param['number'],
            'status'=>$this->param['status'] ?? 0,
            'sort'=>$this->param['sort'] ?? 0,
            'keywords'=>$this->param['keywords'] ?? '',
            'logo_img'=>$this->param['logo_img'] ?? '',
            'template_data'=>isset($this->param['template_data']) ? json_encode($this->param['template_data'],true) : null
        ];
        $rs = $keywordModel->add($data);
        if($rs === false){
            $this->response('添加失败',Code::SYSTEM_ERROR);
        }
        $this->response('success');
    }

    /**
     * @remark :修改项目
     * @name   :editSort
     * @author :lyh
     * @method :post
     * @time   :2024/2/26 11:35
     */
    public function edit(){
        $this->request->validate([
            'id'=>'required'
        ], [
            'id.required' => '主键标识不为空',
        ]);
        $keywordModel  = new KeywordVideoTask();
        $rs = $keywordModel->edit($this->param,['id'=>$this->param['id']]);
        if($rs === false){
            $this->response('编辑失败',Code::SYSTEM_ERROR);
        }
        $this->response('success');
    }

    /**
     * @remark :记录
     * @name   :getVideoTaskLog
     * @author :lyh
     * @method :post
     * @time   :2024/3/1 16:40
     */
    public function getVideoTaskLog(){
        $this->request->validate([
            'project_id'=>'required',
        ], [
            'project_id.required' => '项目唯一标识不为空',
        ]);
        $taskLogModel = new KeywordVideoTaskLog();
        $list = $taskLogModel->lists($this->map,$this->page,$this->row);
        $this->response('success',Code::SUCCESS,$list);
    }


    /**
     * @remark :获取任务数量
     * @name   :taskNum
     * @author :lyh
     * @method :post
     * @time   :2024/6/13 16:16
     */
    public function taskNum(){
        $keywordModel  = new KeywordVideoTask();
        $data['total'] = $keywordModel->count();
        $data['start_total'] = $keywordModel->formatQuery(['status'=>0])->count();
        $data['end_total'] = $keywordModel->formatQuery(['status'=>1])->count();
        $this->response('success',Code::SUCCESS,$data);
    }

    /**
     * @remark :当月视频总数量
     * @name   :getVideoCount
     * @author :lyh
     * @method :post
     * @time   :2024/8/14 9:58
     */
    public function getVideoCount(){
        // 当月的开始时间
        $start_of_month = date("Y-m-01 00:00:00");
        // 当月的结束时间:使用 strtotime() 计算最后一天
        $end_of_month = date("Y-m-d 23:59:59", strtotime("last day of this month"));
        $taskLogModel = new KeywordVideoTaskLog();
        $count = $taskLogModel->formatQuery(['created_at'=>['between',[$start_of_month,$end_of_month]]])->count();
        $this->response('success',Code::SUCCESS,['count'=>$count]);
    }

    /**
     * @remark :获取模版库模版
     * @name   :get_template
     * @author :lyh
     * @method :post
     * @time   :2024/11/1 11:48
     */
    public function get_template(){
        $data = Cache::get('template_data');
        if(empty($data)){
            $url = 'http://216.250.255.116:7866/get_template';
            $header = [
                'accept: application/json'
            ];
            $data = http_get($url,$header);
        }
        $this->response('success',Code::SUCCESS,$data);
    }
}