ration_coe.js 12 KB

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