coe.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. libID: null,
  170. gljLibID: null,
  171. initPage: function (){
  172. var libID = getQueryString("repository");
  173. var libName = storageUtil.getSessionCache("RationGrp","repositoryID_" + libID);
  174. if (libName) {
  175. var html = $("#rationname")[0].outerHTML;
  176. html = html.replace("XXX定额库", libName);
  177. $("#rationname")[0].outerHTML = html;
  178. };
  179. this.gljLibID = storageUtil.getSessionCache("gljLib", "repositoryID_" + libID);
  180. this.libID = libID;
  181. coeOprObj.buildSheet($('#mainSpread')[0]);
  182. gljAdjOprObj.buildSheet($('#contentSpread')[0]);
  183. coeOprObj.getCoeList();
  184. gljAdjOprObj.getGljItemsOcc();
  185. },
  186. showData: function(sheet, setting, data) {
  187. let me = pageObj, ch = GC.Spread.Sheets.SheetArea.viewport;
  188. sheet.suspendPaint();
  189. sheet.suspendEvent();
  190. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  191. sheet.setRowCount(data.length + 3);
  192. for (let col = 0; col < setting.header.length; col++) {
  193. var hAlign = "left", vAlign = "center";
  194. if (setting.header[col].hAlign) {
  195. hAlign = setting.header[col].hAlign;
  196. } else if (setting.header[col].dataType !== "String"){
  197. hAlign = "right";
  198. }
  199. if(setting.header[col].readOnly){
  200. sheet.getRange(-1, col, -1, 1).locked(true);
  201. }
  202. else{
  203. sheet.getRange(-1, col, -1, 1).locked(false);
  204. }
  205. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  206. sheetCommonObj.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  207. if (setting.header[col].formatter) {
  208. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  209. }
  210. for (let row = 0; row < data.length; row++) {
  211. let val = data[row][setting.header[col].dataCode];
  212. sheet.setValue(row, col, val, ch);
  213. }
  214. }
  215. sheet.resumeEvent();
  216. sheet.resumePaint();
  217. }
  218. };
  219. let coeOprObj = {
  220. workBook: null,
  221. workSheet: null,
  222. currentCoeList: [],
  223. currentCoe: null,
  224. currentMaxNo: null,
  225. setting: {
  226. header: [
  227. {headerName:"编号", headerWidth:50, dataCode:"serialNo", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
  228. {headerName:"名称", headerWidth:200, dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center", readOnly: false},
  229. {headerName:"内容", headerWidth:150, dataCode:"content", dataType: "String", hAlign: "left", vAlign: "center", readOnly: false},
  230. {headerName:"原人材机编码", headerWidth:90, dataCode:"original_code", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
  231. {headerName:"可选人材机编码", headerWidth:150, dataCode:"option_codes", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false}
  232. ]
  233. },
  234. buildSheet: function (container) {
  235. let me = coeOprObj;
  236. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  237. sheetCommonObj.bindEscKey(me.workBook, [{sheet: me.workBook.getSheet(0), editStarting: null, editEnded: me.onEditEnded}]);
  238. me.workSheet = me.workBook.getSheet(0);
  239. me.workSheet.options.isProtected = true;
  240. me.onDelOpr(me.workBook, me.setting);
  241. me.workSheet.bind(GC.Spread.Sheets.Events.SelectionChanged, me.onSelectionChanged);
  242. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  243. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  244. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  245. },
  246. onSelectionChanged: function (sender, info) {
  247. let me = coeOprObj, that = gljAdjOprObj;
  248. if(info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row){
  249. let row = info.newSelections[0].row;
  250. if(row < me.currentCoeList.length){
  251. me.currentCoe = me.currentCoeList[row];
  252. that.currentGljAdjList = me.currentCoe.coes;
  253. that.buildDynamicComboBox(that.workSheet);
  254. }
  255. else{
  256. me.currentCoe = null;
  257. that.currentGljAdjList = [];
  258. that.buildBaseCell(that.workSheet);
  259. }
  260. //refresh & show coes
  261. sheetCommonObj.cleanSheet(that.workSheet, that.setting, -1);
  262. me.workBook.focus(true);
  263. that.show(that.currentGljAdjList);
  264. }
  265. },
  266. onEditEnded: function (sender, args) {
  267. let me = coeOprObj, addArr = [], updateArr = [], dataCode = me.setting.header[args.col].dataCode;
  268. if(args.editingText && args.editingText.toString().trim().length > 0){
  269. let inputT = args.editingText.toString().trim();
  270. //update
  271. if(args.row < me.currentCoeList.length){
  272. let updateObj = me.currentCoeList[args.row];
  273. if(updateObj[dataCode] != inputT){
  274. if(dataCode === 'serialNo'){
  275. if(me.isInt(inputT) && !me.hasTisNo(me.currentCoeList, inputT)){
  276. me.currentMaxNo = me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  277. updateObj[dataCode] = inputT;
  278. updateArr.push(updateObj);
  279. me.save([], updateArr, [], true);
  280. }
  281. else if(!me.isInt(inputT)){
  282. alert('编号只能为整数!');
  283. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + '');
  284. }
  285. else if(me.hasTisNo(me.currentCoeList, inputT)){
  286. alert('该编号已存在!');
  287. args.sheet.setValue(args.row, args.col, updateObj[dataCode] + '');
  288. }
  289. }
  290. else {
  291. updateObj[dataCode] = inputT;
  292. me.setOptionList(dataCode,inputT,updateObj);
  293. updateArr.push(updateObj);
  294. me.save([], updateArr, [], true);
  295. }
  296. }
  297. }
  298. //insert
  299. else{
  300. let newCoe = {};
  301. newCoe.libID = pageObj.libID;
  302. if(dataCode === 'serialNo'){
  303. if(me.isInt(inputT) && !me.hasTisNo(me.currentCoeList, inputT)){
  304. me.currentMaxNo = me.currentMaxNo >= inputT ? me.currentMaxNo : inputT;
  305. newCoe[dataCode] = inputT;
  306. addArr.push(newCoe);
  307. me.save(addArr, [], [], true, function (result) {
  308. me.updateCurrentCoeList(result);
  309. });
  310. }
  311. else if(!me.isInt(inputT)){
  312. args.sheet.setValue(args.row, args.col, '');
  313. alert('编号只能为整数!');
  314. }
  315. else if(me.hasTisNo(me.currentCoeList, inputT)){
  316. args.sheet.setValue(args.row, args.col, '');
  317. alert('该编号已存在!');
  318. }
  319. }
  320. else{
  321. newCoe.serialNo = ++me.currentMaxNo;
  322. newCoe[dataCode] = inputT;
  323. me.setOptionList(dataCode,inputT,newCoe);
  324. addArr.push(newCoe);
  325. me.save(addArr, [], [], true, function (result) {
  326. me.updateCurrentCoeList(result);
  327. });
  328. }
  329. }
  330. }
  331. },
  332. setOptionList:function (dataCode,inputT,obj) {
  333. if(dataCode == "option_codes"){//所选人材的情况,要获取人材机下拉列表
  334. inputT = inputT.replace(/[\s\r\n]/g, "")//去掉空格换行等字符
  335. let optionList = [];
  336. let options = inputT.split("|");
  337. for(let code of options){
  338. let name = gljAdjOprObj.getGljName(code,gljAdjOprObj.gljList,true);
  339. if(name) optionList.push({text:name,value:code});
  340. }
  341. obj.option_list = optionList;
  342. }
  343. },
  344. onClipboardPasting: function (sender, info) {
  345. let me = coeOprObj, maxCol = info.cellRange.col + info.cellRange.colCount - 1;
  346. if(maxCol > me.setting.header.length){
  347. info.cancel = true;
  348. }
  349. },
  350. onClipboardPasted: function (sender, info) {
  351. let me = coeOprObj, addArr = [], updateArr = [];
  352. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  353. let uniqItems = me.makeUniqItems(items);
  354. for(let i = 0, len = uniqItems.length; i < len; i++){
  355. let row = i + info.cellRange.row;
  356. //update
  357. if(row < me.currentCoeList.length){
  358. let updateObj = me.currentCoeList[row];
  359. for(let attr in uniqItems[i]){
  360. if(attr === 'serialNo'){
  361. if(me.isInt(uniqItems[i][attr]) && !me.hasTisNo(me.currentCoeList, uniqItems[i][attr])){
  362. me.currentMaxNo = me.currentMaxNo >= uniqItems[i][attr] ? me.currentMaxNo : uniqItems[i][attr];
  363. updateObj[attr] = uniqItems[i][attr];
  364. }
  365. }
  366. else {
  367. updateObj[attr] = uniqItems[i][attr];
  368. }
  369. }
  370. updateArr.push(updateObj);
  371. }
  372. //insert
  373. else {
  374. if(typeof uniqItems[i].serialNo !== 'undefined' && uniqItems[i] && me.isInt(uniqItems[i].serialNo) && !me.hasTisNo(me.currentCoeList, uniqItems[i].serialNo)){
  375. me.currentMaxNo = me.currentMaxNo >= uniqItems[i].serialNo ? me.currentMaxNo : uniqItems[i].serialNo;
  376. }
  377. else {
  378. uniqItems[i].serialNo = ++me.currentMaxNo;
  379. }
  380. uniqItems[i].libID = pageObj.libID;
  381. addArr.push(uniqItems[i]);
  382. }
  383. }
  384. if(addArr.length > 0 || updateArr.length > 0){
  385. me.save(addArr, updateArr, [], true, function (result) {
  386. me.updateCurrentCoeList(result);
  387. });
  388. }
  389. },
  390. onDelOpr: function (workBook, setting) {
  391. let me = coeOprObj, that = gljAdjOprObj;
  392. workBook.commandManager().register('coeListDel', function () {
  393. let deleteArr = [];
  394. let sheet = workBook.getSheet(0);
  395. let sels = sheet.getSelections();
  396. let idx = sels[0].row;
  397. for(let i = 0, len = sels.length; i < len; i++){
  398. if(idx > sels[i].row){
  399. idx = sels[i].row;
  400. }
  401. if(sels[i].colCount === setting.header.length){//can del
  402. for(let r = 0, rLen = sels[i].rowCount; r < rLen; r++){
  403. let row = sels[i].row + r;
  404. if(row < me.currentCoeList.length){
  405. deleteArr.push({libID: me.currentCoeList[row].libID, ID: me.currentCoeList[row].ID});
  406. }
  407. }
  408. me.currentCoeList.splice(sels[i].row, sels[i].rowCount);
  409. }
  410. }
  411. if(deleteArr.length > 0){
  412. me.save([], [], deleteArr, true);
  413. me.currentCoe = typeof me.currentCoeList[idx] !== 'undefined' ? me.currentCoeList[idx] : null;
  414. that.currentGljAdjList = me.currentCoe ? me.currentCoe.coes : [];
  415. gljAdjOprObj.show(that.currentGljAdjList);
  416. }
  417. });
  418. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  419. workBook.commandManager().setShortcutKey('coeListDel', GC.Spread.Commands.Key.del, false, false, false, false);
  420. },
  421. //粘贴的数据,编号唯一化,去除编号重复的项
  422. makeUniqItems: function (items) {
  423. let rst = [];
  424. for(let i = 0, len = items.length; i < len; i++){
  425. if(typeof items[i].serialNo !== 'undefined' && items[i].serialNo){
  426. if(rst.length === 0){
  427. rst.push(items[i]);
  428. }
  429. else{
  430. let isExist = false;
  431. for(let j = 0, jLen = rst.length; j < jLen; j++){
  432. if(items[i].serialNo === rst[j].serialNo){
  433. isExist = true;
  434. break;
  435. }
  436. }
  437. if(!isExist){
  438. rst.push(items[i]);
  439. }
  440. }
  441. }
  442. else {
  443. rst.push(items[i]);
  444. }
  445. }
  446. return rst;
  447. },
  448. isInt: function (num) {
  449. return !isNaN(num) && num % 1 === 0;
  450. },
  451. hasTisNo: function (coeList, newSerialNo) {
  452. let rst = false;
  453. for(let i = 0, len = coeList.length; i < len; i++){
  454. if(coeList[i].serialNo == newSerialNo){
  455. rst = true;
  456. break;
  457. }
  458. }
  459. return rst;
  460. },
  461. updateCurrentCoeList: function (newCoeList) {
  462. let me = coeOprObj;
  463. if(newCoeList){
  464. me.currentCoeList = me.currentCoeList.concat(newCoeList);
  465. }
  466. },
  467. sortCoeList: function (coeList) {
  468. coeList.sort(function (a, b) {
  469. let rst = 0;
  470. if(a.serialNo > b.serialNo) rst = 1;
  471. else if(a.serialNo < b.serialNo) rst = -1;
  472. return rst;
  473. });
  474. },
  475. getCoeList: function () {
  476. let me = coeOprObj;
  477. $.ajax({
  478. type: 'post',
  479. url: '/rationRepository/api/getCoeList',
  480. data: {libID: pageObj.libID},
  481. dataType: 'json',
  482. timeout:20000,
  483. success: function (result) {
  484. if(!result.error){
  485. me.currentCoeList = result.data;
  486. me.sortCoeList(me.currentCoeList);
  487. me.currentMaxNo = me.currentCoeList.length > 0 ? me.currentCoeList[me.currentCoeList.length - 1].serialNo : 0;
  488. pageObj.showData(me.workSheet, me.setting, me.currentCoeList);
  489. me.workSheet.clearSelection();
  490. }
  491. },
  492. error:function(err){
  493. alert("内部程序错误!");
  494. }
  495. });
  496. },
  497. save: function (addArr, updateArr, deleteArr, refresh, callback) {
  498. let me = coeOprObj;
  499. $.ajax({
  500. type:"POST",
  501. url:"api/saveCoeList",
  502. data: {data: JSON.stringify({addArr: addArr, updateArr: updateArr, deleteArr: deleteArr})},
  503. dataType:"json",
  504. timeout:5000,
  505. success:function(result){
  506. if (result.error) {
  507. alert(result.message);
  508. } else{
  509. if(callback){
  510. if(result.message === 'mixed'){
  511. for(let i = 0, len = result.data.length; i < len; i++){
  512. if(result.data[i][0] === 'addSc'){
  513. result.data = result.data[i][1];
  514. break;
  515. }
  516. }
  517. }
  518. callback(result.data);
  519. }
  520. if(refresh){
  521. me.sortCoeList(me.currentCoeList);
  522. me.currentMaxNo = me.currentCoeList.length > 0 ? me.currentCoeList[me.currentCoeList.length - 1].serialNo : 0;
  523. pageObj.showData(me.workSheet, me.setting, me.currentCoeList);
  524. }
  525. }
  526. },
  527. error:function(err){
  528. alert("内部程序错误!");
  529. }
  530. });
  531. }
  532. };
  533. let gljAdjOprObj = {
  534. workBook: null,
  535. workSheet: null,
  536. currentGljAdjList: [],
  537. gljList: [],//只含编号和名称的总工料机列表
  538. setting: {
  539. header: [
  540. {headerName:"调整类型", headerWidth:80, dataCode:"coeType", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
  541. {headerName:"人材机编码", headerWidth:80, dataCode:"gljCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false},
  542. {headerName:"名称", headerWidth:100, dataCode:"gljName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: true},
  543. {headerName:"操作符", headerWidth:60, dataCode:"operator", dataType: "String", hAlign: "center", vAlign: "center", readOnly: false},
  544. {headerName:"数量", headerWidth:80, dataCode:"amount", dataType: "String", hAlign: "center", vAlign: "center" , readOnly: false},
  545. {headerName:"替换为编码", headerWidth:80, dataCode:"replaceCode", dataType: "String", formatter: '@', hAlign: "center", vAlign: "center", readOnly: false},
  546. {headerName:"替换为名称", headerWidth:100, dataCode:"replaceName", dataType: "String", hAlign: "center", vAlign: "center", readOnly: true}
  547. ],
  548. comboItems: {
  549. //调整类型下拉菜单
  550. coeType: ['定额', '人工', '材料', '机械', '主材', '设备', '单个工料机','替换人材机',"所选人材机"],
  551. //操作符下拉菜单
  552. operator: ['+', '-', '*', '/', '=','+*','-*']
  553. }
  554. },
  555. buildSheet: function (container) {
  556. let me = gljAdjOprObj;
  557. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 3);
  558. me.workSheet = me.workBook.getSheet(0);
  559. sheetCommonObj.bindEscKey(me.workBook, [{sheet: me.workBook.getSheet(0), editStarting: me.onEditStart, editEnded: me.onEditEnded}]);
  560. me.workSheet.options.isProtected = true;
  561. me.onDelOpr(me.workBook, me.setting);
  562. me.workSheet.clearSelection();
  563. me.workSheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStart);
  564. me.workSheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  565. me.workSheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
  566. me.workSheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  567. me.workSheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  568. },
  569. buildBaseCell: function (sheet) {
  570. let me = gljAdjOprObj;
  571. sheet.suspendPaint();
  572. sheet.suspendEvent();
  573. let baseCell = GC.Spread.Sheets.CellTypes.Base();
  574. sheet.getCell(-1, 0).cellType(baseCell);
  575. sheet.getCell(-1, 3).cellType(baseCell);
  576. sheet.resumePaint();
  577. sheet.resumeEvent();
  578. },
  579. buildDynamicComboBox: function (sheet) {
  580. let me = gljAdjOprObj;
  581. sheet.suspendPaint();
  582. sheet.suspendEvent();
  583. let dynamicCombo = sheetCommonObj.getDynamicCombo();
  584. dynamicCombo.items(me.setting.comboItems.coeType);
  585. let dynamicOprCombo = sheetCommonObj.getDynamicCombo();
  586. dynamicOprCombo.items(me.setting.comboItems.operator);
  587. sheet.getCell(-1, 0).cellType(dynamicCombo);
  588. sheet.getCell(-1, 3).cellType(dynamicOprCombo);
  589. sheet.resumePaint();
  590. sheet.resumeEvent();
  591. },
  592. onEnterCell: function (sender, args) {
  593. args.sheet.repaint();
  594. },
  595. onEditStart: function (sender, args) {
  596. let me = gljAdjOprObj;
  597. let dataCode = me.setting.header[args.col].dataCode;
  598. if(!coeOprObj.currentCoe || args.row >= me.currentGljAdjList.length ){//超出编辑范围
  599. if(dataCode=== 'gljCode' || dataCode=== 'replaceCode') args.cancel = true;
  600. return;
  601. }
  602. if(dataCode=== 'gljCode' && me.currentGljAdjList[args.row].coeType !== '单个工料机'&& me.currentGljAdjList[args.row].coeType !== '替换人材机'){ //单个人才机和替换人材机,编码才能编辑
  603. args.cancel = true;
  604. return;
  605. }
  606. if(dataCode=== 'replaceCode' && me.currentGljAdjList[args.row].coeType !== '替换人材机'){//替换人材机类型 替换编码才能编辑
  607. args.cancel = true;
  608. }
  609. },
  610. onEditEnded: function (sender, args) {
  611. let me = gljAdjOprObj, isUpdate = false,
  612. dataCode = me.setting.header[args.col].dataCode;
  613. if(args.editingText && args.editingText.toString().trim().length > 0){
  614. if(dataCode === 'amount' && isNaN(args.editingText)){
  615. alert("只能输入数值!");
  616. args.sheet.setValue(args.row, args.col, typeof me.currentGljAdjList[args.row] !== 'undefined' && typeof me.currentGljAdjList[args.row][dataCode] !== 'undefined'
  617. ? me.currentGljAdjList[args.row][dataCode] + '' : '');
  618. }
  619. else {
  620. //update
  621. if(args.row < me.currentGljAdjList.length && args.editingText.toString().trim() !== me.currentGljAdjList[args.row][dataCode]){
  622. let updateObj = me.currentGljAdjList[args.row];
  623. if(dataCode === 'gljCode' && typeof updateObj.coeType !== 'undefined' && (updateObj.coeType === '单个工料机'||updateObj.coeType === '替换人材机')){
  624. let gljName = me.getGljName(args.editingText, me.gljList);
  625. if(gljName){
  626. updateObj.gljCode = args.editingText;
  627. updateObj.gljName = gljName;
  628. isUpdate = true;
  629. } else {
  630. alert("不存在编号为"+ args.editingText +"的工料机");
  631. }
  632. }else if(dataCode === 'replaceCode' && typeof updateObj.coeType !== 'undefined' && updateObj.coeType === '替换人材机'){
  633. let gljName = me.getGljName(args.editingText, me.gljList);
  634. if(gljName){
  635. updateObj.replaceCode = args.editingText;
  636. updateObj.replaceName = gljName;
  637. isUpdate = true;
  638. } else {
  639. alert("不存在编号为"+ args.editingText +"的工料机");
  640. }
  641. }
  642. else if(dataCode === 'coeType'){
  643. isUpdate = true;
  644. updateObj[dataCode] = args.editingText;
  645. updateObj.gljCode = '';
  646. updateObj.gljName = '';
  647. updateObj.replaceCode = '';
  648. updateObj.replaceName = '';
  649. }
  650. else if(dataCode !== 'gljCode') {
  651. isUpdate = true;
  652. updateObj[dataCode] = args.editingText;
  653. }
  654. }
  655. //insert
  656. else if(args.row >= me.currentGljAdjList.length){
  657. isUpdate = true;
  658. let newAdjGlj = {};
  659. newAdjGlj[dataCode] = args.editingText;
  660. me.currentGljAdjList.push(newAdjGlj);
  661. }
  662. if(isUpdate){
  663. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  664. console.log(me.currentGljAdjList);
  665. me.show(me.currentGljAdjList);
  666. });
  667. }
  668. else {
  669. args.sheet.setValue(args.row, args.col, typeof me.currentGljAdjList[args.row] !== 'undefined' && typeof me.currentGljAdjList[args.row][dataCode] !== 'undefined'
  670. ? me.currentGljAdjList[args.row][dataCode] + '' : '');
  671. }
  672. }
  673. }
  674. },
  675. onClipboardPasting: function (sender, info) {
  676. },
  677. getValidPasteDatas: function (pasteItems, info) {//2018-12-21 这里的if else 太多了,不好维护
  678. let me = gljAdjOprObj;
  679. let rst = [];
  680. for(let i = 0, len = pasteItems.length; i < len; i++){
  681. let row = i + info.cellRange.row;
  682. let validObj = {};
  683. //update
  684. if(row < me.currentGljAdjList.length){
  685. let updateObj = me.currentGljAdjList[row];
  686. validObj.index = row;//要有下标做为匹配的依据,不然在复制多行并且某个单元格是只读的情况下,这里返回的updateList个数会比选中的行数少,造成更新行和实际不匹配的情况
  687. if(typeof pasteItems[i].coeType !== 'undefined' && typeof pasteItems[i].gljCode !== 'undefined'){
  688. let gljName = me.getGljName(pasteItems[i].gljCode, me.gljList);
  689. if((updateObj.coeType === '单个工料机'||updateObj.coeType === '替换人材机') && gljName){
  690. validObj.coeType = pasteItems[i].coeType;
  691. validObj.gljCode = pasteItems[i].gljCode;
  692. validObj.gljName = gljName;
  693. }
  694. else if((pasteItems[i].coeType !== '单个工料机'||pasteItems[i].coeType !== '替换人材机') && me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
  695. validObj.coeType = pasteItems[i].coeType;
  696. }
  697. }
  698. else if(typeof pasteItems[i].coeType === 'undefined' && typeof pasteItems[i].gljCode !== 'undefined'){
  699. let gljName = me.getGljName(pasteItems[i].gljCode, me.gljList);
  700. if(typeof updateObj.coeType !== 'undefined' && (updateObj.coeType === '单个工料机'||updateObj.coeType === '替换人材机') && gljName){
  701. validObj.gljCode = pasteItems[i].gljCode;
  702. validObj.gljName = gljName;
  703. }
  704. }
  705. else if(typeof pasteItems[i].coeType !== 'undefined' && typeof pasteItems[i].gljCode === 'undefined'){
  706. if(me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
  707. validObj.coeType = pasteItems[i].coeType;
  708. if(validObj.coeType !== '单个工料机' && typeof updateObj.coeType !== '单个工料机' && updateObj.gljCode.toString().trim().length > 0){
  709. validObj.gljCode = '';
  710. validObj.gljName = '';
  711. }
  712. }
  713. } else {
  714. if(typeof pasteItems[i].operator !== 'undefined' && me.setting.comboItems.operator.indexOf(pasteItems[i].operator) !== -1){
  715. validObj.operator = pasteItems[i].operator;
  716. }
  717. if(typeof pasteItems[i].amount !== 'undefined' && !isNaN(pasteItems[i].amount)){
  718. validObj.amount = pasteItems[i].amount;
  719. }
  720. }
  721. if(typeof pasteItems[i].replaceCode !== 'undefined' && updateObj.coeType === '替换人材机'){
  722. let gljName = me.getGljName(pasteItems[i].replaceCode, me.gljList);
  723. validObj.replaceCode = pasteItems[i].replaceCode;
  724. validObj.replaceName = gljName;
  725. }
  726. }
  727. else {
  728. if(typeof pasteItems[i].coeType !== 'undefined' && typeof pasteItems[i].gljCode !== 'undefined'){
  729. let gljName = me.getGljName(pasteItems[i].gljCode, me.gljList);
  730. if((pasteItems[i].coeType === '单个工料机'||pasteItems[i].coeType === '替换人材机') && gljName){
  731. validObj.coeType = pasteItems[i].coeType;
  732. validObj.gljCode = pasteItems[i].gljCode;
  733. validObj.gljName = gljName;
  734. }
  735. else if((pasteItems[i].coeType !== '单个工料机'||pasteItems[i].coeType !== '替换人材机') && me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
  736. validObj.coeType = pasteItems[i].coeType;
  737. }
  738. }
  739. else if(typeof pasteItems[i].gljCode === 'undefined') {
  740. if(typeof pasteItems[i].coeType !== 'undefined' && me.setting.comboItems.coeType.indexOf(pasteItems[i].coeType) !== -1){
  741. validObj.coeType = pasteItems[i].coeType;
  742. }
  743. if(typeof pasteItems[i].operator !== 'undefined' && me.setting.comboItems.operator.indexOf(pasteItems[i].operator) !== -1){
  744. validObj.operator = pasteItems[i].operator;
  745. }
  746. if(typeof pasteItems[i].amount !== 'undefined' && !isNaN(pasteItems[i].amount)){
  747. validObj.amount = pasteItems[i].amount;
  748. }
  749. }
  750. }
  751. if(Object.keys(validObj).length > 0){
  752. rst.push(validObj);
  753. }
  754. }
  755. return rst;
  756. },
  757. onClipboardPasted: function (sender, info) {
  758. let me = gljAdjOprObj, row;
  759. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  760. let validDatas = me.getValidPasteDatas(items, info);
  761. for(let i = 0, len = validDatas.length; i < len; i++){
  762. row = i + info.cellRange.row;
  763. //update
  764. if(row < me.currentGljAdjList.length && typeof validDatas[i].index !=='undefined'){
  765. let updateObj = me.currentGljAdjList[validDatas[i].index];//这里改成读取下标
  766. delete validDatas[i].index; //清除下标
  767. for(let attr in validDatas[i]){
  768. updateObj[attr] = validDatas[i][attr];
  769. }
  770. }
  771. //insert
  772. else{
  773. me.currentGljAdjList.push(validDatas[i]);
  774. }
  775. }
  776. if(validDatas.length > 0){
  777. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  778. me.show(me.currentGljAdjList);
  779. });
  780. }
  781. else {
  782. me.show(me.currentGljAdjList);
  783. }
  784. },
  785. onDelOpr: function (workBook, setting) {
  786. let me = gljAdjOprObj;
  787. workBook.commandManager().register('gljAdjDel', function () {
  788. let sheet = workBook.getSheet(0);
  789. let sels = sheet.getSelections();
  790. let isUpdate = false;
  791. for(let i = 0, len = sels.length; i < len; i++){
  792. if(sels[i].colCount === setting.header.length){//can del
  793. if(sels[i].row < me.currentGljAdjList.length){
  794. isUpdate = true;
  795. me.currentGljAdjList.splice(sels[i].row, sels[i].rowCount);
  796. }
  797. }
  798. }
  799. if(isUpdate){
  800. coeOprObj.save([], [coeOprObj.currentCoe], [], false, function () {
  801. me.show(me.currentGljAdjList);
  802. });
  803. }
  804. });
  805. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  806. workBook.commandManager().setShortcutKey('gljAdjDel', GC.Spread.Commands.Key.del, false, false, false, false);
  807. },
  808. getGljName: function (gljCode, gljList,withSpecs) {//withSpecs 是否带上规格型号
  809. let rst = null;
  810. for(let i = 0, len = gljList.length; i < len; i++){
  811. if(gljCode === gljList[i].code){
  812. rst = withSpecs == true ?gljList[i].name +" - "+gljList[i].specs :gljList[i].name;
  813. break;
  814. }
  815. }
  816. return rst;
  817. },
  818. show: function (coes) {
  819. let me = gljAdjOprObj;
  820. pageObj.showData(me.workSheet, me.setting, coes)
  821. },
  822. getGljItemsOcc: function () {
  823. let me = gljAdjOprObj;
  824. $.ajax({
  825. type: 'post',
  826. url: '/stdGljRepository/api/getGljItemsOccupied',
  827. data: {repId: pageObj.gljLibID, occupation: '-_id code name specs'},
  828. dataType: 'json',
  829. timeout: 5000,
  830. success:function(result){
  831. if (result.error) {
  832. alert(result.message);
  833. } else{
  834. me.gljList = result.data;
  835. }
  836. },
  837. error:function(err){
  838. alert("内部程序错误!");
  839. }
  840. });
  841. }
  842. };