coe.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /**
  2. * Created by CSL on 2017-05-18.
  3. */
  4. //modiyied by zhong on 2017/9/21
  5. function refreshALlWorkBook() {
  6. if (sectionTreeObj.workBook) {
  7. sectionTreeObj.workBook.refresh();
  8. }
  9. if (rationOprObj.workBook) {
  10. rationOprObj.workBook.refresh();
  11. }
  12. if (rationGLJOprObj.sheet && rationGLJOprObj.sheet.getParent()) {
  13. rationGLJOprObj.sheet.getParent().refresh();
  14. }
  15. if (coeOprObj.workBook) {
  16. coeOprObj.workBook.refresh();
  17. }
  18. if (gljAdjOprObj.workBook) {
  19. gljAdjOprObj.workBook.refresh();
  20. }
  21. if (typeof lossObj !== "undefined" && lossObj.workBook) {
  22. lossObj.workBook.refresh();
  23. }
  24. }
  25. $(document).ready(function () {
  26. //定额章节树与定额表
  27. let leftElesObj = {};
  28. leftElesObj.module = moduleName;
  29. leftElesObj.resize = $("#slideResizeLeft");
  30. leftElesObj.parent = $("#dataRow");
  31. leftElesObj.left = $("#leftContent");
  32. leftElesObj.right = $("#mainContent");
  33. let maxEval = `$('#rightContent').is(':visible') ? $('#dataRow').width() - $('#rightContent').width() - 300 : $('#dataRow').width() - 300`;
  34. SlideResize.horizontalSlide(
  35. leftElesObj,
  36. { min: 300, max: maxEval },
  37. function () {
  38. refreshALlWorkBook();
  39. sectionTreeObj.loadRateWidth();
  40. }
  41. );
  42. SlideResize.loadHorizonWidth(
  43. moduleName,
  44. [$("#slideResizeLeft")],
  45. [$("#leftContent"), $("#mainContent")],
  46. function () {
  47. //refreshAfterZmhs(false);
  48. let leftContentWidth = parseFloat(
  49. $("#leftContent")[0].style.width.replace("%", "")
  50. ),
  51. mainContentWidth = parseFloat(
  52. $("#mainContent")[0].style.width.replace("%", "")
  53. );
  54. let surplus = 100 - leftContentWidth - mainContentWidth;
  55. $("#leftContent").css("width", `${leftContentWidth + surplus / 2}%`);
  56. $("#mainContent").css("width", `${mainContentWidth + surplus / 2}%`);
  57. refreshALlWorkBook();
  58. }
  59. );
  60. //定额表与子目换算表
  61. let rightElesObj = {};
  62. rightElesObj.module = moduleName;
  63. rightElesObj.resize = $("#slideResizeRight");
  64. rightElesObj.parent = $("#dataRow");
  65. rightElesObj.left = $("#mainContent");
  66. rightElesObj.right = $("#rightContent");
  67. let maxEvalRight = `$('#dataRow').width() - $('#leftContent').width() - 200`;
  68. SlideResize.horizontalSlide(
  69. rightElesObj,
  70. { min: 200, max: maxEvalRight },
  71. function () {
  72. refreshALlWorkBook();
  73. }
  74. );
  75. //设置水平拖动条的宽度
  76. //@param {Object dom}resize滚动条
  77. function setResizeWidth(resize) {
  78. const fixedWidth = 10;
  79. //跟滚动条同层的其他节点
  80. let bros = resize.parent().children();
  81. //滚动条节点 及 同层非滚动条节点的索引
  82. let index = bros.index(resize),
  83. otherIndex = index ? 0 : 1;
  84. const other = resize.parent().children(`:eq(${otherIndex})`);
  85. let resizeParentWidth = resize.parent().width();
  86. let resizeDecimalWidth = fixedWidth / resizeParentWidth,
  87. otherDecimalWidth = 1 - resizeDecimalWidth;
  88. let resizePercentWidth = resizeDecimalWidth * 100 + "%",
  89. otherPercentWidth = otherDecimalWidth * 100 + "%";
  90. resize.css("width", resizePercentWidth);
  91. other.css("width", otherPercentWidth);
  92. }
  93. function refreshAfterZmhs(visible) {
  94. const min = 20;
  95. //宽度比例localstorage key
  96. let leftContentKey = `${moduleName}${$("#leftContent").attr("id")}Width`,
  97. mainContentKey = `${moduleName}${$("#mainContent").attr("id")}Width`,
  98. zmhsContentKey = `${moduleName}${$("#rightContent").attr("id")}Width`;
  99. let zmhsWidth = getLocalCache(zmhsContentKey)
  100. ? getLocalCache(zmhsContentKey)
  101. : $("#rightContent")[0].style.width,
  102. mainContentWidth = $("#mainContent")[0].style.width,
  103. leftContentWidth;
  104. zmhsWidth = parseFloat(zmhsWidth.replace("%", ""));
  105. mainContentWidth = parseFloat(mainContentWidth.replace("%", ""));
  106. if (visible) {
  107. mainContentWidth =
  108. mainContentWidth - zmhsWidth / 2 < min
  109. ? min
  110. : mainContentWidth - zmhsWidth / 2;
  111. if (100 - mainContentWidth - zmhsWidth < min) {
  112. leftContentWidth = min;
  113. zmhsWidth = 100 - mainContentWidth - leftContentWidth;
  114. } else {
  115. leftContentWidth = 100 - mainContentWidth - zmhsWidth;
  116. }
  117. } else {
  118. mainContentWidth += zmhsWidth / 2;
  119. leftContentWidth = 100 - mainContentWidth;
  120. }
  121. $("#leftContent").css("width", `${leftContentWidth}%`);
  122. setLocalCache(leftContentKey, `${leftContentWidth}%`);
  123. $("#mainContent").css("width", `${mainContentWidth}%`);
  124. setLocalCache(mainContentKey, `${mainContentWidth}%`);
  125. $("#rightContent").css("width", `${zmhsWidth}%`);
  126. setLocalCache(zmhsContentKey, `${zmhsWidth}%`);
  127. let resizes = [$("#slideResizeLeft"), $("#slideResizeRight")];
  128. for (let resize of resizes) {
  129. setResizeWidth(resize);
  130. }
  131. sectionTreeObj.loadRateWidth();
  132. }
  133. $("#zmhs").click(function () {
  134. if (!$(this).hasClass("active")) {
  135. $("#loss").removeClass("active");
  136. $(this).addClass("active");
  137. refreshAfterZmhs(true);
  138. $("#zmhsWrap").show();
  139. $("#lossWrap").hide();
  140. $("#rightContent").show();
  141. if (!coeOprObj.workBook) {
  142. pageObj.initPage();
  143. }
  144. refreshALlWorkBook();
  145. } else {
  146. $(this).removeClass("active");
  147. refreshAfterZmhs(false);
  148. $("#rightContent").hide();
  149. $("#zmhsWrap").hide();
  150. $("#lossWrap").hide();
  151. refreshALlWorkBook();
  152. }
  153. });
  154. //子目换算和调整表上下拖动
  155. let zmhsAdjResize = getZmhsAdjResize();
  156. SlideResize.verticalSlide(
  157. zmhsAdjResize.eleObj,
  158. zmhsAdjResize.limit,
  159. function () {
  160. if (coeOprObj.workBook) {
  161. coeOprObj.workBook.refresh();
  162. }
  163. if (gljAdjOprObj.workBook) {
  164. gljAdjOprObj.workBook.refresh();
  165. }
  166. }
  167. );
  168. loadZmhsAdjSize(zmhsAdjResize);
  169. });
  170. function getZmhsAdjResize() {
  171. let zmhsAdjResize = {};
  172. zmhsAdjResize.eleObj = {
  173. module: moduleName,
  174. resize: $("#zmhsAdjResize"),
  175. top: $("#mainSpread"),
  176. topSpread: $("#mainSpread"),
  177. bottom: $("#contentSpread"),
  178. bottomSpread: $("#contentSpread"),
  179. };
  180. zmhsAdjResize.limit = {
  181. min: 150,
  182. max: `$(window).height()-$('.header').height()-150-verticalResize`,
  183. totalHeight: `$(window).height()-$('.header').height()-verticalResize`,
  184. notTopSpread: 0,
  185. notBottomSpread: 0,
  186. };
  187. return zmhsAdjResize;
  188. }
  189. function loadZmhsAdjSize(resizeObj) {
  190. if (!resizeObj) {
  191. resizeObj = getZmhsAdjResize();
  192. }
  193. SlideResize.loadVerticalHeight(
  194. resizeObj.eleObj.module,
  195. resizeObj.eleObj,
  196. resizeObj.limit,
  197. function () {
  198. if (coeOprObj.workBook) {
  199. coeOprObj.workBook.refresh();
  200. }
  201. if (gljAdjOprObj.workBook) {
  202. gljAdjOprObj.workBook.refresh();
  203. }
  204. }
  205. );
  206. }
  207. var pageObj = {
  208. initPage: function () {
  209. coeOprObj.buildSheet($("#mainSpread")[0]);
  210. gljAdjOprObj.buildSheet($("#contentSpread")[0]);
  211. coeOprObj.getCoeList();
  212. gljAdjOprObj.getGljItemsOcc();
  213. lockUtil.lockSpreads([coeOprObj.workBook, gljAdjOprObj.workBook], locked);
  214. },
  215. showData: function (sheet, setting, data) {
  216. let me = pageObj,
  217. ch = GC.Spread.Sheets.SheetArea.viewport;
  218. sheet.suspendPaint();
  219. sheet.suspendEvent();
  220. sheet.clear(
  221. 0,
  222. 0,
  223. sheet.getRowCount(),
  224. sheet.getColumnCount(),
  225. GC.Spread.Sheets.SheetArea.viewport,
  226. GC.Spread.Sheets.StorageType.data
  227. );
  228. sheet.setRowCount(data.length + 3);
  229. for (let col = 0; col < setting.header.length; col++) {
  230. var hAlign = "left",
  231. vAlign = "center";
  232. if (setting.header[col].hAlign) {
  233. hAlign = setting.header[col].hAlign;
  234. } else if (setting.header[col].dataType !== "String") {
  235. hAlign = "right";
  236. }
  237. if (setting.header[col].readOnly) {
  238. sheet.getRange(-1, col, -1, 1).locked(true);
  239. } else {
  240. sheet.getRange(-1, col, -1, 1).locked(false);
  241. }
  242. vAlign = setting.header[col].vAlign ? setting.header[col].vAlign : vAlign;
  243. sheetCommonObj.setAreaAlign(
  244. sheet.getRange(-1, col, -1, 1),
  245. hAlign,
  246. vAlign
  247. );
  248. if (setting.header[col].formatter) {
  249. sheet.setFormatter(
  250. -1,
  251. col,
  252. setting.header[col].formatter,
  253. GC.Spread.Sheets.SheetArea.viewport
  254. );
  255. }
  256. for (let row = 0; row < data.length; row++) {
  257. let val = data[row][setting.header[col].dataCode];
  258. sheet.setValue(row, col, val, ch);
  259. }
  260. }
  261. sheet.resumeEvent();
  262. sheet.resumePaint();
  263. },
  264. };
  265. let coeOprObj = {
  266. workBook: null,
  267. workSheet: null,
  268. currentCoeList: [],
  269. currentCoe: null,
  270. currentMaxNo: null,
  271. setting: {
  272. header: [
  273. {
  274. headerName: "编号",
  275. headerWidth: 50,
  276. dataCode: "serialNo",
  277. dataType: "String",
  278. hAlign: "center",
  279. vAlign: "center",
  280. readOnly: false,
  281. },
  282. {
  283. headerName: "名称",
  284. headerWidth: 200,
  285. dataCode: "name",
  286. dataType: "String",
  287. hAlign: "left",
  288. vAlign: "center",
  289. readOnly: false,
  290. },
  291. {
  292. headerName: "内容",
  293. headerWidth: 150,
  294. dataCode: "content",
  295. dataType: "String",
  296. hAlign: "left",
  297. vAlign: "center",
  298. readOnly: false,
  299. },
  300. {
  301. headerName: "原人材机编码",
  302. headerWidth: 90,
  303. dataCode: "original_code",
  304. dataType: "String",
  305. hAlign: "center",
  306. vAlign: "center",
  307. readOnly: false,
  308. },
  309. {
  310. headerName: "可选人材机编码",
  311. headerWidth: 150,
  312. dataCode: "option_codes",
  313. dataType: "String",
  314. hAlign: "center",
  315. vAlign: "center",
  316. readOnly: false,
  317. },
  318. ],
  319. },
  320. buildSheet: function (container) {
  321. let me = coeOprObj;
  322. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  323. sheetCommonObj.bindEscKey(me.workBook, [
  324. {
  325. sheet: me.workBook.getSheet(0),
  326. editStarting: null,
  327. editEnded: me.onEditEnded,
  328. },
  329. ]);
  330. me.workSheet = me.workBook.getSheet(0);
  331. me.workSheet.options.isProtected = true;
  332. me.onDelOpr(me.workBook, me.setting);
  333. me.initContextMenu();
  334. me.workSheet.bind(
  335. GC.Spread.Sheets.Events.SelectionChanged,
  336. me.onSelectionChanged
  337. );
  338. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  339. me.workBook.bind(
  340. GC.Spread.Sheets.Events.ClipboardPasting,
  341. me.onClipboardPasting
  342. );
  343. me.workBook.bind(
  344. GC.Spread.Sheets.Events.ClipboardPasted,
  345. me.onClipboardPasted
  346. );
  347. },
  348. onSelectionChanged: function (sender, info) {
  349. if (
  350. (info.oldSelections.length === 0 && info.newSelections.length > 0) ||
  351. info.oldSelections[0].row !== info.newSelections[0].row
  352. ) {
  353. const row = info.newSelections[0].row;
  354. coeOprObj.coeSelInit(row);
  355. }
  356. },
  357. coeSelInit: function (row) {
  358. const me = coeOprObj;
  359. const adj = gljAdjOprObj;
  360. if (row < me.currentCoeList.length) {
  361. me.currentCoe = me.currentCoeList[row];
  362. adj.currentGljAdjList = me.currentCoe.coes;
  363. adj.buildDynamicComboBox(adj.workSheet);
  364. } else {
  365. me.currentCoe = null;
  366. adj.currentGljAdjList = [];
  367. adj.buildBaseCell(adj.workSheet);
  368. }
  369. //refresh & show coes
  370. sheetCommonObj.cleanSheet(adj.workSheet, adj.setting, -1);
  371. me.workBook.focus(true);
  372. adj.show(adj.currentGljAdjList);
  373. },
  374. onEditEnded: function (sender, args) {
  375. let me = coeOprObj,
  376. addArr = [],
  377. updateArr = [],
  378. dataCode = me.setting.header[args.col].dataCode;
  379. if (args.editingText && args.editingText.toString().trim().length > 0) {
  380. let inputT = args.editingText.toString().trim();
  381. //update
  382. if (args.row < me.currentCoeList.length) {
  383. let updateObj = me.currentCoeList[args.row];
  384. if (updateObj[dataCode] != inputT) {
  385. if (dataCode === "serialNo") {
  386. if (me.isInt(inputT) && !me.hasTisNo(me.currentCoeList, inputT)) {
  387. me.currentMaxNo =
  388. me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  389. updateObj[dataCode] = inputT;
  390. updateArr.push(updateObj);
  391. me.save([], updateArr, [], true);
  392. } else if (!me.isInt(inputT)) {
  393. alert("编号只能为整数!");
  394. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + "");
  395. } else if (me.hasTisNo(me.currentCoeList, inputT)) {
  396. alert("该编号已存在!");
  397. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + "");
  398. }
  399. } else {
  400. updateObj[dataCode] = inputT;
  401. me.setOptionList(dataCode, inputT, updateObj);
  402. updateArr.push(updateObj);
  403. me.save([], updateArr, [], true);
  404. }
  405. }
  406. }
  407. //insert
  408. else {
  409. let newCoe = {};
  410. newCoe.libID = pageOprObj.rationLibId;
  411. if (dataCode === "serialNo") {
  412. if (me.isInt(inputT) && !me.hasTisNo(me.currentCoeList, inputT)) {
  413. me.currentMaxNo =
  414. me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  415. newCoe[dataCode] = inputT;
  416. addArr.push(newCoe);
  417. me.save(addArr, [], [], true, function (result) {
  418. me.updateCurrentCoeList(result);
  419. });
  420. } else if (!me.isInt(inputT)) {
  421. args.sheet.setValue(args.row, args.col, "");
  422. alert("编号只能为整数!");
  423. } else if (me.hasTisNo(me.currentCoeList, inputT)) {
  424. args.sheet.setValue(args.row, args.col, "");
  425. alert("该编号已存在!");
  426. }
  427. } else {
  428. newCoe.serialNo = ++me.currentMaxNo;
  429. newCoe[dataCode] = inputT;
  430. me.setOptionList(dataCode, inputT, newCoe);
  431. addArr.push(newCoe);
  432. me.save(addArr, [], [], true, function (result) {
  433. me.updateCurrentCoeList(result);
  434. });
  435. }
  436. }
  437. }
  438. },
  439. setOptionList: function (dataCode, inputT, obj) {
  440. if (dataCode == "option_codes") {
  441. //所选人材的情况,要获取人材机下拉列表
  442. inputT = inputT.replace(/[\s\r\n]/g, ""); //去掉空格换行等字符
  443. let optionList = [];
  444. let options = inputT.split("|");
  445. for (let code of options) {
  446. let name = gljAdjOprObj.getGljName(code, gljAdjOprObj.gljList, true);
  447. if (name) optionList.push({ text: name, value: code });
  448. }
  449. obj.option_list = optionList;
  450. }
  451. },
  452. onClipboardPasting: function (sender, info) {
  453. let me = coeOprObj,
  454. maxCol = info.cellRange.col + info.cellRange.colCount - 1;
  455. if (maxCol > me.setting.header.length) {
  456. info.cancel = true;
  457. }
  458. },
  459. onClipboardPasted: function (sender, info) {
  460. let me = coeOprObj,
  461. addArr = [],
  462. updateArr = [];
  463. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  464. let uniqItems = me.makeUniqItems(items);
  465. for (let i = 0, len = uniqItems.length; i < len; i++) {
  466. let row = i + info.cellRange.row;
  467. //update
  468. if (row < me.currentCoeList.length) {
  469. let updateObj = me.currentCoeList[row];
  470. for (let attr in uniqItems[i]) {
  471. if (attr === "serialNo") {
  472. if (
  473. me.isInt(uniqItems[i][attr]) &&
  474. !me.hasTisNo(me.currentCoeList, uniqItems[i][attr])
  475. ) {
  476. me.currentMaxNo =
  477. me.currentMaxNo >= uniqItems[i][attr]
  478. ? me.currentMaxNo
  479. : uniqItems[i][attr];
  480. updateObj[attr] = uniqItems[i][attr];
  481. }
  482. } else {
  483. updateObj[attr] = uniqItems[i][attr];
  484. }
  485. }
  486. updateArr.push(updateObj);
  487. }
  488. //insert
  489. else {
  490. if (
  491. typeof uniqItems[i].serialNo !== "undefined" &&
  492. uniqItems[i] &&
  493. me.isInt(uniqItems[i].serialNo) &&
  494. !me.hasTisNo(me.currentCoeList, uniqItems[i].serialNo)
  495. ) {
  496. me.currentMaxNo =
  497. me.currentMaxNo >= uniqItems[i].serialNo
  498. ? me.currentMaxNo
  499. : uniqItems[i].serialNo;
  500. } else {
  501. uniqItems[i].serialNo = ++me.currentMaxNo;
  502. }
  503. uniqItems[i].libID = pageOprObj.rationLibId;
  504. addArr.push(uniqItems[i]);
  505. }
  506. }
  507. if (addArr.length > 0 || updateArr.length > 0) {
  508. me.save(addArr, updateArr, [], true, function (result) {
  509. me.updateCurrentCoeList(result);
  510. });
  511. }
  512. },
  513. onDelOpr: function (workBook, setting) {
  514. let me = coeOprObj,
  515. that = gljAdjOprObj;
  516. workBook.commandManager().register("coeListDel", function () {
  517. let deleteArr = [];
  518. let sheet = workBook.getSheet(0);
  519. let sels = sheet.getSelections();
  520. let idx = sels[0].row;
  521. for (let i = 0, len = sels.length; i < len; i++) {
  522. if (idx > sels[i].row) {
  523. idx = sels[i].row;
  524. }
  525. if (sels[i].colCount === setting.header.length) {
  526. //can del
  527. for (let r = 0, rLen = sels[i].rowCount; r < rLen; r++) {
  528. let row = sels[i].row + r;
  529. if (row < me.currentCoeList.length) {
  530. deleteArr.push({
  531. libID: me.currentCoeList[row].libID,
  532. ID: me.currentCoeList[row].ID,
  533. });
  534. }
  535. }
  536. me.currentCoeList.splice(sels[i].row, sels[i].rowCount);
  537. }
  538. }
  539. if (deleteArr.length > 0) {
  540. let coeID = deleteArr[0].ID;
  541. me.referenceCheck(coeID, function (info) {
  542. if (info.length > 0) {
  543. alert("该子目换算已被引用,不能删除!");
  544. } else {
  545. me.save([], [], deleteArr, true);
  546. me.currentCoe =
  547. typeof me.currentCoeList[idx] !== "undefined"
  548. ? me.currentCoeList[idx]
  549. : null;
  550. that.currentGljAdjList = me.currentCoe ? me.currentCoe.coes : [];
  551. gljAdjOprObj.show(that.currentGljAdjList);
  552. }
  553. });
  554. }
  555. });
  556. workBook
  557. .commandManager()
  558. .setShortcutKey(
  559. null,
  560. GC.Spread.Commands.Key.del,
  561. false,
  562. false,
  563. false,
  564. false
  565. );
  566. workBook
  567. .commandManager()
  568. .setShortcutKey(
  569. "coeListDel",
  570. GC.Spread.Commands.Key.del,
  571. false,
  572. false,
  573. false,
  574. false
  575. );
  576. },
  577. initContextMenu: function () {
  578. const me = this;
  579. $.contextMenu({
  580. selector: "#mainSpread",
  581. build: function ($triggerElement, e) {
  582. //控制允许右键菜单在哪个位置出现
  583. const target = SheetDataHelper.safeRightClickSelection(
  584. $triggerElement,
  585. e,
  586. me.workBook
  587. );
  588. const sheet = me.workBook.getSheet(0);
  589. if (target.hitTestType === 3) {
  590. //在表格内&& typeof target.row !== 'undefined' && typeof target.col !== 'undefined'
  591. if (typeof target.row !== "undefined") {
  592. //控制按钮是否可用
  593. sheet.setActiveCell(target.row, target.col);
  594. me.coeSelInit(target.row);
  595. }
  596. return {
  597. callback: function () {},
  598. items: {
  599. getReference: {
  600. name: "查找引用",
  601. disabled: function () {
  602. return !me.currentCoeList || !me.currentCoeList[target.row];
  603. },
  604. icon: "fa-search",
  605. callback: function () {
  606. const coe = me.currentCoeList[target.row];
  607. me.getReference(coe.ID);
  608. },
  609. },
  610. },
  611. };
  612. } else {
  613. return false;
  614. }
  615. },
  616. });
  617. },
  618. referenceCheck: function (coeID, callback) {
  619. $.bootstrapLoading.start();
  620. CommonAjax.post(
  621. "/rationRepository/api/getCoeReference",
  622. { rationRepId: pageOprObj.rationLibId, coeID },
  623. function (info) {
  624. callback(info);
  625. $.bootstrapLoading.end();
  626. },
  627. function () {
  628. $.bootstrapLoading.end();
  629. }
  630. );
  631. },
  632. getReference: function (coeID) {
  633. const $info = $("#info");
  634. const $infoBody = $("#infoBody");
  635. const me = this;
  636. me.referenceCheck(coeID, function (info) {
  637. let htmlArr = [];
  638. if (!info || !info.length) {
  639. htmlArr.push("<li>无引用数据</li>");
  640. } else {
  641. htmlArr = info.map((ration) => {
  642. return `<li>
  643. <span>${ration.code}</span>
  644. <a data-dismiss="modal" href="javascript:;" onclick="sectionTreeObj.locateToSection('${ration.code}')">定位</a>
  645. </li>`;
  646. });
  647. }
  648. $infoBody.html(htmlArr.join(""));
  649. $info.modal("show");
  650. });
  651. },
  652. //粘贴的数据,编号唯一化,去除编号重复的项
  653. makeUniqItems: function (items) {
  654. let rst = [];
  655. for (let i = 0, len = items.length; i < len; i++) {
  656. if (typeof items[i].serialNo !== "undefined" && items[i].serialNo) {
  657. if (rst.length === 0) {
  658. rst.push(items[i]);
  659. } else {
  660. let isExist = false;
  661. for (let j = 0, jLen = rst.length; j < jLen; j++) {
  662. if (items[i].serialNo === rst[j].serialNo) {
  663. isExist = true;
  664. break;
  665. }
  666. }
  667. if (!isExist) {
  668. rst.push(items[i]);
  669. }
  670. }
  671. } else {
  672. rst.push(items[i]);
  673. }
  674. }
  675. return rst;
  676. },
  677. isInt: function (num) {
  678. return !isNaN(num) && num % 1 === 0;
  679. },
  680. hasTisNo: function (coeList, newSerialNo) {
  681. let rst = false;
  682. for (let i = 0, len = coeList.length; i < len; i++) {
  683. if (coeList[i].serialNo == newSerialNo) {
  684. rst = true;
  685. break;
  686. }
  687. }
  688. return rst;
  689. },
  690. updateCurrentCoeList: function (newCoeList) {
  691. let me = coeOprObj;
  692. if (newCoeList) {
  693. me.currentCoeList = me.currentCoeList.concat(newCoeList);
  694. }
  695. },
  696. sortCoeList: function (coeList) {
  697. coeList.sort(function (a, b) {
  698. let rst = 0;
  699. if (a.serialNo > b.serialNo) rst = 1;
  700. else if (a.serialNo < b.serialNo) rst = -1;
  701. return rst;
  702. });
  703. },
  704. getCoeList: function () {
  705. let me = coeOprObj;
  706. $.ajax({
  707. type: "post",
  708. url: "/rationRepository/api/getCoeList",
  709. data: { libID: pageOprObj.rationLibId },
  710. dataType: "json",
  711. timeout: 20000,
  712. success: function (result) {
  713. if (!result.error) {
  714. me.currentCoeList = result.data;
  715. me.sortCoeList(me.currentCoeList);
  716. // console.log(me.currentCoeList);
  717. me.currentMaxNo =
  718. me.currentCoeList.length > 0
  719. ? me.currentCoeList[me.currentCoeList.length - 1].serialNo
  720. : 0;
  721. pageObj.showData(me.workSheet, me.setting, me.currentCoeList);
  722. me.workSheet.clearSelection();
  723. }
  724. },
  725. error: function (err) {
  726. alert("内部程序错误!");
  727. },
  728. });
  729. },
  730. save: function (addArr, updateArr, deleteArr, refresh, callback) {
  731. let me = coeOprObj;
  732. $.ajax({
  733. type: "POST",
  734. url: "api/saveCoeList",
  735. data: {
  736. data: JSON.stringify({
  737. addArr: addArr,
  738. updateArr: updateArr,
  739. deleteArr: deleteArr,
  740. }),
  741. },
  742. dataType: "json",
  743. timeout: 5000,
  744. success: function (result) {
  745. if (result.error) {
  746. alert(result.message);
  747. } else {
  748. if (callback) {
  749. if (result.message === "mixed") {
  750. for (let i = 0, len = result.data.length; i < len; i++) {
  751. if (result.data[i][0] === "addSc") {
  752. result.data = result.data[i][1];
  753. break;
  754. }
  755. }
  756. }
  757. callback(result.data);
  758. }
  759. if (refresh) {
  760. me.sortCoeList(me.currentCoeList);
  761. me.currentMaxNo =
  762. me.currentCoeList.length > 0
  763. ? me.currentCoeList[me.currentCoeList.length - 1].serialNo
  764. : 0;
  765. pageObj.showData(me.workSheet, me.setting, me.currentCoeList);
  766. }
  767. }
  768. },
  769. error: function (err) {
  770. alert("内部程序错误!");
  771. },
  772. });
  773. },
  774. };
  775. // 验证数量的有效性:可以输入数值,也可以输入表达式,eg: [人材机编码]*1.5+1
  776. const validateAmount = (amountStr) => {
  777. if (!amountStr) {
  778. return true;
  779. }
  780. let str = amountStr.replace(/\s/g, "");
  781. if (!str) {
  782. return true;
  783. }
  784. try {
  785. str = amountStr.replace(/\s/g, "").replace(/\[\d+\]/g, "0");
  786. eval(str);
  787. } catch (error) {
  788. return false;
  789. }
  790. return true;
  791. };
  792. let gljAdjOprObj = {
  793. workBook: null,
  794. workSheet: null,
  795. currentGljAdjList: [],
  796. gljList: [], //只含编号和名称的总工料机列表
  797. setting: {
  798. header: [
  799. {
  800. headerName: "调整类型",
  801. headerWidth: 80,
  802. dataCode: "coeType",
  803. dataType: "String",
  804. hAlign: "center",
  805. vAlign: "center",
  806. readOnly: false,
  807. },
  808. {
  809. headerName: "人材机编码",
  810. headerWidth: 80,
  811. dataCode: "gljCode",
  812. dataType: "String",
  813. formatter: "@",
  814. hAlign: "center",
  815. vAlign: "center",
  816. readOnly: false,
  817. },
  818. // readOnly: true --> false,需要兼容粘贴列包含只读项
  819. {
  820. headerName: "名称",
  821. headerWidth: 100,
  822. dataCode: "gljName",
  823. dataType: "String",
  824. hAlign: "center",
  825. vAlign: "center",
  826. readOnly: false,
  827. },
  828. {
  829. headerName: "操作符",
  830. headerWidth: 60,
  831. dataCode: "operator",
  832. dataType: "String",
  833. hAlign: "center",
  834. vAlign: "center",
  835. readOnly: false,
  836. },
  837. {
  838. headerName: "数量",
  839. headerWidth: 80,
  840. dataCode: "amount",
  841. dataType: "String",
  842. hAlign: "center",
  843. vAlign: "center",
  844. readOnly: false,
  845. },
  846. {
  847. headerName: "替换为编码",
  848. headerWidth: 80,
  849. dataCode: "replaceCode",
  850. dataType: "String",
  851. formatter: "@",
  852. hAlign: "center",
  853. vAlign: "center",
  854. readOnly: false,
  855. },
  856. // readOnly: true --> false
  857. {
  858. headerName: "替换为名称",
  859. headerWidth: 100,
  860. dataCode: "replaceName",
  861. dataType: "String",
  862. hAlign: "center",
  863. vAlign: "center",
  864. readOnly: false,
  865. },
  866. ],
  867. comboItems: {
  868. //调整类型下拉菜单
  869. coeType: [
  870. "定额",
  871. "人工",
  872. "材料",
  873. "机械",
  874. "主材",
  875. "设备",
  876. "单个工料机",
  877. "替换人材机",
  878. "所选人材机",
  879. ],
  880. //操作符下拉菜单
  881. operator: ["+", "-", "*", "/", "=", "+*", "-*"],
  882. },
  883. },
  884. buildSheet: function (container) {
  885. let me = gljAdjOprObj;
  886. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 3);
  887. me.workSheet = me.workBook.getSheet(0);
  888. sheetCommonObj.bindEscKey(me.workBook, [
  889. {
  890. sheet: me.workBook.getSheet(0),
  891. editStarting: me.onEditStart,
  892. editEnded: me.onEditEnded,
  893. },
  894. ]);
  895. me.workSheet.options.isProtected = true;
  896. me.onDelOpr(me.workBook, me.setting);
  897. me.workSheet.clearSelection();
  898. me.workSheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStart);
  899. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  900. me.workSheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
  901. me.workSheet.bind(
  902. GC.Spread.Sheets.Events.ClipboardPasted,
  903. me.onClipboardPasted
  904. );
  905. },
  906. buildBaseCell: function (sheet) {
  907. let me = gljAdjOprObj;
  908. sheet.suspendPaint();
  909. sheet.suspendEvent();
  910. let baseCell = GC.Spread.Sheets.CellTypes.Base();
  911. sheet.getCell(-1, 0).cellType(baseCell);
  912. sheet.getCell(-1, 3).cellType(baseCell);
  913. sheet.resumePaint();
  914. sheet.resumeEvent();
  915. },
  916. buildDynamicComboBox: function (sheet) {
  917. let me = gljAdjOprObj;
  918. sheet.suspendPaint();
  919. sheet.suspendEvent();
  920. let dynamicCombo = sheetCommonObj.getDynamicCombo();
  921. dynamicCombo.items(me.setting.comboItems.coeType);
  922. let dynamicOprCombo = sheetCommonObj.getDynamicCombo();
  923. dynamicOprCombo.items(me.setting.comboItems.operator);
  924. sheet.getCell(-1, 0).cellType(dynamicCombo);
  925. sheet.getCell(-1, 3).cellType(dynamicOprCombo);
  926. sheet.resumePaint();
  927. sheet.resumeEvent();
  928. },
  929. onEnterCell: function (sender, args) {
  930. args.sheet.repaint();
  931. },
  932. onEditStart: function (sender, args) {
  933. let me = gljAdjOprObj;
  934. let dataCode = me.setting.header[args.col].dataCode;
  935. // 超出范围-没有选中的调整主项
  936. if (!coeOprObj.currentCoe) {
  937. args.cancel = true;
  938. return;
  939. }
  940. // 名称、替换为名称总是不可编辑(表头设置readOnly没有设置为true,因为需要粘贴)
  941. let alwaysNotEditable = ["gljName", "replaceName"];
  942. if (alwaysNotEditable.includes(dataCode)) {
  943. args.cancel = true;
  944. return;
  945. }
  946. // 新一行编辑时,除了总是可编辑的单元格:调整类型、操作符、数量,其他不可编辑
  947. let alwaysEditable = ["coeType", "operator", "amount"];
  948. if (
  949. args.row > me.currentGljAdjList.length - 1 &&
  950. !alwaysEditable.includes(dataCode)
  951. ) {
  952. args.cancel = true;
  953. return;
  954. }
  955. let curSubCoe = me.currentGljAdjList[args.row];
  956. if (!curSubCoe) {
  957. return;
  958. }
  959. // 调整类型不为“单个工料机”和“替换人材机”时,人材机编码不可编辑
  960. if (
  961. !["单个工料机", "替换人材机"].includes(curSubCoe.coeType) &&
  962. dataCode === "gljCode"
  963. ) {
  964. args.cancel = true;
  965. return;
  966. }
  967. // 调整类型不为“替换人材机”时,替换为编码不可编辑
  968. if (curSubCoe.coeType !== "替换人材机" && dataCode === "replaceCode") {
  969. args.cancel = true;
  970. return;
  971. }
  972. },
  973. onEditEnded: function (sender, args) {
  974. let me = gljAdjOprObj,
  975. isUpdate = false,
  976. dataCode = me.setting.header[args.col].dataCode;
  977. if (args.editingText && args.editingText.toString().trim().length > 0) {
  978. if (dataCode === "amount" && !validateAmount(args.editingText)) {
  979. alert("请输入数值或者表达式,如:[人材机编码]*0.3+1");
  980. args.sheet.setValue(
  981. args.row,
  982. args.col,
  983. typeof me.currentGljAdjList[args.row] !== "undefined" &&
  984. typeof me.currentGljAdjList[args.row][dataCode] !== "undefined"
  985. ? me.currentGljAdjList[args.row][dataCode] + ""
  986. : ""
  987. );
  988. } else {
  989. //update
  990. if (
  991. args.row < me.currentGljAdjList.length &&
  992. args.editingText.toString().trim() !==
  993. me.currentGljAdjList[args.row][dataCode]
  994. ) {
  995. let updateObj = me.currentGljAdjList[args.row];
  996. if (
  997. dataCode === "gljCode" &&
  998. typeof updateObj.coeType !== "undefined" &&
  999. (updateObj.coeType === "单个工料机" ||
  1000. updateObj.coeType === "替换人材机")
  1001. ) {
  1002. let gljName = me.getGljName(args.editingText, me.gljList);
  1003. if (gljName) {
  1004. updateObj.gljCode = args.editingText;
  1005. updateObj.gljName = gljName;
  1006. isUpdate = true;
  1007. } else {
  1008. alert("不存在编号为" + args.editingText + "的工料机");
  1009. }
  1010. } else if (
  1011. dataCode === "replaceCode" &&
  1012. typeof updateObj.coeType !== "undefined" &&
  1013. updateObj.coeType === "替换人材机"
  1014. ) {
  1015. let gljName = me.getGljName(args.editingText, me.gljList);
  1016. if (gljName) {
  1017. updateObj.replaceCode = args.editingText;
  1018. updateObj.replaceName = gljName;
  1019. isUpdate = true;
  1020. } else {
  1021. alert("不存在编号为" + args.editingText + "的工料机");
  1022. }
  1023. } else if (dataCode === "coeType") {
  1024. isUpdate = true;
  1025. updateObj[dataCode] = args.editingText;
  1026. updateObj.gljCode = "";
  1027. updateObj.gljName = "";
  1028. updateObj.replaceCode = "";
  1029. updateObj.replaceName = "";
  1030. } else if (dataCode !== "gljCode") {
  1031. isUpdate = true;
  1032. updateObj[dataCode] = args.editingText;
  1033. }
  1034. }
  1035. //insert
  1036. else if (args.row >= me.currentGljAdjList.length) {
  1037. isUpdate = true;
  1038. let newAdjGlj = {};
  1039. newAdjGlj[dataCode] = args.editingText;
  1040. me.currentGljAdjList.push(newAdjGlj);
  1041. }
  1042. if (isUpdate) {
  1043. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  1044. console.log(me.currentGljAdjList);
  1045. me.show(me.currentGljAdjList);
  1046. });
  1047. } else {
  1048. args.sheet.setValue(
  1049. args.row,
  1050. args.col,
  1051. typeof me.currentGljAdjList[args.row] !== "undefined" &&
  1052. typeof me.currentGljAdjList[args.row][dataCode] !== "undefined"
  1053. ? me.currentGljAdjList[args.row][dataCode] + ""
  1054. : ""
  1055. );
  1056. }
  1057. }
  1058. }
  1059. },
  1060. getValidPasteData: function (pasteItems, info) {
  1061. let me = gljAdjOprObj;
  1062. function isDef(v) {
  1063. return v !== undefined && v !== null;
  1064. }
  1065. // 粘贴项匹配处理
  1066. let rules = {
  1067. coeType: function (v, cur, tar) {
  1068. if (v === "") {
  1069. tar.coeType = v;
  1070. tar.gljCode = v;
  1071. tar.gljName = v;
  1072. tar.replaceCode = v;
  1073. tar.replaceName = v;
  1074. } else if (me.setting.comboItems.coeType.includes(v)) {
  1075. tar.coeType = v;
  1076. }
  1077. },
  1078. gljCode: function (v, cur, tar) {
  1079. if (v === "") {
  1080. tar.gljCode = v;
  1081. tar.gljName = v;
  1082. } else if (
  1083. ["单个工料机", "替换人材机"].includes(tar.coeType) ||
  1084. (!isDef(tar.coeType) &&
  1085. isDef(cur) &&
  1086. ["单个工料机", "替换人材机"].includes(cur.coeType))
  1087. ) {
  1088. let gljName = me.getGljName(v, me.gljList);
  1089. if (gljName) {
  1090. tar.gljCode = v;
  1091. tar.gljName = gljName;
  1092. }
  1093. }
  1094. },
  1095. amount: function (v, cur, tar) {
  1096. /* if (!isNaN(v)) {
  1097. tar.amount = v;
  1098. } */
  1099. if (validateAmount(v)) {
  1100. tar.amount = v;
  1101. }
  1102. },
  1103. operator: function (v, cur, tar) {
  1104. if (v === "" || me.setting.comboItems.operator.includes(v)) {
  1105. tar.operator = v;
  1106. }
  1107. },
  1108. replaceCode: function (v, cur, tar) {
  1109. if (v === "") {
  1110. tar.replaceCode = v;
  1111. tar.replaceName = v;
  1112. } else if (
  1113. tar.coeType === "替换人材机" ||
  1114. (!isDef(tar.coeType) && isDef(cur) && cur.coeType === "替换人材机")
  1115. ) {
  1116. let replaceName = me.getGljName(v, me.gljList);
  1117. if (replaceName) {
  1118. tar.replaceCode = v;
  1119. tar.replaceName = replaceName;
  1120. }
  1121. }
  1122. },
  1123. };
  1124. let rst = [];
  1125. for (let i = 0, len = pasteItems.length; i < len; i++) {
  1126. let row = i + info.cellRange.row;
  1127. let target = {},
  1128. curObj = me.currentGljAdjList[row],
  1129. pasteItem = pasteItems[i];
  1130. if (row < me.currentGljAdjList.length) {
  1131. target.index = row; //要有下标做为匹配的依据,不然在复制多行并且某个单元格是只读的情况下,这里返回的updateList个数会比选中的行数少,造成更新行和实际不匹配的情况
  1132. }
  1133. for (let pasteKey in pasteItem) {
  1134. if (rules[pasteKey]) {
  1135. rules[pasteKey](pasteItem[pasteKey], curObj, target);
  1136. }
  1137. }
  1138. if (Object.keys(target).length > 0) {
  1139. rst.push(target);
  1140. }
  1141. }
  1142. return rst;
  1143. },
  1144. onClipboardPasted: function (sender, info) {
  1145. let me = gljAdjOprObj,
  1146. row;
  1147. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  1148. let validData = me.getValidPasteData(items, info);
  1149. for (let i = 0, len = validData.length; i < len; i++) {
  1150. row = i + info.cellRange.row;
  1151. //update
  1152. if (
  1153. row < me.currentGljAdjList.length &&
  1154. typeof validData[i].index !== "undefined"
  1155. ) {
  1156. let updateObj = me.currentGljAdjList[validData[i].index]; //这里改成读取下标
  1157. delete validData[i].index; //清除下标
  1158. Object.assign(updateObj, validData[i]);
  1159. }
  1160. //insert
  1161. else {
  1162. me.currentGljAdjList.push(validData[i]);
  1163. }
  1164. }
  1165. if (validData.length > 0) {
  1166. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  1167. me.show(me.currentGljAdjList);
  1168. });
  1169. } else {
  1170. me.show(me.currentGljAdjList);
  1171. }
  1172. },
  1173. onDelOpr: function (workBook, setting) {
  1174. let me = gljAdjOprObj;
  1175. workBook.commandManager().register("gljAdjDel", function () {
  1176. let sheet = workBook.getSheet(0);
  1177. let sels = sheet.getSelections();
  1178. let isUpdate = false;
  1179. for (let i = 0, len = sels.length; i < len; i++) {
  1180. if (sels[i].colCount === setting.header.length) {
  1181. //can del
  1182. if (sels[i].row < me.currentGljAdjList.length) {
  1183. isUpdate = true;
  1184. me.currentGljAdjList.splice(sels[i].row, sels[i].rowCount);
  1185. }
  1186. }
  1187. }
  1188. if (isUpdate) {
  1189. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  1190. me.show(me.currentGljAdjList);
  1191. });
  1192. }
  1193. });
  1194. workBook
  1195. .commandManager()
  1196. .setShortcutKey(
  1197. null,
  1198. GC.Spread.Commands.Key.del,
  1199. false,
  1200. false,
  1201. false,
  1202. false
  1203. );
  1204. workBook
  1205. .commandManager()
  1206. .setShortcutKey(
  1207. "gljAdjDel",
  1208. GC.Spread.Commands.Key.del,
  1209. false,
  1210. false,
  1211. false,
  1212. false
  1213. );
  1214. },
  1215. getGljName: function (gljCode, gljList, withSpecs) {
  1216. //withSpecs 是否带上规格型号
  1217. let rst = null;
  1218. for (let i = 0, len = gljList.length; i < len; i++) {
  1219. if (gljCode === gljList[i].code) {
  1220. rst =
  1221. withSpecs == true
  1222. ? gljList[i].name + " - " + gljList[i].specs
  1223. : gljList[i].name;
  1224. break;
  1225. }
  1226. }
  1227. return rst;
  1228. },
  1229. show: function (coes) {
  1230. let me = gljAdjOprObj;
  1231. pageObj.showData(me.workSheet, me.setting, coes);
  1232. },
  1233. getGljItemsOcc: function () {
  1234. let me = gljAdjOprObj;
  1235. $.ajax({
  1236. type: "post",
  1237. url: "/stdGljRepository/api/getGljItemsOccupied",
  1238. data: { repId: pageOprObj.gljLibId, occupation: "-_id code name specs" },
  1239. dataType: "json",
  1240. timeout: 5000,
  1241. success: function (result) {
  1242. if (result.error) {
  1243. alert(result.message);
  1244. } else {
  1245. me.gljList = result.data;
  1246. }
  1247. },
  1248. error: function (err) {
  1249. alert("内部程序错误!");
  1250. },
  1251. });
  1252. },
  1253. };