std_ration_lib.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /**
  2. * Standard Ration Lib
  3. * Created by Mai on 2017/5/16.
  4. */
  5. /*var rationChapterSpread, sectionRationsSpread;*/
  6. var rationLibObj = {
  7. searchLimit: 50,
  8. libType: {complementary: 0, std: 1},
  9. compleRationLibId: 'compleRationLib',
  10. doAfterGetRationTree: null, //获取章节树回调
  11. doAfterLoadGetRations: null, //获取章节树下定额后回调
  12. rationChapterSpread: null,
  13. sectionRationsSpread: null,
  14. resultSpread: null,
  15. rationChapterTreeController: null,
  16. refreshSettingForHint: function () {
  17. TREE_SHEET_HELPER.initSetting($('#stdSectionRations')[0], rationLibObj.sectionRationsSetting);
  18. },
  19. checkSpread: function (tfrom) {
  20. let from = $('#divide_de').is(':visible')||tfrom?"divide_":"";
  21. if (!this[from+'rationChapterSpread']) {
  22. this[from+'rationChapterSpread'] = SheetDataHelper.createNewSpread($(`#${from}stdRationChapter`)[0]);
  23. let rationChapterSpread = this[from+'rationChapterSpread'];
  24. rationChapterSpread.getSheet(0).options.rowHeaderVisible = false;
  25. sheetCommonObj.spreadDefaultStyle(rationChapterSpread);
  26. rationChapterSpread.getSheet(0).name(from+'stdRationLib_chapter');
  27. rationChapterSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onChapterSpreadCellDoubleClick);
  28. }
  29. if (!this[from+'sectionRationsSpread']) {
  30. this[from+'sectionRationsSpread'] = SheetDataHelper.createNewSpread($(`#${from}stdSectionRations`)[0]);
  31. let sectionRationsSpread = this[from+'sectionRationsSpread'];
  32. sectionRationsSpread.getSheet(0).setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  33. sheetCommonObj.spreadDefaultStyle(sectionRationsSpread);
  34. if (!projectReadOnly) {
  35. sectionRationsSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onRationSpreadCellDoubleClick);
  36. }
  37. this.refreshSettingForHint();
  38. }
  39. },
  40. refreshSpread: function () {
  41. if (this.rationChapterSpread) {
  42. this.rationChapterSpread.refresh();
  43. }
  44. if (this.sectionRationsSpread) {
  45. this.sectionRationsSpread.refresh();
  46. }
  47. if(this.resultSpread){
  48. this.resultSpread.refresh();
  49. }
  50. },
  51. loadStdRationLibs: function () {
  52. let from = $('#divide_de').is(':visible')?"divide_":"";
  53. let select = $(`#${from}stdRationLibSelect`);
  54. select.empty();
  55. let ration_lib = projectObj.project.projectInfo.engineeringInfo.ration_lib;
  56. if(rationLibObj.inited != true){
  57. ration_lib.push({
  58. isDefault: false,
  59. id: rationLibObj.compleRationLibId,
  60. name: '我的补充定额'
  61. });
  62. }
  63. let selectedRationLib = sessionStorage.getItem(`${from}stdRationLib`);
  64. ration_lib.forEach(function (data) {
  65. let option = $('<option>').val(data.id).text(data.name);
  66. if(selectedRationLib){
  67. if(data.id == selectedRationLib){
  68. option.attr('selected', 'selected');
  69. }
  70. }else if(data.isDefault == true){
  71. option.attr('selected', 'selected');
  72. }
  73. select.append(option);
  74. });
  75. //我的补充定额库
  76. /* let $opt = $('<option>').val(rationLibObj.compleRationLibId).text('我的补充定额');
  77. select.append($opt);*/
  78. if (select[0].options.length !== 0) {
  79. rationLibObj.loadStdRation(select.val());
  80. }
  81. },
  82. initQuestionModal: function(row,sheetName) {
  83. let pre = $('#divide_de').is(':visible')?"divide_":"";
  84. let node = rationLibObj[pre+'tree'].items[row];
  85. while (node && !node.data.explanation){
  86. node = node.parent;
  87. }
  88. let explanation = node && node.data.explanation ? node.data.explanation : '无内容';
  89. node = rationLibObj[pre+'tree'].items[row];
  90. $('#questionContent1').html(explanation);
  91. while (node && !node.data.ruleText){
  92. node = node.parent;
  93. }
  94. let ruleText = node && node.data.ruleText ? node.data.ruleText : '无内容';
  95. $('#questionTab1').text('说明');
  96. $('#questionContent2').html(ruleText);
  97. $('#questionModal').modal('show');
  98. },
  99. hasExplanationRuleText: function(row) {
  100. let pre = $('#divide_de').is(':visible')?"divide_":"";
  101. let node = rationLibObj[pre+'tree'].items[row];
  102. if (!node) {
  103. return false;
  104. }
  105. while (node) {
  106. if (node.data.explanation || node.data.ruleText) {
  107. return true;
  108. }
  109. node = node.parent;
  110. }
  111. return false;
  112. },
  113. loadStdRation: function (rationLibID){
  114. let from = $('#divide_de').is(':visible')?"divide_":"";
  115. $.bootstrapLoading.start();
  116. rationLibObj.curLibType = rationLibID === rationLibObj.compleRationLibId ? rationLibObj.libType.complementary : rationLibObj.libType.std;
  117. var that = this;
  118. let rationChapterSpread = this[from+'rationChapterSpread'];
  119. var showRationChapterTree = function (datas) {
  120. var rationChapterTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
  121. that[from+'tree'] = rationChapterTree;
  122. var rationChapterTreeController = TREE_SHEET_CONTROLLER.createNew(rationChapterTree, rationChapterSpread.getActiveSheet(), that.rationChapterTreeSetting);
  123. sheetCommonObj.setColumnWidthByRate($(`#${from}stdRationChapter`).width() - 40, rationChapterSpread, that.rationChapterTreeSetting.cols);
  124. rationChapterTree.loadDatas(datas);
  125. //读取展开收起状态
  126. let currentExpState = sessionStorage.getItem(from+'stdRationLibExpState');
  127. if(currentExpState){
  128. that[from+'tree'].setExpandedByState(that[from+'tree'].items, currentExpState);
  129. }
  130. else {
  131. //展开至第一层
  132. /* for(let root of that.tree.roots){
  133. root.setExpanded(false);
  134. that.tree.setRootExpanded(root.children, false);
  135. } */
  136. that[from+'tree'].setRootExpanded(that[from+'tree'].roots, false);
  137. }
  138. rationChapterTreeController.showTreeData();
  139. rationChapterSpread.getSheet(0).options.rowHeaderVisible = true;
  140. rationChapterTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
  141. rationLibObj.loadSectionRations(node && node.children.length === 0 ? node.getID() : null);
  142. });
  143. if (rationChapterTree.firstNode() && rationChapterTree.firstNode().length === 0) {
  144. rationLibObj.loadSectionRations(rationChapterTree.firstNode().getID());
  145. } else {
  146. rationLibObj.loadSectionRations(null);
  147. };
  148. };
  149. //type: 0-补充库 1-标准库
  150. CommonAjax.post('/complementaryRation/api/getRationTree', {userId: userID, rationRepId: rationLibID, type: rationLibObj.curLibType}, function (datas) {
  151. showRationChapterTree(datas);
  152. if(that.doAfterGetRationTree){
  153. that.doAfterGetRationTree();
  154. }
  155. $.bootstrapLoading.end();
  156. }, function () {
  157. showRationChapterTree([]);
  158. $.bootstrapLoading.end();
  159. });
  160. },
  161. //双击隐藏显示
  162. onChapterSpreadCellDoubleClick: function (sender, args) {
  163. let from = $('#divide_de').is(':visible')?"divide_":"";
  164. let me = rationLibObj;
  165. let node = me[from+'tree'].items[args.row];
  166. if (!node || node.children.length === 0)
  167. return;
  168. node.setExpanded(!node.expanded);
  169. sessionStorage.setItem(from+'stdRationLibExpState', me[from+'tree'].getExpState(me[from+'tree'].items));
  170. TREE_SHEET_HELPER.massOperationSheet(args.sheet, function () {
  171. let iCount = node.posterityCount(), i, child;
  172. for (i = 0; i < iCount; i++) {
  173. child = me[from+'tree'].items[args.row + i + 1];
  174. args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
  175. }
  176. args.sheet.invalidateLayout();
  177. });
  178. args.sheet.repaint();
  179. },
  180. setTagForHint: function (sheet, datas) {
  181. sheet.suspendPaint();
  182. sheet.suspendEvent();
  183. for(let i = 0, len = sheet.getRowCount(); i < len; i++){
  184. sheet.setTag(i, 0, '');
  185. }
  186. for(let i = 0, len = datas.length; i < len; i++){
  187. sheet.setTag(i, 0, datas[i].hint ? datas[i].hint : '');
  188. }
  189. sheet.resumePaint();
  190. sheet.resumeEvent();
  191. },
  192. loadSectionRations: function (sectionID) {
  193. let me = this;
  194. let from = $('#divide_de').is(':visible')?"divide_":"";
  195. var showDatas = function (datas, setting) {
  196. let rationSheet = rationLibObj[from+"sectionRationsSpread"].getActiveSheet();
  197. /*TREE_SHEET_HELPER.massOperationSheet(rationSheet, function () {
  198. rationSheet.setColumnWidth(0, 25, GC.Spread.Sheets.SheetArea.rowHeader);
  199. });*/
  200. SheetDataHelper.loadSheetHeader(setting, rationLibObj[from+"sectionRationsSpread"].getActiveSheet());
  201. SheetDataHelper.loadSheetData(setting, rationLibObj[from+"sectionRationsSpread"].getActiveSheet(), datas);
  202. rationLibObj.setTagForHint(rationSheet, datas);
  203. };
  204. //定额名称的处理:
  205. /*
  206. * 1、从定额库提取的名称,是否含有空格:
  207. * 1.1、无,则不处理。
  208. * 1.2、有,则取第一个空格前的文本,与定额所属节点名称(去掉前面和后面的编号、括号、空格,保留中间的中文及符号)比较是否相同:
  209. * 1.2.1、不同,则不处理。
  210. * 1.2.2、相同,则将定额名称显示为去除第一个空格及空格之前的文本。
  211. */
  212. //@param {String}sectionName(章节名称) {Array}datas(定额数据)
  213. function simplifyName(sectionName, datas){
  214. if (!sectionName || !datas || datas.length === 0) {
  215. return;
  216. }
  217. //提取需要匹配的章节名称
  218. //去掉前缀
  219. let toMatchArr = sectionName.split(' '),
  220. toMatchStr = toMatchArr[toMatchArr.length - 1];
  221. //去掉后缀
  222. let sectionReg = /\(\w{9,}\)/g,
  223. regMatch = toMatchStr.match(sectionReg);
  224. if (regMatch) {
  225. toMatchStr = toMatchStr.replace(regMatch[regMatch.length - 1], '');
  226. }
  227. //简化匹配到的定额名称
  228. for (let data of datas) {
  229. if (!data.name) {
  230. continue;
  231. }
  232. //第一个空格前的字符串去进行匹配,没有则不匹配
  233. let nameArr = data.name.split(' ');
  234. if (nameArr.length <= 1) {
  235. continue;
  236. }
  237. let matchName = nameArr[0];
  238. if (matchName === toMatchStr) {
  239. nameArr.shift();
  240. data.name = nameArr.join(' ');
  241. }
  242. }
  243. }
  244. if (sectionID) {
  245. CommonAjax.post('/complementaryRation/api/getRationGljItemsBySection', {user_Id: userID, sectionId: sectionID, type: me.curLibType}, function (datas) {
  246. let chapterSheet = me[from+'rationChapterSpread'].getActiveSheet();
  247. let sectionName = chapterSheet.getText(chapterSheet.getActiveRowIndex(), chapterSheet.getActiveColumnIndex());
  248. simplifyName(sectionName, datas);
  249. showDatas(datas, rationLibObj.sectionRationsSetting);
  250. if(me.doAfterLoadGetRations){
  251. me.doAfterLoadGetRations(datas);
  252. me.doAfterLoadGetRations = null;
  253. }
  254. }, function () {
  255. showDatas([], rationLibObj.sectionRationsSetting);
  256. });
  257. } else {
  258. showDatas([], rationLibObj.sectionRationsSetting);
  259. }
  260. },
  261. onRationSpreadCellDoubleClick: function (sender, args) {
  262. let pre = $('#divide_de').is(':visible')?"divide_":"";
  263. var select = $(`#${pre}stdRationLibSelect`), rationCode = args.sheet.getText(args.row, 0);
  264. if (rationCode !== '') {
  265. let query = {userID: userID, rationRepId: select.val(), code: rationCode};
  266. //搜索结果全部定额中双击添加定额、有可能同名不同库,更新查询的库ID
  267. if (rationLibObj.resultCache && rationLibObj.resultCache[args.row]) {
  268. query.rationRepId = rationLibObj.resultCache[args.row].type === 'std' ? rationLibObj.resultCache[args.row].rationRepId : rationLibObj.compleRationLibId;
  269. }
  270. if(pre =="divide_"){
  271. divideObj.addDivideRation(select.val(),rationCode);
  272. }else{
  273. projectObj.project.Ration.addNewRation(query,rationType.ration, function () {
  274. projectObj.setActiveCell('quantity', true);
  275. });
  276. }
  277. }
  278. },
  279. //滚动条到底部加载
  280. onRationSpreadTopRowChanged: function (sender, args) {
  281. let me = rationLibObj;
  282. if(me.searching) {
  283. return;
  284. }
  285. let bottomRow = args.sheet.getViewportBottomRow(1),
  286. rowCount = args.sheet.getRowCount();
  287. //滚到了底部
  288. if (bottomRow + 1 - me.sectionRationsSetting.emptyRows === rowCount - me.sectionRationsSetting.emptyRows) {
  289. seachRation();
  290. }
  291. },
  292. loadStdRationContextMenu: function () {
  293. let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet(), rationModel = projectObj.project.Ration;;
  294. $.contextMenu({
  295. selector: '#stdSectionRations',
  296. build: function ($trigger, e) {
  297. let target = SheetDataHelper.safeRightClickSelection($trigger, e, rationSpread);
  298. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  299. },
  300. items: {
  301. "insertStdRation": {
  302. name: "插入定额",
  303. icon: 'fa-sign-in',
  304. disabled: function () {
  305. return projectReadOnly;
  306. },
  307. callback: function (key, opt) {
  308. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  309. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  310. if (rationCode !== '') {
  311. rationModel.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  312. });
  313. }
  314. }
  315. },
  316. "replaceStdRation": {
  317. name: "替换定额",
  318. icon: 'fa-sign-in',
  319. disabled: function () {
  320. return projectReadOnly
  321. },
  322. callback: function (key, opt) {
  323. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  324. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  325. let mainTreeSelected = projectObj.project.mainTree.selected;
  326. if (rationCode !== ''&&mainTreeSelected&&mainTreeSelected.sourceType == rationModel.getSourceType()) {
  327. rationModel.updateRationCodes([{'node':mainTreeSelected,value:rationCode}]);
  328. }
  329. }
  330. },
  331. }
  332. });
  333. },
  334. expandSearchNodes: function(nodes){
  335. let that = rationLibObj;
  336. TREE_SHEET_HELPER.massOperationSheet(that.rationChapterSpread.getActiveSheet(), function () {
  337. function expParentNode(node){
  338. if(node.parent){
  339. expParentNode(node.parent);
  340. if(!node.parent.expanded){
  341. node.parent.setExpanded(true);
  342. }
  343. }
  344. }
  345. for(let node of nodes){
  346. expParentNode(node);
  347. }
  348. TREE_SHEET_HELPER.refreshTreeNodeData(that.rationChapterTreeSetting, that.rationChapterSpread.getActiveSheet(), that.tree.roots, true);
  349. TREE_SHEET_HELPER.refreshNodesVisible(that.tree.roots, that.rationChapterSpread.getActiveSheet(), true);
  350. });
  351. },
  352. initSel: function (row) {
  353. let me = this;
  354. let sheet = me.rationChapterSpread.getActiveSheet();
  355. sheet.setActiveCell(row, 0);
  356. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  357. let sectionNode = me.tree.items[row] || null;
  358. me.loadSectionRations(sectionNode && sectionNode.children.length === 0 ? sectionNode.data.ID : null);
  359. },
  360. locateAtRation: function(libID, code){
  361. if(!isDef(libID)) return;
  362. let me = rationLibObj;
  363. if ($('#rationSearchResult').is(':visible')) {
  364. $('#rationSearchResult a').click();
  365. }
  366. //查找定额,以确定定额所在章节节点
  367. let firstLibID = projectObj.project.projectInfo.engineeringInfo.ration_lib.length > 0 ?
  368. projectObj.project.projectInfo.engineeringInfo.ration_lib[0].id : null;
  369. let locateRow = 0,
  370. locateSubRow = 0;
  371. CommonAjax.post('/complementaryRation/api/getRationItem', {rationRepIds: [libID], code: code}, function (ration) {
  372. if(ration && ration.sectionId){
  373. let sectionNode = me.tree.findNode(ration.sectionId);
  374. if(sectionNode){
  375. me.expandSearchNodes([sectionNode]);
  376. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  377. }
  378. locateRow = sectionNode.serialNo();
  379. me.doAfterLoadGetRations = function (rationItems) {
  380. let rationSheet = me.sectionRationsSpread.getActiveSheet();
  381. locateSubRow = _.findIndex(rationItems, {ID: ration.ID});
  382. rationSheet.setActiveCell(locateSubRow, 0);
  383. rationSheet.showRow(locateSubRow, GC.Spread.Sheets.VerticalPosition.center);
  384. };
  385. me.initSel(locateRow);
  386. }
  387. else {
  388. me.initSel(locateRow);
  389. }
  390. }, function () {
  391. me.initSel(locateRow);
  392. });
  393. },
  394. rationChapterTreeSetting: {
  395. "emptyRowHeader": true,
  396. "rowHeaderWidth": 1,
  397. "emptyRows":0,
  398. "headRows":1,
  399. "headRowHeight":[30],
  400. "defaultRowHeight": 21,
  401. "treeCol": 0,
  402. "cols":[{
  403. "rateWidth": 1,
  404. "width":400,
  405. "readOnly": true,
  406. "head":{
  407. "titleNames":["名称"],
  408. "spanCols":[1],
  409. "spanRows":[1],
  410. "vAlign":[1],
  411. "hAlign":[1],
  412. "font":["Arial"]
  413. },
  414. "data":{
  415. "field":"name",
  416. "vAlign":1,
  417. "hAlign":0,
  418. "font":"Arial"
  419. }
  420. }]
  421. },
  422. sectionRationsSetting: {
  423. "emptyRowHeader": true,
  424. "rowHeaderWidth": 1,
  425. "emptyRows":3,
  426. "headRows":1,
  427. "headRowHeight":[20],
  428. "defaultRowHeight": 21,
  429. "cols":[{
  430. "width":60,
  431. "readOnly": true,
  432. "showHint": true,
  433. "head":{
  434. "titleNames":["编码"],
  435. "spanCols":[1],
  436. "spanRows":[1],
  437. "vAlign":[1],
  438. "hAlign":[1],
  439. "font":["Arial"]
  440. },
  441. "data":{
  442. "field":"code",
  443. "vAlign":1,
  444. "hAlign":0,
  445. "font":"Arial"
  446. }
  447. }, {
  448. "width":220,
  449. "readOnly": true,
  450. "showHint": true,
  451. "head":{
  452. "titleNames":["名称"],
  453. "spanCols":[1],
  454. "spanRows":[1],
  455. "vAlign":[1],
  456. "hAlign":[1],
  457. "font":["Arial"]
  458. },
  459. "data":{
  460. "field":"name",
  461. "vAlign":1,
  462. "hAlign":0,
  463. "font":"Arial"
  464. }
  465. }, {
  466. "width":55,
  467. "readOnly":true,
  468. "head":{
  469. "titleNames":["单位"],
  470. "spanCols":[1],
  471. "spanRows":[1],
  472. "vAlign":[1],
  473. "hAlign":[1],
  474. "font":["Arial"]
  475. },
  476. "data":{
  477. "field":"unit",
  478. "vAlign":1,
  479. "hAlign":1,
  480. "font":"Arial"
  481. }
  482. }, {
  483. "width":60,
  484. "readOnly":true,
  485. "head":{
  486. "titleNames":["基价"],
  487. "spanCols":[1],
  488. "spanRows":[1],
  489. "vAlign":[1],
  490. "hAlign":[1],
  491. "font":["Arial"]
  492. },
  493. "data":{
  494. "field":"basePrice",
  495. "vAlign":1,
  496. "hAlign":2,
  497. "font":"Arial"
  498. }
  499. }]
  500. },
  501. getStdRationLibIDs: function () {
  502. let ids = [];
  503. if(projectObj.project.projectInfo.engineeringInfo.ration_lib.length === 0){
  504. alert('当前项目无定额库,请添加定额库。');
  505. return null;
  506. }
  507. for(let rationLib of projectObj.project.projectInfo.engineeringInfo.ration_lib){
  508. ids.push(rationLib.id);
  509. }
  510. return ids;
  511. },
  512. getCurrentStdRationLibID:function () {
  513. if(projectObj.project.projectInfo.engineeringInfo.ration_lib.length === 0){
  514. alert('当前项目无定额库,请添加定额库。');
  515. return null;
  516. }
  517. let pre = $('#divide_de').is(':visible')?"divide_":"";
  518. if($(`#${pre}stdRationLibSelect`).val()){
  519. return parseInt($(`#${pre}stdRationLibSelect`).val());
  520. }else {
  521. return projectObj.project.projectInfo.engineeringInfo.ration_lib[0].id;
  522. }
  523. },
  524. getFirstStdRationLibID: function () {
  525. if(projectObj.project.projectInfo.engineeringInfo.ration_lib.length === 0){
  526. alert('当前项目无定额库,请添加定额库。');
  527. return null;
  528. }
  529. return parseInt(projectObj.project.projectInfo.engineeringInfo.ration_lib[0].id);
  530. },
  531. getDefaultStdRationLibID:function(){
  532. let ration_lib = projectObj.project.projectInfo.engineeringInfo.ration_lib;
  533. if(ration_lib.length === 0){
  534. alert('当前项目无定额库,请添加定额库。');
  535. return null;
  536. }
  537. let defaultLib = _.find(ration_lib,{'isDefault':true});
  538. let libID = defaultLib?defaultLib.id:ration_lib[0].id;
  539. return parseInt(libID);
  540. },
  541. //@param {Array}datas(resultCache) @return {Object}
  542. //搜索skip信息,不能被每页搜索数整除,则说明上次搜索已经搜索完整
  543. getSearchSkip: function (datas) {
  544. if (datas.length % this.searchLimit !== 0) {
  545. return null;
  546. }
  547. let skip = {std: 0, comple: 0};
  548. if (!datas || !Array.isArray(datas) || datas.length === 0) {
  549. return skip;
  550. }
  551. for (let data of datas) {
  552. if (data.type === 'std') {
  553. skip.std++;
  554. } else {
  555. skip.comple++;
  556. }
  557. }
  558. return skip;
  559. }
  560. };
  561. addEventOnResize(rationLibObj.refreshSettingForHint);
  562. //赋初始高度
  563. if($('#stdRationChapter').height() === 0 || $('#stdSectionRations').height() === 0){
  564. $('#stdRationChapter').height($(window).height()-$(".header").height()-$(".toolsbar").height()-$(".tools-bar-height-q").height()-312);
  565. $('#stdSectionRations').height(270);
  566. }
  567. $('#stdRationTab').bind('click', function () {
  568. var select = $('#stdRationLibSelect');
  569. rationLibObj.checkSpread();
  570. if (select[0].options.length === 0) {
  571. rationLibObj.loadStdRationLibs();
  572. rationLibObj.loadStdRationContextMenu();
  573. };
  574. });
  575. $('#stdRationLibSelect').change(function () {
  576. var select = $(this);
  577. if (this.children.length !== 0) {
  578. let rationLibId = select.val();
  579. sessionStorage.setItem('stdRationLib', rationLibId);
  580. sessionStorage.removeItem('stdRationLibExpState');
  581. rationLibObj.loadStdRation(rationLibId);
  582. }
  583. });
  584. //回车键搜索
  585. $('#rationSearchKeyword').bind('keypress', function (event) {
  586. if(event.keyCode === 13){
  587. $(this).blur();
  588. $('#rationSearch').click();
  589. }
  590. });
  591. $('#rationSearchKeyword').keyup(function () {
  592. let keyword = $('#rationSearchKeyword').val();
  593. if(keyword === ''){
  594. if($('#rationSearchResult').is(':visible')){
  595. rationLibObj.resultCache = [];
  596. $('#rationSearchResult').hide();
  597. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  598. switchRationSearchMode(0);
  599. autoFlashHeight();
  600. rationLibObj.refreshSpread();
  601. }
  602. }
  603. });
  604. //变换搜索本定额、全部定额状态
  605. function switchRationSearchMode(mode) {
  606. rationLibObj.resultCache = [];
  607. //搜索本定额
  608. if(mode === 0){
  609. $('#curRationLib').removeClass('btn-light');
  610. $('#curRationLib').addClass('btn-secondary');
  611. $('#allRationLibs').removeClass('btn-secondary');
  612. $('#allRationLibs').addClass('btn-light');
  613. } else {//搜索全部定额
  614. $('#allRationLibs').removeClass('btn-light');
  615. $('#allRationLibs').addClass('btn-secondary');
  616. $('#curRationLib').removeClass('btn-secondary');
  617. $('#curRationLib').addClass('btn-light');
  618. }
  619. }
  620. //搜索本定额
  621. $('#curRationLib').click(function () {
  622. if($(this).hasClass('btn-secondary')){
  623. return;
  624. }
  625. switchRationSearchMode(0);
  626. $('#rationSearch').click();
  627. });
  628. //搜索全部定额
  629. $('#allRationLibs').click(function () {
  630. if($(this).hasClass('btn-secondary')){
  631. return;
  632. }
  633. switchRationSearchMode(1);
  634. $('#rationSearch').click();
  635. });
  636. //搜索
  637. function seachRation(){
  638. let skip = rationLibObj.getSearchSkip(rationLibObj.resultCache);
  639. if (!skip) {
  640. return;
  641. }
  642. rationLibObj.searching = true;
  643. var keyword = $('#rationSearchKeyword').val();
  644. if(keyword === ''){
  645. if($('#rationSearchResult').is(':visible')){
  646. rationLibObj.resultCache = [];
  647. $('#rationSearchResult').hide();
  648. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  649. autoFlashHeight();
  650. rationLibObj.refreshSpread();
  651. }
  652. return;
  653. }
  654. //获取搜索定额的库:本库/所有库
  655. let rationLibIDs = [];
  656. if($('#curRationLib').hasClass('btn-secondary')){
  657. rationLibIDs.push($('#stdRationLibSelect').val());
  658. } else {
  659. for(let lib of projectObj.project.projectInfo.engineeringInfo.ration_lib){
  660. rationLibIDs.push(lib.id);
  661. }
  662. }
  663. let bindContextmenuOpr = function (sheet) {
  664. $.contextMenu({
  665. selector: '#rationSearchResult',
  666. build: function($triggerElement, e){
  667. //控制允许右键菜单在哪个位置出现
  668. let offset = $('.main-data-side-search').offset(),
  669. x = e.pageX - offset.left,
  670. y = e.pageY - offset.top;
  671. let target = sheet.hitTest(x, y);
  672. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  673. sheet.setActiveCell(target.row, target.col);
  674. return {
  675. callback: function(){},
  676. items: {
  677. "locate": {
  678. name: "定位至章节",
  679. disabled: function () {
  680. return target.row >= rationLibObj.resultCache.length;
  681. },
  682. icon: "fa-arrow-left",
  683. callback: function (key, opt) {
  684. let data = rationLibObj.resultCache[target.row],
  685. libId = data.rationRepId ? data.rationRepId : rationLibObj.compleRationLibId;
  686. $('#rationSearchResult').hide();
  687. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  688. autoFlashHeight();
  689. rationLibObj.refreshSpread();
  690. switchRationSearchMode(0);
  691. if($('#stdRationLibSelect').select().val() != libId){
  692. let libOpts = $('#stdRationLibSelect').find('option');
  693. for(let libOpt of libOpts){
  694. if($(libOpt).val() == libId){
  695. $(libOpt).prop('selected', 'selected');
  696. break;
  697. }
  698. }
  699. $('#stdRationLibSelect').change();
  700. rationLibObj.doAfterGetRationTree = function () {
  701. this.locateAtRation(libId, data.code);
  702. this.doAfterGetRationTree = null;
  703. };
  704. } else {
  705. rationLibObj.locateAtRation(libId, data.code);
  706. }
  707. }}
  708. }
  709. };
  710. }
  711. else{
  712. return false;
  713. }
  714. }
  715. });
  716. };
  717. var showResult = function (result) {
  718. if(!rationLibObj.resultSpread){
  719. let resultSpread = SheetDataHelper.createNewSpread($('.main-data-side-search')[0]);
  720. rationLibObj.resultSpread = resultSpread;
  721. bindContextmenuOpr(resultSpread.getActiveSheet());
  722. SheetDataHelper.loadSheetHeader(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet());
  723. if (!projectReadOnly) {
  724. resultSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, rationLibObj.onRationSpreadCellDoubleClick);
  725. }
  726. resultSpread.bind(GC.Spread.Sheets.Events.TopRowChanged, rationLibObj.onRationSpreadTopRowChanged);
  727. }else {
  728. rationLibObj.resultSpread.refresh();
  729. }
  730. SheetDataHelper.loadSheetData(rationLibObj.sectionRationsSetting, rationLibObj.resultSpread.getActiveSheet(), result);
  731. rationLibObj.setTagForHint(rationLibObj.resultSpread.getActiveSheet(), result);
  732. rationLibObj.resultCache = result;
  733. };
  734. $.bootstrapLoading.start();
  735. CommonAjax.post('/complementaryRation/api/findRation', {'user_id': userID, 'rationRepId': rationLibIDs, 'keyword': keyword, skip: skip}, function (result) {
  736. var resultObj = $('#rationSearchResult');
  737. if (result.count !== null) {
  738. $('#rationSearchCount').text(`搜索结果:${result.count}`);
  739. }
  740. $('a', resultObj).unbind('click');
  741. $('a', resultObj).bind('click', function () {
  742. rationLibObj.resultCache = [];
  743. switchRationSearchMode(0);
  744. resultObj.hide();
  745. $(".main-data-side-search", resultObj).height(0);
  746. autoFlashHeight();
  747. loadSideToolsHeight();
  748. });
  749. resultObj.show();
  750. $(".main-data-side-search", resultObj).height($(window).height() - $(".header").height() - $(".toolsbar").height() - 64);
  751. showResult(rationLibObj.resultCache.concat(result.data));
  752. rationLibObj.searching = false;
  753. //以防一开始就需要加载后面的分页数据
  754. if (result.data.length > 0) {
  755. rationLibObj.onRationSpreadTopRowChanged({}, {sheet: rationLibObj.resultSpread.getActiveSheet()});
  756. }
  757. $.bootstrapLoading.end();
  758. }, function () {
  759. rationLibObj.searching = false;
  760. $.bootstrapLoading.end();
  761. });
  762. }
  763. $('#rationSearch').click(function () {
  764. rationLibObj.resultCache = [];
  765. seachRation();
  766. });