coe.js 42 KB

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