lossRate.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. let lossObj = {
  2. workBook: null,
  3. workSheet: null,
  4. currentLossList: [],
  5. currentLoss: null,
  6. currentMaxNo: null,
  7. setting: {
  8. header: [
  9. { headerName: "编号", headerWidth: 50, dataCode: "serialNo", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false },
  10. { headerName: "名称", headerWidth: 200, dataCode: "name", dataType: "String", hAlign: "left", vAlign: "center", readOnly: false },
  11. { headerName: "损耗率", headerWidth: 150, dataCode: "rate", dataType: "Number", hAlign: "right", vAlign: "center", readOnly: false },
  12. ]
  13. },
  14. buildSheet: function (container) {
  15. let me = lossObj;
  16. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  17. sheetCommonObj.bindEscKey(me.workBook, [{ sheet: me.workBook.getSheet(0), editStarting: null, editEnded: me.onEditEnded }]);
  18. me.workSheet = me.workBook.getSheet(0);
  19. me.workSheet.options.isProtected = true;
  20. me.onDelOpr(me.workBook, me.setting);
  21. me.workSheet.bind(GC.Spread.Sheets.Events.SelectionChanged, me.onSelectionChanged);
  22. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  23. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  24. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  25. },
  26. onSelectionChanged: function (sender, info) {
  27. if (info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row) {
  28. const row = info.newSelections[0].row;
  29. lossObj.lossSelInit(row);
  30. }
  31. },
  32. lossSelInit: function (row) {
  33. const me = lossObj;
  34. if (row < me.currentLossList.length) {
  35. me.currentLoss = me.currentLossList[row];
  36. } else {
  37. me.currentLoss = null;
  38. }
  39. },
  40. onEditEnded: function (sender, args) {
  41. let me = lossObj, addArr = [], updateArr = [], dataCode = me.setting.header[args.col].dataCode;
  42. if (args.editingText && args.editingText.toString().trim().length > 0) {
  43. let inputT = args.editingText.toString().trim();
  44. //update
  45. if (args.row < me.currentLossList.length) {
  46. let updateObj = me.currentLossList[args.row];
  47. if (updateObj[dataCode] != inputT) {
  48. if (dataCode === 'serialNo') {
  49. if (me.isInt(inputT) && !me.hasTisNo(me.currentLossList, inputT)) {
  50. me.currentMaxNo = me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  51. updateObj[dataCode] = inputT;
  52. updateArr.push(updateObj);
  53. me.save([], updateArr, [], true);
  54. }
  55. else if (!me.isInt(inputT)) {
  56. alert('编号只能为整数!');
  57. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + '');
  58. }
  59. else if (me.hasTisNo(me.currentLossList, inputT)) {
  60. alert('该编号已存在!');
  61. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + '');
  62. }
  63. } else if (dataCode === 'rate' && isNaN(inputT)) {
  64. alert('损耗率只能为数值!');
  65. args.sheet.setValue(args.row, args.col, updateObj[dataCode] || '');
  66. }
  67. else {
  68. updateObj[dataCode] = inputT;
  69. updateArr.push(updateObj);
  70. me.save([], updateArr, [], true);
  71. }
  72. }
  73. }
  74. //insert
  75. else {
  76. let newLoss = {};
  77. newLoss.libID = pageOprObj.rationLibId;
  78. if (dataCode === 'serialNo') {
  79. if (me.isInt(inputT) && !me.hasTisNo(me.currentLossList, inputT)) {
  80. me.currentMaxNo = me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  81. newLoss[dataCode] = inputT;
  82. addArr.push(newLoss);
  83. me.save(addArr, [], [], true, function (result) {
  84. me.updateCurrentLossList(result);
  85. });
  86. }
  87. else if (!me.isInt(inputT)) {
  88. args.sheet.setValue(args.row, args.col, '');
  89. alert('编号只能为整数!');
  90. }
  91. else if (me.hasTisNo(me.currentLossList, inputT)) {
  92. args.sheet.setValue(args.row, args.col, '');
  93. alert('该编号已存在!');
  94. }
  95. }
  96. else if (dataCode === 'rate' && isNaN(inputT)) {
  97. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + '');
  98. alert('损耗率只能为数值!');
  99. }
  100. else {
  101. newLoss.serialNo = ++me.currentMaxNo;
  102. newLoss[dataCode] = inputT;
  103. addArr.push(newLoss);
  104. me.save(addArr, [], [], true, function (result) {
  105. me.updateCurrentLossList(result);
  106. });
  107. }
  108. }
  109. }
  110. },
  111. onClipboardPasting: function (sender, info) {
  112. let me = lossObj, maxCol = info.cellRange.col + info.cellRange.colCount - 1;
  113. if (maxCol > me.setting.header.length) {
  114. info.cancel = true;
  115. }
  116. },
  117. onClipboardPasted: function (sender, info) {
  118. debugger;
  119. let me = lossObj, addArr = [], updateArr = [];
  120. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  121. let uniqItems = me.makeUniqItems(items);
  122. for (let i = 0, len = uniqItems.length; i < len; i++) {
  123. let row = i + info.cellRange.row;
  124. //update
  125. if (row < me.currentLossList.length) {
  126. let updateObj = me.currentLossList[row];
  127. for (let attr in uniqItems[i]) {
  128. if (attr === 'serialNo') {
  129. if (me.isInt(uniqItems[i][attr]) && !me.hasTisNo(me.currentLossList, uniqItems[i][attr])) {
  130. me.currentMaxNo = me.currentMaxNo >= uniqItems[i][attr] ? me.currentMaxNo : uniqItems[i][attr];
  131. updateObj[attr] = uniqItems[i][attr];
  132. }
  133. }
  134. else if (attr !== 'rate' || !isNaN(uniqItems[i][attr])) {
  135. updateObj[attr] = uniqItems[i][attr];
  136. }
  137. }
  138. updateArr.push(updateObj);
  139. }
  140. //insert
  141. else {
  142. if (typeof uniqItems[i].serialNo !== 'undefined' && uniqItems[i] && me.isInt(uniqItems[i].serialNo) && !me.hasTisNo(me.currentLossList, uniqItems[i].serialNo)) {
  143. me.currentMaxNo = me.currentMaxNo >= uniqItems[i].serialNo ? me.currentMaxNo : uniqItems[i].serialNo;
  144. }
  145. else {
  146. uniqItems[i].serialNo = ++me.currentMaxNo;
  147. }
  148. if (typeof uniqItems[i].rate !== 'undefined' && isNaN(uniqItems[i].rate)) {
  149. delete uniqItems[i].rate;
  150. }
  151. uniqItems[i].libID = pageOprObj.rationLibId;
  152. addArr.push(uniqItems[i]);
  153. }
  154. }
  155. if (addArr.length > 0 || updateArr.length > 0) {
  156. me.save(addArr, updateArr, [], true, function (result) {
  157. me.updateCurrentLossList(result);
  158. });
  159. }
  160. },
  161. onDelOpr: function (workBook, setting) {
  162. let me = lossObj;
  163. workBook.commandManager().register('coeListDel', function () {
  164. let deleteArr = [];
  165. let sheet = workBook.getSheet(0);
  166. let sels = sheet.getSelections();
  167. let idx = sels[0].row;
  168. for (let i = 0, len = sels.length; i < len; i++) {
  169. if (idx > sels[i].row) {
  170. idx = sels[i].row;
  171. }
  172. if (sels[i].colCount === setting.header.length) {//can del
  173. for (let r = 0, rLen = sels[i].rowCount; r < rLen; r++) {
  174. let row = sels[i].row + r;
  175. if (row < me.currentLossList.length) {
  176. deleteArr.push({ libID: me.currentLossList[row].libID, ID: me.currentLossList[row].ID });
  177. }
  178. }
  179. me.currentLossList.splice(sels[i].row, sels[i].rowCount);
  180. }
  181. }
  182. if (deleteArr.length > 0) {
  183. me.save([], [], deleteArr, true);
  184. me.currentLoss = typeof me.currentLossList[idx] !== 'undefined' ? me.currentLossList[idx] : null;
  185. }
  186. });
  187. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  188. workBook.commandManager().setShortcutKey('coeListDel', GC.Spread.Commands.Key.del, false, false, false, false);
  189. },
  190. //粘贴的数据,编号唯一化,去除编号重复的项
  191. makeUniqItems: function (items) {
  192. let rst = [];
  193. for (let i = 0, len = items.length; i < len; i++) {
  194. if (typeof items[i].serialNo !== 'undefined' && items[i].serialNo) {
  195. if (rst.length === 0) {
  196. rst.push(items[i]);
  197. }
  198. else {
  199. let isExist = false;
  200. for (let j = 0, jLen = rst.length; j < jLen; j++) {
  201. if (items[i].serialNo === rst[j].serialNo) {
  202. isExist = true;
  203. break;
  204. }
  205. }
  206. if (!isExist) {
  207. rst.push(items[i]);
  208. }
  209. }
  210. }
  211. else {
  212. rst.push(items[i]);
  213. }
  214. }
  215. return rst;
  216. },
  217. isInt: function (num) {
  218. return !isNaN(num) && num % 1 === 0;
  219. },
  220. hasTisNo: function (lossList, newSerialNo) {
  221. let rst = false;
  222. for (let i = 0, len = lossList.length; i < len; i++) {
  223. if (lossList[i].serialNo == newSerialNo) {
  224. rst = true;
  225. break;
  226. }
  227. }
  228. return rst;
  229. },
  230. updateCurrentLossList: function (newCoeList) {
  231. let me = lossObj;
  232. if (newCoeList) {
  233. me.currentLossList = me.currentLossList.concat(newCoeList);
  234. }
  235. },
  236. sortLossList: function (lossList) {
  237. lossList.sort(function (a, b) {
  238. let rst = 0;
  239. if (a.serialNo > b.serialNo) rst = 1;
  240. else if (a.serialNo < b.serialNo) rst = -1;
  241. return rst;
  242. });
  243. },
  244. getLossList: function () {
  245. let me = lossObj;
  246. $.ajax({
  247. type: 'post',
  248. url: '/rationRepository/api/getLossList',
  249. data: { libID: pageOprObj.rationLibId },
  250. dataType: 'json',
  251. timeout: 20000,
  252. success: function (result) {
  253. if (!result.error) {
  254. me.currentLossList = result.data;
  255. me.sortLossList(me.currentLossList);
  256. me.currentMaxNo = me.currentLossList.length > 0 ? me.currentLossList[me.currentLossList.length - 1].serialNo : 0;
  257. pageObj.showData(me.workSheet, me.setting, me.currentLossList);
  258. me.workSheet.clearSelection();
  259. }
  260. },
  261. error: function (err) {
  262. alert("内部程序错误!");
  263. }
  264. });
  265. },
  266. updateRationGljs: function (updateMap, rationGljs) {
  267. if (!rationGljs || !rationGljs.length) {
  268. return;
  269. }
  270. rationGljs.forEach(glj => {
  271. const match = updateMap[glj.lossRateID];
  272. if (match) {
  273. glj.lossRateNo = match.serialNo;
  274. glj.lossRateName = match.name;
  275. glj.lossRate = match.rate;
  276. }
  277. })
  278. },
  279. updateRationAndGljCache: function (updateRateData) {
  280. debugger;
  281. const updateMap = {};
  282. updateRateData.forEach(item => {
  283. updateMap[item.ID] = item;
  284. });
  285. const rations = Object.values(rationOprObj.currentRations).flat();
  286. rations.forEach(ration => {
  287. lossObj.updateRationGljs(updateMap, ration.rationGljList);
  288. })
  289. const rGljList = Object.values(rationGLJOprObj.cache).flat();
  290. lossObj.updateRationGljs(updateMap, rGljList);
  291. if (rationGLJOprObj.currentRationItem) {
  292. rationGLJOprObj.showGljItems(rationGLJOprObj.currentRationItem.ID);
  293. }
  294. },
  295. save: function (addArr, updateArr, deleteArr, refresh, callback) {
  296. let me = lossObj;
  297. $.ajax({
  298. type: "POST",
  299. url: "api/saveLossList",
  300. data: { data: JSON.stringify({ addArr: addArr, updateArr: updateArr, deleteArr: deleteArr }) },
  301. dataType: "json",
  302. timeout: 5000,
  303. success: function (result) {
  304. if (result.error) {
  305. alert(result.message);
  306. } else {
  307. debugger;
  308. if (updateArr && updateArr.length) {
  309. lossObj.updateRationAndGljCache(updateArr);
  310. }
  311. if (callback) {
  312. callback(result.data);
  313. }
  314. if (refresh) {
  315. me.sortLossList(me.currentLossList);
  316. me.currentMaxNo = me.currentLossList.length > 0 ? me.currentLossList[me.currentLossList.length - 1].serialNo : 0;
  317. pageObj.showData(me.workSheet, me.setting, me.currentLossList);
  318. }
  319. }
  320. },
  321. error: function (err) {
  322. alert("内部程序错误!");
  323. }
  324. });
  325. }
  326. }
  327. $(document).ready(function () {
  328. //设置水平拖动条的宽度
  329. //@param {Object dom}resize滚动条
  330. function setResizeWidth(resize) {
  331. const fixedWidth = 10;
  332. //跟滚动条同层的其他节点
  333. let bros = resize.parent().children();
  334. //滚动条节点 及 同层非滚动条节点的索引
  335. let index = bros.index(resize),
  336. otherIndex = index ? 0 : 2;
  337. const other = resize.parent().children(`:eq(${otherIndex})`);
  338. let resizeParentWidth = resize.parent().width();
  339. let resizeDecimalWidth = fixedWidth / resizeParentWidth,
  340. otherDecimalWidth = 1 - resizeDecimalWidth;
  341. let resizePercentWidth = resizeDecimalWidth * 100 + '%',
  342. otherPercentWidth = otherDecimalWidth * 100 + '%';
  343. resize.css('width', resizePercentWidth);
  344. other.css('width', otherPercentWidth);
  345. }
  346. function refreshAfterShow(visible) {
  347. const min = 20;
  348. //宽度比例localstorage key
  349. let leftContentKey = `${moduleName}${$('#leftContent').attr('id')}Width`,
  350. mainContentKey = `${moduleName}${$('#mainContent').attr('id')}Width`,
  351. lossContentKey = `${moduleName}${$('#rightContent').attr('id')}Width`;
  352. let lossWidth = getLocalCache(lossContentKey) ? getLocalCache(lossContentKey) : $('#rightContent')[0].style.width,
  353. mainContentWidth = $('#mainContent')[0].style.width,
  354. leftContentWidth;
  355. lossWidth = parseFloat(lossWidth.replace('%', ''));
  356. mainContentWidth = parseFloat(mainContentWidth.replace('%', ''));
  357. if (visible) {
  358. mainContentWidth = mainContentWidth - lossWidth / 2 < min ? min : mainContentWidth - lossWidth / 2;
  359. if (100 - mainContentWidth - lossWidth < min) {
  360. leftContentWidth = min;
  361. lossWidth = 100 - mainContentWidth - leftContentWidth;
  362. } else {
  363. leftContentWidth = 100 - mainContentWidth - lossWidth;
  364. }
  365. } else {
  366. mainContentWidth += lossWidth / 2;
  367. leftContentWidth = 100 - mainContentWidth;
  368. }
  369. $('#leftContent').css('width', `${leftContentWidth}%`);
  370. setLocalCache(leftContentKey, `${leftContentWidth}%`);
  371. $('#mainContent').css('width', `${mainContentWidth}%`);
  372. setLocalCache(mainContentKey, `${mainContentWidth}%`);
  373. $('#rightContent').css('width', `${lossWidth}%`);
  374. setLocalCache(lossContentKey, `${lossWidth}%`);
  375. let resizes = [$('#slideResizeLeft'), $('#slideResizeRight')];
  376. for (let resize of resizes) {
  377. setResizeWidth(resize);
  378. }
  379. sectionTreeObj.loadRateWidth();
  380. }
  381. $('#loss').click(function () {
  382. if (!$(this).hasClass('active')) {
  383. $('#zmhs').removeClass('active');
  384. $(this).addClass('active');
  385. refreshAfterShow(true);
  386. $('#lossWrap').show();
  387. $('#zmhsWrap').hide();
  388. $('#rightContent').show();
  389. if (!lossObj.workBook) {
  390. pageObj.initPage();
  391. }
  392. refreshALlWorkBook();
  393. } else {
  394. $(this).removeClass('active');
  395. refreshAfterShow(false);
  396. $('#rightContent').hide();
  397. $('#zmhsWrap').hide();
  398. $('#lossWrap').hide();
  399. refreshALlWorkBook();
  400. }
  401. });
  402. var pageObj = {
  403. initPage: function () {
  404. lossObj.buildSheet($('#lossSpread')[0]);
  405. lossObj.getLossList();
  406. lockUtil.lockSpreads([lossObj.workBook], locked);
  407. },
  408. showData: function (sheet, setting, data) {
  409. let me = pageObj, ch = GC.Spread.Sheets.SheetArea.viewport;
  410. sheet.suspendPaint();
  411. sheet.suspendEvent();
  412. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  413. sheet.setRowCount(data.length + 3);
  414. for (let col = 0; col < setting.header.length; col++) {
  415. var hAlign = "left", vAlign = "center";
  416. if (setting.header[col].hAlign) {
  417. hAlign = setting.header[col].hAlign;
  418. } else if (setting.header[col].dataType !== "String") {
  419. hAlign = "right";
  420. }
  421. if (setting.header[col].readOnly) {
  422. sheet.getRange(-1, col, -1, 1).locked(true);
  423. }
  424. else {
  425. sheet.getRange(-1, col, -1, 1).locked(false);
  426. }
  427. vAlign = setting.header[col].vAlign ? setting.header[col].vAlign : vAlign;
  428. sheetCommonObj.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  429. if (setting.header[col].formatter) {
  430. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  431. }
  432. for (let row = 0; row < data.length; row++) {
  433. let val = data[row][setting.header[col].dataCode];
  434. sheet.setValue(row, col, val, ch);
  435. }
  436. }
  437. sheet.resumeEvent();
  438. sheet.resumePaint();
  439. }
  440. };
  441. });