dsk.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. 'use strict';
  2. const dsk = (function () {
  3. const projectType = [
  4. { key: 'folder', value: 1, name: '文件夹' },
  5. { key: 'project', value: 2, name: '建设项目' },
  6. { key: 'item', value: 3, name: '单项工程' },
  7. { key: 'unit', value: 4, name: '单位工程' },
  8. ];
  9. const projectTypeKey = (function(arr) {
  10. const result = {};
  11. for (const a of arr) {
  12. result[a.key] = a.value;
  13. }
  14. return result;
  15. })(projectType);
  16. const checkBind = async function() {
  17. const data = await postDataAsync('/profile/dsk/api', {type: 'hadbind'});
  18. return data !== 1 && data !== 2;
  19. };
  20. const loadCompilation = async function () {
  21. const data = await postDataAsync('/profile/dsk/api', {type: 'compilation'});
  22. return data.compilation;
  23. };
  24. const loadProject = async function(compilationId, showWaiting = true) {
  25. return await postDataAsync('/profile/dsk/api', {type: 'project', compilationId}, showWaiting);
  26. };
  27. const loadProjectTree = async function (compilationId, projectId, showWaiting = true) {
  28. return await postDataAsync('/profile/dsk/api', {type: 'project_tree', compilationId, projectId}, showWaiting)
  29. };
  30. const loadBills = async function(compilationId, subjectId, showWaiting = true) {
  31. return await postDataAsync('/profile/dsk/api', {type: 'project_bills', compilationId, treeId: subjectId}, showWaiting)
  32. };
  33. class DskTree {
  34. /**
  35. *
  36. * @param setting - {Object}配置
  37. * setting中必须设置id,pid,order,rootId(id, 父id, 同层排序, 根节点id)
  38. * e.g.{id: 'ID', pid: 'parentID', order: 'seq'}
  39. * 目前仅用于载入建筑ybp文件中的清单部分,生成树结构以便汇总导入
  40. */
  41. constructor(setting) {
  42. this.setting = JSON.parse(JSON.stringify(setting));
  43. this.clearDatas();
  44. }
  45. clearDatas() {
  46. // 数据集合
  47. this.datas = [];
  48. // id索引
  49. this.items = {};
  50. // 排序索引
  51. this.nodes = [];
  52. // 首层节点
  53. this.children = [];
  54. }
  55. sortChildren(children, recursive) {
  56. const setting = this.setting;
  57. children.sort((x, y) => { return x[setting.order] - y[setting.order]; });
  58. if (!recursive) return;
  59. for (const c of children) {
  60. this.sortChildren(c.children);
  61. }
  62. }
  63. sort() {
  64. this.sortChildren(this.children, true);
  65. const self = this;
  66. const _loadNode = function(node) {
  67. self.nodes.push(node);
  68. for (const c of node.children) {
  69. _loadNode(c);
  70. }
  71. };
  72. for (const child of this.children) {
  73. _loadNode(child);
  74. }
  75. }
  76. loadDatas(datas) {
  77. this.clearDatas();
  78. const setting = this.setting;
  79. const self = this;
  80. const _loadData = function(d) {
  81. if (d[setting.pid] === setting.rootId) {
  82. self.children.push(d);
  83. } else {
  84. let parent = self.items[d[setting.pid]];
  85. if (!parent) {
  86. parent = datas.find(x => { return x[setting.id] === d[setting.pid]; });
  87. if (!parent) {
  88. return null;
  89. }
  90. parent = _loadData(parent);
  91. }
  92. if (!parent) return null;
  93. parent.children.push(d);
  94. }
  95. d.children = [];
  96. self.datas.push(d);
  97. self.items[d[setting.id]] = d;
  98. return d;
  99. };
  100. for (const d of datas) {
  101. if (this.items[d[setting.id]]) continue;
  102. _loadData(d);
  103. }
  104. this.sort();
  105. }
  106. }
  107. const chooseSubjectObj = {
  108. obj: null,
  109. compilationObj: null,
  110. subjectSpread: null,
  111. subjectSheet: null,
  112. compliation: null,
  113. loadCompilation: async function() {
  114. if (!this.compliation) {
  115. this.compliation = await loadCompilation();
  116. for (const c of this.compliation) {
  117. c.project = await loadProject(c.ID);
  118. }
  119. }
  120. const html = [];
  121. for (const c of this.compliation) {
  122. html.push(`<option value="${c.ID}">${c.name}</option>`);
  123. }
  124. this.compilationObj.html(html.join(''));
  125. },
  126. analysisSubjectTree(compilation) {
  127. const filter = this.setting.filterProjectType;
  128. const subjectTree = createNewPathTree('gather', {
  129. id: 'id',
  130. pid: 'pid',
  131. order: 'sort',
  132. level: 'level',
  133. rootId: -1,
  134. fullPath: 'full_path',
  135. });
  136. const tempTree = new DskTree({ id: 'ID', pid: 'parentID', order: 'seq', rootId: '-1' });
  137. tempTree.loadDatas(compilation.project);
  138. const projectTree = new DskTree({ id: 'ID', pid: 'parentID', order: 'seq', rootId: '-1' });
  139. const loadProjectTreeNode = function (data, parent, project) {
  140. let cur;
  141. if (data.type === projectTypeKey.project) {
  142. cur = parent;
  143. } else {
  144. const type = projectType.find(x => { return x.value === data.type; });
  145. const node = { type: type.value, type_str: type.name, dsk_id: data.ID, name: data.name, project_id: project.ID, compilation_id: compilation.ID };
  146. cur = subjectTree.addNode(node, parent);
  147. }
  148. if (data.children) {
  149. data.children.forEach(c => {
  150. loadProjectTreeNode(c, cur, project);
  151. })
  152. }
  153. };
  154. const loadTempTreeNode = function (data, parent) {
  155. if (data.type === projectTypeKey.project && filter) {
  156. if (filter.indexOf(data.property.fileType) < 0) return;
  157. }
  158. const type = projectType.find(x => { return x.value === data.type; });
  159. const node = { type: type.value, type_str: type.name, file_type_str: data.property ? data.property.fileTypeStr || '' : '', dsk_id: data.ID, name: data.name, compilation_id: compilation.ID };
  160. const cur = subjectTree.addNode(node, parent);
  161. if (data.children) {
  162. data.children.forEach(c => {
  163. loadTempTreeNode(c, cur);
  164. })
  165. }
  166. if (data.type === projectTypeKey.project) {
  167. const top = data.subjects.find(x => { return x.ID === data.ID; });
  168. top.parentID = "-1";
  169. projectTree.loadDatas(data.subjects);
  170. cur.project_id = data.ID;
  171. projectTree.children.forEach(c => {
  172. loadProjectTreeNode(c, cur, data);
  173. });
  174. }
  175. };
  176. tempTree.children.forEach(c => {
  177. loadTempTreeNode(c, null);
  178. });
  179. subjectTree.sortTreeNode(true);
  180. compilation.subjectTree = subjectTree;
  181. },
  182. loadSubjects: async function (compilationId) {
  183. showWaitingView();
  184. const compilation = compilationId ? this.compliation.find(x => { return x.ID === compilationId}) : this.compliation[0];
  185. if (!compilation) return;
  186. if (!compilation.subjectTree) {
  187. for (const p of compilation.project) {
  188. if (p.type === projectTypeKey.project) {
  189. p.subjects = await loadProjectTree(compilation.ID, p.ID, false);
  190. }
  191. }
  192. this.analysisSubjectTree(compilation);
  193. }
  194. SpreadJsObj.loadSheetData(this.subjectSheet, SpreadJsObj.DataType.Tree, compilation.subjectTree);
  195. closeWaitingView();
  196. },
  197. getSelectSubjects: function () {
  198. const tree = this.subjectSheet.zh_tree;
  199. if (!tree) return [];
  200. return tree.datas.filter(x => { return x.type === projectTypeKey.unit && x.selected; }).map(x => { return { name: x.name, compilationId: x.compilation_id, subjectId: x.dsk_id }; });
  201. },
  202. csButtonClicked: function(e, info) {
  203. if (!info.sheet.zh_setting) return;
  204. const col = info.sheet.zh_setting.cols[info.col];
  205. if (col.field !== 'selected') return;
  206. const tree = info.sheet.zh_tree;
  207. const node = SpreadJsObj.getSelectObject(info.sheet);
  208. if (!node.selected) {
  209. const canChoose = chooseSubjectObj.setting.validCheck ? chooseSubjectObj.setting.validCheck(node, tree) : true;
  210. if (!canChoose) return;
  211. }
  212. node.selected = !node.selected;
  213. if (node.children && node.children.length > 0) {
  214. const posterity = tree.getPosterity(node);
  215. for (const p of posterity) {
  216. p.selected = node.selected;
  217. }
  218. SpreadJsObj.reLoadRowData(info.sheet, info.row, posterity.length + 1);
  219. } else {
  220. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  221. }
  222. },
  223. init() {
  224. if (this.subjectSpread) return;
  225. this.obj = $('#choose-dsk-subject');
  226. this.compilationObj = $('#cdsks-compilation');
  227. this.subjectSpread = SpreadJsObj.createNewSpread($('#cdsks-spread')[0]);
  228. this.subjectSheet = this.subjectSpread.getActiveSheet();
  229. SpreadJsObj.initSheet(this.subjectSheet, {
  230. cols: [
  231. {title: '选择', field: 'selected', hAlign: 1, width: 60, formatter: '@', cellType: 'checkbox'},
  232. {title: '项目/分段名称', field: 'name', hAlign: 0, width: 640, formatter: '@', cellType: 'tree'},
  233. {title: '项目类型', field: 'file_type_str', hAlign: 1, width: 100, formatter: '@' },
  234. {title: '类型', field: 'type_str', hAlign: 1, width: 80, formatter: '@' },
  235. ],
  236. emptyRows: 0,
  237. headRows: 1,
  238. headRowHeight: [32],
  239. defaultRowHeight: 21,
  240. headerFont: '12px 微软雅黑',
  241. font: '12px 微软雅黑',
  242. selectedBackColor: '#fffacd',
  243. readOnly: true,
  244. });
  245. this.subjectSpread.bind(spreadNS.Events.ButtonClicked, this.csButtonClicked);
  246. $('#choose-dsk-subject-ok').click(() => {
  247. if (chooseSubjectObj.setting.callback) {
  248. const subjects = chooseSubjectObj.getSelectSubjects();
  249. if (subjects.length === 0) {
  250. toastr.warning('请选择导入的计价分段');
  251. return;
  252. }
  253. chooseSubjectObj.setting.callback(subjects);
  254. this.obj.modal('hide');
  255. }
  256. });
  257. $('#choose-dsk-subject').on('shown.bs.modal', function() {
  258. chooseSubjectObj.subjectSpread.refresh();
  259. });
  260. $('#cdsks-compilation').click(function() {
  261. chooseSubjectObj.loadSubjects(this.value);
  262. });
  263. },
  264. async show (setting) {
  265. this.init();
  266. this.setting = setting;
  267. this.obj.modal('show');
  268. await this.loadCompilation();
  269. await this.loadSubjects();
  270. }
  271. };
  272. const chooseSubject = function (setting) {
  273. chooseSubjectObj.show(setting);
  274. };
  275. return {
  276. projectType, projectTypeKey,
  277. checkBind, loadCompilation, loadProject, loadProjectTree, loadBills,
  278. chooseSubject
  279. };
  280. })();