billsGuidance.js 30 KB

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