ration.js 13 KB

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