std_ration_lib.js 34 KB

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