ration.js 19 KB

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