ration.js 13 KB

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