GeoQuestionResLogic.php 4.9 KB
<?php
/**
 * @remark :
 * @name   :GeoQuestionResLogic.php
 * @author :lyh
 * @method :post
 * @time   :2025/7/4 9:47
 */

namespace App\Http\Logic\Bside\Geo;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\Geo\GeoPlatform;
use App\Models\Geo\GeoQuestion;
use App\Models\Geo\GeoQuestionLog;
use App\Models\Geo\GeoQuestionResult;
use Illuminate\Support\Facades\DB;

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

    /**
     * @remark :获取类型统计数据
     * @name   :getCount
     * @author :lyh
     * @method :post
     * @time   :2025/7/8 17:16
     */
    public function getCount(){
        $total = $this->model->counts(['project_id'=>$this->user['project_id']]);
        $type_1 = $this->model->counts(['type'=>$this->model::BRAND_TYPE,'project_id'=>$this->user['project_id']]);
        $type_2 = $this->model->counts(['type'=>$this->model::MARKETING_TYPE,'project_id'=>$this->user['project_id']]);
        return $this->success(['total'=>$total,'type_1'=>$type_1,'type_2'=>$type_2]);
    }

    /**
     * @remark :获取列表页数据
     * @name   :getResultList
     * @author :lyh
     * @method :post
     * @time   :2025/7/4 9:48
     */
    public function getResultList($map = [],$page = 1,$row = 20){
        $map['project_id'] = $this->user['project_id'];
        $filed = ['id','project_id','question_id','platform','question','en_question','keywords','url','created_at','updated_at'];
        if(!empty($map['created_at'])){
            $map['created_at'] = ['between',[$map['created_at'].' 00:00:00',$map['created_at'].' 23:59:59']];
            $this->model = new GeoQuestionLog();
        }
        if(!empty($map['keywords'])){
            $map['keywords'] = ['like','%'.$map['keywords'].'%'];
        }
        $query = $this->model->formatQuery($map);
        $query = $query->where(function ($q) {
            $q->whereRaw('JSON_LENGTH(keywords) > 0')
                ->orWhereRaw('JSON_LENGTH(url) > 0');
        });
        $data = $query->orderByRaw('CHAR_LENGTH(question) ASC')->paginate($row, $filed, 'page', $page);;
        return $this->success($data);
    }

    /**
     * @remark :获取数据详情
     * @name   :getResultInfo
     * @author :lyh
     * @method :post
     * @time   :2025/7/4 10:19
     */
    public function getResultInfo(){
        $data = $this->model->read($this->param);
        return $this->success($data);
    }

    /**
     * @remark :统计数量
     * @name   :countQuantity
     * @author :lyh
     * @method :post
     * @time   :2025/7/21 11:12
     */
    public function countQuantity(){
        $questionModel = new GeoQuestion();
        $list = $questionModel->list(['project_id'=>$this->user['project_id']],['question','keywords','url']);
        $questionTotalCount = $urlTotalCount = $keywordsTotalCount = $keywordUrlCount = 0;
        $keywordArr = [];
        $questionLogModel = new GeoQuestionLog();
        $keywordUrlCount = $questionLogModel->counts(['project_id'=>$this->user['project_id'],'hit'=>['!=',0]]);
        foreach ($list as $item){
            $questionTotalCount += count($item['question'] ?? []);
            $keywordsTotalCount += count($item['keywords'] ?? []);
            $urlTotalCount += count($item['url'] ?? []);
            foreach ($item['keywords'] as $keyWordItem){
                if (!array_key_exists($keyWordItem, $keywordArr)) {
                    $keywordArr[$keyWordItem] = $questionLogModel->counts(['keywords'=>['like','%"'.$keyWordItem.'"%']]);
                }
            }
        }
        $data = [
            'keywords_count'=>$keywordsTotalCount,
            'url_count'=>$urlTotalCount,
            'question_count'=>$questionTotalCount,
            'keywords_url_count'=>$keywordUrlCount,
            'keywords_arr' => $keywordArr,
        ];
        return $this->success($data);
    }

    /**
     * @remark :按平台统计问题数量
     * @name   :platformHitCount
     * @author :lyh
     * @method :post
     * @time   :2025/7/21 15:00
     */
    public function platformHitCount(){
        $data = [];
        $platformModel = new GeoPlatform();
        $list = $platformModel->list(['status'=>1],'id',['name','en_name']);
        $questionResModel = new GeoQuestionLog();
        foreach ($list as $item){
             $data[$item['name']] = $questionResModel->counts(['project_id'=>$this->user['project_id'],'hit'=>['!=',0],'platform'=>$item['en_name']]);
        }
        return $this->success($data);
    }

    /**
     * @remark :获取搜索时间
     * @name   :getSearchDate
     * @author :lyh
     * @method :post
     * @time   :2025/7/21 16:36
     */
    public function getSearchDate(){
        $dates = $this->model->select(DB::raw('DATE(created_at) as date_only'))->distinct()->pluck('date_only');
        return $this->success($dates);
    }

}