GeoQuestionLogic.php 5.5 KB
<?php
/**
 * @remark :
 * @name   :GeoQuestionLogic.php
 * @author :lyh
 * @method :post
 * @time   :2025/7/2 17:51
 */

namespace App\Http\Logic\Aside\Geo;

use App\Http\Logic\Aside\BaseLogic;
use App\Models\Geo\GeoPlatform;
use App\Models\Geo\GeoQuestion;
use App\Models\Geo\GeoQuestionResult;
use App\Models\Project\Project;

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

    /**
     * @remark :设置geo状态
     * @name   :setGeoStatus
     * @author :lyh
     * @method :post
     * @time   :2025/7/2 17:51
     */
    public function setGeoStatus(){
        $projectModel = new Project();
        $data = $projectModel->edit(['geo_status'=>$this->param['geo_status'],'geo_frequency'=>$this->param['geo_frequency']],['id'=>$this->param['project_id']]);
        $questionModel = new GeoQuestion();
        $questionModel->edit(['status'=>$this->param['geo_status']],['project_id'=>$this->param['project_id']]);
        return $this->success($data);
    }

    /**
     * @remark :获取类型
     * @name   :getType
     * @author :lyh
     * @method :post
     * @time   :2025/7/3 10:47
     */
    public function getType(){
        $data['type'] = $this->model->brandType();
        $data['frequency'] = $this->model->frequency;
        $geoPlatformModel = new GeoPlatform();
        $data['platform'] = $geoPlatformModel->getList();
        return $this->success($data);
    }

    /**
     * @remark :获取问题列表
     * @name   :getGeoQuestionList
     * @author :lyh
     * @method :post
     * @time   :2025/7/3 9:12
     */
    public function getGeoQuestionList($map,$page,$row,$order,$field = ['*']){
        $data = $this->model->lists($map,$page,$row,$order,$field);
        if(!empty($data) && !empty($data['list'])){
            foreach ($data['list'] as $key => $item){
                $item['type_name'] = $this->model->brandType()[$item['type']];
                $data['list'][$key] = $item;
            }
        }
        return $this->success($data);
    }

    /**
     * @remark :保存数据
     * @name   :saveGeoQuestion
     * @author :lyh
     * @method :post
     * @time   :2025/7/3 9:47
     * @param  : question->提交的问题
     * @param  : url->提交的网址
     * @param  : keywords->提交的关键字
     * @param  : project_id->项目id
     */
    public function saveGeoQuestion(){
        //处理数据
        $max_num = 200;//最大数量
        $count = count($this->param['question']);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            //编辑
            $sum = $this->model->where('project_id',$this->param['project_id'])->where('id','!=',$this->param['id'])->sum('question_num') ?? 0;
        }else{
            //新增
            $sum = $this->model->where('project_id',$this->param['project_id'])->sum('question_num') ?? 0;
        }
        if(($count + $sum) > $max_num){
            $this->fail('当前问题数量大于最大数量200个问题,不允许保存');
        }
        $question = $this->param['question'];
        $this->param['url'] = json_encode($this->param['url'] ?? [],true);
        $this->param['keywords'] = json_encode($this->param['keywords'] ?? [],true);
        $chunks = array_chunk($question, 20);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            foreach ($chunks as $index => $chunk) {
                $this->param['question_num'] = count($chunk);
                $this->param['question'] = json_encode($chunk ?? [],true);
                if($index == 0){
                    $id = $this->param['id'];
                    $this->model->edit($this->param,['id'=>$id]);
                }else{
                    unset($this->param['id']);
                    $id = $this->model->addReturnId($this->param);
                }
            }
        }else{
            $this->param['next_time'] = date('Y-m-d');
            foreach ($chunks as $chunk) {
                $this->param['question_num'] = count($chunk);
                $this->param['question'] = json_encode($chunk ?? [],true);
                $id = $this->model->addReturnId($this->param);
            }
        }
        return $this->success(['id'=>$id]);

    }

    /**
     * @remark :删除数据
     * @name   :delGeoQuestion
     * @author :lyh
     * @method :post
     * @time   :2025/7/3 10:13
     */
    public function delGeoQuestion(){
        $data = $this->model->del(['id'=>['in',$this->param['ids']]]);
        return $this->success($data);
    }

    /**
     * 获取未名字的问题列表
     * @return array
     * @author Akun
     * @date 2025/11/25 16:44
     */
    public function getUnHitQuestionList(){
        $data = GeoQuestionResult::where('project_id',$this->param['project_id'])->where('hit',0)->select('platform','question')->get()->toArray();
        $list = [];
        if(!empty($data)){
            $temp = [];
            $question_list = [];
            foreach ($data as $item){
                if(in_array($item['question'],$question_list)){
                    array_push($temp[$item['question']],$item['platform']);
                }else{
                    $temp[$item['question']][] = $item['platform'];
                    $question_list[] = $item['question'];
                }
            }

            if(!empty($temp)){
                foreach ($temp as $kt=>$vt){
                    $list[] = ['question'=>$kt,'platform'=>$vt];
                }
            }
        }
        return $this->success($list);
    }
}