billsGuidance.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/6/1
  7. * @version
  8. */
  9. const billsGuidance = (function () {
  10. //自执行函数全局变量定义
  11. const libID = getQueryString('libID');
  12. const bills = {
  13. dom: $('#billsSpread'),
  14. workBook: null,
  15. cache: [],
  16. tree: null,
  17. controller: null,
  18. treeSetting: {
  19. treeCol: 0,
  20. emptyRows: 0,
  21. headRows: 1,
  22. headRowHeight: [40],
  23. defaultRowHeight: 21,
  24. cols: [{
  25. width: 200,
  26. readOnly: true,
  27. head: {
  28. titleNames: ["项目编码"],
  29. spanCols: [1],
  30. spanRows: [1],
  31. vAlign: [1],
  32. hAlign: [1],
  33. font: ["Arial"]
  34. },
  35. data: {
  36. field: "code",
  37. vAlign: 1,
  38. hAlign: 0,
  39. font: "Arial"
  40. }
  41. }, {
  42. width: 200,
  43. readOnly: true,
  44. head: {
  45. titleNames: ["项目名称"],
  46. spanCols: [1],
  47. spanRows: [1],
  48. vAlign: [1],
  49. hAlign: [1],
  50. font: ["Arial"]
  51. },
  52. data: {
  53. field: "name",
  54. vAlign: 1,
  55. hAlign: 0,
  56. font: "Arial"
  57. }
  58. }]
  59. },
  60. headers: [
  61. {name: '项目编码', dataCode: 'code', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'},
  62. {name: '项目名称', dataCode: 'name', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'}
  63. ],
  64. events: {
  65. SelectionChanged: function (sender, info) {
  66. billsInitSel(info.newSelections[0].row);
  67. }
  68. }
  69. };
  70. //项目指引类型
  71. const itemType = {
  72. job: 0,
  73. ration: 1
  74. };
  75. const updateType = {
  76. create: 'create',
  77. update: 'update',
  78. del: 'delete'
  79. };
  80. const guideItem = {
  81. dom: $('#guideItemSpread'),
  82. workBook: null,
  83. tree: null,
  84. controller: null,
  85. treeSetting: {
  86. treeCol: 0,
  87. emptyRows: 0,
  88. headRows: 1,
  89. headRowHeight: [40],
  90. defaultRowHeight: 21,
  91. cols: [{
  92. width: 400,
  93. readOnly: false,
  94. head: {
  95. titleNames: ["项目指引"],
  96. spanCols: [1],
  97. spanRows: [1],
  98. vAlign: [1],
  99. hAlign: [1],
  100. font: ["Arial"]
  101. },
  102. data: {
  103. field: "name",
  104. vAlign: 1,
  105. hAlign: 0,
  106. font: "Arial"
  107. }
  108. }]
  109. },
  110. headers: [
  111. {name: '项目指引', dataCode: 'name', width: 400, vAlign: 'center', hAlign: 'left', formatter: '@'},
  112. ],
  113. events: {
  114. SelectionChanged: function (sender, info) {
  115. guideItemInitSel(info.newSelections[0].row)
  116. },
  117. EditEnded: function (sender, args) {
  118. edit(args.sheet, [{row: args.row, col: args.col}]);
  119. },
  120. RangeChanged: function (sender, args) {
  121. edit(args.sheet, args.changedCells);
  122. }
  123. }
  124. };
  125. //定额章节树
  126. const section = {
  127. dom: $('#sectionSpread'),
  128. workBook: null,
  129. cache: [],
  130. tree: null,
  131. controller: null,
  132. treeSetting: {
  133. treeCol: 0,
  134. emptyRows: 0,
  135. headRows: 1,
  136. headRowHeight: [40],
  137. defaultRowHeight: 21,
  138. cols: [{
  139. width: 400,
  140. readOnly: true,
  141. head: {
  142. titleNames: ["名称"],
  143. spanCols: [1],
  144. spanRows: [1],
  145. vAlign: [1],
  146. hAlign: [1],
  147. font: ["Arial"]
  148. },
  149. data: {
  150. field: "name",
  151. vAlign: 1,
  152. hAlign: 0,
  153. font: "Arial"
  154. }
  155. }]
  156. },
  157. headers: [
  158. {name: '名称', dataCode: 'name', width: 400, vAlign: 'center', hAlign: 'left', formatter: '@'},
  159. ],
  160. events: {
  161. SelectionChanged: function (sender, info) {
  162. sectionInitSel(info.newSelections[0].row)
  163. }
  164. }
  165. };
  166. const ration = {
  167. dom: $('#rationSpread'),
  168. workBook: null,
  169. datas: [],//所有的数据,搜索定额时,从所有数据中筛选
  170. cache: [],//显示在表格上的数据,添加定额可以有效根据行识别定额
  171. headers: [
  172. {name: '选择', dataCode: 'select', width: 50, vAlign: 'center', hAlign: 'center'},
  173. {name: '编码', dataCode: 'code', width: 110, vAlign: 'center', hAlign: 'left', formatter: '@'},
  174. {name: '名称', dataCode: 'name', width: 250, vAlign: 'center', hAlign: 'left', formatter: '@'},
  175. {name: '单位', dataCode: 'unit', width: 100, vAlign: 'center', hAlign: 'left', formatter: '@'}
  176. ],
  177. events: {
  178. ButtonClicked: function (sender, args) {
  179. if(args.sheet.isEditing()){
  180. args.sheet.endEdit(true);
  181. }
  182. },
  183. CellDoubleClick: function (sender, args) {
  184. if(ration.headers[args.col].dataCode === 'name'){
  185. let insertDatas = getInsertRations([args.row]);
  186. if(insertDatas.length > 0){
  187. insert(insertDatas);
  188. }
  189. }
  190. }
  191. }
  192. };
  193. const options = {
  194. workBook: {
  195. tabStripVisible: false,
  196. allowContextMenu: false,
  197. allowCopyPasteExcelStyle : false,
  198. allowExtendPasteRange: false,
  199. allowUserDragDrop : false,
  200. allowUserDragFill: false,
  201. scrollbarMaxAlign : true
  202. },
  203. sheet: {
  204. protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
  205. clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
  206. }
  207. };
  208. //渲染时方法,停止渲染
  209. //@param {Object}sheet {Function}func @return {void}
  210. function renderSheetFunc(sheet, func){
  211. sheet.suspendEvent();
  212. sheet.suspendPaint();
  213. if(func){
  214. func();
  215. }
  216. sheet.resumeEvent();
  217. sheet.resumePaint();
  218. }
  219. //设置表选项
  220. //@param {Object}workBook {Object}opts @return {void}
  221. function setOptions (workBook, opts) {
  222. for(let opt in opts.workBook){
  223. workBook.options[opt] = opts.workBook[opt];
  224. }
  225. for(let opt in opts.sheet){
  226. workBook.getActiveSheet().options[opt] = opts.sheet[opt];
  227. }
  228. }
  229. //建表头
  230. //@param {Object}sheet {Array}headers @return {void}
  231. function buildHeader(sheet, headers) {
  232. let fuc = function () {
  233. sheet.setColumnCount(headers.length);
  234. sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
  235. for(let i = 0, len = headers.length; i < len; i++){
  236. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  237. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  238. if(headers[i].formatter){
  239. sheet.setFormatter(-1, i, headers[i].formatter);
  240. }
  241. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  242. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  243. }
  244. };
  245. renderSheetFunc(sheet, fuc);
  246. }
  247. //表监听事件
  248. //@param {Object}workBook @return {void}
  249. function bindEvent(workBook, events) {
  250. if(Object.keys(events).length === 0){
  251. return;
  252. }
  253. const Events = GC.Spread.Sheets.Events;
  254. let sheet = workBook.getActiveSheet();
  255. for(let event in events){
  256. workBook.bind(Events[event], events[event]);
  257. }
  258. }
  259. //建表
  260. //@param {Object}module @return {void}
  261. function buildSheet(module) {
  262. if(!module.workBook){
  263. module.workBook = new GC.Spread.Sheets.Workbook(module.dom[0], {sheetCount: 1});
  264. let sheet = module.workBook.getActiveSheet();
  265. if(module === bills){
  266. //默认初始可控制焦点在清单表中
  267. module.workBook.focus();
  268. sheet.options.isProtected = true;
  269. }
  270. else if(module === ration){
  271. sheet.options.isProtected = true;
  272. sheet.getRange(-1, 0, -1, 1).locked(false);
  273. sheet.getRange(-1, 1, -1, -1).locked(true);
  274. }
  275. else if(module === guideItem){
  276. sheetCommonObj.bindEscKey(module.workBook, [{sheet: sheet, editStarting: null, editEnded: module.events.EditEnded}]);
  277. }
  278. setOptions(module.workBook, options);
  279. buildHeader(module.workBook.getActiveSheet(), module.headers);
  280. bindEvent(module.workBook, module.events);
  281. }
  282. }
  283. //清空表数据
  284. //@param {Object}sheet {Array}headers {Number}rowCount @return {void}
  285. function cleanData(sheet, headers, rowCount){
  286. renderSheetFunc(sheet, function () {
  287. sheet.clear(-1, 0, -1, headers.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  288. if (rowCount >= 0) {
  289. sheet.setRowCount(rowCount);
  290. }
  291. });
  292. }
  293. //根据清单获取项目指引
  294. //@param {String}guidanceLibID {Number}billsID {Function}callback @return {void}
  295. function getItemsByBills(guidanceLibID, billsID, callback){
  296. CommonAjax.post('/billsGuidance/api/getItemsByBills', {guidanceLibID: guidanceLibID, billsID: billsID}, function (rstData) {
  297. if(callback){
  298. callback(rstData);
  299. }
  300. });
  301. }
  302. //清单表焦点控制
  303. //@param {Number}row @return {void}
  304. function billsInitSel(row){
  305. let guideSheet = guideItem.workBook.getActiveSheet();
  306. cleanData(guideSheet, guideItem.headers, -1);
  307. let node = bills.tree.items[row];
  308. if(!node){
  309. return;
  310. }
  311. bills.tree.selected = node;
  312. if(!node.guidance.tree){
  313. getItemsByBills(libID, node.data.ID, function (rstData) {
  314. initTree(node.guidance, guideSheet, guideItem.treeSetting, rstData);
  315. //项目指引初始焦点
  316. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  317. });
  318. }
  319. else{
  320. node.guidance.controller.showTreeData();
  321. //项目指引初始焦点
  322. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  323. }
  324. }
  325. //节点子项是否全是工作内容
  326. //@param {Object}node @return {Boolean}
  327. function allJobChildren(node){
  328. for(let c of node.children){
  329. if(c.data.type === itemType.ration){
  330. return false;
  331. }
  332. }
  333. return true;
  334. }
  335. //节点子项是否全是定额
  336. //@param {Object}node @return {Boolean}
  337. function allRationChildren(node){
  338. for(let c of node.children){
  339. if(c.data.type === itemType.job){
  340. return false;
  341. }
  342. }
  343. return true;
  344. }
  345. //刷新按钮有效性
  346. //@param {Object}node @return {void}
  347. function refreshBtn(node){
  348. //全部设为无效
  349. $('.tools-btn').children().addClass('disabled');
  350. $('#insertRation').addClass('disabled');
  351. //插入
  352. if(bills.tree.selected && bills.tree.selected.guidance.tree){
  353. $('#insert').removeClass('disabled');
  354. if(node && node.data.type === itemType.ration){
  355. $('#insert').addClass('disabled');
  356. }
  357. }
  358. //删除
  359. if(node){
  360. $('#del').removeClass('disabled');
  361. }
  362. if(node && node.data.type === itemType.job){
  363. //升级
  364. if(node.parent){
  365. $('#upLevel').removeClass('disabled');
  366. if(node.nextSibling && node.children.length > 0 && !allJobChildren(node)){
  367. $('#upLevel').addClass('disabled');
  368. }
  369. }
  370. //降级
  371. if(node.preSibling){
  372. $('#downLevel').removeClass('disabled');
  373. if(node.preSibling.children.length > 0 && !allJobChildren(node.preSibling)){
  374. $('#downLevel').addClass('disabled');
  375. }
  376. }
  377. }
  378. //上移
  379. if(node && node.preSibling){
  380. $('#upMove').removeClass('disabled')
  381. }
  382. //下移
  383. if(node && node.nextSibling){
  384. $('#downMove').removeClass('disabled');
  385. }
  386. //插入定额
  387. if(node && (node.children.length === 0 || allRationChildren(node))){
  388. $('#insertRation').removeClass('disabled');
  389. }
  390. }
  391. //项目指引表焦点控制
  392. //@param {Number}row @return {void}
  393. function guideItemInitSel(row){
  394. let billsNode = bills.tree.selected;
  395. let node = null;
  396. if(billsNode && billsNode.guidance.tree){
  397. node = billsNode.guidance.tree.items[row];
  398. if(node){
  399. billsNode.guidance.tree.selected = node;
  400. }
  401. }
  402. refreshBtn(node);
  403. }
  404. //初始化当前库名
  405. //@param {String} @return {void}
  406. function initLibName(libName) {
  407. $('#libName')[0].outerHTML = $('#libName')[0].outerHTML.replace("XXX清单指引", libName);
  408. }
  409. //初始化各工作表
  410. //@param {Array}modules @return {void}
  411. function initWorkBooks(modules){
  412. for(let module of modules){
  413. buildSheet(module);
  414. }
  415. }
  416. //输出表数据(定额表)
  417. //@param {Object}sheet {Array}headers {Array}datas @return {void}
  418. function showData(sheet, headers, datas){
  419. let fuc = function () {
  420. sheet.setRowCount(datas.length);
  421. //复选框
  422. let checkBoxType = new GC.Spread.Sheets.CellTypes.CheckBox();
  423. sheet.setCellType(-1, 0, checkBoxType);
  424. for(let col = 0, cLen = headers.length; col < cLen; col++){
  425. for(let row = 0, rLen = datas.length; row < rLen; row++){
  426. sheet.setValue(row, col, datas[row][headers[col]['dataCode']]);
  427. }
  428. }
  429. };
  430. renderSheetFunc(sheet, fuc);
  431. }
  432. //根据定额章节树ID获取定额(从数据缓存中获取,定额数据一开始一次性拉取)
  433. //@param {Number}sectionId {Array}rations @return {Array}
  434. function getRationsBySectionId(sectionId, rations) {
  435. if(!sectionId || !rations){
  436. return [];
  437. }
  438. return _.filter(rations, {sectionId});
  439. }
  440. //定额章节树焦点控制
  441. //@param {Number}row @return {void}
  442. function sectionInitSel(row) {
  443. let rationSheet = ration.workBook.getActiveSheet();
  444. let sectionNode = section.tree ? section.tree.items[row] : null;
  445. if(sectionNode && sectionNode.children.length === 0){
  446. let sectionRations = getRationsBySectionId(sectionNode.data.ID, ration.datas);
  447. ration.cache = sectionRations;
  448. showData(rationSheet, ration.headers, sectionRations);
  449. }
  450. else {
  451. cleanData(rationSheet, ration.headers, 0);
  452. }
  453. }
  454. //初始化定额条目
  455. //@param {Number}rationLibId @return {void}
  456. function initRationItems(rationLibId){
  457. $.bootstrapLoading.start();
  458. //获取定额章节树
  459. let sectionSheet = section.workBook.getActiveSheet();
  460. CommonAjax.post('/rationRepository/api/getRationTree', {rationLibId: rationLibId}, function (sectionDatas) {
  461. //获取所有定额数据
  462. CommonAjax.post('/rationRepository/api/getRationItemsByLib', {rationLibId: rationLibId}, function (rstData) {
  463. section.cache = sectionDatas;
  464. initTree(section, section.workBook.getActiveSheet(), section.treeSetting, sectionDatas);
  465. //初始焦点在第一行(切换库)
  466. sectionSheet.setActiveCell(0, 0);
  467. rstData.sort(function (a, b) {
  468. let rst = 0;
  469. if(a.code > b.code){
  470. rst = 1;
  471. }
  472. else if(a.code < b.code){
  473. rst = -1;
  474. }
  475. return rst;
  476. });
  477. ration.datas = rstData;
  478. sectionInitSel(0);
  479. $.bootstrapLoading.end();
  480. }, function () {
  481. $.bootstrapLoading.end();
  482. });
  483. }, function () {
  484. $.bootstrapLoading.end();
  485. });
  486. }
  487. //初始化定额库选择
  488. //@param {String}compilationId @return {void}
  489. function initRationLibs(compilationId){
  490. CommonAjax.post('/rationRepository/api/getRationLibsByCompilation', {compilationId: compilationId}, function (rstData) {
  491. $('#rationLibSel').empty();
  492. for(let rationLib of rstData){
  493. let opt = `<option value="${rationLib.ID}">${rationLib.dispName}</option>`;
  494. $('#rationLibSel').append(opt);
  495. }
  496. //初始选择
  497. initRationItems(parseInt($('#rationLibSel').select().val()));
  498. $('#rationLibSel').change(function () {
  499. let rationLibId = parseInt($(this).select().val());
  500. initRationItems(rationLibId);
  501. })
  502. });
  503. }
  504. //获取指引库信息及关联的清单
  505. //@param {Number}libID {Function}callback @return {Object}
  506. function getLibWithBills(libID, callback){
  507. CommonAjax.post('/billsGuidance/api/getLibWithBills', {libID: libID}, function (rstData) {
  508. initRationLibs(rstData.guidanceLib.compilationId);
  509. bills.cache = rstData.bills;
  510. initLibName(rstData.guidanceLib.name);
  511. initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, bills.cache);
  512. //每一棵项目指引树挂在清单节点上
  513. for(let node of bills.tree.items){
  514. node.guidance = {tree: null, controller: null};
  515. }
  516. //默认初始节点
  517. billsInitSel(0);
  518. if(callback){
  519. callback(rstData);
  520. }
  521. }, function (msg) {
  522. window.location.href = '/billsGuidance/main';
  523. });
  524. }
  525. //初始化并输出树
  526. //@param {Object}module {Object}sheet {Object}treeSetting {Array}datas
  527. function initTree(module, sheet, treeSetting, datas){
  528. module.tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
  529. module.controller = TREE_SHEET_CONTROLLER.createNew(module.tree, sheet, treeSetting);
  530. module.tree.loadDatas(datas);
  531. module.controller.showTreeData();
  532. }
  533. //更新项目指引
  534. //@param {Array}updateDatas {Function}callback @return {void}
  535. function updateGuideItems(updateDatas, callback){
  536. CommonAjax.post('/billsGuidance/api/updateItems', {updateDatas: updateDatas}, function (rstData) {
  537. if(callback){
  538. callback(rstData);
  539. }
  540. });
  541. }
  542. //项目指引编辑
  543. //@param {Object}sheet {Array}cells
  544. function edit(sheet, cells){
  545. let updateDatas = [];
  546. //同步节点数据
  547. let syncDatas = [];
  548. for(let cell of cells){
  549. let text = sheet.getValue(cell.row, cell.col);
  550. text = text ? text : '';
  551. let node = bills.tree.selected.guidance.tree.items[cell.row];
  552. if(node.data.name != text){
  553. syncDatas.push({node: node, text: text});
  554. updateDatas.push({updateType: updateType.update, findData: {ID: node.getID()}, updateData: {name: text}});
  555. }
  556. }
  557. if(updateDatas.length > 0){
  558. updateGuideItems(updateDatas, function () {
  559. for(let syncData of syncDatas){
  560. syncData.node.data.name = syncData.text;
  561. }
  562. }, function () {
  563. //失败恢复
  564. renderSheetFunc(sheet, function () {
  565. for(let syncData of syncDatas){
  566. sheet.setValue(syncData.node.serialNo(), 0, syncData.node.data.name ? syncData.node.data.name : '');
  567. }
  568. });
  569. });
  570. }
  571. }
  572. //项目指引插入,支持一次插入多条数据
  573. //@param {Array}datas {Function}callback @return {void}
  574. function insert(datas, callback = null){
  575. $.bootstrapLoading.start();
  576. let sheet = guideItem.workBook.getActiveSheet();
  577. let controller = bills.tree.selected.guidance.controller;
  578. let selected = bills.tree.selected.guidance.tree.selected;
  579. let updateDatas = [];
  580. //建立数组下标索引
  581. let newDataIndex = {};
  582. for(let i = 0; i < datas.length; i++){
  583. let newNodeData = {
  584. libID: libID, ID: uuid.v1(), ParentID: selected ? selected.getParentID() : -1, NextSiblingID: selected ? selected.getNextSiblingID() : -1,
  585. billsID: bills.tree.selected.getID()
  586. };
  587. //定额类型插入当前工作内容焦点行,
  588. if(selected && selected.data.type === itemType.job && datas[i].type === itemType.ration){
  589. newNodeData.ParentID = selected.getID();
  590. newNodeData.NextSiblingID = -1;
  591. }
  592. Object.assign(newNodeData, datas[i]);
  593. newDataIndex[i] = newNodeData;
  594. }
  595. for(let i = 0; i < datas.length; i++){
  596. //第一个节点
  597. if(i === 0){
  598. //非插入成子节点,更新选中节点NestSiblingID
  599. if(selected && !(selected.data.type === itemType.job && datas[i].type === itemType.ration)){
  600. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: newDataIndex[i].ID}});
  601. }
  602. }
  603. //非最后一个节点
  604. if(i !== datas.length - 1){
  605. newDataIndex[i].NextSiblingID = newDataIndex[i + 1].ID;
  606. }
  607. updateDatas.push({updateType: updateType.create, updateData: newDataIndex[i]});
  608. }
  609. updateGuideItems(updateDatas, function () {
  610. for(let updateData of updateDatas){
  611. if(updateData.updateType === updateType.create){
  612. let newNode = controller.insertByIDS(updateData.updateData.ID, updateData.updateData.ParentID, updateData.updateData.NextSiblingID);
  613. //同步data
  614. Object.assign(newNode.data, updateData.updateData);
  615. sheet.setValue(newNode.serialNo(), 0, newNode.data.name);
  616. refreshBtn(newNode);
  617. }
  618. }
  619. if(callback){
  620. callback();
  621. }
  622. $.bootstrapLoading.end();
  623. });
  624. }
  625. //项目指引删除操作
  626. //@return {void}
  627. function del(){
  628. $.bootstrapLoading.start();
  629. let controller = bills.tree.selected.guidance.controller;
  630. let selected = bills.tree.selected.guidance.tree.selected;
  631. let updateDatas = [];
  632. function getDelDatas(node){
  633. updateDatas.push({updateType: updateType.del, findData: {ID: node.getID()}, updateData: {deleted: true}});
  634. if(node.children.length > 0){
  635. for(let c of node.children){
  636. getDelDatas(c);
  637. }
  638. }
  639. }
  640. getDelDatas(selected);
  641. updateGuideItems(updateDatas, function () {
  642. controller.delete();
  643. refreshBtn(bills.tree.selected.guidance.tree.selected);
  644. $.bootstrapLoading.end();
  645. });
  646. }
  647. //项目指引升级
  648. //@return {void}
  649. function upLevel(){
  650. $.bootstrapLoading.start();
  651. let controller = bills.tree.selected.guidance.controller;
  652. let selected = bills.tree.selected.guidance.tree.selected;
  653. let updateDatas = [];
  654. //更新父节点
  655. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getParentID()}, updateData: {NextSiblingID: selected.getID()}});
  656. //更新选中节点
  657. updateDatas.push({udpateType: updateType.update, findData: {ID: selected.getID()},
  658. updateData: {ParentID: selected.parent.getParentID(), NextSiblingID: selected.parent.getNextSiblingID()}});
  659. if(selected.nextSibling && selected.children.length > 0){
  660. //更新选中节点最末子节点
  661. let lastChild = selected.children[selected.children.length - 1];
  662. updateDatas.push({updateType: updateType.update, findData: {ID: lastChild.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  663. }
  664. updateGuideItems(updateDatas, function () {
  665. controller.upLevel();
  666. refreshBtn(bills.tree.selected.guidance.tree.selected);
  667. $.bootstrapLoading.end();
  668. });
  669. }
  670. //项目指引降级
  671. //@return {void}
  672. function downLevel(){
  673. $.bootstrapLoading.start();
  674. let controller = bills.tree.selected.guidance.controller;
  675. let selected = bills.tree.selected.guidance.tree.selected;
  676. let updateDatas = [];
  677. //更新前兄弟节点
  678. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  679. //更新前兄弟节点最末子节点
  680. if(selected.preSibling.children.length > 0){
  681. let lastChild = selected.preSibling.children[selected.preSibling.children.length - 1];
  682. updateDatas.push({updateType: updateType.update, findData: {ID: lastChild.getID()}, updateData: {NextSiblingID: selected.getID()}});
  683. }
  684. //更新选中节点
  685. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {ParentID: selected.preSibling.getID(), NextSiblingID: -1}});
  686. updateGuideItems(updateDatas, function () {
  687. controller.downLevel();
  688. refreshBtn(bills.tree.selected.guidance.tree.selected);
  689. $.bootstrapLoading.end();
  690. });
  691. }
  692. //项目指引上移
  693. //@return {void}
  694. function upMove(){
  695. $.bootstrapLoading.start();
  696. let controller = bills.tree.selected.guidance.controller;
  697. let selected = bills.tree.selected.guidance.tree.selected;
  698. let updateDatas = [];
  699. //更新前节点
  700. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  701. //更新前前节点
  702. if(selected.preSibling.preSibling){
  703. updateDatas.push({udpateType: updateType.update, findData: {ID: selected.preSibling.preSibling.getID()}, updateData: {NextSiblingID: selected.getID()}});
  704. }
  705. //更新选中节点
  706. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: selected.preSibling.getID()}});
  707. updateGuideItems(updateDatas, function () {
  708. controller.upMove();
  709. refreshBtn(bills.tree.selected.guidance.tree.selected);
  710. $.bootstrapLoading.end();
  711. });
  712. }
  713. //项目指引下移
  714. //@return {void}
  715. function downMove(){
  716. $.bootstrapLoading.start();
  717. let controller = bills.tree.selected.guidance.controller;
  718. let selected = bills.tree.selected.guidance.tree.selected;
  719. let updateDatas = [];
  720. //更新下节点
  721. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getNextSiblingID()}, updateData: {NextSiblingID: selected.getID()}});
  722. //更新前节点
  723. if(selected.preSibling){
  724. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  725. }
  726. //更新选中节点
  727. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: selected.nextSibling.getNextSiblingID()}});
  728. updateGuideItems(updateDatas, function () {
  729. controller.downMove();
  730. refreshBtn(bills.tree.selected.guidance.tree.selected);
  731. $.bootstrapLoading.end();
  732. });
  733. }
  734. //获取定额类型的项目指引名称,通过定额转换
  735. //@param {Object}ration @return {String}
  736. function getRationItemName(ration){
  737. let arr = [];
  738. arr.push(ration.code ? ration.code : '');
  739. arr.push(ration.name ? ration.name : '');
  740. arr.push(ration.basePrice ? ration.basePrice : '');
  741. let rst = arr.join(' ');
  742. rst += `/${ration.unit ? ration.unit : ''}`;
  743. return rst;
  744. }
  745. //获取选中的定额表行
  746. //@return {Array}
  747. function getCheckedRationRows(){
  748. let rst = [];
  749. let sheet = ration.workBook.getActiveSheet();
  750. for(let i = 0; i < sheet.getRowCount(); i++){
  751. let checked = sheet.getValue(i, 0);
  752. if(checked){
  753. rst.push(i);
  754. }
  755. }
  756. return rst;
  757. }
  758. //清空选中定额表行
  759. //@param {Array}rows @return {void}
  760. function clearCheckedRation(rows) {
  761. let sheet = ration.workBook.getActiveSheet();
  762. renderSheetFunc(sheet, function () {
  763. for(let row of rows){
  764. sheet.setValue(row, 0, 0);
  765. }
  766. });
  767. }
  768. //获取要插入的定额数据
  769. //@param {Array}rows @return {Array}
  770. function getInsertRations(rows){
  771. let rst = [];
  772. //当前已存在定额
  773. let curRationIems = [];
  774. let selected = bills.tree.selected.guidance.tree.selected;
  775. if(selected){
  776. if(selected.data.type === itemType.job){
  777. curRationIems = selected.children;
  778. }
  779. else {
  780. curRationIems = selected.parent ? selected.parent.children : selected.tree.roots;
  781. }
  782. }
  783. for(let row of rows){
  784. let selRation = ration.cache[row];
  785. if(selRation){
  786. //添加的定额是否已存在,不重复添加
  787. let isExist = false;
  788. for(let curRation of curRationIems){
  789. if(curRation.data.rationID == selRation.ID){
  790. isExist = true;
  791. break;
  792. }
  793. }
  794. if(!isExist){
  795. rst.push({type: itemType.ration, name: getRationItemName(selRation), rationID: selRation.ID});
  796. }
  797. }
  798. }
  799. return rst;
  800. }
  801. //初始化个按钮点击
  802. //@return {void}
  803. function initBtn(){
  804. $('#insert').click(function () {
  805. insert([{type: itemType.job, name: ''}]);
  806. });
  807. $('#delConfirm').click(function () {
  808. del();
  809. $('#delAlert').modal('hide');
  810. });
  811. $('#del').click(function () {
  812. $('#delAlert').modal('show');
  813. });
  814. $('#upLevel').click(function () {
  815. upLevel();
  816. });
  817. $('#downLevel').click(function () {
  818. downLevel();
  819. });
  820. $('#upMove').click(function () {
  821. upMove();
  822. });
  823. $('#downMove').click(function () {
  824. downMove();
  825. });
  826. $('#insertRation').click(function () {
  827. let checkedRows = getCheckedRationRows();
  828. let insertDatas = getInsertRations(checkedRows);
  829. if(insertDatas.length > 0){
  830. insert(insertDatas, function () {
  831. //清空选择
  832. clearCheckedRation(checkedRows);
  833. });
  834. }
  835. else {
  836. clearCheckedRation(checkedRows);
  837. }
  838. });
  839. //搜索定额
  840. $('#searchBtn').click(function () {
  841. let searchStr = $('#searchText').val();
  842. if(!searchStr || searchStr === ''){
  843. ration.cache = ration.datas;
  844. }
  845. else{
  846. ration.cache = _.filter(ration.datas, function (data) {
  847. return data.code.includes(searchStr);
  848. });
  849. }
  850. $('.top-content').hide();
  851. $('#searchCount').text(`搜索结果: ${ration.cache.length}`);
  852. $('#rationSearchResult').show();
  853. autoFlashHeight();
  854. ration.workBook.refresh();
  855. let rationSheet = ration.workBook.getActiveSheet();
  856. renderSheetFunc(rationSheet, function () {
  857. clearCheckedRation(getCheckedRationRows());
  858. showData(rationSheet, ration.headers, ration.cache);
  859. })
  860. });
  861. //关闭搜索
  862. $('#rationSearchResult a').click(function () {
  863. $('.top-content').show();
  864. $('#rationSearchResult').hide();
  865. autoFlashHeight();
  866. renderSheetFunc(ration.workBook.getActiveSheet(), function () {
  867. clearCheckedRation(getCheckedRationRows());
  868. });
  869. section.workBook.refresh();
  870. ration.workBook.refresh();
  871. $('#searchText').val('');
  872. //恢复章节树下的定额
  873. sectionInitSel(section.workBook.getActiveSheet().getActiveRowIndex());
  874. });
  875. //执行搜索
  876. $('#searchText').keyup(function (e) {
  877. $('#searchBtn').click();
  878. });
  879. }
  880. //初始化视图
  881. //@param {void} @return {void}
  882. function initViews(){
  883. let modules = [bills, guideItem, section, ration];
  884. initWorkBooks(modules);
  885. getLibWithBills(libID);
  886. initBtn();
  887. }
  888. return {initViews};
  889. })();
  890. $(document).ready(function () {
  891. billsGuidance.initViews();
  892. });