ration_coe.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 = pageOprObj.rationLibId; // 不可靠,有时取不到
  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. },
  33. isDef: function (v) {
  34. return v !== undefined && v !== null;
  35. },
  36. onContextmenuOpr: function () {
  37. let raCoe = rationCoeOprObj;
  38. $.contextMenu({
  39. selector: '#rdSpread',
  40. build: function($triggerElement, e){
  41. //控制允许右键菜单在哪个位置出现
  42. let sheet = raCoe.sheet;
  43. let offset = $("#rdSpread").offset(),
  44. x = e.pageX - offset.left,
  45. y = e.pageY - offset.top;
  46. let target = sheet.hitTest(x, y);
  47. if(sheet.getParent().getActiveSheetIndex() === 2 && target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  48. let currentCache = raCoe.isDef(raCoe.cache["_Coe_" + raCoe.curRation.ID]) ? raCoe.cache["_Coe_" + raCoe.curRation.ID] : [];
  49. sheet.setActiveCell(target.row, target.col);
  50. //控制按钮是否可用
  51. let upDis = false,
  52. downDis = false,
  53. refDis = false;
  54. if(target.row >= currentCache.length){
  55. upDis = true;
  56. downDis = true;
  57. refDis = true;
  58. }
  59. else {
  60. if(!raCoe.isDef(currentCache[target.row - 1])){
  61. upDis = true;
  62. }
  63. if(!raCoe.isDef(currentCache[target.row + 1])){
  64. downDis = true;
  65. }
  66. }
  67. return {
  68. callback: function(){},
  69. items: {
  70. "upMove": {name: "上移", disabled: upDis, icon: "fa-arrow-up", callback: function (key, opt) {
  71. raCoe.upMove(currentCache[target.row], currentCache[target.row - 1], {row: target.row - 1, col: target.col});
  72. }},
  73. "downMove": {name: "下移", disabled: downDis, icon: "fa-arrow-down", callback: function (key, opt) {
  74. raCoe.downMove(currentCache[target.row], currentCache[target.row + 1], {row: target.row + 1, col: target.col});
  75. }},
  76. "ref": {name: "添加到本节其他定额", disabled: refDis, icon: "fa-arrow-left", callback: function (key, opt) {
  77. raCoe.updateSectionRation(rationOprObj.currentRations["_SEC_ID_" + rationOprObj.currentSectionId], currentCache[target.row], function (updateArr) {
  78. for(let i = 0, len = updateArr.length; i < len; i++){
  79. let ration = updateArr[i];
  80. let rationCoeList = updateArr[i].rationCoeList;
  81. let newNo = 1;
  82. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  83. if(rationCoeList[j].no >= newNo){
  84. newNo = rationCoeList[j].no + 1;
  85. }
  86. }
  87. let theCache = raCoe.cache["_Coe_" + ration.ID];
  88. if(theCache !== undefined && theCache !== null){
  89. let newCoe = {};
  90. for(let attr in currentCache[target.row]){
  91. newCoe[attr] = currentCache[target.row][attr];
  92. }
  93. newCoe.no = newNo;
  94. theCache.push(newCoe);
  95. }
  96. }
  97. });
  98. }}
  99. }
  100. };
  101. }
  102. else{
  103. return false;
  104. }
  105. }
  106. });
  107. },
  108. upMove: function (thisObj, preObj, cell) {
  109. let me = this;
  110. let tempNo = thisObj.no;
  111. thisObj.no = preObj.no;
  112. preObj.no = tempNo;
  113. //ajax
  114. me.updateCurRation(function () {
  115. me.showCoeItems(me.curRation.ID);
  116. me.sheet.setActiveCell(cell.row, cell.col);
  117. });
  118. },
  119. downMove: function (thisObj, nextObj, cell) {
  120. let me = this;
  121. let tempNo = thisObj.no;
  122. thisObj.no = nextObj.no;
  123. nextObj.no = tempNo;
  124. me.updateCurRation(function () {
  125. me.showCoeItems(me.curRation.ID);
  126. me.sheet.setActiveCell(cell.row, cell.col);
  127. });
  128. },
  129. //本节所有使用此条件
  130. useAll: function (coe) {
  131. },
  132. sortByNo: function (cache) {
  133. cache.sort(function (a, b) {
  134. let rst = 0;
  135. if(a.no > b.no){
  136. rst = 1;
  137. }
  138. else if(a.no < b.no){
  139. rst = -1;
  140. }
  141. return rst;
  142. });
  143. },
  144. onClipboardPasting: function(sender, args) {
  145. var me = rationCoeOprObj;
  146. let rationSection = rationOprObj.getCache();
  147. let rationRow = rationOprObj.workBook.getActiveSheet().getSelections()[0].row;
  148. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  149. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation)) {
  150. args.cancel = true;
  151. }
  152. },
  153. onClipboardPasted: function(e, info) {
  154. var me = rationCoeOprObj;
  155. if (me.libID) {
  156. // 修改第一列(编号)
  157. if (info.cellRange.col == 0) {
  158. me.tempDelArr = [];
  159. var coeNos = [];
  160. var items = sheetCommonObj.analyzePasteData({header:[{dataCode: "serialNo"}] }, info);
  161. let curCache = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  162. let isRefresh = false;
  163. for(let i = 0, len = items.length; i < len; i++){
  164. if(!isNaN(items[i].serialNo)){
  165. let row = i + info.cellRange.row;
  166. //update
  167. if(row < curCache.length){
  168. let isExist = false;
  169. for(let j = 0, jLen = curCache.length; j < jLen; j++){
  170. if(items[i].serialNo === curCache[j].serialNo && j !== row){
  171. isExist = true;
  172. break;
  173. }
  174. }
  175. if(!isExist){
  176. me.tempDelArr.push({org: curCache[row], newNo: items[i].serialNo});
  177. coeNos.push(items[i].serialNo);
  178. }
  179. else{
  180. isRefresh = true;
  181. }
  182. }
  183. else{
  184. coeNos.push(items[i].serialNo);
  185. }
  186. }
  187. }
  188. //delete in front
  189. if(me.tempDelArr.length > 0){
  190. for(let i = 0, len = me.tempDelArr.length; i < len; i++){
  191. for(let j = 0; j < curCache.length; j++){
  192. if(me.tempDelArr[i].org.serialNo === curCache[j].serialNo){
  193. curCache.splice(j, 1);
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. me.addCoeItems(coeNos);
  200. if(isRefresh){
  201. me.showCoeItems(me.curRation.ID);
  202. }
  203. } else {
  204. //修改其它列。
  205. }
  206. }
  207. },
  208. onEditStarting: function (sender, args) {
  209. let me = rationCoeOprObj;
  210. let rationSection = rationOprObj.getCache();
  211. let rationRow = rationOprObj.workBook.getActiveSheet().getSelections()[0].row;
  212. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  213. if(!me.curRation || args.col !== 0){
  214. args.cancel = true;
  215. }
  216. },
  217. onEditEnded: function(sender, args){
  218. var me = rationCoeOprObj;
  219. if (args.col == 0 && args.editingText && args.editingText.toString().trim().length > 0 && !isNaN(args.editingText)) { // 编号列
  220. let curCahe = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  221. me.tempDelArr = [];
  222. //update
  223. if(args.row < curCahe.length && args.editingText != curCahe[args.row].serialNo){
  224. let isExist = false;
  225. for(let i = 0, len = curCahe.length; i < len; i++){
  226. if(args.editingText == curCahe[i].serialNo){
  227. isExist = true;
  228. break;
  229. }
  230. }
  231. if(!isExist){
  232. me.tempDelArr.push({org: curCahe[args.row], newNo: args.editingText});
  233. curCahe.splice(args.row, 1);
  234. me.addCoeItems([args.editingText]);
  235. }
  236. else{
  237. me.showCoeItems(me.curRation.ID);
  238. }
  239. }
  240. //insert
  241. else{
  242. me.addCoeItems([args.editingText]);
  243. }
  244. }
  245. else{
  246. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  247. me.showCoeItems(me.curRation.ID);
  248. }
  249. },
  250. bindRationCoeDel: function () {
  251. if (locked) {
  252. return;
  253. }
  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. if(callback) callback();
  397. },
  398. error:function(err){
  399. alert(err);
  400. }
  401. });
  402. };
  403. },
  404. showCoeItems: function(rationID) {
  405. var me = this;
  406. var curCache = me.cache["_Coe_" + rationID];
  407. if (curCache) {
  408. curCache.sort(function(a, b) {
  409. var rst = 0;
  410. if (a.no > b.no) rst = 1
  411. else if (a.no < b.no) rst = -1;
  412. return rst;
  413. });
  414. sheetsOprObj.showData(me.sheet, me.setting, curCache);
  415. }
  416. },
  417. updateCurRation: function(callback) {
  418. var me = this, updateArr = [];
  419. if (me.curRation) {
  420. var rst = [];
  421. var curCache = me.cache["_Coe_" + me.curRation.ID];
  422. if (curCache) {
  423. for (let obj of curCache) {
  424. rst.push({ID: obj.ID, no: obj.no});
  425. }
  426. me.curRation.rationCoeList = rst;
  427. updateArr.push(me.curRation);
  428. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  429. if(callback) callback();
  430. });
  431. }
  432. }
  433. },
  434. //更新本节所有定额
  435. updateSectionRation: function (rations, coe, callback) {
  436. let me = this;
  437. let updateArr = [];
  438. for(let i = 0, len = rations.length; i < len; i++){
  439. if(rations[i].ID !== me.curRation.ID){
  440. let rationCoeList = rations[i].rationCoeList;
  441. let isExist = false;
  442. let newNo = 1;
  443. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  444. if(rationCoeList[j].no >= newNo){
  445. newNo = rationCoeList[j].no + 1;
  446. }
  447. if(rationCoeList[j].ID === coe.ID){
  448. isExist = true;
  449. }
  450. }
  451. if(!isExist){
  452. rationCoeList.push({ID: coe.ID, no: newNo});
  453. updateArr.push(rations[i]);
  454. }
  455. }
  456. }
  457. if(updateArr.length > 0){
  458. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  459. if(callback) callback(updateArr);
  460. });
  461. }
  462. }
  463. }