std_ration_lib.js 20 KB

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