ration.js 14 KB

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