billsGuidance.js 30 KB

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