std_ration_lib.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. * Standard Ration Lib
  3. * Created by Mai on 2017/5/16.
  4. */
  5. /*var rationChapterSpread, sectionRationsSpread;*/
  6. var rationLibObj = {
  7. doAfterGetRationTree: null, //获取章节树回调
  8. doAfterLoadGetRations: null, //获取章节树下定额后回调
  9. rationChapterSpread: null,
  10. sectionRationsSpread: null,
  11. resultSpread: null,
  12. rationChapterTreeController: null,
  13. refreshSettingForHint: function () {
  14. TREE_SHEET_HELPER.initSetting($('#stdSectionRations')[0], rationLibObj.sectionRationsSetting);
  15. },
  16. checkSpread: function () {
  17. if (!this.rationChapterSpread) {
  18. this.rationChapterSpread = SheetDataHelper.createNewSpread($('#stdRationChapter')[0]);
  19. sheetCommonObj.spreadDefaultStyle(this.rationChapterSpread);
  20. this.rationChapterSpread.getSheet(0).name('stdRationLib_chapter');
  21. this.rationChapterSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onChapterSpreadCellDoubleClick);
  22. }
  23. if (!this.sectionRationsSpread) {
  24. this.sectionRationsSpread = SheetDataHelper.createNewSpread($('#stdSectionRations')[0]);
  25. sheetCommonObj.spreadDefaultStyle(this.sectionRationsSpread);
  26. this.sectionRationsSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onRationSpreadCellDoubleClick);
  27. this.refreshSettingForHint();
  28. }
  29. },
  30. refreshSpread: function () {
  31. if (this.rationChapterSpread) {
  32. this.rationChapterSpread.refresh();
  33. }
  34. if (this.sectionRationsSpread) {
  35. this.sectionRationsSpread.refresh();
  36. }
  37. if(this.resultSpread){
  38. this.resultSpread.refresh();
  39. }
  40. },
  41. loadStdRationLibs: function () {
  42. let select = $('#stdRationLibSelect');
  43. select.empty();
  44. let ration_lib = projectInfoObj.projectInfo.engineeringInfo.ration_lib;
  45. let selectedRationLib = sessionStorage.getItem('stdRationLib');
  46. ration_lib.forEach(function (data) {
  47. let option = $('<option>').val(data.id).text(data.name);
  48. //select.append($('<option>').val(data.id).text(data.name));
  49. if(selectedRationLib){
  50. if(data.id == selectedRationLib){
  51. option.attr('selected', 'selected');
  52. }
  53. }else if(data.isDefault == true){
  54. option.attr('selected', 'selected');
  55. }
  56. select.append(option);
  57. });
  58. if (select[0].options.length !== 0) {
  59. rationLibObj.loadStdRation(select.val());
  60. }
  61. },
  62. loadStdRation: function (rationLibID) {
  63. $.bootstrapLoading.start();
  64. var that = this;
  65. var showRationChapterTree = function (datas) {
  66. var rationChapterTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
  67. console.log(that);
  68. that.tree = rationChapterTree;
  69. var rationChapterTreeController = TREE_SHEET_CONTROLLER.createNew(rationChapterTree, that.rationChapterSpread.getActiveSheet(), that.rationChapterTreeSetting);
  70. rationChapterTree.loadDatas(datas);
  71. //读取展开收起状态
  72. let currentExpState = sessionStorage.getItem('stdRationLibExpState');
  73. if(currentExpState){
  74. that.tree.setExpandedByState(that.tree.items, currentExpState);
  75. }
  76. else {
  77. //展开至第二层
  78. for(let root of that.tree.roots){
  79. root.setExpanded(true);
  80. that.tree.setRootExpanded(root.children, false);
  81. }
  82. }
  83. rationChapterTreeController.showTreeData();
  84. rationChapterTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
  85. rationLibObj.loadSectionRations(node && node.children.length === 0 ? node.getID() : null);
  86. });
  87. if (rationChapterTree.firstNode() && rationChapterTree.firstNode().length === 0) {
  88. rationLibObj.loadSectionRations(rationChapterTree.firstNode().getID());
  89. } else {
  90. rationLibObj.loadSectionRations();
  91. };
  92. };
  93. CommonAjax.post('/complementaryRation/api/getRationTree', {userId: userID, rationRepId: rationLibID}, function (datas) {
  94. showRationChapterTree(datas);
  95. if(that.doAfterGetRationTree){
  96. that.doAfterGetRationTree();
  97. }
  98. $.bootstrapLoading.end();
  99. }, function () {
  100. showRationChapterTree([]);
  101. $.bootstrapLoading.end();
  102. });
  103. },
  104. //双击隐藏显示
  105. onChapterSpreadCellDoubleClick: function (sender, args) {
  106. let me = rationLibObj;
  107. let node = me.tree.items[args.row];
  108. if (!node || node.children.length === 0)
  109. return;
  110. node.setExpanded(!node.expanded);
  111. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  112. TREE_SHEET_HELPER.massOperationSheet(args.sheet, function () {
  113. let iCount = node.posterityCount(), i, child;
  114. for (i = 0; i < iCount; i++) {
  115. child = me.tree.items[args.row + i + 1];
  116. args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
  117. }
  118. args.sheet.invalidateLayout();
  119. });
  120. args.sheet.repaint();
  121. },
  122. setTagForHint: function (sheet, datas) {
  123. sheet.suspendPaint();
  124. sheet.suspendEvent();
  125. for(let i = 0, len = sheet.getRowCount(); i < len; i++){
  126. sheet.setTag(i, 0, '');
  127. }
  128. for(let i = 0, len = datas.length; i < len; i++){
  129. if(datas[i].code === 'AA0032'){
  130. console.log(datas[i]);
  131. }
  132. sheet.setTag(i, 0, datas[i].hint ? datas[i].hint : '');
  133. }
  134. sheet.resumePaint();
  135. sheet.resumeEvent();
  136. },
  137. loadSectionRations: function (sectionID) {
  138. let me = this;
  139. var showDatas = function (datas, setting) {
  140. let rationSheet = rationLibObj.sectionRationsSpread.getActiveSheet();
  141. TREE_SHEET_HELPER.massOperationSheet(rationSheet, function () {
  142. rationSheet.setColumnWidth(0, 25, GC.Spread.Sheets.SheetArea.rowHeader);
  143. });
  144. SheetDataHelper.loadSheetHeader(setting, rationLibObj.sectionRationsSpread.getActiveSheet());
  145. SheetDataHelper.loadSheetData(setting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
  146. rationLibObj.setTagForHint(rationSheet, datas);
  147. };
  148. if (sectionID) {
  149. CommonAjax.post('/complementaryRation/api/getRationGljItemsBySection', {user_Id: userID, sectionId: sectionID}, function (datas) {
  150. showDatas(datas, rationLibObj.sectionRationsSetting);
  151. if(me.doAfterLoadGetRations){
  152. me.doAfterLoadGetRations(datas);
  153. me.doAfterLoadGetRations = null;
  154. }
  155. }, function () {
  156. showDatas([], rationLibObj.sectionRationsSetting);
  157. });
  158. } else {
  159. showDatas([], rationLibObj.sectionRationsSetting);
  160. }
  161. },
  162. onRationSpreadCellDoubleClick: function (sender, args) {
  163. var select = $('#stdRationLibSelect'), rationCode = args.sheet.getText(args.row, 0);
  164. if (rationCode !== '') {
  165. projectObj.project.Ration.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  166. projectObj.setActiveCell('quantity', true);
  167. });
  168. }
  169. },
  170. loadStdRationContextMenu: function () {
  171. let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet(), rationModel = projectObj.project.Ration;;
  172. $.contextMenu({
  173. selector: '#stdSectionRations',
  174. build: function ($trigger, e) {
  175. let target = SheetDataHelper.safeRightClickSelection($trigger, e, rationSpread);
  176. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  177. },
  178. items: {
  179. "insertStdRation": {
  180. name: "插入定额",
  181. icon: 'fa-sign-in',
  182. callback: function (key, opt) {
  183. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  184. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  185. if (rationCode !== '') {
  186. rationModel.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  187. });
  188. }
  189. }
  190. },
  191. "replaceStdRation": {
  192. name: "替换定额",
  193. icon: 'fa-sign-in',
  194. callback: function (key, opt) {
  195. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  196. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  197. let mainTreeSelected = projectObj.project.mainTree.selected;
  198. if (rationCode !== ''&&mainTreeSelected&&mainTreeSelected.sourceType == rationModel.getSourceType()) {
  199. rationModel.updateRationCodes([{'node':mainTreeSelected,value:rationCode}]);
  200. }
  201. }
  202. },
  203. }
  204. });
  205. },
  206. expandSearchNodes: function(nodes){
  207. let that = rationLibObj;
  208. TREE_SHEET_HELPER.massOperationSheet(that.rationChapterSpread.getActiveSheet(), function () {
  209. function expParentNode(node){
  210. if(node.parent){
  211. expParentNode(node.parent);
  212. if(!node.parent.expanded){
  213. node.parent.setExpanded(true);
  214. }
  215. }
  216. }
  217. for(let node of nodes){
  218. expParentNode(node);
  219. }
  220. TREE_SHEET_HELPER.refreshTreeNodeData(that.rationChapterTreeSetting, that.rationChapterSpread.getActiveSheet(), that.tree.roots, true);
  221. TREE_SHEET_HELPER.refreshNodesVisible(that.tree.roots, that.rationChapterSpread.getActiveSheet(), true);
  222. });
  223. },
  224. initSel: function (row) {
  225. let me = this;
  226. let sheet = me.rationChapterSpread.getActiveSheet();
  227. sheet.setActiveCell(row, 0);
  228. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  229. let sectionNode = me.tree.items[row] || null;
  230. me.loadSectionRations(sectionNode && sectionNode.children.length === 0 ? sectionNode.data.ID : null);
  231. },
  232. locateAtRation: function(libID, code){
  233. let me = rationLibObj;
  234. //查找定额,以确定定额所在章节节点
  235. let firstLibID = projectInfoObj.projectInfo.engineeringInfo.ration_lib.length > 0 ?
  236. projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id : null;
  237. let locateRow = 0,
  238. locateSubRow = 0;
  239. CommonAjax.post('/complementaryRation/api/getRationItem', {rationRepIds: [libID], code: code}, function (ration) {
  240. if(ration && ration.sectionId){
  241. let sectionNode = me.tree.findNode(ration.sectionId);
  242. if(sectionNode){
  243. me.expandSearchNodes([sectionNode]);
  244. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  245. }
  246. locateRow = sectionNode.serialNo();
  247. me.doAfterLoadGetRations = function (rationItems) {
  248. let rationSheet = me.sectionRationsSpread.getActiveSheet();
  249. locateSubRow = _.findIndex(rationItems, {ID: ration.ID});
  250. rationSheet.setActiveCell(locateSubRow, 0);
  251. rationSheet.showRow(locateSubRow, GC.Spread.Sheets.VerticalPosition.center);
  252. };
  253. me.initSel(locateRow);
  254. }
  255. else {
  256. me.initSel(locateRow);
  257. }
  258. }, function () {
  259. me.initSel(locateRow);
  260. });
  261. },
  262. rationChapterTreeSetting: {
  263. "emptyRowHeader": true,
  264. "rowHeaderWidth": 15,
  265. "emptyRows":0,
  266. "headRows":1,
  267. "headRowHeight":[30],
  268. "defaultRowHeight": 21,
  269. "treeCol": 0,
  270. "cols":[{
  271. "width":400,
  272. "readOnly": true,
  273. "head":{
  274. "titleNames":["名称"],
  275. "spanCols":[1],
  276. "spanRows":[1],
  277. "vAlign":[1],
  278. "hAlign":[1],
  279. "font":["Arial"]
  280. },
  281. "data":{
  282. "field":"name",
  283. "vAlign":1,
  284. "hAlign":0,
  285. "font":"Arial"
  286. }
  287. }]
  288. },
  289. sectionRationsSetting: {
  290. "emptyRows":3,
  291. "headRows":1,
  292. "headRowHeight":[20],
  293. "defaultRowHeight": 21,
  294. "cols":[{
  295. "width":60,
  296. "readOnly": true,
  297. "showHint": true,
  298. "head":{
  299. "titleNames":["编码"],
  300. "spanCols":[1],
  301. "spanRows":[1],
  302. "vAlign":[1],
  303. "hAlign":[1],
  304. "font":["Arial"]
  305. },
  306. "data":{
  307. "field":"code",
  308. "vAlign":1,
  309. "hAlign":0,
  310. "font":"Arial"
  311. }
  312. }, {
  313. "width":220,
  314. "readOnly": true,
  315. "head":{
  316. "titleNames":["名称"],
  317. "spanCols":[1],
  318. "spanRows":[1],
  319. "vAlign":[1],
  320. "hAlign":[1],
  321. "font":["Arial"]
  322. },
  323. "data":{
  324. "field":"name",
  325. "vAlign":1,
  326. "hAlign":0,
  327. "font":"Arial"
  328. }
  329. }, {
  330. "width":55,
  331. "readOnly":true,
  332. "head":{
  333. "titleNames":["单位"],
  334. "spanCols":[1],
  335. "spanRows":[1],
  336. "vAlign":[1],
  337. "hAlign":[1],
  338. "font":["Arial"]
  339. },
  340. "data":{
  341. "field":"unit",
  342. "vAlign":1,
  343. "hAlign":1,
  344. "font":"Arial"
  345. }
  346. }, {
  347. "width":60,
  348. "readOnly":true,
  349. "head":{
  350. "titleNames":["基价"],
  351. "spanCols":[1],
  352. "spanRows":[1],
  353. "vAlign":[1],
  354. "hAlign":[1],
  355. "font":["Arial"]
  356. },
  357. "data":{
  358. "field":"basePrice",
  359. "vAlign":1,
  360. "hAlign":2,
  361. "font":"Arial"
  362. }
  363. }]
  364. },
  365. getStdRationLibIDs: function () {
  366. let ids = [];
  367. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  368. alert('当前项目无定额库,请添加定额库。');
  369. return null;
  370. }
  371. for(let rationLib of projectInfoObj.projectInfo.engineeringInfo.ration_lib){
  372. ids.push(rationLib.id);
  373. }
  374. return ids;
  375. },
  376. getCurrentStdRationLibID:function () {
  377. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  378. alert('当前项目无定额库,请添加定额库。');
  379. return null;
  380. }
  381. if($('#stdRationLibSelect').val()){
  382. return parseInt($('#stdRationLibSelect').val());
  383. }else {
  384. return projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id;
  385. }
  386. },
  387. getFirstStdRationLibID: function () {
  388. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  389. alert('当前项目无定额库,请添加定额库。');
  390. return null;
  391. }
  392. return parseInt(projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id);
  393. },
  394. getDefaultStdRationLibID:function(){
  395. let ration_lib = projectInfoObj.projectInfo.engineeringInfo.ration_lib;
  396. if(ration_lib.length === 0){
  397. alert('当前项目无定额库,请添加定额库。');
  398. return null;
  399. }
  400. let defaultLib = _.find(ration_lib,{'isDefault':true});
  401. let libID = defaultLib?defaultLib.id:ration_lib[0].id;
  402. return parseInt(libID);
  403. }
  404. };
  405. addEventOnResize(rationLibObj.refreshSettingForHint);
  406. //赋初始高度
  407. if($('#stdRationChapter').height() === 0 || $('#stdSectionRations').height() === 0){
  408. $('#stdRationChapter').height($(window).height()-$(".header").height()-$(".toolsbar").height()-$(".tools-bar-height-q").height()-312);
  409. $('#stdSectionRations').height(270);
  410. }
  411. $('#stdRationTab').bind('click', function () {
  412. if(!projectReadOnly){
  413. var select = $('#stdRationLibSelect');
  414. rationLibObj.checkSpread();
  415. if (select[0].options.length === 0) {
  416. rationLibObj.loadStdRationLibs();
  417. rationLibObj.loadStdRationContextMenu();
  418. };
  419. }
  420. });
  421. $('#stdRationLibSelect').change(function () {
  422. var select = $(this);
  423. if (this.children.length !== 0) {
  424. let rationLibId = select.val();
  425. sessionStorage.setItem('stdRationLib', rationLibId);
  426. sessionStorage.removeItem('stdRationLibExpState');
  427. rationLibObj.loadStdRation(rationLibId);
  428. }
  429. });
  430. //回车键搜索
  431. $('#rationSearchKeyword').bind('keypress', function (event) {
  432. if(event.keyCode === 13){
  433. $(this).blur();
  434. $('#rationSearch').click();
  435. }
  436. });
  437. $('#rationSearchKeyword').keyup(function () {
  438. let keyword = $('#rationSearchKeyword').val();
  439. if(keyword === ''){
  440. if($('#rationSearchResult').is(':visible')){
  441. $('#rationSearchResult').hide();
  442. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  443. autoFlashHeight();
  444. rationLibObj.refreshSpread();
  445. }
  446. }
  447. });
  448. $('#rationSearch').click(function () {
  449. var keyword = $('#rationSearchKeyword').val(), rationLibID = $('#stdRationLibSelect').val();
  450. if(keyword === ''){
  451. if($('#rationSearchResult').is(':visible')){
  452. $('#rationSearchResult').hide();
  453. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  454. autoFlashHeight();
  455. rationLibObj.refreshSpread();
  456. }
  457. return;
  458. }
  459. var getResultHtml = function (result) {
  460. var html = [], i, serialNo;
  461. html.push('<div class="d-flex justify-content-between">');
  462. html.push('<span>搜索结果:');
  463. html.push(result.length.toString());
  464. html.push('</span>');
  465. html.push('<a title="关闭搜索" class="btn btn-link btn-sm" href="javascript:void(0)"><i class="fa fa-remove" aria-hidden="true"></i></a>');
  466. html.push('</div>');
  467. html.push('<div class="w-100 main-data-side-search">');
  468. html.push('</div>');
  469. return html.join('');
  470. };
  471. let bindContextmenuOpr = function (sheet) {
  472. $.contextMenu({
  473. selector: '#rationSearchResult',
  474. build: function($triggerElement, e){
  475. //控制允许右键菜单在哪个位置出现
  476. let offset = $('.main-data-side-search').offset(),
  477. x = e.pageX - offset.left,
  478. y = e.pageY - offset.top;
  479. let target = sheet.hitTest(x, y);
  480. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  481. sheet.setActiveCell(target.row, target.col);
  482. return {
  483. callback: function(){},
  484. items: {
  485. "locate": {
  486. name: "定位至章节",
  487. disabled: function () {
  488. return target.row >= rationLibObj.resultCache.length;
  489. },
  490. icon: "fa-arrow-left",
  491. callback: function (key, opt) {
  492. let data = rationLibObj.resultCache[target.row];
  493. $('#rationSearchResult').hide();
  494. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  495. autoFlashHeight();
  496. rationLibObj.refreshSpread();
  497. rationLibObj.locateAtRation(data.rationRepId, data.code);
  498. }}
  499. }
  500. };
  501. }
  502. else{
  503. return false;
  504. }
  505. }
  506. });
  507. };
  508. var showResult = function (result) {
  509. rationLibObj.resultCache = result;
  510. if(!rationLibObj.resultSpread){
  511. let resultSpread = SheetDataHelper.createNewSpread($('.main-data-side-search')[0]);
  512. rationLibObj.resultSpread = resultSpread;
  513. bindContextmenuOpr(resultSpread.getActiveSheet());
  514. SheetDataHelper.loadSheetHeader(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet());
  515. resultSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, rationLibObj.onRationSpreadCellDoubleClick);
  516. }else {
  517. rationLibObj.resultSpread.refresh();
  518. }
  519. SheetDataHelper.loadSheetData(rationLibObj.sectionRationsSetting, rationLibObj.resultSpread.getActiveSheet(), result);
  520. rationLibObj.setTagForHint(rationLibObj.resultSpread.getActiveSheet(), result);
  521. };
  522. CommonAjax.post('/complementaryRation/api/findRation', {'user_id': userID, 'rationRepId': rationLibID, 'keyword': keyword}, function (result) {
  523. //sort
  524. result.sort(function (a, b) {
  525. let rst = 0;
  526. if(a.code > b.code) rst = 1;
  527. else if(a.code < b.code) rst = -1;
  528. return rst;
  529. });
  530. var resultObj = $('#rationSearchResult');
  531. /*resultObj.empty();
  532. resultObj.append(getResultHtml(result));*/
  533. $('#rationSearchCount').text(`搜索结果:${result.length.toString()}`);
  534. $('a', result).unbind('click');
  535. $('a', resultObj).bind('click', function () {
  536. resultObj.hide();
  537. $(".main-data-side-search", resultObj).height(0);
  538. autoFlashHeight();
  539. rationLibObj.refreshSpread();
  540. });
  541. resultObj.show();
  542. $(".main-data-side-search", resultObj).height($(window).height() - $(".header").height() - $(".toolsbar").height() - 64);
  543. showResult(result);
  544. });
  545. });