'company']); [$page, $limit] = pageParams(); $keyword = trim($_REQUEST['keyword'] ?? ''); $field = trim($_REQUEST['field'] ?? ''); $industry = trim($_REQUEST['industry'] ?? ''); $companyType = trim($_REQUEST['company_type'] ?? ''); $active = isset($_REQUEST['is_active']) && $_REQUEST['is_active'] !== '' ? (int)$_REQUEST['is_active'] : null; // 可指定筛选的字段白名单(下拉选项来自前端「筛选」) $searchableFields = [ 'name_zh', 'registration_number', 'legal_representative', 'address', 'industry', 'industry_subdivision', 'website', 'source_channel', 'source_detail', 'business_scope', 'stock_code', ]; $where = ['is_active = 1']; $params = []; if ($active !== null) { $where = ['is_active = ?']; $params = [$active]; } // 筛选弹窗多字段条件:filters=JSON数组 [{field,op,value}],filter_logic=and|or // 注意:必须在此处($where/$params 初始化后)追加,否则会被上面的赋值覆盖 $filtersJson = trim($_REQUEST['filters'] ?? ''); $filterLogic = strtoupper(trim($_REQUEST['filter_logic'] ?? 'and')) === 'OR' ? 'OR' : 'AND'; $filterConds = []; if ($filtersJson !== '') { $decoded = json_decode($filtersJson, true); if (is_array($decoded)) { foreach ($decoded as $c) { $fField = trim($c['field'] ?? ''); $fOp = trim($c['op'] ?? ''); $fVal = trim((string)($c['value'] ?? '')); if ($fField === '' || !in_array($fField, $searchableFields, true)) { continue; } switch ($fOp) { case 'contains': $filterConds[] = "$fField LIKE ?"; $params[] = "%$fVal%"; break; case 'eq': $filterConds[] = "$fField = ?"; $params[] = $fVal; break; case 'neq': $filterConds[] = "$fField <> ?"; $params[] = $fVal; break; case 'starts_with': $filterConds[] = "$fField LIKE ?"; $params[] = "$fVal%"; break; case 'ends_with': $filterConds[] = "$fField LIKE ?"; $params[] = "%$fVal"; break; case 'gt': $filterConds[] = "$fField > ?"; $params[] = $fVal; break; case 'lt': $filterConds[] = "$fField < ?"; $params[] = $fVal; break; case 'gte': $filterConds[] = "$fField >= ?"; $params[] = $fVal; break; case 'lte': $filterConds[] = "$fField <= ?"; $params[] = $fVal; break; case 'is_empty': $filterConds[] = "($fField IS NULL OR $fField = '')"; break; case 'is_not_empty': $filterConds[] = "($fField IS NOT NULL AND $fField <> '')"; break; default: break; } } } } if ($filterConds) { $where[] = '(' . implode(" $filterLogic ", $filterConds) . ')'; } if ($keyword !== '') { if ($field !== '' && in_array($field, $searchableFields, true)) { $where[] = "$field LIKE ?"; $params[] = "%$keyword%"; } else { // 全部字段:匹配常用字段 $where[] = '(name_zh LIKE ? OR display_name LIKE ? OR registration_number LIKE ? OR legal_representative LIKE ? OR industry LIKE ? OR industry_subdivision LIKE ? OR website LIKE ? OR source_detail LIKE ? OR stock_code LIKE ?)'; $like = "%$keyword%"; array_push($params, $like, $like, $like, $like, $like, $like, $like, $like, $like, $like); } } if ($industry !== '') { $where[] = 'industry = ?'; $params[] = $industry; } if ($companyType !== '') { $where[] = 'legal_form = ?'; $params[] = $companyType; } $whereSql = implode(' AND ', $where); $pdo = DB::getInstance()->getPdo(); $stmt = $pdo->prepare("SELECT COUNT(*) FROM companies WHERE $whereSql"); $stmt->execute($params); $total = (int)$stmt->fetchColumn(); $offset = ($page - 1) * $limit; $stmt = $pdo->prepare( "SELECT id, name_zh, display_name, business_role, legal_form, country, registration_number, address, legal_representative, industry, industry_subdivision, website, latest_employee_count, latest_annual_revenue, is_listed, stock_code, source_channel, source_detail, is_active, created_at, updated_at FROM companies WHERE $whereSql ORDER BY id DESC LIMIT $limit OFFSET $offset" ); $stmt->execute($params); $list = $stmt->fetchAll(); Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);