ration.js 13 KB

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