ration_coe.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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:50,dataCode:"serialNo", dataType: "Number", hAlign: 'center'},
  14. {headerName:"名称",headerWidth:280,dataCode:"name", dataType: "String", hAlign: 'left'},
  15. {headerName:"内容",headerWidth:150,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. },
  34. isDef: function (v) {
  35. return v !== undefined && v !== null;
  36. },
  37. upMove: function (thisObj, preObj, cell) {
  38. let me = this;
  39. let tempNo = thisObj.no;
  40. thisObj.no = preObj.no;
  41. preObj.no = tempNo;
  42. //ajax
  43. me.updateCurRation(function () {
  44. me.showCoeItems(me.curRation.ID);
  45. me.sheet.setActiveCell(cell.row, cell.col);
  46. });
  47. },
  48. downMove: function (thisObj, nextObj, cell) {
  49. let me = this;
  50. let tempNo = thisObj.no;
  51. thisObj.no = nextObj.no;
  52. nextObj.no = tempNo;
  53. me.updateCurRation(function () {
  54. me.showCoeItems(me.curRation.ID);
  55. me.sheet.setActiveCell(cell.row, cell.col);
  56. });
  57. },
  58. //本节所有使用此条件
  59. useAll: function (coe) {
  60. },
  61. sortByNo: function (cache) {
  62. cache.sort(function (a, b) {
  63. let rst = 0;
  64. if(a.no > b.no){
  65. rst = 1;
  66. }
  67. else if(a.no < b.no){
  68. rst = -1;
  69. }
  70. return rst;
  71. });
  72. },
  73. onClipboardPasting: function(sender, args) {
  74. var me = rationCoeOprObj;
  75. let rationSection = rationOprObj.getCache();
  76. let rationRow = rationOprObj.workBook.getActiveSheet().getSelections()[0].row;
  77. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  78. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation)) {
  79. args.cancel = true;
  80. }
  81. },
  82. onClipboardPasted: function(e, info) {
  83. var me = rationCoeOprObj;
  84. if (me.libID) {
  85. // 修改第一列(编号)
  86. if (info.cellRange.col == 0) {
  87. me.tempDelArr = [];
  88. var coeNos = [];
  89. var items = sheetCommonObj.analyzePasteData({header:[{dataCode: "serialNo"}] }, info);
  90. let curCache = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  91. let isRefresh = false;
  92. for(let i = 0, len = items.length; i < len; i++){
  93. if(!isNaN(items[i].serialNo)){
  94. let row = i + info.cellRange.row;
  95. //update
  96. if(row < curCache.length){
  97. let isExist = false;
  98. for(let j = 0, jLen = curCache.length; j < jLen; j++){
  99. if(items[i].serialNo === curCache[j].serialNo && j !== row){
  100. isExist = true;
  101. break;
  102. }
  103. }
  104. if(!isExist){
  105. me.tempDelArr.push({org: curCache[row], newNo: items[i].serialNo});
  106. coeNos.push(items[i].serialNo);
  107. }
  108. else{
  109. isRefresh = true;
  110. }
  111. }
  112. else{
  113. coeNos.push(items[i].serialNo);
  114. }
  115. }
  116. }
  117. //delete in front
  118. if(me.tempDelArr.length > 0){
  119. for(let i = 0, len = me.tempDelArr.length; i < len; i++){
  120. for(let j = 0; j < curCache.length; j++){
  121. if(me.tempDelArr[i].org.serialNo === curCache[j].serialNo){
  122. curCache.splice(j, 1);
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. me.addCoeItems(coeNos);
  129. if(isRefresh){
  130. me.showCoeItems(me.curRation.ID);
  131. }
  132. } else {
  133. //修改其它列。
  134. }
  135. }
  136. },
  137. onEditStarting: function (sender, args) {
  138. let me = rationCoeOprObj;
  139. let rationSection = rationOprObj.getCache();
  140. let rationRow = rationOprObj.workBook.getActiveSheet().getSelections()[0].row;
  141. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  142. if(!me.curRation || args.col !== 0){
  143. args.cancel = true;
  144. }
  145. },
  146. onEditEnded: function(sender, args){
  147. var me = rationCoeOprObj;
  148. if (args.col == 0 && args.editingText && args.editingText.toString().trim().length > 0 && !isNaN(args.editingText)) { // 编号列
  149. let curCahe = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  150. me.tempDelArr = [];
  151. //update
  152. if(args.row < curCahe.length && args.editingText != curCahe[args.row].serialNo){
  153. let isExist = false;
  154. for(let i = 0, len = curCahe.length; i < len; i++){
  155. if(args.editingText == curCahe[i].serialNo){
  156. isExist = true;
  157. break;
  158. }
  159. }
  160. if(!isExist){
  161. me.tempDelArr.push({org: curCahe[args.row], newNo: args.editingText});
  162. curCahe.splice(args.row, 1);
  163. me.addCoeItems([args.editingText]);
  164. }
  165. else{
  166. me.showCoeItems(me.curRation.ID);
  167. }
  168. }
  169. //insert
  170. else{
  171. me.addCoeItems([args.editingText]);
  172. }
  173. }
  174. else{
  175. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  176. me.showCoeItems(me.curRation.ID);
  177. }
  178. },
  179. bindRationCoeDel: function () {
  180. if (locked) {
  181. return;
  182. }
  183. let me = rationCoeOprObj;
  184. let workBook = me.sheet.getParent();
  185. workBook.commandManager().register('rationCoeDel', function () {
  186. let sels = me.sheet.getSelections(), isUpdate = false;
  187. let curCahe = me.cache["_Coe_" + me.curRation.ID];
  188. for(let i = 0, len = sels.length; i < len; i ++ ){
  189. if(sels[i].colCount === me.setting.header.length){
  190. if(sels[i].row < curCahe.length){
  191. isUpdate = true;
  192. curCahe.splice(sels[i].row, sels[i].rowCount);
  193. }
  194. }
  195. }
  196. if(isUpdate){
  197. me.updateCurRation(function () {
  198. me.sheet.getParent().focus(true);
  199. });
  200. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  201. me.showCoeItems(me.curRation.ID);
  202. }
  203. });
  204. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  205. workBook.commandManager().setShortcutKey('rationCoeDel', GC.Spread.Commands.Key.del, false, false, false, false);
  206. },
  207. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的coe不存在,恢复删除的被更新数据
  208. let rst = [];
  209. for(let i = 0, len = tempDelArr.length; i < len; i++){
  210. let isExist = false;
  211. for(let j = 0, jLen = newArr.length; j < jLen; j++){
  212. if(tempDelArr[i].newNo == newArr[j].serialNo){
  213. isExist = true;
  214. break;
  215. }
  216. }
  217. if(!isExist){
  218. rst.push(tempDelArr[i].org);
  219. }
  220. }
  221. return rst;
  222. },
  223. addCoeItems: function(coeNos) {
  224. var me = this;
  225. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  226. var curCache = me.cache["_Coe_" + me.curRation.ID];
  227. var temp = [];
  228. if (curCache) {
  229. for (var i = 0; i < coeNos.length; i++) {
  230. var isExist = false;
  231. for (let obj of curCache) {
  232. if (obj.serialNo == coeNos[i]) {
  233. isExist = true;
  234. break;
  235. };
  236. };
  237. if (!isExist) {
  238. temp.push(coeNos[i]);
  239. };
  240. };
  241. }else{
  242. for (let obj of coeNos){temp.push(obj)};
  243. };
  244. if(temp.length == 0){
  245. me.showCoeItems(me.curRation.ID);
  246. //sheetCommonObj.lockCells(me.sheet, me.setting);
  247. }else{
  248. $.ajax({
  249. type:"POST",
  250. url:"api/getCoeItemsByNos",
  251. data: {"data": JSON.stringify({"libID": me.libID, "coeNos": temp})},
  252. dataType:"json",
  253. cache:false,
  254. timeout:5000,
  255. success:function(result){
  256. if (result) {
  257. var rstArr = [];
  258. let newNo = curCache && curCache.length > 0 ? curCache[curCache.length - 1].no + 1 : 1;
  259. for (let obj of result.data){
  260. obj.no = newNo++;
  261. rstArr.push(obj)
  262. }
  263. if (curCache) {
  264. curCache = curCache.concat(rstArr);
  265. }else{
  266. curCache = rstArr;
  267. }
  268. let recoveryArr = me.getRecoveryArr(me.tempDelArr, result.data);
  269. if(recoveryArr.length > 0){
  270. curCache = curCache.concat(recoveryArr);
  271. }
  272. me.cache["_Coe_" + me.curRation.ID] = curCache;
  273. me.updateCurRation(function () {
  274. me.sheet.getParent().focus(true);
  275. });
  276. me.showCoeItems(me.curRation.ID);
  277. };
  278. //sheetCommonObj.lockCells(me.sheet, me.setting);
  279. },
  280. error:function(err){
  281. alert(err);
  282. }
  283. });
  284. };
  285. },
  286. getCoeItems: function(ration, callback) {
  287. var me = this;
  288. me.curRation = ration;
  289. /*if (ration == undefined || ration.rationCoeList == undefined ||
  290. ration.rationCoeList.length == 0){return;};*/
  291. var coeList = ration.rationCoeList;
  292. let coeIDs = [];
  293. for(let i = 0, len = coeList.length; i < len; i++){
  294. coeIDs.push(coeList[i].ID);
  295. }
  296. var curCache = me.cache["_Coe_" + ration.ID];
  297. if (curCache) {
  298. me.showCoeItems(ration.ID);
  299. //sheetCommonObj.lockCells(me.sheet, me.setting);
  300. } else if(!curCache && typeof coeList !== 'undefined' && coeList.length > 0) {
  301. var data = {"libID": me.libID, "coeIDs": coeIDs};
  302. $.ajax({
  303. type:"POST",
  304. url:"api/getCoeItemsByIDs",
  305. data: {"data": JSON.stringify(data)},
  306. dataType:"json",
  307. cache:false,
  308. timeout:5000,
  309. success:function(result){
  310. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  311. if (result.data) {
  312. var tempResult = [];
  313. let stdMap = _.keyBy(result.data,'ID');
  314. for(let i = 0, len = coeList.length; i < len; i++){
  315. let obj = stdMap[coeList[i].ID];
  316. if(obj){
  317. obj.no = coeList[i].no;
  318. }else{
  319. obj= {ID:coeList[i].ID,no:coeList[i].no,serialNo:-999,name:'引用错误或子目已被删除'}
  320. }
  321. tempResult.push(obj);
  322. }
  323. me.cache["_Coe_" + ration.ID] = tempResult;
  324. me.showCoeItems(ration.ID);
  325. }
  326. //sheetCommonObj.lockCells(me.sheet, me.setting);
  327. if(callback) callback();
  328. },
  329. error:function(err){
  330. alert(err);
  331. }
  332. });
  333. };
  334. },
  335. showCoeItems: function(rationID) {
  336. var me = this;
  337. var curCache = me.cache["_Coe_" + rationID];
  338. if (curCache) {
  339. curCache.sort(function(a, b) {
  340. var rst = 0;
  341. if (a.no > b.no) rst = 1
  342. else if (a.no < b.no) rst = -1;
  343. return rst;
  344. });
  345. sheetsOprObj.showData(me.sheet, me.setting, curCache);
  346. }
  347. },
  348. updateCurRation: function(callback) {
  349. var me = this, updateArr = [];
  350. if (me.curRation) {
  351. var rst = [];
  352. var curCache = me.cache["_Coe_" + me.curRation.ID];
  353. if (curCache) {
  354. for (let obj of curCache) {
  355. rst.push({ID: obj.ID, no: obj.no});
  356. }
  357. me.curRation.rationCoeList = rst;
  358. updateArr.push(me.curRation);
  359. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  360. if(callback) callback();
  361. });
  362. }
  363. }
  364. },
  365. //更新本节所有定额
  366. updateSectionRation: function (rations, coe, callback) {
  367. let me = this;
  368. let updateArr = [];
  369. for(let i = 0, len = rations.length; i < len; i++){
  370. if(rations[i].ID !== me.curRation.ID){
  371. let rationCoeList = rations[i].rationCoeList;
  372. let isExist = false;
  373. let newNo = 1;
  374. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  375. if(rationCoeList[j].no >= newNo){
  376. newNo = rationCoeList[j].no + 1;
  377. }
  378. if(rationCoeList[j].ID === coe.ID){
  379. isExist = true;
  380. }
  381. }
  382. if(!isExist){
  383. rationCoeList.push({ID: coe.ID, no: newNo});
  384. updateArr.push(rations[i]);
  385. }
  386. }
  387. }
  388. if(updateArr.length > 0){
  389. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  390. if(callback) callback(updateArr);
  391. });
  392. }
  393. }
  394. }