coe.js 41 KB

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