/**
* company.js - 企业数据管理逻辑(列表+筛选+分页 / 新增编辑弹窗 / 导入导出 / 详情)
*/
$(function () {
renderShell('企业数据', '数据管理 / 企业数据');
renderPage();
initPage('company', function (user) {
if (!checkPagePermission('company')) return;
loadList(1);
});
var currentPage = 1;
/** 渲染页面内容(工具栏+表格+分页) */
function renderPage() {
$('#page-content').html(
'
'
);
$('#check-all').on('change', function () { $('.row-check').prop('checked', this.checked); });
}
/* ---------- 列表 ---------- */
function loadList(page) {
currentPage = page;
var params = buildFilterParam();
params.page = page;
params.limit = PAGE_SIZE;
httpGet('company/list.php', params).then(function (data) {
var rows = data.list || [];
var html = '';
rows.forEach(function (r) {
html += '' +
' | ' +
'' + r.id + ' | ' +
'' + escHtml(r.name_zh || r.name_en || '-') + ' | ' +
'' + escHtml(r.industry || '-') + ' | ' +
'' + escHtml(r.industry_subdivision || '-') + ' | ' +
'' + escHtml(r.country || '-') + ' | ' +
'' + escHtml(r.business_role || '-') + ' | ' +
'' + escHtml(r.registration_number || '-') + ' | ' +
'' + escHtml(r.legal_representative || '-') + ' | ' +
'' + (r.is_listed ? '上市' : '-') + ' | ' +
'' + fmtDate(r.created_at) + ' | ' +
'' +
' 详情' +
' 员工' +
' 产品' +
' 编辑' +
' |
';
});
if (!rows.length) {
html = '| 暂无数据 |
';
}
$('#company-tbody').html(html);
renderPagination($('#pagination'), data, loadList);
});
}
/* ---------- 筛选 ---------- */
$('#btn-search').on('click', function () { loadList(1); });
$('#btn-reset').on('click', function () {
$('.toolbar [data-filter]').val('');
loadList(1);
});
/* ---------- 新增/编辑 ---------- */
$('#btn-add').on('click', function () {
httpGet('company/cert_types.php').then(function (d) {
loadDicts().then(function (dicts) {
openForm(null, [], [], d.types || [], dicts);
});
});
});
window.editCompany = function (id) {
httpGet('company/detail.php', { id: id }).then(function (d) {
loadDicts().then(function (dicts) {
openForm(d.company, d.financials, d.certifications, d.certification_types, dicts);
});
});
};
/** 拉取数据字典(国家/行业/来源渠道),供表单下拉使用 */
function loadDicts() {
return httpGet('common/dicts.php', { type: 'all' }).catch(function () {
return { countries: [], industries: [], source_channels: [] };
});
}
/** 生成下拉选项:value 与当前值匹配则选中;若当前值不在字典中则追加一个选项保留原值 */
function dictOptions(list, cur, labelKey) {
var html = '';
var found = false;
(list || []).forEach(function (item) {
var name = item[labelKey || 'name'];
var sel = (String(name) === String(cur)) ? ' selected' : '';
if (sel) found = true;
html += '';
});
if (cur && !found) {
html += '';
}
return html;
}
function openForm(company, financials, certifications, certTypes, dicts) {
var isEdit = !!company;
var c = company || {};
financials = financials || [];
certifications = certifications || [];
certTypes = certTypes || [];
dicts = dicts || {};
// Tab1 工商信息
var bizHtml =
'';
// Tab2 行业信息(业务角色/行业/行业细分/官网)
var indHtml =
'';
// Tab3 财务信息:顶部独立字段(是否上市/股票代码/最新员工/最新营收)+ 年度财务明细列表
var finRowsHtml = '';
if (!financials.length) {
finRowsHtml = finRowHtml(null);
} else {
financials.forEach(function (f) { finRowsHtml += finRowHtml(f); });
}
var finHtml =
'' +
'' +
' 年度财务明细(单位:万元)' +
' ' +
'
' +
'' +
' | 财年 | 员工数 | 总营收 | 净利润 | 总资产 | 总负债 | 所有者权益 | 毛利率(%) | 净利率(%) | 资产负债率(%) | 财务报表链接 | 数据来源 | 操作 |
' +
' ' + finRowsHtml + '' +
'
';
// Tab3 资质认证:列表形式(瞪羚企业等)
var certRowsHtml = '';
if (!certifications.length) {
certRowsHtml = certRowHtml(null, certTypes);
} else {
certifications.forEach(function (ct) { certRowsHtml += certRowHtml(ct, certTypes); });
}
var certHtml =
'' +
' 资质认证' +
' ' +
'
' +
'' + certRowsHtml + '
' +
'如:瞪羚企业、国家高新技术企业、专精特新企业、ISO9001认证等
';
var content =
'';
var idx = Dialog.open({
title: isEdit ? '编辑企业' : '新增企业',
area: ['820px', 'auto'],
content: content,
btn: false
});
$('#form-cancel').on('click', function () { Dialog.close(idx); });
$('#add-fin-row').on('click', function () { $('#fin-tbody').append(finRowHtml(null)); });
$('#add-cert-row').on('click', function () { $('#cert-rows').append(certRowHtml(null, certTypes)); });
// 工商查询按钮(预留:后续接入工商查询API自动填写)
$('#btn-gongshang-query').on('click', function () {
Dialog.alert('工商查询API预留中,接入后可通过企业名称自动查询并填写公司名称、注册号、注册地址等信息');
});
$('#company-form').on('submit', function (e) {
e.preventDefault();
var fd = new FormData(this);
var nameZh = fd.get('name_zh');
var nameEn = fd.get('name_en');
if (isEdit) fd.delete('display_name'); // 编辑不允许改显示名称
if (!isEdit && !nameZh && !nameEn) {
Dialog.error('公司名称或英文名称至少填写一项');
return;
}
// 年度财务明细(整表替换)
var financials = [];
$('#fin-tbody .fin-row').each(function () {
var $y = $(this);
var year = $.trim($y.find('[name="fin-fiscal_year"]').val());
if (!year) return;
financials.push({
fiscal_year: year,
employee_count: $y.find('[name="fin-employee_count"]').val(),
total_revenue: $y.find('[name="fin-total_revenue"]').val(),
net_profit: $y.find('[name="fin-net_profit"]').val(),
total_assets: $y.find('[name="fin-total_assets"]').val(),
total_liabilities: $y.find('[name="fin-total_liabilities"]').val(),
owner_equity: $y.find('[name="fin-owner_equity"]').val(),
gross_margin: $y.find('[name="fin-gross_margin"]').val(),
net_margin: $y.find('[name="fin-net_margin"]').val(),
debt_ratio: $y.find('[name="fin-debt_ratio"]').val(),
financial_report_url: $y.find('[name="fin-financial_report_url"]').val(),
data_source: $y.find('[name="fin-data_source"]').val()
});
});
fd.append('financials', JSON.stringify(financials));
// 资质认证(整表替换)
var certs = [];
$('#cert-rows .cert-row').each(function () {
var $r = $(this);
var tid = $r.find('[name="cert-type"]').val();
if (!tid) return;
certs.push({
certification_type_id: tid,
certificate_number: $r.find('[name="cert-number"]').val(),
issue_date: $r.find('[name="cert-issue"]').val(),
expiry_date: $r.find('[name="cert-expiry"]').val()
});
});
fd.append('certifications', JSON.stringify(certs));
httpPost(isEdit ? 'company/update.php' : 'company/add.php', fd, true).then(function () {
Dialog.success('保存成功', function () {
Dialog.close(idx);
loadList(currentPage);
});
});
});
}
/* ---------- 编辑弹窗 Tab 切换 ---------- */
window.switchEditTab = function (el) {
var $tabs = $(el).closest('.edit-tabs');
$tabs.find('.edit-tab').removeClass('active');
$(el).addClass('active');
$tabs.find('.edit-pane').hide();
$('#edit-pane-' + $(el).data('tab')).show();
};
/** 年度财务明细行模板(列表形式,一行一个财年) */
function finRowHtml(f) {
f = f || {};
var empty = function (v) { return (v === null || v === undefined) ? '' : v; };
return '' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
' | ' +
'
';
}
/** 资质认证行模板 */
function certRowHtml(ct, certTypes) {
ct = ct || {};
var opts = '';
certTypes.forEach(function (t) {
opts += '';
});
return '' +
'' +
'' +
'' +
'' +
'' +
'
';
}
function roleOptions(sel) {
var opts = ['', 'manufacturer', 'integrator', 'distributor', 'brand_operator', 'others'];
var labels = { 'manufacturer': '生产型', 'integrator': '集成商', 'distributor': '代理商', 'brand_operator': '品牌运营', 'others': '其他' };
var html = '';
opts.forEach(function (v) {
if (v === '') return;
html += '';
});
return html;
}
/* ---------- 导入 ---------- */
$('#btn-import').on('click', function () {
triggerFileInput('.csv', function (file) {
var fd = new FormData();
fd.append('file', file);
httpPost('company/import.php', fd, true).then(function (d) {
Dialog.alert('导入结果:成功 ' + d.inserted + ' 条,失败 ' + d.failed + ' 条', function () {
loadList(1);
});
});
});
});
/* ---------- 导出(仅导出勾选的企业,未勾选弹窗提醒) ---------- */
$('#btn-export').on('click', function () {
var ids = [];
$('.row-check:checked').each(function () { ids.push($(this).val()); });
if (!ids.length) {
layer.alert('请先选择需要导出的企业数据', { icon: 2, title: '提示' });
return;
}
download('company/export.php', { ids: ids.join(',') });
});
/* ---------- 详情 ---------- */
window.viewCompany = function (id) {
httpGet('company/detail.php', { id: id }).then(function (d) {
var c = d.company;
// 联系方式列表(social_accounts 中属于该公司的记录,仅展示)
var accountsHtml = '';
(d.accounts || []).forEach(function (a) {
var tag = '';
if (a.is_defult) tag = ' 常用';
else if (a.is_primary) tag = ' 默认';
accountsHtml += '' +
'| ' + escHtml(a.platform || '-') + ' | ' +
'' + escHtml(a.account_id || '-') + ' | ' +
'' + (a.profile_url ? '打开' : '-') + ' | ' +
'' + escHtml(a.remark || '-') + ' | ' +
'' + (a.is_active ? '启用' : '停用') + tag + ' | ' +
'
';
});
var accountsSection = accountsHtml
? '| 平台 | 账号/号码 | 主页链接 | 备注 | 状态 |
' + accountsHtml + '
'
: '暂无联系方式
';
var html =
'' +
'
' + escHtml(c.name_zh || c.name_en || '企业 #' + c.id) + '
' +
'
' +
detailRow('公司名称', c.name_zh) + detailRow('英文名称', c.name_en) +
detailRow('业务角色', c.business_role) + detailRow('国家/地区', c.country) +
detailRow('注册号', c.registration_number) + detailRow('地址', c.address) +
detailRow('法定代表人', c.legal_representative) + detailRow('行业', c.industry) +
detailRow('行业细分', c.industry_subdivision) + detailRow('官网', c.website) +
detailRow('注册资本', c.registered_capital) + detailRow('成立日期', c.established_date) +
detailRow('员工数', c.latest_employee_count) + detailRow('年营收', c.latest_annual_revenue) +
detailRow('是否上市', c.is_listed ? '是' : '否') + detailRow('股票代码', c.stock_code) +
detailRow('来源渠道', c.source_channel) + detailRow('来源详情', c.source_detail) +
'
' +
'
经营范围
' + escHtml(c.business_scope || '-') + '
' +
'
联系方式
' + accountsSection +
'
';
Dialog.open({ title: '企业详情', area: ['820px', 'auto'], content: html, btn: ['关闭'] });
});
};
function detailRow(label, val) {
return '' + label + ':' + escHtml(val || '-') + '
';
}
/* ---------- 员工列表弹窗(按公司查看员工,分页 + 姓名/职级筛选) ---------- */
var empCompanyId = 0;
var empPage = 1;
window.viewEmployees = function (companyId) {
empCompanyId = companyId;
empPage = 1;
var content =
'';
var idx = Dialog.open({ title: '员工列表', area: ['920px', 'auto'], content: content, btn: ['关闭'] });
window.__empDialogIdx = idx;
loadEmployees(1);
$('#emp-search').on('click', function () { loadEmployees(1); });
$('#emp-reset').on('click', function () {
$('#emp-keyword').val('');
$('#emp-level').val('');
$('#emp-cur').prop('checked', true);
$('#emp-off').prop('checked', true);
loadEmployees(1);
});
};
function loadEmployees(page) {
empPage = page;
var params = { company_id: empCompanyId, page: page, limit: 5 };
var kw = $.trim($('#emp-keyword').val());
var lv = $('#emp-level').val();
var cur = $('#emp-cur').prop('checked');
var off = $('#emp-off').prop('checked');
if (kw) params.keyword = kw;
if (lv) params.job_level = lv;
// 状态筛选:只勾一项则按该项过滤;都不勾视为全部
if (cur && !off) params.is_current = 1;
else if (!cur && off) params.is_current = 0;
httpGet('company/employees.php', params).then(function (data) {
var rows = data.list || [];
var html = '';
rows.forEach(function (r) {
html += '' +
'| ' + escHtml(r.full_name) + ' | ' +
'' + escHtml(r.phone || '-') + ' | ' +
'' + escHtml(r.email || '-') + ' | ' +
'' + escHtml(r.job_level || '-') + ' | ' +
'' + escHtml(r.department || '-') + ' | ' +
'' + escHtml(r.position || '-') + ' | ' +
'' + escHtml(r.start_date || '-') + ' | ' +
'' + (r.is_current ? '在职' : '离职') + ' | ' +
'
';
});
if (!rows.length) {
html = '| 暂无员工 |
';
}
$('#emp-tbody').html(html);
// 职级下拉(仅首次填充)
var $lv = $('#emp-level');
if ($lv.find('option').length <= 1) {
var optHtml = '';
(data.levels || []).forEach(function (l) {
optHtml += '';
});
$lv.html(optHtml);
}
renderPagination($('#emp-pagination'), data, loadEmployees);
});
}
/* ---------- 产品品类弹窗(按企业查看产品品类,分页) ---------- */
var prodCompanyId = 0;
window.viewProducts = function (companyId) {
prodCompanyId = companyId;
var content =
'' +
'
' +
' | 品类名称 | 品类描述 | 是否核心 | 状态 |
' +
' ' +
'
' +
' ' +
'
';
Dialog.open({ title: '产品品类', area: ['720px', 'auto'], content: content, btn: ['关闭'] });
loadProducts(1);
};
function loadProducts(page) {
httpGet('company/products.php', { company_id: prodCompanyId, page: page, limit: 5 }).then(function (data) {
var rows = data.list || [];
var html = '';
rows.forEach(function (r) {
html += '' +
'| ' + escHtml(r.category_name) + ' | ' +
'' + escHtml(r.category_description || '-') + ' | ' +
'' + (r.is_core ? '核心' : '-') + ' | ' +
'' + (r.is_active ? '在产' : '停产') + ' | ' +
'
';
});
if (!rows.length) {
html = '| 暂无产品品类 |
';
}
$('#prod-tbody').html(html);
renderPagination($('#prod-pagination'), data, loadProducts);
});
}
});