installation.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /**
  2. * Created by Zhong on 2018/1/19.
  3. **/
  4. const locked = lockUtil.getLocked();
  5. $(document).ready(function () {
  6. feeItemObj.buildSheet();
  7. const lockedSpreads = [
  8. feeItemObj.workBook,
  9. sectionObj.workBook,
  10. feeRuleObj.workBook
  11. ];
  12. lockUtil.lockSpreads(lockedSpreads, locked);
  13. $('#sectionTreeModal').on('shown.bs.modal', function (e) {
  14. batchSectionObj.workBook.refresh();
  15. //bind confirm btn
  16. batchSectionObj.onConfirmBtn();
  17. });
  18. $('#sectionTreeModal').on('hidden.bs.modal', function () {
  19. batchSectionObj.clearChecked();
  20. });
  21. $('#delConfirm').click(function () {
  22. if(curDeleteType === feeItemObj.deleteType){
  23. let me = feeItemObj, se = sectionObj, fr = feeRuleObj;
  24. let fiPostData = [], sePostData = [];
  25. let sels = me.workBook.getSheet(0).getSelections();
  26. for(let i = 0, len = sels.length; i < len; i++){
  27. let sel = sels[i];
  28. //delete
  29. if(sel.colCount === me.setting.header.length){
  30. for(let j = 0, jLen = sel.rowCount; j < jLen; j++){
  31. let row = sel.row + j;
  32. let feeItem = me.cache[row];
  33. //有数据,删除数据
  34. if(me.isDef(feeItem)){
  35. fiPostData.push({updateType: me.updateType.update, updateData: {ID: feeItem.ID, deleted: true}});
  36. //delete feeItem section
  37. for(let section of feeItem.section){
  38. sePostData.push({updateType: me.updateType.update, updateData: {ID: section.ID, deleted: true}});
  39. }
  40. }
  41. }
  42. //front delete
  43. me.cache.splice(sel.row, sel.rowCount);
  44. }
  45. }
  46. //update currentFeeItem
  47. me.currentFeeItem = me.getCurrentData(me.sheet, me.cache);
  48. se.cache = me.isDef(me.currentFeeItem) ? me.currentFeeItem.section : [];
  49. if(fiPostData.length > 0){
  50. me.updateFeeItem(fiPostData, function () {
  51. me.sheet.setRowCount(me.cache.length + 10);
  52. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  53. $('#delAlert').modal('hide');
  54. });
  55. if(sePostData.length > 0){
  56. se.updateSection(sePostData);
  57. }
  58. sheetCommonObj.cleanData(se.sheet, se.setting, 10);
  59. sheetCommonObj.cleanData(fr.sheet, fr.setting, 10);
  60. }
  61. else {
  62. $('#delAlert').modal('hide');
  63. }
  64. }
  65. else if(curDeleteType === sectionObj.deleteType){
  66. let me = sectionObj, fi = feeItemObj, fr = feeRuleObj;
  67. let sePostData = [], fiPostData = [];
  68. let sels = me.workBook.getSheet(0).getSelections();
  69. for(let i = 0, len = sels.length; i < len; i++){
  70. let sel = sels[i];
  71. //delete
  72. if(sel.colCount === me.setting.header.length){
  73. for(let j = 0, jLen = sel.rowCount; j < jLen; j++){
  74. let row = sel.row + j;
  75. let section = me.cache[row];
  76. //有数据,删除数据
  77. if(me.isDef(section)){
  78. sePostData.push({updateType: me.updateType.update, updateData: {ID: section.ID, deleted: true}});
  79. fiPostData.push({updateType: me.updateType.update, updateData: {ID: fi.currentFeeItem.ID, $pull: {section: {ID: section.ID}}}});
  80. }
  81. }
  82. //front delete
  83. me.cache.splice(sel.row, sel.rowCount);
  84. }
  85. }
  86. //update currentSection
  87. me.currentSection = fi.getCurrentData(me.sheet, me.cache);
  88. fr.cache = me.isDef(me.currentSection) ? me.currentSection.feeRule : [];
  89. if(sePostData.length > 0){
  90. me.updateSection(sePostData, function () {
  91. me.sheet.setRowCount(me.cache.length + 5);
  92. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  93. $('#delAlert').modal('hide');
  94. });
  95. if(fiPostData.length > 0){
  96. fi.updateFeeItem(fiPostData);
  97. }
  98. sheetCommonObj.cleanData(fr.sheet, fr.setting, 10);
  99. }
  100. else{
  101. $('#delAlert').modal('hide');
  102. }
  103. }
  104. });
  105. });
  106. let curDeleteType = null;
  107. //费用项
  108. let feeItemObj = {
  109. rationRepId: null,
  110. workBook: null,
  111. sheet: null,
  112. cache: [],
  113. currentFeeItem: null,
  114. feeType: {ZM: '子目费用', FX: '分项费用', CS: '措施费用'},
  115. updateType: {update: 'update', new: 'new'},
  116. deleteType: 'feeItem',
  117. setting: {
  118. header:[
  119. {headerName:"费用项",headerWidth:200,dataCode:"feeItem", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  120. {headerName:"费用类型",headerWidth:80,dataCode:"feeType", dataType: "String", hAlign: "center", vAlign: "center"},
  121. {headerName:"记取位置",headerWidth:100,dataCode:"position", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"}
  122. ],
  123. view: {lockColumns: []},
  124. comboItems: {feeType: ['子目费用', '分项费用', '措施费用']},
  125. options: {
  126. tabStripVisible: false,
  127. allowCopyPasteExcelStyle : false,
  128. allowExtendPasteRange: true,
  129. allowUserDragDrop : false,
  130. allowUserDragFill: false,
  131. scrollbarMaxAlign : true
  132. }
  133. },
  134. isDef: function (v) {
  135. return v !== undefined && v !== null;
  136. },
  137. //sheet things
  138. setOptions: function (workbook, opts) {
  139. for(let opt in opts){
  140. workbook.options[opt] = opts[opt];
  141. }
  142. },
  143. setCombo: function (sheet, col, comboItems) {
  144. let combo = sheetCommonObj.getDynamicCombo();
  145. combo.items(comboItems).editable = false;
  146. sheet.getRange(-1, col, -1, 1).cellType(combo);
  147. },
  148. getCurrentData: function (sheet, cache) {
  149. let acRow = sheet.getActiveRowIndex();
  150. return this.isDef(cache[acRow]) ? cache[acRow] : null;
  151. },
  152. buildSheet: function () {
  153. let me = this, se = sectionObj, fr = feeRuleObj, bs = batchSectionObj;
  154. if(!this.isDef(this.workBook)){
  155. this.workBook = sheetCommonObj.buildSheet($('#feeItemSpread')[0], this.setting, 10);
  156. this.sheet = this.workBook.getActiveSheet();
  157. sheetCommonObj.bindEscKey(this.workBook, [{sheet: this.sheet, editStarting: me.onEditStarting, editEnded: me.onEditEnded}]);
  158. this.setOptions(this.workBook, this.setting.options);
  159. this.sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  160. this.bindEvents(this.sheet);
  161. this.rationRepId = parseInt(getQueryString("repository"));
  162. //init combo
  163. this.setCombo(this.sheet, 1, this.setting.comboItems.feeType);
  164. //init sectionSpread
  165. se.buildSheet();
  166. //init feeRuleSpread
  167. fr.buildSheet();
  168. //init installation
  169. this.getInstallation(this.rationRepId, function (rstData) {
  170. me.cache = rstData;
  171. me.sheet.setRowCount(me.cache.length + 10);
  172. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  173. me.initSelection(me.cache[0]);
  174. });
  175. //init batchSectionSpread
  176. bs.getSectionTree(this.rationRepId);
  177. }
  178. },
  179. bindEvents: function (sheet) {
  180. let me = this;
  181. const Events = GC.Spread.Sheets.Events;
  182. sheet.bind(Events.SelectionChanged, me.onSelectionChanged);
  183. sheet.bind(Events.EnterCell, me.onEnterCell);
  184. sheet.bind(Events.EditStarting, me.onEditStarting);
  185. sheet.bind(Events.EditEnded, me.onEditEnded);
  186. sheet.bind(Events.ClipboardPasting, me.onClipboardPasting);
  187. sheet.bind(Events.ClipboardPasted, me.onClipboardPasted);
  188. me.feeItemDelOpr();
  189. },
  190. initSelection: function (feeItem) {
  191. let me = this, se = sectionObj, fr = feeRuleObj;
  192. sheetCommonObj.cleanSheet(se.sheet, se.setting, -1);
  193. sheetCommonObj.cleanSheet(fr.sheet, fr.setting, -1);
  194. if(!me.isDef(feeItem)){
  195. me.workBook.focus(true);
  196. me.currentFeeItem = null;
  197. return;
  198. }
  199. fr.addObj = null;
  200. fr.updateObj = null;
  201. me.currentFeeItem = feeItem;
  202. se.cache = me.currentFeeItem.section;
  203. se.sheet.setRowCount(se.cache.length + 5);
  204. sheetCommonObj.showData(se.sheet, se.setting, se.cache);
  205. se.initSelection(se.cache[0]);
  206. me.workBook.focus(true);
  207. },
  208. onSelectionChanged: function (sender, info) {
  209. let me = feeItemObj;
  210. if(info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row){
  211. let row = info.newSelections[0].row;
  212. let node = me.cache[row];
  213. me.initSelection(node);
  214. }
  215. },
  216. onEnterCell: function (sender, args) {
  217. let me = feeItemObj;
  218. me.sheet.repaint();
  219. },
  220. onEditStarting: function (sender, args) {
  221. let me = feeItemObj;
  222. //费用类型为子目时,记取位置不可编辑
  223. let feeItem = me.cache[args.row];
  224. if(me.isDef(feeItem) && (feeItem.feeType === me.feeType.ZM || !me.isDef(feeItem.feeType) || feeItem.feeType == '') && args.col === 2){
  225. args.cancel = true;
  226. }
  227. },
  228. onEditEnded: function (sender, args) {
  229. let me = feeItemObj, se = sectionObj;
  230. let v = me.isDef(args.editingText) ? args.editingText.toString().trim() : '';
  231. let feeItem = me.cache[args.row];
  232. let updateObj = {updateType: '', updateData: {}};
  233. //update
  234. if(me.isDef(feeItem)){
  235. feeItem[me.setting.header[args.col]['dataCode']] = !me.isDef(feeItem[me.setting.header[args.col]['dataCode']]) ? '' : feeItem[me.setting.header[args.col]['dataCode']];
  236. if(v == feeItem[me.setting.header[args.col]['dataCode']]){
  237. return;
  238. }
  239. updateObj.updateType = me.updateType.update;
  240. //当费用类型切换为子目费用时,自动清空记取位置
  241. if(args.col === 1 && v === me.feeType.ZM){
  242. feeItem.position = '';
  243. args.sheet.setValue(args.row, 2, '');
  244. updateObj.updateData.position = '';
  245. }
  246. feeItem[me.setting.header[args.col]['dataCode']] = v;
  247. updateObj.updateData.ID = feeItem.ID;
  248. updateObj.updateData[me.setting.header[args.col]['dataCode']] = v;
  249. }
  250. //insert
  251. else {
  252. if(v === ''){
  253. return;
  254. }
  255. updateObj.updateType = me.updateType.new;
  256. updateObj.updateData[me.setting.header[args.col]['dataCode']] = v;
  257. updateObj.updateData.rationRepId = me.rationRepId;
  258. updateObj.updateData.ID = uuid.v1();
  259. updateObj.updateData.section = [];
  260. me.cache.push(updateObj.updateData);
  261. me.currentFeeItem = updateObj.updateData;
  262. se.cache = me.currentFeeItem.section;
  263. }
  264. me.sheet.setRowCount(me.cache.length + 10);
  265. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  266. //ajax
  267. me.updateFeeItem([updateObj]);
  268. },
  269. onClipboardPasting: function (sender, info) {
  270. let me = this;
  271. if(info.cellRange.col + info.cellRange.colCount > me.setting.header.length){
  272. info.cancel = true;
  273. }
  274. },
  275. onClipboardPasted: function (sender, info) {
  276. let me = feeItemObj, se = sectionObj;
  277. let postData = [];
  278. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  279. for(let i = 0, len = items.length; i < len; i++){
  280. let row = info.cellRange.row + i;
  281. let feeItem = me.cache[row];
  282. //update
  283. if(me.isDef(feeItem)){
  284. if(me.validData(items[i])){
  285. for(let attr in items[i]){
  286. feeItem[attr] = items[i][attr];
  287. }
  288. postData.push(me.getUpdateObj(me.updateType.update, feeItem));
  289. }
  290. }
  291. //insert
  292. else{
  293. if(me.validData(items[i])){
  294. items[i].ID = uuid.v1();
  295. items[i].rationRepId = me.rationRepId;
  296. items[i].section = [];
  297. postData.push(me.getUpdateObj(me.updateType.new, items[i]));
  298. me.cache.push(items[i]);
  299. }
  300. }
  301. }
  302. //update currentFeeItem
  303. me.currentFeeItem = me.getCurrentData(me.sheet, me.cache);
  304. se.cache = me.currentFeeItem.section;
  305. me.sheet.setRowCount(me.cache.length + 10);
  306. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  307. if(postData.length > 0){
  308. //ajax then refresh
  309. me.updateFeeItem(postData);
  310. }
  311. },
  312. feeItemDelOpr: function () {
  313. if (locked) {
  314. return;
  315. }
  316. let me = this;
  317. me.workBook.commandManager().register('feeItemDel', function () {
  318. curDeleteType = me.deleteType;
  319. let sels = me.workBook.getSheet(0).getSelections();
  320. for(let i = 0, len = sels.length; i < len; i++){
  321. let sel = sels[i];
  322. //delete
  323. if(sel.colCount === me.setting.header.length){
  324. $('#delAlert').modal('show');
  325. break;
  326. }
  327. }
  328. });
  329. me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  330. me.workBook.commandManager().setShortcutKey('feeItemDel', GC.Spread.Commands.Key.del, false, false, false, false);
  331. },
  332. getUpdateObj: function (updateType, data) {
  333. let rst = {updateType: '', updateData: {}};
  334. rst.updateType = updateType;
  335. for(let attr in data){
  336. rst.updateData[attr] = data[attr];
  337. }
  338. return rst;
  339. },
  340. validData: function (pasteData) {
  341. if(this.isDef(pasteData.feeType)){
  342. if(this.setting.comboItems.feeType.indexOf(pasteData.feeType) < 0){
  343. return false;
  344. }
  345. //费用类型为子目时,不可有记取位置
  346. if(pasteData.feeType === this.feeType.ZM && this.isDef(pasteData.position) && pasteData.position !== ''){
  347. return false;
  348. }
  349. }
  350. return true;
  351. },
  352. getInstallation: function (rationRepId, callback) {
  353. let me = this;
  354. CommonAjax.post('/rationRepository/api/getInstallation', {rationRepId: rationRepId}, function (rstData) {
  355. me.cache = rstData;
  356. if(callback){
  357. callback(rstData);
  358. }
  359. });
  360. },
  361. updateFeeItem: function (updateData, callback) {
  362. let me = this;
  363. CommonAjax.post('/rationRepository/api/updateFeeItem', {updateData: updateData}, function (rstData) {
  364. if(callback){
  365. callback();
  366. }
  367. });
  368. }
  369. };
  370. //分册章节
  371. let sectionObj = {
  372. workBook: null,
  373. sheet: null,
  374. cache: [],
  375. currentSection: null,
  376. updateType: {update: 'update', new: 'new'},
  377. deleteType: 'section',
  378. setting: {
  379. header:[
  380. {headerName:"分册章节",headerWidth:330,dataCode:"name", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"}
  381. ],
  382. view: {lockColumns: []},
  383. options: {
  384. tabStripVisible: false,
  385. allowCopyPasteExcelStyle : false,
  386. allowExtendPasteRange: true,
  387. allowUserDragDrop : false,
  388. allowUserDragFill: false,
  389. scrollbarMaxAlign : true
  390. }
  391. },
  392. isDef: function (v) {
  393. return v !== undefined && v !== null;
  394. },
  395. //sheet things
  396. setOptions: function (workbook, opts) {
  397. for(let opt in opts){
  398. workbook.options[opt] = opts[opt];
  399. }
  400. },
  401. renderFunc: function (sheet, func) {
  402. sheet.suspendPaint();
  403. sheet.suspendEvent();
  404. if(this.isFunc(func)){
  405. func();
  406. }
  407. sheet.resumePaint();
  408. sheet.resumeEvent();
  409. },
  410. buildSheet: function () {
  411. if(!this.isDef(this.workBook)){
  412. this.workBook = sheetCommonObj.buildSheet($('#instSectionSpread')[0], this.setting, 10);
  413. this.sheet = this.workBook.getActiveSheet();
  414. sheetCommonObj.bindEscKey(this.workBook, [{sheet: this.sheet, editStarting: this.onEditStarting, editEnded: this.onEditEnded}]);
  415. this.setOptions(this.workBook, this.setting.options);
  416. this.sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  417. this.bindEvents(this.sheet);
  418. }
  419. },
  420. bindEvents: function (sheet) {
  421. let me = sectionObj;
  422. const Events = GC.Spread.Sheets.Events;
  423. sheet.bind(Events.SelectionChanged, me.onSelectionChanged);
  424. sheet.bind(Events.EditStarting, me.onEditStarting);
  425. sheet.bind(Events.EditEnded, me.onEditEnded);
  426. sheet.bind(Events.ClipboardPasting, me.onClipboardPasting);
  427. sheet.bind(Events.ClipboardPasted, me.onClipboardPasted);
  428. me.sectionDelOpr();
  429. me.onContextmenuOpr();
  430. },
  431. initSelection: function (section) {
  432. let me = sectionObj, fr = feeRuleObj;
  433. sheetCommonObj.cleanSheet(fr.sheet, fr.setting, -1);
  434. this.workBook.focus(true);
  435. if(!this.isDef(section)){
  436. me.currentSection = null;
  437. return;
  438. }
  439. me.currentSection = section;
  440. fr.addObj = null;
  441. fr.updateObj = null;
  442. fr.cache = section.feeRule;
  443. fr.sheet.setRowCount(fr.cache.length + 5);
  444. sheetCommonObj.showData(fr.sheet, fr.setting, fr.cache);
  445. },
  446. onContextmenuOpr: function () {
  447. let me = this;
  448. $.contextMenu({
  449. selector: '#instSectionSpread',
  450. build: function($triggerElement, e){
  451. //控制允许右键菜单在哪个位置出现
  452. let sheet = me.sheet;
  453. let offset = $("#instSectionSpread").offset(),
  454. x = e.pageX - offset.left,
  455. y = e.pageY - offset.top;
  456. let target = sheet.hitTest(x, y);
  457. if(target.hitTestType === 3 && me.isDef(target.row) && me.isDef(target.col)){//在表格内
  458. sheet.setActiveCell(target.row, target.col);
  459. me.initSelection(me.cache[target.row]);
  460. return {
  461. callback: function(){},
  462. items: {
  463. "ref": {
  464. name: "批量关联至定额",
  465. disabled: function () {
  466. const inValidCell = !commonUtil.isDef(target.row) || !commonUtil.isDef(target.col);
  467. const inValidData = target.row >= me.cache.length;
  468. return locked || inValidCell || inValidData;
  469. },
  470. icon: "fa-arrow-left",
  471. callback: function (key, opt) {
  472. $('#sectionTreeModal').modal('show');
  473. }
  474. }
  475. }
  476. };
  477. }
  478. else{
  479. return false;
  480. }
  481. }
  482. });
  483. },
  484. onSelectionChanged: function (sender, info) {
  485. let me = sectionObj;
  486. if(info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row){
  487. let row = info.newSelections[0].row;
  488. let section = me.cache[row];
  489. me.initSelection(section);
  490. }
  491. },
  492. onEditStarting: function (sender, args) {
  493. let me = sectionObj, fi = feeItemObj;
  494. if(!me.isDef(fi.currentFeeItem)){
  495. args.cancel = true;
  496. }
  497. },
  498. onEditEnded: function (sender, args) {
  499. let me = sectionObj, fi = feeItemObj, fr = feeRuleObj;
  500. let v = me.isDef(args.editingText) ? args.editingText.toString().trim() : '';
  501. let section = me.cache[args.row];
  502. let updateObj = {updateType: '', updateData: {}};
  503. //update
  504. if(me.isDef(section)){
  505. section.name = !me.isDef(section.name) ? '' : section.name;
  506. if(section.name === v){
  507. return;
  508. }
  509. if(v === ''){
  510. me.sheet.setValue(args.row, args.col, section.name);
  511. return;
  512. }
  513. section.name = v;
  514. updateObj.updateType = me.updateType.update;
  515. updateObj.updateData.ID = section.ID;
  516. updateObj.updateData.name = v;
  517. }
  518. //insert
  519. else{
  520. if(v === ''){
  521. return;
  522. }
  523. updateObj.updateType = me.updateType.new;
  524. updateObj.updateData.ID = uuid.v1();
  525. updateObj.updateData.rationRepId = fi.rationRepId;
  526. updateObj.updateData.name = v;
  527. updateObj.updateData.feeItemId = fi.currentFeeItem.ID;
  528. updateObj.updateData.feeRule = [];
  529. me.cache.push(updateObj.updateData);
  530. me.currentSection = updateObj.updateData;
  531. fr.cache = me.currentSection.feeRule;
  532. }
  533. me.sheet.setRowCount(me.cache.length + 5);
  534. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  535. me.updateSection([updateObj]);
  536. fi.updateFeeItem([{updateType: fi.updateType.update, updateData: {ID: fi.currentFeeItem.ID, $addToSet: {section: {ID: updateObj.updateData.ID}}}}]);
  537. },
  538. onClipboardPasting: function (sender, info) {
  539. let me = sectionObj, fi = feeItemObj;
  540. if(info.cellRange.col + info.cellRange.colCount > me.setting.header.length || !me.isDef(fi.currentFeeItem)){
  541. info.cancel = true;
  542. }
  543. },
  544. onClipboardPasted: function (sender, info) {
  545. let me = sectionObj, fi = feeItemObj, fr = feeRuleObj;
  546. let sePostData = [], fiPostData = [];
  547. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  548. for(let i = 0, len = items.length; i < len; i++){
  549. let row = info.cellRange.row + i;
  550. let section = me.cache[row];
  551. //update
  552. if(me.isDef(section)){
  553. section.name = items[i].name;
  554. let updateObj = {updateType: me.updateType.update, updateData: {ID: section.ID, name: items[i].name}};
  555. sePostData.push(updateObj);
  556. }
  557. //insert
  558. else{
  559. let updateObj = {updateType: me.updateType.new, updateData: {ID: uuid.v1(), rationRepId: fi.rationRepId, name: items[i].name, feeItemId: fi.currentFeeItem.ID, feeRule: []}};
  560. sePostData.push(updateObj);
  561. me.cache.push(updateObj.updateData);
  562. fiPostData.push({updateType: fi.updateType.update, updateData: {ID: fi.currentFeeItem.ID, $addToSet: {section: {ID: updateObj.updateData.ID}}}});
  563. }
  564. }
  565. //update currentSection
  566. me.currentSection = fi.getCurrentData(me.sheet, me.cache);
  567. fr.cache = me.currentSection.feeRule;
  568. me.sheet.setRowCount(me.cache.length + 5);
  569. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  570. if(sePostData.length > 0){
  571. me.updateSection(sePostData);
  572. if(fiPostData.length > 0){
  573. fi.updateFeeItem(fiPostData);
  574. }
  575. }
  576. },
  577. sectionDelOpr: function () {
  578. if (locked) {
  579. return;
  580. }
  581. let me = this;
  582. me.workBook.commandManager().register('sectionDel', function () {
  583. curDeleteType = me.deleteType;
  584. let sels = me.workBook.getSheet(0).getSelections();
  585. for(let i = 0, len = sels.length; i < len; i++){
  586. let sel = sels[i];
  587. //delete
  588. if(sel.colCount === me.setting.header.length){
  589. $('#delAlert').modal('show');
  590. break;
  591. }
  592. }
  593. });
  594. me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  595. me.workBook.commandManager().setShortcutKey('sectionDel', GC.Spread.Commands.Key.del, false, false, false, false);
  596. },
  597. updateSection: function (updateData, callback) {
  598. CommonAjax.post('/rationRepository/api/updateSection', {updateData: updateData}, function (rstData) {
  599. if(callback){
  600. callback();
  601. }
  602. });
  603. }
  604. };
  605. //费用规则
  606. let feeRuleObj = {
  607. workBook: null,
  608. sheet: null,
  609. addObj: null,
  610. updateObj: null,
  611. avtiveCell: {row: 0, col: 0},
  612. cache: [],
  613. base: {RCJ: '分别按人材机乘系数', RG: '人工', CL: '材料', JX: '机械'},
  614. setting: {
  615. header:[
  616. {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center", span:{row: 0, rowCount: 2, colCount: 1}},
  617. {headerName:"费用规则",headerWidth:280,dataCode:"rule", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center", span:{row: 0, rowCount: 2, colCount: 1}},
  618. {headerName:"基数",headerWidth:140,dataCode:"base", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center", span:{row: 0, rowCount: 2, colCount: 1}},
  619. {headerName:"费率(%)",headerWidth:70,dataCode:"feeRate", dataType: "String", formatter: "@", hAlign: "center", vAlign: "center", span:{row: 0, rowCount: 2, colCount: 1}},
  620. {headerName:"人工(%)",headerWidth:70,dataCode:"labour", dataType: "String", formatter: "@", hAlign: "center", vAlign: "center", span:{row: 1, rowCount: 1, colCount: 1}},
  621. {headerName:"材料(%)",headerWidth:70,dataCode:"material", dataType: "String", formatter: "@", hAlign: "center", vAlign: "center", span:{row: 1, rowCount: 1, colCount: 1}},
  622. {headerName:"机械(%)",headerWidth:70,dataCode:"machine", dataType: "String", formatter: "@", hAlign: "center", vAlign: "center", span:{row: 1, rowCount: 1, colCount: 1}}
  623. ],
  624. view: {lockColumns: []},
  625. comboItems: {base: ['分别按人材机乘系数', '人工', '材料', '机械']},
  626. options: {
  627. tabStripVisible: false,
  628. allowCopyPasteExcelStyle : false,
  629. allowExtendPasteRange: true,
  630. allowUserDragDrop : false,
  631. allowUserDragFill: false,
  632. scrollbarMaxAlign : true
  633. }
  634. },
  635. isDef: function (v) {
  636. return v !== undefined && v !== null;
  637. },
  638. //sheet things
  639. setOptions: function (workbook, opts) {
  640. for(let opt in opts){
  641. workbook.options[opt] = opts[opt];
  642. }
  643. },
  644. buildHeader: function () {
  645. if(!this.sheet){
  646. return;
  647. }
  648. this.sheet.suspendPaint();
  649. this.sheet.suspendEvent();
  650. let ch = GC.Spread.Sheets.SheetArea.colHeader;
  651. this.sheet.setRowCount(2, ch);
  652. for(let i = 0; i < this.setting.header.length; i++){
  653. let header = this.setting.header[i];
  654. this.sheet.addSpan(header.span.row, i, header.span.rowCount, header.span.colCount, ch);
  655. this.sheet.setValue(header.span.row, i, header.headerName, ch);
  656. if(header.dataCode === 'feeRate'){
  657. this.sheet.addSpan(header.span.row, i + 1, 1, 3, ch);
  658. this.sheet.setValue(header.span.row, i + 1, '其中', ch);
  659. }
  660. }
  661. this.sheet.resumePaint();
  662. this.sheet.resumeEvent();
  663. },
  664. buildSheet: function () {
  665. let fi = feeItemObj;
  666. if(!this.isDef(this.workBook)){
  667. this.workBook = sheetCommonObj.buildSheet($('#instFeeRuleSpread')[0], this.setting, 10);
  668. this.sheet = this.workBook.getActiveSheet();
  669. this.buildHeader();
  670. this.sheet.setRowCount();
  671. sheetCommonObj.bindEscKey(this.workBook, [{sheet: this.sheet, editStarting: this.onEditStarting, editEnded: this.onEditEnded}])
  672. this.setOptions(this.workBook, this.setting.options);
  673. this.sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  674. this.bindEvents(this.sheet);
  675. //init combo
  676. fi.setCombo(this.sheet, 2, this.setting.comboItems.base);
  677. }
  678. },
  679. bindEvents: function (sheet) {
  680. let me = feeRuleObj;
  681. const Events = GC.Spread.Sheets.Events;
  682. sheet.bind(Events.SelectionChanged, me.onSelectionChanged);
  683. sheet.bind(Events.EnterCell, me.onEnterCell);
  684. sheet.bind(Events.EditStarting, me.onEditStarting);
  685. sheet.bind(Events.EditEnded, me.onEditEnded);
  686. sheet.bind(Events.ClipboardPasting, me.onClipboardPasting);
  687. sheet.bind(Events.ClipboardPasted, me.onClipboardPasted);
  688. me.feeRuleDelOpr();
  689. },
  690. canSave: function (addObj) {
  691. return this.isDef(addObj.code) && addObj.code !== '' && !this.hasCode(addObj) && this.validRCJ(addObj);
  692. },
  693. hasCode: function (obj) {
  694. let fi = feeItemObj;
  695. for(let feeItem of fi.cache){
  696. for(let section of feeItem.section){
  697. for(let feeRule of section.feeRule){
  698. if(obj.code == feeRule.code && (this.isDef(obj.ID) && obj.ID !== feeRule.ID || !this.isDef(obj.ID))){
  699. return true;
  700. }
  701. }
  702. }
  703. }
  704. return false;
  705. },
  706. //费用类型为人、材、机时人材机总和一定要为100
  707. validRCJ: function (feeRule) {
  708. if(!this.isDef(feeRule.base) || feeRule.base === this.base.RCJ){
  709. return true;
  710. }
  711. let sum = 0;
  712. if(this.isDef(feeRule.labour)){
  713. sum = parseFloat(sum + feeRule.labour).toDecimal(2);
  714. }
  715. if(this.isDef(feeRule.material)){
  716. sum = parseFloat(sum + feeRule.material).toDecimal(2);
  717. }
  718. if(this.isDef(feeRule.machine)){
  719. sum = parseFloat(sum + feeRule.machine).toDecimal(2);
  720. }
  721. return sum == 100;
  722. },
  723. validPasteData: function (data) {
  724. //插入须有编码
  725. if(!this.isDef(data.code)){
  726. return false;
  727. }
  728. //编码须唯一
  729. if(this.isDef(data.code) && this.hasCode(data)){
  730. return false;
  731. }
  732. //基数要合法
  733. if(this.isDef(data.base) && this.setting.comboItems.base.indexOf(data.base) < 0){
  734. return false;
  735. }
  736. //费率、人工、材料、机械,须数字
  737. if(this.isDef(data.feeRate) && isNaN(data.feeRate)){
  738. return false;
  739. }
  740. if(this.isDef(data.labour) && isNaN(data.labour)){
  741. return false;
  742. }
  743. if(this.isDef(data.material) && isNaN(data.material)){
  744. return false;
  745. }
  746. if(this.isDef(data.machine) && isNaN(data.machine)){
  747. return false;
  748. }
  749. //基数为分别按人材机时,费率须为空
  750. if(this.isDef(data.base) && data.base === this.base.RCJ && this.isDef(data.feeRate) && data.feeRate !== ''){
  751. return false;
  752. }
  753. //基数为 人工、材料、机械时 三者之和须为100
  754. if(this.isDef(data.base) && data.base !== this.base.RCJ && this.validRCJ(data)){
  755. return false;
  756. }
  757. return true;
  758. },
  759. decimalPasteData: function (data) {
  760. if(this.isDef(data.feeRate) && isNaN(data.feeRate)){
  761. data.feeRate = parseFloat(data.feeRate).toDecimal(2);
  762. }
  763. if(this.isDef(data.labour) && isNaN(data.labour)){
  764. data.labour = parseFloat(data.labour).toDecimal(2);
  765. }
  766. if(this.isDef(data.material) && isNaN(data.material)){
  767. data.material = parseFloat(data.material).toDecimal(2);
  768. }
  769. if(this.isDef(data.machine) && isNaN(data.machine)){
  770. data.machine = parseFloat(data.machine).toDecimal(2);
  771. }
  772. },
  773. getFocusToCol: function (feeRule) {
  774. if(!this.isDef(feeRule.labour) || feeRule.labour === '' || feeRule.labour > 100 || feeRule.labour == 0){
  775. return 4;
  776. }
  777. if(!this.isDef(feeRule.material) || feeRule.material === '' || feeRule.material > 100 || feeRule.material == 0){
  778. return 5;
  779. }
  780. if(!this.isDef(feeRule.machine) || feeRule.machine === '' || feeRule.machine > 100 || feeRule.machine == 0){
  781. return 6;
  782. }
  783. return 4;
  784. },
  785. onSelectionChanged: function (sender, info) {
  786. let me = feeRuleObj, se = sectionObj;
  787. if(info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row){
  788. if(me.isDef(me.updateObj) && !me.validRCJ(me.updateObj)){
  789. $('#rcjAlert').modal('show');
  790. $('#aleCanceBtn').bind('click', function () {
  791. me.updateObj.base = me.base.RCJ;
  792. me.updateObj.labour = 0;
  793. me.updateObj.material = 0;
  794. me.updateObj.machine = 0;
  795. me.sheet.setRowCount(me.cache.length + 5);
  796. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  797. se.updateSection([{updateType: se.updateType.update, updateData: se.currentSection}]);
  798. $(this).unbind();
  799. });
  800. $('#aleConfBtn').bind('click', function () {
  801. me.sheet.setActiveCell(me.avtiveCell.row, me.getFocusToCol(me.updateObj));
  802. $(this).unbind();
  803. });
  804. }
  805. if(me.isDef(me.addObj)){
  806. if(!me.isDef(me.addObj.code) || me.addObj.code === ''){
  807. $('#alertText').text('请输入编码');
  808. $('#codeAlert').modal('show');
  809. $('#codAleClose').click(function () {
  810. me.addObj.code = '';
  811. me.sheet.setActiveCell(me.avtiveCell.row, 0);
  812. $(this).unbind();
  813. });
  814. $('#codAlertQR').click(function () {
  815. me.addObj.code = '';
  816. me.sheet.setActiveCell(me.avtiveCell.row, 0);
  817. $(this).unbind();
  818. });
  819. //取消插入
  820. $('#codAlertQX').click(function () {
  821. me.addObj = null;
  822. me.sheet.setRowCount(me.cache.length + 5);
  823. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  824. $(this).unbind();
  825. });
  826. }
  827. else if(me.isDef(me.addObj.code) && me.hasCode(me.addObj)){
  828. $('#alertText').text('输入的编号已存在,请重新输入');
  829. $('#codeAlert').modal('show');
  830. $('#codAleClose').click(function () {
  831. me.addObj.code = '';
  832. me.sheet.setValue(me.avtiveCell.row, 0, '');
  833. me.sheet.setActiveCell(me.avtiveCell.row,0);
  834. $(this).unbind();
  835. });
  836. $('#codAlertQR').click(function () {
  837. me.addObj.code = '';
  838. me.sheet.setValue(me.avtiveCell.row, 0, '');
  839. me.sheet.setActiveCell(me.avtiveCell.row, 0);
  840. $(this).unbind();
  841. });
  842. //取消插入
  843. $('#codAlertQX').click(function () {
  844. me.addObj = null;
  845. me.sheet.setRowCount(me.cache.length + 5);
  846. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  847. $(this).unbind();
  848. });
  849. }
  850. else if(!me.validRCJ(me.addObj)){
  851. $('#rcjAlert').modal('show');
  852. $('#aleCanceBtn').bind('click', function () {
  853. me.addObj = null;
  854. me.sheet.setRowCount(me.cache.length + 5);
  855. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  856. $(this).unbind();
  857. });
  858. $('#aleConfBtn').bind('click', function () {
  859. me.sheet.setActiveCell(me.avtiveCell.row, me.getFocusToCol(me.addObj));
  860. $(this).unbind();
  861. });
  862. }
  863. }
  864. }
  865. },
  866. onEnterCell: function (sender, args) {
  867. let me = feeRuleObj;
  868. me.sheet.repaint();
  869. },
  870. onEditStarting: function (sender, args) {
  871. let me = feeRuleObj, se = sectionObj;
  872. let feeRule = me.cache[args.row];
  873. let rcjCols = [4, 5, 6];
  874. if(!me.isDef(se.currentSection)){
  875. args.cancel = true;
  876. }
  877. if(me.isDef(feeRule) && me.isDef(feeRule.base) && feeRule.base === me.base.RCJ && args.col === 3){
  878. args.cancel = true;
  879. }
  880. else if(!me.isDef(feeRule) && rcjCols.indexOf(args.col) > 0){
  881. args.cancel = true;
  882. }
  883. },
  884. onEditEnded: function (sender, args) {
  885. let me = feeRuleObj, se = sectionObj;
  886. let v = me.isDef(args.editingText) ? args.editingText.toString().trim() : '';
  887. let updateObj = {updateType: '', updateData: {}};
  888. let feeRule = me.cache[args.row];
  889. let field = me.setting.header[args.col]['dataCode'];
  890. let orgV = me.isDef(feeRule) && me.isDef(feeRule[field]) ? feeRule[field] : '';
  891. me.updateObj = feeRule;
  892. me.avtiveCell.row = args.row;
  893. me.avtiveCell.col = args.col;
  894. //update
  895. if(me.isDef(feeRule)){
  896. feeRule[field] = me.isDef(feeRule[field]) ? feeRule[field] : '';
  897. if(v === feeRule[field]){
  898. return;
  899. }
  900. if(field === 'base' && v === me.base.RCJ){
  901. //基数为人材机时,费率清空
  902. feeRule.feeRate = '';
  903. me.sheet.setValue(args.row, 3, '');
  904. }
  905. else if(field === 'base' && (v === me.base.RG || v === me.base.CL || v === me.base.JX)){
  906. if(!me.validRCJ(feeRule)){//基数变为人工、材料、机械时,若人材机之和不为100,则清空
  907. feeRule.labour = 0;
  908. feeRule.material = 0;
  909. feeRule.machine = 0;
  910. }
  911. }
  912. else if(field === 'labour' || field === 'material' || field === 'machine' || field === 'feeRate'){
  913. if(isNaN(v)){
  914. alert('只能输入数值');
  915. me.sheet.setValue(args.row, args.col, orgV);
  916. return;
  917. }
  918. v = parseFloat(v).toDecimal(2);
  919. me.sheet.setValue(args.row, args.col, v);
  920. }
  921. feeRule[field] = v;
  922. //编码重复,不可更新
  923. if(field === 'code'){
  924. if(v == ''){
  925. me.sheet.setValue(args.row, args.col, orgV);
  926. return;
  927. feeRule[field] = orgV;
  928. }
  929. if(me.hasCode(feeRule)){
  930. $('#codeAlert').modal('show');
  931. $('#codAleClose').bind('click', function () {
  932. me.sheet.setActiveCell(args.row, 0);
  933. me.sheet.setValue(args.row, args.col, orgV);
  934. });
  935. $('#codAlertQR').bind('click', function () {
  936. me.sheet.setActiveCell(args.row, 0);
  937. me.sheet.setValue(args.row, args.col, orgV);
  938. });
  939. feeRule[field] = orgV;
  940. return;
  941. }
  942. }
  943. //是否能更新
  944. //人材机之和不为100,不解决之和为100取消操作的话,则基数自动换成分别按人材机
  945. if(!me.validRCJ(feeRule)){
  946. return;
  947. }
  948. updateObj.updateType = se.updateType.update;
  949. updateObj.updateData.ID = se.currentSection.ID;
  950. updateObj.updateData.feeRule = me.cache;
  951. me.updateObj = null;
  952. se.updateSection([{updateType: se.updateType.update, updateData: se.currentSection}]);
  953. }
  954. //insert
  955. else{
  956. if(v === ''){
  957. return;
  958. }
  959. if(!me.isDef(me.addObj)){
  960. me.addObj = Object.create(null);
  961. }
  962. me.addObj[me.setting.header[args.col]['dataCode']] = v;
  963. if(me.canSave(me.addObj)){
  964. me.addObj.ID = uuid.v1();
  965. me.cache.push(me.addObj);
  966. updateObj.updateType = se.updateType.update;
  967. updateObj.updateData.ID = se.currentSection.ID;
  968. updateObj.updateData.feeRule = me.cache;
  969. me.sheet.setRowCount(me.cache.length + 5);
  970. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  971. se.updateSection([{updateType: se.updateType.update, updateData: se.currentSection}]);
  972. me.addObj = null;
  973. }
  974. }
  975. },
  976. onClipboardPasting: function (sender, info) {
  977. let me = feeRuleObj, se = sectionObj;
  978. if(info.cellRange.col + info.cellRange.colCount > me.setting.header.length){
  979. info.cancel = true;
  980. }
  981. if(!me.isDef(se.currentSection)){
  982. info.cancel = true;
  983. }
  984. },
  985. onClipboardPasted: function (sender, info) {
  986. let me = feeRuleObj, se = sectionObj, fi = feeItemObj;
  987. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  988. me.toUpdate = false;
  989. for(let i = 0, len = items.length; i < len; i++){
  990. let row = info.cellRange.row + i;
  991. let feeRule = me.cache[row];
  992. //update
  993. if(me.isDef(feeRule)){
  994. let tempObj = {};
  995. for(let attr in feeRule){
  996. tempObj[attr] = feeRule[attr];
  997. }
  998. for(let attr in items[i]){
  999. tempObj[attr] = items[i][attr]
  1000. }
  1001. if(me.validPasteData(tempObj)){
  1002. me.decimalPasteData(tempObj);
  1003. //front
  1004. feeRule = tempObj;
  1005. me.toUpdate = true;
  1006. }
  1007. }
  1008. //insert
  1009. else{
  1010. if(me.validPasteData(items[i])){
  1011. me.decimalPasteData(items[i]);
  1012. items[i].ID = uuid.v1();
  1013. //front
  1014. me.cache.push(items[i]);
  1015. me.toUpdate = true;
  1016. }
  1017. }
  1018. }
  1019. me.sheet.setRowCount(me.cache.length + 5);
  1020. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  1021. if(me.toUpdate){
  1022. se.updateSection([{updateType: se.updateType.update, updateData: se.currentSection}]);
  1023. }
  1024. },
  1025. feeRuleDelOpr: function () {
  1026. if (locked) {
  1027. return;
  1028. }
  1029. let me = feeRuleObj, se = sectionObj;
  1030. me.workBook.commandManager().register('feeRuleDel', function () {
  1031. me.toUpdate = false;
  1032. let sels = me.workBook.getSheet(0).getSelections();
  1033. for(let i = 0, len = sels.length; i < len; i++){
  1034. let sel = sels[i];
  1035. //delete
  1036. if(sel.colCount === me.setting.header.length){
  1037. for(let j = 0, jLen = sel.rowCount; j < jLen; j++){
  1038. let row = sel.row + j;
  1039. let feeRule = me.cache[row];
  1040. //有数据,删除数据
  1041. if(me.isDef(feeRule)){
  1042. me.toUpdate = true;
  1043. }
  1044. }
  1045. //front delete
  1046. me.cache.splice(sel.row, sel.rowCount);
  1047. }
  1048. }
  1049. if(me.toUpdate){
  1050. se.updateSection([{updateType: se.updateType.update, updateData: se.currentSection}], function () {
  1051. me.sheet.setRowCount(me.cache.length + 5);
  1052. sheetCommonObj.showData(me.sheet, me.setting, me.cache);
  1053. });
  1054. }
  1055. });
  1056. me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  1057. me.workBook.commandManager().setShortcutKey('feeRuleDel', GC.Spread.Commands.Key.del, false, false, false, false);
  1058. }
  1059. };
  1060. let batchSectionObj = {
  1061. cache: null,
  1062. tree: null,
  1063. controller: null,
  1064. workBook: null,
  1065. sheet: null,
  1066. IDRowMapping: null, //树节点ID与行号映射
  1067. setting: {
  1068. sheet: {
  1069. cols:[
  1070. {
  1071. head: {
  1072. titleNames: ['选择'],
  1073. spanCols: [1],
  1074. spanRows: [2],
  1075. vAlign: [1, 1],
  1076. hAlign: [1, 1],
  1077. font: 'Arial'
  1078. },
  1079. data: {
  1080. field: 'select',
  1081. vAlign: 1,
  1082. hAlign: 0,
  1083. font: 'Arial'
  1084. },
  1085. width: 50
  1086. },
  1087. {
  1088. head: {
  1089. titleNames: ['名称'],
  1090. spanCols: [1],
  1091. spanRows: [2],
  1092. vAlign: [1, 1],
  1093. hAlign: [1, 1],
  1094. font: 'Arial'
  1095. },
  1096. data: {
  1097. field: 'name',
  1098. vAlign: 1,
  1099. hAlign: 0,
  1100. font: 'Arial'
  1101. },
  1102. width: 400
  1103. }
  1104. ],
  1105. headRows: 1,
  1106. headRowHeight: [47],
  1107. emptyRows: 0,
  1108. treeCol: 1
  1109. },
  1110. tree: {
  1111. id: 'ID',
  1112. pid: 'ParentID',
  1113. nid: 'NextSiblingID',
  1114. rootId: -1
  1115. },
  1116. options: {
  1117. tabStripVisible: false,
  1118. allowCopyPasteExcelStyle : false,
  1119. allowExtendPasteRange: true,
  1120. allowUserDragDrop : false,
  1121. allowUserDragFill: false,
  1122. scrollbarMaxAlign : true
  1123. }
  1124. },
  1125. isDef: function (v) {
  1126. return v !== undefined && v !== null;
  1127. },
  1128. renderFunc: function (sheet, func) {
  1129. sheet.suspendPaint();
  1130. sheet.suspendEvent();
  1131. if(func){
  1132. func();
  1133. }
  1134. sheet.resumePaint();
  1135. sheet.resumeEvent();
  1136. },
  1137. setOptions: function (workbook, opts) {
  1138. for(let opt in opts){
  1139. workbook.options[opt] = opts[opt];
  1140. }
  1141. },
  1142. buildSheet: function () {
  1143. if(!this.isDef(this.workBook)){
  1144. this.workBook = new GC.Spread.Sheets.Workbook($('#batchSectionSpread')[0], {sheetCount: 1});
  1145. this.sheet = this.workBook.getActiveSheet();
  1146. this.setOptions(this.workBook, this.setting.options);
  1147. this.bindEvents(this.sheet);
  1148. }
  1149. },
  1150. bindEvents: function (sheet) {
  1151. let me = this;
  1152. const Events = GC.Spread.Sheets.Events;
  1153. sheet.bind(Events.EditStarting, me.onEditStarting);
  1154. sheet.bind(Events.EditEnded, me.onEditEnded);
  1155. sheet.bind(Events.ClipboardPasting, me.onClipboardPasting);
  1156. me.workBook.bind(Events.ButtonClicked, me.onButtonClicked);//复选框点击事件
  1157. },
  1158. getSectionTree: function (repId) {
  1159. let me = this;
  1160. let url = 'api/getRationTree';
  1161. let postData = {rationLibId: repId};
  1162. let sucFunc = function (rstData) {
  1163. //init
  1164. me.buildSheet();
  1165. me.initTree(rstData);
  1166. me.buildMapping(me.tree.items);
  1167. me.cache = me.tree.items;
  1168. me.initController(me.tree, me.sheet, me.setting.sheet);
  1169. me.controller.showTreeData();
  1170. me.sheet.setFormatter(-1, 1, '@');
  1171. //init checkBox
  1172. let checkBox = new GC.Spread.Sheets.CellTypes.CheckBox();
  1173. me.sheet.getRange(-1, 0, -1, 1).cellType(checkBox);
  1174. me.sheet.getRange(-1, 0, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  1175. };
  1176. let errFunc = function () {
  1177. };
  1178. CommonAjax.post(url, postData, sucFunc, errFunc);
  1179. },
  1180. buildMapping: function (items) {
  1181. this.IDRowMapping = {};
  1182. for(let i = 0, len = items.length; i < len; i++){
  1183. this.IDRowMapping[items[i].data.ID] = i;
  1184. }
  1185. },
  1186. initTree: function (datas) {
  1187. this.tree = idTree.createNew(this.setting.tree);
  1188. this.tree.loadDatas(datas);
  1189. this.tree.selected = this.tree.items.length > 0 ? this.tree.items[0] : null;
  1190. },
  1191. initController: function (tree, sheet, setting) {
  1192. this.controller = TREE_SHEET_CONTROLLER.createNew(tree, sheet, setting);
  1193. },
  1194. onEditStarting: function (sender, args) {
  1195. args.cancel = true;
  1196. },
  1197. onButtonClicked: function (sender, args) {
  1198. let me = batchSectionObj;
  1199. if(args.sheet.isEditing()){
  1200. args.sheet.endEdit(true);
  1201. }
  1202. let node = me.cache[args.row];
  1203. let checked = args.sheet.getValue(args.row, args.col);
  1204. if(!me.isDef(node)){
  1205. return;
  1206. }
  1207. let rows = me.getChildRows(node);
  1208. me.setChecked(rows, checked);
  1209. },
  1210. onClipboardPasting: function (sender, info) {
  1211. info.cancel = true;
  1212. },
  1213. getChildRows: function (node) {
  1214. let rst = [], ids = [];
  1215. getChildIds(node);
  1216. for(id of ids){
  1217. let row = this.IDRowMapping[id];
  1218. if(this.isDef(row)){
  1219. rst.push(row);
  1220. }
  1221. }
  1222. return rst;
  1223. function getChildIds(node){
  1224. ids.push(node.data.ID);
  1225. for(let child of node.children){
  1226. getChildIds(child);
  1227. }
  1228. }
  1229. },
  1230. //设置勾选,跟随父节点
  1231. setChecked: function (rows, checked) {
  1232. let me = this;
  1233. this.renderFunc(this.sheet, function () {
  1234. for(let row of rows){
  1235. me.sheet.setValue(row, 0, checked);
  1236. }
  1237. });
  1238. },
  1239. //获得勾选的章节ID
  1240. getCheckedSection: function (sheet) {
  1241. let rst = [];
  1242. let rowCount = sheet.getRowCount();
  1243. for(let i = 0, len = rowCount; i < len; i++){
  1244. let checked = sheet.getValue(i, 0);
  1245. //只返回叶子节点
  1246. if(this.isDef(checked) && checked == true && this.isDef(this.cache[i]) && this.isDef(this.cache[i].data.ID) && this.cache[i].children.length === 0){
  1247. rst.push(this.cache[i].data.ID);
  1248. }
  1249. }
  1250. return Array.from(new Set(rst));
  1251. },
  1252. clearChecked: function () {
  1253. let me = this;
  1254. this.renderFunc(this.sheet, function () {
  1255. let rowCount = me.sheet.getRowCount();
  1256. for(let i = 0, len = rowCount; i < len; i++){
  1257. me.sheet.setValue(i, 0, false);
  1258. }
  1259. });
  1260. },
  1261. onConfirmBtn: function () {
  1262. let me = this, fi = feeItemObj, se = sectionObj;
  1263. $('#bsTree_conf').bind('click', function () {
  1264. let feeItemId = fi.currentFeeItem.ID;
  1265. let sectionId = se.currentSection.ID;
  1266. let rationSection = me.getCheckedSection(me.sheet);
  1267. me.batchUpdate(feeItemId, sectionId, rationSection);
  1268. $(this).unbind();
  1269. });
  1270. },
  1271. //inst: 定额安装费用, rationSection: 定额章节树IDs
  1272. batchUpdate: function (feeItemId, sectionId, rationSection) {
  1273. CommonAjax.post('/rationRepository/api/batchUpdateInst', {inst: {feeItemId: feeItemId, sectionId: sectionId}, rationSection: rationSection}, function (rstData) {
  1274. $('#sectionTreeModal').modal('hide');
  1275. });
  1276. }
  1277. };