db_controller.js 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. /**
  2. * Created by vian on 2017/4/12.
  3. */
  4. var dbController = {
  5. insert: function(controller, btn){
  6. tools.btnClose(btn, 'focusOnBills');
  7. if(controller.tree.items.length === 0){
  8. controller.tree.maxNodeID(0);
  9. let newNodeId = controller.tree.newNodeID();
  10. billsAjax.createBills(billsLibId, newNodeId, -1 , -1, null, function(){
  11. controller.insert();
  12. controller.tree.selected.jobs = new Array();
  13. controller.tree.selected.items = new Array();
  14. controller.sheet.setTag(controller.tree.selected.serialNo(), 0, newNodeId);
  15. tools.btnAction(btn, 'focusOnBills');
  16. });
  17. }
  18. else {
  19. let node = controller.tree.selected;
  20. if(node){
  21. var updateId = node.getID(), createpid = node.getParentID(), createnid = node.getNextSiblingID();
  22. let newNodeId = controller.tree.newNodeID();
  23. let updatePreData = {ID: updateId, NextSiblingID: newNodeId};
  24. billsAjax.createBills(billsLibId, newNodeId, createpid, createnid, updatePreData, function(){
  25. controller.insert();
  26. controller.tree.selected.jobs = new Array();
  27. controller.tree.selected.items = new Array();
  28. controller.sheet.setTag(controller.tree.selected.serialNo(), 0, newNodeId);
  29. sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  30. tools.btnAction(btn, 'focusOnBills');
  31. });
  32. }
  33. else {
  34. let updateNode = controller.tree.roots[controller.tree.roots.length - 1];
  35. let updateId = updateNode.getID();
  36. let newNodeId = controller.tree.newNodeID();
  37. let updatePreData = {ID: updateId, NextSiblingID: newNodeId};
  38. billsAjax.createBills(billsLibId, newNodeId, -1, -1, updatePreData, function(){
  39. controller.insert();
  40. controller.tree.selected.jobs = new Array();
  41. controller.tree.selected.items = new Array();
  42. controller.sheet.setTag(controller.tree.selected.serialNo(), 0, newNodeId);
  43. sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  44. tools.btnAction(btn, 'focusOnBills');
  45. });
  46. }
  47. }
  48. },
  49. upLevel: function(controller, btn){
  50. tools.btnClose(btn, 'focusOnBills');
  51. var node = controller.tree.selected;
  52. var ids = [];
  53. var updateData = [];
  54. var getNextSibling = function(node){
  55. if(node.nextSibling){
  56. ids.push(node.getNextSiblingID());
  57. getNextSibling(node.nextSibling);
  58. }
  59. }
  60. //node
  61. if(node){
  62. if(node.parent){
  63. let updateA = {
  64. ID: node.getID(),
  65. ParentID: node.parent.getParentID(),
  66. NextSiblingID: node.parent.getNextSiblingID(),
  67. type: 'oprNode'
  68. };
  69. updateData.push(updateA);
  70. //parent
  71. let updateB = {
  72. ID: node.getParentID(),
  73. NextSiblingID: node.getID(),
  74. type: 'parentNode'
  75. }
  76. updateData.push(updateB);
  77. if(node.nextSibling){
  78. getNextSibling(node);
  79. let updateC = {
  80. ID: ids,
  81. ParentID: node.getID(),
  82. type: 'nextSiblingNode'
  83. }
  84. updateData.push(updateC);
  85. }
  86. if(node.preSibling){
  87. let updateD = {
  88. ID: node.preSibling.getID(),
  89. NextSiblingID: -1,
  90. type: 'preSiblingNode'
  91. }
  92. updateData.push(updateD);
  93. }
  94. billsAjax.upLevel(billsLibId, updateData, function(){
  95. console.log(`enter upL`);
  96. controller.upLevel();
  97. sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  98. tools.btnAction(btn, 'focusOnBills');
  99. });
  100. //rechargeController.createRecharge(controller.tree.selected);
  101. }
  102. }
  103. },
  104. downLevel: function(controller, btn){
  105. tools.btnClose(btn, 'focusOnBills');
  106. var node = controller.tree.selected;
  107. var updateData = [];
  108. if(node){
  109. if(node.preSibling){
  110. var updateA = {
  111. ID: node.preSibling.getID(),
  112. NextSiblingID: node.getNextSiblingID(),
  113. type: 'preSiblingNode'
  114. };
  115. var updateB = {
  116. ID: node.getID(),
  117. ParentID: node.preSibling.getID(),
  118. NextSiblingID: -1,
  119. type: 'oprNode'
  120. };
  121. updateData.push(updateA);
  122. updateData.push(updateB);
  123. if(node.preSibling.children.length > 0){
  124. var updateC = {
  125. ID: node.preSibling.children[node.preSibling.children.length -1].getID(),
  126. NextSiblingID: node.getID(),
  127. type: 'preChildren'
  128. }
  129. updateData.push(updateC);
  130. }
  131. billsAjax.downLevel(billsLibId, updateData, function(){
  132. controller.downLevel();
  133. sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  134. tools.btnAction(btn, 'focusOnBills');
  135. });
  136. //rechargeController.createRecharge(controller.tree.selected);
  137. }
  138. }
  139. },
  140. delete: function(controller, btn, totalJobs, totalItems){
  141. tools.btnClose(btn, 'focusOnBills');
  142. var node = controller.tree.selected;
  143. if(node){
  144. var deleteIds = [];
  145. var getDeleteIds = function(node){
  146. if(node){
  147. deleteIds.push(node.getID());
  148. var length = node.children.length;
  149. if(length > 0){
  150. for(var i=0; i<length; i++){
  151. getDeleteIds(node.children[i]);
  152. }
  153. }
  154. }
  155. };
  156. getDeleteIds(node);
  157. let updateNode = node.preSibling ? {ID: node.preSibling.getID(), NextSiblingID: node.getNextSiblingID()} : null;
  158. billsAjax.deleteBills(billsLibId, deleteIds, updateNode, function(){
  159. controller.delete();
  160. //to solve refresh after deleted
  161. let jobsSheet = jobsSpread.getActiveSheet();
  162. let itemsSheet = itemsSpread.getActiveSheet();
  163. let nowNode = controller.tree.selected ;
  164. if(nowNode){
  165. let jobs = controller.tree.selected.jobs;
  166. $('#exampleTextarea').val(controller.tree.selected.data.recharge);
  167. setSheet.setMaxRowCount(jobsSheet, jobs);
  168. if(jobs.length > 0){
  169. tools.orderReshowData(jobsSheet, jobs, jobsSetting, 'job', false);
  170. orgJobData = jobsSheet.getValue(0, 0);
  171. }
  172. let items = controller.tree.selected.items;
  173. setSheet.setMaxRowCount(itemsSheet, items);
  174. if(items.length > 0){
  175. tools.orderReshowData(itemsSheet, items, itemsSetting, 'item', false);
  176. orgItemData = itemsSheet.getValue(0, 0);
  177. }
  178. }
  179. else {
  180. tools.clearData(jobsSheet);
  181. tools.clearData(itemsSheet);
  182. $('#exampleTextarea').val("");
  183. }
  184. sheetDatas = tools.getsheetDatas(jobsSheet, 'jobs');
  185. sheetItemsDatas = tools.getsheetDatas(itemsSheet, 'items');
  186. sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  187. tools.btnAction(btn, 'focusOnBills');
  188. //to solve refresh after deleted
  189. });
  190. }
  191. },
  192. upMove: function(controller, btn){
  193. tools.btnClose(btn, 'focusOnBills');
  194. var node = controller.tree.selected;
  195. var updateData = [];
  196. if(node){
  197. if(node.preSibling){
  198. let updateA = {
  199. ID: node.preSibling.getID(),
  200. NextSiblingID: node.getNextSiblingID()
  201. };
  202. let updateB = {
  203. ID: node.getID(),
  204. NextSiblingID: node.preSibling.getID()
  205. };
  206. updateData.push(updateA);
  207. updateData.push(updateB);
  208. if(node.preSibling.preSibling){
  209. let updateC = {
  210. ID: node.preSibling.preSibling.getID(),
  211. NextSiblingID: node.getID()
  212. }
  213. updateData.push(updateC);
  214. }
  215. billsAjax.upMove(billsLibId, updateData, function(){
  216. controller.upMove();
  217. sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  218. tools.btnAction(btn, 'focusOnBills');
  219. });
  220. }
  221. }
  222. },
  223. downMove: function(controller, btn){
  224. tools.btnClose(btn, 'focusOnBills');
  225. var node = controller.tree.selected;
  226. var updateData = [];
  227. if(node){
  228. if(node.nextSibling){
  229. var updateA = {
  230. ID: node.getNextSiblingID(),
  231. NextSiblingID: node.getID(),
  232. type: 'nextSiblingNode'
  233. };
  234. var updateB = {
  235. ID: node.getID(),
  236. NextSiblingID: node.nextSibling.getNextSiblingID(),
  237. type: 'oprNode'
  238. };
  239. updateData.push(updateA);
  240. updateData.push(updateB);
  241. if(node.preSibling){
  242. var updateC = {
  243. ID: node.preSibling.getID(),
  244. NextSiblingID: node.getNextSiblingID(),
  245. type: 'preSiblingNode'
  246. };
  247. updateData.push(updateC);
  248. }
  249. billsAjax.downMove(billsLibId, updateData);
  250. controller.downMove();
  251. sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  252. tools.btnAction(btn, 'focusOnBills');
  253. }
  254. }
  255. },
  256. editData: function(controller){
  257. controller.sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  258. var node = controller.tree.selected, updateId, field;
  259. if(node){
  260. updateId = node.getID();
  261. billsLibSetting.cols.forEach(function(col, idx){
  262. if(args.col === idx){
  263. field = col.data.field;
  264. node.data[field] = args.editingText;
  265. sheetBillsDatas.datasIdx['rowIdx'+ args.row][field] = args.editingText;
  266. console.log(sheetBillsDatas);
  267. }
  268. });
  269. billsAjax.updateBills(billsLibId, updateId, field, args.editingText);
  270. //sheetBillsDatas = tools.getsheetDatas(controller.sheet, 'bills', controller);
  271. }
  272. else {
  273. args.sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  274. }
  275. });
  276. }
  277. };
  278. var createObj = {
  279. newJob: function(data){
  280. var Job = function(data){
  281. this.data = data;
  282. //count = 0则没有被引用了
  283. this.count = 0;
  284. };
  285. return new Job(data);
  286. },
  287. newJobs: function(){
  288. var TotalJobs = function(){
  289. this.jobs = {};
  290. this.jobsArr = [];
  291. this.prefix = '_id';
  292. };
  293. TotalJobs.prototype.findJob = function(id){
  294. return this.jobs[this.prefix + id] ? this.jobs[this.prefix + id] : null;
  295. }
  296. TotalJobs.prototype.loadJobs = function (nodes, datas){
  297. var me = this;
  298. datas.forEach(function(jobData){
  299. var job = createObj.newJob(jobData);
  300. me.jobs[me.prefix + jobData.id] = job;
  301. me.jobsArr.push(job);
  302. });
  303. if(nodes){
  304. nodes.forEach(function(node){
  305. node.jobs = new Array();
  306. /*node.data.jobs.forEach(function(jobId){
  307. if(me.jobs[me.prefix + jobId]){
  308. node.jobs.push(me.jobs[me.prefix + jobId]);
  309. me.jobs[me.prefix + jobId].count ++;
  310. }
  311. else {
  312. node.data.jobs.splice(node.data.jobs.indexOf(jobId), 1);
  313. }
  314. });*/
  315. //update
  316. node.data.jobs.forEach(function(obj){
  317. if(me.jobs[me.prefix + obj.id]){
  318. node.jobs.push({job: me.jobs[me.prefix + obj.id], serialNo: obj.serialNo});
  319. me.jobs[me.prefix + obj.id].count ++;
  320. }
  321. else {
  322. node.data.jobs.splice(node.data.jobs.indexOf(obj), 1);
  323. }
  324. });
  325. });
  326. }
  327. };
  328. TotalJobs.prototype.getUpdateIds = function(nodes, repeatId){
  329. var ids = [];
  330. nodes.forEach(function(node){
  331. node.jobs.forEach(function(job){
  332. if(job.data.id === repeatId){
  333. ids.push(node.getID());
  334. }
  335. });
  336. });
  337. return ids;
  338. };
  339. return new TotalJobs();
  340. },
  341. newItem: function(data){
  342. var Item = function(data){
  343. this.data = data;
  344. this.count = 0;
  345. };
  346. return new Item(data);
  347. },
  348. newItems: function(){
  349. var TotalItems = function(){
  350. this.items = {};
  351. this.itemsArr = [];
  352. this.prefix = '_id';
  353. };
  354. TotalItems.prototype.findItem = function(id){
  355. return this.items[this.prefix + id] ? this.items[this.prefix + id] : null;
  356. }
  357. TotalItems.prototype.loadItems = function (nodes, datas){
  358. var me = this;
  359. datas.forEach(function(itemData){
  360. var item = createObj.newItem(itemData);
  361. me.items[me.prefix + itemData.id] = item;
  362. me.itemsArr.push(item);
  363. });
  364. if(nodes){
  365. nodes.forEach(function(node){
  366. node.items = new Array();
  367. /*node.data.items.forEach(function(itemID){
  368. if(me.items[me.prefix + itemID]){
  369. node.items.push(me.items[me.prefix + itemID]);
  370. me.items[me.prefix + itemID].count ++;
  371. }
  372. else {
  373. node.data.items.splice(node.data.items.indexOf(itemID), 1);
  374. }
  375. });*/
  376. node.data.items.forEach(function(obj){
  377. if(me.items[me.prefix + obj.id]){
  378. node.items.push({item: me.items[me.prefix + obj.id], serialNo: obj.serialNo});
  379. me.items[me.prefix + obj.id].count ++;
  380. }
  381. else {
  382. node.data.items.splice(node.data.items.indexOf(obj), 1);
  383. }
  384. });
  385. });
  386. }
  387. };
  388. TotalItems.prototype.getUpdateIds = function(nodes, repeatId){
  389. var ids = [];
  390. nodes.forEach(function(node){
  391. node.items.forEach(function(item){
  392. if(item.data.id === repeatId){
  393. ids.push(node.getID());
  394. }
  395. });
  396. });
  397. return ids;
  398. };
  399. return new TotalItems();
  400. }
  401. };
  402. var tools = {
  403. btnClose: function (btn, attr) {
  404. btn.css("opacity", "0.2");
  405. btn.addClass("disabled");
  406. btn.attr(attr, true);
  407. },
  408. btnAction: function (btn, attr) {
  409. btn.css("opacity", "");
  410. btn.removeClass("disabled");
  411. btn.removeAttr(attr);
  412. },
  413. isExist: function (totalArr, field, newData, orgData){
  414. var isExist = false;
  415. if(totalArr.length > 0){
  416. totalArr.forEach(function(item){
  417. if(item.data[field] == newData && newData !== orgData){
  418. isExist = true;
  419. }
  420. });
  421. }
  422. return isExist;
  423. },
  424. isRepeat: function(arr, field, newData, ref, classify){
  425. var isRepeat = false;
  426. if(classify){
  427. if(arr){
  428. arr.forEach(function(item){
  429. if(ref === 'reference' && item[classify].data[field] == newData){
  430. isRepeat = true;
  431. }
  432. else if(ref === 'document' && item[field] === newData){
  433. isRepeat = true;
  434. }
  435. });
  436. }
  437. }
  438. else {
  439. if(arr){
  440. arr.forEach(function(item){
  441. if(ref === 'reference' && item.data[field] == newData){
  442. isRepeat = true;
  443. }
  444. else if(ref === 'document' && item[field] === newData){
  445. isRepeat = true;
  446. }
  447. });
  448. }
  449. }
  450. return isRepeat;
  451. },
  452. addAttr: function(attr){
  453. if(!attr){
  454. attr = new Array();
  455. }
  456. },
  457. isEmptyObj: function(obj){
  458. let t;
  459. for(t in obj)
  460. return !1;
  461. return !0;
  462. },
  463. reSetCell: function(sheet, rowIdx, colIdx, value, id){
  464. if(value && colIdx!== null){
  465. sheet.getCell(rowIdx, colIdx, GC.Spread.Sheets.SheetArea.viewport).value(value);
  466. }
  467. sheet.setTag(rowIdx, 0, id);
  468. sheet.setTag(rowIdx, 1, id);
  469. },
  470. getIndex: function(arr, id, classify){
  471. var index;
  472. arr.forEach(function(item){
  473. if(item[classify].data.id === id){
  474. index = arr.indexOf(item);
  475. }
  476. });
  477. return index;
  478. },
  479. getObj: function(arr, id, classify){
  480. for(let i=0; i<arr.length; i++){
  481. if(arr[i][classify].data.id === id){
  482. return arr[i];
  483. }
  484. }
  485. return null;
  486. },
  487. getNewCode: function(totalObj, classify){
  488. let arr;
  489. if(classify === 'jobs'){
  490. arr = totalObj.jobsArr;
  491. }
  492. else {
  493. arr = totalObj.itemsArr;
  494. }
  495. tools.resort(arr, 'code', false);
  496. return arr.length > 0 ? arr[arr.length - 1].data.code : 0;
  497. },
  498. getValidDatas: function(sheet, setting, args){
  499. let validDatas = [];
  500. const colLen = 4;
  501. let orgRow = args.cellRange.row, orgCol = args.cellRange.col, rowCount = args.cellRange.rowCount, colCount = args.cellRange.colCount;
  502. let maxRow = orgRow + rowCount - 1, maxCol = orgCol + colCount -1;
  503. for(let i =orgRow; i<=maxRow; i++){
  504. let validData = {};
  505. for(let j= orgCol; j<=maxCol; j++){
  506. let value = sheet.getValue(i, j);
  507. if(value){
  508. setting.cols.forEach(function(col, colIdx){
  509. if(colIdx === j){
  510. validData[col.data.field] = value;
  511. }
  512. });
  513. }
  514. }
  515. if(!tools.isEmptyObj(validData)){
  516. validDatas.push(validData);
  517. }
  518. }
  519. //---------------------------------------
  520. /*for(let i=orgRow; i<= maxRow; i++){
  521. if(tools.getValidRow(sheet, i)){
  522. console.log(`validRow: ${i}`);
  523. let validData = {code: '', name: '', unit: '', ruleText: ''};
  524. for(var j=orgCol; j<=maxCol; j++){
  525. let field;
  526. setting.cols.forEach(function(col, colIdx){
  527. if(colIdx === j){
  528. field = col.data.field;
  529. if(sheet.getValue(i, j)){
  530. validData[field] = sheet.getValue(i, j);
  531. }
  532. }
  533. });
  534. //validData[field] = sheet.getValue(i, j);
  535. }
  536. validDatas.push(validData);
  537. }
  538. }*/
  539. return validDatas;
  540. },
  541. getValidRow: function(sheet, rowIdx){
  542. const colCount = 4;
  543. let isValid = false;
  544. for(let i=0; i< colCount; i++){
  545. if(sheet.getValue(rowIdx, i, GC.Spread.Sheets.SheetArea.viewport)){
  546. isValid = true;
  547. }
  548. }
  549. return isValid;
  550. },
  551. getDelDatas: function(controller, totalJobs, totalItems){
  552. let delJobs = controller.tree.selected.jobs,
  553. delItems = controller.tree.selected.items,
  554. delJobsIds = [], delItemsIds = [];
  555. if(delJobs.length > 0){
  556. delJobs.forEach()
  557. }
  558. },
  559. delteSheets: function(controller, totalJobs, totalItems, jobsSheet, itemsSheet){
  560. var delJobs = controller.tree.selected.jobs;
  561. var delItems = controller.tree.selected.items;
  562. var delJobsIds = [];
  563. var delItemsIds = [];
  564. if(delJobs.length > 0){
  565. delJobs.forEach(function(obj){
  566. obj.job.count --;
  567. if(obj.job.count <=0){
  568. totalJobs.jobsArr.splice(totalJobs.jobsArr.indexOf(obj), 1);
  569. delJobsIds.push(obj.job.data.id)
  570. }
  571. });
  572. jobsAjax.deleteJobContent(delJobsIds);
  573. tools.clearData(jobsSheet);
  574. tools.orderReshowData(jobsSheet, controller.tree.selected.jobs, jobsSetting, 'job', true);
  575. }
  576. if(delItems.length > 0){
  577. delItems.forEach(function(obj){
  578. obj.item.count --;
  579. if(obj.item.count <=0){
  580. totalItems.itemsArr.splice(totalItems.itemsArr.indexOf(obj), 1);
  581. delItemsIds.push(obj.item.data.id)
  582. }
  583. });
  584. itemsAjax.deleteItemCharacter(delItemsIds);
  585. tools.clearData(itemsSheet);
  586. tools.orderReshowData(itemsSheet, controller.tree.selected.items, itemsSetting, 'item', true);
  587. }
  588. controller.delete();
  589. },
  590. deleteELes: function (arr, delIds, classifyStr){
  591. var ids = [];
  592. delIds.forEach(function(delId){
  593. arr.forEach(function(ele){
  594. if(ele[classifyStr].data.id === delId){
  595. arr.splice(arr.indexOf(ele), 1);
  596. ele.count--;
  597. /*if(ele.count <= 0){
  598. ids.push(ele.data.id);
  599. }*/
  600. }
  601. });
  602. });
  603. /* if(callback){
  604. callback(ids);
  605. }*/
  606. },
  607. getSerialNo: function(arr){
  608. if(arr.length > 0){
  609. tools.resortSerialNo(arr);
  610. return arr[arr.length - 1].serialNo + 1;
  611. }
  612. return 1;
  613. },
  614. resortSerialNo: function(arr){
  615. function compare(){
  616. return function (a, b){
  617. let valA = a.serialNo,
  618. valB = b.serialNo;
  619. return valA - valB;
  620. }
  621. }
  622. arr.sort(compare());
  623. },
  624. resort: function(arr, attr, isValue){
  625. function compare(attr){
  626. return function (a, b){
  627. var valA, valB;
  628. if(isValue){
  629. valA = a[attr];
  630. valB = a[attr];
  631. }
  632. else {
  633. valA = a.data[attr];
  634. valB = b.data[attr];
  635. }
  636. return valA - valB;
  637. }
  638. }
  639. arr.sort(compare(attr));
  640. },
  641. //update--
  642. orderReshowData: function(sheet, arr, setting, classify, isResort){
  643. tools.clearData(sheet);
  644. if(arr.length > 0){
  645. setSheet.setMaxRowCount(sheet, arr);
  646. if(isResort){
  647. tools.resortSerialNo(arr);
  648. }
  649. let length = arr.length;
  650. for(let i=0; i<length; i++){
  651. setting.cols.forEach(function(col, colIdx){
  652. sheet.setTag(i, colIdx, arr[i][classify].data.id);
  653. if(arr[i][classify].data[col.data.field]){
  654. sheet.getCell(i, colIdx).value(arr[i][classify].data[col.data.field]);
  655. }
  656. else {
  657. sheet.getCell(i, colIdx).value('');
  658. }
  659. });
  660. }
  661. }
  662. },
  663. reshowData: function(sheet, arr, setting, isResort){
  664. tools.clearData(sheet);
  665. if(isResort){
  666. tools.resort(arr, 'code', false);
  667. }
  668. if(arr.length > 0){
  669. var length = arr.length;
  670. for(var i=0; i<length; i++){
  671. setting.cols.forEach(function(col, colIdx){
  672. sheet.setTag(i, colIdx, arr[i].data.id);
  673. if(arr[i].data[col.data.field]){
  674. sheet.getCell(i, colIdx).value(arr[i].data[col.data.field]);
  675. }
  676. else {
  677. sheet.getCell(i, colIdx).value('');
  678. }
  679. });
  680. }
  681. }
  682. },
  683. reshowValue: function(sheet, arr, setting, isResort){
  684. tools.clearData(sheet);
  685. if(isResort){
  686. //tools.resort(arr, 'code', true);
  687. function myCompareCode(a, b){
  688. let valA = a.code,
  689. valB = b.code;
  690. return valA- valB;
  691. }
  692. arr.sort(myCompareCode);
  693. }
  694. if(arr.length > 0){
  695. var length = arr.length;
  696. for(var i=0; i<length; i++){
  697. setting.cols.forEach(function(col, colIdx){
  698. sheet.setTag(i, colIdx, arr[i].code);
  699. if(arr[i][col.data.field]){
  700. sheet.getCell(i, colIdx).value(arr[i][col.data.field]);
  701. }
  702. else {
  703. sheet.getCell(i, colIdx).value('');
  704. }
  705. });
  706. }
  707. }
  708. },
  709. clearData: function(sheet){
  710. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
  711. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.tag);
  712. },
  713. getRoot: function(node){
  714. return node.parent ? tools.getRoot(node.parent) : node;
  715. },
  716. getBillsIds: function(callback){
  717. billsAjax.getBills(billsLibId, function(datas){
  718. var ids = [];
  719. datas.forEach(function(data){
  720. ids.push(data.ID);
  721. });
  722. if(callback){
  723. callback(ids);
  724. }
  725. });
  726. },
  727. delIds: function(sheet){
  728. var ids = [];
  729. var sels = sheet.getSelections();
  730. sels.forEach(function(sel){
  731. var orgRow = sel.row, rowCount = sel.rowCount, colCount = sel.colCount;
  732. var orgCol = sel.col === -1 ? 0 : sel.col;
  733. var maxRow = orgRow + rowCount - 1;
  734. for(var i=orgRow; i<= maxRow; i++){
  735. if(sheet.getTag(i, orgCol)){
  736. ids.push(sheet.getTag(i, orgCol));
  737. }
  738. }
  739. });
  740. return ids;
  741. },
  742. redirect: function(billsLibId, newHref){
  743. mainAjax.getABillsLib(billsLibId, function(result){
  744. if(result.length === 0){
  745. window.location.href = newHref;
  746. }
  747. });
  748. },
  749. uniqArr: function (arr) {
  750. let newArr = [];
  751. for(let i=0; i<arr.length; i++){
  752. if(newArr.indexOf(arr[i]) === -1){
  753. newArr.push(arr[i]);
  754. }
  755. }
  756. return newArr;
  757. },
  758. uniqObjArr: function(arr){
  759. let uniqArr = [];
  760. for(let i =0; i< arr.length; i++){
  761. let uniqLen = uniqArr.length;
  762. let flag = false;
  763. if(uniqLen > 0){
  764. for(let j=0; j< uniqLen; j++){
  765. if(arr[i].field === uniqArr[j].field && arr[i].data === uniqArr[j].data){
  766. flag = true;
  767. //uniqArr.push(arr[i]);
  768. }
  769. }
  770. if(!flag){
  771. uniqArr.push(arr[i]);
  772. }
  773. }
  774. else {
  775. uniqArr.push(arr[i])
  776. }
  777. }
  778. return uniqArr;
  779. },
  780. resetRowIdx: function(arr, beginRow){
  781. let rowIdx = beginRow;
  782. for(let i=0; i< arr.length; i++){
  783. if(arr[i].rowIdx !== rowIdx){
  784. rowIdx++;
  785. arr[i].rowIdx = rowIdx;
  786. }
  787. }
  788. },
  789. getsheetDatas: function(sheet, classify, controller){
  790. let rowCount = sheet.getRowCount();
  791. const colIdx = 1;
  792. let sheetDatas;
  793. if(classify === 'total'){
  794. sheetDatas = [];
  795. for(let i=0; i<rowCount; i++){
  796. for(let j=0; j<= 1; j++){
  797. let data = sheet.getCell(i, j).value, id = sheet.getTag(i, j);
  798. if(data && id){
  799. let unitData ={
  800. rowIdx: i,
  801. colIdx: j,
  802. data: data,
  803. id: id
  804. };
  805. sheetDatas.push(unitData);
  806. }
  807. }
  808. }
  809. }
  810. else if(classify === 'bills'){
  811. sheetDatas = {datasIdx: {}, datas: []};
  812. for(let i= 0; i< controller.tree.items.length; i++){
  813. let code = sheet.getValue(i, 0), name = sheet.getValue(i, 1),
  814. unit = sheet.getValue(i, 2), ruleText = sheet.getValue(i, 3);
  815. let data = {code: code, name: name, unit: unit, ruleText: ruleText, rowIdx: i};
  816. sheetDatas.datas.push(data);
  817. sheetDatas.datasIdx['rowIdx' + i] = data;
  818. }
  819. }
  820. else{
  821. sheetDatas = [];
  822. for(let i=0; i<rowCount; i++){
  823. let data = sheet.getCell(i, colIdx).value(), id = sheet.getTag(i, colIdx);
  824. if(data && id){
  825. let rowData = {
  826. rowIdx: i,
  827. data: data,
  828. id: id
  829. }
  830. sheetDatas.push(rowData);
  831. }
  832. }
  833. }
  834. return sheetDatas;
  835. },
  836. encapData: function(uncrossedDatas, crossedDatas, controller, totalJobs){
  837. const UpdateExist = 'updateExist', UpdateNew = 'updateNew', CreateExist = 'createExist', CreateNew = 'createNew';
  838. let updateDatas = [];
  839. let createDatas = [];
  840. let nodeId = controller.tree.selected.getID();
  841. uncrossedDatas.forEach(obj => {
  842. let cnDataObj, ceDataObj, content, jobId;
  843. let isExisit = tools.isExist(totalJobs.jobsArr, 'content', obj.data, null);
  844. let isRepeat = tools.isRepeat(controller.tree.selected.jobs, 'content', obj.data, 'reference', 'job');
  845. if(isExisit && !isRepeat){
  846. totalJobs.jobsArr.forEach(job => {
  847. if(obj.data === job.data.content){
  848. jobId = job.data.id;
  849. }
  850. });
  851. ceDataObj= {
  852. newData: obj.data,
  853. billsLibId: billsLibId,
  854. nodeId: nodeId,
  855. jobId: jobId,
  856. serialNo: obj.serialNo,
  857. type: CreateExist
  858. };
  859. createDatas.push(ceDataObj);
  860. }
  861. if(!isExisit){
  862. maxJobsNumber++;
  863. //currentCountId++;
  864. cnDataObj = {
  865. //id: maxJobsNumber,
  866. billsLibId: billsLibId,
  867. nodeId: nodeId,
  868. newData: obj.data,
  869. code: maxJobsNumber,
  870. serialNo: obj.serialNo,
  871. type: CreateNew
  872. };
  873. createDatas.push(cnDataObj);
  874. }
  875. });
  876. crossedDatas.forEach(cData => {
  877. let ueObj, unObj, newJobId;
  878. let isExisit = tools.isExist(totalJobs.jobsArr, 'content', cData.newData, null);
  879. let isRepeat = tools.isRepeat(controller.tree.selected.jobs, 'content', cData.newData, 'reference', 'job');
  880. if(isExisit && !isRepeat){
  881. totalJobs.jobsArr.forEach(job => {
  882. if(cData.newData === job.data.content){
  883. newJobId = job.data.id;
  884. }
  885. });
  886. ueObj = {
  887. newData: cData.newData,
  888. billsLibId: billsLibId,
  889. nodeId: nodeId,
  890. newJobId: newJobId,
  891. orgJobId: cData.orgId,
  892. serialNo:cData.serialNo,
  893. type: UpdateExist
  894. };
  895. updateDatas.push(ueObj);
  896. }
  897. if(!isExisit && !isRepeat){
  898. maxJobsNumber++;
  899. //currentCountId++;
  900. unObj = {
  901. //newJobId: maxJobsNumber,
  902. billsLibId: billsLibId,
  903. nodeId: nodeId,
  904. newData: cData.newData,
  905. orgJobId: cData.orgId,
  906. code: maxJobsNumber,
  907. serialNo: cData.serialNo,
  908. type: UpdateNew
  909. };
  910. updateDatas.push(unObj);
  911. }
  912. });
  913. let pasteDatas = {
  914. updateDatas: updateDatas,
  915. createDatas: createDatas
  916. };
  917. return pasteDatas;
  918. },
  919. encapItemsData: function(uncrossedDatas, crossedDatas, controller, totalItems){
  920. const UpdateExist = 'updateExist', UpdateNew = 'updateNew', CreateExist = 'createExist', CreateNew = 'createNew';
  921. let updateDatas = [];
  922. let createDatas = [];
  923. let nodeId = controller.tree.selected.getID();
  924. uncrossedDatas.forEach(obj => {
  925. let cnDataObj, ceDataObj, content,itemId;
  926. let isExisit = tools.isExist(totalItems.itemsArr, 'content', obj.data, null);
  927. let isRepeat = tools.isRepeat(controller.tree.selected.items, 'content', obj.data, 'reference', 'item');
  928. if(isExisit && !isRepeat){
  929. totalItems.itemsArr.forEach(item => {
  930. if(obj.data === item.data.content){
  931. itemId = item.data.id;
  932. }
  933. });
  934. ceDataObj= {
  935. newData: obj.data,
  936. billsLibId: billsLibId,
  937. nodeId: nodeId,
  938. itemId: itemId,
  939. serialNo: obj.serialNo,
  940. type: CreateExist
  941. };
  942. createDatas.push(ceDataObj);
  943. }
  944. if(!isExisit){
  945. maxItemsNumber++;
  946. //currentCountId++;
  947. cnDataObj = {
  948. // id: maxItemsNumber,
  949. billsLibId: billsLibId,
  950. nodeId: nodeId,
  951. newData: obj.data,
  952. serialNo: obj.serialNo,
  953. code: maxItemsNumber,
  954. type: CreateNew
  955. };
  956. createDatas.push(cnDataObj);
  957. }
  958. });
  959. crossedDatas.forEach(cData => {
  960. let ueObj, unObj, newItemId;
  961. let isExisit = tools.isExist(totalItems.itemsArr, 'content', cData.newData, null);
  962. let isRepeat = tools.isRepeat(controller.tree.selected.items, 'content', cData.newData, 'reference', 'item');
  963. if(isExisit && !isRepeat){
  964. totalItems.itemsArr.forEach(item => {
  965. if(cData.newData === item.data.content){
  966. newItemId = item.data.id;
  967. }
  968. });
  969. ueObj = {
  970. newData: cData.newData,
  971. billsLibId: billsLibId,
  972. nodeId: nodeId,
  973. newItemId: newItemId,
  974. orgItemId: cData.orgId,
  975. serialNo: cData.serialNo,
  976. type: UpdateExist
  977. };
  978. updateDatas.push(ueObj);
  979. }
  980. if(!isExisit && !isRepeat){
  981. maxItemsNumber++;
  982. //currentCountId++;
  983. unObj = {
  984. // newItemId: maxItemsNumber,
  985. billsLibId: billsLibId,
  986. nodeId: nodeId,
  987. newData: cData.newData,
  988. orgItemId: cData.orgId,
  989. code: maxItemsNumber,
  990. serialNo: cData.serialNo,
  991. type: UpdateNew
  992. };
  993. updateDatas.push(unObj);
  994. }
  995. });
  996. let pasteDatas = {
  997. updateDatas: updateDatas,
  998. createDatas: createDatas
  999. };
  1000. return pasteDatas;
  1001. },
  1002. encapTotalJobsDatas: function(sheet, totalJobs, uncrossedDatas, crossedDatas){
  1003. let pasteDatas;
  1004. let rebuildArr =[];
  1005. if(crossedDatas.length > 0){
  1006. for(let i=0; i< crossedDatas.length; i++){
  1007. let field = crossedDatas[i].field, data = crossedDatas[i].data;
  1008. let isRepeat = tools.isRepeat(totalJobs.jobsArr, field, data, 'reference');
  1009. if(isRepeat){
  1010. crossedDatas.splice(i--, 1);
  1011. }
  1012. }
  1013. for(let i=0; i< crossedDatas.length; i++){
  1014. let field = crossedDatas[i].field, data = crossedDatas[i].data;
  1015. if(crossedDatas[i].field === 'code' && typeof data !== 'number' ){
  1016. crossedDatas.splice(i--, 1);
  1017. }
  1018. }
  1019. }
  1020. if(uncrossedDatas.length > 0){
  1021. for(let i=0; i< uncrossedDatas.length; i++){
  1022. let field = uncrossedDatas[i].field;
  1023. let isRepeat = tools.isRepeat(totalJobs.jobsArr, field, uncrossedDatas[i].data, 'reference');
  1024. if(isRepeat){
  1025. uncrossedDatas.splice(i--, 1);
  1026. }
  1027. }
  1028. for(let i=0; i< uncrossedDatas.length; i++){
  1029. let field = uncrossedDatas[i].field;
  1030. if(uncrossedDatas[i].field === 'code' && typeof uncrossedDatas[i].data !== 'number' ){
  1031. uncrossedDatas.splice(i--, 1);
  1032. }
  1033. }
  1034. rebuildArr = tools.rebuildArr(uncrossedDatas, 'create');
  1035. function myCompare(a, b){
  1036. if(a.rowIdx > b.rowIdx)
  1037. return 1;
  1038. if(a.rowIdx < b.rowIdx)
  1039. return -1;
  1040. return 0;
  1041. }
  1042. function myCompareCode(a, b){
  1043. if(a.code > b.code)
  1044. return -1;
  1045. if(a.code < b.code)
  1046. return 1;
  1047. return 0;
  1048. }
  1049. if(rebuildArr.length > 0){
  1050. rebuildArr.sort(myCompareCode);
  1051. maxJobsNumber = maxJobsNumber + 1 > rebuildArr[0].code ? maxJobsNumber : rebuildArr[0].code;
  1052. rebuildArr.forEach(function(data){
  1053. if(data.type !== 'CreateT' && data.field === 'content' ){
  1054. maxJobsNumber++;
  1055. let code = maxJobsNumber;
  1056. data.code = code;
  1057. }
  1058. });
  1059. }
  1060. //rebuildArr.sort(myCompare);
  1061. }
  1062. //rebuildArr.sort(myCompare);
  1063. pasteDatas = {
  1064. updateDatas: crossedDatas,
  1065. createDatas: rebuildArr
  1066. }
  1067. return pasteDatas;
  1068. },
  1069. encapTotalItemsDatas: function(sheet, totalItems, uncrossedDatas, crossedDatas){
  1070. let pasteDatas;
  1071. let rebuildArr =[];
  1072. if(crossedDatas.length > 0){
  1073. for(let i=0; i< crossedDatas.length; i++){
  1074. let field = crossedDatas[i].field, data = crossedDatas[i].data;
  1075. let isRepeat = tools.isRepeat(totalItems.itemsArr, field, data, 'reference');
  1076. if(isRepeat){
  1077. crossedDatas.splice(i--, 1);
  1078. }
  1079. }
  1080. for(let i=0; i< crossedDatas.length; i++){
  1081. let field = crossedDatas[i].field, data = crossedDatas[i].data;
  1082. if(crossedDatas[i].field === 'code' && typeof data !== 'number' ){
  1083. crossedDatas.splice(i--, 1);
  1084. }
  1085. }
  1086. }
  1087. if(uncrossedDatas.length > 0){
  1088. for(let i=0; i< uncrossedDatas.length; i++){
  1089. let field = uncrossedDatas[i].field;
  1090. let isRepeat = tools.isRepeat(totalItems.itemsArr, field, uncrossedDatas[i].data, 'reference');
  1091. if(isRepeat){
  1092. uncrossedDatas.splice(i--, 1);
  1093. }
  1094. }
  1095. for(let i=0; i< uncrossedDatas.length; i++){
  1096. let field = uncrossedDatas[i].field;
  1097. if(uncrossedDatas[i].field === 'code' && typeof uncrossedDatas[i].data !== 'number' ){
  1098. uncrossedDatas.splice(i--, 1);
  1099. }
  1100. }
  1101. rebuildArr = tools.rebuildArr(uncrossedDatas, 'create');
  1102. function myCompare(a, b){
  1103. if(a.rowIdx > b.rowIdx)
  1104. return 1;
  1105. if(a.rowIdx < b.rowIdx)
  1106. return -1;
  1107. return 0;
  1108. }
  1109. function myCompareCode(a, b){
  1110. if(a.code > b.code)
  1111. return -1;
  1112. if(a.code < b.code)
  1113. return 1;
  1114. return 0;
  1115. }
  1116. //rebuildArr.sort(myCompare);
  1117. if(rebuildArr.length > 0){
  1118. rebuildArr.sort(myCompareCode);
  1119. maxItemsNumber = maxItemsNumber + 1 > rebuildArr[0].code ? maxItemsNumber : rebuildArr[0].code;
  1120. rebuildArr.forEach(function(data){
  1121. if(data.type !== 'CreateT' && data.field === 'content' ){
  1122. maxItemsNumber ++;
  1123. let code = maxItemsNumber;
  1124. data.code = code;
  1125. }
  1126. });
  1127. }
  1128. }
  1129. // rebuildArr.sort(myCompare);
  1130. pasteDatas = {
  1131. updateDatas: crossedDatas,
  1132. createDatas: rebuildArr
  1133. }
  1134. return pasteDatas;
  1135. },
  1136. encapValues: function(sheet, totalItems, uncrossedDatas, crossedDatas){
  1137. let rebuildArr =[];
  1138. let rebuildUpdateDatas = [];
  1139. let valuesArr = valueController.getValues(totalItems, selectedId);
  1140. function myCompare(a, b){
  1141. if(a.rowIdx > b.rowIdx)
  1142. return 1;
  1143. if(a.rowIdx < b.rowIdx)
  1144. return -1;
  1145. return 0;
  1146. }
  1147. if(crossedDatas.length > 0){
  1148. for(let i=0; i< crossedDatas.length; i++){
  1149. let field = crossedDatas[i].field, data = crossedDatas[i].data;
  1150. let isRepeat = tools.isRepeat(valuesArr, field, data, 'document', 'value');
  1151. if(isRepeat){
  1152. crossedDatas.splice(i--, 1);
  1153. }
  1154. }
  1155. for(let i=0; i< crossedDatas.length; i++){
  1156. let field = crossedDatas[i].field, data = crossedDatas[i].data;
  1157. if(crossedDatas[i].field === 'code' && typeof data !== 'number' ){
  1158. crossedDatas.splice(i--, 1);
  1159. }
  1160. }
  1161. rebuildUpdateDatas = tools.rebuildArr(crossedDatas, 'update');
  1162. rebuildUpdateDatas.sort(myCompare);
  1163. for(let i =0; i< rebuildUpdateDatas.length; i++){
  1164. rebuildUpdateDatas[i].itemId = selectedId;
  1165. }
  1166. }
  1167. if(uncrossedDatas.length > 0){
  1168. for(let i=0; i< uncrossedDatas.length; i++){
  1169. let field = uncrossedDatas[i].field;
  1170. let isRepeat = tools.isRepeat(valuesArr, field, uncrossedDatas[i].data, 'document', 'value');
  1171. if(isRepeat){
  1172. uncrossedDatas.splice(i--, 1);
  1173. }
  1174. }
  1175. for(let i=0; i< uncrossedDatas.length; i++){
  1176. let field = uncrossedDatas[i].field;
  1177. if(uncrossedDatas[i].field === 'code' && typeof uncrossedDatas[i].data !== 'number' ){
  1178. uncrossedDatas.splice(i--, 1);
  1179. }
  1180. }
  1181. rebuildArr = tools.rebuildArr(uncrossedDatas, 'create');
  1182. //rebuildArr.sort(myCompare);
  1183. function myCompareCode(a, b){
  1184. let valA = a.code,
  1185. valB = b.code;
  1186. return valB- valA;
  1187. }
  1188. rebuildArr.sort(myCompareCode);
  1189. console.log(`rebuildArr`);
  1190. console.log(rebuildArr);
  1191. let newCode = valueController.getCode(totalItems, selectedId);
  1192. newCode = newCode > rebuildArr[0].code ? newCode : rebuildArr[0].code + 1;
  1193. for(let i=0; i< rebuildArr.length; i++){
  1194. rebuildArr[i].itemId = selectedId;
  1195. if(rebuildArr[i].type !== 'CreateT' && rebuildArr[i].field === 'value'){
  1196. rebuildArr[i].code = newCode;
  1197. newCode++;
  1198. }
  1199. }
  1200. }
  1201. //backendOP
  1202. let pasteDatas = {
  1203. updateDatas: rebuildUpdateDatas,
  1204. createDatas: rebuildArr
  1205. };
  1206. return pasteDatas;
  1207. },
  1208. rebuildArr: function(arr, classify){
  1209. let conformDatas = [];
  1210. let flag = true;
  1211. for(let i=0; i< arr.length; i++){
  1212. for(let j=i+1; j<arr.length; j++){
  1213. if(arr[i].rowIdx === arr[j].rowIdx){
  1214. flag = false;
  1215. let field = arr[i].field;
  1216. let conformData;
  1217. if(classify === 'create'){
  1218. if(field === 'code'){
  1219. conformData = {
  1220. billsLibId: billsLibId,
  1221. code: arr[i].data,
  1222. content: arr[j].data,
  1223. rowIdx: arr[i].rowIdx,
  1224. type: 'CreateT'
  1225. }
  1226. }
  1227. else {
  1228. conformData = {
  1229. billsLIbId: billsLibId,
  1230. code: arr[j].data,
  1231. content: arr[i].data,
  1232. rowIdx: arr[i].rowIdx,
  1233. type: 'CreateT'
  1234. }
  1235. }
  1236. }
  1237. else {
  1238. if(field === 'code'){
  1239. conformData = {
  1240. billsLibId: billsLibId,
  1241. code: arr[i].data,
  1242. content: arr[j].data,
  1243. rowIdx: arr[i].rowIdx,
  1244. orgId: arr[i].orgId,
  1245. type: 'UpdateT'
  1246. }
  1247. }
  1248. else {
  1249. conformData = {
  1250. billsLIbId: billsLibId,
  1251. code: arr[j].data,
  1252. content: arr[i].data,
  1253. rowIdx: arr[i].rowIdx,
  1254. orgId: arr[i].orgId,
  1255. type: 'UpdateT'
  1256. }
  1257. }
  1258. }
  1259. conformDatas.push(conformData);
  1260. }
  1261. }
  1262. }
  1263. for(let i =0; i< arr.length; i++){
  1264. let flag = true;
  1265. for(let j=0; j<arr.length; j++){
  1266. if(arr[i].rowIdx === arr[j].rowIdx && arr[i].field !== arr[j].field){
  1267. flag = false;
  1268. }
  1269. }
  1270. if(flag){
  1271. conformDatas.push(arr[i]);
  1272. }
  1273. }
  1274. return conformDatas;
  1275. }
  1276. };
  1277. let pasteController = {
  1278. frontOperator: function(sheet, setting, controller, totalJobs, datas){
  1279. const UpdateExist = 'updateExist', UpdateNew = 'updateNew', CreateExist = 'createExist', CreateNew = 'createNew';
  1280. if(datas.length > 0){
  1281. for(let i=0; i<datas.length; i++){
  1282. if(datas[i].type === UpdateExist){
  1283. totalJobs.jobsArr.forEach(job => {
  1284. if(job.data.content === datas[i].newData){
  1285. let serialNo = datas[i].serialNo;
  1286. let index = tools.getIndex(controller.tree.selected.jobs, datas[i].orgJobId, 'job');
  1287. job.count ++;
  1288. controller.tree.selected.jobs.splice(index, 1);
  1289. controller.tree.selected.jobs.splice(index, 0, {job: job, serialNo: serialNo});
  1290. }
  1291. });
  1292. }
  1293. if(datas[i].type === UpdateNew){
  1294. let newJobData, newJob;
  1295. let newJobId = datas[i].newJobId, serialNo = datas[i].serialNo;
  1296. newJobData = {id: newJobId, content: datas[i].newData, code: datas[i].code};
  1297. newJob = createObj.newJob(newJobData);
  1298. newJob.count = 1;
  1299. totalJobs.jobs[totalJobs.prefix + newJobId] = newJob;
  1300. totalJobs.jobsArr.push(newJob);
  1301. var index = tools.getIndex(controller.tree.selected.jobs, datas[i].orgJobId, 'job');
  1302. controller.tree.selected.jobs.splice(index, 1);
  1303. controller.tree.selected.jobs.splice(index, 0, {job: newJob, serialNo: serialNo});
  1304. }
  1305. if(datas[i].type === CreateExist){
  1306. let serialNo = datas[i].serialNo;
  1307. totalJobs.jobsArr.forEach(job => {
  1308. if(job.data.content === datas[i].newData){
  1309. job.count ++;
  1310. controller.tree.selected.jobs.push({job: job, serialNo: serialNo});
  1311. }
  1312. });
  1313. }
  1314. if(datas[i].type === CreateNew){
  1315. let newJobId = datas[i].newJobId, serialNo = datas[i].serialNo;
  1316. let newJobData, newJob;
  1317. newJobData = {id: newJobId, content: datas[i].newData, code: datas[i].code};
  1318. newJob = createObj.newJob(newJobData);
  1319. newJob.count = 1;
  1320. totalJobs.jobs[totalJobs.prefix + newJobId] = newJob;
  1321. totalJobs.jobsArr.push(newJob);
  1322. controller.tree.selected.jobs.push({job: newJob, serialNo: serialNo});
  1323. }
  1324. }
  1325. }
  1326. //resort&reshow
  1327. tools.orderReshowData(sheet, controller.tree.selected.jobs, setting, 'job', true);
  1328. sheetDatas = tools.getsheetDatas(sheet);
  1329. },
  1330. frontItemsRelOperator: function(sheet, setting, controller, totalItems, datas){
  1331. const UpdateExist = 'updateExist', UpdateNew = 'updateNew', CreateExist = 'createExist', CreateNew = 'createNew';
  1332. if(datas.length > 0){
  1333. for(let i =0; i<datas.length; i++){
  1334. if(datas[i].type === UpdateExist){
  1335. let serialNo = datas[i].serialNo;
  1336. totalItems.itemsArr.forEach(item => {
  1337. if(item.data.content === datas[i].newData){
  1338. let index = tools.getIndex(controller.tree.selected.items, datas[i].orgItemId, 'item');
  1339. item.count ++;
  1340. controller.tree.selected.items.splice(index, 1);
  1341. controller.tree.selected.items.splice(index, 0, {item: item, serialNo: serialNo});
  1342. }
  1343. });
  1344. }
  1345. if(datas[i].type === UpdateNew){
  1346. let newItemData, newItem;
  1347. let newItemId = datas[i].newItemId, serialNo = datas[i].serialNo;
  1348. newItemData = {id: newItemId, content: datas[i].newData, code: datas[i].code};
  1349. newItem = createObj.newItem(newItemData);
  1350. newItem.count = 1;
  1351. totalItems.items[totalItems.prefix + newItemId] = newItem;
  1352. totalItems.itemsArr.push(newItem);
  1353. var index = tools.getIndex(controller.tree.selected.items, datas[i].orgItemId, 'item');
  1354. controller.tree.selected.items.splice(index, 1);
  1355. controller.tree.selected.items.splice(index, 0, {item: newItem, serialNo: serialNo});
  1356. }
  1357. if(datas[i].type === CreateExist){
  1358. let serialNo = datas[i].serialNo;
  1359. totalItems.itemsArr.forEach(item => {
  1360. if(item.data.content === datas[i].newData){
  1361. item.count ++;
  1362. controller.tree.selected.items.push({item: item, serialNo: serialNo});
  1363. }
  1364. });
  1365. }
  1366. if(datas[i].type === CreateNew){
  1367. let newItemId = datas[i].newItemId, serialNo = datas[i].serialNo;
  1368. let newItemData, newItem;
  1369. newItemData = {id: newItemId, content: datas[i].newData, code: datas[i].code};
  1370. newItem = createObj.newItem(newItemData);
  1371. newItem.count = 1;
  1372. totalItems.items[totalItems.prefix + newItemId] = newItem;
  1373. totalItems.itemsArr.push(newItem);
  1374. controller.tree.selected.items.push({item: newItem, serialNo: serialNo});
  1375. }
  1376. }
  1377. }
  1378. //resort&reshow
  1379. tools.orderReshowData(sheet, controller.tree.selected.items, setting, 'item', true);
  1380. sheetItemsDatas = tools.getsheetDatas(sheet);
  1381. },
  1382. pasteJobsFront: function(sheet, totalJobs, datas){
  1383. const Update = 'Update', CreateT = 'CreateT';
  1384. if(datas.length > 0){
  1385. for(let i=0; i<datas.length; i++){
  1386. let newJobData, newJob;
  1387. if(datas[i].type === Update){
  1388. totalJobs.jobsArr.forEach(job => {
  1389. if(job.data.id === datas[i].orgId){
  1390. job.data[field] = datas[i].data;
  1391. }
  1392. });
  1393. }
  1394. if(datas[i].type === CreateT){
  1395. maxJobsNumber++;
  1396. maxJobsNumber = maxJobsNumber >= datas[i].code ? maxJobsNumber : datas[i].code;
  1397. newJobData = {id: datas[i].newJobId, content: datas[i].content, code: datas[i].code};
  1398. newJob = createObj.newJob(newJobData);
  1399. totalJobs.jobsArr.push(newJob);
  1400. }
  1401. if(datas[i].type !== CreateT && datas[i].field === 'content'){
  1402. newJobData = {id: datas[i].newJobId, content: datas[i].data, code: datas[i].code};
  1403. newJob = createObj.newJob(newJobData);
  1404. totalJobs.jobsArr.push(newJob);
  1405. }
  1406. if(datas[i].type !== 'CreateT' && datas[i].field === 'code'){
  1407. maxJobsNumber++;
  1408. maxJobsNumber = maxJobsNumber >= datas[i].data ? maxJobsNumber : datas[i].data;
  1409. newJobData = {id: datas[i].newJobId, content: '', code: datas[i].data};
  1410. newJob = createObj.newJob(newJobData);
  1411. totalJobs.jobsArr.push(newJob);
  1412. }
  1413. }
  1414. }
  1415. setSheet.setMaxRowCount(sheet, totalJobs.jobsArr);
  1416. tools.reshowData(sheet, totalJobs.jobsArr, totalJobsSetting, true);
  1417. },
  1418. pasteItemsFront: function(sheet, totalItems, datas){
  1419. console.log(`enterFront`);
  1420. const Update = 'Update', CreateT = 'CreateT';
  1421. if(datas.length > 0){
  1422. for(let i=0; i<datas.length; i++){
  1423. let newItemData, newItem;
  1424. if(datas[i].type === Update){
  1425. totalItems.itemsArr.forEach(item => {
  1426. if(item.data.id === datas[i].orgId){
  1427. item.data[field] = datas[i].data;
  1428. }
  1429. });
  1430. }
  1431. if(datas[i].type === CreateT){
  1432. maxItemsNumber++;
  1433. maxItemsNumber = maxItemsNumber >= datas[i].code ? maxItemsNumber : datas[i].code;
  1434. newItemData = {id: datas[i].newItemId, content: datas[i].content, code: datas[i].code};
  1435. newItem = createObj.newItem(newItemData);
  1436. totalItems.itemsArr.push(newItem);
  1437. totalItems.items[totalItems.prefix + datas[i].newItemId] = newItem;
  1438. }
  1439. if(datas[i].type !== CreateT && datas[i].field === 'content'){
  1440. newItemData = {id: datas[i].newItemId, content: datas[i].data, code: datas[i].code};
  1441. newItem = createObj.newItem(newItemData);
  1442. totalItems.itemsArr.push(newItem);
  1443. totalItems.items[totalItems.prefix + datas[i].newItemId] = newItem;
  1444. }
  1445. if(datas[i].type !== 'CreateT' && datas[i].field === 'code'){
  1446. maxItemsNumber++;
  1447. maxItemsNumber = maxItemsNumber >= datas[i].data ? maxItemsNumber : datas[i].data;
  1448. newItemData = {id: datas[i].newItemId, content: '', code: datas[i].data};
  1449. newItem = createObj.newItem(newItemData);
  1450. totalItems.itemsArr.push(newItem);
  1451. totalItems.items[totalItems.prefix + datas[i].newItemId] = newItem;
  1452. }
  1453. }
  1454. }
  1455. setSheet.setMaxRowCount(sheet, totalItems.itemsArr);
  1456. tools.reshowData(sheet, totalItems.itemsArr, totalItemsSetting, true);
  1457. },
  1458. pasteValueFront: function(sheet, totalItems, datas){
  1459. let backendUpdateDatas = [];
  1460. let valuesArr = valueController.getValues(totalItems, selectedId);
  1461. if(datas.length > 0){
  1462. for(let i =0; i< datas.length; i++){
  1463. let index;
  1464. if(datas[i].type === 'UpdateT'){
  1465. if(valuesArr){
  1466. for(let j=0; j< valuesArr.length; j++){
  1467. if(datas[i].orgId === valuesArr[j].code){
  1468. index = j;
  1469. valuesArr.splice(j--, 1);
  1470. }
  1471. }
  1472. let updateEle = {code: datas[i].code, value: datas[i].content};
  1473. valuesArr.splice(index, 0, updateEle);
  1474. }
  1475. }
  1476. if(datas[i].type !== 'UpdateT' && datas[i].field === 'code'){
  1477. if(valuesArr){
  1478. for(let j=0; j< valuesArr.length; j++){
  1479. if(datas[i].orgId === valuesArr[j].code){
  1480. valuesArr[j].code = datas[i].data;
  1481. }
  1482. }
  1483. }
  1484. }
  1485. if(datas[i].type !== 'UpdateT' && datas[i].field === 'value'){
  1486. if(valuesArr){
  1487. for(let j=0; j< valuesArr.length; j++){
  1488. if(datas[i].orgId === valuesArr[j].code){
  1489. valuesArr[j].value = datas[i].data
  1490. }
  1491. }
  1492. }
  1493. }
  1494. if(datas[i].type === 'CreateT'){
  1495. let newValueData = {value: datas[i].content, code: datas[i].code};
  1496. valuesArr.push(newValueData);
  1497. }
  1498. if(datas[i].type !== 'CreateT' && datas[i].field === 'value'){
  1499. let newValueData = {value: datas[i].data, code: datas[i].code};
  1500. valuesArr.push(newValueData);
  1501. }
  1502. if(datas[i].type !== 'CreateT' && datas[i].field === 'code'){
  1503. let newValueData = {value: '', code: datas[i].data};
  1504. valuesArr.push(newValueData);
  1505. }
  1506. }
  1507. }
  1508. tools.reshowValue(sheet, valuesArr, eigenValueSetting, true);
  1509. }
  1510. };
  1511. var jobsController = {
  1512. editData: function(controller, sheet, totalJobs, setting){
  1513. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  1514. orgJobData = sheet.getCell(args.row, args.col).value();
  1515. });
  1516. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args) {
  1517. var field, newData = args.editingText, id = sheet.getTag(args.row, args.col);
  1518. setting.cols.forEach(function (col, idx) {
  1519. if (args.col === idx) {
  1520. field = col.data.field;
  1521. }
  1522. });
  1523. if (controller.tree.selected) {
  1524. var isExist = tools.isExist(totalJobs.jobsArr, field, newData, orgJobData);
  1525. var isRepeat = tools.isRepeat(controller.tree.selected.jobs, field ,newData, 'reference', 'job');
  1526. //create
  1527. if(!id && newData && !isRepeat){
  1528. if(isExist){
  1529. jobsController.createExist(sheet, controller, totalJobs, field, newData, args, setting);
  1530. }
  1531. else {
  1532. jobsController.createNew(sheet, controller, totalJobs, field, newData, args, setting);
  1533. }
  1534. }
  1535. //update
  1536. else if(id && newData !== orgJobData && !isRepeat){
  1537. jobsController.update(sheet, controller, totalJobs, field, newData, id, isExist, args, setting);
  1538. }
  1539. //处理重复
  1540. if(isRepeat){
  1541. if(id && newData){
  1542. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgJobData);
  1543. }
  1544. else {
  1545. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  1546. }
  1547. }
  1548. }
  1549. else {
  1550. sheet.getCell(args.row, args.col).value('');
  1551. }
  1552. });
  1553. },
  1554. createNew: function(sheet, controller, totalJobs, field, newData, args, setting){
  1555. if(field === 'content'){
  1556. maxJobsNumber++;
  1557. //update--
  1558. let serialNo = tools.getSerialNo(controller.tree.selected.jobs);
  1559. console.log(`serialNo: ${serialNo}`);
  1560. //--
  1561. console.log(`billsLibId: ${billsLibId} billsId: ${controller.tree.selected.getID()} content: ${newData} code: ${maxJobsNumber}`);
  1562. jobsAjax.edCreateJob(billsLibId, controller.tree.selected.getID(), newData, maxJobsNumber, serialNo, function(newJobId){
  1563. var newJobData, newJob;
  1564. newJobData = {id: newJobId, content: newData, code: maxJobsNumber};
  1565. newJob = createObj.newJob(newJobData);
  1566. newJob.count = 1;
  1567. totalJobs.jobs[totalJobs.prefix + newJobId] = newJob;
  1568. totalJobs.jobsArr.push(newJob);
  1569. controller.tree.selected.jobs.push({job: newJob, serialNo: serialNo});//update--
  1570. //tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);//update--
  1571. tools.orderReshowData(sheet, controller.tree.selected.jobs, setting, 'job', true)//update--
  1572. });
  1573. }
  1574. else {
  1575. sheet.getCell(args.row, args.col).value('');
  1576. }
  1577. },
  1578. createExist: function(sheet, controller, totalJobs, field, newData, args, setting){
  1579. totalJobs.jobsArr.forEach(function(job){
  1580. if(field === 'content'&& newData === job.data.content){
  1581. let serialNo = tools.getSerialNo(controller.tree.selected.jobs);//update--
  1582. //billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', job.data.id);//update--
  1583. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', {id: job.data.id, serialNo: serialNo});//update--
  1584. job.count++;
  1585. // controller.tree.selected.jobs.push(job);//update--
  1586. controller.tree.selected.jobs.push({job: job, serialNo: serialNo});//update--
  1587. //tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);//update--
  1588. tools.orderReshowData(sheet,controller.tree.selected.jobs, setting, 'job', true);
  1589. }
  1590. else if(field == 'code' && newData == job.data.code){
  1591. let serialNo = tools.getSerialNo(controller.tree.selected.jobs);//update--
  1592. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'jobs', {id: job.data.id, serialNo: serialNo});//update--
  1593. job.count++;
  1594. controller.tree.selected.jobs.push({job: job, serialNo: serialNo});//update--
  1595. // tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);//update--
  1596. tools.orderReshowData(sheet, controller.tree.selected.jobs, setting, 'job', true);
  1597. }
  1598. });
  1599. },
  1600. update: function(sheet, controller, totalJobs, field, newData, id, isExist, args, setting){
  1601. if(isExist){
  1602. totalJobs.jobsArr.forEach(function(job){
  1603. if(field == 'code' && job.data[field] == newData){
  1604. let serialNo = tools.getObj(controller.tree.selected.jobs, id, 'job').serialNo;//update--add
  1605. let index = tools.getIndex(controller.tree.selected.jobs, id, 'job');//update-- getIndex
  1606. job.count++;
  1607. controller.tree.selected.jobs.splice(index, 1);
  1608. //controller.tree.selected.jobs.splice(index, 0, job);//update--
  1609. controller.tree.selected.jobs.splice(index, 0, {job: job, serialNo: serialNo});//update--
  1610. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, job.data.id, 'update', 'jobs');
  1611. //tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);//update--
  1612. tools.orderReshowData(sheet, controller.tree.selected.jobs, setting, 'job', true);
  1613. }
  1614. if(field === 'content' && job.data[field] === newData){
  1615. let serialNo = tools.getObj(controller.tree.selected.jobs, id, 'job').serialNo;//update--add
  1616. let index = tools.getIndex(controller.tree.selected.jobs, id, 'job');
  1617. job.count++;
  1618. controller.tree.selected.jobs.splice(index, 1);
  1619. controller.tree.selected.jobs.splice(index, 0, {job: job, serialNo: serialNo});//update--
  1620. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, job.data.id, 'update', 'jobs');
  1621. // tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);//update--
  1622. tools.orderReshowData(sheet,controller.tree.selected.jobs, setting, 'job', true);
  1623. }
  1624. });
  1625. }
  1626. else{
  1627. if(field === 'content'){
  1628. maxJobsNumber++;
  1629. //let serialNo = tools.getSerialNo(controller.tree.selected.jobs);//update--
  1630. let serialNo = tools.getObj(controller.tree.selected.jobs, id, 'job').serialNo;
  1631. console.log(`upNe: serialNo: ${serialNo}`);
  1632. jobsAjax.edUpdateJob(billsLibId, controller.tree.selected.getID(), newData, maxJobsNumber, id, function(newJobId){//update--
  1633. var newJobData, newJob;
  1634. newJobData = {id: newJobId, content: newData, code: maxJobsNumber};
  1635. newJob = createObj.newJob(newJobData);
  1636. newJob.count = 1;
  1637. totalJobs.jobs[totalJobs.prefix + newJobId] = newJob;
  1638. totalJobs.jobsArr.push(newJob);
  1639. var index = tools.getIndex(controller.tree.selected.jobs, id, 'job');
  1640. controller.tree.selected.jobs.splice(index, 1);
  1641. controller.tree.selected.jobs.splice(index, 0, {job: newJob, serialNo: serialNo});//update--
  1642. //tools.reshowData(sheet, controller.tree.selected.jobs, setting, true);//update--
  1643. tools.orderReshowData(sheet, controller.tree.selected.jobs, setting, 'job', true);//update--
  1644. });
  1645. }
  1646. else {
  1647. //todo: 提示窗口
  1648. if(typeof newData === 'number'){
  1649. sheet.getCell(args.row, args.col).value(orgJobData);
  1650. }
  1651. else {
  1652. //编号只能是数字
  1653. sheet.getCell(args.row, args.col).value('');
  1654. }
  1655. }
  1656. }
  1657. },
  1658. };
  1659. var itemsController = {
  1660. editData: function(controller, sheet, totalItems, setting){
  1661. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  1662. orgItemData = sheet.getCell(args.row, args.col).value();
  1663. });
  1664. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args) {
  1665. var field, newData = args.editingText, id = sheet.getTag(args.row, args.col);
  1666. setting.cols.forEach(function (col, idx) {
  1667. if (args.col === idx) {
  1668. field = col.data.field;
  1669. }
  1670. });
  1671. if (controller.tree.selected) {
  1672. var isExist = tools.isExist(totalItems.itemsArr, field, newData, orgItemData);
  1673. var isRepeat = tools.isRepeat(controller.tree.selected.items, field ,newData, 'reference', 'item');
  1674. //create
  1675. if(!id && newData && !isRepeat){
  1676. if(isExist){
  1677. itemsController.createExist(sheet, controller, totalItems, field, newData, args, setting);
  1678. }
  1679. else {
  1680. itemsController.createNew(sheet, controller, totalItems, field, newData, args, setting);
  1681. }
  1682. }
  1683. //update
  1684. else if(id && newData !== orgItemData && !isRepeat){
  1685. itemsController.update(sheet, controller, totalItems, field, newData, id, isExist, args, setting);
  1686. }
  1687. //处理重复
  1688. if(isRepeat){
  1689. //todo:redirect focus
  1690. if(id && newData){
  1691. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgItemData);
  1692. }
  1693. else {
  1694. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  1695. }
  1696. }
  1697. }
  1698. else {
  1699. sheet.getCell(args.row, args.col).value('');
  1700. }
  1701. });
  1702. },
  1703. createNew: function(sheet, controller, totalItems, field, newData, args, setting){
  1704. if(field === 'content'){
  1705. maxItemsNumber++;
  1706. let serialNo = tools.getSerialNo(controller.tree.selected.items);
  1707. console.log(`billsLibId: ${billsLibId} billsId: ${controller.tree.selected.getID()} content: ${newData} code: ${maxItemsNumber}`);
  1708. itemsAjax.edCreateItem(billsLibId, controller.tree.selected.getID(), newData, maxItemsNumber, serialNo, function(newItemId){
  1709. let newItemData, newItem;
  1710. newItemData = {id: newItemId, content: newData, code: maxItemsNumber};
  1711. newItem = createObj.newItem(newItemData);
  1712. newItem.count = 1;
  1713. totalItems.items[totalItems.prefix + newItemId] = newItem;
  1714. totalItems.itemsArr.push(newItem);
  1715. controller.tree.selected.items.push({item: newItem, serialNo: serialNo});
  1716. //tools.reshowData(sheet, controller.tree.selected.items, setting, true);
  1717. tools.orderReshowData(sheet, controller.tree.selected.items, setting, 'item', true);
  1718. });
  1719. }
  1720. else {
  1721. if(typeof newData === 'number'){
  1722. sheet.getCell(args.row, args.col).value('');
  1723. }
  1724. else {
  1725. //编号只能为数字
  1726. sheet.getCell(args.row, args.col).value('');
  1727. }
  1728. }
  1729. },
  1730. createExist: function(sheet, controller, totalItems, field, newData, args, setting){
  1731. totalItems.itemsArr.forEach(function(item){
  1732. if(field === 'content'&& newData === item.data.content){
  1733. let serialNo = tools.getSerialNo(controller.tree.selected.items);
  1734. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', {id: item.data.id, serialNo: serialNo});
  1735. item.count++;
  1736. controller.tree.selected.items.push({item: item, serialNo: serialNo});
  1737. tools.orderReshowData(sheet, controller.tree.selected.items, setting, 'item', true);
  1738. }
  1739. else if(field == 'code' && newData == item.data.code){
  1740. let serialNo = tools.getSerialNo(controller.tree.selected.items);
  1741. billsAjax.updateBills(billsLibId, controller.tree.selected.getID(), 'items', {id: item.data.id, serialNo: serialNo});
  1742. item.count++;
  1743. controller.tree.selected.items.push({item: item, serialNo: serialNo});
  1744. tools.orderReshowData(sheet, controller.tree.selected.items, setting, 'item', true);
  1745. }
  1746. });
  1747. },
  1748. update: function(sheet, controller, totalItems, field, newData, id, isExist, args, setting){
  1749. if(isExist){
  1750. totalItems.itemsArr.forEach(function(item){
  1751. if(field == 'code' && item.data[field] == newData){
  1752. let serialNo = tools.getObj(controller.tree.selected.items, id, 'item').serialNo;
  1753. let index = tools.getIndex(controller.tree.selected.items, id, 'item');
  1754. item.count++;
  1755. controller.tree.selected.items.splice(index, 1);
  1756. controller.tree.selected.items.splice(index, 0, {item: item, serialNo: serialNo});
  1757. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, item.data.id, 'update', 'items');
  1758. tools.orderReshowData(sheet, controller.tree.selected.items, setting, 'item', true);
  1759. }
  1760. if(field === 'content' && item.data[field] === newData){
  1761. let serialNo = tools.getObj(controller.tree.selected.items, id, 'item').serialNo;
  1762. let index = tools.getIndex(controller.tree.selected.items, id, 'item');
  1763. item.count++;
  1764. controller.tree.selected.items.splice(index, 1);
  1765. controller.tree.selected.items.splice(index, 0, {item: item, serialNo: serialNo});
  1766. billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), id, item.data.id, 'update', 'items');
  1767. tools.orderReshowData(sheet, controller.tree.selected.items, setting, 'item', true);
  1768. }
  1769. });
  1770. }
  1771. else{
  1772. if(field === 'content'){
  1773. maxItemsNumber++;
  1774. let serialNo = tools.getObj(controller.tree.selected.items, id, 'item').serialNo;
  1775. itemsAjax.edUpdateItem(billsLibId, controller.tree.selected.getID(), newData, maxItemsNumber, id, function(newItemId){
  1776. var newItemData, newItem;
  1777. newItemData = {id: newItemId, content: newData, code: maxItemsNumber};
  1778. newItem = createObj.newItem(newItemData);
  1779. newItem.count = 1;
  1780. totalItems.items[totalItems.prefix + newItemId] = newItem;
  1781. totalItems.itemsArr.push(newItem);
  1782. var index = tools.getIndex(controller.tree.selected.items, id, 'item');
  1783. controller.tree.selected.items.splice(index, 1);
  1784. controller.tree.selected.items.splice(index, 0, {item: newItem, serialNo: serialNo});
  1785. tools.orderReshowData(sheet, controller.tree.selected.items, setting, 'item', true);
  1786. });
  1787. }
  1788. else {
  1789. if(typeof newData === 'number'){
  1790. sheet.getCell(args.row, args.col).value(orgItemData);
  1791. }
  1792. else {
  1793. //编号只能为数字
  1794. sheet.getCell(args.row, args.col).value('');
  1795. }
  1796. }
  1797. }
  1798. },
  1799. };
  1800. var valueController = {
  1801. editData: function(totalItems, sheet, setting){
  1802. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  1803. orgValue = sheet.getValue(args.row, args.col);
  1804. });
  1805. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  1806. var newValue = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  1807. setting.cols.forEach(function(col, colIdx){
  1808. if(args.col === colIdx){
  1809. field = col.data.field;
  1810. }
  1811. });
  1812. if(selectedId){
  1813. isRepeat = tools.isRepeat(valueController.getValues(totalItems, selectedId), field, newValue, 'document');
  1814. if(!tagId && !isRepeat && newValue){//create
  1815. valueController.createValue(sheet, totalItems, selectedId, field, newValue);
  1816. }
  1817. else if(tagId && !isRepeat && newValue !== orgValue){//update
  1818. valueController.updateValue(totalItems, tagId, newValue, field);
  1819. }
  1820. if(isRepeat){
  1821. if(tagId && newValue){
  1822. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgValue);
  1823. }
  1824. else {
  1825. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  1826. }
  1827. }
  1828. }
  1829. });
  1830. },
  1831. createValue: function(sheet, totalItems, id, field, newValue){
  1832. var newData;
  1833. if(field === 'value'){
  1834. var newCode = valueController.getCode(totalItems, id);
  1835. newData = {code: newCode, value: newValue};
  1836. tools.reSetCell(sheet, sheet.getActiveRowIndex(), 0, newCode, newCode);
  1837. }
  1838. else {
  1839. if(typeof newValue === 'number'){
  1840. newData = {code: newValue, value: ''};
  1841. tools.reSetCell(sheet, sheet.getActiveRowIndex(), null, null, newValue);
  1842. }
  1843. else {
  1844. //编号只能为数字!
  1845. sheet.getCell(args.row, args.col).value('');
  1846. }
  1847. }
  1848. console.log(`finItem`);
  1849. console.log(totalItems.findItem(id).data);
  1850. totalItems.findItem(id).data.itemValue.push(newData);
  1851. itemsAjax.updateValue(billsLibId, id, newData, null, 'create');
  1852. },
  1853. updateValue: function(totalItems, tagId, newData, field){
  1854. var updateData = {code: tagId, newData: newData, field: field};
  1855. var itemVals = totalItems.findItem(selectedId).data.itemValue;
  1856. if(field === 'value'){
  1857. var updateEle = {code: tagId, value: newData};
  1858. if(itemVals){
  1859. var index;
  1860. itemVals.forEach(function(val){
  1861. if(val.code === tagId){
  1862. index = itemVals.indexOf(val);
  1863. itemVals.splice(index, 1);
  1864. }
  1865. });
  1866. itemVals.splice(index, 0, updateEle);
  1867. }
  1868. itemsAjax.updateValue(billsLibId, selectedId, updateData, null, 'update');
  1869. }
  1870. else {
  1871. if(typeof newData === 'number'){
  1872. if(itemVals){
  1873. var updateEle;
  1874. var index;
  1875. itemVals.forEach(function(val){
  1876. if(val.code === tagId){
  1877. updateEle = {code: newData, value: val.value};
  1878. index = itemVals.indexOf(val);
  1879. itemVals.splice(index, 1);
  1880. }
  1881. });
  1882. itemVals.splice(index, 0, updateEle);
  1883. }
  1884. itemsAjax.updateValue(billsLibId, selectedId, updateData, null, 'update');
  1885. }
  1886. else {
  1887. //编号只能为数字!
  1888. sheet.getCell(args.row, args.col).value('');
  1889. }
  1890. }
  1891. },
  1892. getValues: function(totalItems, id){
  1893. return totalItems.findItem(id).data.itemValue;
  1894. },
  1895. getTopItem: function(totalItems){
  1896. tools.resort(totalItems.itemsArr, 'code', false);
  1897. return totalItems.itemsArr.length > 0 ? totalItems.itemsArr[0] : null
  1898. },
  1899. getCode: function(totalItems, id){
  1900. var valArr = totalItems.findItem(id).data.itemValue;
  1901. if(valArr){
  1902. tools.resort(valArr, 'code', true);
  1903. return valArr.length > 0 ? valArr[valArr.length - 1].code + 1 : 1;
  1904. }else {
  1905. return 1;
  1906. }
  1907. },
  1908. isRepeat: function(arr, newValue){
  1909. var isRepeat = false;
  1910. arr.forEach(function(itemValue){
  1911. if(itemValue.value === newValue){
  1912. isRepeat = true;
  1913. }
  1914. });
  1915. return isRepeat;
  1916. }
  1917. };
  1918. var totalJobsController = {
  1919. eiditData: function(totalJobs, sheet, setting){
  1920. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  1921. orgJobData = sheet.getCell(args.row, args.col).value();
  1922. });
  1923. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  1924. var newData = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  1925. setting.cols.forEach(function(col, colIdx){
  1926. if(args.col === colIdx){
  1927. field = col.data.field;
  1928. }
  1929. });
  1930. isRepeat = tools.isRepeat(totalJobs.jobsArr, field, newData, 'reference', null);
  1931. if(!tagId && !isRepeat && newData){//create
  1932. totalJobsController.createJob(sheet, totalJobs, field, newData, args);
  1933. }
  1934. else if(tagId && !isRepeat && newData !== orgJobData){//update
  1935. totalJobsController.updateJob(totalJobs, tagId, field, newData);
  1936. }
  1937. if(isRepeat){
  1938. if(tagId && newData){
  1939. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgJobData);
  1940. }
  1941. else {
  1942. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  1943. }
  1944. }
  1945. });
  1946. },
  1947. createJob: function(sheet, totalJobs, field, newData, args){
  1948. if(field === 'content'){
  1949. maxJobsNumber++;
  1950. let code = maxJobsNumber;
  1951. jobsAjax.createJobContent(billsLibId, newData, code, function(newJobId){
  1952. let newJobData, newJob;
  1953. newJobData = {id: newJobId, content: newData, code: code};
  1954. newJob = createObj.newJob(newJobData);
  1955. totalJobs.jobsArr.push(newJob);
  1956. tools.reSetCell(sheet, args.row, 0, code, newJobId);
  1957. setSheet.setMaxRowCount(sheet, totalJobs.jobsArr);
  1958. });
  1959. }
  1960. else {
  1961. if(typeof newData === 'number'){
  1962. maxJobsNumber = maxJobsNumber >= newData ? maxJobsNumber : newData;
  1963. jobsAjax.createJobContent(billsLibId, '', newData, function(newJobId){
  1964. let newJobData, newJob;
  1965. newJobData = {id: newJobId, content: '', code: newData};
  1966. newJob = createObj.newJob(newJobData);
  1967. totalJobs.jobsArr.push(newJob);
  1968. tools.reSetCell(sheet, args.row, 0, null, newJobId);
  1969. setSheet.setMaxRowCount(sheet, totalJobs.jobsArr);
  1970. });
  1971. }
  1972. else {
  1973. //编号只能为数字!
  1974. sheet.getCell(args.row, args.col).value("");
  1975. }
  1976. }
  1977. },
  1978. updateJob: function(totalJobs, id, field, newData){
  1979. if(field === 'code' && typeof newData !== 'number'){
  1980. //编号只能为数字
  1981. }
  1982. else {
  1983. console.log(`enterupdate id: ${id}`);
  1984. totalJobs.jobsArr.forEach(function(job){
  1985. if(job.data.id === id){
  1986. job.data[field] = newData;
  1987. }
  1988. });
  1989. jobsAjax.updateJobContent(billsLibId, id, field, newData);
  1990. }
  1991. }
  1992. };
  1993. var totalItemsController = {
  1994. eiditData: function(totalItems, sheet, setting){
  1995. sheet.bind(GC.Spread.Sheets.Events.EnterCell, function(sender, args){
  1996. orgItemData = sheet.getCell(args.row, args.col).value();
  1997. });
  1998. sheet.bind(GC.Spread.Sheets.Events.EditEnded, function(sender, args){
  1999. var newData = sheet.getValue(args.row, args.col), tagId = sheet.getTag(args.row, args.col), field, isRepeat;
  2000. setting.cols.forEach(function(col, colIdx){
  2001. if(args.col === colIdx){
  2002. field = col.data.field;
  2003. }
  2004. });
  2005. isRepeat = tools.isRepeat(totalItems.itemsArr, field, newData, 'reference', null);
  2006. if(!tagId && !isRepeat && newData){//create
  2007. totalItemsController.createItem(sheet, totalItems, field, newData, args);
  2008. }
  2009. else if(tagId && !isRepeat && newData !== orgItemData){//update
  2010. totalItemsController.updateItem(totalItems, tagId, field, newData);
  2011. }
  2012. if(isRepeat){
  2013. if(tagId && newData){
  2014. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value(orgItemData);
  2015. }
  2016. else {
  2017. sheet.getCell(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport).value('');
  2018. }
  2019. }
  2020. });
  2021. },
  2022. createItem: function(sheet, totalItems, field, newData, args){
  2023. if(field === 'content'){
  2024. maxItemsNumber++;
  2025. let code = maxItemsNumber;
  2026. itemsAjax.createItemCharacter(billsLibId, newData, code, function(newItemId){
  2027. var newItemData, newItem;
  2028. newItemData = {id: newItemId, content: newData, code: code, itemValue: []};
  2029. newItem = createObj.newItem(newItemData);
  2030. totalItems.itemsArr.push(newItem);
  2031. totalItems.items[totalItems.prefix + newItemId] = newItem;
  2032. tools.reSetCell(sheet, args.row, 0, code, newItemId);
  2033. setSheet.setMaxRowCount(sheet, totalItems.itemsArr);
  2034. });
  2035. }
  2036. else {
  2037. if(typeof newData === 'number'){
  2038. maxItemsNumber = maxItemsNumber >= newData ? maxItemsNumber : newData;
  2039. itemsAjax.createItemCharacter(billsLibId, '', newData, function(newItemId){
  2040. var newItemData, newItem;
  2041. newItemData = {id: newItemId, content: '', code: newData, itemValue: []};
  2042. newItem = createObj.newItem(newItemData);
  2043. totalItems.itemsArr.push(newItem);
  2044. totalItems.items[totalItems.prefix + newItemId] = newItem;
  2045. tools.reSetCell(sheet, args.row, 0, null, newItemId);
  2046. setSheet.setMaxRowCount(sheet, totalItems.itemsArr);
  2047. });
  2048. }
  2049. else {
  2050. //编号只能为数字
  2051. sheet.getCell(args.row, args.col).value("");
  2052. }
  2053. }
  2054. },
  2055. updateItem: function(totalItems, id, field, newData){
  2056. if(field === 'code' && typeof newData !== 'number'){
  2057. //编号只能为数字
  2058. sheet.getCell(args.row, args.col).value('');
  2059. }
  2060. else {
  2061. totalItems.itemsArr.forEach(function(item){
  2062. if(item.data.id === id){
  2063. item.data[field] = newData;
  2064. }
  2065. });
  2066. itemsAjax.updateItemCharacter(billsLibId, id, field, newData);
  2067. }
  2068. }
  2069. };
  2070. var rechargeController = {
  2071. updateRechar: function(selectedNode, newData){
  2072. if(selectedNode){
  2073. var updateId = selectedNode.getID();
  2074. selectedNode.data.recharge = newData;
  2075. /*var getupdateIds = function(node){
  2076. updateIds.push(node.getID());
  2077. node.data.recharge = newData;
  2078. if(node.children.length > 0){
  2079. node.children.forEach(function(node){
  2080. getupdateIds(node);
  2081. });
  2082. }
  2083. }
  2084. getupdateIds(tools.getRoot(selectedNode));*/
  2085. billsAjax.updateRecharge(billsLibId, updateId, newData);
  2086. }
  2087. else {
  2088. $('exampleTextarea').val('');
  2089. }
  2090. },
  2091. createRecharge: function(node){
  2092. if(node){
  2093. var tempId = [];
  2094. tempId.push(node.getID());
  2095. var rootNode = tools.getRoot(node);
  2096. if(rootNode.data.recharge && rootNode !== node){
  2097. billsAjax.updateRecharge(billsLibId, tempId, rootNode.data.recharge);
  2098. node.data.recharge = rootNode.data.recharge;
  2099. $('#exampleTextarea').val(node.data.recharge);
  2100. }
  2101. else {
  2102. node.data.recharge = '';
  2103. $('#exampleTextarea').val('');
  2104. }
  2105. }
  2106. else {
  2107. $('exampleTextarea').val('');
  2108. }
  2109. }
  2110. };