billsGuidance.js 30 KB

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