dsk.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. 'use strict';
  2. const dsk = (function () {
  3. const projectType = [
  4. { key: 'folder', value: 1, name: '文件夹' },
  5. { key: 'project', value: 2, name: '建设项目' },
  6. { key: 'unit', value: 3, name: '单位工程' },
  7. { key: 'item', 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. // console.log(d[setting.pid]);
  89. return null;
  90. }
  91. parent = _loadData(parent);
  92. }
  93. if (!parent) return null;
  94. parent.children.push(d);
  95. }
  96. d.children = [];
  97. self.datas.push(d);
  98. self.items[d[setting.id]] = d;
  99. return d;
  100. };
  101. for (const d of datas) {
  102. if (this.items[d[setting.id]]) continue;
  103. _loadData(d);
  104. }
  105. this.sort();
  106. }
  107. }
  108. const chooseSubjectObj = {
  109. obj: null,
  110. compilationObj: null,
  111. subjectSpread: null,
  112. subjectSheet: null,
  113. compliation: null,
  114. loadCompilation: async function() {
  115. if (this.compliation) return;
  116. this.compliation = await loadCompilation();
  117. for (const c of this.compliation) {
  118. c.project = await loadProject(c.ID);
  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 subjectTree = createNewPathTree('gather', {
  128. id: 'id',
  129. pid: 'pid',
  130. order: 'sort',
  131. level: 'level',
  132. rootId: -1,
  133. fullPath: 'full_path',
  134. });
  135. const tempTree = new DskTree({ id: 'ID', pid: 'parentID', order: 'seq', rootId: '-1' });
  136. tempTree.loadDatas(compilation.project);
  137. const projectTree = new DskTree({ id: 'ID', pid: 'parentID', order: 'seq', rootId: '-1' });
  138. const loadProjectTreeNode = function (data, parent, project) {
  139. let cur;
  140. if (data.type === projectTypeKey.project) {
  141. cur = parent;
  142. } else {
  143. const type = projectType.find(x => { return x.value === data.type; });
  144. const node = { type: type.value, type_str: type.name, dsk_id: data.ID, name: data.name, project_id: project.ID, compilation_id: compilation.ID };
  145. cur = subjectTree.addNode(node, parent);
  146. }
  147. if (data.children) {
  148. data.children.forEach(c => {
  149. loadProjectTreeNode(c, cur, project);
  150. })
  151. }
  152. };
  153. const loadTempTreeNode = function (data, parent) {
  154. const type = projectType.find(x => { return x.value === data.type; });
  155. const node = { type: type.value, type_str: type.name, dsk_id: data.ID, name: data.name, compilation_id: compilation.ID };
  156. const cur = subjectTree.addNode(node, parent);
  157. if (data.children) {
  158. data.children.forEach(c => {
  159. loadTempTreeNode(c, cur);
  160. })
  161. }
  162. if (data.type === projectTypeKey.project) {
  163. projectTree.loadDatas(data.subjects);
  164. cur.project_id = data.ID;
  165. projectTree.children.forEach(c => {
  166. loadProjectTreeNode(c, cur, data);
  167. });
  168. }
  169. };
  170. tempTree.children.forEach(c => {
  171. loadTempTreeNode(c, null);
  172. });
  173. subjectTree.sortTreeNode(true);
  174. compilation.subjectTree = subjectTree;
  175. },
  176. loadSubjects: async function (compilationId) {
  177. showWaitingView();
  178. const compilation = compilationId ? this.compliation.find(x => { return x.ID === compilationId}) : this.compliation[0];
  179. if (!compilation) return;
  180. if (!compilation.subjectTree) {
  181. for (const p of compilation.project) {
  182. if (p.type === projectTypeKey.project) p.subjects = await loadProjectTree(compilation.ID, p.ID, false);
  183. }
  184. this.analysisSubjectTree(compilation);
  185. }
  186. SpreadJsObj.loadSheetData(this.subjectSheet, SpreadJsObj.DataType.Tree, compilation.subjectTree);
  187. closeWaitingView();
  188. },
  189. getSelectSubjects: function () {
  190. const tree = this.subjectSheet.zh_tree;
  191. if (!tree) return [];
  192. return tree.datas.filter(x => { return x.type === projectTypeKey.item && x.selected; }).map(x => { return { compilationId: x.compilation_id, subjectId: x.dsk_id }; });
  193. },
  194. csButtonClicked: function(e, info) {
  195. if (!info.sheet.zh_setting) return;
  196. const col = info.sheet.zh_setting.cols[info.col];
  197. if (col.field !== 'selected') return;
  198. const tree = info.sheet.zh_tree;
  199. const node = SpreadJsObj.getSelectObject(info.sheet);
  200. if (!node.selected) {
  201. const canChoose = chooseSubjectObj.setting.validCheck ? chooseSubjectObj.setting.validCheck(node, tree) : true;
  202. if (!canChoose) return;
  203. }
  204. node.selected = !node.selected;
  205. if (node.children && node.children.length > 0) {
  206. const posterity = tree.getPosterity(node);
  207. for (const p of posterity) {
  208. p.selected = node.selected;
  209. }
  210. SpreadJsObj.reLoadRowData(info.sheet, info.row, posterity.length + 1);
  211. } else {
  212. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  213. }
  214. },
  215. init() {
  216. if (this.subjectSpread) return;
  217. this.obj = $('#choose-dsk-subject');
  218. this.compilationObj = $('#cdsks-compilation');
  219. this.subjectSpread = SpreadJsObj.createNewSpread($('#cdsks-spread')[0]);
  220. this.subjectSheet = this.subjectSpread.getActiveSheet();
  221. SpreadJsObj.initSheet(this.subjectSheet, {
  222. cols: [
  223. {title: '选择', field: 'selected', hAlign: 1, width: 60, formatter: '@', cellType: 'checkbox'},
  224. {title: '项目/分段名称', field: 'name', hAlign: 0, width: 740, formatter: '@', cellType: 'tree'},
  225. {title: '类型', field: 'type_str', hAlign: 0, width: 80, formatter: '@' },
  226. ],
  227. emptyRows: 0,
  228. headRows: 1,
  229. headRowHeight: [32],
  230. defaultRowHeight: 21,
  231. headerFont: '12px 微软雅黑',
  232. font: '12px 微软雅黑',
  233. selectedBackColor: '#fffacd',
  234. readOnly: true,
  235. });
  236. this.subjectSpread.bind(spreadNS.Events.ButtonClicked, this.csButtonClicked);
  237. $('#choose-dsk-subject-ok').click(() => {
  238. if (chooseSubjectObj.setting.callback) {
  239. const subjects = chooseSubjectObj.getSelectSubjects();
  240. if (subjects.length === 0) {
  241. toastr.warning('请选择导入的计价分段');
  242. return;
  243. }
  244. chooseSubjectObj.setting.callback(subjects);
  245. this.obj.modal('hide');
  246. }
  247. });
  248. $('#choose-dsk-subject').on('shown.bs.modal', function() {
  249. chooseSubjectObj.subjectSpread.refresh();
  250. });
  251. $('#cdsks-compilation').click(function() {
  252. chooseSubjectObj.loadSubjects(this.value);
  253. });
  254. },
  255. async show (setting) {
  256. this.init();
  257. this.setting = setting;
  258. this.obj.modal('show');
  259. await this.loadCompilation();
  260. await this.loadSubjects();
  261. }
  262. };
  263. const chooseSubject = function (setting) {
  264. chooseSubjectObj.show(setting);
  265. };
  266. return {
  267. projectType, projectTypeKey,
  268. checkBind, loadCompilation, loadProject, loadProjectTree, loadBills,
  269. chooseSubject
  270. };
  271. })();