coe.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. ]
  233. },
  234. buildSheet: function (container) {
  235. let me = coeOprObj;
  236. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  237. sheetCommonObj.bindEscKey(me.workBook, [{ sheet: me.workBook.getSheet(0), editStarting: null, editEnded: me.onEditEnded }]);
  238. me.workSheet = me.workBook.getSheet(0);
  239. me.workSheet.options.isProtected = true;
  240. me.onDelOpr(me.workBook, me.setting);
  241. me.initContextMenu();
  242. me.workSheet.bind(GC.Spread.Sheets.Events.SelectionChanged, me.onSelectionChanged);
  243. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  244. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  245. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  246. },
  247. onSelectionChanged: function (sender, info) {
  248. if (info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row) {
  249. const row = info.newSelections[0].row;
  250. coeOprObj.coeSelInit(row);
  251. }
  252. },
  253. coeSelInit: function (row) {
  254. const me = coeOprObj;
  255. const adj = gljAdjOprObj;
  256. if (row < me.currentCoeList.length) {
  257. me.currentCoe = me.currentCoeList[row];
  258. adj.currentGljAdjList = me.currentCoe.coes;
  259. adj.buildDynamicComboBox(adj.workSheet);
  260. } else {
  261. me.currentCoe = null;
  262. adj.currentGljAdjList = [];
  263. adj.buildBaseCell(adj.workSheet);
  264. }
  265. //refresh & show coes
  266. sheetCommonObj.cleanSheet(adj.workSheet, adj.setting, -1);
  267. me.workBook.focus(true);
  268. adj.show(adj.currentGljAdjList);
  269. },
  270. onEditEnded: function (sender, args) {
  271. let me = coeOprObj, addArr = [], updateArr = [], dataCode = me.setting.header[args.col].dataCode;
  272. if (args.editingText && args.editingText.toString().trim().length > 0) {
  273. let inputT = args.editingText.toString().trim();
  274. //update
  275. if (args.row < me.currentCoeList.length) {
  276. let updateObj = me.currentCoeList[args.row];
  277. if (updateObj[dataCode] != inputT) {
  278. if (dataCode === 'serialNo') {
  279. if (me.isInt(inputT) && !me.hasTisNo(me.currentCoeList, inputT)) {
  280. me.currentMaxNo = me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  281. updateObj[dataCode] = inputT;
  282. updateArr.push(updateObj);
  283. me.save([], updateArr, [], true);
  284. }
  285. else if (!me.isInt(inputT)) {
  286. alert('编号只能为整数!');
  287. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + '');
  288. }
  289. else if (me.hasTisNo(me.currentCoeList, inputT)) {
  290. alert('该编号已存在!');
  291. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + '');
  292. }
  293. }
  294. else {
  295. updateObj[dataCode] = inputT;
  296. me.setOptionList(dataCode, inputT, updateObj);
  297. updateArr.push(updateObj);
  298. me.save([], updateArr, [], true);
  299. }
  300. }
  301. }
  302. //insert
  303. else {
  304. let newCoe = {};
  305. newCoe.libID = pageOprObj.rationLibId;
  306. if (dataCode === 'serialNo') {
  307. if (me.isInt(inputT) && !me.hasTisNo(me.currentCoeList, inputT)) {
  308. me.currentMaxNo = me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  309. newCoe[dataCode] = inputT;
  310. addArr.push(newCoe);
  311. me.save(addArr, [], [], true, function (result) {
  312. me.updateCurrentCoeList(result);
  313. });
  314. }
  315. else if (!me.isInt(inputT)) {
  316. args.sheet.setValue(args.row, args.col, '');
  317. alert('编号只能为整数!');
  318. }
  319. else if (me.hasTisNo(me.currentCoeList, inputT)) {
  320. args.sheet.setValue(args.row, args.col, '');
  321. alert('该编号已存在!');
  322. }
  323. }
  324. else {
  325. newCoe.serialNo = ++me.currentMaxNo;
  326. newCoe[dataCode] = inputT;
  327. me.setOptionList(dataCode, inputT, newCoe);
  328. addArr.push(newCoe);
  329. me.save(addArr, [], [], true, function (result) {
  330. me.updateCurrentCoeList(result);
  331. });
  332. }
  333. }
  334. }
  335. },
  336. setOptionList: function (dataCode, inputT, obj) {
  337. if (dataCode == "option_codes") {//所选人材的情况,要获取人材机下拉列表
  338. inputT = inputT.replace(/[\s\r\n]/g, "")//去掉空格换行等字符
  339. let optionList = [];
  340. let options = inputT.split("|");
  341. for (let code of options) {
  342. let name = gljAdjOprObj.getGljName(code, gljAdjOprObj.gljList, true);
  343. if (name) optionList.push({ text: name, value: code });
  344. }
  345. obj.option_list = optionList;
  346. }
  347. },
  348. onClipboardPasting: function (sender, info) {
  349. let me = coeOprObj, maxCol = info.cellRange.col + info.cellRange.colCount - 1;
  350. if (maxCol > me.setting.header.length) {
  351. info.cancel = true;
  352. }
  353. },
  354. onClipboardPasted: function (sender, info) {
  355. let me = coeOprObj, addArr = [], updateArr = [];
  356. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  357. let uniqItems = me.makeUniqItems(items);
  358. for (let i = 0, len = uniqItems.length; i < len; i++) {
  359. let row = i + info.cellRange.row;
  360. //update
  361. if (row < me.currentCoeList.length) {
  362. let updateObj = me.currentCoeList[row];
  363. for (let attr in uniqItems[i]) {
  364. if (attr === 'serialNo') {
  365. if (me.isInt(uniqItems[i][attr]) && !me.hasTisNo(me.currentCoeList, uniqItems[i][attr])) {
  366. me.currentMaxNo = me.currentMaxNo >= uniqItems[i][attr] ? me.currentMaxNo : uniqItems[i][attr];
  367. updateObj[attr] = uniqItems[i][attr];
  368. }
  369. }
  370. else {
  371. updateObj[attr] = uniqItems[i][attr];
  372. }
  373. }
  374. updateArr.push(updateObj);
  375. }
  376. //insert
  377. else {
  378. if (typeof uniqItems[i].serialNo !== 'undefined' && uniqItems[i] && me.isInt(uniqItems[i].serialNo) && !me.hasTisNo(me.currentCoeList, uniqItems[i].serialNo)) {
  379. me.currentMaxNo = me.currentMaxNo >= uniqItems[i].serialNo ? me.currentMaxNo : uniqItems[i].serialNo;
  380. }
  381. else {
  382. uniqItems[i].serialNo = ++me.currentMaxNo;
  383. }
  384. uniqItems[i].libID = pageOprObj.rationLibId;
  385. addArr.push(uniqItems[i]);
  386. }
  387. }
  388. if (addArr.length > 0 || updateArr.length > 0) {
  389. me.save(addArr, updateArr, [], true, function (result) {
  390. me.updateCurrentCoeList(result);
  391. });
  392. }
  393. },
  394. onDelOpr: function (workBook, setting) {
  395. let me = coeOprObj, that = gljAdjOprObj;
  396. workBook.commandManager().register('coeListDel', function () {
  397. let deleteArr = [];
  398. let sheet = workBook.getSheet(0);
  399. let sels = sheet.getSelections();
  400. let idx = sels[0].row;
  401. for (let i = 0, len = sels.length; i < len; i++) {
  402. if (idx > sels[i].row) {
  403. idx = sels[i].row;
  404. }
  405. if (sels[i].colCount === setting.header.length) {//can del
  406. for (let r = 0, rLen = sels[i].rowCount; r < rLen; r++) {
  407. let row = sels[i].row + r;
  408. if (row < me.currentCoeList.length) {
  409. deleteArr.push({ libID: me.currentCoeList[row].libID, ID: me.currentCoeList[row].ID });
  410. }
  411. }
  412. me.currentCoeList.splice(sels[i].row, sels[i].rowCount);
  413. }
  414. }
  415. if (deleteArr.length > 0) {
  416. me.save([], [], deleteArr, true);
  417. me.currentCoe = typeof me.currentCoeList[idx] !== 'undefined' ? me.currentCoeList[idx] : null;
  418. that.currentGljAdjList = me.currentCoe ? me.currentCoe.coes : [];
  419. gljAdjOprObj.show(that.currentGljAdjList);
  420. }
  421. });
  422. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  423. workBook.commandManager().setShortcutKey('coeListDel', GC.Spread.Commands.Key.del, false, false, false, false);
  424. },
  425. initContextMenu: function () {
  426. const me = this;
  427. $.contextMenu({
  428. selector: '#mainSpread',
  429. build: function ($triggerElement, e) {
  430. //控制允许右键菜单在哪个位置出现
  431. const target = SheetDataHelper.safeRightClickSelection($triggerElement, e, me.workBook);
  432. const sheet = me.workBook.getSheet(0);
  433. if (target.hitTestType === 3) {//在表格内&& typeof target.row !== 'undefined' && typeof target.col !== 'undefined'
  434. if (typeof target.row !== 'undefined') {
  435. //控制按钮是否可用
  436. sheet.setActiveCell(target.row, target.col);
  437. me.coeSelInit(target.row);
  438. }
  439. return {
  440. callback: function () { },
  441. items: {
  442. getReference: {
  443. name: '查找引用',
  444. disabled: function () {
  445. return !me.currentCoeList || !me.currentCoeList[target.row];
  446. },
  447. icon: 'fa-search',
  448. callback: function () {
  449. const coe = me.currentCoeList[target.row];
  450. me.getReference(coe.ID);
  451. }
  452. },
  453. }
  454. };
  455. }
  456. else {
  457. return false;
  458. }
  459. }
  460. });
  461. },
  462. getReference: function (coeID) {
  463. const $info = $('#info');
  464. const $infoBody = $('#infoBody');
  465. $.bootstrapLoading.start();
  466. CommonAjax.post('/rationRepository/api/getCoeReference', { rationRepId: pageOprObj.rationLibId, coeID }, function (info) {
  467. let htmlArr = [];
  468. if (!info || !info.length) {
  469. htmlArr.push('<li>无引用数据</li>');
  470. } else {
  471. htmlArr = info.map(ration => {
  472. return `<li>
  473. <span>${ration.code}</span>
  474. <a data-dismiss="modal" href="javascript:;" onclick="sectionTreeObj.locateToSection('${ration.code}')">定位</a>
  475. </li>`;
  476. });
  477. }
  478. $infoBody.html(htmlArr.join(''));
  479. $info.modal('show');
  480. $.bootstrapLoading.end();
  481. }, function () {
  482. $.bootstrapLoading.end();
  483. });
  484. },
  485. //粘贴的数据,编号唯一化,去除编号重复的项
  486. makeUniqItems: function (items) {
  487. let rst = [];
  488. for (let i = 0, len = items.length; i < len; i++) {
  489. if (typeof items[i].serialNo !== 'undefined' && items[i].serialNo) {
  490. if (rst.length === 0) {
  491. rst.push(items[i]);
  492. }
  493. else {
  494. let isExist = false;
  495. for (let j = 0, jLen = rst.length; j < jLen; j++) {
  496. if (items[i].serialNo === rst[j].serialNo) {
  497. isExist = true;
  498. break;
  499. }
  500. }
  501. if (!isExist) {
  502. rst.push(items[i]);
  503. }
  504. }
  505. }
  506. else {
  507. rst.push(items[i]);
  508. }
  509. }
  510. return rst;
  511. },
  512. isInt: function (num) {
  513. return !isNaN(num) && num % 1 === 0;
  514. },
  515. hasTisNo: function (coeList, newSerialNo) {
  516. let rst = false;
  517. for (let i = 0, len = coeList.length; i < len; i++) {
  518. if (coeList[i].serialNo == newSerialNo) {
  519. rst = true;
  520. break;
  521. }
  522. }
  523. return rst;
  524. },
  525. updateCurrentCoeList: function (newCoeList) {
  526. let me = coeOprObj;
  527. if (newCoeList) {
  528. me.currentCoeList = me.currentCoeList.concat(newCoeList);
  529. }
  530. },
  531. sortCoeList: function (coeList) {
  532. coeList.sort(function (a, b) {
  533. let rst = 0;
  534. if (a.serialNo > b.serialNo) rst = 1;
  535. else if (a.serialNo < b.serialNo) rst = -1;
  536. return rst;
  537. });
  538. },
  539. getCoeList: function () {
  540. let me = coeOprObj;
  541. $.ajax({
  542. type: 'post',
  543. url: '/rationRepository/api/getCoeList',
  544. data: { libID: pageOprObj.rationLibId },
  545. dataType: 'json',
  546. timeout: 20000,
  547. success: function (result) {
  548. if (!result.error) {
  549. me.currentCoeList = result.data;
  550. me.sortCoeList(me.currentCoeList);
  551. me.currentMaxNo = me.currentCoeList.length > 0 ? me.currentCoeList[me.currentCoeList.length - 1].serialNo : 0;
  552. pageObj.showData(me.workSheet, me.setting, me.currentCoeList);
  553. me.workSheet.clearSelection();
  554. }
  555. },
  556. error: function (err) {
  557. alert("内部程序错误!");
  558. }
  559. });
  560. },
  561. save: function (addArr, updateArr, deleteArr, refresh, callback) {
  562. let me = coeOprObj;
  563. $.ajax({
  564. type: "POST",
  565. url: "api/saveCoeList",
  566. data: { data: JSON.stringify({ addArr: addArr, updateArr: updateArr, deleteArr: deleteArr }) },
  567. dataType: "json",
  568. timeout: 5000,
  569. success: function (result) {
  570. if (result.error) {
  571. alert(result.message);
  572. } else {
  573. if (callback) {
  574. if (result.message === 'mixed') {
  575. for (let i = 0, len = result.data.length; i < len; i++) {
  576. if (result.data[i][0] === 'addSc') {
  577. result.data = result.data[i][1];
  578. break;
  579. }
  580. }
  581. }
  582. callback(result.data);
  583. }
  584. if (refresh) {
  585. me.sortCoeList(me.currentCoeList);
  586. me.currentMaxNo = me.currentCoeList.length > 0 ? me.currentCoeList[me.currentCoeList.length - 1].serialNo : 0;
  587. pageObj.showData(me.workSheet, me.setting, me.currentCoeList);
  588. }
  589. }
  590. },
  591. error: function (err) {
  592. alert("内部程序错误!");
  593. }
  594. });
  595. }
  596. };
  597. // 验证数量的有效性:可以输入数值,也可以输入表达式,eg: [人材机编码]*1.5+1
  598. const validateAmount = (amountStr) => {
  599. if (!amountStr) {
  600. return true;
  601. }
  602. let str = amountStr.replace(/\s/g, '');
  603. if (!str) {
  604. return true;
  605. }
  606. try {
  607. str = amountStr.replace(/\s/g, '').replace(/\[\d+\]/g, '0');
  608. eval(str);
  609. } catch (error) {
  610. return false;
  611. }
  612. return true;
  613. }
  614. let gljAdjOprObj = {
  615. workBook: null,
  616. workSheet: null,
  617. currentGljAdjList: [],
  618. gljList: [],//只含编号和名称的总工料机列表
  619. setting: {
  620. header: [
  621. { headerName: "调整类型", headerWidth: 80, dataCode: "coeType", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false },
  622. { headerName: "人材机编码", headerWidth: 80, dataCode: "gljCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false },
  623. // readOnly: true --> false,需要兼容粘贴列包含只读项
  624. { headerName: "名称", headerWidth: 100, dataCode: "gljName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false },
  625. { headerName: "操作符", headerWidth: 60, dataCode: "operator", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false },
  626. { headerName: "数量", headerWidth: 80, dataCode: "amount", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false },
  627. { headerName: "替换为编码", headerWidth: 80, dataCode: "replaceCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false },
  628. // readOnly: true --> false
  629. { headerName: "替换为名称", headerWidth: 100, dataCode: "replaceName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false }
  630. ],
  631. comboItems: {
  632. //调整类型下拉菜单
  633. coeType: ['定额', '人工', '材料', '机械', '主材', '设备', '单个工料机', '替换人材机', "所选人材机"],
  634. //操作符下拉菜单
  635. operator: ['+', '-', '*', '/', '=', '+*', '-*']
  636. }
  637. },
  638. buildSheet: function (container) {
  639. let me = gljAdjOprObj;
  640. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 3);
  641. me.workSheet = me.workBook.getSheet(0);
  642. sheetCommonObj.bindEscKey(me.workBook, [{ sheet: me.workBook.getSheet(0), editStarting: me.onEditStart, editEnded: me.onEditEnded }]);
  643. me.workSheet.options.isProtected = true;
  644. me.onDelOpr(me.workBook, me.setting);
  645. me.workSheet.clearSelection();
  646. me.workSheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStart);
  647. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  648. me.workSheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
  649. me.workSheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  650. },
  651. buildBaseCell: function (sheet) {
  652. let me = gljAdjOprObj;
  653. sheet.suspendPaint();
  654. sheet.suspendEvent();
  655. let baseCell = GC.Spread.Sheets.CellTypes.Base();
  656. sheet.getCell(-1, 0).cellType(baseCell);
  657. sheet.getCell(-1, 3).cellType(baseCell);
  658. sheet.resumePaint();
  659. sheet.resumeEvent();
  660. },
  661. buildDynamicComboBox: function (sheet) {
  662. let me = gljAdjOprObj;
  663. sheet.suspendPaint();
  664. sheet.suspendEvent();
  665. let dynamicCombo = sheetCommonObj.getDynamicCombo();
  666. dynamicCombo.items(me.setting.comboItems.coeType);
  667. let dynamicOprCombo = sheetCommonObj.getDynamicCombo();
  668. dynamicOprCombo.items(me.setting.comboItems.operator);
  669. sheet.getCell(-1, 0).cellType(dynamicCombo);
  670. sheet.getCell(-1, 3).cellType(dynamicOprCombo);
  671. sheet.resumePaint();
  672. sheet.resumeEvent();
  673. },
  674. onEnterCell: function (sender, args) {
  675. args.sheet.repaint();
  676. },
  677. onEditStart: function (sender, args) {
  678. let me = gljAdjOprObj;
  679. let dataCode = me.setting.header[args.col].dataCode;
  680. // 超出范围-没有选中的调整主项
  681. if (!coeOprObj.currentCoe) {
  682. args.cancel = true;
  683. return;
  684. }
  685. // 名称、替换为名称总是不可编辑(表头设置readOnly没有设置为true,因为需要粘贴)
  686. let alwaysNotEditable = ['gljName', 'replaceName'];
  687. if (alwaysNotEditable.includes(dataCode)) {
  688. args.cancel = true;
  689. return;
  690. }
  691. // 新一行编辑时,除了总是可编辑的单元格:调整类型、操作符、数量,其他不可编辑
  692. let alwaysEditable = ['coeType', 'operator', 'amount'];
  693. if (args.row > me.currentGljAdjList.length - 1 &&
  694. !alwaysEditable.includes(dataCode)) {
  695. args.cancel = true;
  696. return;
  697. }
  698. let curSubCoe = me.currentGljAdjList[args.row];
  699. if (!curSubCoe) {
  700. return;
  701. }
  702. // 调整类型不为“单个工料机”和“替换人材机”时,人材机编码不可编辑
  703. if (!['单个工料机', '替换人材机'].includes(curSubCoe.coeType) && dataCode === 'gljCode') {
  704. args.cancel = true;
  705. return;
  706. }
  707. // 调整类型不为“替换人材机”时,替换为编码不可编辑
  708. if (curSubCoe.coeType !== '替换人材机' && dataCode === 'replaceCode') {
  709. args.cancel = true;
  710. return;
  711. }
  712. },
  713. onEditEnded: function (sender, args) {
  714. let me = gljAdjOprObj, isUpdate = false,
  715. dataCode = me.setting.header[args.col].dataCode;
  716. if (args.editingText && args.editingText.toString().trim().length > 0) {
  717. if (dataCode === 'amount' && !validateAmount(args.editingText)) {
  718. alert("请输入数值或者表达式,如:[人材机编码]*0.3+1");
  719. args.sheet.setValue(args.row, args.col, typeof me.currentGljAdjList[args.row] !== 'undefined' && typeof me.currentGljAdjList[args.row][dataCode] !== 'undefined'
  720. ? me.currentGljAdjList[args.row][dataCode] + '' : '');
  721. }
  722. else {
  723. //update
  724. if (args.row < me.currentGljAdjList.length && args.editingText.toString().trim() !== me.currentGljAdjList[args.row][dataCode]) {
  725. let updateObj = me.currentGljAdjList[args.row];
  726. if (dataCode === 'gljCode' && typeof updateObj.coeType !== 'undefined' && (updateObj.coeType === '单个工料机' || updateObj.coeType === '替换人材机')) {
  727. let gljName = me.getGljName(args.editingText, me.gljList);
  728. if (gljName) {
  729. updateObj.gljCode = args.editingText;
  730. updateObj.gljName = gljName;
  731. isUpdate = true;
  732. } else {
  733. alert("不存在编号为" + args.editingText + "的工料机");
  734. }
  735. } else if (dataCode === 'replaceCode' && typeof updateObj.coeType !== 'undefined' && updateObj.coeType === '替换人材机') {
  736. let gljName = me.getGljName(args.editingText, me.gljList);
  737. if (gljName) {
  738. updateObj.replaceCode = args.editingText;
  739. updateObj.replaceName = gljName;
  740. isUpdate = true;
  741. } else {
  742. alert("不存在编号为" + args.editingText + "的工料机");
  743. }
  744. }
  745. else if (dataCode === 'coeType') {
  746. isUpdate = true;
  747. updateObj[dataCode] = args.editingText;
  748. updateObj.gljCode = '';
  749. updateObj.gljName = '';
  750. updateObj.replaceCode = '';
  751. updateObj.replaceName = '';
  752. }
  753. else if (dataCode !== 'gljCode') {
  754. isUpdate = true;
  755. updateObj[dataCode] = args.editingText;
  756. }
  757. }
  758. //insert
  759. else if (args.row >= me.currentGljAdjList.length) {
  760. isUpdate = true;
  761. let newAdjGlj = {};
  762. newAdjGlj[dataCode] = args.editingText;
  763. me.currentGljAdjList.push(newAdjGlj);
  764. }
  765. if (isUpdate) {
  766. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  767. console.log(me.currentGljAdjList);
  768. me.show(me.currentGljAdjList);
  769. });
  770. }
  771. else {
  772. args.sheet.setValue(args.row, args.col, typeof me.currentGljAdjList[args.row] !== 'undefined' && typeof me.currentGljAdjList[args.row][dataCode] !== 'undefined'
  773. ? me.currentGljAdjList[args.row][dataCode] + '' : '');
  774. }
  775. }
  776. }
  777. },
  778. getValidPasteData: function (pasteItems, info) {
  779. let me = gljAdjOprObj;
  780. function isDef(v) {
  781. return v !== undefined && v !== null;
  782. }
  783. // 粘贴项匹配处理
  784. let rules = {
  785. coeType: function (v, cur, tar) {
  786. if (v === '') {
  787. tar.coeType = v;
  788. tar.gljCode = v;
  789. tar.gljName = v;
  790. tar.replaceCode = v;
  791. tar.replaceName = v;
  792. } else if (me.setting.comboItems.coeType.includes(v)) {
  793. tar.coeType = v;
  794. }
  795. },
  796. gljCode: function (v, cur, tar) {
  797. if (v === '') {
  798. tar.gljCode = v;
  799. tar.gljName = v;
  800. } else if (['单个工料机', '替换人材机'].includes(tar.coeType) ||
  801. (!isDef(tar.coeType) && isDef(cur) && ['单个工料机', '替换人材机'].includes(cur.coeType))) {
  802. let gljName = me.getGljName(v, me.gljList);
  803. if (gljName) {
  804. tar.gljCode = v;
  805. tar.gljName = gljName;
  806. }
  807. }
  808. },
  809. amount: function (v, cur, tar) {
  810. /* if (!isNaN(v)) {
  811. tar.amount = v;
  812. } */
  813. if (validateAmount(v)) {
  814. tar.amount = v;
  815. }
  816. },
  817. operator: function (v, cur, tar) {
  818. if (v === '' || me.setting.comboItems.operator.includes(v)) {
  819. tar.operator = v;
  820. }
  821. },
  822. replaceCode: function (v, cur, tar) {
  823. if (v === '') {
  824. tar.replaceCode = v;
  825. tar.replaceName = v;
  826. } else if (tar.coeType === '替换人材机' ||
  827. (!isDef(tar.coeType) && isDef(cur) && cur.coeType === '替换人材机')) {
  828. let replaceName = me.getGljName(v, me.gljList);
  829. if (replaceName) {
  830. tar.replaceCode = v;
  831. tar.replaceName = replaceName;
  832. }
  833. }
  834. }
  835. };
  836. let rst = [];
  837. for (let i = 0, len = pasteItems.length; i < len; i++) {
  838. let row = i + info.cellRange.row;
  839. let target = {},
  840. curObj = me.currentGljAdjList[row],
  841. pasteItem = pasteItems[i];
  842. if (row < me.currentGljAdjList.length) {
  843. target.index = row;//要有下标做为匹配的依据,不然在复制多行并且某个单元格是只读的情况下,这里返回的updateList个数会比选中的行数少,造成更新行和实际不匹配的情况
  844. }
  845. for (let pasteKey in pasteItem) {
  846. if (rules[pasteKey]) {
  847. rules[pasteKey](pasteItem[pasteKey], curObj, target);
  848. }
  849. }
  850. if (Object.keys(target).length > 0) {
  851. rst.push(target);
  852. }
  853. }
  854. return rst;
  855. },
  856. onClipboardPasted: function (sender, info) {
  857. let me = gljAdjOprObj, row;
  858. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  859. let validData = me.getValidPasteData(items, info);
  860. for (let i = 0, len = validData.length; i < len; i++) {
  861. row = i + info.cellRange.row;
  862. //update
  863. if (row < me.currentGljAdjList.length && typeof validData[i].index !== 'undefined') {
  864. let updateObj = me.currentGljAdjList[validData[i].index];//这里改成读取下标
  865. delete validData[i].index; //清除下标
  866. Object.assign(updateObj, validData[i])
  867. }
  868. //insert
  869. else {
  870. me.currentGljAdjList.push(validData[i]);
  871. }
  872. }
  873. if (validData.length > 0) {
  874. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  875. me.show(me.currentGljAdjList);
  876. });
  877. }
  878. else {
  879. me.show(me.currentGljAdjList);
  880. }
  881. },
  882. onDelOpr: function (workBook, setting) {
  883. let me = gljAdjOprObj;
  884. workBook.commandManager().register('gljAdjDel', function () {
  885. let sheet = workBook.getSheet(0);
  886. let sels = sheet.getSelections();
  887. let isUpdate = false;
  888. for (let i = 0, len = sels.length; i < len; i++) {
  889. if (sels[i].colCount === setting.header.length) {//can del
  890. if (sels[i].row < me.currentGljAdjList.length) {
  891. isUpdate = true;
  892. me.currentGljAdjList.splice(sels[i].row, sels[i].rowCount);
  893. }
  894. }
  895. }
  896. if (isUpdate) {
  897. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  898. me.show(me.currentGljAdjList);
  899. });
  900. }
  901. });
  902. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  903. workBook.commandManager().setShortcutKey('gljAdjDel', GC.Spread.Commands.Key.del, false, false, false, false);
  904. },
  905. getGljName: function (gljCode, gljList, withSpecs) {//withSpecs 是否带上规格型号
  906. let rst = null;
  907. for (let i = 0, len = gljList.length; i < len; i++) {
  908. if (gljCode === gljList[i].code) {
  909. rst = withSpecs == true ? gljList[i].name + " - " + gljList[i].specs : gljList[i].name;
  910. break;
  911. }
  912. }
  913. return rst;
  914. },
  915. show: function (coes) {
  916. let me = gljAdjOprObj;
  917. pageObj.showData(me.workSheet, me.setting, coes)
  918. },
  919. getGljItemsOcc: function () {
  920. let me = gljAdjOprObj;
  921. $.ajax({
  922. type: 'post',
  923. url: '/stdGljRepository/api/getGljItemsOccupied',
  924. data: { repId: pageOprObj.gljLibId, occupation: '-_id code name specs' },
  925. dataType: 'json',
  926. timeout: 5000,
  927. success: function (result) {
  928. if (result.error) {
  929. alert(result.message);
  930. } else {
  931. me.gljList = result.data;
  932. }
  933. },
  934. error: function (err) {
  935. alert("内部程序错误!");
  936. }
  937. });
  938. }
  939. };