ration_coe.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * Created by CSL on 2017-06-08.
  3. */
  4. //modified by zhong on 2017-09-25
  5. var rationCoeOprObj = {
  6. sheet: null,
  7. libID: null,
  8. curRation: null,
  9. tempDelArr: [],
  10. cache: {},
  11. setting: {
  12. header:[
  13. {headerName:"编码",headerWidth:120,dataCode:"ID", dataType: "Number", hAlign: 'left'},
  14. {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String"},
  15. {headerName:"内容",headerWidth:300,dataCode:"content", dataType: "String"}
  16. ],
  17. view:{
  18. comboBox:[],
  19. lockColumns:[1,2]
  20. }
  21. },
  22. buildSheet: function(sheet) {
  23. var me = this;
  24. me.sheet = sheet;
  25. me.libID = storageUtil.getSessionCache("RationGrp","repositoryID"); // 不可靠,有时取不到
  26. if (me.libID == undefined){me.libID = getQueryString('repository')};
  27. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  28. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  29. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  30. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  31. // me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  32. },
  33. onClipboardPasting: function(sender, args) {
  34. var me = rationCoeOprObj;
  35. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation)) {
  36. args.cancel = true;
  37. }
  38. },
  39. onClipboardPasted: function(e, info) {
  40. var me = rationCoeOprObj;
  41. if (me.libID) {
  42. // 修改第一列(编号)
  43. if (info.cellRange.col == 0) {
  44. me.tempDelArr = [];
  45. var coeIDs = [];
  46. var items = sheetCommonObj.analyzePasteData({header:[{dataCode: "ID"}] }, info);
  47. let curCache = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  48. let isRefresh = false;
  49. for(let i = 0, len = items.length; i < len; i++){
  50. let row = i + info.cellRange.row;
  51. //update
  52. if(row < curCache.length){
  53. let isExist = false;
  54. for(let j = 0, jLen = curCache.length; j < jLen; j++){
  55. if(items[i].ID === curCache[j].ID && j !== row){
  56. isExist = true;
  57. break;
  58. }
  59. }
  60. if(!isExist){
  61. me.tempDelArr.push({org: curCache[row], newID: items[i].ID});
  62. coeIDs.push(items[i].ID);
  63. }
  64. else{
  65. isRefresh = true;
  66. }
  67. }
  68. else{
  69. coeIDs.push(items[i].ID);
  70. }
  71. }
  72. //delete in front
  73. if(me.tempDelArr.length > 0){
  74. for(let i = 0, len = me.tempDelArr.length; i < len; i++){
  75. for(let j = 0; j < curCache.length; j++){
  76. if(me.tempDelArr[i].org.ID === curCache[j].ID){
  77. curCache.splice(j, 1);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. me.addCoeItems(coeIDs);
  84. if(isRefresh){
  85. me.showCoeItems(me.curRation.ID);
  86. }
  87. } else {
  88. //修改其它列。
  89. }
  90. }
  91. },
  92. onEditEnded: function(sender, args){
  93. var me = rationCoeOprObj;
  94. if (args.col == 0 && args.editingText && args.editingText.toString().trim().length > 0 && !isNaN(args.editingText)) { // 编号列
  95. let curCahe = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  96. me.tempDelArr = [];
  97. //update
  98. if(args.row < curCahe.length && args.editingText != curCahe[args.row].ID){
  99. let isExist = false;
  100. for(let i = 0, len = curCahe.length; i < len; i++){
  101. if(args.editingText == curCahe[i].ID){
  102. isExist = true;
  103. break;
  104. }
  105. }
  106. if(!isExist){
  107. me.tempDelArr.push({org: curCahe[args.row], newID: args.editingText});
  108. curCahe.splice(args.row, 1);
  109. me.addCoeItems([args.editingText]);
  110. }
  111. else{
  112. me.showCoeItems(me.curRation.ID);
  113. }
  114. }
  115. //insert
  116. else{
  117. me.addCoeItems([args.editingText]);
  118. }
  119. }
  120. else{
  121. me.showCoeItems(me.curRation.ID);
  122. }
  123. },
  124. bindRationCoeDel: function () {
  125. let me = rationCoeOprObj;
  126. let workBook = me.sheet.getParent();
  127. workBook.commandManager().register('rationCoeDel', function () {
  128. let sels = me.sheet.getSelections(), isUpdate = false;
  129. let curCahe = me.cache["_Coe_" + me.curRation.ID];
  130. for(let i = 0, len = sels.length; i < len; i ++ ){
  131. if(sels[i].colCount === me.setting.header.length){
  132. if(sels[i].row < curCahe.length){
  133. isUpdate = true;
  134. curCahe.splice(sels[i].row, sels[i].rowCount);
  135. }
  136. }
  137. }
  138. if(isUpdate){
  139. me.updateCurRation();
  140. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  141. me.showCoeItems(me.curRation.ID);
  142. }
  143. });
  144. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  145. workBook.commandManager().setShortcutKey('rationCoeDel', GC.Spread.Commands.Key.del, false, false, false, false);
  146. },
  147. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的coe不存在,恢复删除的被更新数据
  148. let rst = [];
  149. for(let i = 0, len = tempDelArr.length; i < len; i++){
  150. let isExist = false;
  151. for(let j = 0, jLen = newArr.length; j < jLen; j++){
  152. if(tempDelArr[i].newID == newArr[j].ID){
  153. isExist = true;
  154. break;
  155. }
  156. }
  157. if(!isExist){
  158. rst.push(tempDelArr[i].org);
  159. }
  160. }
  161. return rst;
  162. },
  163. addCoeItems: function(coeIDs) {
  164. var me = this;
  165. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  166. var curCache = me.cache["_Coe_" + me.curRation.ID];
  167. var temp = [];
  168. if (curCache) {
  169. for (var i = 0; i < coeIDs.length; i++) {
  170. var isExist = false;
  171. for (let obj of curCache) {
  172. if (obj.ID == coeIDs[i]) {
  173. isExist = true;
  174. break;
  175. };
  176. };
  177. if (!isExist) {
  178. temp.push(coeIDs[i]);
  179. };
  180. };
  181. }else{
  182. for (let obj of coeIDs){temp.push(obj)};
  183. };
  184. if(temp.length == 0){
  185. me.showCoeItems(me.curRation.ID);
  186. sheetCommonObj.lockCells(me.sheet, me.setting);
  187. }else{
  188. $.ajax({
  189. type:"POST",
  190. url:"api/getCoeItemsByIDs",
  191. data: {"data": JSON.stringify({"libID": me.libID, "coeIDs": temp})},
  192. dataType:"json",
  193. cache:false,
  194. timeout:5000,
  195. success:function(result){
  196. if (result) {
  197. var rstArr = [];
  198. for (let obj of result.data){rstArr.push(obj)};
  199. if (curCache) {
  200. curCache = curCache.concat(rstArr);
  201. }else{
  202. curCache = rstArr;
  203. }
  204. let recoveryArr = me.getRecoveryArr(me.tempDelArr, result.data);
  205. if(recoveryArr.length > 0){
  206. curCache = curCache.concat(recoveryArr);
  207. }
  208. me.cache["_Coe_" + me.curRation.ID] = curCache;
  209. me.updateCurRation();
  210. me.showCoeItems(me.curRation.ID);
  211. };
  212. sheetCommonObj.lockCells(me.sheet, me.setting);
  213. },
  214. error:function(err){
  215. alert(err);
  216. }
  217. });
  218. };
  219. },
  220. getCoeItems: function(ration, callback) {
  221. var me = this;
  222. me.curRation = ration;
  223. if (ration == undefined || ration.rationCoeList == undefined ||
  224. ration.rationCoeList.length == 0){return;};
  225. var coeList = ration.rationCoeList;
  226. var curCache = me.cache["_Coe_" + ration.ID];
  227. if (curCache && curCache.length > 0) {
  228. me.showCoeItems(ration.ID);
  229. sheetCommonObj.lockCells(me.sheet, me.setting);
  230. } else {
  231. var data = {"libID": me.libID, "coeIDs": coeList};
  232. $.ajax({
  233. type:"POST",
  234. url:"api/getCoeItemsByIDs",
  235. data: {"data": JSON.stringify(data)},
  236. dataType:"json",
  237. cache:false,
  238. timeout:5000,
  239. success:function(result){
  240. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  241. if (result.data) {
  242. var tempResult = [];
  243. for (let obj of result.data) {
  244. tempResult.push(obj);
  245. };
  246. me.cache["_Coe_" + ration.ID] = tempResult;
  247. me.showCoeItems(ration.ID);
  248. }
  249. sheetCommonObj.lockCells(me.sheet, me.setting);
  250. callback();
  251. },
  252. error:function(err){
  253. alert(err);
  254. }
  255. });
  256. };
  257. },
  258. showCoeItems: function(rationID) {
  259. var me = this;
  260. var curCache = me.cache["_Coe_" + rationID];
  261. if (curCache) {
  262. curCache.sort(function(a, b) {
  263. var rst = 0;
  264. if (a.ID > b.ID) rst = 1
  265. else if (a.ID < b.ID) rst = -1;
  266. return rst;
  267. });
  268. sheetsOprObj.showData(me.sheet, me.setting, curCache);
  269. }
  270. },
  271. updateCurRation: function() {
  272. var me = this, updateArr = [];
  273. if (me.curRation) {
  274. var rst = [];
  275. var curCache = me.cache["_Coe_" + me.curRation.ID];
  276. if (curCache) {
  277. for (let obj of curCache) {
  278. rst.push(obj.ID);
  279. };
  280. me.curRation.rationCoeList = rst;
  281. updateArr.push(me.curRation);
  282. rationOprObj.mixUpdateRequest(updateArr, [], []);
  283. };
  284. };
  285. }
  286. }