std_ration_lib.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 (datas) {
  123. let sheet = this.sectionRationsSpread.getActiveSheet();
  124. sheet.suspendPaint();
  125. sheet.suspendEvent();
  126. for(let i = 0, len = sheet.getRowCount(); i < len; i++){
  127. sheet.setTag(i, 0, '');
  128. }
  129. for(let i = 0, len = datas.length; i < len; i++){
  130. sheet.setTag(i, 0, datas[i].hint ? datas[i].hint : '');
  131. }
  132. sheet.resumePaint();
  133. sheet.resumeEvent();
  134. },
  135. loadSectionRations: function (sectionID) {
  136. let me = this;
  137. var showDatas = function (datas, setting) {
  138. let rationSheet = rationLibObj.sectionRationsSpread.getActiveSheet();
  139. TREE_SHEET_HELPER.massOperationSheet(rationSheet, function () {
  140. rationSheet.setColumnWidth(0, 25, GC.Spread.Sheets.SheetArea.rowHeader);
  141. });
  142. SheetDataHelper.loadSheetHeader(setting, rationLibObj.sectionRationsSpread.getActiveSheet());
  143. SheetDataHelper.loadSheetData(setting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
  144. rationLibObj.setTagForHint(datas);
  145. };
  146. if (sectionID) {
  147. CommonAjax.post('/complementaryRation/api/getRationGljItemsBySection', {user_Id: userID, sectionId: sectionID}, function (datas) {
  148. showDatas(datas, rationLibObj.sectionRationsSetting);
  149. if(me.doAfterLoadGetRations){
  150. me.doAfterLoadGetRations(datas);
  151. me.doAfterLoadGetRations = null;
  152. }
  153. }, function () {
  154. showDatas([], rationLibObj.sectionRationsSetting);
  155. });
  156. } else {
  157. showDatas([], rationLibObj.sectionRationsSetting);
  158. }
  159. },
  160. onRationSpreadCellDoubleClick: function (sender, args) {
  161. var select = $('#stdRationLibSelect'), rationCode = args.sheet.getText(args.row, 0);
  162. if (rationCode !== '') {
  163. projectObj.project.Ration.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  164. projectObj.setActiveCell('quantity', true);
  165. });
  166. }
  167. },
  168. loadStdRationContextMenu: function () {
  169. let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet(), rationModel = projectObj.project.Ration;;
  170. $.contextMenu({
  171. selector: '#stdSectionRations',
  172. build: function ($trigger, e) {
  173. let target = SheetDataHelper.safeRightClickSelection($trigger, e, rationSpread);
  174. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  175. },
  176. items: {
  177. "insertStdRation": {
  178. name: "插入定额",
  179. icon: 'fa-sign-in',
  180. callback: function (key, opt) {
  181. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  182. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  183. if (rationCode !== '') {
  184. rationModel.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  185. });
  186. }
  187. }
  188. },
  189. "replaceStdRation": {
  190. name: "替换定额",
  191. icon: 'fa-sign-in',
  192. callback: function (key, opt) {
  193. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  194. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  195. let mainTreeSelected = projectObj.project.mainTree.selected;
  196. if (rationCode !== ''&&mainTreeSelected&&mainTreeSelected.sourceType == rationModel.getSourceType()) {
  197. rationModel.updateRationCodes([{'node':mainTreeSelected,value:rationCode}]);
  198. }
  199. }
  200. },
  201. }
  202. });
  203. },
  204. expandSearchNodes: function(nodes){
  205. let that = rationLibObj;
  206. TREE_SHEET_HELPER.massOperationSheet(that.rationChapterSpread.getActiveSheet(), function () {
  207. function expParentNode(node){
  208. if(node.parent){
  209. expParentNode(node.parent);
  210. if(!node.parent.expanded){
  211. node.parent.setExpanded(true);
  212. }
  213. }
  214. }
  215. for(let node of nodes){
  216. expParentNode(node);
  217. }
  218. TREE_SHEET_HELPER.refreshTreeNodeData(that.rationChapterTreeSetting, that.rationChapterSpread.getActiveSheet(), that.tree.roots, true);
  219. TREE_SHEET_HELPER.refreshNodesVisible(that.tree.roots, that.rationChapterSpread.getActiveSheet(), true);
  220. });
  221. },
  222. initSel: function (row) {
  223. let me = this;
  224. let sheet = me.rationChapterSpread.getActiveSheet();
  225. sheet.setActiveCell(row, 0);
  226. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  227. let sectionNode = me.tree.items[row] || null;
  228. me.loadSectionRations(sectionNode && sectionNode.children.length === 0 ? sectionNode.data.ID : null);
  229. },
  230. locateAtRation: function(code){
  231. let me = rationLibObj;
  232. //查找定额,以确定定额所在章节节点
  233. let firstLibID = projectInfoObj.projectInfo.engineeringInfo.ration_lib.length > 0 ?
  234. projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id : null;
  235. let locateRow = 0,
  236. locateSubRow = 0;
  237. CommonAjax.post('/complementaryRation/api/getRationItem', {rationRepIds: [firstLibID], code: code}, function (ration) {
  238. if(ration && ration.sectionId){
  239. let sectionNode = me.tree.findNode(ration.sectionId);
  240. if(sectionNode){
  241. me.expandSearchNodes([sectionNode]);
  242. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  243. }
  244. locateRow = sectionNode.serialNo();
  245. me.doAfterLoadGetRations = function (rationItems) {
  246. let rationSheet = me.sectionRationsSpread.getActiveSheet();
  247. locateSubRow = _.findIndex(rationItems, {ID: ration.ID});
  248. rationSheet.setActiveCell(locateSubRow, 0);
  249. rationSheet.showRow(locateSubRow, GC.Spread.Sheets.VerticalPosition.center);
  250. };
  251. me.initSel(locateRow);
  252. }
  253. else {
  254. me.initSel(locateRow);
  255. }
  256. }, function () {
  257. me.initSel(locateRow);
  258. });
  259. },
  260. rationChapterTreeSetting: {
  261. "emptyRowHeader": true,
  262. "rowHeaderWidth": 15,
  263. "emptyRows":0,
  264. "headRows":1,
  265. "headRowHeight":[30],
  266. "defaultRowHeight": 21,
  267. "treeCol": 0,
  268. "cols":[{
  269. "width":400,
  270. "readOnly": true,
  271. "head":{
  272. "titleNames":["名称"],
  273. "spanCols":[1],
  274. "spanRows":[1],
  275. "vAlign":[1],
  276. "hAlign":[1],
  277. "font":["Arial"]
  278. },
  279. "data":{
  280. "field":"name",
  281. "vAlign":1,
  282. "hAlign":0,
  283. "font":"Arial"
  284. }
  285. }]
  286. },
  287. sectionRationsSetting: {
  288. "emptyRows":3,
  289. "headRows":1,
  290. "headRowHeight":[20],
  291. "defaultRowHeight": 21,
  292. "cols":[{
  293. "width":60,
  294. "readOnly": true,
  295. "showHint": true,
  296. "head":{
  297. "titleNames":["编码"],
  298. "spanCols":[1],
  299. "spanRows":[1],
  300. "vAlign":[1],
  301. "hAlign":[1],
  302. "font":["Arial"]
  303. },
  304. "data":{
  305. "field":"code",
  306. "vAlign":1,
  307. "hAlign":0,
  308. "font":"Arial"
  309. }
  310. }, {
  311. "width":220,
  312. "readOnly": true,
  313. "head":{
  314. "titleNames":["名称"],
  315. "spanCols":[1],
  316. "spanRows":[1],
  317. "vAlign":[1],
  318. "hAlign":[1],
  319. "font":["Arial"]
  320. },
  321. "data":{
  322. "field":"name",
  323. "vAlign":1,
  324. "hAlign":0,
  325. "font":"Arial"
  326. }
  327. }, {
  328. "width":55,
  329. "readOnly":true,
  330. "head":{
  331. "titleNames":["单位"],
  332. "spanCols":[1],
  333. "spanRows":[1],
  334. "vAlign":[1],
  335. "hAlign":[1],
  336. "font":["Arial"]
  337. },
  338. "data":{
  339. "field":"unit",
  340. "vAlign":1,
  341. "hAlign":1,
  342. "font":"Arial"
  343. }
  344. }, {
  345. "width":60,
  346. "readOnly":true,
  347. "head":{
  348. "titleNames":["基价"],
  349. "spanCols":[1],
  350. "spanRows":[1],
  351. "vAlign":[1],
  352. "hAlign":[1],
  353. "font":["Arial"]
  354. },
  355. "data":{
  356. "field":"basePrice",
  357. "vAlign":1,
  358. "hAlign":2,
  359. "font":"Arial"
  360. }
  361. }]
  362. },
  363. getStdRationLibIDs: function () {
  364. let ids = [];
  365. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  366. alert('当前项目无定额库,请添加定额库。');
  367. return null;
  368. }
  369. for(let rationLib of projectInfoObj.projectInfo.engineeringInfo.ration_lib){
  370. ids.push(rationLib.id);
  371. }
  372. return ids;
  373. },
  374. getCurrentStdRationLibID:function () {
  375. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  376. alert('当前项目无定额库,请添加定额库。');
  377. return null;
  378. }
  379. if($('#stdRationLibSelect').val()){
  380. return parseInt($('#stdRationLibSelect').val());
  381. }else {
  382. return projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id;
  383. }
  384. },
  385. getFirstStdRationLibID: function () {
  386. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  387. alert('当前项目无定额库,请添加定额库。');
  388. return null;
  389. }
  390. return parseInt(projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id);
  391. },
  392. getDefaultStdRationLibID:function(){
  393. let ration_lib = projectInfoObj.projectInfo.engineeringInfo.ration_lib;
  394. if(ration_lib.length === 0){
  395. alert('当前项目无定额库,请添加定额库。');
  396. return null;
  397. }
  398. let defaultLib = _.find(ration_lib,{'isDefault':true});
  399. let libID = defaultLib?defaultLib.id:ration_lib[0].id;
  400. return parseInt(libID);
  401. }
  402. };
  403. addEventOnResize(rationLibObj.refreshSettingForHint);
  404. //赋初始高度
  405. if($('#stdRationChapter').height() === 0 || $('#stdSectionRations').height() === 0){
  406. $('#stdRationChapter').height($(window).height()-$(".header").height()-$(".toolsbar").height()-$(".tools-bar-height-q").height()-312);
  407. $('#stdSectionRations').height(270);
  408. }
  409. $('#stdRationTab').bind('click', function () {
  410. var select = $('#stdRationLibSelect');
  411. rationLibObj.checkSpread();
  412. if (select[0].options.length === 0) {
  413. rationLibObj.loadStdRationLibs();
  414. rationLibObj.loadStdRationContextMenu();
  415. };
  416. });
  417. $('#stdRationLibSelect').change(function () {
  418. var select = $(this);
  419. if (this.children.length !== 0) {
  420. let rationLibId = select.val();
  421. sessionStorage.setItem('stdRationLib', rationLibId);
  422. sessionStorage.removeItem('stdRationLibExpState');
  423. rationLibObj.loadStdRation(rationLibId);
  424. }
  425. });
  426. //回车键搜索
  427. $('#rationSearchKeyword').bind('keypress', function (event) {
  428. if(event.keyCode === 13){
  429. $(this).blur();
  430. $('#rationSearch').click();
  431. }
  432. });
  433. $('#rationSearch').click(function () {
  434. var keyword = $('#rationSearchKeyword').val(), rationLibID = $('#stdRationLibSelect').val();
  435. var getResultHtml = function (result) {
  436. var html = [], i, serialNo;
  437. html.push('<div class="d-flex justify-content-between">');
  438. html.push('<span>搜索结果:');
  439. html.push(result.length.toString());
  440. html.push('</span>');
  441. html.push('<a title="关闭搜索" class="btn btn-link btn-sm" href="javascript:void(0)"><i class="fa fa-remove" aria-hidden="true"></i></a>');
  442. html.push('</div>');
  443. html.push('<div class="w-100 main-data-side-search">');
  444. html.push('</div>');
  445. return html.join('');
  446. };
  447. var showResult = function (result) {
  448. let resultSpread = SheetDataHelper.createNewSpread($('.main-data-side-search')[0]);
  449. rationLibObj.resultSpread = resultSpread;
  450. rationLibObj.setTagForHint(datas);
  451. SheetDataHelper.loadSheetHeader(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet());
  452. SheetDataHelper.loadSheetData(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet(), result);
  453. resultSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, rationLibObj.onRationSpreadCellDoubleClick);
  454. };
  455. CommonAjax.post('/complementaryRation/api/findRation', {'user_id': userID, 'rationRepId': rationLibID, 'keyword': keyword}, function (result) {
  456. //sort
  457. result.sort(function (a, b) {
  458. let rst = 0;
  459. if(a.code > b.code) rst = 1;
  460. else if(a.code < b.code) rst = -1;
  461. return rst;
  462. });
  463. var resultObj = $('#rationSearchResult'), resultSpread = null;
  464. resultObj.empty();
  465. resultObj.append(getResultHtml(result));
  466. $('a', resultObj).click(function () {
  467. resultObj.hide();
  468. $(".main-data-side-search", resultObj).height(0);
  469. autoFlashHeight();
  470. //$(".main-data-side-d").height($(window).height() - $(".header").height() - $(".toolsbar").height() - $(".tools-bar-height-d").height() - 202);
  471. rationLibObj.refreshSpread();
  472. });
  473. resultObj.show();
  474. $(".main-data-side-search", resultObj).height($(window).height() - $(".header").height() - $(".toolsbar").height() - 64);
  475. showResult(result);
  476. });
  477. });