GeoQuestionLogic.php 4.7 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\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(){
        //处理数据
        $count = count($this->param['question']);
        $sum = $this->model->where('proejct_id',$this->param['project_id'])->sum('question_num') ?? 0;
        if($sum >= 200 || $count >= 200){
            $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);
        if($count <= 20){
            $this->param['question_num'] = $count;
            if(isset($this->param['id']) && !empty($this->param['id'])){
                $id = $this->param['id'];
                $this->model->edit($this->param,['id'=>$id]);
            }else{
                $this->param['next_time'] = date('Y-m-d');
                $id = $this->model->addReturnId($this->param);
            }
            return $this->success(['id'=>$id]);
        }else{
            $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'] = 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);
    }
}