ration.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. var rationOprObj = {
  8. workBook: null,
  9. currentRations: {},
  10. currentEditingRation: null,
  11. currentSectionId: -1,
  12. setting: {
  13. header:[
  14. {headerName:"编码",headerWidth:120,dataCode:"code", dataType: "String", formatter: "@"},
  15. {headerName:"名称",headerWidth:300,dataCode:"name", dataType: "String"},
  16. {headerName:"单位",headerWidth:120,dataCode:"unit", dataType: "String", hAlign: "center"},
  17. {headerName:"基价",headerWidth:120,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right"},
  18. {headerName:"显示名称(以%s表示参数)",headerWidth:350,dataCode:"caption", dataType: "String"},
  19. {headerName:"取费专业",headerWidth:120,dataCode:"feeType", dataType: "Number", hAlign: "center"}
  20. ],
  21. view:{
  22. comboBox:[
  23. {row:-1,col:2,rowCount:-1,colCount:1}
  24. ],
  25. lockedCells:[
  26. {row:-1,col:3,rowCount:-1, colCount:1}
  27. ]
  28. }
  29. },
  30. buildSheet: function(container) {
  31. var me = this;
  32. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  33. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  34. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  35. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  36. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  37. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  38. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.CellClick, me.onCellClcik);
  39. },
  40. onCellClcik: function(sender, args) {
  41. var me = rationOprObj, sheet = rationGLJOprObj.workBook.getSheet(0), setting = rationGLJOprObj.setting;
  42. if(args.sheetArea === GC.Spread.Sheets.SheetArea.colHeader || args.sheetArea === GC.Spread.Sheets.SheetArea.corner){
  43. sheetCommonObj.cleanSheet(sheet, setting, -1);
  44. sheetCommonObj.shieldAllCells(sheet);
  45. } else {
  46. var cacheSection = me.getCache();
  47. if (cacheSection && args.row < cacheSection.length) {
  48. rationGLJOprObj.getGljItems(cacheSection[args.row]);
  49. }
  50. //
  51. }
  52. },
  53. getCache: function() {
  54. var me = this, rst = me.currentRations["_SEC_ID_" + me.currentSectionId];
  55. if (!(rst)) {
  56. me.currentRations["_SEC_ID_" + me.currentSectionId] = [];
  57. rst = me.currentRations["_SEC_ID_" + me.currentSectionId];
  58. }
  59. return rst;
  60. },
  61. updateCache: function(addArr, updateArr, removeIds, result) {
  62. var me = this, cacheSection = me.getCache();
  63. if (addArr.length > 0) {
  64. me.currentRations["_SEC_ID_" + me.currentSectionId] = cacheSection.concat(addArr);
  65. cacheSection = me.currentRations["_SEC_ID_" + me.currentSectionId];
  66. }
  67. for (var i = removeIds.length - 1; i >= 0; i--) {
  68. for (var j = cacheSection.length - 1; j >= 0 ; j--) {
  69. if (cacheSection[j]["ID"] == removeIds[i]) {
  70. cacheSection.splice(j,1);
  71. }
  72. }
  73. }
  74. if (result && result.data.ops && result.data.ops.length > 0) {
  75. for (var i = 0; i < result.data.ops.length; i++) {
  76. for (var j = 0; j < cacheSection.length; j++) {
  77. if (cacheSection[j][me.setting.header[0].dataCode] == result.data.ops[i][me.setting.header[0].dataCode]) {
  78. cacheSection[j]["ID"] = result.data.ops[i]["ID"];
  79. cacheSection[j]["rationGljList"] = result.data.ops[i]["rationGljList"];
  80. }
  81. }
  82. }
  83. }
  84. for (var i = 0; i < updateArr.length; i++) {
  85. for (var j = 0; j < cacheSection.length; j++) {
  86. if (updateArr[i]["ID"] && cacheSection[j]["ID"]) {
  87. if (cacheSection[j]["ID"] == updateArr[i]["ID"]) {
  88. cacheSection[j] = updateArr[i];
  89. }
  90. } else {
  91. if (cacheSection[j][me.setting.header[0].dataCode] == updateArr[i][me.setting.header[0].dataCode]) {
  92. cacheSection[j] = updateArr[i];
  93. }
  94. }
  95. }
  96. }
  97. return cacheSection;
  98. },
  99. onRangeChanged: function(sender, args) {
  100. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  101. var me = rationOprObj, updateArr = [], removeArr = [];
  102. var cacheSection = me.getCache();
  103. if (cacheSection) {
  104. for (var i = 0; i < args.rowCount; i++) {
  105. var hasUpdate = false, rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row + i),
  106. isEmpty = sheetCommonObj.chkIfEmpty(rObj, me.setting);
  107. for (var j = 0; j < cacheSection.length; j++) {
  108. if (cacheSection[j][me.setting.header[0].dataCode] == rObj[me.setting.header[0].dataCode]) {
  109. rObj["ID"] = cacheSection[j]["ID"];
  110. hasUpdate = true;
  111. break;
  112. }
  113. }
  114. if (hasUpdate) {
  115. if (isEmpty) {
  116. removeArr.push(rObj);
  117. } else updateArr.push(rObj);
  118. } else if (isEmpty) {
  119. if (args.row + i < cacheSection.length) {
  120. rObj["ID"] = cacheSection[args.row + i]["ID"];
  121. removeArr.push(rObj["ID"]);
  122. }
  123. }
  124. }
  125. me.mixUpdateRequest(updateArr, [], removeArr);
  126. //removeRationItems
  127. }
  128. }
  129. },
  130. onCellEditStart: function(sender, args) {
  131. var me = rationOprObj;
  132. var rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row);
  133. me.currentEditingRation = rObj;
  134. var cacheSection = me.getCache();
  135. if (cacheSection) {
  136. for (var j = 0; j < cacheSection.length; j++) {
  137. if (cacheSection[j][me.setting.header[0].dataCode] == rObj[me.setting.header[0].dataCode]) {
  138. rObj["ID"] = cacheSection[j]["ID"];
  139. break;
  140. }
  141. }
  142. }
  143. },
  144. onCellEditEnd: function(sender, args) {
  145. var me = rationOprObj, rObj = sheetCommonObj.combineRowData(me.workBook.getSheet(0), me.setting, args.row),
  146. updateArr = [], addArr = [];
  147. if (me.currentEditingRation["ID"]) {
  148. rObj["ID"] = me.currentEditingRation["ID"];
  149. updateArr.push(rObj);
  150. } else {
  151. addArr.push(rObj);
  152. }
  153. me.currentEditingRation = null;
  154. me.mixUpdateRequest(updateArr, addArr, []);
  155. },
  156. onClipboardPasting: function(sender, args) {
  157. var me = rationOprObj;
  158. if (args.cellRange.colCount != me.setting.header.length) {
  159. args.cancel = true;
  160. }
  161. },
  162. onClipboardPasted: function(e, info) {
  163. var me = rationOprObj;
  164. var cacheSection = me.getCache();
  165. var updateArr = [], addArr = [];
  166. var items = sheetCommonObj.analyzePasteData(me.setting, info);
  167. for (var i = 0; i < items.length; i++) {
  168. if (cacheSection) {
  169. var hasCacheItem = false;
  170. for (var j = 0; j < cacheSection.length; j++) {
  171. if (cacheSection[j][me.setting.header[0].dataCode] == items[i][me.setting.header[0].dataCode]) {
  172. hasCacheItem = true;
  173. items[i]["ID"] = cacheSection[j]["ID"];
  174. break;
  175. }
  176. }
  177. if (!hasCacheItem) {
  178. addArr.push(items[i]);
  179. } else {
  180. updateArr.push(items[i]);
  181. }
  182. } else {
  183. addArr.push(items[i])
  184. }
  185. };
  186. if (updateArr.length > 0 || addArr.length > 0) {
  187. me.mixUpdateRequest(updateArr, addArr, []);
  188. }
  189. },
  190. mixUpdateRequest: function(updateArr, addArr, removeIds) {
  191. var me = rationOprObj;
  192. $.ajax({
  193. type:"POST",
  194. url:"api/mixUpdateRationItems",
  195. data:{"sectionID": me.currentSectionId, "updateItems": JSON.stringify(updateArr), "addItems": JSON.stringify(addArr), "removeIds": JSON.stringify(removeIds)},
  196. dataType:"json",
  197. cache:false,
  198. timeout:5000,
  199. success:function(result){
  200. if (result.err) {
  201. alert(err);
  202. me.getRationItems(me.currentSectionId);
  203. } else {
  204. var cacheSection = me.updateCache(addArr, updateArr, removeIds, result);
  205. cacheSection.sort(function(a, b){
  206. var rst = 0;
  207. if (a.code > b.code) rst = 1
  208. else if (a.code < b.code) rst = -1;
  209. return rst;
  210. });
  211. me.showRationItems(me.currentSectionId);
  212. }
  213. },
  214. error:function(){
  215. }
  216. });
  217. },
  218. getRationItems: function(sectionID){
  219. if (sectionID != -1) {
  220. var me = rationOprObj;
  221. me.currentSectionId = sectionID;
  222. if (me.currentRations["_SEC_ID_" + sectionID]) {
  223. me.showRationItems(sectionID);
  224. sheetCommonObj.unShieldAllCells(me.workBook.getSheet(0));
  225. sheetCommonObj.lockCells(rationGLJOprObj.workBook.getSheet(0), rationGLJOprObj.setting);
  226. } else {
  227. $.ajax({
  228. type:"POST",
  229. url:"api/getRationItems",
  230. data:{"sectionID": sectionID},
  231. dataType:"json",
  232. cache:false,
  233. timeout:1000,
  234. success:function(result){
  235. if (result) {
  236. me.currentRations["_SEC_ID_" + sectionID] = result.data;
  237. me.showRationItems(sectionID);
  238. sheetCommonObj.unShieldAllCells(me.workBook.getSheet(0));
  239. sheetCommonObj.lockCells(rationGLJOprObj.workBook.getSheet(0), rationGLJOprObj.setting);
  240. } else {
  241. sheetCommonObj.shieldAllCells(me.workBook.getSheet(0));
  242. sheetCommonObj.shieldAllCells(rationGLJOprObj.workBook.getSheet(0));
  243. }
  244. },
  245. error:function(err){
  246. alert(err);
  247. }
  248. })
  249. }
  250. }
  251. },
  252. showRationItems: function(sectionID){
  253. var me = rationOprObj;
  254. if (me.workBook) {
  255. if (me.currentRations && me.currentRations["_SEC_ID_" + sectionID] && me.currentRations["_SEC_ID_" + sectionID].length > 0) {
  256. var cacheSection = me.currentRations["_SEC_ID_" + sectionID];
  257. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  258. sheetCommonObj.showData(me.workBook.getSheet(0), me.setting, cacheSection);
  259. } else {
  260. //清除ration数据及工料机数据
  261. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  262. }
  263. }
  264. }
  265. }