ration_coe.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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:"serialNo", dataType: "Number", hAlign: 'left'},
  14. {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String", hAlign: 'left'},
  15. {headerName:"内容",headerWidth:300,dataCode:"content", dataType: "String", hAlign: 'left'}
  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 coeNos = [];
  46. var items = sheetCommonObj.analyzePasteData({header:[{dataCode: "serialNo"}] }, 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. if(!isNaN(items[i].serialNo)){
  51. let row = i + info.cellRange.row;
  52. //update
  53. if(row < curCache.length){
  54. let isExist = false;
  55. for(let j = 0, jLen = curCache.length; j < jLen; j++){
  56. if(items[i].serialNo === curCache[j].serialNo && j !== row){
  57. isExist = true;
  58. break;
  59. }
  60. }
  61. if(!isExist){
  62. me.tempDelArr.push({org: curCache[row], newNo: items[i].serialNo});
  63. coeNos.push(items[i].serialNo);
  64. }
  65. else{
  66. isRefresh = true;
  67. }
  68. }
  69. else{
  70. coeNos.push(items[i].serialNo);
  71. }
  72. }
  73. }
  74. //delete in front
  75. if(me.tempDelArr.length > 0){
  76. for(let i = 0, len = me.tempDelArr.length; i < len; i++){
  77. for(let j = 0; j < curCache.length; j++){
  78. if(me.tempDelArr[i].org.serialNo === curCache[j].serialNo){
  79. curCache.splice(j, 1);
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. me.addCoeItems(coeNos);
  86. if(isRefresh){
  87. me.showCoeItems(me.curRation.ID);
  88. }
  89. } else {
  90. //修改其它列。
  91. }
  92. }
  93. },
  94. onEditEnded: function(sender, args){
  95. var me = rationCoeOprObj;
  96. if (args.col == 0 && args.editingText && args.editingText.toString().trim().length > 0 && !isNaN(args.editingText)) { // 编号列
  97. let curCahe = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  98. me.tempDelArr = [];
  99. //update
  100. if(args.row < curCahe.length && args.editingText != curCahe[args.row].serialNo){
  101. let isExist = false;
  102. for(let i = 0, len = curCahe.length; i < len; i++){
  103. if(args.editingText == curCahe[i].serialNo){
  104. isExist = true;
  105. break;
  106. }
  107. }
  108. if(!isExist){
  109. me.tempDelArr.push({org: curCahe[args.row], newNo: args.editingText});
  110. curCahe.splice(args.row, 1);
  111. me.addCoeItems([args.editingText]);
  112. }
  113. else{
  114. me.showCoeItems(me.curRation.ID);
  115. }
  116. }
  117. //insert
  118. else{
  119. me.addCoeItems([args.editingText]);
  120. }
  121. }
  122. else{
  123. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  124. me.showCoeItems(me.curRation.ID);
  125. }
  126. },
  127. bindRationCoeDel: function () {
  128. let me = rationCoeOprObj;
  129. let workBook = me.sheet.getParent();
  130. workBook.commandManager().register('rationCoeDel', function () {
  131. let sels = me.sheet.getSelections(), isUpdate = false;
  132. let curCahe = me.cache["_Coe_" + me.curRation.ID];
  133. for(let i = 0, len = sels.length; i < len; i ++ ){
  134. if(sels[i].colCount === me.setting.header.length){
  135. if(sels[i].row < curCahe.length){
  136. isUpdate = true;
  137. curCahe.splice(sels[i].row, sels[i].rowCount);
  138. }
  139. }
  140. }
  141. if(isUpdate){
  142. me.updateCurRation(function () {
  143. me.sheet.getParent().focus(true);
  144. });
  145. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  146. me.showCoeItems(me.curRation.ID);
  147. }
  148. });
  149. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  150. workBook.commandManager().setShortcutKey('rationCoeDel', GC.Spread.Commands.Key.del, false, false, false, false);
  151. },
  152. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的coe不存在,恢复删除的被更新数据
  153. let rst = [];
  154. for(let i = 0, len = tempDelArr.length; i < len; i++){
  155. let isExist = false;
  156. for(let j = 0, jLen = newArr.length; j < jLen; j++){
  157. if(tempDelArr[i].newNo == newArr[j].serialNo){
  158. isExist = true;
  159. break;
  160. }
  161. }
  162. if(!isExist){
  163. rst.push(tempDelArr[i].org);
  164. }
  165. }
  166. return rst;
  167. },
  168. addCoeItems: function(coeNos) {
  169. var me = this;
  170. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  171. var curCache = me.cache["_Coe_" + me.curRation.ID];
  172. var temp = [];
  173. if (curCache) {
  174. for (var i = 0; i < coeNos.length; i++) {
  175. var isExist = false;
  176. for (let obj of curCache) {
  177. if (obj.serialNo == coeNos[i]) {
  178. isExist = true;
  179. break;
  180. };
  181. };
  182. if (!isExist) {
  183. temp.push(coeNos[i]);
  184. };
  185. };
  186. }else{
  187. for (let obj of coeNos){temp.push(obj)};
  188. };
  189. if(temp.length == 0){
  190. me.showCoeItems(me.curRation.ID);
  191. sheetCommonObj.lockCells(me.sheet, me.setting);
  192. }else{
  193. $.ajax({
  194. type:"POST",
  195. url:"api/getCoeItemsByNos",
  196. data: {"data": JSON.stringify({"libID": me.libID, "coeNos": temp})},
  197. dataType:"json",
  198. cache:false,
  199. timeout:5000,
  200. success:function(result){
  201. if (result) {
  202. var rstArr = [];
  203. for (let obj of result.data){rstArr.push(obj)};
  204. if (curCache) {
  205. curCache = curCache.concat(rstArr);
  206. }else{
  207. curCache = rstArr;
  208. }
  209. let recoveryArr = me.getRecoveryArr(me.tempDelArr, result.data);
  210. if(recoveryArr.length > 0){
  211. curCache = curCache.concat(recoveryArr);
  212. }
  213. me.cache["_Coe_" + me.curRation.ID] = curCache;
  214. me.updateCurRation(function () {
  215. me.sheet.getParent().focus(true);
  216. });
  217. me.showCoeItems(me.curRation.ID);
  218. };
  219. sheetCommonObj.lockCells(me.sheet, me.setting);
  220. },
  221. error:function(err){
  222. alert(err);
  223. }
  224. });
  225. };
  226. },
  227. getCoeItems: function(ration, callback) {
  228. var me = this;
  229. me.curRation = ration;
  230. /*if (ration == undefined || ration.rationCoeList == undefined ||
  231. ration.rationCoeList.length == 0){return;};*/
  232. var coeList = ration.rationCoeList;
  233. console.log(`me.cache["_Coe_" + ration.ID]`);
  234. console.log(me.cache["_Coe_" + ration.ID]);
  235. var curCache = me.cache["_Coe_" + ration.ID];
  236. if (curCache) {
  237. me.showCoeItems(ration.ID);
  238. sheetCommonObj.lockCells(me.sheet, me.setting);
  239. } else {
  240. var data = {"libID": me.libID, "coeIDs": coeList};
  241. $.ajax({
  242. type:"POST",
  243. url:"api/getCoeItemsByIDs",
  244. data: {"data": JSON.stringify(data)},
  245. dataType:"json",
  246. cache:false,
  247. timeout:5000,
  248. success:function(result){
  249. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  250. if (result.data) {
  251. var tempResult = [];
  252. for (let obj of result.data) {
  253. tempResult.push(obj);
  254. };
  255. me.cache["_Coe_" + ration.ID] = tempResult;
  256. me.showCoeItems(ration.ID);
  257. }
  258. sheetCommonObj.lockCells(me.sheet, me.setting);
  259. if(callback) callback();
  260. },
  261. error:function(err){
  262. alert(err);
  263. }
  264. });
  265. };
  266. },
  267. showCoeItems: function(rationID) {
  268. var me = this;
  269. var curCache = me.cache["_Coe_" + rationID];
  270. if (curCache) {
  271. curCache.sort(function(a, b) {
  272. var rst = 0;
  273. if (a.serialNo > b.serialNo) rst = 1
  274. else if (a.serialNo < b.serialNo) rst = -1;
  275. return rst;
  276. });
  277. sheetsOprObj.showData(me.sheet, me.setting, curCache);
  278. }
  279. },
  280. updateCurRation: function(callback) {
  281. var me = this, updateArr = [];
  282. if (me.curRation) {
  283. var rst = [];
  284. var curCache = me.cache["_Coe_" + me.curRation.ID];
  285. if (curCache) {
  286. for (let obj of curCache) {
  287. rst.push(obj.ID);
  288. };
  289. me.curRation.rationCoeList = rst;
  290. updateArr.push(me.curRation);
  291. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  292. if(callback) callback();
  293. });
  294. };
  295. };
  296. }
  297. }