dsk.js 13 KB

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