tp3.1临时连接指定数据库,切片分类in查询,带过滤需要的数据

最近写了一段比较不错的代码,记录一下,tp3.1临时连接指定数据库,切片分类in查询,带过滤需要的数据

php 复制代码
/**
     * 获取季度报告总结
     * @author Bruce 2025/6/10
     */
    public function getQuarterReportSummarize() {
        // 获取全部数据
        $allData = $this->getAllProtectedDomains();

        if ($allData['code'] != 1) {
            return $this->ajaxError($allData['message']);
        }

        // 分批处理大量数据,每批500个
        $batchSize = 500;
        $result = [];
        $totalDomains = count($allData['list']);

        for ($i = 0; $i < $totalDomains; $i += $batchSize) {
            $batch = array_slice($allData['list'], $i, $batchSize);
            $configs = D('WangZhanDomainHost')->getConfigs($batch);

            foreach ($configs as $key=>$config) {
                $messages = [];
                if (!$config['iswaf']) $messages[] = 'Web漏洞防护未开启';
                if (!$config['cc_switch']) $messages[] = 'CC防护未开启';
                if (!$config['cs_switch']) $messages[] = '爬虫防护未开启';

                if (!empty($messages)) {
                    $result[$key] = implode(',', $messages) . ';';
                }
            }

            // 释放内存
            unset($batch, $configs);
        }

        return $this->ajaxSuccess($result);
    }
php 复制代码
# WangZhanDomainHostModel.class.php
/**
     * 获取配置信息(优化版)
     */
    public function getConfigs($data = [])
    {
        if (empty($data)) return [];

        // 提取所有host和zone组合
        $hosts = [];
        $zones = [];
        $hostZoneMap = [];

        foreach ($data as $item) {
            if (empty($item['host']) || empty($item['zone'])) continue;

            $host = addslashes($item['host']);
            $zone = addslashes($item['zone']);

            $hosts[$host] = $host;
            $zones[$zone] = $zone;
            $hostZoneMap[$host][$zone] = true;
        }

        if (empty($hosts) || empty($zones)) return [];

        // 使用IN查询替代大量OR条件
        $allConfigs = $this->alias('dh')
            ->join('LEFT JOIN anti_cc_config cc ON dh.host = cc.host AND dh.zone = cc.zone')
            ->join('LEFT JOIN anti_cs_config cs ON dh.host = cs.host AND dh.zone = cs.zone')
            ->where([
                'dh.host' => ['IN', array_unique($hosts)],
                'dh.zone' => ['IN', array_unique($zones)],
            ])
            ->field('dh.host, dh.zone, 
                IFNULL(dh.iswaf, 0) as iswaf,
                IFNULL(cc.main_switch, 0) as cc_switch,
                IFNULL(cs.main_switch, 0) as cs_switch')
            ->select();

        // 过滤出真正需要的记录
        $finalConfigs = [];
        foreach ($allConfigs as $config) {
            if (isset($hostZoneMap[$config['host']][$config['zone']])) {
                $finalConfigs[$config['host'].'.'.$config['zone']] = $config;
            }
        }

        return $finalConfigs;
    }
相关推荐
倔强的石头_10 小时前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
阿巴斯甜15 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker16 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952717 小时前
Andorid Google 登录接入文档
android
黄林晴18 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android