billsGuidance.js 42 KB

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