v1.0.12: 企业/人员/媒体去删除入口;企业/人员加产品入口;企业详情去产品品类/关联联系人;编辑企业改Tab卡(工商信息/财务信息/资质认证)

This commit is contained in:
nanguaboss
2026-08-03 23:22:33 +08:00
parent 71c6e8d9c1
commit 96cba121a8
14 changed files with 644 additions and 83 deletions
+112
View File
@@ -90,3 +90,115 @@ function buildUpdate($data)
}
return [implode(',', $sets), array_values($data)];
}
/** 数字字符串或 null(空串转 null,避免写入空值) */
function numOrNull($v)
{
$v = trim((string)$v);
return ($v === '') ? null : $v;
}
/** 字符串或 null(空串转 null) */
function strOrNull($v)
{
if ($v === null) {
return null;
}
$v = trim((string)$v);
return ($v === '') ? null : $v;
}
/**
* 保存企业年度财务明细(整表替换:先删后插)
* @param PDO $pdo
* @param int $companyId
* @param array $financials JSON 解码后的数组
*/
function saveCompanyFinancials($pdo, $companyId, $financials)
{
$del = $pdo->prepare("DELETE FROM company_financials WHERE company_id = ?");
$del->execute([$companyId]);
if (empty($financials)) {
return;
}
$ins = $pdo->prepare(
"INSERT INTO company_financials
(company_id, fiscal_year, employee_count, total_revenue, net_profit, total_assets,
total_liabilities, owner_equity, gross_margin, net_margin, debt_ratio, financial_report_url, data_source)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"
);
$seen = [];
foreach ($financials as $f) {
if (!is_array($f)) {
continue;
}
$year = (int)($f['fiscal_year'] ?? 0);
if ($year < 1990 || $year > 2100) {
continue;
}
if (isset($seen[$year])) {
continue; // 同一财年只保留一条
}
$seen[$year] = true;
$ins->execute([
$companyId,
$year,
numOrNull($f['employee_count'] ?? null),
numOrNull($f['total_revenue'] ?? null),
numOrNull($f['net_profit'] ?? null),
numOrNull($f['total_assets'] ?? null),
numOrNull($f['total_liabilities'] ?? null),
numOrNull($f['owner_equity'] ?? null),
numOrNull($f['gross_margin'] ?? null),
numOrNull($f['net_margin'] ?? null),
numOrNull($f['debt_ratio'] ?? null),
strOrNull($f['financial_report_url'] ?? null),
strOrNull($f['data_source'] ?? null) ?? '手动录入',
]);
}
}
/**
* 保存企业资质认证(整表替换:先删后插)
* @param PDO $pdo
* @param int $companyId
* @param array $certifications JSON 解码后的数组
*/
function saveCompanyCertifications($pdo, $companyId, $certifications)
{
$del = $pdo->prepare("DELETE FROM company_certifications WHERE company_id = ?");
$del->execute([$companyId]);
if (empty($certifications)) {
return;
}
$valid = $pdo->prepare("SELECT id FROM certification_types WHERE id = ?");
$ins = $pdo->prepare(
"INSERT INTO company_certifications
(company_id, certification_type_id, certificate_number, issue_date, expiry_date)
VALUES (?,?,?,?,?)"
);
$seen = [];
foreach ($certifications as $ct) {
if (!is_array($ct)) {
continue;
}
$tid = (int)($ct['certification_type_id'] ?? 0);
if ($tid <= 0 || isset($seen[$tid])) {
continue;
}
$valid->execute([$tid]);
if (!$valid->fetch()) {
continue; // 认证类型不存在
}
$seen[$tid] = true;
$ins->execute([
$companyId,
$tid,
strOrNull($ct['certificate_number'] ?? null),
strOrNull($ct['issue_date'] ?? null),
strOrNull($ct['expiry_date'] ?? null),
]);
}
}
+27
View File
@@ -26,5 +26,32 @@ $pdo = DB::getInstance()->getPdo();
$pdo->prepare("INSERT INTO companies $sql")->execute($params);
$newId = (int)$pdo->lastInsertId();
// 财务信息 / 资质认证(可选,整表替换)
if (array_key_exists('financials', $_POST) || array_key_exists('certifications', $_POST)) {
$pdo->beginTransaction();
try {
if (array_key_exists('financials', $_POST)) {
$financials = json_decode($_POST['financials'], true);
if (!is_array($financials)) {
$pdo->rollBack();
Response::error('财务信息格式错误', 400);
}
saveCompanyFinancials($pdo, $newId, $financials);
}
if (array_key_exists('certifications', $_POST)) {
$certs = json_decode($_POST['certifications'], true);
if (!is_array($certs)) {
$pdo->rollBack();
Response::error('资质认证格式错误', 400);
}
saveCompanyCertifications($pdo, $newId, $certs);
}
$pdo->commit();
} catch (Exception $e) {
$pdo->rollBack();
Response::error('关联信息保存失败:' . $e->getMessage());
}
}
logCurrent('add', 'company', 'companies', $newId, $data);
Response::success(['id' => $newId], '新增成功');
+15
View File
@@ -0,0 +1,15 @@
<?php
/**
* 认证类型字典接口 GET /api/company/cert_types.php
* 返回启用中的认证类型(如:瞪羚企业、国家高新技术企业等),供编辑弹窗下拉使用
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
checkPermission('company');
$pdo = DB::getInstance()->getPdo();
$types = $pdo->query("SELECT id, name FROM certification_types WHERE is_active = 1 ORDER BY sort_order, id")->fetchAll();
Response::success(['types' => $types]);
+15
View File
@@ -48,6 +48,19 @@ $relations = $pdo->prepare(
);
$relations->execute([$id]);
// 资质认证(瞪羚企业等,JOIN 字典表取名称)
$certs = $pdo->prepare(
"SELECT cc.certification_type_id, ct.name AS certification_name, cc.certificate_number,
cc.issue_date, cc.expiry_date
FROM company_certifications cc
INNER JOIN certification_types ct ON ct.id = cc.certification_type_id
WHERE cc.company_id = ?
ORDER BY ct.sort_order, ct.id"
);
$certs->execute([$id]);
$certTypes = $pdo->query("SELECT id, name FROM certification_types WHERE is_active = 1 ORDER BY sort_order, id")->fetchAll();
// 关联联系人(工作经历 + 联系方式)
$contacts = $pdo->prepare(
"SELECT p.id, p.full_name,
@@ -71,4 +84,6 @@ Response::success([
'financials' => $financials->fetchAll(),
'relations' => $relations->fetchAll(),
'contacts' => $contacts->fetchAll(),
'certifications' => $certs->fetchAll(),
'certification_types' => $certTypes,
]);
+4
View File
@@ -38,6 +38,10 @@ if ($jobLevel !== '') {
$where[] = 'pwe.job_level = ?';
$params[] = $jobLevel;
}
if (isset($_REQUEST['is_current']) && $_REQUEST['is_current'] !== '') {
$where[] = 'pwe.is_current = ?';
$params[] = (int)$_REQUEST['is_current'] ? 1 : 0;
}
$whereSql = implode(' AND ', $where);
// 总数
+41
View File
@@ -0,0 +1,41 @@
<?php
/**
* 企业产品品类列表接口 GET /api/company/products.php
* 入参: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, is_core, is_active
FROM company_products
WHERE company_id = ? AND is_active = 1
ORDER BY is_core DESC, id DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute([$companyId]);
Response::success(['list' => $stmt->fetchAll(), 'total' => $total, 'page' => $page, 'limit' => $limit]);
+38 -5
View File
@@ -19,7 +19,9 @@ if ($id <= 0) {
$data = extractFields(COMPANY_FIELDS);
unset($data['display_name']); // 显示名称不允许通过编辑接口置空/改名,如需改名请走完整字段
if (empty($data)) {
$hasFinancials = array_key_exists('financials', $_POST);
$hasCertifications = array_key_exists('certifications', $_POST);
if (empty($data) && !$hasFinancials && !$hasCertifications) {
Response::error('没有需要更新的字段', 400);
}
@@ -32,9 +34,40 @@ if (!$old) {
Response::error('企业不存在');
}
[$sets, $params] = buildUpdate($data);
$params[] = $id;
$pdo->prepare("UPDATE companies SET $sets WHERE id = ?")->execute($params);
if (!empty($data)) {
[$sets, $params] = buildUpdate($data);
$params[] = $id;
$pdo->prepare("UPDATE companies SET $sets WHERE id = ?")->execute($params);
}
logCurrent('update', 'company', 'companies', $id, ['before' => $old, 'after' => $data]);
// 财务信息 / 资质认证(可选,整表替换)
if ($hasFinancials || $hasCertifications) {
$pdo->beginTransaction();
try {
if ($hasFinancials) {
$financials = json_decode($_POST['financials'], true);
if (!is_array($financials)) {
$pdo->rollBack();
Response::error('财务信息格式错误', 400);
}
saveCompanyFinancials($pdo, $id, $financials);
}
if ($hasCertifications) {
$certs = json_decode($_POST['certifications'], true);
if (!is_array($certs)) {
$pdo->rollBack();
Response::error('资质认证格式错误', 400);
}
saveCompanyCertifications($pdo, $id, $certs);
}
$pdo->commit();
} catch (Exception $e) {
$pdo->rollBack();
Response::error('关联信息保存失败:' . $e->getMessage());
}
}
if (!empty($data)) {
logCurrent('update', 'company', 'companies', $id, ['before' => $old, 'after' => $data]);
}
Response::success(null, '更新成功');
+12 -4
View File
@@ -25,12 +25,20 @@ if ((int)$chk->fetchColumn() === 0) {
Response::error('人员不存在');
}
$where = ['pwe.person_id = ?', 'pwe.is_active = 1'];
$params = [$personId];
if (isset($_REQUEST['is_current']) && $_REQUEST['is_current'] !== '') {
$where[] = 'pwe.is_current = ?';
$params[] = (int)$_REQUEST['is_current'] ? 1 : 0;
}
$whereSql = implode(' AND ', $where);
$stmt = $pdo->prepare(
"SELECT COUNT(*) FROM person_work_experiences pwe
INNER JOIN companies c ON c.id = pwe.company_id
WHERE pwe.person_id = ? AND pwe.is_active = 1"
WHERE $whereSql"
);
$stmt->execute([$personId]);
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$offset = ($page - 1) * $limit;
@@ -42,11 +50,11 @@ $stmt = $pdo->prepare(
pwe.start_date, pwe.end_date, pwe.is_current
FROM person_work_experiences pwe
INNER JOIN companies c ON c.id = pwe.company_id
WHERE pwe.person_id = ? AND pwe.is_active = 1
WHERE $whereSql
ORDER BY pwe.is_current DESC, pwe.start_date DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute([$personId]);
$stmt->execute($params);
$list = $stmt->fetchAll();
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);
+48
View File
@@ -0,0 +1,48 @@
<?php
/**
* 人员相关产品品类列表接口 GET /api/person/products.php
* 入参:person_id(必填)/ page / limit
* 数据源:该人员任职公司的产品品类(去重)
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
checkPermission('person');
$personId = (int)($_REQUEST['person_id'] ?? 0);
if ($personId <= 0) {
Response::error('参数错误', 400);
}
[$page, $limit] = pageParams(5);
$pdo = DB::getInstance()->getPdo();
$chk = $pdo->prepare("SELECT COUNT(*) FROM persons WHERE id = ?");
$chk->execute([$personId]);
if ((int)$chk->fetchColumn() === 0) {
Response::error('人员不存在');
}
$join = "FROM person_work_experiences pwe
INNER JOIN company_products cp ON cp.company_id = pwe.company_id AND cp.is_active = 1
INNER JOIN companies c ON c.id = pwe.company_id
WHERE pwe.person_id = ? AND pwe.is_active = 1";
$stmt = $pdo->prepare("SELECT COUNT(DISTINCT cp.id) $join");
$stmt->execute([$personId]);
$total = (int)$stmt->fetchColumn();
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
"SELECT DISTINCT cp.id, cp.category_name, cp.category_description, cp.is_core, cp.is_active,
CASE WHEN c.name_zh IS NOT NULL AND c.name_zh <> '' THEN c.name_zh
ELSE COALESCE(c.name_en, c.display_name) END AS company_name
$join
ORDER BY company_name, cp.id DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute([$personId]);
Response::success(['list' => $stmt->fetchAll(), 'total' => $total, 'page' => $page, 'limit' => $limit]);