UpdateController.php 9.7 KB
<?php
/**
 * @remark :
 * @name   :UpdateController.php
 * @author :lyh
 * @method :post
 * @time   :2023/8/19 9:08
 */

namespace App\Http\Controllers\Aside\Com;

use App\Http\Controllers\Bside\BaseController;
use App\Models\Com\UpdateLog;
use App\Models\Com\UpdateOldInfo;
use App\Models\Domain\DomainInfo;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Project\ProjectUpdateTdk;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;

/**
 * @remark :b端网站更新相关
 * @name   :UpdateController
 * @author :lyh
 * @method :post
 * @time   :2023/8/19 9:08
 */
class UpdateController extends BaseController
{
    /**
     * @remark :一键更新所有tdk
     * @name   :updateSeoTdk
     * @author :lyh
     * @method :post
     * @time   :2023/8/19 9:25
     */
    public function updateSeoTdk()
    {
        $this->request->validate([
            'project_id' => 'required',
        ], [
            'project_id.required' => 'project_id不能为空',
        ]);
        try {
            ProjectUpdateTdk::add_task($this->param['project_id']);
        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }
        $this->response('任务添加成功');
    }

    /**
     * 采集项目所有内容
     * @author Akun
     * @date 2023/11/24 11:33
     */
    public function dataCollect()
    {
        $this->request->validate([
            'project_id' => 'required',
            'type' => 'required',
            'old_collect' => 'required',
        ], [
            'project_id.required' => 'project_id不能为空',
            'type.required' => '是否重新采集分类不能为空',
            'old_collect.required' => '现有数据是否重新采集页面不能为空',
        ]);

        $collect_un = UpdateLog::where('project_id', $this->param['project_id'])->where('collect_status', 0)->get();
        if ($collect_un->count() > 0) {
            $this->fail('项目正在采集中');
        }

        $project = ProjectServer::useProject($this->param['project_id']);

        if (!$project) {
            $this->fail('项目不存在');
        }

        if ($project->is_upgrade != 1) {
            $this->fail('非升级无法进行采集操作');
        }

        $test_domain = $this->param['test_domain'] ?? '';
        if ($test_domain) {
            $test_domain_arr = parse_url($test_domain);
            $test_domain = $test_domain_arr['host'] ?? '';
        }

        //查看项目是否已上线
        $domain_info = DomainInfo::where('project_id', $this->param['project_id'])->first();

        if ($domain_info && !$test_domain) {
            $this->fail('已上线项目需填写采集的测试站域名');
        }

        $product_cate_type = 0;//产品分类是否重采
        $news_cate_type = 0;//新闻分类是否重采

        if ($this->param['type'] == 1) {
            $product_cate_type = 1;
            $news_cate_type = 1;

            $bSettingModel = new Setting();
            $template_info = $bSettingModel->read(['project_id' => $this->param['project_id']]);
            $template_id = $template_info ? $template_info['template_id'] : 0;//获取模版id

            //产品分类重采之前,判断产品分类是否开启了可视化
            $category_list = Category::where('project_id', $this->param['project_id'])->get();
            if ($category_list->count() > 0) {
                foreach ($category_list as $category) {
                    if ($this->getRenovation($this->param['project_id'], BTemplate::SOURCE_PRODUCT, BTemplate::IS_LIST, $template_id, $category['id']) == 1) {
                        //有分类开启了可视化
                        $product_cate_type = 0;
                        break;
                    }
                }
            }

            //新闻分类重采之前,判断新闻分类是否开启了可视化
            $category_news_list = NewsCategory::where('project_id', $this->param['project_id'])->get();
            if ($category_news_list->count() > 0) {
                foreach ($category_news_list as $category_news) {
                    if ($this->getRenovation($this->param['project_id'], BTemplate::SOURCE_NEWS, BTemplate::IS_LIST, $template_id, $category_news['id']) == 1) {
                        //有分类开启了可视化
                        $news_cate_type = 0;
                        break;
                    }
                }
            }
        }

        try {
            if ($this->param['old_collect'] == 1) {
                //现有数据需要重新采集页面
                $collect_routes = '';
                if (isset($this->param['collect_routes'])) {
                    //填写了采集路由
                    $routes_arr = explode(',', $this->param['collect_routes']);
                    foreach ($routes_arr as &$route) {
                        $route = "'" . $route . "'";
                    }
                    $collect_routes = implode(',', $routes_arr);
                }

                DB::connection('custom_mysql')->statement("DELETE FROM `gl_collect_source`  WHERE `origin` LIKE '%.css%' OR `origin` LIKE '%.js%'");
                if ($domain_info) {
                    //已上线项目
                    if ($collect_routes) {
                        DB::connection('custom_mysql')->statement("UPDATE `gl_collect_task` SET `status` = 0,`domain` = '" . $test_domain . "' WHERE `language` = '' AND `route` IN (" . $collect_routes . ")");
                    } else {
                        DB::connection('custom_mysql')->statement("UPDATE `gl_collect_task` SET `status` = 0,`domain` = '" . $test_domain . "' WHERE `language` = ''");
                    }
                } else {
                    if ($collect_routes) {
                        DB::connection('custom_mysql')->statement("UPDATE `gl_collect_task` SET `status` = 0 WHERE `language` = '' AND `route` IN (" . $collect_routes . ")");
                    } else {
                        DB::connection('custom_mysql')->statement("UPDATE `gl_collect_task` SET `status` = 0 WHERE `language` = ''");
                    }
                }
            }

            if ($product_cate_type == 1) {
                //需要重新采集产品分类
                DB::connection('custom_mysql')->statement("TRUNCATE `gl_product_category`");
                DB::connection('custom_mysql')->statement("TRUNCATE `gl_product_category_related`");

                DB::connection('custom_mysql')->statement("DELETE FROM `gl_route_map`  WHERE `source` = 'product_category'");
            }

            if ($news_cate_type == 1) {
                //需要重新采集新闻分类
                DB::connection('custom_mysql')->statement("TRUNCATE `gl_news_category`");

                DB::connection('custom_mysql')->statement("DELETE FROM `gl_route_map`  WHERE `source` = 'news_category'");
            }
        } catch (\Exception $e) {
            errorLog('重新采集升级项目数据', $this->param, $e);

            $this->fail('采集任务添加失败');
        }

        //关闭数据库
        DB::disconnect('custom_mysql');

        if ($domain_info) {
            $old_info = UpdateOldInfo::where('project_id', $this->param['project_id'])->first();
            if (!$old_info) {
                $old_info = new UpdateOldInfo();
                $old_info->project_id = $this->param['project_id'];
                $old_info->link_type = 0;
                $old_info->old_domain_online = $domain_info->domain;
            }
            $old_info->old_domain_test = $test_domain;
            $old_info->save();
        }

        if ($product_cate_type == 1 && $news_cate_type == 1) {
            //需要重新采集产品分类和新闻分类
            $logs = UpdateLog::where('project_id', $this->param['project_id'])->orderBy('sort', 'asc')->get();
        } elseif ($product_cate_type == 1 && $news_cate_type == 0) {
            //只重采产品分类
            $logs = UpdateLog::where('project_id', $this->param['project_id'])->where('api_type', '!=', 'category_news')->orderBy('sort', 'asc')->get();
        } elseif ($product_cate_type == 0 && $news_cate_type == 1) {
            //只重采新闻分类
            $logs = UpdateLog::where('project_id', $this->param['project_id'])->where('api_type', '!=', 'category')->orderBy('sort', 'asc')->get();
        } else {
            //分类都不重采
            $logs = UpdateLog::where('project_id', $this->param['project_id'])->whereNotIn('api_type', ['category', 'category_news'])->orderBy('sort', 'asc')->get();
        }

        foreach ($logs as $log) {
            $log->status = 0;

            if (!in_array($log->api_type, ['website_info', 'tag', 'category', 'category_news'])) {
                //website_info,tag,category三种类型无需采集页面
                $log->collect_status = 0;
            }

            if ($domain_info) {
                //已上线项目
                $url_arr = parse_url($log->api_url);
                $api_url = str_replace($url_arr['host'], $test_domain, $log->api_url);
                $log->api_url = $api_url;
            }

            $log->save();
        }

        $this->response('采集任务添加成功');
    }

    public function getRenovation($project_id, $source, $is_list, $template_id, $id, $is_custom = 0)
    {
        $webTemplateModel = new BTemplate();
        $param = [
            'source' => $source,
            'project_id' => $project_id,
            'source_id' => $id,
            'template_id' => $template_id,
            'is_list' => $is_list,
            'is_custom' => $is_custom
        ];
        $templateInfo = $webTemplateModel->read($param);
        if ($templateInfo !== false) {
            return 1;
        }
        return 0;
    }
}