'use strict'; const dsk = (function () { const projectType = [ { key: 'folder', value: 1, name: '文件夹' }, { key: 'project', value: 2, name: '建设项目' }, { key: 'unit', value: 3, name: '单位工程' }, { key: 'item', value: 4, name: '单项工程' }, ]; const projectTypeKey = (function(arr) { const result = {}; for (const a of arr) { result[a.key] = a.value; } return result; })(projectType); const checkBind = async function() { const data = await postDataAsync('/profile/dsk/api', {type: 'hadbind'}); return data !== 1 && data !== 2; }; const loadCompilation = async function () { const data = await postDataAsync('/profile/dsk/api', {type: 'compilation'}); return data.compilation; }; const loadProject = async function(compilationId, showWaiting = true) { return await postDataAsync('/profile/dsk/api', {type: 'project', compilationId}, showWaiting); }; const loadProjectTree = async function (compilationId, projectId, showWaiting = true) { return await postDataAsync('/profile/dsk/api', {type: 'project_tree', compilationId, projectId}, showWaiting) }; const loadBills = async function(compilationId, subjectId, showWaiting = true) { return await postDataAsync('/profile/dsk/api', {type: 'project_bills', compilationId, treeId: subjectId}, showWaiting) }; class DskTree { /** * * @param setting - {Object}配置 * setting中必须设置id,pid,order,rootId(id, 父id, 同层排序, 根节点id) * e.g.{id: 'ID', pid: 'parentID', order: 'seq'} * 目前仅用于载入建筑ybp文件中的清单部分,生成树结构以便汇总导入 */ constructor(setting) { this.setting = JSON.parse(JSON.stringify(setting)); this.clearDatas(); } clearDatas() { // 数据集合 this.datas = []; // id索引 this.items = {}; // 排序索引 this.nodes = []; // 首层节点 this.children = []; } sortChildren(children, recursive) { const setting = this.setting; children.sort((x, y) => { return x[setting.order] > y[setting.order]; }); if (!recursive) return; for (const c of children) { this.sortChildren(c.children); } } sort() { this.sortChildren(this.children, true); const self = this; const _loadNode = function(node) { self.nodes.push(node); for (const c of node.children) { _loadNode(c); } }; for (const child of this.children) { _loadNode(child); } } loadDatas(datas) { this.clearDatas(); const setting = this.setting; const self = this; const _loadData = function(d) { if (d[setting.pid] === setting.rootId) { self.children.push(d); } else { let parent = self.items[d[setting.pid]]; if (!parent) { parent = datas.find(x => { return x[setting.id] === d[setting.pid]; }); if (!parent) { // console.log(d[setting.pid]); return null; } parent = _loadData(parent); } if (!parent) return null; parent.children.push(d); } d.children = []; self.datas.push(d); self.items[d[setting.id]] = d; return d; }; for (const d of datas) { if (this.items[d[setting.id]]) continue; _loadData(d); } this.sort(); } } const chooseSubjectObj = { obj: null, compilationObj: null, subjectSpread: null, subjectSheet: null, compliation: null, loadCompilation: async function() { if (this.compliation) return; this.compliation = await loadCompilation(); for (const c of this.compliation) { c.project = await loadProject(c.ID); } const html = []; for (const c of this.compliation) { html.push(``); } this.compilationObj.html(html.join('')); }, analysisSubjectTree(compilation) { const subjectTree = createNewPathTree('gather', { id: 'id', pid: 'pid', order: 'sort', level: 'level', rootId: -1, fullPath: 'full_path', }); const tempTree = new DskTree({ id: 'ID', pid: 'parentID', order: 'seq', rootId: '-1' }); tempTree.loadDatas(compilation.project); const projectTree = new DskTree({ id: 'ID', pid: 'parentID', order: 'seq', rootId: '-1' }); const loadProjectTreeNode = function (data, parent, project) { let cur; if (data.type === projectTypeKey.project) { cur = parent; } else { const type = projectType.find(x => { return x.value === data.type; }); const node = { type: type.value, type_str: type.name, dsk_id: data.ID, name: data.name, project_id: project.ID, compilation_id: compilation.ID }; cur = subjectTree.addNode(node, parent); } if (data.children) { data.children.forEach(c => { loadProjectTreeNode(c, cur, project); }) } }; const loadTempTreeNode = function (data, parent) { const type = projectType.find(x => { return x.value === data.type; }); const node = { type: type.value, type_str: type.name, dsk_id: data.ID, name: data.name, compilation_id: compilation.ID }; const cur = subjectTree.addNode(node, parent); if (data.children) { data.children.forEach(c => { loadTempTreeNode(c, cur); }) } if (data.type === projectTypeKey.project) { projectTree.loadDatas(data.subjects); cur.project_id = data.ID; projectTree.children.forEach(c => { loadProjectTreeNode(c, cur, data); }); } }; tempTree.children.forEach(c => { loadTempTreeNode(c, null); }); subjectTree.sortTreeNode(true); compilation.subjectTree = subjectTree; }, loadSubjects: async function (compilationId) { showWaitingView(); const compilation = compilationId ? this.compliation.find(x => { return x.ID === compilationId}) : this.compliation[0]; if (!compilation) return; if (!compilation.subjectTree) { for (const p of compilation.project) { if (p.type === projectTypeKey.project) p.subjects = await loadProjectTree(compilation.ID, p.ID, false); } this.analysisSubjectTree(compilation); } SpreadJsObj.loadSheetData(this.subjectSheet, SpreadJsObj.DataType.Tree, compilation.subjectTree); closeWaitingView(); }, getSelectSubjects: function () { const tree = this.subjectSheet.zh_tree; if (!tree) return []; return tree.datas.filter(x => { return x.type === projectTypeKey.item && x.selected; }).map(x => { return { compilationId: x.compilation_id, subjectId: x.dsk_id }; }); }, csButtonClicked: function(e, info) { if (!info.sheet.zh_setting) return; const col = info.sheet.zh_setting.cols[info.col]; if (col.field !== 'selected') return; const tree = info.sheet.zh_tree; const node = SpreadJsObj.getSelectObject(info.sheet); if (!node.selected) { const canChoose = chooseSubjectObj.setting.validCheck ? chooseSubjectObj.setting.validCheck(node, tree) : true; if (!canChoose) return; } node.selected = !node.selected; if (node.children && node.children.length > 0) { const posterity = tree.getPosterity(node); for (const p of posterity) { p.selected = node.selected; } SpreadJsObj.reLoadRowData(info.sheet, info.row, posterity.length + 1); } else { SpreadJsObj.reLoadRowData(info.sheet, info.row, 1); } }, init() { if (this.subjectSpread) return; this.obj = $('#choose-dsk-subject'); this.compilationObj = $('#cdsks-compilation'); this.subjectSpread = SpreadJsObj.createNewSpread($('#cdsks-spread')[0]); this.subjectSheet = this.subjectSpread.getActiveSheet(); SpreadJsObj.initSheet(this.subjectSheet, { cols: [ {title: '选择', field: 'selected', hAlign: 1, width: 60, formatter: '@', cellType: 'checkbox'}, {title: '项目/分段名称', field: 'name', hAlign: 0, width: 740, formatter: '@', cellType: 'tree'}, {title: '类型', field: 'type_str', hAlign: 0, width: 80, formatter: '@' }, ], emptyRows: 0, headRows: 1, headRowHeight: [32], defaultRowHeight: 21, headerFont: '12px 微软雅黑', font: '12px 微软雅黑', selectedBackColor: '#fffacd', readOnly: true, }); this.subjectSpread.bind(spreadNS.Events.ButtonClicked, this.csButtonClicked); $('#choose-dsk-subject-ok').click(() => { if (chooseSubjectObj.setting.callback) { const subjects = chooseSubjectObj.getSelectSubjects(); if (subjects.length === 0) { toastr.warning('请选择导入的计价分段'); return; } chooseSubjectObj.setting.callback(subjects); this.obj.modal('hide'); } }); $('#choose-dsk-subject').on('shown.bs.modal', function() { chooseSubjectObj.subjectSpread.refresh(); }); $('#cdsks-compilation').click(function() { chooseSubjectObj.loadSubjects(this.value); }); }, async show (setting) { this.init(); this.setting = setting; this.obj.modal('show'); await this.loadCompilation(); await this.loadSubjects(); } }; const chooseSubject = function (setting) { chooseSubjectObj.show(setting); }; return { projectType, projectTypeKey, checkBind, loadCompilation, loadProject, loadProjectTree, loadBills, chooseSubject }; })();