ration_coe.js 12 KB

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