$(document).ready(function() { const projectTreeObj = (function(setting){ const ProjectTree = createDragTree(setting.treeSetting); ProjectTree.loadDatas(setting.source); const TableObj = $(setting.table); const TableHeaderObj = $(setting.tableHeader); let tenderTreeShowLevel; let colSetCache; const Utils = { calculateFolder: function(node) { node.tp_cache = {}; for (const c of node.children) { if (!c.tp_cache) continue; for (const prop in c.tp_cache) { node.tp_cache[prop] = ZhCalc.add(node.tp_cache[prop], c.tp_cache[prop]); } } }, calculateSubProject: function(node) { node.tp_cache.gather_tp = ZhCalc.sum([node.tp_cache.contract_tp, node.tp_cache.qc_tp, node.tp_cache.pc_tp]); node.tp_cache.end_contract_tp = ZhCalc.sum([node.tp_cache.pre_contract_tp, node.tp_cache.contract_tp, node.tp_cache.contract_pc_tp]); node.tp_cache.end_qc_tp = ZhCalc.sum([node.tp_cache.pre_qc_tp, node.tp_cache.qc_tp, node.tp_cache.qc_pc_tp]); node.tp_cache.end_gather_tp = ZhCalc.add(node.tp_cache.end_contract_tp, node.tp_cache.end_qc_tp); node.tp_cache.pre_gather_tp = ZhCalc.add(node.tp_cache.pre_contract_tp, node.tp_cache.pre_qc_tp); node.tp_cache.end_yf_tp = ZhCalc.add(node.tp_cache.pre_yf_tp, node.tp_cache.yf_tp); node.tp_cache.end_sf_tp = ZhCalc.add(node.tp_cache.pre_sf_tp, node.tp_cache.sf_tp); node.tp_cache.wf_tp = ZhCalc.sub(node.tp_cache.end_yf_tp, node.tp_cache.end_sf_tp); }, calculateNode: function(node) { if (node.children && node.children.length > 0) { for (const c of node.children) { this.calculateNode(c); } this.calculateFolder(node); } else { if (node.is_folder) { this.calculateFolder(node) } else { this.calculateSubProject(node); } } }, calculateAll: function() { for (const p of ProjectTree.children) { this.calculateNode(p); } }, generateColSetCache: function() { const result = {}; colSet.forEach(x => { result[x.field] = { show: x.show, alias: x.alias || x.name }; }); return result; }, getHeaderHtml: function() { colSetCache = this.generateColSetCache(); const html = []; html.push(''); if (colSetCache.name.show) html.push('', colSetCache.name.alias, ''); if (colSetCache.contract_price.show) html.push('', colSetCache.contract_price.alias, ''); if (colSetCache.total_price.show) html.push('', colSetCache.total_price.alias, ''); if (colSetCache.gather_tp.show) html.push('', colSetCache.gather_tp.alias, ''); if (colSetCache.end_contract_tp.show) html.push('', colSetCache.end_contract_tp.alias, ''); if (colSetCache.end_qc_tp.show) html.push('', colSetCache.end_qc_tp.alias, ''); if (colSetCache.end_gather_tp.show) html.push('', colSetCache.end_gather_tp.alias, ''); if (colSetCache.pre_gather_tp.show) html.push('', colSetCache.pre_gather_tp.alias, ''); if (colSetCache.advance_tp.show) html.push('', colSetCache.advance_tp.alias, '', ''); if (colSetCache.yf_tp.show) html.push('', colSetCache.yf_tp.alias, ''); if (colSetCache.end_yf_tp.show) html.push('', colSetCache.end_yf_tp.alias, ''); if (colSetCache.sf_tp.show) html.push('', colSetCache.sf_tp.alias, ''); if (colSetCache.end_sf_tp.show) html.push('', colSetCache.end_sf_tp.alias, ''); if (colSetCache.wf_tp.show) html.push('', colSetCache.wf_tp.alias, ''); html.push(''); return html.join(''); }, getRowTdHtml: function (node, tree) { const html = []; // 名称 if (colSetCache.name.show) { html.push(''); if (node.is_folder) { if (node.children.length > 0) { html.push(' ', node.name); } else { html.push(' ', node.name); } } else { html.push(`${tree.isLastSibling(node) ? '└' : '├'}`); html.push(``, node.name, ''); } html.push(''); } if (colSetCache.contract_price.show) { html.push(''); html.push(node.tp_cache.contract_price || ''); html.push(''); } // 0号台账合同 if (colSetCache.total_price.show) { html.push(''); html.push(node.tp_cache.total_price || ''); html.push(''); } // 本期完成 if (colSetCache.gather_tp.show) { html.push(''); html.push(node.tp_cache.gather_tp || ''); html.push(''); } // 截止本期合同 if (colSetCache.end_contract_tp.show) { html.push(''); html.push(node.tp_cache.end_contract_tp || ''); html.push(''); } // 截止本期变更 if (colSetCache.end_qc_tp.show) { html.push(''); html.push(node.end_qc_tp || ''); html.push(''); } // 截止本期完成 if (colSetCache.end_gather_tp.show) { html.push(''); html.push(node.tp_cache.end_gather_tp || ''); html.push(''); } // 截止上期完成 if (colSetCache.pre_gather_tp.show) { html.push(''); html.push(node.pre_gather_tp || ''); html.push(''); } // 预付款 if (colSetCache.advance_tp.show) { html.push(''); html.push(node.tp_cache.advance_tp || ''); html.push(''); } // 本期应付 if (colSetCache.yf_tp.show) { html.push(''); html.push(node.tp_cache.yf_tp || ''); html.push(''); } // 截止本期应付 if (colSetCache.end_yf_tp.show) { html.push(''); html.push(node.tp_cache.end_yf_tp || ''); html.push(''); } // 本期实付 if (colSetCache.sf_tp.show) { html.push(''); html.push(node.tp_cache.sf_tp || ''); html.push(''); } // 截止本期实付 if (colSetCache.end_sf_tp.show) { html.push(''); html.push(node.tp_cache.end_sf_tp || ''); html.push(''); } // 本期未付 if (colSetCache.wf_tp.show) { html.push(''); html.push(node.tp_cache.wf_tp || ''); html.push(''); } return html.join(''); }, getNodeTrHtml: function (node, tree) { const html = []; html.push(``); html.push(Utils.getRowTdHtml(node, tree)); html.push(``); return html.join(''); }, reloadTable: function () { this.calculateAll(); TableHeaderObj.html(Utils.getHeaderHtml()); const html = []; for (const node of ProjectTree.nodes) { html.push(Utils.getNodeTrHtml(node, ProjectTree)); } TableObj.html(html.join('')); }, getSelectNode: function() { const selectId = $('tr.table-active').attr('tree_id'); return selectId ? ProjectTree.getItems(selectId) : null; }, getSelectNodeId: function() { const selectId = $('tr.table-active').attr('tree_id'); return selectId || setting.treeSetting.rootId; }, refreshTreeTable: function(result) { ProjectTree.loadDatas(result); if (ProjectTree.nodes.length > 0 && $('#no-project').length > 0) window.location.reload(); Utils.reloadTable(); }, refreshRow: function(result) { const refreshData = ProjectTree.loadPostData(result); if (!refreshData.update) return; for (const u of refreshData.update) { $(`tr[tree_id=${u.id}]`).html(Utils.getRowTdHtml(u, ProjectTree)); } }, expandByLevel: function(level){ ProjectTree.expandByLevel(level); for (const node of ProjectTree.nodes) { const tr = $(`tr[tree_id=${node.id}]`); if (node.expanded) { $('.fold-switch', tr).html(``); } else { $('.fold-switch', tr).html(``); } if (node.visible) { tr.show(); } else { tr.hide(); } } } }; Utils.reloadTable(); $('body').on('click', 'tr[tree_id]', function() { if ($(this).hasClass('table-active')) { $(this).removeClass('table-active'); } else { $('tr[tree_id].table-active').removeClass('table-active'); $(this).addClass('table-active'); } Utils.refreshAddButton(); }); $('body').on('click', '.fold-switch', function() { const id = this.getAttribute('id'); const node = ProjectTree.getItems(id); ProjectTree.setExpanded(node, !node.expanded); const posterity = ProjectTree.getPosterity(node); if (node.expanded) { $(this).html(``); } else { $(this).html(``); } for (const p of posterity) { if (p.visible) { $(`tr[tree_id=${p.id}]`).show(); } else { $(`tr[tree_id=${p.id}]`).hide(); } } }); const getChildrenLevel = function (node) { let iLevel = node.tree_level || 1; if (node.children && node.children.length > 0) { for (const c of node.children) { iLevel = Math.max(iLevel, getChildrenLevel(c)); } } return iLevel; }; tenderTreeShowLevel = $.cs_showLevel({ selector: '#show-level', levels: [ { type: 'sort', count: 5, visible_count: function () { return ProjectTree.children.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1; } }, { type: 'last', title: '最底层', visible: function () { const count = ProjectTree.children.map(getChildrenLevel).reduce((x, y) => { return Math.max(x, y); }, 0) - 1; return count > 0; } }, ], showLevel: function (tag) { switch (tag) { case "1": case "2": case "3": case "4": case "5": Utils.expandByLevel(parseInt(tag)); break; case "last": Utils.expandByLevel(20); break; default: return; } } }); tenderTreeShowLevel.initShowLevel(); tenderTreeShowLevel.refreshMenuVisible(); return { ProjectTree, TableObj, ...Utils }; })({ treeSetting: { id: 'id', pid: 'tree_pid', level: 'tree_level', order: 'tree_order', rootId: '-1' }, source: projectList, table: '#projectList', tableHeader: '#projectListHeader', }); $('#refresh-cache').click(() => { const spid = []; for (const p of projectTreeObj.ProjectTree.nodes) { if (p.is_folder) continue; spid.push(p.id); } postData('/subproj/info/refreshCache', { spid: spid.join(';') }, function(result) { for (const r of result) { const project = projectTreeObj.ProjectTree.nodes.find(x => { return x.id === r.id; }); if (!project) continue; project.tp_cache = r.tp_cache; } projectTreeObj.reloadTable(); }); }) });