ration.js 14 KB

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