coe.js 41 KB

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