std_ration_lib.js 32 KB

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