dbController.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /**
  2. * Created by vian on 2017/4/12.
  3. */
  4. var dbController = {
  5. insert: function(controller){
  6. if(controller.tree.items.length === 0){
  7. controller.tree.maxNodeID(0);
  8. controller.insert();
  9. billsAjax.createBills(billsLibId, controller.tree.selected.getID(), -1 , -1);
  10. }
  11. else {
  12. var node = controller.tree.selected;
  13. if(node){
  14. var updateId = node.getID(), createpid = node.getParentID(), createnid = node.getNextSiblingID();
  15. controller.insert();
  16. billsAjax.createBills(billsLibId, node.getNextSiblingID(), createpid, createnid, function(){
  17. billsAjax.updatePNId(billsLibId, [{ID: updateId, NextSiblingID: node.getNextSiblingID()}]);
  18. rechargeController.createRecharge(controller.tree.selected);
  19. });
  20. }
  21. else {
  22. var updateNode = controller.tree.roots[controller.tree.roots.length - 1];
  23. var updateId = updateNode.getID();
  24. controller.insert();
  25. billsAjax.createBills(billsLibId, updateNode.getNextSiblingID(), -1, -1, function(){
  26. billsAjax.updatePNId(billsLibId, [{ID: updateId, NextSiblingID: updateNode.getNextSiblingID()}]);
  27. rechargeController.createRecharge(controller.tree.selected);
  28. });
  29. }
  30. }
  31. },
  32. delete: function(controller, totalJobs, totalItems){
  33. var node = controller.tree.selected;
  34. var deleteIds = [];
  35. var getDeleteIds = function(node){
  36. if(node){
  37. deleteIds.push(node.getID());
  38. if(node.children.length > 0){
  39. for(var i=0; i<node.children.length; i++){
  40. getDeleteIds(node.children[i]);
  41. }
  42. }
  43. }
  44. };
  45. getDeleteIds(node);
  46. billsAjax.deleteBills(billsLibId, deleteIds, function(){
  47. if(node.preSibling){
  48. billsAjax.updatePNId(billsLibId, [{ID: node.preSibling.getID(), NextSiblingID: node.getNextSiblingID()}]);
  49. }
  50. });
  51. //controller.delete();
  52. tools.delteSheets(controller, totalJobs, totalItems, jobsSpread.getActiveSheet(), itemsSpread.getActiveSheet());
  53. },
  54. upLevel: function(controller){
  55. var node = controller.tree.selected;
  56. var ids = [];
  57. var updateData = [];
  58. var getNextSibling = function(node){
  59. if(node.nextSibling){
  60. ids.push(node.getNextSiblingID());
  61. getNextSibling(node.nextSibling);
  62. }
  63. }
  64. //node
  65. if(node){
  66. if(node.parent){
  67. var updateA = {
  68. ID: node.getID(),
  69. ParentID: node.parent.getParentID(),
  70. NextSiblingID: node.parent.getNextSiblingID()
  71. };
  72. updateData.push(updateA);
  73. //parent
  74. var updateB = {
  75. ID: node.getParentID(),
  76. NextSiblingID: node.getID()
  77. }
  78. updateData.push(updateB);
  79. if(node.nextSibling){
  80. getNextSibling(node);
  81. var updateC = {
  82. ID: ids,
  83. ParentID: node.getID()
  84. }
  85. updateData.push(updateC);
  86. }
  87. if(node.preSibling){
  88. var updateD = {
  89. ID: node.preSibling.getID(),
  90. NextSibingID: -1
  91. }
  92. updateData.push(updateD);
  93. }
  94. billsAjax.updatePNId(billsLibId, updateData);
  95. controller.upLevel();
  96. rechargeController.createRecharge(controller.tree.selected);
  97. }
  98. }
  99. },
  100. downLevel: function(controller){
  101. var node = controller.tree.selected;
  102. var updateData = [];
  103. if(node){
  104. if(node.preSibling){
  105. var updateA = {
  106. ID: node.preSibling.getID(),
  107. NextSiblingID: node.getNextSiblingID()
  108. };
  109. var updateB = {
  110. ID: node.getID(),
  111. ParentID: node.preSibling.getID(),
  112. NextSiblingID: -1
  113. };
  114. updateData.push(updateA);
  115. updateData.push(updateB);
  116. if(node.preSibling.children.length > 0){
  117. var updateC = {
  118. ID: node.preSibling.children[node.preSibling.children.length -1].getID(),
  119. NextSiblingID: node.getID()
  120. }
  121. updateData.push(updateC);
  122. }
  123. billsAjax.updatePNId(billsLibId, updateData);
  124. controller.downLevel();
  125. rechargeController.createRecharge(controller.tree.selected);
  126. }
  127. }
  128. },
  129. upMove: function(controller){
  130. var node = controller.tree.selected;
  131. var updateData = [];
  132. if(node){
  133. if(node.preSibling){
  134. var updateA = {
  135. ID: node.preSibling.getID(),
  136. NextSiblingID: node.getNextSiblingID()
  137. };
  138. var updateB = {
  139. ID: node.getID(),
  140. NextSiblingID: node.preSibling.getID()
  141. };
  142. updateData.push(updateA);
  143. updateData.push(updateB);
  144. if(node.preSibling.preSibling){
  145. var updateC = {
  146. ID: node.preSibling.preSibling.getID(),
  147. NextSiblingID: node.getID()
  148. }
  149. updateData.push(updateC);
  150. }
  151. billsAjax.updatePNId(billsLibId, updateData);
  152. controller.upMove();
  153. }
  154. }
  155. },
  156. downMove: function(controller){
  157. var node = controller.tree.selected;
  158. var updateData = [];
  159. if(node){
  160. if(node.nextSibling){
  161. var updateA = {
  162. ID: node.getNextSiblingID(),
  163. NextSiblingID: node.getID()
  164. };
  165. var updateB = {
  166. ID: node.getID(),
  167. NextSiblingID: node.nextSibling.getNextSiblingID()
  168. };
  169. updateData.push(updateA);
  170. updateData.push(updateB);
  171. if(node.preSibling){
  172. var updateC = {
  173. ID: node.preSibling.getID(),
  174. NextSiblingID: node.getNextSiblingID()
  175. };
  176. updateData.push(updateC);
  177. }
  178. billsAjax.updatePNId(billsLibId, updateData);
  179. controller.downMove();
  180. }
  181. }
  182. },
  183. editData: function(controller){
  184. controller.sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  185. var node = controller.tree.selected, updateId, field;
  186. if(node){
  187. updateId = node.getID();
  188. billsLibSetting.cols.forEach(function(col, idx){
  189. if(args.col === idx){
  190. field = col.data.field;
  191. node.data[field] = args.editingText;
  192. }
  193. });
  194. billsAjax.updateBills(billsLibId, updateId, field, args.editingText);
  195. }
  196. else {
  197. args.sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  198. }
  199. });
  200. }
  201. };
  202. var createObj = {
  203. newJob: function(data){
  204. var Job = function(data){
  205. this.data = data;
  206. //count = 0则没有被引用了
  207. this.count = 0;
  208. };
  209. return new Job(data);
  210. },
  211. newJobs: function(){
  212. var TotalJobs = function(){
  213. this.jobs = {};
  214. this.jobsArr = [];
  215. this.prefix = '_id';
  216. };
  217. TotalJobs.prototype.findJob = function(id){
  218. return this.jobs[this.prefix + id] ? this.jobs[this.prefix + id] : null;
  219. }
  220. TotalJobs.prototype.loadJobs = function (nodes, datas){
  221. var me = this;
  222. datas.forEach(function(jobData){
  223. var job = createObj.newJob(jobData);
  224. me.jobs[me.prefix + jobData.id] = job;
  225. me.jobsArr.push(job);
  226. });
  227. if(nodes){
  228. nodes.forEach(function(node){
  229. node.data.jobs.forEach(function(jobId){
  230. if(me.jobs[me.prefix + jobId]){
  231. node.jobs.push(me.jobs[me.prefix + jobId]);
  232. me.jobs[me.prefix + jobId].count ++;
  233. }
  234. else {
  235. node.data.jobs.splice(node.data.jobs.indexOf(jobId), 1);
  236. }
  237. });
  238. });
  239. }
  240. };
  241. TotalJobs.prototype.getUpdateIds = function(nodes, repeatId){
  242. var ids = [];
  243. nodes.forEach(function(node){
  244. node.jobs.forEach(function(job){
  245. if(job.data.id === repeatId){
  246. ids.push(node.getID());
  247. }
  248. });
  249. });
  250. return ids;
  251. };
  252. return new TotalJobs();
  253. },
  254. newItem: function(data){
  255. var Item = function(data){
  256. this.data = data;
  257. this.count = 0;
  258. };
  259. return new Item(data);
  260. },
  261. newItems: function(){
  262. var TotalItems = function(){
  263. this.items = {};
  264. this.itemsArr = [];
  265. this.prefix = '_id';
  266. };
  267. TotalItems.prototype.findItem = function(id){
  268. return this.items[this.prefix + id] ? this.items[this.prefix + id] : null;
  269. }
  270. TotalItems.prototype.loadItems = function (nodes, datas){
  271. var me = this;
  272. datas.forEach(function(itemData){
  273. var item = createObj.newItem(itemData);
  274. me.items[me.prefix + itemData.id] = item;
  275. me.itemsArr.push(item);
  276. });
  277. if(nodes){
  278. nodes.forEach(function(node){
  279. node.data.items.forEach(function(itemID){
  280. if(me.items[me.prefix + itemID]){
  281. node.items.push(me.items[me.prefix + itemID]);
  282. me.items[me.prefix + itemID].count ++;
  283. }
  284. else {
  285. node.data.items.splice(node.data.items.indexOf(itemID), 1);
  286. }
  287. });
  288. });
  289. }
  290. };
  291. TotalItems.prototype.getUpdateIds = function(nodes, repeatId){
  292. var ids = [];
  293. nodes.forEach(function(node){
  294. node.items.forEach(function(item){
  295. if(item.data.id === repeatId){
  296. ids.push(node.getID());
  297. }
  298. });
  299. });
  300. return ids;
  301. };
  302. return new TotalItems();
  303. }
  304. };
  305. var tools = {
  306. isExist: function (totalArr, field, newData, orgData){
  307. var isExist = false;
  308. if(totalArr.length > 0){
  309. totalArr.forEach(function(item){
  310. if(item.data[field] == newData && newData !== orgData){
  311. isExist = true;
  312. }
  313. });
  314. }
  315. return isExist;
  316. },
  317. isRepeat: function(arr, field, newData, ref){
  318. var isRepeat = false;
  319. if(arr){
  320. arr.forEach(function(item){
  321. if(ref === 'reference' && item.data[field] == newData){
  322. isRepeat = true;
  323. }
  324. else if(ref === 'document' && item[field] === newData){
  325. isRepeat = true;
  326. }
  327. });
  328. }
  329. return isRepeat;
  330. },
  331. addAttr: function(attr){
  332. if(!attr){
  333. attr = new Array();
  334. }
  335. },
  336. reSetCell: function(sheet, rowIdx, colIdx, value, id){
  337. sheet.getCell(rowIdx, colIdx, GC.Spread.Sheets.SheetArea.viewport).value(value);
  338. sheet.setTag(rowIdx, 0, id);
  339. sheet.setTag(rowIdx, 1, id);
  340. },
  341. getIndex: function(arr, id){
  342. var index;
  343. arr.forEach(function(item){
  344. if(item.data.id === id){
  345. index = arr.indexOf(item);
  346. }
  347. });
  348. return index;
  349. },
  350. delteSheets: function(controller, totalJobs, totalItems, jobsSheet, itemsSheet){
  351. var delJobs = controller.tree.selected.jobs;
  352. var delItems = controller.tree.selected.items;
  353. var delJobsIds = [];
  354. var delItemsIds = [];
  355. if(delJobs.length > 0){
  356. delJobs.forEach(function(job){
  357. job.count --;
  358. if(job.count <=0){
  359. totalJobs.jobsArr.splice(totalJobs.jobsArr.indexOf(job), 1);
  360. delJobsIds.push(job.data.id)
  361. }
  362. });
  363. jobsAjax.deleteJobContent(delJobsIds);
  364. tools.clearData(jobsSheet);
  365. tools.reshowData(jobsSheet, controller.tree.selected.jobs, jobsSetting, true);
  366. }
  367. if(delItems.length > 0){
  368. delItems.forEach(function(item){
  369. item.count --;
  370. if(item.count <=0){
  371. totalItems.itemsArr.splice(totalItems.itemsArr.indexOf(item), 1);
  372. delItemsIds.push(item.data.id)
  373. }
  374. });
  375. itemsAjax.deleteItemCharacter(delItemsIds);
  376. tools.clearData(itemsSheet);
  377. tools.reshowData(itemsSheet, controller.tree.selected.items, itemsSetting, true);
  378. }
  379. controller.delete();
  380. },
  381. deleteELes: function (arr, delIds, callback){
  382. var ids = [];
  383. delIds.forEach(function(delId){
  384. arr.forEach(function(ele){
  385. if(ele.data.id === delId){
  386. arr.splice(arr.indexOf(ele), 1);
  387. ele.count--;
  388. if(ele.count <= 0){
  389. ids.push(ele.data.id);
  390. }
  391. }
  392. });
  393. });
  394. if(callback){
  395. callback(ids);
  396. }
  397. },
  398. resort: function(arr, attr, isValue){
  399. //var arr = node.jobs;
  400. function compare(attr){
  401. return function (a, b){
  402. var valA, valB;
  403. if(isValue){
  404. valA = a[attr];
  405. valB = a[attr];
  406. }
  407. else {
  408. valA = a.data[attr];
  409. valB = b.data[attr];
  410. }
  411. return valA - valB;
  412. }
  413. }
  414. arr.sort(compare(attr));
  415. },
  416. reshowData: function(sheet, arr, setting, isResort){
  417. tools.clearData(sheet);
  418. if(isResort){
  419. tools.resort(arr, 'code', false);
  420. }
  421. if(arr.length > 0){
  422. for(var i=0; i<arr.length; i++){
  423. setting.cols.forEach(function(col, colIdx){
  424. sheet.setTag(i, colIdx, arr[i].data.id);
  425. if(arr[i].data[col.data.field]){
  426. sheet.getCell(i, colIdx).value(arr[i].data[col.data.field]);
  427. }
  428. else {
  429. sheet.getCell(i, colIdx).value('');
  430. }
  431. });
  432. }
  433. }
  434. },
  435. reshowValue: function(sheet, arr, setting, isResort){
  436. tools.clearData(sheet);
  437. if(isResort){
  438. tools.resort(arr, 'code', true);
  439. }
  440. if(arr.length > 0){
  441. for(var i=0; i<arr.length; i++){
  442. setting.cols.forEach(function(col, colIdx){
  443. sheet.setTag(i, colIdx, arr[i].code);
  444. if(arr[i][col.data.field]){
  445. sheet.getCell(i, colIdx).value(arr[i][col.data.field]);
  446. }
  447. else {
  448. sheet.getCell(i, colIdx).value('');
  449. }
  450. });
  451. }
  452. }
  453. },
  454. clearData: function(sheet){
  455. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
  456. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.tag);
  457. },
  458. getRoot: function(node){
  459. return node.parent ? tools.getRoot(node.parent) : node;
  460. },
  461. getBillsIds: function(callback){
  462. billsAjax.getBills(billsLibId, function(datas){
  463. var ids = [];
  464. datas.forEach(function(data){
  465. ids.push(data.ID);
  466. });
  467. if(callback){
  468. callback(ids);
  469. }
  470. });
  471. },
  472. delIds: function(sheet){
  473. var ids = [];
  474. var sels = sheet.getSelections();
  475. sels.forEach(function(sel){
  476. var orgRow = sel.row, rowCount = sel.rowCount, colCount = sel.colCount;
  477. var orgCol = sel.col === -1 ? 0 : sel.col;
  478. var maxRow = orgRow + rowCount - 1;
  479. for(var i=orgRow; i<= maxRow; i++){
  480. if(sheet.getTag(i, orgCol)){
  481. ids.push(sheet.getTag(i, orgCol));
  482. }
  483. }
  484. });
  485. return ids;
  486. },
  487. redirect: function(billsLibId, newHref){
  488. mainAjax.getABillsLib(billsLibId, function(result){
  489. if(result.length === 0){
  490. window.location.href = newHref;
  491. }
  492. });
  493. }
  494. }
  495. var jobsController = {
  496. editData: function(controller, sheet, totalJobs, setting){
  497. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  498. orgJobData = sheet.getCell(args.row, args.col).value();
  499. });
  500. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args) {
  501. var field, newData = args.editingText, id = sheet.getTag(args.row, args.col);
  502. setting.cols.forEach(function (col, idx) {
  503. if (args.col === idx) {
  504. field = col.data.field;
  505. }
  506. });
  507. if (controller.tree.selected) {
  508. var isExist = tools.isExist(totalJobs.jobsArr, field, newData, orgJobData);
  509. var isRepeat = tools.isRepeat(controller.tree.selected.jobs, field ,newData, 'reference');
  510. //create
  511. if(!id && newData && !isRepeat){
  512. if(isExist){
  513. jobsController.createExist(sheet, controller, totalJobs, field, newData, args, setting);
  514. }
  515. else {
  516. jobsController.createNew(sheet, controller, totalJobs, field, newData, args, setting);
  517. }
  518. }
  519. //update
  520. else if(id && newData !== orgJobData && !isRepeat){
  521. jobsController.update(sheet, controller, totalJobs, field, newData, id, isExist, args, setting);
  522. }
  523. //处理重复
  524. if(isRepeat){
  525. //todo:redirect focus
  526. if(id && newData){
  527. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgJobData);
  528. }
  529. else {
  530. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  531. }
  532. }
  533. }
  534. else {
  535. sheet.getCell(args.row, args.col).value('');
  536. }
  537. });
  538. },
  539. createNew: function(sheet, controller, totalJobs, field, newData, args, setting){
  540. if(field === 'content'){
  541. maxJobsNumber++;
  542. jobsAjax.createJobContent(billsLibId, field, newData, maxJobsNumber, function(id){
  543. var newJobData, newJob;
  544. newJobData = {id: id, content: newData, code: maxJobsNumber};
  545. newJob = createObj.newJob(newJobData);
  546. newJob.count = 1;
  547. totalJobs.jobs[totalJobs.prefix + id] = newJob;
  548. totalJobs.jobsArr.push(newJob);
  549. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', id);
  550. tools.addAttr(controller.tree.selected.jobs);
  551. controller.tree.selected.jobs.push(newJob);
  552. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  553. });
  554. }
  555. else {
  556. sheet.getCell(args.row, args.col).value('');
  557. }
  558. },
  559. createExist: function(sheet, controller, totalJobs, field, newData, args, setting){
  560. totalJobs.jobsArr.forEach(function(job){
  561. //todo:整合代码
  562. if(field === 'content'&& newData === job.data.content){
  563. //isExist = true;
  564. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', job.data.id);
  565. //整合
  566. tools.addAttr(controller.tree.selected.jobs);
  567. job.count++;
  568. controller.tree.selected.jobs.push(job);
  569. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  570. }
  571. else if(field == 'code' && newData == job.data.code){
  572. //isExist = true;
  573. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', job.data.id);
  574. tools.addAttr(controller.tree.selected.jobs);
  575. job.count++;
  576. controller.tree.selected.jobs.push(job);
  577. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  578. }
  579. });
  580. },
  581. update: function(sheet, controller, totalJobs, field, newData, id, isExist, args, setting){
  582. if(isExist){
  583. totalJobs.jobsArr.forEach(function(job){
  584. if(field == 'code' && job.data[field] == newData){
  585. var index = tools.getIndex(controller.tree.selected.jobs, id);
  586. job.count++;
  587. controller.tree.selected.jobs.splice(index, 1);
  588. controller.tree.selected.jobs.splice(index, 0, job);
  589. // var newJobData = {id: job.data.id, code: newData, content: job.data.content}
  590. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, job.data.id, 'update', 'jobs');
  591. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  592. }
  593. if(field === 'content' && job.data[field] === newData){
  594. var index = tools.getIndex(controller.tree.selected.jobs, id);
  595. job.count++;
  596. controller.tree.selected.jobs.splice(index, 1);
  597. controller.tree.selected.jobs.splice(index, 0, job);
  598. //var newJobData = {id: job.data.id, code: job.data.code, content: newData}
  599. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, job.data.id, 'update', 'jobs');
  600. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  601. }
  602. });
  603. }
  604. else{
  605. if(field === 'content'){
  606. maxJobsNumber++;
  607. jobsAjax.createJobContent(billsLibId, field, newData, maxJobsNumber, function(newId){
  608. var newJobData, newJob;
  609. newJobData = {id: newId, content: newData, code: maxJobsNumber};
  610. newJob = createObj.newJob(newJobData);
  611. newJob.count = 1;
  612. totalJobs.jobs[totalJobs.prefix + newId] = newJob;
  613. totalJobs.jobsArr.push(newJob);
  614. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, newId, 'update', 'jobs');
  615. tools.addAttr(controller.tree.selected.jobs);
  616. var index = tools.getIndex(controller.tree.selected.jobs, id);
  617. controller.tree.selected.jobs.splice(index, 1);
  618. controller.tree.selected.jobs.splice(index, 0, newJob);
  619. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  620. });
  621. }
  622. else {
  623. sheet.getCell(args.row, args.col).value(orgJobData);
  624. }
  625. }
  626. },
  627. };
  628. var itemsController = {
  629. editData: function(controller, sheet, totalItems, setting){
  630. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  631. orgItemData = sheet.getCell(args.row, args.col).value();
  632. });
  633. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args) {
  634. var field, newData = args.editingText, id = sheet.getTag(args.row, args.col);
  635. setting.cols.forEach(function (col, idx) {
  636. if (args.col === idx) {
  637. field = col.data.field;
  638. }
  639. });
  640. if (controller.tree.selected) {
  641. var isExist = tools.isExist(totalItems.itemsArr, field, newData, orgItemData);
  642. var isRepeat = tools.isRepeat(controller.tree.selected.items, field ,newData, 'reference');
  643. //create
  644. if(!id && newData && !isRepeat){
  645. if(isExist){
  646. itemsController.createExist(sheet, controller, totalItems, field, newData, args, setting);
  647. }
  648. else {
  649. itemsController.createNew(sheet, controller, totalItems, field, newData, args, setting);
  650. }
  651. }
  652. //update
  653. else if(id && newData !== orgItemData && !isRepeat){
  654. itemsController.update(sheet, controller, totalItems, field, newData, id, isExist, args, setting);
  655. }
  656. //处理重复
  657. if(isRepeat){
  658. //todo:redirect focus
  659. if(id && newData){
  660. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgItemData);
  661. }
  662. else {
  663. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  664. }
  665. }
  666. }
  667. else {
  668. sheet.getCell(args.row, args.col).value('');
  669. }
  670. });
  671. },
  672. createNew: function(sheet, controller, totalItems, field, newData, args, setting){
  673. if(field === 'content'){
  674. maxItemsNumber++;
  675. itemsAjax.createItemCharacter(billsLibId, field, newData, maxItemsNumber, function(id){
  676. var newItemData, newItem;
  677. newItemData = {id: id, content: newData, code: maxItemsNumber};
  678. newItem = createObj.newItem(newItemData);
  679. newItem.count = 1;
  680. totalItems.items[totalItems.prefix + id] = newItem;
  681. totalItems.itemsArr.push(newItem);
  682. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', id);
  683. tools.addAttr(controller.tree.selected.items);
  684. controller.tree.selected.items.push(newItem);
  685. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  686. });
  687. }
  688. else {
  689. sheet.getCell(args.row, args.col).value('');
  690. }
  691. },
  692. createExist: function(sheet, controller, totalItems, field, newData, args, setting){
  693. totalItems.itemsArr.forEach(function(item){
  694. //todo:整合代码
  695. if(field === 'content'&& newData === item.data.content){
  696. //isExist = true;
  697. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', item.data.id);
  698. //整合
  699. tools.addAttr(controller.tree.selected.items);
  700. item.count++;
  701. controller.tree.selected.items.push(item);
  702. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  703. }
  704. else if(field == 'code' && newData == item.data.code){
  705. //isExist = true;
  706. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', item.data.id);
  707. tools.addAttr(controller.tree.selected.items);
  708. item.count++;
  709. controller.tree.selected.items.push(item);
  710. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  711. }
  712. });
  713. },
  714. update: function(sheet, controller, totalItems, field, newData, id, isExist, args, setting){
  715. if(isExist){
  716. totalItems.itemsArr.forEach(function(item){
  717. if(field == 'code' && item.data[field] == newData){
  718. var index = tools.getIndex(controller.tree.selected.items, id);
  719. item.count++;
  720. controller.tree.selected.items.splice(index, 1);
  721. controller.tree.selected.items.splice(index, 0, item);
  722. //var newItemData = {id: item.data.id, code: newData, content: item.data.content}
  723. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, item.data.id, 'update', 'items');
  724. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  725. }
  726. if(field === 'content' && item.data[field] === newData){
  727. var index = tools.getIndex(controller.tree.selected.items, id);
  728. item.count++;
  729. controller.tree.selected.items.splice(index, 1);
  730. controller.tree.selected.items.splice(index, 0, item);
  731. //var newItemData = {id: item.data.id, code: item.data.code, content: newData}
  732. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, item.data.id, 'update', 'items');
  733. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  734. }
  735. });
  736. }
  737. else{
  738. if(field === 'content'){
  739. maxItemsNumber++;
  740. itemsAjax.createItemCharacter(billsLibId, field, newData, maxItemsNumber, function(newId){
  741. var newItemData, newItem;
  742. newItemData = {id: newId, content: newData, code: maxItemsNumber};
  743. newItem = createObj.newItem(newItemData);
  744. newItem.count = 1;
  745. totalItems.items[totalItems.prefix + newId] = newItem;
  746. totalItems.itemsArr.push(newItem);
  747. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, newId, 'update', 'items');
  748. tools.addAttr(controller.tree.selected.items);
  749. var index = tools.getIndex(controller.tree.selected.items, id);
  750. controller.tree.selected.items.splice(index, 1);
  751. controller.tree.selected.items.splice(index, 0, newItem);
  752. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  753. });
  754. }
  755. else {
  756. sheet.getCell(args.row, args.col).value(orgItemData);
  757. }
  758. }
  759. },
  760. };
  761. var valueController = {
  762. editData: function(totalItems, sheet, setting){
  763. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  764. orgValue = sheet.getValue(args.row, args.col);
  765. });
  766. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  767. var newValue = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  768. setting.cols.forEach(function(col, colIdx){
  769. if(args.col === colIdx){
  770. field = col.data.field;
  771. }
  772. });
  773. if(selectedId){
  774. isRepeat = tools.isRepeat(valueController.getValues(totalItems, selectedId), field, newValue, 'document');
  775. if(!tagId && !isRepeat && newValue){//create
  776. if(field === 'value'){
  777. valueController.createValue(sheet, totalItems, selectedId, newValue);
  778. }
  779. else {
  780. sheet.getCell(args.row, args.col).value('');
  781. }
  782. }
  783. else if(tagId && !isRepeat && newValue !== orgValue){//update
  784. if(field === 'value'){
  785. valueController.updateValue(totalItems, tagId, newValue);
  786. }
  787. else {
  788. sheet.getCell(args.row, args.col).value(orgValue);
  789. }
  790. }
  791. if(isRepeat){
  792. if(tagId && newValue){
  793. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgValue);
  794. }
  795. else {
  796. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  797. }
  798. }
  799. }
  800. });
  801. },
  802. createValue: function(sheet, totalItems, id, newValue){
  803. var newData, newCode = valueController.getCode(totalItems, id);
  804. newData = {code: newCode, value: newValue};
  805. totalItems.findItem(id).data.itemValue.push(newData);
  806. itemsAjax.updateValue(id, newData, null, 'create');
  807. tools.reSetCell(sheet, sheet.getActiveRowIndex(), 0, newCode, newCode);
  808. },
  809. updateValue: function(totalItems, tagId, newValue){
  810. var updateData = {code: tagId, value: newValue};
  811. var itemVals = totalItems.findItem(selectedId).data.itemValue;
  812. if(itemVals){
  813. var index;
  814. itemVals.forEach(function(val){
  815. if(val.code === tagId){
  816. index = itemVals.indexOf(val);
  817. itemVals.splice(index, 1);
  818. }
  819. });
  820. itemVals.splice(index, 0, updateData);
  821. }
  822. itemsAjax.updateValue(selectedId, updateData, null, 'update');
  823. },
  824. getValues: function(totalItems, id){
  825. return totalItems.findItem(id).data.itemValue;
  826. },
  827. getTopItem: function(totalItems){
  828. tools.resort(totalItems.itemsArr, 'code', false);
  829. return totalItems.itemsArr.length > 0 ? totalItems.itemsArr[0] : null
  830. },
  831. getCode: function(totalItems, id){
  832. var valArr = totalItems.findItem(id).data.itemValue;
  833. tools.resort(valArr, 'code', true);
  834. return valArr.length > 0 ? valArr[valArr.length - 1].code + 1 : 1;
  835. //return totalItems.findItem(id).data.itemValue.length;
  836. },
  837. isRepeat: function(arr, newValue){
  838. var isRepeat = false;
  839. arr.forEach(function(itemValue){
  840. if(itemValue.value === newValue){
  841. isRepeat = true;
  842. }
  843. });
  844. return isRepeat;
  845. }
  846. };
  847. var totalJobsController = {
  848. eiditData: function(totalJobs, sheet, setting){
  849. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  850. orgJobData = sheet.getCell(args.row, args.col).value();
  851. });
  852. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  853. var newData = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  854. setting.cols.forEach(function(col, colIdx){
  855. if(args.col === colIdx){
  856. field = col.data.field;
  857. console.log('colIdx' + colIdx);
  858. }
  859. });
  860. isRepeat = tools.isRepeat(totalJobs.jobsArr, field, newData, 'reference');
  861. if(!tagId && !isRepeat && newData){//create
  862. totalJobsController.createJob(sheet, totalJobs, field, newData, args);
  863. }
  864. else if(tagId && !isRepeat && newData !== orgJobData){//update
  865. if(field === 'content'){
  866. totalJobsController.updateJob(totalJobs, tagId, field, newData);
  867. }
  868. else {
  869. sheet.getCell(args.row, args.col).value(orgJobData);
  870. }
  871. }
  872. if(isRepeat){
  873. if(tagId && newData){
  874. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgJobData);
  875. }
  876. else {
  877. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  878. }
  879. }
  880. });
  881. },
  882. createJob: function(sheet, totalJobs, field, newData, args){
  883. if(field === 'content'){
  884. maxJobsNumber++;
  885. jobsAjax.createJobContent(billsLibId, field, newData, maxJobsNumber, function(id){
  886. var newJobData, newJob;
  887. newJobData = {id: id, content: newData, code: maxJobsNumber};
  888. newJob = createObj.newJob(newJobData);
  889. totalJobs.jobsArr.push(newJob);
  890. tools.reSetCell(sheet, args.row, 0, maxJobsNumber, id);
  891. });
  892. }
  893. else {
  894. sheet.getCell(args.row, args.col).value('');
  895. }
  896. },
  897. updateJob: function(totalJobs, id, field, newData){
  898. totalJobs.jobsArr.forEach(function(job){
  899. if(job.data[field] === newData){
  900. job.data[field] = newData;
  901. }
  902. });
  903. jobsAjax.updateJobContent(id, field, newData);
  904. }
  905. };
  906. var totalItemsController = {
  907. eiditData: function(totalItems, sheet, setting){
  908. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  909. orgItemData = sheet.getCell(args.row, args.col).value();
  910. });
  911. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  912. var newData = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  913. setting.cols.forEach(function(col, colIdx){
  914. if(args.col === colIdx){
  915. field = col.data.field;
  916. console.log('colIdx' + colIdx);
  917. }
  918. });
  919. isRepeat = tools.isRepeat(totalItems.itemsArr, field, newData, 'reference');
  920. if(!tagId && !isRepeat && newData){//create
  921. totalItemsController.createJob(sheet, totalItems, field, newData, args);
  922. }
  923. else if(tagId && !isRepeat && newData !== orgItemData){//update
  924. if(field === 'content'){
  925. totalItemsController.updateItem(totalItems, tagId, field, newData);
  926. }
  927. else {
  928. sheet.getCell(args.row, args.col).value(orgItemData);
  929. }
  930. }
  931. if(isRepeat){
  932. if(tagId && newData){
  933. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgItemData);
  934. }
  935. else {
  936. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  937. }
  938. }
  939. });
  940. },
  941. createItem: function(sheet, totalItems, field, newData, args){
  942. if(field === 'content'){
  943. maxItemsNumber++;
  944. itemsAjax.createItemCharacter(billsLibId, field, newData, maxItemsNumber, function(id){
  945. var newItemData, newItem;
  946. newItemData = {id: id, content: newData, code: maxItemsNumber};
  947. newItem = createObj.newItem(newItemData);
  948. totalItems.itemsArr.push(newItem);
  949. tools.reSetCell(sheet, args.row, 0, maxItemsNumber, id);
  950. });
  951. }
  952. else {
  953. sheet.getCell(args.row, args.col).value('');
  954. }
  955. },
  956. updateItem: function(totalItems, id, field, newData){
  957. totalItems.jobsArr.forEach(function(item){
  958. if(item.data[field] === newData){
  959. item.data[field] = newData;
  960. }
  961. });
  962. itemsAjax.updateItemCharacter(id, field, newData);
  963. }
  964. };
  965. var rechargeController = {
  966. updateRechar: function(selectedNode, newData){
  967. if(selectedNode){
  968. var updateIds = [];
  969. var getupdateIds = function(node){
  970. updateIds.push(node.getID());
  971. node.data.recharge = newData;
  972. if(node.children.length > 0){
  973. node.children.forEach(function(node){
  974. getupdateIds(node);
  975. });
  976. }
  977. }
  978. getupdateIds(tools.getRoot(selectedNode));
  979. billsAjax.updateRecharge(billsLibId, updateIds, newData);
  980. }
  981. else {
  982. $('exampleTextarea').val('');
  983. }
  984. },
  985. createRecharge: function(node){
  986. if(node){
  987. var tempId = [];
  988. tempId.push(node.getID());
  989. var rootNode = tools.getRoot(node);
  990. console.log('rootNodeId: ' + rootNode.getID());
  991. if(rootNode.data.recharge && rootNode !== node){
  992. billsAjax.updateRecharge(billsLibId, tempId, rootNode.data.recharge);
  993. node.data.recharge = rootNode.data.recharge;
  994. $('#exampleTextarea').val(node.data.recharge);
  995. }
  996. else {
  997. node.data.recharge = '';
  998. $('#exampleTextarea').val('');
  999. }
  1000. }
  1001. else {
  1002. $('exampleTextarea').val('');
  1003. }
  1004. }
  1005. }