coe.js 41 KB

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