std_ration_lib.js 19 KB

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