ration.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  43. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  44. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  45. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  46. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  47. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.CellClick, me.onCellClick);
  48. },
  49. onCellClick: function(sender, args) {
  50. var me = rationOprObj,
  51. sheetGLJ = rationGLJOprObj.sheet, settingGLJ = rationGLJOprObj.setting,
  52. sheetCoe = rationCoeOprObj.sheet, settingCoe = rationCoeOprObj.setting,
  53. sheetAss = rationAssistOprObj.sheet, settingAss = rationAssistOprObj.setting;
  54. sheetCommonObj.cleanSheet(sheetGLJ, settingGLJ, -1);
  55. sheetCommonObj.shieldAllCells(sheetGLJ);
  56. sheetCommonObj.cleanSheet(sheetCoe, settingCoe, -1);
  57. sheetCommonObj.shieldAllCells(sheetCoe);
  58. sheetCommonObj.cleanSheet(sheetAss, settingAss, -1);
  59. sheetCommonObj.shieldAllCells(sheetAss);
  60. if(!(args.sheetArea === GC.Spread.Sheets.SheetArea.colHeader || args.sheetArea === GC.Spread.Sheets.SheetArea.corner)){
  61. var cacheSection = me.getCache();
  62. if (cacheSection && args.row < cacheSection.length) {
  63. rationGLJOprObj.getGljItems(cacheSection[args.row]);
  64. rationCoeOprObj.getCoeItems(cacheSection[args.row]);
  65. rationAssistOprObj.getAssItems(cacheSection[args.row]);
  66. }
  67. };
  68. me.workBook.focus(true);
  69. },
  70. getCache: function() {
  71. var me = this, rst = me.currentRations["_SEC_ID_" + me.currentSectionId];
  72. if (!(rst)) {
  73. me.currentRations["_SEC_ID_" + me.currentSectionId] = [];
  74. rst = me.currentRations["_SEC_ID_" + me.currentSectionId];
  75. }
  76. return rst;
  77. },
  78. updateCache: function(addArr, updateArr, removeIds, result) {
  79. var me = this, cacheSection = me.getCache();
  80. if (addArr.length > 0) {
  81. me.currentRations["_SEC_ID_" + me.currentSectionId] = cacheSection.concat(addArr);
  82. cacheSection = me.currentRations["_SEC_ID_" + me.currentSectionId];
  83. }
  84. for (var i = removeIds.length - 1; i >= 0; i--) {
  85. for (var j = cacheSection.length - 1; j >= 0 ; j--) {
  86. if (cacheSection[j]["ID"] == removeIds[i]) {
  87. cacheSection.splice(j,1);
  88. }
  89. }
  90. }
  91. if (result && result.data.ops && result.data.ops.length > 0) {
  92. for (var i = 0; i < result.data.ops.length; i++) {
  93. for (var j = 0; j < cacheSection.length; j++) {
  94. if (cacheSection[j][me.setting.header[0].dataCode] == result.data.ops[i][me.setting.header[0].dataCode]) {
  95. cacheSection[j]["ID"] = result.data.ops[i]["ID"];
  96. cacheSection[j]["rationGljList"] = result.data.ops[i]["rationGljList"];
  97. }
  98. }
  99. }
  100. }
  101. for (var i = 0; i < updateArr.length; i++) {
  102. for (var j = 0; j < cacheSection.length; j++) {
  103. if (updateArr[i]["ID"] && cacheSection[j]["ID"]) {
  104. if (cacheSection[j]["ID"] == updateArr[i]["ID"]) {
  105. cacheSection[j] = updateArr[i];
  106. }
  107. } else {
  108. if (cacheSection[j][me.setting.header[0].dataCode] == updateArr[i][me.setting.header[0].dataCode]) {
  109. cacheSection[j] = updateArr[i];
  110. }
  111. }
  112. }
  113. }
  114. return cacheSection;
  115. },
  116. onRangeChanged: function(sender, args) {
  117. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  118. var me = rationOprObj, updateArr = [], removeArr = [];
  119. var cacheSection = me.getCache();
  120. if (cacheSection) {
  121. for (var i = 0; i < args.rowCount; i++) {
  122. //var hasUpdate = false, rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row + i),
  123. var hasUpdate = false, rObj = sheetCommonObj.combineRationRowData(me.workBook.getSheet(0), me.setting, args.row + i),
  124. isEmpty = sheetCommonObj.chkIfEmpty(rObj, me.setting);
  125. for (var j = 0; j < cacheSection.length; j++) {
  126. if (cacheSection[j][me.setting.header[0].dataCode] == rObj[me.setting.header[0].dataCode]) {
  127. rObj["ID"] = cacheSection[j]["ID"];
  128. hasUpdate = true;
  129. break;
  130. }
  131. }
  132. if (hasUpdate) {
  133. if (isEmpty) {
  134. removeArr.push(rObj);
  135. } else updateArr.push(rObj);
  136. } else if (isEmpty) {
  137. if (args.row + i < cacheSection.length) {
  138. rObj["ID"] = cacheSection[args.row + i]["ID"];
  139. removeArr.push(rObj["ID"]);
  140. }
  141. }
  142. }
  143. me.mixUpdateRequest(updateArr, [], removeArr);
  144. //removeRationItems
  145. }
  146. }
  147. },
  148. onCellEditStart: function(sender, args) {
  149. var me = rationOprObj;
  150. //var rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row);
  151. var rObj = sheetCommonObj.combineRationRowData(me.workBook.getSheet(0), me.setting, args.row);
  152. me.currentEditingRation = rObj;
  153. console.log(`currentEditingRation`);
  154. console.log(me.currentEditingRation);
  155. var cacheSection = me.getCache();
  156. if (cacheSection) {
  157. for (var j = 0; j < cacheSection.length; j++) {
  158. if (cacheSection[j][me.setting.header[0].dataCode] == rObj[me.setting.header[0].dataCode]) {
  159. rObj["ID"] = cacheSection[j]["ID"];
  160. break;
  161. }
  162. }
  163. }
  164. },
  165. onCellEditEnd: function(sender, args) {
  166. //var me = rationOprObj, rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row),
  167. var me = rationOprObj, rObj = sheetCommonObj.combineRationRowData(me.workBook.getSheet(0), me.setting, args.row),
  168. updateArr = [], addArr = [];
  169. console.log(`editEnd`);
  170. console.log(rObj);
  171. if (me.currentEditingRation["ID"]) {
  172. rObj["ID"] = me.currentEditingRation["ID"];
  173. updateArr.push(rObj);
  174. } else {
  175. if (!sheetCommonObj.chkIfEmpty(rObj, me.setting)) {
  176. addArr.push(rObj);
  177. }
  178. }
  179. me.currentEditingRation = null;
  180. if (updateArr.length > 0 || addArr.length > 0) {
  181. me.mixUpdateRequest(updateArr, addArr, []);
  182. }
  183. },
  184. onClipboardPasting: function(sender, args) {
  185. var me = rationOprObj;
  186. console.log(`args`);
  187. console.log(args);
  188. /* if (args.cellRange.colCount != me.setting.header.length) {
  189. args.cancel = true;
  190. }*/
  191. },
  192. onClipboardPasted: function(e, info) {
  193. console.log(`info`);
  194. console.log(info);
  195. var me = rationOprObj;
  196. var cacheSection = me.getCache();
  197. var updateArr = [], addArr = [];
  198. var items = sheetCommonObj.analyzePasteData(me.setting, info);
  199. console.log(`items`);
  200. console.log(items);
  201. for (var i = 0; i < items.length; i++) {
  202. if (cacheSection) {
  203. var hasCacheItem = false;
  204. for (var j = 0; j < cacheSection.length; j++) {
  205. if (cacheSection[j][me.setting.header[0].dataCode] == items[i][me.setting.header[0].dataCode]) {
  206. hasCacheItem = true;
  207. items[i]["ID"] = cacheSection[j]["ID"];
  208. break;
  209. }
  210. }
  211. if (!hasCacheItem) {
  212. addArr.push(items[i]);
  213. } else {
  214. updateArr.push(items[i]);
  215. }
  216. } else {
  217. addArr.push(items[i])
  218. }
  219. };
  220. if (updateArr.length > 0 || addArr.length > 0) {
  221. //me.mixUpdateRequest(updateArr, addArr, []);
  222. }
  223. },
  224. mixUpdateRequest: function(updateArr, addArr, removeIds) {
  225. var me = rationOprObj;
  226. $.ajax({
  227. type:"POST",
  228. url:"api/mixUpdateRationItems",
  229. data:{"rationLibId": getQueryString("repository"), "sectionID": me.currentSectionId, "updateItems": JSON.stringify(updateArr), "addItems": JSON.stringify(addArr), "removeIds": JSON.stringify(removeIds)},
  230. dataType:"json",
  231. cache:false,
  232. timeout:5000,
  233. success:function(result){
  234. if (result.err) {
  235. alert(err);
  236. me.getRationItems(me.currentSectionId);
  237. } else {
  238. var cacheSection = me.updateCache(addArr, updateArr, removeIds, result);
  239. cacheSection.sort(function(a, b){
  240. var rst = 0;
  241. if (a.code > b.code) rst = 1
  242. else if (a.code < b.code) rst = -1;
  243. return rst;
  244. });
  245. me.showRationItems(me.currentSectionId);
  246. }
  247. },
  248. error:function(){
  249. }
  250. });
  251. },
  252. getRationItems: function(sectionID){
  253. if (sectionID != -1) {
  254. var me = rationOprObj;
  255. me.currentSectionId = sectionID;
  256. if (me.currentRations["_SEC_ID_" + sectionID]) {
  257. console.log(`currentRations`);
  258. console.log(me.currentRations);
  259. me.showRationItems(sectionID);
  260. ///sheetCommonObj.unShieldAllCells(me.workBook.getSheet(0));
  261. sheetCommonObj.lockCells(rationGLJOprObj.sheet, rationGLJOprObj.setting);
  262. sheetCommonObj.lockCells(rationCoeOprObj.sheet, rationCoeOprObj.setting);
  263. sheetCommonObj.lockCells(me.workBook.getSheet(0), rationOprObj.setting);
  264. sheetCommonObj.unShieldAllCells(rationAssistOprObj.sheet);
  265. } else {
  266. $.ajax({
  267. type:"POST",
  268. url:"api/getRationItems",
  269. data:{"sectionID": sectionID},
  270. dataType:"json",
  271. cache:false,
  272. timeout:1000,
  273. success:function(result){
  274. if (result) {
  275. me.currentRations["_SEC_ID_" + sectionID] = result.data;
  276. me.showRationItems(sectionID);
  277. //sheetCommonObj.unShieldAllCells(me.workBook.getSheet(0));
  278. sheetCommonObj.lockCells(me.workBook.getSheet(0), rationOprObj.setting);
  279. sheetCommonObj.lockCells(rationGLJOprObj.sheet, rationGLJOprObj.setting);
  280. sheetCommonObj.lockCells(rationCoeOprObj.sheet, rationCoeOprObj.setting);
  281. sheetCommonObj.unShieldAllCells(rationAssistOprObj.sheet);
  282. } else {
  283. sheetCommonObj.shieldAllCells(me.workBook.getSheet(0));
  284. sheetCommonObj.shieldAllCells(rationGLJOprObj.sheet);
  285. sheetCommonObj.shieldAllCells(rationCoeOprObj.sheet);
  286. sheetCommonObj.shieldAllCells(rationAssistOprObj.sheet);
  287. }
  288. },
  289. error:function(err){
  290. alert(err);
  291. }
  292. })
  293. }
  294. }
  295. },
  296. showRationItems: function(sectionID){
  297. var me = rationOprObj;
  298. if (me.workBook) {
  299. if (me.currentRations && me.currentRations["_SEC_ID_" + sectionID] && me.currentRations["_SEC_ID_" + sectionID].length > 0) {
  300. var cacheSection = me.currentRations["_SEC_ID_" + sectionID];
  301. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  302. sheetCommonObj.showData(me.workBook.getSheet(0), me.setting, cacheSection);
  303. } else {
  304. //清除ration数据及工料机数据
  305. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  306. }
  307. }
  308. },
  309. sortByCode: function(arr){
  310. function compare(){
  311. return function (a, b) {
  312. let valA = a.code,
  313. valB = b.code;
  314. return valA - valB;
  315. }
  316. }
  317. arr.sort(compare());
  318. }
  319. }