ration_coe.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. 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. me.onContextmenuOpr();
  35. },
  36. isDef: function (v) {
  37. return v !== undefined && v !== null;
  38. },
  39. onContextmenuOpr: function () {
  40. let me = rationCoeOprObj;
  41. $.contextMenu({
  42. selector: '#rdSpread',
  43. build: function($triggerElement, e){
  44. //控制允许右键菜单在哪个位置出现
  45. let sheet = me.sheet;
  46. let offset = $("#rdSpread").offset(),
  47. x = e.pageX - offset.left,
  48. y = e.pageY - offset.top;
  49. let target = sheet.hitTest(x, y);
  50. if(sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  51. let currentCache = me.isDef(me.cache["_Coe_" + me.curRation.ID]) ? me.cache["_Coe_" + me.curRation.ID] : [];
  52. sheet.setActiveCell(target.row, target.col);
  53. //控制按钮是否可用
  54. let upDis = false,
  55. downDis = false,
  56. refDis = false;
  57. if(target.row >= currentCache.length){
  58. upDis = true;
  59. downDis = true;
  60. refDis = true;
  61. }
  62. else {
  63. if(!me.isDef(currentCache[target.row - 1])){
  64. upDis = true;
  65. }
  66. if(!me.isDef(currentCache[target.row + 1])){
  67. downDis = true;
  68. }
  69. }
  70. return {
  71. callback: function(){},
  72. items: {
  73. "upMove": {name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) {
  74. me.upMove(currentCache[target.row], currentCache[target.row - 1], {row: target.row - 1, col: target.col});
  75. }},
  76. "downMove": {name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) {
  77. me.downMove(currentCache[target.row], currentCache[target.row + 1], {row: target.row + 1, col: target.col});
  78. }},
  79. "ref": {name: "添加到本节其他定额", disabled: refDis, icon: "fa-arrow-left", callback: function (key, opt) {
  80. me.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  81. for(let i = 0, len = updateArr.length; i < len; i++){
  82. let ration = updateArr[i];
  83. let rationCoeList = updateArr[i].rationCoeList;
  84. let newNo = 1;
  85. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  86. if(rationCoeList[j].no >= newNo){
  87. newNo = rationCoeList[j].no + 1;
  88. }
  89. }
  90. let theCache = me.cache["_Coe_" + ration.ID];
  91. if(theCache !== undefined && theCache !== null){
  92. let newCoe = {};
  93. for(let attr in currentCache[target.row]){
  94. newCoe[attr] = currentCache[target.row][attr];
  95. }
  96. newCoe.no = newNo;
  97. theCache.push(newCoe);
  98. }
  99. }
  100. });
  101. }}
  102. }
  103. };
  104. }
  105. else{
  106. return false;
  107. }
  108. }
  109. });
  110. },
  111. upMove: function (thisObj, preObj, cell) {
  112. let me = this;
  113. let tempNo = thisObj.no;
  114. thisObj.no = preObj.no;
  115. preObj.no = tempNo;
  116. //ajax
  117. me.updateCurRation(function () {
  118. me.showCoeItems(me.curRation.ID);
  119. me.sheet.setActiveCell(cell.row, cell.col);
  120. });
  121. },
  122. downMove: function (thisObj, nextObj, cell) {
  123. let me = this;
  124. let tempNo = thisObj.no;
  125. thisObj.no = nextObj.no;
  126. nextObj.no = tempNo;
  127. me.updateCurRation(function () {
  128. me.showCoeItems(me.curRation.ID);
  129. me.sheet.setActiveCell(cell.row, cell.col);
  130. });
  131. },
  132. //本节所有使用此条件
  133. useAll: function (coe) {
  134. },
  135. sortByNo: function (cache) {
  136. cache.sort(function (a, b) {
  137. let rst = 0;
  138. if(a.no > b.no){
  139. rst = 1;
  140. }
  141. else if(a.no < b.no){
  142. rst = -1;
  143. }
  144. return rst;
  145. });
  146. },
  147. onClipboardPasting: function(sender, args) {
  148. var me = rationCoeOprObj;
  149. let rationSection = rationOprObj.getCache();
  150. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  151. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  152. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation)) {
  153. args.cancel = true;
  154. }
  155. },
  156. onClipboardPasted: function(e, info) {
  157. var me = rationCoeOprObj;
  158. if (me.libID) {
  159. // 修改第一列(编号)
  160. if (info.cellRange.col == 0) {
  161. me.tempDelArr = [];
  162. var coeNos = [];
  163. var items = sheetCommonObj.analyzePasteData({header:[{dataCode: "serialNo"}] }, info);
  164. let curCache = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  165. let isRefresh = false;
  166. for(let i = 0, len = items.length; i < len; i++){
  167. if(!isNaN(items[i].serialNo)){
  168. let row = i + info.cellRange.row;
  169. //update
  170. if(row < curCache.length){
  171. let isExist = false;
  172. for(let j = 0, jLen = curCache.length; j < jLen; j++){
  173. if(items[i].serialNo === curCache[j].serialNo && j !== row){
  174. isExist = true;
  175. break;
  176. }
  177. }
  178. if(!isExist){
  179. me.tempDelArr.push({org: curCache[row], newNo: items[i].serialNo});
  180. coeNos.push(items[i].serialNo);
  181. }
  182. else{
  183. isRefresh = true;
  184. }
  185. }
  186. else{
  187. coeNos.push(items[i].serialNo);
  188. }
  189. }
  190. }
  191. //delete in front
  192. if(me.tempDelArr.length > 0){
  193. for(let i = 0, len = me.tempDelArr.length; i < len; i++){
  194. for(let j = 0; j < curCache.length; j++){
  195. if(me.tempDelArr[i].org.serialNo === curCache[j].serialNo){
  196. curCache.splice(j, 1);
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. me.addCoeItems(coeNos);
  203. if(isRefresh){
  204. me.showCoeItems(me.curRation.ID);
  205. }
  206. } else {
  207. //修改其它列。
  208. }
  209. }
  210. },
  211. onEditStarting: function (sender, args) {
  212. let me = rationCoeOprObj;
  213. let rationSection = rationOprObj.getCache();
  214. let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
  215. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  216. if(!me.curRation || args.col !== 0){
  217. args.cancel = true;
  218. }
  219. },
  220. onEditEnded: function(sender, args){
  221. var me = rationCoeOprObj;
  222. if (args.col == 0 && args.editingText && args.editingText.toString().trim().length > 0 && !isNaN(args.editingText)) { // 编号列
  223. let curCahe = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  224. me.tempDelArr = [];
  225. //update
  226. if(args.row < curCahe.length && args.editingText != curCahe[args.row].serialNo){
  227. let isExist = false;
  228. for(let i = 0, len = curCahe.length; i < len; i++){
  229. if(args.editingText == curCahe[i].serialNo){
  230. isExist = true;
  231. break;
  232. }
  233. }
  234. if(!isExist){
  235. me.tempDelArr.push({org: curCahe[args.row], newNo: args.editingText});
  236. curCahe.splice(args.row, 1);
  237. me.addCoeItems([args.editingText]);
  238. }
  239. else{
  240. me.showCoeItems(me.curRation.ID);
  241. }
  242. }
  243. //insert
  244. else{
  245. me.addCoeItems([args.editingText]);
  246. }
  247. }
  248. else{
  249. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  250. me.showCoeItems(me.curRation.ID);
  251. }
  252. },
  253. bindRationCoeDel: function () {
  254. let me = rationCoeOprObj;
  255. let workBook = me.sheet.getParent();
  256. workBook.commandManager().register('rationCoeDel', function () {
  257. let sels = me.sheet.getSelections(), isUpdate = false;
  258. let curCahe = me.cache["_Coe_" + me.curRation.ID];
  259. for(let i = 0, len = sels.length; i < len; i ++ ){
  260. if(sels[i].colCount === me.setting.header.length){
  261. if(sels[i].row < curCahe.length){
  262. isUpdate = true;
  263. curCahe.splice(sels[i].row, sels[i].rowCount);
  264. }
  265. }
  266. }
  267. if(isUpdate){
  268. me.updateCurRation(function () {
  269. me.sheet.getParent().focus(true);
  270. });
  271. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  272. me.showCoeItems(me.curRation.ID);
  273. }
  274. });
  275. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  276. workBook.commandManager().setShortcutKey('rationCoeDel', GC.Spread.Commands.Key.del, false, false, false, false);
  277. },
  278. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的coe不存在,恢复删除的被更新数据
  279. let rst = [];
  280. for(let i = 0, len = tempDelArr.length; i < len; i++){
  281. let isExist = false;
  282. for(let j = 0, jLen = newArr.length; j < jLen; j++){
  283. if(tempDelArr[i].newNo == newArr[j].serialNo){
  284. isExist = true;
  285. break;
  286. }
  287. }
  288. if(!isExist){
  289. rst.push(tempDelArr[i].org);
  290. }
  291. }
  292. return rst;
  293. },
  294. addCoeItems: function(coeNos) {
  295. var me = this;
  296. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  297. var curCache = me.cache["_Coe_" + me.curRation.ID];
  298. var temp = [];
  299. if (curCache) {
  300. for (var i = 0; i < coeNos.length; i++) {
  301. var isExist = false;
  302. for (let obj of curCache) {
  303. if (obj.serialNo == coeNos[i]) {
  304. isExist = true;
  305. break;
  306. };
  307. };
  308. if (!isExist) {
  309. temp.push(coeNos[i]);
  310. };
  311. };
  312. }else{
  313. for (let obj of coeNos){temp.push(obj)};
  314. };
  315. if(temp.length == 0){
  316. me.showCoeItems(me.curRation.ID);
  317. //sheetCommonObj.lockCells(me.sheet, me.setting);
  318. }else{
  319. $.ajax({
  320. type:"POST",
  321. url:"api/getCoeItemsByNos",
  322. data: {"data": JSON.stringify({"libID": me.libID, "coeNos": temp})},
  323. dataType:"json",
  324. cache:false,
  325. timeout:5000,
  326. success:function(result){
  327. if (result) {
  328. var rstArr = [];
  329. let newNo = curCache && curCache.length > 0 ? curCache[curCache.length - 1].no + 1 : 1;
  330. for (let obj of result.data){
  331. obj.no = newNo++;
  332. rstArr.push(obj)
  333. }
  334. if (curCache) {
  335. curCache = curCache.concat(rstArr);
  336. }else{
  337. curCache = rstArr;
  338. }
  339. let recoveryArr = me.getRecoveryArr(me.tempDelArr, result.data);
  340. if(recoveryArr.length > 0){
  341. curCache = curCache.concat(recoveryArr);
  342. }
  343. me.cache["_Coe_" + me.curRation.ID] = curCache;
  344. me.updateCurRation(function () {
  345. me.sheet.getParent().focus(true);
  346. });
  347. me.showCoeItems(me.curRation.ID);
  348. };
  349. //sheetCommonObj.lockCells(me.sheet, me.setting);
  350. },
  351. error:function(err){
  352. alert(err);
  353. }
  354. });
  355. };
  356. },
  357. getCoeItems: function(ration, callback) {
  358. var me = this;
  359. me.curRation = ration;
  360. /*if (ration == undefined || ration.rationCoeList == undefined ||
  361. ration.rationCoeList.length == 0){return;};*/
  362. var coeList = ration.rationCoeList;
  363. let coeIDs = [];
  364. for(let i = 0, len = coeList.length; i < len; i++){
  365. coeIDs.push(coeList[i].ID);
  366. }
  367. var curCache = me.cache["_Coe_" + ration.ID];
  368. if (curCache) {
  369. me.showCoeItems(ration.ID);
  370. //sheetCommonObj.lockCells(me.sheet, me.setting);
  371. } else if(!curCache && typeof coeList !== 'undefined' && coeList.length > 0) {
  372. var data = {"libID": me.libID, "coeIDs": coeIDs};
  373. $.ajax({
  374. type:"POST",
  375. url:"api/getCoeItemsByIDs",
  376. data: {"data": JSON.stringify(data)},
  377. dataType:"json",
  378. cache:false,
  379. timeout:5000,
  380. success:function(result){
  381. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  382. if (result.data) {
  383. var tempResult = [];
  384. for (let obj of result.data) {
  385. for(let i = 0, len = coeList.length; i < len; i++){
  386. if(obj.ID === coeList[i].ID){
  387. obj.no = coeList[i].no;
  388. tempResult.push(obj);
  389. break;
  390. }
  391. }
  392. };
  393. me.cache["_Coe_" + ration.ID] = tempResult;
  394. me.showCoeItems(ration.ID);
  395. }
  396. //sheetCommonObj.lockCells(me.sheet, me.setting);
  397. if(callback) callback();
  398. },
  399. error:function(err){
  400. alert(err);
  401. }
  402. });
  403. };
  404. },
  405. showCoeItems: function(rationID) {
  406. var me = this;
  407. var curCache = me.cache["_Coe_" + rationID];
  408. if (curCache) {
  409. curCache.sort(function(a, b) {
  410. var rst = 0;
  411. if (a.no > b.no) rst = 1
  412. else if (a.no < b.no) rst = -1;
  413. return rst;
  414. });
  415. sheetsOprObj.showData(me.sheet, me.setting, curCache);
  416. }
  417. },
  418. updateCurRation: function(callback) {
  419. var me = this, updateArr = [];
  420. if (me.curRation) {
  421. var rst = [];
  422. var curCache = me.cache["_Coe_" + me.curRation.ID];
  423. if (curCache) {
  424. for (let obj of curCache) {
  425. rst.push({ID: obj.ID, no: obj.no});
  426. }
  427. me.curRation.rationCoeList = rst;
  428. updateArr.push(me.curRation);
  429. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  430. if(callback) callback();
  431. });
  432. }
  433. }
  434. },
  435. //更新本节所有定额
  436. updateSectionRation: function (rations, coe, callback) {
  437. let me = this;
  438. let updateArr = [];
  439. for(let i = 0, len = rations.length; i < len; i++){
  440. if(rations[i].ID !== me.curRation.ID){
  441. let rationCoeList = rations[i].rationCoeList;
  442. let isExist = false;
  443. let newNo = 1;
  444. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  445. if(rationCoeList[j].no >= newNo){
  446. newNo = rationCoeList[j].no + 1;
  447. }
  448. if(rationCoeList[j].ID === coe.ID){
  449. isExist = true;
  450. }
  451. }
  452. if(!isExist){
  453. rationCoeList.push({ID: coe.ID, no: newNo});
  454. updateArr.push(rations[i]);
  455. }
  456. }
  457. }
  458. if(updateArr.length > 0){
  459. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  460. if(callback) callback(updateArr);
  461. });
  462. }
  463. }
  464. }