ration.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. $("#gongliao").click(function(){
  5. $(this).attr('href', "/rationRepository/lmm" + "?repository=" + getQueryString("repository"))
  6. });
  7. $("#fuzhu").click(function(){
  8. $(this).attr('href', "/rationRepository/coeList" + "?repository=" + getQueryString("repository"))
  9. });
  10. var rationOprObj = {
  11. workBook: null,
  12. currentRations: {},
  13. currentEditingRation: null,
  14. currentSectionId: -1,
  15. rationsCodes: [],
  16. setting: {
  17. header:[
  18. {headerName:"编码",headerWidth:120,dataCode:"code", dataType: "String", formatter: "@"},
  19. {headerName:"名称",headerWidth:300,dataCode:"name", dataType: "String"},
  20. {headerName:"单位",headerWidth:120,dataCode:"unit", dataType: "String", hAlign: "center"},
  21. {headerName:"人工费",headerWidth:120,dataCode:"labourPrice", dataType: "Number", formatter: "0.00", hAlign: "right"},
  22. {headerName:"材料费",headerWidth:120,dataCode:"materialPrice", dataType: "Number", formatter: "0.00", hAlign: "right"},
  23. {headerName:"机械费",headerWidth:120,dataCode:"machinePrice", dataType: "Number", formatter: "0.00", hAlign: "right"},
  24. {headerName:"基价",headerWidth:120,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right"},
  25. {headerName:"显示名称(以%s表示参数)",headerWidth:350,dataCode:"caption", dataType: "String"},
  26. {headerName:"取费专业",headerWidth:120,dataCode:"feeType", dataType: "Number", hAlign: "center"}
  27. ],
  28. view:{
  29. comboBox:[
  30. {row:-1,col:2,rowCount:-1,colCount:1}
  31. ],
  32. lockedCells:[
  33. {row:-1,col:3,rowCount:-1, colCount:1}
  34. ],
  35. lockColumns: [
  36. 3, 4, 5, 6
  37. ]
  38. }
  39. },
  40. buildSheet: function(container) {
  41. let rationRepId = getQueryString("repository");
  42. var me = rationOprObj;
  43. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  44. me.workBook.options.showHorizontalScrollbar = true;
  45. me.getRationsCodes(rationRepId);
  46. me.rationDelOpr();
  47. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  48. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  49. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
  50. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  51. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  52. //me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  53. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.CellClick, me.onCellClick);
  54. },
  55. onCellClick: function(sender, args) {
  56. var me = rationOprObj,
  57. sheetGLJ = rationGLJOprObj.sheet, settingGLJ = rationGLJOprObj.setting,
  58. sheetCoe = rationCoeOprObj.sheet, settingCoe = rationCoeOprObj.setting,
  59. sheetAss = rationAssistOprObj.sheet, settingAss = rationAssistOprObj.setting;
  60. sheetCommonObj.cleanSheet(sheetGLJ, settingGLJ, -1);
  61. sheetCommonObj.shieldAllCells(sheetGLJ);
  62. sheetCommonObj.cleanSheet(sheetCoe, settingCoe, -1);
  63. sheetCommonObj.shieldAllCells(sheetCoe);
  64. sheetCommonObj.cleanSheet(sheetAss, settingAss, -1);
  65. sheetCommonObj.shieldAllCells(sheetAss);
  66. if(!(args.sheetArea === GC.Spread.Sheets.SheetArea.colHeader || args.sheetArea === GC.Spread.Sheets.SheetArea.corner)){
  67. var cacheSection = me.getCache();
  68. if (cacheSection && args.row < cacheSection.length) {
  69. rationGLJOprObj.getGljItems(cacheSection[args.row]);
  70. rationCoeOprObj.getCoeItems(cacheSection[args.row]);
  71. rationAssistOprObj.getAssItems(cacheSection[args.row]);
  72. }
  73. };
  74. me.workBook.focus(true);
  75. },
  76. getCache: function() {
  77. var me = this, rst = me.currentRations["_SEC_ID_" + me.currentSectionId];
  78. if (!(rst)) {
  79. me.currentRations["_SEC_ID_" + me.currentSectionId] = [];
  80. rst = me.currentRations["_SEC_ID_" + me.currentSectionId];
  81. }
  82. return rst;
  83. },
  84. updateCache: function(addArr, updateArr, removeIds, result) {
  85. var me = this, cacheSection = me.getCache();
  86. if (addArr.length > 0) {
  87. me.currentRations["_SEC_ID_" + me.currentSectionId] = cacheSection.concat(addArr);
  88. cacheSection = me.currentRations["_SEC_ID_" + me.currentSectionId];
  89. }
  90. for (var i = removeIds.length - 1; i >= 0; i--) {
  91. for (var j = cacheSection.length - 1; j >= 0 ; j--) {
  92. if (cacheSection[j]["ID"] == removeIds[i]) {
  93. cacheSection.splice(j,1);
  94. }
  95. }
  96. }
  97. if (result && result.data.ops && result.data.ops.length > 0) {
  98. for (var i = 0; i < result.data.ops.length; i++) {
  99. for (var j = 0; j < cacheSection.length; j++) {
  100. if (cacheSection[j][me.setting.header[0].dataCode] == result.data.ops[i][me.setting.header[0].dataCode]) {
  101. cacheSection[j]["ID"] = result.data.ops[i]["ID"];
  102. cacheSection[j]["rationGljList"] = result.data.ops[i]["rationGljList"];
  103. }
  104. }
  105. }
  106. }
  107. for (var i = 0; i < updateArr.length; i++) {
  108. for (var j = 0; j < cacheSection.length; j++) {
  109. if (updateArr[i]["ID"] && cacheSection[j]["ID"]) {
  110. if (cacheSection[j]["ID"] == updateArr[i]["ID"]) {
  111. cacheSection[j] = updateArr[i];
  112. }
  113. } else {
  114. if (cacheSection[j][me.setting.header[0].dataCode] == updateArr[i][me.setting.header[0].dataCode]) {
  115. cacheSection[j] = updateArr[i];
  116. }
  117. }
  118. }
  119. }
  120. return cacheSection;
  121. },
  122. rationDelOpr: function () {
  123. console.log(`rebuildDelete`);
  124. let me = rationOprObj;
  125. me.workBook.commandManager().register('rationDelete', function () {
  126. let rationSheet = me.workBook.getActiveSheet();
  127. let sels = rationSheet.getSelections(), updateArr = [], removeArr = [], lockCols = me.setting.view.lockColumns;
  128. let cacheSection = me.getCache();
  129. if(sels.length > 0){
  130. for(let sel = 0; sel < sels.length; sel++){
  131. if(sels[sel].colCount === me.setting.header.length){
  132. if(cacheSection){
  133. for(let i = 0; i < sels[sel].rowCount; i++){
  134. if(sels[sel].row + i < cacheSection.length){
  135. removeArr.push(cacheSection[sels[sel].row + i].ID);
  136. me.rationsCodes.splice(me.rationsCodes.indexOf(cacheSection[sels[sel].row + i].code), 1);
  137. console.log(`me.rationsCodes`);
  138. console.log(me.rationsCodes);
  139. }
  140. }
  141. }
  142. }
  143. else{
  144. if(sels[sel].col === 0){
  145. $('#alertText').text("编号不能为空,修改失败!");
  146. $('#alertModalBtn').click();
  147. rationSheet.options.isProtected = true;
  148. $('#alertModalCls').click(function () {
  149. rationSheet.options.isProtected = false;
  150. });
  151. $('#alertModalCof').click(function () {
  152. rationSheet.options.isProtected = false;
  153. })
  154. }
  155. else if(sels[sel].col !== 0 && !(sels[sel].col === 3 && sels.col + sels[sel].colCount -1 === 6)){
  156. if(cacheSection){
  157. for(let i =0; i < sels[sel].rowCount; i++){
  158. if(sels[sel].row + i < cacheSection.length){
  159. for(let col = sels[sel].col; col <= sels[sel].col + sels[sel].colCount - 1; col++){
  160. if(lockCols.indexOf(col) === -1){
  161. cacheSection[sels[sel].row + i][me.setting.header[col].dataCode] = '';
  162. }
  163. }
  164. }
  165. updateArr.push(cacheSection[sels[sel].row + i]);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. if(updateArr.length > 0 || removeArr.length > 0){
  173. me.mixUpdateRequest(updateArr, [], removeArr);
  174. }
  175. });
  176. me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  177. me.workBook.commandManager().setShortcutKey('rationDelete', GC.Spread.Commands.Key.del, false, false, false, false);
  178. },
  179. onRangeChanged: function(sender, args) {
  180. console.log(`sdad`);
  181. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  182. var me = rationOprObj, updateArr = [], removeArr = [];
  183. var cacheSection = me.getCache();
  184. if (cacheSection) {
  185. for (var i = 0; i < args.rowCount; i++) {
  186. //var hasUpdate = false, rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row + i),
  187. var hasUpdate = false, rObj = sheetCommonObj.combineRationRowData(me.workBook.getSheet(0), me.setting, args.row + i),
  188. isEmpty = sheetCommonObj.chkIfEmpty(rObj, me.setting);
  189. for (var j = 0; j < cacheSection.length; j++) {
  190. if (cacheSection[j][me.setting.header[0].dataCode] == rObj[me.setting.header[0].dataCode]) {
  191. rObj["ID"] = cacheSection[j]["ID"];
  192. hasUpdate = true;
  193. break;
  194. }
  195. }
  196. if(isEmpty){
  197. if(args.row + i < cacheSection.length){
  198. rObj.ID = cacheSection[args.row + i].ID;
  199. removeArr.push(rObj.ID);
  200. }
  201. }
  202. else{
  203. }
  204. if (hasUpdate) {
  205. if (isEmpty) {
  206. removeArr.push(rObj);
  207. } else updateArr.push(rObj);
  208. } else if (isEmpty) {
  209. if (args.row + i < cacheSection.length) {
  210. rObj["ID"] = cacheSection[args.row + i]["ID"];
  211. removeArr.push(rObj["ID"]);
  212. }
  213. }
  214. }
  215. // me.mixUpdateRequest(updateArr, [], removeArr);
  216. //removeRationItems
  217. }
  218. }
  219. },
  220. onEnterCell: function (sender, args) {
  221. let me = rationOprObj;
  222. me.cellRowIdx = args.row;
  223. let isHasData = false;
  224. if(me.addRationItem){
  225. for(let i=0; i<me.setting.header.length; i++){
  226. if(me.addRationItem[me.setting.header[i].dataCode]){
  227. isHasData = true;
  228. break;
  229. }
  230. }
  231. }
  232. if(isHasData){
  233. if(me.editingRowIdx !== me.cellRowIdx) {
  234. let focusToCol = !me.addRationItem.code ? 0 : -1;
  235. // else {
  236. if(focusToCol !== -1){
  237. $('#rationAlertBtn').click();
  238. me.workBook.getSheet(0).options.isProtected = true;
  239. $('#rationAlertCac').click(function () {
  240. me.workBook.getSheet(0).options.isProtected = false;
  241. me.workBook.getSheet(0).setActiveCell(me.editingRowIdx, focusToCol);
  242. });
  243. $('#rationAlertCls').click(function () {
  244. me.workBook.getSheet(0).options.isProtected = false;
  245. me.workBook.getSheet(0).setActiveCell(me.editingRowIdx, focusToCol);
  246. });
  247. $('#rationAlertCof').click(function () {
  248. me.workBook.getSheet(0).options.isProtected = false;
  249. me.addRationItem = null;
  250. for(let col=0; col<me.setting.header.length; col++){
  251. me.workBook.getSheet(0).getCell(me.editingRowIdx, col).value(me.currentEditingRation[me.setting.header[col].dataCode]);
  252. }
  253. me.workBook.getSheet(0).setActiveCell(me.editingRowIdx, 0);
  254. });
  255. }
  256. // }
  257. }
  258. }
  259. },
  260. onCellEditStart: function(sender, args) {
  261. var me = rationOprObj;
  262. //var rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row);
  263. var rObj = sheetCommonObj.combineRationRowData(me.workBook.getSheet(0), me.setting, args.row);
  264. me.currentEditingRation = rObj;
  265. var cacheSection = me.getCache();
  266. if (cacheSection) {
  267. for (var j = 0; j < cacheSection.length; j++) {
  268. if (cacheSection[j][me.setting.header[0].dataCode] == rObj[me.setting.header[0].dataCode]) {
  269. rObj["ID"] = cacheSection[j]["ID"];
  270. break;
  271. }
  272. }
  273. }
  274. },
  275. onCellEditEnd: function(sender, args) {
  276. //var me = rationOprObj, rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row),
  277. var me = rationOprObj, rObj = sheetCommonObj.combineRationRowData(me.workBook.getSheet(0), me.setting, args.row),
  278. updateArr = [], addArr = [];
  279. let dataCode = me.setting.header[args.col].dataCode;
  280. me.editingRowIdx = args.row;
  281. if (me.currentEditingRation["ID"]) {
  282. rObj["ID"] = me.currentEditingRation["ID"];
  283. //updateArr.push(rObj);
  284. for(let col =0; col< me.setting.header.length; col++){
  285. if(me.currentEditingRation[me.setting.header[col].dataCode] !== rObj[me.setting.header[col].dataCode]){
  286. me.addRationItem = rObj;
  287. if(rObj[me.setting.header[0].dataCode]){
  288. updateArr.push(rObj);
  289. break;
  290. }
  291. }
  292. }
  293. } else if(!me.currentEditingRation["ID"]) {
  294. if (!sheetCommonObj.chkIfEmpty(rObj, me.setting)) {
  295. //addArr.push(rObj);
  296. me.addRationItem = rObj;
  297. if(rObj.code){
  298. if(me.rationsCodes.indexOf(rObj.code) === -1){
  299. addArr.push(rObj);
  300. me.rationsCodes.push(rObj.code);
  301. me.addRationItem = null;
  302. }
  303. else{
  304. $('#alertModalBtn').click();
  305. me.workBook.getSheet(0).options.isProtected = true;
  306. $('#alertModalCls').click(function () {
  307. me.workBook.getSheet(0).options.isProtected = false;
  308. me.addRationItem.code = '';
  309. me.workBook.getSheet(0).setValue(args.row, args.col, '');
  310. me.workBook.getSheet(0).setActiveCell(args.row, args.col);
  311. });
  312. $('#alertModalCof').click(function () {
  313. me.workBook.getSheet(0).options.isProtected = false;
  314. me.addRationItem.code = '';
  315. me.workBook.getSheet(0).setValue(args.row, args.col, '');
  316. me.workBook.getSheet(0).setActiveCell(args.row, args.col);
  317. });
  318. }
  319. }
  320. }
  321. }
  322. if (updateArr.length > 0 || addArr.length > 0) {
  323. me.currentEditingRation = null;
  324. me.mixUpdateRequest(updateArr, addArr, []);
  325. }
  326. },
  327. onClipboardPasting: function(sender, args) {
  328. var me = rationOprObj;
  329. console.log(`args`);
  330. console.log(args);
  331. /* if (args.cellRange.colCount != me.setting.header.length) {
  332. args.cancel = true;
  333. }*/
  334. },
  335. onClipboardPasted: function(e, info) {
  336. console.log(`info`);
  337. console.log(info);
  338. var me = rationOprObj;
  339. var cacheSection = me.getCache();
  340. console.log(`cache`);
  341. console.log(cacheSection);
  342. var updateArr = [], addArr = [];
  343. var items = sheetCommonObj.analyzePasteData(me.setting, info);
  344. console.log(`items`);
  345. console.log(items);
  346. for (var i = 0; i < items.length; i++) {
  347. if (cacheSection) {
  348. var hasCacheItem = false;
  349. for (var j = 0; j < cacheSection.length; j++) {
  350. if (cacheSection[j][me.setting.header[0].dataCode] == items[i][me.setting.header[0].dataCode]) {
  351. hasCacheItem = true;
  352. items[i]["ID"] = cacheSection[j]["ID"];
  353. break;
  354. }
  355. }
  356. if (!hasCacheItem && info.cellRange.col === 0) {
  357. addArr.push(items[i]);
  358. } else if(hasCacheItem){
  359. updateArr.push(items[i]);
  360. }
  361. } else {
  362. //add
  363. if(info.cellRange.col === 0){
  364. addArr.push(items[i])
  365. }
  366. }
  367. };
  368. console.log(`updateArr`);
  369. console.log(updateArr);
  370. console.log(addArr);
  371. if (updateArr.length > 0 || addArr.length > 0) {
  372. //me.mixUpdateRequest(updateArr, addArr, []);
  373. }
  374. },
  375. getRationsCodes: function (repId) {
  376. let me = rationOprObj;
  377. $.ajax({
  378. type: 'post',
  379. url: 'api/getRationsCodes',
  380. data: {data: JSON.stringify({repId: repId})},
  381. dataType: 'json',
  382. success: function (result) {
  383. if(!result.error){
  384. me.rationsCodes = result.data;
  385. }
  386. }
  387. })
  388. },
  389. mixUpdateRequest: function(updateArr, addArr, removeIds) {
  390. var me = rationOprObj;
  391. $.ajax({
  392. type:"POST",
  393. url:"api/mixUpdateRationItems",
  394. data:{"rationLibId": getQueryString("repository"), "sectionID": me.currentSectionId, "updateItems": JSON.stringify(updateArr), "addItems": JSON.stringify(addArr), "removeIds": JSON.stringify(removeIds)},
  395. dataType:"json",
  396. cache:false,
  397. timeout:5000,
  398. success:function(result){
  399. if (result.err) {
  400. alert(err);
  401. me.getRationItems(me.currentSectionId);
  402. } else {
  403. var cacheSection = me.updateCache(addArr, updateArr, removeIds, result);
  404. cacheSection.sort(function(a, b){
  405. var rst = 0;
  406. if (a.code > b.code) rst = 1
  407. else if (a.code < b.code) rst = -1;
  408. return rst;
  409. });
  410. me.showRationItems(me.currentSectionId);
  411. }
  412. },
  413. error:function(){
  414. }
  415. });
  416. },
  417. getRationItems: function(sectionID){
  418. if (sectionID != -1) {
  419. var me = rationOprObj;
  420. me.currentSectionId = sectionID;
  421. if (me.currentRations["_SEC_ID_" + sectionID]) {
  422. me.showRationItems(sectionID);
  423. ///sheetCommonObj.unShieldAllCells(me.workBook.getSheet(0));
  424. sheetCommonObj.lockCells(rationGLJOprObj.sheet, rationGLJOprObj.setting);
  425. sheetCommonObj.lockCells(rationCoeOprObj.sheet, rationCoeOprObj.setting);
  426. sheetCommonObj.lockCells(me.workBook.getSheet(0), rationOprObj.setting);
  427. sheetCommonObj.unShieldAllCells(rationAssistOprObj.sheet);
  428. } else {
  429. $.ajax({
  430. type:"POST",
  431. url:"api/getRationItems",
  432. data:{"sectionID": sectionID},
  433. dataType:"json",
  434. cache:false,
  435. timeout:1000,
  436. success:function(result){
  437. if (result) {
  438. me.currentRations["_SEC_ID_" + sectionID] = result.data;
  439. me.sortByCode(me.currentRations["_SEC_ID_" + sectionID]);
  440. me.showRationItems(sectionID);
  441. //sheetCommonObj.unShieldAllCells(me.workBook.getSheet(0));
  442. sheetCommonObj.lockCells(me.workBook.getSheet(0), rationOprObj.setting);
  443. sheetCommonObj.lockCells(rationGLJOprObj.sheet, rationGLJOprObj.setting);
  444. sheetCommonObj.lockCells(rationCoeOprObj.sheet, rationCoeOprObj.setting);
  445. sheetCommonObj.unShieldAllCells(rationAssistOprObj.sheet);
  446. } else {
  447. sheetCommonObj.shieldAllCells(me.workBook.getSheet(0));
  448. sheetCommonObj.shieldAllCells(rationGLJOprObj.sheet);
  449. sheetCommonObj.shieldAllCells(rationCoeOprObj.sheet);
  450. sheetCommonObj.shieldAllCells(rationAssistOprObj.sheet);
  451. }
  452. },
  453. error:function(err){
  454. alert(err);
  455. }
  456. })
  457. }
  458. }
  459. },
  460. showRationItems: function(sectionID){
  461. var me = rationOprObj;
  462. if (me.workBook) {
  463. if (me.currentRations && me.currentRations["_SEC_ID_" + sectionID] && me.currentRations["_SEC_ID_" + sectionID].length > 0) {
  464. var cacheSection = me.currentRations["_SEC_ID_" + sectionID];
  465. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  466. sheetCommonObj.showData(me.workBook.getSheet(0), me.setting, cacheSection);
  467. } else {
  468. //清除ration数据及工料机数据
  469. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  470. }
  471. }
  472. },
  473. sortByCode: function(arr){
  474. function compare(){
  475. return function (a, b) {
  476. let rst = 0;
  477. if (a.code > b.code) {
  478. rst = 1;
  479. }
  480. else if (a.code < b.code) {
  481. rst = -1;
  482. }
  483. return rst;
  484. }
  485. }
  486. arr.sort(compare());
  487. }
  488. }