74 lines
2.6 KiB
PHP
74 lines
2.6 KiB
PHP
<?php
|
||
/**
|
||
* 企业产品列表接口 GET /api/company/products.php
|
||
* v1.0.18:返回产品基础字段(产品品类/基本定位/包含系列/状态/更新日期)+ EAV 参数值(JSON)
|
||
* 入参:company_id(必填)/ page / limit
|
||
*/
|
||
require_once __DIR__ . '/../common/db.php';
|
||
require_once __DIR__ . '/../common/response.php';
|
||
require_once __DIR__ . '/../common/auth.php';
|
||
|
||
checkPermission('company');
|
||
|
||
$companyId = (int)($_REQUEST['company_id'] ?? 0);
|
||
if ($companyId <= 0) {
|
||
Response::error('参数错误', 400);
|
||
}
|
||
|
||
[$page, $limit] = pageParams(5);
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
|
||
$chk = $pdo->prepare("SELECT COUNT(*) FROM companies WHERE id = ?");
|
||
$chk->execute([$companyId]);
|
||
if ((int)$chk->fetchColumn() === 0) {
|
||
Response::error('企业不存在');
|
||
}
|
||
|
||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM company_products WHERE company_id = ? AND is_active = 1");
|
||
$stmt->execute([$companyId]);
|
||
$total = (int)$stmt->fetchColumn();
|
||
|
||
$offset = ($page - 1) * $limit;
|
||
$stmt = $pdo->prepare(
|
||
"SELECT id, category_name, category_description, positioning, series, is_core, is_active, created_at, updated_at
|
||
FROM company_products
|
||
WHERE company_id = ? AND is_active = 1
|
||
ORDER BY is_core DESC, id DESC
|
||
LIMIT $limit OFFSET $offset"
|
||
);
|
||
$stmt->execute([$companyId]);
|
||
$list = $stmt->fetchAll();
|
||
|
||
// 批量取 EAV 参数值(每个产品一个 JSON:attr_id => 值)
|
||
if ($list) {
|
||
$ids = array_column($list, 'id');
|
||
$in = implode(',', array_fill(0, count($ids), '?'));
|
||
$valStmt = $pdo->prepare(
|
||
"SELECT v.product_id, a.attr_name, a.attr_type, a.unit,
|
||
v.value_string, v.value_number, v.value_boolean, v.value_date
|
||
FROM company_products_attr_value v
|
||
INNER JOIN company_products_attr a ON a.id = v.attr_id
|
||
WHERE v.product_id IN ($in)"
|
||
);
|
||
$valStmt->execute($ids);
|
||
$values = [];
|
||
foreach ($valStmt as $v) {
|
||
$values[$v['product_id']][] = [
|
||
'attr_name' => $v['attr_name'],
|
||
'attr_type' => $v['attr_type'],
|
||
'unit' => $v['unit'],
|
||
'value' => $v['value_string'] !== null ? $v['value_string']
|
||
: ($v['value_number'] !== null ? rtrim(rtrim(sprintf('%.4f', $v['value_number']), '0'), '.')
|
||
: ($v['value_boolean'] !== null ? ($v['value_boolean'] ? '是' : '否')
|
||
: ($v['value_date'] ?? ''))),
|
||
];
|
||
}
|
||
foreach ($list as &$row) {
|
||
$row['attrs'] = $values[$row['id']] ?? [];
|
||
}
|
||
unset($row);
|
||
}
|
||
|
||
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);
|