dbController.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. tools.addAttr(node.jobs);
  230. node.data.jobs.forEach(function(jobId){
  231. if(me.jobs[me.prefix + jobId]){
  232. node.jobs.push(me.jobs[me.prefix + jobId]);
  233. me.jobs[me.prefix + jobId].count ++;
  234. }
  235. else {
  236. node.data.jobs.splice(node.data.jobs.indexOf(jobId), 1);
  237. }
  238. });
  239. });
  240. }
  241. };
  242. TotalJobs.prototype.getUpdateIds = function(nodes, repeatId){
  243. var ids = [];
  244. nodes.forEach(function(node){
  245. node.jobs.forEach(function(job){
  246. if(job.data.id === repeatId){
  247. ids.push(node.getID());
  248. }
  249. });
  250. });
  251. return ids;
  252. };
  253. return new TotalJobs();
  254. },
  255. newItem: function(data){
  256. var Item = function(data){
  257. this.data = data;
  258. this.count = 0;
  259. };
  260. return new Item(data);
  261. },
  262. newItems: function(){
  263. var TotalItems = function(){
  264. this.items = {};
  265. this.itemsArr = [];
  266. this.prefix = '_id';
  267. };
  268. TotalItems.prototype.findItem = function(id){
  269. return this.items[this.prefix + id] ? this.items[this.prefix + id] : null;
  270. }
  271. TotalItems.prototype.loadItems = function (nodes, datas){
  272. var me = this;
  273. datas.forEach(function(itemData){
  274. var item = createObj.newItem(itemData);
  275. me.items[me.prefix + itemData.id] = item;
  276. me.itemsArr.push(item);
  277. });
  278. if(nodes){
  279. nodes.forEach(function(node){
  280. tools.addAttr(node.items);
  281. node.data.items.forEach(function(itemID){
  282. if(me.items[me.prefix + itemID]){
  283. node.items.push(me.items[me.prefix + itemID]);
  284. me.items[me.prefix + itemID].count ++;
  285. }
  286. else {
  287. node.data.items.splice(node.data.items.indexOf(itemID), 1);
  288. }
  289. });
  290. });
  291. }
  292. };
  293. TotalItems.prototype.getUpdateIds = function(nodes, repeatId){
  294. var ids = [];
  295. nodes.forEach(function(node){
  296. node.items.forEach(function(item){
  297. if(item.data.id === repeatId){
  298. ids.push(node.getID());
  299. }
  300. });
  301. });
  302. return ids;
  303. };
  304. return new TotalItems();
  305. }
  306. };
  307. var tools = {
  308. isExist: function (totalArr, field, newData, orgData){
  309. var isExist = false;
  310. if(totalArr.length > 0){
  311. totalArr.forEach(function(item){
  312. if(item.data[field] == newData && newData !== orgData){
  313. isExist = true;
  314. }
  315. });
  316. }
  317. return isExist;
  318. },
  319. isRepeat: function(arr, field, newData, ref){
  320. var isRepeat = false;
  321. if(arr){
  322. arr.forEach(function(item){
  323. if(ref === 'reference' && item.data[field] == newData){
  324. isRepeat = true;
  325. }
  326. else if(ref === 'document' && item[field] === newData){
  327. isRepeat = true;
  328. }
  329. });
  330. }
  331. return isRepeat;
  332. },
  333. addAttr: function(attr){
  334. if(!attr){
  335. attr = new Array();
  336. }
  337. },
  338. reSetCell: function(sheet, rowIdx, colIdx, value, id){
  339. sheet.getCell(rowIdx, colIdx, GC.Spread.Sheets.SheetArea.viewport).value(value);
  340. sheet.setTag(rowIdx, 0, id);
  341. sheet.setTag(rowIdx, 1, id);
  342. },
  343. getIndex: function(arr, id){
  344. var index;
  345. arr.forEach(function(item){
  346. if(item.data.id === id){
  347. index = arr.indexOf(item);
  348. }
  349. });
  350. return index;
  351. },
  352. delteSheets: function(controller, totalJobs, totalItems, jobsSheet, itemsSheet){
  353. var delJobs = controller.tree.selected.jobs;
  354. var delItems = controller.tree.selected.items;
  355. var delJobsIds = [];
  356. var delItemsIds = [];
  357. if(delJobs.length > 0){
  358. delJobs.forEach(function(job){
  359. job.count --;
  360. if(job.count <=0){
  361. totalJobs.jobsArr.splice(totalJobs.jobsArr.indexOf(job), 1);
  362. delJobsIds.push(job.data.id)
  363. }
  364. });
  365. jobsAjax.deleteJobContent(delJobsIds);
  366. tools.clearData(jobsSheet);
  367. tools.reshowData(jobsSheet, controller.tree.selected.jobs, jobsSetting, true);
  368. }
  369. if(delItems.length > 0){
  370. delItems.forEach(function(item){
  371. item.count --;
  372. if(item.count <=0){
  373. totalItems.itemsArr.splice(totalItems.itemsArr.indexOf(item), 1);
  374. delItemsIds.push(item.data.id)
  375. }
  376. });
  377. itemsAjax.deleteItemCharacter(delItemsIds);
  378. tools.clearData(itemsSheet);
  379. tools.reshowData(itemsSheet, controller.tree.selected.items, itemsSetting, true);
  380. }
  381. controller.delete();
  382. },
  383. deleteELes: function (arr, delIds, callback){
  384. var ids = [];
  385. delIds.forEach(function(delId){
  386. arr.forEach(function(ele){
  387. if(ele.data.id === delId){
  388. arr.splice(arr.indexOf(ele), 1);
  389. ele.count--;
  390. if(ele.count <= 0){
  391. ids.push(ele.data.id);
  392. }
  393. }
  394. });
  395. });
  396. if(callback){
  397. callback(ids);
  398. }
  399. },
  400. resort: function(arr, attr, isValue){
  401. //var arr = node.jobs;
  402. function compare(attr){
  403. return function (a, b){
  404. var valA, valB;
  405. if(isValue){
  406. valA = a[attr];
  407. valB = a[attr];
  408. }
  409. else {
  410. valA = a.data[attr];
  411. valB = b.data[attr];
  412. }
  413. return valA - valB;
  414. }
  415. }
  416. arr.sort(compare(attr));
  417. },
  418. reshowData: function(sheet, arr, setting, isResort){
  419. tools.clearData(sheet);
  420. if(isResort){
  421. tools.resort(arr, 'code', false);
  422. }
  423. if(arr.length > 0){
  424. for(var i=0; i<arr.length; i++){
  425. setting.cols.forEach(function(col, colIdx){
  426. sheet.setTag(i, colIdx, arr[i].data.id);
  427. if(arr[i].data[col.data.field]){
  428. sheet.getCell(i, colIdx).value(arr[i].data[col.data.field]);
  429. }
  430. else {
  431. sheet.getCell(i, colIdx).value('');
  432. }
  433. });
  434. }
  435. }
  436. },
  437. reshowValue: function(sheet, arr, setting, isResort){
  438. tools.clearData(sheet);
  439. if(isResort){
  440. tools.resort(arr, 'code', true);
  441. }
  442. if(arr.length > 0){
  443. for(var i=0; i<arr.length; i++){
  444. setting.cols.forEach(function(col, colIdx){
  445. sheet.setTag(i, colIdx, arr[i].code);
  446. if(arr[i][col.data.field]){
  447. sheet.getCell(i, colIdx).value(arr[i][col.data.field]);
  448. }
  449. else {
  450. sheet.getCell(i, colIdx).value('');
  451. }
  452. });
  453. }
  454. }
  455. },
  456. clearData: function(sheet){
  457. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
  458. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.tag);
  459. },
  460. getRoot: function(node){
  461. return node.parent ? tools.getRoot(node.parent) : node;
  462. },
  463. getBillsIds: function(callback){
  464. billsAjax.getBills(billsLibId, function(datas){
  465. var ids = [];
  466. datas.forEach(function(data){
  467. ids.push(data.ID);
  468. });
  469. if(callback){
  470. callback(ids);
  471. }
  472. });
  473. },
  474. delIds: function(sheet){
  475. var ids = [];
  476. var sels = sheet.getSelections();
  477. sels.forEach(function(sel){
  478. var orgRow = sel.row, rowCount = sel.rowCount, colCount = sel.colCount;
  479. var orgCol = sel.col === -1 ? 0 : sel.col;
  480. var maxRow = orgRow + rowCount - 1;
  481. for(var i=orgRow; i<= maxRow; i++){
  482. if(sheet.getTag(i, orgCol)){
  483. ids.push(sheet.getTag(i, orgCol));
  484. }
  485. }
  486. });
  487. return ids;
  488. },
  489. redirect: function(billsLibId, newHref){
  490. mainAjax.getABillsLib(billsLibId, function(result){
  491. if(result.length === 0){
  492. window.location.href = newHref;
  493. }
  494. });
  495. }
  496. }
  497. var jobsController = {
  498. editData: function(controller, sheet, totalJobs, setting){
  499. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  500. orgJobData = sheet.getCell(args.row, args.col).value();
  501. });
  502. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args) {
  503. var field, newData = args.editingText, id = sheet.getTag(args.row, args.col);
  504. setting.cols.forEach(function (col, idx) {
  505. if (args.col === idx) {
  506. field = col.data.field;
  507. }
  508. });
  509. if (controller.tree.selected) {
  510. var isExist = tools.isExist(totalJobs.jobsArr, field, newData, orgJobData);
  511. var isRepeat = tools.isRepeat(controller.tree.selected.jobs, field ,newData, 'reference');
  512. //create
  513. if(!id && newData && !isRepeat){
  514. if(isExist){
  515. jobsController.createExist(sheet, controller, totalJobs, field, newData, args, setting);
  516. }
  517. else {
  518. jobsController.createNew(sheet, controller, totalJobs, field, newData, args, setting);
  519. }
  520. }
  521. //update
  522. else if(id && newData !== orgJobData && !isRepeat){
  523. jobsController.update(sheet, controller, totalJobs, field, newData, id, isExist, args, setting);
  524. }
  525. //处理重复
  526. if(isRepeat){
  527. //todo:redirect focus
  528. if(id && newData){
  529. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgJobData);
  530. }
  531. else {
  532. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  533. }
  534. }
  535. }
  536. else {
  537. sheet.getCell(args.row, args.col).value('');
  538. }
  539. });
  540. },
  541. createNew: function(sheet, controller, totalJobs, field, newData, args, setting){
  542. if(field === 'content'){
  543. maxJobsNumber++;
  544. jobsAjax.createJobContent(billsLibId, field, newData, maxJobsNumber, function(id){
  545. var newJobData, newJob;
  546. newJobData = {id: id, content: newData, code: maxJobsNumber};
  547. newJob = createObj.newJob(newJobData);
  548. newJob.count = 1;
  549. totalJobs.jobs[totalJobs.prefix + id] = newJob;
  550. totalJobs.jobsArr.push(newJob);
  551. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', id);
  552. tools.addAttr(controller.tree.selected.jobs);
  553. controller.tree.selected.jobs.push(newJob);
  554. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  555. });
  556. }
  557. else {
  558. sheet.getCell(args.row, args.col).value('');
  559. }
  560. },
  561. createExist: function(sheet, controller, totalJobs, field, newData, args, setting){
  562. totalJobs.jobsArr.forEach(function(job){
  563. //todo:整合代码
  564. if(field === 'content'&& newData === job.data.content){
  565. //isExist = true;
  566. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', job.data.id);
  567. //整合
  568. tools.addAttr(controller.tree.selected.jobs);
  569. job.count++;
  570. controller.tree.selected.jobs.push(job);
  571. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  572. }
  573. else if(field == 'code' && newData == job.data.code){
  574. //isExist = true;
  575. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', job.data.id);
  576. tools.addAttr(controller.tree.selected.jobs);
  577. job.count++;
  578. controller.tree.selected.jobs.push(job);
  579. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  580. }
  581. });
  582. },
  583. update: function(sheet, controller, totalJobs, field, newData, id, isExist, args, setting){
  584. if(isExist){
  585. totalJobs.jobsArr.forEach(function(job){
  586. if(field == 'code' && job.data[field] == newData){
  587. var index = tools.getIndex(controller.tree.selected.jobs, id);
  588. job.count++;
  589. controller.tree.selected.jobs.splice(index, 1);
  590. controller.tree.selected.jobs.splice(index, 0, job);
  591. // var newJobData = {id: job.data.id, code: newData, content: job.data.content}
  592. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, job.data.id, 'update', 'jobs');
  593. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  594. }
  595. if(field === 'content' && job.data[field] === newData){
  596. var index = tools.getIndex(controller.tree.selected.jobs, id);
  597. job.count++;
  598. controller.tree.selected.jobs.splice(index, 1);
  599. controller.tree.selected.jobs.splice(index, 0, job);
  600. //var newJobData = {id: job.data.id, code: job.data.code, content: newData}
  601. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, job.data.id, 'update', 'jobs');
  602. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  603. }
  604. });
  605. }
  606. else{
  607. if(field === 'content'){
  608. maxJobsNumber++;
  609. jobsAjax.createJobContent(billsLibId, field, newData, maxJobsNumber, function(newId){
  610. var newJobData, newJob;
  611. newJobData = {id: newId, content: newData, code: maxJobsNumber};
  612. newJob = createObj.newJob(newJobData);
  613. newJob.count = 1;
  614. totalJobs.jobs[totalJobs.prefix + newId] = newJob;
  615. totalJobs.jobsArr.push(newJob);
  616. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, newId, 'update', 'jobs');
  617. tools.addAttr(controller.tree.selected.jobs);
  618. var index = tools.getIndex(controller.tree.selected.jobs, id);
  619. controller.tree.selected.jobs.splice(index, 1);
  620. controller.tree.selected.jobs.splice(index, 0, newJob);
  621. tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);
  622. });
  623. }
  624. else {
  625. sheet.getCell(args.row, args.col).value(orgJobData);
  626. }
  627. }
  628. },
  629. };
  630. var itemsController = {
  631. editData: function(controller, sheet, totalItems, setting){
  632. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  633. orgItemData = sheet.getCell(args.row, args.col).value();
  634. });
  635. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args) {
  636. var field, newData = args.editingText, id = sheet.getTag(args.row, args.col);
  637. setting.cols.forEach(function (col, idx) {
  638. if (args.col === idx) {
  639. field = col.data.field;
  640. }
  641. });
  642. if (controller.tree.selected) {
  643. var isExist = tools.isExist(totalItems.itemsArr, field, newData, orgItemData);
  644. var isRepeat = tools.isRepeat(controller.tree.selected.items, field ,newData, 'reference');
  645. //create
  646. if(!id && newData && !isRepeat){
  647. if(isExist){
  648. itemsController.createExist(sheet, controller, totalItems, field, newData, args, setting);
  649. }
  650. else {
  651. itemsController.createNew(sheet, controller, totalItems, field, newData, args, setting);
  652. }
  653. }
  654. //update
  655. else if(id && newData !== orgItemData && !isRepeat){
  656. itemsController.update(sheet, controller, totalItems, field, newData, id, isExist, args, setting);
  657. }
  658. //处理重复
  659. if(isRepeat){
  660. //todo:redirect focus
  661. if(id && newData){
  662. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgItemData);
  663. }
  664. else {
  665. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  666. }
  667. }
  668. }
  669. else {
  670. sheet.getCell(args.row, args.col).value('');
  671. }
  672. });
  673. },
  674. createNew: function(sheet, controller, totalItems, field, newData, args, setting){
  675. if(field === 'content'){
  676. maxItemsNumber++;
  677. itemsAjax.createItemCharacter(billsLibId, field, newData, maxItemsNumber, function(id){
  678. var newItemData, newItem;
  679. newItemData = {id: id, content: newData, code: maxItemsNumber};
  680. newItem = createObj.newItem(newItemData);
  681. newItem.count = 1;
  682. totalItems.items[totalItems.prefix + id] = newItem;
  683. totalItems.itemsArr.push(newItem);
  684. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', id);
  685. tools.addAttr(controller.tree.selected.items);
  686. controller.tree.selected.items.push(newItem);
  687. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  688. });
  689. }
  690. else {
  691. sheet.getCell(args.row, args.col).value('');
  692. }
  693. },
  694. createExist: function(sheet, controller, totalItems, field, newData, args, setting){
  695. totalItems.itemsArr.forEach(function(item){
  696. //todo:整合代码
  697. if(field === 'content'&& newData === item.data.content){
  698. //isExist = true;
  699. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', item.data.id);
  700. //整合
  701. tools.addAttr(controller.tree.selected.items);
  702. item.count++;
  703. controller.tree.selected.items.push(item);
  704. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  705. }
  706. else if(field == 'code' && newData == item.data.code){
  707. //isExist = true;
  708. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', item.data.id);
  709. tools.addAttr(controller.tree.selected.items);
  710. item.count++;
  711. controller.tree.selected.items.push(item);
  712. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  713. }
  714. });
  715. },
  716. update: function(sheet, controller, totalItems, field, newData, id, isExist, args, setting){
  717. if(isExist){
  718. totalItems.itemsArr.forEach(function(item){
  719. if(field == 'code' && item.data[field] == newData){
  720. var index = tools.getIndex(controller.tree.selected.items, id);
  721. item.count++;
  722. controller.tree.selected.items.splice(index, 1);
  723. controller.tree.selected.items.splice(index, 0, item);
  724. //var newItemData = {id: item.data.id, code: newData, content: item.data.content}
  725. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, item.data.id, 'update', 'items');
  726. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  727. }
  728. if(field === 'content' && item.data[field] === newData){
  729. var index = tools.getIndex(controller.tree.selected.items, id);
  730. item.count++;
  731. controller.tree.selected.items.splice(index, 1);
  732. controller.tree.selected.items.splice(index, 0, item);
  733. //var newItemData = {id: item.data.id, code: item.data.code, content: newData}
  734. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, item.data.id, 'update', 'items');
  735. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  736. }
  737. });
  738. }
  739. else{
  740. if(field === 'content'){
  741. maxItemsNumber++;
  742. itemsAjax.createItemCharacter(billsLibId, field, newData, maxItemsNumber, function(newId){
  743. var newItemData, newItem;
  744. newItemData = {id: newId, content: newData, code: maxItemsNumber};
  745. newItem = createObj.newItem(newItemData);
  746. newItem.count = 1;
  747. totalItems.items[totalItems.prefix + newId] = newItem;
  748. totalItems.itemsArr.push(newItem);
  749. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, newId, 'update', 'items');
  750. tools.addAttr(controller.tree.selected.items);
  751. var index = tools.getIndex(controller.tree.selected.items, id);
  752. controller.tree.selected.items.splice(index, 1);
  753. controller.tree.selected.items.splice(index, 0, newItem);
  754. tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  755. });
  756. }
  757. else {
  758. sheet.getCell(args.row, args.col).value(orgItemData);
  759. }
  760. }
  761. },
  762. };
  763. var valueController = {
  764. editData: function(totalItems, sheet, setting){
  765. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  766. orgValue = sheet.getValue(args.row, args.col);
  767. });
  768. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  769. var newValue = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  770. setting.cols.forEach(function(col, colIdx){
  771. if(args.col === colIdx){
  772. field = col.data.field;
  773. }
  774. });
  775. if(selectedId){
  776. isRepeat = tools.isRepeat(valueController.getValues(totalItems, selectedId), field, newValue, 'document');
  777. if(!tagId && !isRepeat && newValue){//create
  778. if(field === 'value'){
  779. valueController.createValue(sheet, totalItems, selectedId, newValue);
  780. }
  781. else {
  782. sheet.getCell(args.row, args.col).value('');
  783. }
  784. }
  785. else if(tagId && !isRepeat && newValue !== orgValue){//update
  786. if(field === 'value'){
  787. valueController.updateValue(totalItems, tagId, newValue);
  788. }
  789. else {
  790. sheet.getCell(args.row, args.col).value(orgValue);
  791. }
  792. }
  793. if(isRepeat){
  794. if(tagId && newValue){
  795. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgValue);
  796. }
  797. else {
  798. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  799. }
  800. }
  801. }
  802. });
  803. },
  804. createValue: function(sheet, totalItems, id, newValue){
  805. var newData, newCode = valueController.getCode(totalItems, id);
  806. newData = {code: newCode, value: newValue};
  807. totalItems.findItem(id).data.itemValue.push(newData);
  808. itemsAjax.updateValue(id, newData, null, 'create');
  809. tools.reSetCell(sheet, sheet.getActiveRowIndex(), 0, newCode, newCode);
  810. },
  811. updateValue: function(totalItems, tagId, newValue){
  812. var updateData = {code: tagId, value: newValue};
  813. var itemVals = totalItems.findItem(selectedId).data.itemValue;
  814. if(itemVals){
  815. var index;
  816. itemVals.forEach(function(val){
  817. if(val.code === tagId){
  818. index = itemVals.indexOf(val);
  819. itemVals.splice(index, 1);
  820. }
  821. });
  822. itemVals.splice(index, 0, updateData);
  823. }
  824. itemsAjax.updateValue(selectedId, updateData, null, 'update');
  825. },
  826. getValues: function(totalItems, id){
  827. return totalItems.findItem(id).data.itemValue;
  828. },
  829. getTopItem: function(totalItems){
  830. tools.resort(totalItems.itemsArr, 'code', false);
  831. return totalItems.itemsArr.length > 0 ? totalItems.itemsArr[0] : null
  832. },
  833. getCode: function(totalItems, id){
  834. var valArr = totalItems.findItem(id).data.itemValue;
  835. tools.resort(valArr, 'code', true);
  836. return valArr.length > 0 ? valArr[valArr.length - 1].code + 1 : 1;
  837. //return totalItems.findItem(id).data.itemValue.length;
  838. },
  839. isRepeat: function(arr, newValue){
  840. var isRepeat = false;
  841. arr.forEach(function(itemValue){
  842. if(itemValue.value === newValue){
  843. isRepeat = true;
  844. }
  845. });
  846. return isRepeat;
  847. }
  848. };
  849. var totalJobsController = {
  850. eiditData: function(totalJobs, sheet, setting){
  851. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  852. orgJobData = sheet.getCell(args.row, args.col).value();
  853. });
  854. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  855. var newData = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  856. setting.cols.forEach(function(col, colIdx){
  857. if(args.col === colIdx){
  858. field = col.data.field;
  859. console.log('colIdx' + colIdx);
  860. }
  861. });
  862. isRepeat = tools.isRepeat(totalJobs.jobsArr, field, newData, 'reference');
  863. if(!tagId && !isRepeat && newData){//create
  864. totalJobsController.createJob(sheet, totalJobs, field, newData, args);
  865. }
  866. else if(tagId && !isRepeat && newData !== orgJobData){//update
  867. if(field === 'content'){
  868. totalJobsController.updateJob(totalJobs, tagId, field, newData);
  869. }
  870. else {
  871. sheet.getCell(args.row, args.col).value(orgJobData);
  872. }
  873. }
  874. if(isRepeat){
  875. if(tagId && newData){
  876. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgJobData);
  877. }
  878. else {
  879. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  880. }
  881. }
  882. });
  883. },
  884. createJob: function(sheet, totalJobs, field, newData, args){
  885. if(field === 'content'){
  886. maxJobsNumber++;
  887. jobsAjax.createJobContent(billsLibId, field, newData, maxJobsNumber, function(id){
  888. var newJobData, newJob;
  889. newJobData = {id: id, content: newData, code: maxJobsNumber};
  890. newJob = createObj.newJob(newJobData);
  891. totalJobs.jobsArr.push(newJob);
  892. tools.reSetCell(sheet, args.row, 0, maxJobsNumber, id);
  893. });
  894. }
  895. else {
  896. sheet.getCell(args.row, args.col).value('');
  897. }
  898. },
  899. updateJob: function(totalJobs, id, field, newData){
  900. totalJobs.jobsArr.forEach(function(job){
  901. if(job.data[field] === newData){
  902. job.data[field] = newData;
  903. }
  904. });
  905. jobsAjax.updateJobContent(id, field, newData);
  906. }
  907. };
  908. var totalItemsController = {
  909. eiditData: function(totalItems, sheet, setting){
  910. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  911. orgItemData = sheet.getCell(args.row, args.col).value();
  912. });
  913. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  914. var newData = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  915. setting.cols.forEach(function(col, colIdx){
  916. if(args.col === colIdx){
  917. field = col.data.field;
  918. console.log('colIdx' + colIdx);
  919. }
  920. });
  921. isRepeat = tools.isRepeat(totalItems.itemsArr, field, newData, 'reference');
  922. if(!tagId && !isRepeat && newData){//create
  923. totalItemsController.createJob(sheet, totalItems, field, newData, args);
  924. }
  925. else if(tagId && !isRepeat && newData !== orgItemData){//update
  926. if(field === 'content'){
  927. totalItemsController.updateItem(totalItems, tagId, field, newData);
  928. }
  929. else {
  930. sheet.getCell(args.row, args.col).value(orgItemData);
  931. }
  932. }
  933. if(isRepeat){
  934. if(tagId && newData){
  935. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgItemData);
  936. }
  937. else {
  938. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  939. }
  940. }
  941. });
  942. },
  943. createItem: function(sheet, totalItems, field, newData, args){
  944. if(field === 'content'){
  945. maxItemsNumber++;
  946. itemsAjax.createItemCharacter(billsLibId, field, newData, maxItemsNumber, function(id){
  947. var newItemData, newItem;
  948. newItemData = {id: id, content: newData, code: maxItemsNumber};
  949. newItem = createObj.newItem(newItemData);
  950. totalItems.itemsArr.push(newItem);
  951. tools.reSetCell(sheet, args.row, 0, maxItemsNumber, id);
  952. });
  953. }
  954. else {
  955. sheet.getCell(args.row, args.col).value('');
  956. }
  957. },
  958. updateItem: function(totalItems, id, field, newData){
  959. totalItems.jobsArr.forEach(function(item){
  960. if(item.data[field] === newData){
  961. item.data[field] = newData;
  962. }
  963. });
  964. itemsAjax.updateItemCharacter(id, field, newData);
  965. }
  966. };
  967. var rechargeController = {
  968. updateRechar: function(selectedNode, newData){
  969. if(selectedNode){
  970. var updateIds = [];
  971. var getupdateIds = function(node){
  972. updateIds.push(node.getID());
  973. node.data.recharge = newData;
  974. if(node.children.length > 0){
  975. node.children.forEach(function(node){
  976. getupdateIds(node);
  977. });
  978. }
  979. }
  980. getupdateIds(tools.getRoot(selectedNode));
  981. billsAjax.updateRecharge(billsLibId, updateIds, newData);
  982. }
  983. else {
  984. $('exampleTextarea').val('');
  985. }
  986. },
  987. createRecharge: function(node){
  988. if(node){
  989. var tempId = [];
  990. tempId.push(node.getID());
  991. var rootNode = tools.getRoot(node);
  992. console.log('rootNodeId: ' + rootNode.getID());
  993. if(rootNode.data.recharge && rootNode !== node){
  994. billsAjax.updateRecharge(billsLibId, tempId, rootNode.data.recharge);
  995. node.data.recharge = rootNode.data.recharge;
  996. $('#exampleTextarea').val(node.data.recharge);
  997. }
  998. else {
  999. node.data.recharge = '';
  1000. $('#exampleTextarea').val('');
  1001. }
  1002. }
  1003. else {
  1004. $('exampleTextarea').val('');
  1005. }
  1006. }
  1007. }