coe.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. let gljAdjOprObj = {
  587. workBook: null,
  588. workSheet: null,
  589. currentGljAdjList: [],
  590. gljList: [],//只含编号和名称的总工料机列表
  591. setting: {
  592. header: [
  593. {headerName:"调整类型", headerWidth:80, dataCode:"coeType", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
  594. {headerName:"人材机编码", headerWidth:80, dataCode:"gljCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false},
  595. // readOnly: true --> false,需要兼容粘贴列包含只读项
  596. {headerName:"名称", headerWidth:100, dataCode:"gljName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
  597. {headerName:"操作符", headerWidth:60, dataCode:"operator", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
  598. {headerName:"数量", headerWidth:80, dataCode:"amount", dataType: "String", hAlign: "center", vAlign: "center" , readOnly: false},
  599. {headerName:"替换为编码", headerWidth:80, dataCode:"replaceCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false},
  600. // readOnly: true --> false
  601. {headerName:"替换为名称", headerWidth:100, dataCode:"replaceName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false}
  602. ],
  603. comboItems: {
  604. //调整类型下拉菜单
  605. coeType: ['定额', '人工', '材料', '机械', '主材', '设备', '单个工料机','替换人材机',"所选人材机"],
  606. //操作符下拉菜单
  607. operator: ['+', '-', '*', '/', '=','+*','-*']
  608. }
  609. },
  610. buildSheet: function (container) {
  611. let me = gljAdjOprObj;
  612. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 3);
  613. me.workSheet = me.workBook.getSheet(0);
  614. sheetCommonObj.bindEscKey(me.workBook, [{sheet: me.workBook.getSheet(0), editStarting: me.onEditStart, editEnded: me.onEditEnded}]);
  615. me.workSheet.options.isProtected = true;
  616. me.onDelOpr(me.workBook, me.setting);
  617. me.workSheet.clearSelection();
  618. me.workSheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStart);
  619. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  620. me.workSheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
  621. me.workSheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  622. },
  623. buildBaseCell: function (sheet) {
  624. let me = gljAdjOprObj;
  625. sheet.suspendPaint();
  626. sheet.suspendEvent();
  627. let baseCell = GC.Spread.Sheets.CellTypes.Base();
  628. sheet.getCell(-1, 0).cellType(baseCell);
  629. sheet.getCell(-1, 3).cellType(baseCell);
  630. sheet.resumePaint();
  631. sheet.resumeEvent();
  632. },
  633. buildDynamicComboBox: function (sheet) {
  634. let me = gljAdjOprObj;
  635. sheet.suspendPaint();
  636. sheet.suspendEvent();
  637. let dynamicCombo = sheetCommonObj.getDynamicCombo();
  638. dynamicCombo.items(me.setting.comboItems.coeType);
  639. let dynamicOprCombo = sheetCommonObj.getDynamicCombo();
  640. dynamicOprCombo.items(me.setting.comboItems.operator);
  641. sheet.getCell(-1, 0).cellType(dynamicCombo);
  642. sheet.getCell(-1, 3).cellType(dynamicOprCombo);
  643. sheet.resumePaint();
  644. sheet.resumeEvent();
  645. },
  646. onEnterCell: function (sender, args) {
  647. args.sheet.repaint();
  648. },
  649. onEditStart: function (sender, args) {
  650. let me = gljAdjOprObj;
  651. let dataCode = me.setting.header[args.col].dataCode;
  652. // 超出范围-没有选中的调整主项
  653. if (!coeOprObj.currentCoe) {
  654. args.cancel = true;
  655. return;
  656. }
  657. // 名称、替换为名称总是不可编辑(表头设置readOnly没有设置为true,因为需要粘贴)
  658. let alwaysNotEditable = ['gljName', 'replaceName'];
  659. if (alwaysNotEditable.includes(dataCode)) {
  660. args.cancel = true;
  661. return;
  662. }
  663. // 新一行编辑时,除了总是可编辑的单元格:调整类型、操作符、数量,其他不可编辑
  664. let alwaysEditable = ['coeType', 'operator', 'amount'];
  665. if (args.row > me.currentGljAdjList.length - 1 &&
  666. !alwaysEditable.includes(dataCode)) {
  667. args.cancel = true;
  668. return;
  669. }
  670. let curSubCoe = me.currentGljAdjList[args.row];
  671. if (!curSubCoe) {
  672. return;
  673. }
  674. // 调整类型不为“单个工料机”和“替换人材机”时,人材机编码不可编辑
  675. if (!['单个工料机', '替换人材机'].includes(curSubCoe.coeType) && dataCode === 'gljCode') {
  676. args.cancel = true;
  677. return;
  678. }
  679. // 调整类型不为“替换人材机”时,替换为编码不可编辑
  680. if (curSubCoe.coeType !== '替换人材机' && dataCode === 'replaceCode') {
  681. args.cancel = true;
  682. return;
  683. }
  684. },
  685. onEditEnded: function (sender, args) {
  686. let me = gljAdjOprObj, isUpdate = false,
  687. dataCode = me.setting.header[args.col].dataCode;
  688. if(args.editingText && args.editingText.toString().trim().length > 0){
  689. if(dataCode === 'amount' && isNaN(args.editingText)){
  690. alert("只能输入数值!");
  691. args.sheet.setValue(args.row, args.col, typeof me.currentGljAdjList[args.row] !== 'undefined' && typeof me.currentGljAdjList[args.row][dataCode] !== 'undefined'
  692. ? me.currentGljAdjList[args.row][dataCode] + '' : '');
  693. }
  694. else {
  695. //update
  696. if(args.row < me.currentGljAdjList.length && args.editingText.toString().trim() !== me.currentGljAdjList[args.row][dataCode]){
  697. let updateObj = me.currentGljAdjList[args.row];
  698. if(dataCode === 'gljCode' && typeof updateObj.coeType !== 'undefined' && (updateObj.coeType === '单个工料机'||updateObj.coeType === '替换人材机')){
  699. let gljName = me.getGljName(args.editingText, me.gljList);
  700. if(gljName){
  701. updateObj.gljCode = args.editingText;
  702. updateObj.gljName = gljName;
  703. isUpdate = true;
  704. } else {
  705. alert("不存在编号为"+ args.editingText +"的工料机");
  706. }
  707. }else if(dataCode === 'replaceCode' && typeof updateObj.coeType !== 'undefined' && updateObj.coeType === '替换人材机'){
  708. let gljName = me.getGljName(args.editingText, me.gljList);
  709. if(gljName){
  710. updateObj.replaceCode = args.editingText;
  711. updateObj.replaceName = gljName;
  712. isUpdate = true;
  713. } else {
  714. alert("不存在编号为"+ args.editingText +"的工料机");
  715. }
  716. }
  717. else if(dataCode === 'coeType'){
  718. isUpdate = true;
  719. updateObj[dataCode] = args.editingText;
  720. updateObj.gljCode = '';
  721. updateObj.gljName = '';
  722. updateObj.replaceCode = '';
  723. updateObj.replaceName = '';
  724. }
  725. else if(dataCode !== 'gljCode') {
  726. isUpdate = true;
  727. updateObj[dataCode] = args.editingText;
  728. }
  729. }
  730. //insert
  731. else if(args.row >= me.currentGljAdjList.length){
  732. isUpdate = true;
  733. let newAdjGlj = {};
  734. newAdjGlj[dataCode] = args.editingText;
  735. me.currentGljAdjList.push(newAdjGlj);
  736. }
  737. if(isUpdate){
  738. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  739. console.log(me.currentGljAdjList);
  740. me.show(me.currentGljAdjList);
  741. });
  742. }
  743. else {
  744. args.sheet.setValue(args.row, args.col, typeof me.currentGljAdjList[args.row] !== 'undefined' && typeof me.currentGljAdjList[args.row][dataCode] !== 'undefined'
  745. ? me.currentGljAdjList[args.row][dataCode] + '' : '');
  746. }
  747. }
  748. }
  749. },
  750. getValidPasteData: function (pasteItems, info) {
  751. let me = gljAdjOprObj;
  752. function isDef(v) {
  753. return v !== undefined && v !== null;
  754. }
  755. // 粘贴项匹配处理
  756. let rules = {
  757. coeType: function (v, cur, tar) {
  758. if (v === '') {
  759. tar.coeType = v;
  760. tar.gljCode = v;
  761. tar.gljName = v;
  762. tar.replaceCode = v;
  763. tar.replaceName = v;
  764. } else if (me.setting.comboItems.coeType.includes(v)) {
  765. tar.coeType = v;
  766. }
  767. },
  768. gljCode: function (v, cur, tar) {
  769. if (v === '') {
  770. tar.gljCode = v;
  771. tar.gljName = v;
  772. } else if (['单个工料机', '替换人材机'].includes(tar.coeType) ||
  773. (!isDef(tar.coeType) && isDef(cur) && ['单个工料机', '替换人材机'].includes(cur.coeType))) {
  774. let gljName = me.getGljName(v, me.gljList);
  775. if (gljName) {
  776. tar.gljCode = v;
  777. tar.gljName = gljName;
  778. }
  779. }
  780. },
  781. amount: function (v, cur, tar) {
  782. if (!isNaN(v)) {
  783. tar.amount = v;
  784. }
  785. },
  786. operator: function (v, cur, tar) {
  787. if (v === '' || me.setting.comboItems.operator.includes(v)) {
  788. tar.operator = v;
  789. }
  790. },
  791. replaceCode: function (v, cur, tar) {
  792. if (v === '') {
  793. tar.replaceCode = v;
  794. tar.replaceName = v;
  795. } else if (tar.coeType === '替换人材机' ||
  796. (!isDef(tar.coeType) && isDef(cur) && cur.coeType === '替换人材机')) {
  797. let replaceName = me.getGljName(v, me.gljList);
  798. if (replaceName) {
  799. tar.replaceCode = v;
  800. tar.replaceName = replaceName;
  801. }
  802. }
  803. }
  804. };
  805. let rst = [];
  806. for(let i = 0, len = pasteItems.length; i < len; i++){
  807. let row = i + info.cellRange.row;
  808. let target = {},
  809. curObj = me.currentGljAdjList[row],
  810. pasteItem = pasteItems[i];
  811. if(row < me.currentGljAdjList.length){
  812. target.index = row;//要有下标做为匹配的依据,不然在复制多行并且某个单元格是只读的情况下,这里返回的updateList个数会比选中的行数少,造成更新行和实际不匹配的情况
  813. }
  814. for (let pasteKey in pasteItem) {
  815. if (rules[pasteKey]) {
  816. rules[pasteKey](pasteItem[pasteKey], curObj, target);
  817. }
  818. }
  819. if(Object.keys(target).length > 0){
  820. rst.push(target);
  821. }
  822. }
  823. return rst;
  824. },
  825. onClipboardPasted: function (sender, info) {
  826. let me = gljAdjOprObj, row;
  827. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  828. let validData = me.getValidPasteData(items, info);
  829. for(let i = 0, len = validData.length; i < len; i++){
  830. row = i + info.cellRange.row;
  831. //update
  832. if(row < me.currentGljAdjList.length && typeof validData[i].index !=='undefined'){
  833. let updateObj = me.currentGljAdjList[validData[i].index];//这里改成读取下标
  834. delete validData[i].index; //清除下标
  835. Object.assign(updateObj, validData[i])
  836. }
  837. //insert
  838. else{
  839. me.currentGljAdjList.push(validData[i]);
  840. }
  841. }
  842. if(validData.length > 0){
  843. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  844. me.show(me.currentGljAdjList);
  845. });
  846. }
  847. else {
  848. me.show(me.currentGljAdjList);
  849. }
  850. },
  851. onDelOpr: function (workBook, setting) {
  852. let me = gljAdjOprObj;
  853. workBook.commandManager().register('gljAdjDel', function () {
  854. let sheet = workBook.getSheet(0);
  855. let sels = sheet.getSelections();
  856. let isUpdate = false;
  857. for(let i = 0, len = sels.length; i < len; i++){
  858. if(sels[i].colCount === setting.header.length){//can del
  859. if(sels[i].row < me.currentGljAdjList.length){
  860. isUpdate = true;
  861. me.currentGljAdjList.splice(sels[i].row, sels[i].rowCount);
  862. }
  863. }
  864. }
  865. if(isUpdate){
  866. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  867. me.show(me.currentGljAdjList);
  868. });
  869. }
  870. });
  871. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  872. workBook.commandManager().setShortcutKey('gljAdjDel', GC.Spread.Commands.Key.del, false, false, false, false);
  873. },
  874. getGljName: function (gljCode, gljList,withSpecs) {//withSpecs 是否带上规格型号
  875. let rst = null;
  876. for(let i = 0, len = gljList.length; i < len; i++){
  877. if(gljCode === gljList[i].code){
  878. rst = withSpecs == true ?gljList[i].name +" - "+gljList[i].specs :gljList[i].name;
  879. break;
  880. }
  881. }
  882. return rst;
  883. },
  884. show: function (coes) {
  885. let me = gljAdjOprObj;
  886. pageObj.showData(me.workSheet, me.setting, coes)
  887. },
  888. getGljItemsOcc: function () {
  889. let me = gljAdjOprObj;
  890. $.ajax({
  891. type: 'post',
  892. url: '/stdGljRepository/api/getGljItemsOccupied',
  893. data: {repId: pageOprObj.gljLibId, occupation: '-_id code name specs'},
  894. dataType: 'json',
  895. timeout: 5000,
  896. success:function(result){
  897. if (result.error) {
  898. alert(result.message);
  899. } else{
  900. me.gljList = result.data;
  901. }
  902. },
  903. error:function(err){
  904. alert("内部程序错误!");
  905. }
  906. });
  907. }
  908. };