billsGuidance.js 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/6/1
  7. * @version
  8. */
  9. const billsGuidance = (function () {
  10. function _isDef(v) {
  11. return typeof v !== 'undefined' && v !== null;
  12. }
  13. function sortByCode(arr) {
  14. function recurCompare(a, b, index){
  15. if (a[index] && !b[index]) {
  16. return 1;
  17. } else if (!a[index] && b[index]) {
  18. return -1;
  19. } else if (a[index] && b[index]) {
  20. let aV = a[index],
  21. bV = b[index];
  22. if (!isNaN(aV) && !isNaN(bV)) {
  23. aV = parseFloat(a[index]);
  24. bV = parseFloat(b[index]);
  25. }
  26. if (aV > bV) {
  27. return 1;
  28. } else if (aV < bV) {
  29. return -1;
  30. } else {
  31. return recurCompare(a, b, index + 1);
  32. }
  33. }
  34. return 0;
  35. }
  36. arr.sort(function (a, b) {
  37. if (!_isDef(a.code) || !_isDef(b.code)) {
  38. return 0;
  39. }
  40. let aArr = a.code.split('-'),
  41. bArr = b.code.split('-');
  42. return recurCompare(aArr, bArr, 0);
  43. });
  44. }
  45. const locked = lockUtil.getLocked();
  46. let moduleName = 'stdBillsGuidance';
  47. //上下拖动的拖动条高度
  48. const verticalResize = 10;
  49. //自执行函数全局变量定义
  50. const libID = getQueryString('libID');
  51. //总工作内容数据
  52. let stdBillsJobData = [];
  53. //总项目特征数据
  54. let stdBillsFeatureData = [];
  55. const bills = {
  56. dom: $('#billsSpread'),
  57. workBook: null,
  58. cache: [],
  59. tree: null,
  60. controller: null,
  61. treeSetting: {
  62. treeCol: 0,
  63. emptyRows: 0,
  64. headRows: 1,
  65. headRowHeight: [40],
  66. defaultRowHeight: 21,
  67. cols: [{
  68. width: 200,
  69. readOnly: true,
  70. showHint: true,
  71. head: {
  72. titleNames: ["项目编码"],
  73. spanCols: [1],
  74. spanRows: [1],
  75. vAlign: [1],
  76. hAlign: [1],
  77. font: ["Arial"]
  78. },
  79. data: {
  80. field: "code",
  81. vAlign: 1,
  82. hAlign: 0,
  83. font: "Arial"
  84. }
  85. }, {
  86. width: 200,
  87. readOnly: true,
  88. head: {
  89. titleNames: ["项目名称"],
  90. spanCols: [1],
  91. spanRows: [1],
  92. vAlign: [1],
  93. hAlign: [1],
  94. font: ["Arial"]
  95. },
  96. data: {
  97. field: "name",
  98. vAlign: 1,
  99. hAlign: 0,
  100. font: "Arial"
  101. }
  102. }]
  103. },
  104. headers: [
  105. {name: '项目编码', dataCode: 'code', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'},
  106. {name: '项目名称', dataCode: 'name', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'}
  107. ],
  108. events: {
  109. SelectionChanged: function (sender, info) {
  110. billsInitSel(info.newSelections[0].row, info.oldSelections[0]);
  111. }
  112. }
  113. };
  114. const selectedBgColor = '#DFE8F9';
  115. const searchBgColor = 'lemonChiffon';
  116. //项目指引类型
  117. const itemType = {
  118. job: 0,
  119. ration: 1
  120. };
  121. //项目指引复制整块localStorage key
  122. const itemCopyBlockKey = 'guideItemCopyBlock';
  123. const updateType = {
  124. create: 'create',
  125. update: 'update',
  126. del: 'delete'
  127. };
  128. //项目指引节点状态:展开全部、收起定额
  129. const itemExpandState = {
  130. expand: 1,
  131. contract: 0
  132. };
  133. //项目指引当前节点展开收缩状态,默认展开全部
  134. let curExpandState = 1;
  135. const guideItem = {
  136. dom: $('#guideItemSpread'),
  137. workBook: null,
  138. tree: null,
  139. controller: null,
  140. treeSetting: {
  141. treeCol: 0,
  142. emptyRows: 0,
  143. headRows: 1,
  144. headRowHeight: [40],
  145. defaultRowHeight: 21,
  146. cols: [{
  147. width: 400,
  148. readOnly: locked,
  149. head: {
  150. titleNames: ["项目指引"],
  151. spanCols: [1],
  152. spanRows: [1],
  153. vAlign: [1],
  154. hAlign: [1],
  155. font: ["Arial"]
  156. },
  157. data: {
  158. field: "name",
  159. vAlign: 1,
  160. hAlign: 0,
  161. font: "Arial",
  162. formatter: "@"
  163. }
  164. },
  165. {
  166. width: 40,
  167. readOnly: true,
  168. head: {
  169. titleNames: ["输出特征"],
  170. spanCols: [1],
  171. spanRows: [1],
  172. vAlign: [1],
  173. hAlign: [1],
  174. font: ["Arial"]
  175. },
  176. data: {
  177. field: "outputItemCharacter",
  178. vAlign: 1,
  179. hAlign: 1,
  180. font: "Arial"
  181. }
  182. },
  183. {
  184. width: 40,
  185. readOnly: true,
  186. head: {
  187. titleNames: ["必填"],
  188. spanCols: [1],
  189. spanRows: [1],
  190. vAlign: [1],
  191. hAlign: [1],
  192. font: ["Arial"]
  193. },
  194. data: {
  195. field: "required",
  196. vAlign: 1,
  197. hAlign: 1,
  198. font: "Arial"
  199. }
  200. }
  201. ]
  202. },
  203. headers: [
  204. {name: '项目指引', dataCode: 'name', width: 400, vAlign: 'center', hAlign: 'left', formatter: '@'},
  205. {name: '输出特征', dataCode: 'outputItemCharacter', width: 40, vAlign: 'center', hAlign: 'center'},
  206. {name: '必填', dataCode: 'required', width: 40, vAlign: 'center', hAlign: 'center'},
  207. ],
  208. events: {
  209. SelectionChanged: function (sender, info) {
  210. guideItemInitSel(info.newSelections[0].row)
  211. },
  212. EditEnded: function (sender, args) {
  213. edit(args.sheet, [{row: args.row, col: args.col}]);
  214. },
  215. ButtonClicked: function (sender, args) {
  216. edit(args.sheet, [{row: args.row, col: args.col}]);
  217. },
  218. RangeChanged: function (sender, args) {
  219. edit(args.sheet, args.changedCells);
  220. },
  221. CellDoubleClick: function(sender, args) {
  222. locateAtRation(args.row);
  223. }
  224. }
  225. };
  226. //定额章节树
  227. const section = {
  228. dom: $('#sectionSpread'),
  229. workBook: null,
  230. cache: [],
  231. tree: null,
  232. controller: null,
  233. treeSetting: {
  234. treeCol: 0,
  235. emptyRows: 0,
  236. headRows: 1,
  237. headRowHeight: [40],
  238. defaultRowHeight: 21,
  239. cols: [{
  240. width: 400,
  241. readOnly: true,
  242. head: {
  243. titleNames: ["名称"],
  244. spanCols: [1],
  245. spanRows: [1],
  246. vAlign: [1],
  247. hAlign: [1],
  248. font: ["Arial"]
  249. },
  250. data: {
  251. field: "name",
  252. vAlign: 1,
  253. hAlign: 0,
  254. font: "Arial"
  255. }
  256. }]
  257. },
  258. headers: [
  259. {name: '名称', dataCode: 'name', width: 400, vAlign: 'center', hAlign: 'left', formatter: '@'},
  260. ],
  261. events: {
  262. SelectionChanged: function (sender, info) {
  263. sectionInitSel(info.newSelections[0].row)
  264. }
  265. }
  266. };
  267. const ration = {
  268. dom: $('#rationSpread'),
  269. workBook: null,
  270. datas: [],//所有的数据,搜索定额时,从所有数据中筛选
  271. cache: [],//显示在表格上的数据,添加定额可以有效根据行识别定额
  272. headers: [
  273. {name: '选择', dataCode: 'select', width: 50, vAlign: 'center', hAlign: 'center'},
  274. {name: '编码', dataCode: 'code', width: 110, vAlign: 'center', hAlign: 'left', formatter: '@'},
  275. {name: '名称', dataCode: 'name', width: 250, vAlign: 'center', hAlign: 'left', formatter: '@'},
  276. {name: '单位', dataCode: 'unit', width: 100, vAlign: 'center', hAlign: 'left', formatter: '@'}
  277. ],
  278. events: {
  279. ButtonClicked: function (sender, args) {
  280. if(args.sheet.isEditing()){
  281. args.sheet.endEdit(true);
  282. }
  283. },
  284. CellDoubleClick: function (sender, args) {
  285. if(ration.headers[args.col].dataCode === 'name'){
  286. let insertDatas = getInsertRations([args.row]);
  287. if(insertDatas.length > 0){
  288. insert(insertDatas, false);
  289. }
  290. }
  291. }
  292. }
  293. };
  294. const options = {
  295. workBook: {
  296. tabStripVisible: false,
  297. allowContextMenu: false,
  298. allowCopyPasteExcelStyle : false,
  299. allowExtendPasteRange: false,
  300. allowUserDragDrop : false,
  301. allowUserDragFill: false,
  302. scrollbarMaxAlign : true
  303. },
  304. sheet: {
  305. protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
  306. clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
  307. }
  308. };
  309. //渲染时方法,停止渲染
  310. //@param {Object}sheet {Function}func @return {void}
  311. function renderSheetFunc(sheet, func){
  312. sheet.suspendEvent();
  313. sheet.suspendPaint();
  314. if(func){
  315. func();
  316. }
  317. sheet.resumeEvent();
  318. sheet.resumePaint();
  319. }
  320. //设置表选项
  321. //@param {Object}workBook {Object}opts @return {void}
  322. function setOptions (workBook, opts) {
  323. for(let opt in opts.workBook){
  324. workBook.options[opt] = opts.workBook[opt];
  325. }
  326. for(let opt in opts.sheet){
  327. workBook.getActiveSheet().options[opt] = opts.sheet[opt];
  328. }
  329. }
  330. //建表头
  331. //@param {Object}sheet {Array}headers @return {void}
  332. function buildHeader(sheet, headers) {
  333. let fuc = function () {
  334. sheet.setColumnCount(headers.length);
  335. sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
  336. for(let i = 0, len = headers.length; i < len; i++){
  337. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  338. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  339. sheet.getCell(0, i, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  340. if(headers[i].formatter){
  341. sheet.setFormatter(-1, i, headers[i].formatter);
  342. }
  343. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  344. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  345. }
  346. };
  347. renderSheetFunc(sheet, fuc);
  348. }
  349. //表监听事件
  350. //@param {Object}workBook @return {void}
  351. function bindEvent(workBook, events) {
  352. if(Object.keys(events).length === 0){
  353. return;
  354. }
  355. const Events = GC.Spread.Sheets.Events;
  356. for(let event in events){
  357. workBook.bind(Events[event], events[event]);
  358. }
  359. }
  360. //建表
  361. //@param {Object}module @return {void}
  362. function buildSheet(module) {
  363. if(!module.workBook){
  364. module.workBook = new GC.Spread.Sheets.Workbook(module.dom[0], {sheetCount: 1});
  365. let sheet = module.workBook.getActiveSheet();
  366. if(module === bills){
  367. sheet.name('stdBillsGuidance_bills');
  368. //默认初始可控制焦点在清单表中
  369. module.workBook.focus();
  370. sheet.options.isProtected = true;
  371. }
  372. else if(module === ration){
  373. sheet.options.isProtected = true;
  374. sheet.getRange(-1, 0, -1, 1).locked(locked);
  375. sheet.getRange(-1, 1, -1, -1).locked(true);
  376. }
  377. else if(module === guideItem){
  378. sheetCommonObj.bindEscKey(module.workBook, [{sheet: sheet, editStarting: null, editEnded: module.events.EditEnded}]);
  379. }
  380. setOptions(module.workBook, options);
  381. buildHeader(module.workBook.getActiveSheet(), module.headers);
  382. bindEvent(module.workBook, module.events);
  383. }
  384. lockUtil.lockSpreads([module.workBook], locked);
  385. if (locked) {
  386. // 锁定表格后双击事件失效了,但是需要双击定位,因此重新绑定双击
  387. if (module === guideItem) {
  388. module.workBook.bind('CellDoubleClick', module.events.CellDoubleClick);
  389. }
  390. }
  391. }
  392. //清空表数据
  393. //@param {Object}sheet {Array}headers {Number}rowCount @return {void}
  394. function cleanData(sheet, headers, rowCount){
  395. renderSheetFunc(sheet, function () {
  396. sheet.clear(-1, 0, -1, headers.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  397. if (rowCount >= 0) {
  398. sheet.setRowCount(rowCount);
  399. }
  400. });
  401. }
  402. //根据清单获取项目指引
  403. //@param {String}guidanceLibID {Number}billsID {Function}callback @return {void}
  404. function getItemsByBills(guidanceLibID, billsID, callback){
  405. CommonAjax.post('/billsGuidance/api/getItemsByBills', {guidanceLibID: guidanceLibID, billsID: billsID}, function (rstData) {
  406. if(callback){
  407. callback(rstData);
  408. }
  409. });
  410. }
  411. //清单表焦点控制
  412. //@param {Number}row @return {void}
  413. function billsInitSel(row, oldSel){
  414. let guideSheet = guideItem.workBook.getActiveSheet();
  415. cleanData(guideSheet, guideItem.headers, -1);
  416. let node = bills.tree.items[row];
  417. if(!node){
  418. return;
  419. }
  420. const billSheet = bills.workBook.getActiveSheet();
  421. setBgColor(billSheet, row, selectedBgColor);
  422. if (oldSel && row !== oldSel.row) {
  423. const orgNode = bills.tree.items[oldSel.row]
  424. setBgColor(billSheet, oldSel.row, orgNode && orgNode.isSearch ? searchBgColor : 'white');
  425. }
  426. bills.tree.selected = node;
  427. //显示备注
  428. $('.main-side-bottom').find('textarea').val(node.data.comment ? node.data.comment : '');
  429. if(!node.guidance.tree){
  430. getItemsByBills(libID, node.data.ID, function (rstData) {
  431. initTree(node.guidance, guideSheet, guideItem.treeSetting, rstData);
  432. setNodesExpandState(node.guidance.tree.items, curExpandState);
  433. showCheckBox(guideSheet, node.guidance.tree.items);
  434. renderSheetFunc(guideSheet, function () {
  435. TREE_SHEET_HELPER.refreshNodesVisible(node.guidance.tree.roots, guideSheet, true);
  436. });
  437. //设置底色
  438. setNodesColor(guideSheet, node.guidance.tree.items);
  439. //项目指引初始焦点
  440. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  441. });
  442. } else{
  443. setNodesExpandState(node.guidance.tree.items, curExpandState);
  444. node.guidance.controller.showTreeData();
  445. showCheckBox(guideSheet, node.guidance.tree.items);
  446. //设置底色
  447. setNodesColor(guideSheet, node.guidance.tree.items);
  448. //项目指引初始焦点
  449. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  450. }
  451. }
  452. function showCheckBox(sheet, nodes) {
  453. // const checkBoxType = locked ? sheetCommonObj.getReadOnlyCheckBox() : sheetCommonObj.getCheckBox();
  454. const checkBoxType = new GC.Spread.Sheets.CellTypes.CheckBox();
  455. const baseType = new GC.Spread.Sheets.CellTypes.Base();
  456. renderSheetFunc(sheet, function () {
  457. nodes.forEach(node => {
  458. const row = node.serialNo();
  459. if (node.depth() % 2 === 0 && _isDef(node.data.type) && node.data.type === itemType.job) {
  460. sheet.setCellType(row, 1, checkBoxType);
  461. sheet.setCellType(row, 2, checkBoxType);
  462. sheet.setValue(row, 1, node.data.outputItemCharacter || false);
  463. sheet.setValue(row, 2, node.data.required || false);
  464. } else {
  465. sheet.setCellType(row, 1, baseType);
  466. sheet.setCellType(row, 2, baseType);
  467. sheet.setValue(row, 1, '');
  468. sheet.setValue(row, 2, '');
  469. }
  470. })
  471. });
  472. }
  473. //设置项目节点展开收起状态:展开全部、收起定额
  474. //@param {Array}nodes(当前清单下的所有项目指引节点) {Number}expandState(展开全部1或收起定额0).
  475. function setNodesExpandState(nodes, expandState) {
  476. if(expandState === itemExpandState.contract) {
  477. //找出所有定额的父节点
  478. let rations = _.filter(nodes, function (node) {
  479. return node.data.type === itemType.ration;
  480. });
  481. let rationParentIDs = [];
  482. for(let ration of rations){
  483. if(ration.data.ParentID != -1){
  484. rationParentIDs.push(ration.data.ParentID);
  485. }
  486. }
  487. rationParentIDs = Array.from(new Set(rationParentIDs));
  488. let rationParentNodes = _.filter(nodes, function (node) {
  489. return rationParentIDs.includes(node.data.ID);
  490. });
  491. //收起定额
  492. for(let node of rationParentNodes){
  493. node.setExpanded(false);
  494. }
  495. } else {
  496. for(let node of nodes){
  497. node.setExpanded(true);
  498. }
  499. }
  500. }
  501. // 设置行底色
  502. function setBgColor(sheet, row, color) {
  503. const style = new GC.Spread.Sheets.Style();
  504. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  505. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  506. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  507. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  508. style.backColor = color;
  509. sheet.setStyle(row, -1, style);
  510. }
  511. //根据奇偶层级设置节点底色,奇数层为蓝色(树节点深度为偶数)
  512. //@param {Object}sheet {Array}nodes @return {void}
  513. function setNodesColor(sheet, nodes) {
  514. renderSheetFunc(sheet, function () {
  515. for(let node of nodes){
  516. const nDepth = node.depth();
  517. const color = nDepth % 2 == 0 && _isDef(node.data.type) && node.data.type === itemType.job ? selectedBgColor : 'White';
  518. setBgColor(sheet, node.serialNo(), color);
  519. }
  520. });
  521. }
  522. //选中的节点是否全是同层节点
  523. //@param {Object}sheet {Array}items @return {Boolean}
  524. function itemsSameDepth(sheet, items) {
  525. let sels = sheet.getSelections();
  526. if(sels.length === 0 || items.length === 0){
  527. return false;
  528. }
  529. let depths = [];
  530. for(let i = 0; i < sels[0].rowCount; i++){
  531. let row = sels[0].row + i;
  532. let node = items[row];
  533. if(node){
  534. depths.push(node.depth());
  535. }
  536. }
  537. }
  538. //节点子项是否全是工作内容
  539. //@param {Object}node @return {Boolean}
  540. function allJobChildren(node){
  541. for(let c of node.children){
  542. if(c.data.type === itemType.ration){
  543. return false;
  544. }
  545. }
  546. return true;
  547. }
  548. //节点子项是否全是定额
  549. //@param {Object}node @return {Boolean}
  550. function allRationChildren(node){
  551. for(let c of node.children){
  552. if(c.data.type === itemType.job){
  553. return false;
  554. }
  555. }
  556. return true;
  557. }
  558. //刷新按钮有效性
  559. //@param {Object}node @return {void}
  560. function refreshBtn(node){
  561. if (locked) {
  562. return;
  563. }
  564. //全部设为无效
  565. $('.tools-btn').children().addClass('disabled');
  566. $('#insertRation').addClass('disabled');
  567. $('#insertAll').addClass('disabled');
  568. $('.main-bottom-content').find('textarea').attr('readonly', true);
  569. //插入
  570. if(bills.tree.selected && bills.tree.selected.guidance.tree){
  571. $('#insert').removeClass('disabled');
  572. if(node && node.data.type === itemType.ration){
  573. $('#insert').addClass('disabled');
  574. }
  575. }
  576. //删除
  577. if(node){
  578. $('#del').removeClass('disabled');
  579. }
  580. if(node && node.data.type === itemType.job){
  581. //升级
  582. if(node.parent){
  583. $('#upLevel').removeClass('disabled');
  584. if(node.nextSibling && node.children.length > 0 && !allJobChildren(node)){
  585. $('#upLevel').addClass('disabled');
  586. }
  587. }
  588. //降级
  589. if(node.preSibling){
  590. $('#downLevel').removeClass('disabled');
  591. if(node.preSibling.children.length > 0 && !allJobChildren(node.preSibling)){
  592. $('#downLevel').addClass('disabled');
  593. }
  594. }
  595. }
  596. //上移
  597. if(node && node.preSibling){
  598. $('#upMove').removeClass('disabled')
  599. }
  600. //下移
  601. if(node && node.nextSibling){
  602. $('#downMove').removeClass('disabled');
  603. }
  604. //收起定额、展开全部
  605. $('#expandContract').removeClass('disabled');
  606. //插入定额
  607. if(node && (node.children.length === 0 || allRationChildren(node))){
  608. $('#insertRation').removeClass('disabled');
  609. $('#insertAll').removeClass('disabled');
  610. }
  611. //备注,奇数节点可用
  612. if(node && (node.depth() + 1) % 2 === 1 && node.data.type !== itemType.ration){
  613. $('.main-bottom-content').find('textarea').attr('readonly', false);
  614. }
  615. }
  616. //项目指引表焦点控制
  617. //@param {Number}row @return {void}
  618. function guideItemInitSel(row){
  619. let billsNode = bills.tree.selected;
  620. let node = null;
  621. if(billsNode && billsNode.guidance.tree){
  622. node = billsNode.guidance.tree.items[row];
  623. if(node){
  624. billsNode.guidance.tree.selected = node;
  625. //显示备注
  626. $('.main-bottom-content').find('textarea').val(node.data.comment ? node.data.comment : '');
  627. }
  628. }
  629. refreshBtn(node);
  630. }
  631. //初始化当前库名
  632. //@param {String} @return {void}
  633. function initLibName(libName) {
  634. $('#libName')[0].outerHTML = $('#libName')[0].outerHTML.replace("XXX清单指引", libName);
  635. }
  636. //初始化各工作表
  637. //@param {Array}modules @return {void}
  638. function initWorkBooks(modules){
  639. for(let module of modules){
  640. buildSheet(module);
  641. }
  642. }
  643. function tipDivCheck(){
  644. setTimeout(function () {
  645. let tips = $('#autoTip');
  646. if(ration.tipDiv == 'show'){
  647. return;
  648. } else if(ration.tipDiv == 'hide'&&tips){
  649. tips.hide();
  650. ration._toolTipElement = null;
  651. }
  652. },600)
  653. }
  654. //获取悬浮提示单元格
  655. //@param {Object}sheet @return {Object}
  656. function getTipCellType(sheet) {
  657. let setting = {};
  658. let TipCellType = function () {};
  659. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  660. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  661. return {
  662. x: x,
  663. y: y,
  664. row: context.row,
  665. col: context.col,
  666. cellStyle: cellStyle,
  667. cellRect: cellRect,
  668. sheet: context.sheet,
  669. sheetArea: context.sheetArea
  670. };
  671. };
  672. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  673. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  674. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  675. if(tag !== undefined && tag){
  676. text = tag;
  677. }
  678. if(sheet && sheet.getParent().qo){
  679. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  680. }
  681. let delayTimes = 500; //延时时间
  682. let now_timeStamp = +new Date();
  683. this.tipTimeStamp = now_timeStamp;
  684. let me = this;
  685. setTimeout(function () {
  686. if(now_timeStamp - me.tipTimeStamp == 0){//鼠标停下的时候才显示
  687. if (setting.pos && text && text !== '') {
  688. //固定不显示的div,存储文本获取固定div宽度,toolTipElement由于显示和隐藏,获取宽度不正确
  689. if(!me._fixedTipElement){
  690. let div = $('#fixedTip1')[0];
  691. if (!div) {
  692. div = document.createElement("div");
  693. $(div).css("padding", 5)
  694. .attr("id", 'fixedTip');
  695. $(div).hide();
  696. document.body.insertBefore(div, null);
  697. }
  698. me._fixedTipElement = div;
  699. }
  700. $(me._fixedTipElement).width('');
  701. $(me._fixedTipElement).html(text);
  702. if (!me._toolTipElement) {
  703. let div = $('#autoTip1')[0];
  704. if (!div) {
  705. div = document.createElement("div");
  706. $(div).css("position", "absolute")
  707. .css("border", "1px #C0C0C0 solid")
  708. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  709. .css("font", "0.9rem Calibri")
  710. .css("background", "white")
  711. .css("padding", 5)
  712. .attr("id", 'autoTip1');
  713. $(div).hide();
  714. document.body.insertBefore(div, null);
  715. }
  716. me._toolTipElement = div;
  717. $(me._toolTipElement).width('');
  718. //实时读取位置信息
  719. if(hitinfo.sheet && hitinfo.sheet.getParent().qo){
  720. setting.pos = SheetDataHelper.getObjPos(hitinfo.sheet.getParent().qo);
  721. }
  722. $(me._toolTipElement).html(text);
  723. //定额库定额特殊处理
  724. if($(hitinfo.sheet.getParent().qo).attr('id') === 'rationSpread'){
  725. let divWidth = $(me._fixedTipElement).width(),
  726. divHeight = $(me._fixedTipElement).height();
  727. if(divWidth > 600){
  728. divWidth = 590;
  729. $(me._toolTipElement).width(divWidth);
  730. }
  731. let top = setting.pos.y + hitinfo.y - divHeight < 0 ? 0 : setting.pos.y + hitinfo.cellRect.y - divHeight;
  732. $(me._toolTipElement).css("top", top).css("left", setting.pos.x - divWidth);
  733. }
  734. else{
  735. $(me._toolTipElement).css("top", setting.pos.y + hitinfo.y +15).css("left", setting.pos.x + hitinfo.x + 15);
  736. }
  737. //名称
  738. if(hitinfo.col === 2){
  739. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  740. zoom = hitinfo.sheet.zoom();
  741. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  742. let textLength = me.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  743. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  744. if(textLength > cellWidth){
  745. $(me._toolTipElement).css("top", setting.pos.y + hitinfo.y +15).css("left", setting.pos.x + hitinfo.x + 15);
  746. $(me._toolTipElement).show("fast");
  747. ration.tipDiv = 'show';//做个标记
  748. }
  749. }
  750. else {
  751. $(me._toolTipElement).show("fast");
  752. ration.tipDiv = 'show';//做个标记
  753. }
  754. }
  755. }
  756. }
  757. },delayTimes);
  758. };
  759. TipCellType.prototype.processMouseLeave = function (hininfo) {
  760. this.tipTimeStamp = +new Date();
  761. ration.tipDiv = 'hide';
  762. if (this._toolTipElement) {
  763. $(this._toolTipElement).hide();
  764. this._toolTipElement = null;
  765. }
  766. tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  767. }
  768. return new TipCellType();
  769. }
  770. //输出表数据(定额表)
  771. //@param {Object}sheet {Array}headers {Array}datas @return {void}
  772. function showData(sheet, headers, datas){
  773. let fuc = function () {
  774. sheet.setRowCount(datas.length);
  775. //复选框
  776. let checkBoxType = new GC.Spread.Sheets.CellTypes.CheckBox();
  777. let tipCellType = getTipCellType(sheet);
  778. sheet.setCellType(-1, 0, checkBoxType);
  779. for(let col = 0, cLen = headers.length; col < cLen; col++){
  780. for(let row = 0, rLen = datas.length; row < rLen; row++){
  781. sheet.setValue(row, col, datas[row][headers[col]['dataCode']]);
  782. if(col === 1){
  783. sheet.setTag(row, col, datas[row]['hint']);
  784. }
  785. }
  786. }
  787. sheet.setCellType(-1, 1, tipCellType);
  788. sheet.setCellType(-1, 2, tipCellType);
  789. };
  790. renderSheetFunc(sheet, fuc);
  791. }
  792. //根据定额章节树ID获取定额(从数据缓存中获取,定额数据一开始一次性拉取)
  793. //@param {Number}sectionId {Array}rations @return {Array}
  794. function getRationsBySectionId(sectionId, rations) {
  795. if(!sectionId || !rations){
  796. return [];
  797. }
  798. return _.filter(rations, {sectionId});
  799. }
  800. //定额章节树焦点控制
  801. //@param {Number}row @return {void}
  802. function sectionInitSel(row) {
  803. let rationSheet = ration.workBook.getActiveSheet();
  804. let sectionNode = section.tree ? section.tree.items[row] : null;
  805. if(sectionNode && sectionNode.children.length === 0){
  806. let sectionRations = getRationsBySectionId(sectionNode.data.ID, ration.datas);
  807. ration.cache = sectionRations;
  808. showData(rationSheet, ration.headers, sectionRations);
  809. }
  810. else {
  811. cleanData(rationSheet, ration.headers, 0);
  812. }
  813. }
  814. //初始化定额条目
  815. //@param {Number}rationLibId @return {void}
  816. function initRationItems(rationLibId, successCb){
  817. $.bootstrapLoading.start();
  818. //获取定额章节树
  819. let sectionSheet = section.workBook.getActiveSheet();
  820. CommonAjax.post('/rationRepository/api/getRationTree', {rationLibId: rationLibId}, function (sectionDatas) {
  821. //获取所有定额数据
  822. let reqEntity = {rationLibId: rationLibId, showHint: true, returnFields: '-_id code ID sectionId name unit basePrice rationGljList jobContent annotation'};
  823. CommonAjax.post('/rationRepository/api/getRationItemsByLib', reqEntity, function (rstData) {
  824. section.cache = sectionDatas;
  825. initTree(section, section.workBook.getActiveSheet(), section.treeSetting, sectionDatas);
  826. //初始焦点在第一行(切换库)
  827. sectionSheet.setActiveCell(0, 0);
  828. sortByCode(rstData);
  829. /* rstData.sort(function (a, b) {
  830. let rst = 0;
  831. if(a.code > b.code){
  832. rst = 1;
  833. }
  834. else if(a.code < b.code){
  835. rst = -1;
  836. }
  837. return rst;
  838. }); */
  839. ration.datas = rstData;
  840. sectionInitSel(0);
  841. $.bootstrapLoading.end();
  842. if (successCb) {
  843. successCb();
  844. }
  845. }, function () {
  846. $.bootstrapLoading.end();
  847. });
  848. }, function () {
  849. $.bootstrapLoading.end();
  850. });
  851. }
  852. // 可用的定额库ID
  853. const rationLibIDs = [];
  854. //初始化定额库选择
  855. //@param {String}compilationId @return {void}
  856. function initRationLibs(compilationId){
  857. CommonAjax.post('/rationRepository/api/getRationLibsByCompilation', {compilationId: compilationId}, function (rstData) {
  858. $('#rationLibSel').empty();
  859. for(let rationLib of rstData){
  860. rationLibIDs.push(+rationLib.ID);
  861. let opt = `<option value="${rationLib.ID}">${rationLib.dispName}</option>`;
  862. $('#rationLibSel').append(opt);
  863. }
  864. //初始选择
  865. initRationItems(parseInt($('#rationLibSel').select().val()));
  866. $('#rationLibSel').change(function () {
  867. let rationLibId = parseInt($(this).select().val());
  868. initRationItems(rationLibId);
  869. })
  870. });
  871. }
  872. // 变更定额库
  873. function changeRationLib(libID, successCb) {
  874. $('#rationLibSel').val(libID);
  875. initRationItems(libID, successCb);
  876. }
  877. // 设置清单名称文本色
  878. function setBillsForeColor(billsNodes) {
  879. const sheet = bills.workBook.getActiveSheet();
  880. renderSheetFunc(sheet, function () {
  881. for(let bills of billsNodes){
  882. const row = bills.serialNo();
  883. sheet.setStyle(row, 1, { foreColor: bills.data.hasGuide ? 'darkgreen' : 'black' });
  884. }
  885. });
  886. }
  887. //清单设置悬浮提示信息
  888. //@param {Array}billsNodes(清单节点) {Array}jobs(总的工作内容数据) {Array}items(总的项目特征数据)
  889. function setBillsHint(billsNodes, jobs, items) {
  890. let jobsMapping = {},
  891. itemsMapping = {};
  892. for(let job of jobs){
  893. jobsMapping[job.id] = job;
  894. }
  895. for(let item of items){
  896. itemsMapping[item.id] = item;
  897. }
  898. let tagInfo = [];
  899. for(let billsNode of billsNodes){
  900. let hintArr = [];
  901. let billsItems = billsNode.data.items;
  902. if(billsItems.length > 0){
  903. //项目特征
  904. hintArr.push('项目特征:');
  905. }
  906. let itemCount = 1,
  907. jobCount = 1;
  908. for(let billsItem of billsItems){
  909. let itemData = itemsMapping[billsItem.id];
  910. if(itemData){
  911. //特征值
  912. let eigens = [];
  913. for(let eigen of itemData.itemValue){
  914. eigens.push(eigen.value);
  915. }
  916. eigens = eigens.join(';');
  917. hintArr.push(`${itemCount}.${itemData.content}${eigens === '' ? '' : ': ' + eigens}`);
  918. itemCount ++;
  919. }
  920. }
  921. //工作内容
  922. let billsJobs = billsNode.data.jobs;
  923. if(billsJobs.length > 0){
  924. hintArr.push('工作内容:');
  925. }
  926. for(let billsJob of billsJobs){
  927. let jobData = jobsMapping[billsJob.id];
  928. if(jobData){
  929. hintArr.push(`${jobCount}.${jobData.content}`);
  930. jobCount ++;
  931. }
  932. }
  933. /*if(billsNode.data.ruleText && billsNode.data.ruleText !== ''){
  934. hintArr.push('工程量计算规则:');
  935. hintArr.push(billsNode.data.ruleText);
  936. }
  937. if(billsNode.data.recharge && billsNode.data.recharge !== ''){
  938. hintArr.push('补注:');
  939. hintArr.push(billsNode.data.recharge);
  940. }*/
  941. if(hintArr.length > 0){
  942. tagInfo.push({row: billsNode.serialNo(), value: hintArr.join('\n')});
  943. }
  944. }
  945. let sheet = bills.workBook.getActiveSheet();
  946. renderSheetFunc(sheet, function () {
  947. for(let tagI of tagInfo){
  948. sheet.setTag(tagI.row, 0, tagI.value);
  949. }
  950. });
  951. }
  952. //初始化清单的工作内容和项目特征
  953. //@param {Number}billsLibId {Function}callback @return {void}
  954. function initJobAndCharacter(billsLibId, callback){
  955. CommonAjax.post('/stdBillsEditor/getJobContent', {billsLibId: billsLibId}, function (datas) {
  956. stdBillsJobData = datas;
  957. CommonAjax.post('/stdBillsEditor/getItemCharacter', {billsLibId: billsLibId}, function (datas) {
  958. stdBillsFeatureData = datas;
  959. if(callback){
  960. callback();
  961. }
  962. });
  963. });
  964. }
  965. let billsLibId = 0;
  966. //获取指引库信息及关联的清单
  967. //@param {Number}libID {Function}callback @return {Object}
  968. function getLibWithBills(libID, callback){
  969. CommonAjax.post('/billsGuidance/api/getLibWithBills', {libID: libID}, function (rstData) {
  970. billsLibId = rstData.guidanceLib.billsLibId;
  971. initRationLibs(rstData.guidanceLib.compilationId);
  972. bills.cache = rstData.bills;
  973. initLibName(rstData.guidanceLib.name);
  974. /*initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, bills.cache);
  975. //每一棵项目指引树挂在清单节点上
  976. for(let node of bills.tree.items){
  977. node.guidance = {tree: null, controller: null};
  978. }
  979. //默认初始节点
  980. billsInitSel(0);
  981. if(callback){
  982. callback(rstData);
  983. }*/
  984. let initDataCallback = function () {
  985. initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, bills.cache);
  986. //每一棵项目指引树挂在清单节点上
  987. for(let node of bills.tree.items){
  988. node.guidance = {tree: null, controller: null};
  989. }
  990. //默认初始节点
  991. billsInitSel(0);
  992. if(callback){
  993. callback(rstData);
  994. }
  995. };
  996. initJobAndCharacter(rstData.guidanceLib.billsLibId, initDataCallback);
  997. }, function (msg) {
  998. window.location.href = '/billsGuidance/main';
  999. });
  1000. }
  1001. //初始化并输出树
  1002. //@param {Object}module {Object}sheet {Object}treeSetting {Array}datas
  1003. function initTree(module, sheet, treeSetting, datas){
  1004. module.tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
  1005. module.controller = TREE_SHEET_CONTROLLER.createNew(module.tree, sheet, treeSetting);
  1006. module.tree.loadDatas(datas);
  1007. if(module === bills){
  1008. module.tree.roots.forEach(root => {
  1009. root.setExpanded(false);
  1010. })
  1011. }
  1012. module.controller.showTreeData();
  1013. if(module === bills){
  1014. setBillsHint(bills.tree.items, stdBillsJobData, stdBillsFeatureData);
  1015. setBillsForeColor(bills.tree.items);
  1016. }
  1017. }
  1018. //更新清单备注
  1019. function updateBillsComment(updateData, callback, errCB) {
  1020. CommonAjax.post('/stdBillsEditor/updateBills', updateData, function () {
  1021. if (callback) {
  1022. callback();
  1023. }
  1024. }, function () {
  1025. if(errCB) {
  1026. errCB();
  1027. }
  1028. });
  1029. }
  1030. //更新项目指引
  1031. //@param {Array}updateDatas {Function}callback @return {void}
  1032. function updateGuideItems(updateDatas, callback, errCB){
  1033. CommonAjax.post('/billsGuidance/api/updateItems', {updateDatas: updateDatas}, function (rstData) {
  1034. if(callback){
  1035. callback(rstData);
  1036. }
  1037. }, function () {
  1038. if(errCB) {
  1039. errCB();
  1040. }
  1041. $.bootstrapLoading.end();
  1042. });
  1043. }
  1044. //编辑后自动去除换行符回车符
  1045. const deESC = /[\n]/g;
  1046. //项目指引编辑
  1047. //@param {Object}sheet {Array}cells
  1048. function edit(sheet, cells){
  1049. let updateDatas = [];
  1050. //同步节点数据
  1051. let syncDatas = [];
  1052. for(let cell of cells){
  1053. const field = guideItem.headers[cell.col].dataCode;
  1054. let node = bills.tree.selected.guidance.tree.items[cell.row];
  1055. if (field === 'name') {
  1056. let text = sheet.getValue(cell.row, cell.col);
  1057. text = text ? text.toString() : '';
  1058. text = text.replace(deESC, '');
  1059. sheet.setValue(cell.row, cell.col, text);
  1060. if(node.data.name != text){
  1061. syncDatas.push({node: node, text: text, field, cell});
  1062. updateDatas.push({updateType: updateType.update, findData: {ID: node.getID()}, updateData: {name: text}});
  1063. }
  1064. } else if (field === 'outputItemCharacter' || field === 'required') {
  1065. const val = !sheet.getValue(cell.row, cell.col);
  1066. sheet.setValue(cell.row, cell.col, val);
  1067. syncDatas.push({node: node, text: val, field, cell });
  1068. updateDatas.push({updateType: updateType.update, findData: {ID: node.getID()}, updateData: {[field]: val}});
  1069. }
  1070. }
  1071. if(updateDatas.length > 0){
  1072. updateGuideItems(updateDatas, function () {
  1073. for(let syncData of syncDatas){
  1074. syncData.node.data[syncData.field] = syncData.text;
  1075. }
  1076. }, function () {
  1077. //失败恢复
  1078. renderSheetFunc(sheet, function () {
  1079. for(let syncData of syncDatas){
  1080. sheet.setValue(syncData.node.serialNo(), syncData.cell.col, syncData.node.data[syncData.field] ? syncData.node.data[syncData.field] : '');
  1081. }
  1082. });
  1083. });
  1084. }
  1085. }
  1086. //项目指引插入,支持一次插入多条数据
  1087. //@param {Array}datas {Boolean}tobeChild(插入成为子项) {Function}callback @return {void}
  1088. function insert(datas, tobeChild, callback = null){
  1089. $.bootstrapLoading.start();
  1090. let sheet = guideItem.workBook.getActiveSheet();
  1091. let controller = bills.tree.selected.guidance.controller;
  1092. let selected = bills.tree.selected.guidance.tree.selected;
  1093. let updateDatas = [];
  1094. //建立数组下标索引
  1095. let newDataIndex = {};
  1096. for(let i = 0; i < datas.length; i++){
  1097. let newNodeData = {
  1098. libID: libID, ID: uuid.v1(), ParentID: selected ? selected.getParentID() : -1, NextSiblingID: selected ? selected.getNextSiblingID() : -1,
  1099. billsID: bills.tree.selected.getID()
  1100. };
  1101. //定额类型插入当前工作内容焦点行,
  1102. if(selected && ((selected.data.type === itemType.job && datas[i].type === itemType.ration) || tobeChild)){
  1103. newNodeData.ParentID = selected.getID();
  1104. newNodeData.NextSiblingID = -1;
  1105. }
  1106. Object.assign(newNodeData, datas[i]);
  1107. newDataIndex[i] = newNodeData;
  1108. }
  1109. for(let i = 0; i < datas.length; i++){
  1110. //第一个节点
  1111. if(i === 0){
  1112. //非插入成子节点,更新选中节点NestSiblingID
  1113. if(selected && !((selected.data.type === itemType.job && datas[i].type === itemType.ration) || tobeChild)){
  1114. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: newDataIndex[i].ID}});
  1115. }
  1116. }
  1117. //非最后一个节点
  1118. if(i !== datas.length - 1){
  1119. newDataIndex[i].NextSiblingID = newDataIndex[i + 1].ID;
  1120. }
  1121. updateDatas.push({updateType: updateType.create, updateData: newDataIndex[i]});
  1122. }
  1123. updateGuideItems(updateDatas, function () {
  1124. for(let updateData of updateDatas){
  1125. if(updateData.updateType === updateType.create){
  1126. let newNode = controller.insertByIDS(updateData.updateData.ID, updateData.updateData.ParentID, updateData.updateData.NextSiblingID);
  1127. //同步data
  1128. Object.assign(newNode.data, updateData.updateData);
  1129. const row = newNode.serialNo();
  1130. sheet.setValue(row, 0, newNode.data.name);
  1131. if (newNode.data.outputItemCharacter !== undefined) {
  1132. sheet.setValue(row, 1, newNode.data.outputItemCharacter);
  1133. }
  1134. showCheckBox(sheet, [newNode]);
  1135. refreshBtn(newNode);
  1136. }
  1137. }
  1138. if (!bills.tree.selected.data.hasGuide && bills.tree.selected.guidance.tree.items.length) {
  1139. bills.tree.selected.data.hasGuide = true;
  1140. setBillsForeColor([bills.tree.selected]);
  1141. }
  1142. if(callback){
  1143. callback();
  1144. }
  1145. setNodesColor(sheet, bills.tree.selected.guidance.tree.items);
  1146. guideItem.workBook.focus(true);
  1147. $.bootstrapLoading.end();
  1148. });
  1149. }
  1150. // 删除定额(嵌套删除自身及后代定额)
  1151. function delRations() {
  1152. const selected = bills.tree.selected.guidance.tree.selected;
  1153. if (!selected) {
  1154. return;
  1155. }
  1156. if (selected.data.type === itemType.ration) {
  1157. del({ row: selected.serialNo(), col: 0, rowCount: 1, colCount: 1 });
  1158. return;
  1159. }
  1160. const rations = selected.getPosterity().filter(item => item.data.type === itemType.ration);
  1161. if (!rations.length) {
  1162. return;
  1163. }
  1164. const updateDatas = rations.map(item => ({ updateType: updateType.del, findData: { ID: item.data.ID } }));
  1165. const rationIDs = rations.map(r => r.data.ID);
  1166. $.bootstrapLoading.start();
  1167. updateGuideItems(updateDatas, function () {
  1168. const guideSheet = guideItem.workBook.getSheet(0);
  1169. const treeNodes = bills.tree.selected.guidance.tree.items.filter(item => !rationIDs.includes(item.data.ID));
  1170. const state = bills.tree.selected.guidance.tree.getExpState(treeNodes);
  1171. const treeData = treeNodes.map(item => item.data);
  1172. initTree(bills.tree.selected.guidance, guideSheet, guideItem.treeSetting, treeData);
  1173. bills.tree.selected.guidance.tree.setExpandedByState(bills.tree.selected.guidance.tree.items, state);
  1174. renderSheetFunc(guideSheet, function () {
  1175. TREE_SHEET_HELPER.refreshNodesVisible(bills.tree.selected.guidance.tree.roots, guideSheet, true);
  1176. });
  1177. //设置底色
  1178. setNodesColor(guideSheet, bills.tree.selected.guidance.tree.items);
  1179. //项目指引初始焦点
  1180. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  1181. $.bootstrapLoading.end();
  1182. guideItem.workBook.focus(true)
  1183. });
  1184. }
  1185. //项目指引删除操作
  1186. //@return {void}
  1187. function del(selArea = null){
  1188. $.bootstrapLoading.start();
  1189. let controller = bills.tree.selected.guidance.controller;
  1190. let selNodes = [];
  1191. let sheet = guideItem.workBook.getSheet(0);
  1192. let sel = selArea ? selArea : sheet.getSelections()[0];
  1193. if(sel){
  1194. sel.row = sel.row === -1 ? 0 : sel.row;
  1195. for(let i = 0; i < sel.rowCount; i++){
  1196. if(bills.tree.selected.guidance.tree.items[sel.row + i]){
  1197. selNodes.push(bills.tree.selected.guidance.tree.items[sel.row + i]);
  1198. }
  1199. }
  1200. }
  1201. //选中的块节点
  1202. let blockNodes = getBlockNodes(selNodes);
  1203. let updateDatas = [];
  1204. function getDelDatas(nodes){
  1205. for (let node of nodes) {
  1206. updateDatas.push({updateType: updateType.del, findData: {ID: node.getID()}});
  1207. if (node.children.length > 0) {
  1208. getDelDatas(node.children);
  1209. }
  1210. }
  1211. }
  1212. getDelDatas(blockNodes);
  1213. //更新相关的前节点
  1214. for (let node of blockNodes) {
  1215. if (node.preSibling && !blockNodes.includes(node.preSibling)) {
  1216. let next = node;
  1217. while (next.nextSibling && blockNodes.includes(next.nextSibling)) {
  1218. next = next.nextSibling;
  1219. }
  1220. updateDatas.push({updateType: updateType.update, findData: {ID: node.preSibling.getID()}, updateData: {NextSiblingID: next.getNextSiblingID()}});
  1221. }
  1222. }
  1223. updateGuideItems(updateDatas, function () {
  1224. console.log(blockNodes);
  1225. controller.m_delete(blockNodes);
  1226. guideItemInitSel(sheet.getActiveRowIndex());
  1227. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1228. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1229. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1230. if (bills.tree.selected.data.hasGuide && !bills.tree.selected.guidance.tree.items.length) {
  1231. bills.tree.selected.data.hasGuide = false;
  1232. setBillsForeColor([bills.tree.selected]);
  1233. }
  1234. $.bootstrapLoading.end();
  1235. guideItem.workBook.focus(true)
  1236. });
  1237. }
  1238. //项目指引升级
  1239. //@return {void}
  1240. function upLevel(){
  1241. $.bootstrapLoading.start();
  1242. let controller = bills.tree.selected.guidance.controller;
  1243. let selected = bills.tree.selected.guidance.tree.selected;
  1244. let updateDatas = [];
  1245. //更新父节点
  1246. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getParentID()}, updateData: {NextSiblingID: selected.getID()}});
  1247. //更新选中节点
  1248. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()},
  1249. updateData: {ParentID: selected.parent.getParentID(), NextSiblingID: selected.parent.getNextSiblingID()}});
  1250. if(selected.nextSibling && selected.children.length > 0){
  1251. //更新选中节点最末子节点
  1252. let lastChild = selected.children[selected.children.length - 1];
  1253. updateDatas.push({updateType: updateType.update, findData: {ID: lastChild.getID()}, updateData: {NextSiblingID: -1}});
  1254. }
  1255. //选中节点的所有后兄弟节点成为选中节点的子项
  1256. let selectedNextIDs = [];
  1257. let sNext = selected.nextSibling;
  1258. while(sNext){
  1259. selectedNextIDs.push(sNext.getID());
  1260. sNext = sNext.nextSibling;
  1261. }
  1262. for(let sID of selectedNextIDs){
  1263. updateDatas.push({updateType: updateType.update, findData: {ID: sID}, updateData: {ParentID: selected.getID()}});
  1264. }
  1265. updateGuideItems(updateDatas, function () {
  1266. controller.upLevel();
  1267. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1268. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1269. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1270. $.bootstrapLoading.end();
  1271. guideItem.workBook.focus(true)//31574
  1272. });
  1273. }
  1274. //项目指引降级
  1275. //@return {void}
  1276. function downLevel(){
  1277. $.bootstrapLoading.start();
  1278. let controller = bills.tree.selected.guidance.controller;
  1279. let selected = bills.tree.selected.guidance.tree.selected;
  1280. let updateDatas = [];
  1281. //更新前兄弟节点
  1282. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  1283. //更新前兄弟节点最末子节点
  1284. if(selected.preSibling.children.length > 0){
  1285. let lastChild = selected.preSibling.children[selected.preSibling.children.length - 1];
  1286. updateDatas.push({updateType: updateType.update, findData: {ID: lastChild.getID()}, updateData: {NextSiblingID: selected.getID()}});
  1287. }
  1288. //更新选中节点
  1289. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {ParentID: selected.preSibling.getID(), NextSiblingID: -1}});
  1290. updateGuideItems(updateDatas, function () {
  1291. controller.downLevel();
  1292. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1293. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1294. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1295. $.bootstrapLoading.end();
  1296. guideItem.workBook.focus(true)
  1297. });
  1298. }
  1299. //项目指引上移
  1300. //@return {void}
  1301. function upMove(){
  1302. $.bootstrapLoading.start();
  1303. let controller = bills.tree.selected.guidance.controller;
  1304. let selected = bills.tree.selected.guidance.tree.selected;
  1305. let updateDatas = [];
  1306. //更新前节点
  1307. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  1308. //更新前前节点
  1309. if(selected.preSibling.preSibling){
  1310. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.preSibling.getID()}, updateData: {NextSiblingID: selected.getID()}});
  1311. }
  1312. //更新选中节点
  1313. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: selected.preSibling.getID()}});
  1314. updateGuideItems(updateDatas, function () {
  1315. controller.upMove();
  1316. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1317. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1318. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1319. $.bootstrapLoading.end();
  1320. guideItem.workBook.focus(true)
  1321. });
  1322. }
  1323. //项目指引下移
  1324. //@return {void}
  1325. function downMove(){
  1326. $.bootstrapLoading.start();
  1327. let controller = bills.tree.selected.guidance.controller;
  1328. let selected = bills.tree.selected.guidance.tree.selected;
  1329. let updateDatas = [];
  1330. //更新下节点
  1331. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getNextSiblingID()}, updateData: {NextSiblingID: selected.getID()}});
  1332. //更新前节点
  1333. if(selected.preSibling){
  1334. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  1335. }
  1336. //更新选中节点
  1337. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: selected.nextSibling.getNextSiblingID()}});
  1338. updateGuideItems(updateDatas, function () {
  1339. controller.downMove();
  1340. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1341. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1342. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1343. $.bootstrapLoading.end();
  1344. guideItem.workBook.focus(true)
  1345. });
  1346. }
  1347. //获取定额类型的项目指引名称,通过定额转换
  1348. //@param {Object}ration @return {String}
  1349. function getRationItemName(ration){
  1350. let arr = [];
  1351. arr.push(ration.code ? ration.code : '');
  1352. arr.push(ration.name ? ration.name : '');
  1353. arr.push(ration.basePrice ? ration.basePrice : '');
  1354. let rst = arr.join(' ');
  1355. rst += `/${ration.unit ? ration.unit : ''}`;
  1356. return rst;
  1357. }
  1358. //获取选中的定额表行
  1359. //@return {Array}
  1360. function getCheckedRationRows(all){
  1361. let rst = [];
  1362. let sheet = ration.workBook.getActiveSheet();
  1363. for(let i = 0; i < sheet.getRowCount(); i++){
  1364. // 全选
  1365. if (all) {
  1366. rst.push(i);
  1367. continue;
  1368. }
  1369. let checked = sheet.getValue(i, 0);
  1370. if(checked){
  1371. rst.push(i);
  1372. }
  1373. }
  1374. return rst;
  1375. }
  1376. //清空选中定额表行
  1377. //@param {Array}rows @return {void}
  1378. function clearCheckedRation(rows) {
  1379. let sheet = ration.workBook.getActiveSheet();
  1380. renderSheetFunc(sheet, function () {
  1381. for(let row of rows){
  1382. sheet.setValue(row, 0, 0);
  1383. }
  1384. });
  1385. }
  1386. //获取要插入的定额数据
  1387. //@param {Array}rows @return {Array}
  1388. function getInsertRations(rows){
  1389. let rst = [];
  1390. //当前已存在定额
  1391. let curRationIems = [];
  1392. let selected = bills.tree.selected.guidance.tree.selected;
  1393. if(selected){
  1394. if(selected.data.type === itemType.job){
  1395. curRationIems = selected.children;
  1396. }
  1397. else {
  1398. curRationIems = selected.parent ? selected.parent.children : selected.tree.roots;
  1399. }
  1400. }
  1401. for(let row of rows){
  1402. let selRation = ration.cache[row];
  1403. if(selRation){
  1404. //添加的定额是否已存在,不重复添加
  1405. let isExist = false;
  1406. for(let curRation of curRationIems){
  1407. if(curRation.data.rationID == selRation.ID){
  1408. isExist = true;
  1409. break;
  1410. }
  1411. }
  1412. if(!isExist){
  1413. rst.push({type: itemType.ration, name: getRationItemName(selRation), rationID: selRation.ID});
  1414. }
  1415. }
  1416. }
  1417. return rst;
  1418. }
  1419. //获取块节点父项不存在于选中节点中的节点
  1420. //@param {Array}nodes(选中的节点) @return {Array}
  1421. function getBlockNodes(nodes) {
  1422. let nodeMapping = {};
  1423. for(let node of nodes){
  1424. nodeMapping[node.data.ID] = node;
  1425. }
  1426. //块节点,父项不存在于选中节点中的节点
  1427. let blockNodes = [];
  1428. for(let node of nodes){
  1429. if(!nodeMapping[node.data.ParentID]){
  1430. blockNodes.push(node);
  1431. }
  1432. }
  1433. return blockNodes;
  1434. }
  1435. //允许复制整块,如果有多个块节点,且块节点的父项不同,则不可复制
  1436. //@param {Array}nodes(块节点) @return {Boolean}
  1437. function canCopyBlock(nodes) {
  1438. if(!nodes || nodes.length === 0){
  1439. return false;
  1440. }
  1441. let pID = nodes[0].data.ParentID;
  1442. for(let node of nodes){
  1443. if(node.data.ParentID !== pID){
  1444. return false;
  1445. }
  1446. }
  1447. return true;
  1448. }
  1449. //允许粘贴整块 有粘贴数据,节点存在,如果粘贴到的节点为定额数据,粘贴数据为全定额数据
  1450. //@param {Object}node(粘贴到的节点)
  1451. function canPasteBlock(node) {
  1452. let pasteDatas = JSON.parse(getLocalCache(itemCopyBlockKey));
  1453. if(!pasteDatas || pasteDatas.length === 0){
  1454. return false;
  1455. }
  1456. if(!node){
  1457. return false;
  1458. }
  1459. //若粘贴到定额节点,则数据须全为定额
  1460. if(node.data.type === itemType.ration){
  1461. for(let data of pasteDatas){
  1462. if(data.type !== itemType.ration){
  1463. return false;
  1464. }
  1465. }
  1466. }
  1467. //若粘贴到非定额节点,则粘贴的顶层数据须全为非定额
  1468. else {
  1469. let topDatas = _.filter(pasteDatas, {ParentID: -1});
  1470. for(let topData of topDatas){
  1471. if(topData.type === itemType.ration){
  1472. return false;
  1473. }
  1474. }
  1475. }
  1476. return true;
  1477. }
  1478. //复制整块,将块节点下所有节点数据复制一份,并且重新生成ID、ParentID、NextSiblingID,使用localStorage存储
  1479. //@param {Array}nodes(块节点) @return {void}
  1480. function copyBlocks(nodes) {
  1481. nodes = _.cloneDeep(nodes);
  1482. //将块节点的ParentID暂时设置为-1
  1483. for(let topNode of nodes){
  1484. topNode.data.ParentID = -1;
  1485. }
  1486. let copyDatas = [];
  1487. let copyNodes = [];
  1488. //获取块节点包含的所有节点(包括自己)
  1489. function containNodes(nodes) {
  1490. for(let node of nodes){
  1491. copyNodes.push(node);
  1492. if(node.children.length > 0){
  1493. containNodes(node.children);
  1494. }
  1495. }
  1496. }
  1497. containNodes(nodes);
  1498. for(let node of copyNodes){
  1499. copyDatas.push(node.data);
  1500. }
  1501. console.log(`copyDatas`);
  1502. console.log(copyDatas);
  1503. setLocalCache(itemCopyBlockKey, JSON.stringify(copyDatas));
  1504. }
  1505. //粘贴整块,整块数据粘贴到相关节点,并成为其后项
  1506. //@param {Object}node(粘贴到的节点) @return {void}
  1507. function pasteBlock(node) {
  1508. let itemObj = bills.tree.selected.guidance;
  1509. let pasteDatas = JSON.parse(getLocalCache(itemCopyBlockKey));
  1510. //整理ID
  1511. let IDMapping = {};
  1512. for(let data of pasteDatas){
  1513. data.newID = uuid.v1();
  1514. IDMapping[data.ID] = data;
  1515. }
  1516. for(let data of pasteDatas){
  1517. let nextData = IDMapping[data.NextSiblingID];
  1518. data.NextSiblingID = nextData ? nextData.newID : -1;
  1519. let parentData = IDMapping[data.ParentID];
  1520. data.ParentID = parentData ? parentData.newID : -1;
  1521. }
  1522. for(let data of pasteDatas){
  1523. data.ID = data.newID;
  1524. delete data.newID;
  1525. }
  1526. let updateDatas = [];
  1527. //将最顶层的块数据的ParentID设置成粘贴到节点的ParentID,并设置新的billsID
  1528. let topDatas = _.filter(pasteDatas, {ParentID: -1});
  1529. for(let topData of topDatas){
  1530. topData.ParentID = node.getParentID();
  1531. }
  1532. //更新数据
  1533. //更新插入的最末顶层数据NextSiblingID
  1534. if(node.nextSibling){
  1535. topDatas[topDatas.length - 1].NextSiblingID = node.getNextSiblingID();
  1536. }
  1537. //新建节点
  1538. for(let data of pasteDatas){
  1539. data.libID = libID;
  1540. data.billsID = node.data.billsID;
  1541. delete data._id;
  1542. updateDatas.push({updateType: updateType.create, updateData: data});
  1543. }
  1544. console.log(`pasteDatas`);
  1545. console.log(pasteDatas);
  1546. //更新粘贴到的节点的NextSiblingID
  1547. updateDatas.push({updateType: updateType.update, findData: {ID: node.data.ID}, updateData: {NextSiblingID: topDatas[0].ID}})
  1548. $.bootstrapLoading.start();
  1549. updateGuideItems(updateDatas, function (rstData) {
  1550. $.bootstrapLoading.end();
  1551. node.data.NextSiblingID = topDatas[0].ID;
  1552. let newNodes = itemObj.tree.insertDatasTo(node.data, pasteDatas);
  1553. cleanData(guideItem.workBook.getActiveSheet(), guideItem.headers, -1);
  1554. itemObj.controller.showTreeData();
  1555. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1556. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1557. }, function () {
  1558. $.bootstrapLoading.end();
  1559. });
  1560. }
  1561. // 双击定位定额
  1562. async function locateAtRation(row) {
  1563. const node = bills.tree.selected.guidance.tree.items[row];
  1564. if (!node || !node.data.rationID) {
  1565. return;
  1566. }
  1567. const rationItem = ration.datas.find(item => item.ID === node.data.rationID);
  1568. // 当前库没有找到,需要后端查找
  1569. if (!rationItem) {
  1570. const findedRation = await ajaxPost('/rationRepository/api/getRationByID',{ ID: node.data.rationID });
  1571. if (!findedRation || !rationLibIDs.includes(findedRation.rationRepId)) {
  1572. alert('无法定位此定额,此定额已被删除');
  1573. return;
  1574. }
  1575. changeRationLib(findedRation.rationRepId, () => {
  1576. located(findedRation.ID, findedRation.sectionId);
  1577. })
  1578. } else {
  1579. located(rationItem.ID, rationItem.sectionId);
  1580. }
  1581. function located(rationID, sectionID) {
  1582. if (!section.tree) {
  1583. return;
  1584. }
  1585. const sectionNode = section.tree.nodes[section.tree.prefix + sectionID];
  1586. if (!sectionNode) {
  1587. return;
  1588. }
  1589. // 定位到对应章节树
  1590. const sectionSheet = section.workBook.getActiveSheet();
  1591. const sectionRow = sectionNode.serialNo();
  1592. expandSearchNodes(sectionSheet, [sectionNode], section.tree.roots);
  1593. sectionSheet.setSelection(sectionRow, 0, 1, 1);
  1594. sectionSheet.showRow(sectionRow, GC.Spread.Sheets.VerticalPosition.center);
  1595. sectionInitSel(sectionRow);
  1596. // 定位到对应定额
  1597. const locatedRationRow = ration.cache.findIndex(item => item.ID === rationID);
  1598. if (locatedRationRow !== -1) {
  1599. const rationSheet = ration.workBook.getActiveSheet();
  1600. rationSheet.setSelection(locatedRationRow, 1, 1, 1);
  1601. rationSheet.showRow(locatedRationRow, GC.Spread.Sheets.VerticalPosition.center);
  1602. }
  1603. }
  1604. }
  1605. //初始化右键菜单
  1606. //@return {void}
  1607. function initContextMenu() {
  1608. $.contextMenu({
  1609. selector: '#guideItemSpread',
  1610. build: function($triggerElement, e){
  1611. //控制允许右键菜单在哪个位置出现
  1612. let sheet = guideItem.workBook.getSheet(0);
  1613. let offset = $("#guideItemSpread").offset(),
  1614. x = e.pageX - offset.left,
  1615. y = e.pageY - offset.top;
  1616. let target = sheet.hitTest(x, y);
  1617. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  1618. let sel = sheet.getSelections()[0];
  1619. if(sel && sel.rowCount === 1){
  1620. sheet.setActiveCell(target.row, target.col);
  1621. }
  1622. sel = sheet.getSelections()[0];
  1623. let selNodes = [];
  1624. if(sel){
  1625. sel.row = sel.row === -1 ? 0 : sel.row;
  1626. for(let i = 0; i < sel.rowCount; i++){
  1627. if(bills.tree.selected.guidance.tree.items[sel.row + i]){
  1628. selNodes.push(bills.tree.selected.guidance.tree.items[sel.row + i]);
  1629. }
  1630. }
  1631. }
  1632. //块节点
  1633. let blockNodes = getBlockNodes(selNodes);
  1634. //右键在多选内则不重设焦点
  1635. if(!sel || sel.rowCount === 1 || !(target.row >= sel.row && target.row <= sel.row + sel.rowCount - 1)){
  1636. sheet.setActiveCell(target.row, target.col);
  1637. }
  1638. guideItemInitSel(target.row);
  1639. return {
  1640. callback: function(){},
  1641. items: {
  1642. "copy": {
  1643. name: "复制整块",
  1644. disabled: function () {
  1645. return locked || !canCopyBlock(blockNodes);
  1646. },
  1647. icon: "fa-copy",
  1648. callback: function (key, opt) {
  1649. copyBlocks(blockNodes);
  1650. }},
  1651. "paste": {
  1652. name: "粘贴整块",
  1653. disabled: function () {
  1654. let pasteNode = bills.tree.selected.guidance.tree.items[target.row];
  1655. return locked || !canPasteBlock(pasteNode);
  1656. },
  1657. icon: "fa-paste",
  1658. callback: function (key, opt) {
  1659. let pasteNode = bills.tree.selected.guidance.tree.items[target.row];
  1660. pasteBlock(pasteNode);
  1661. }},
  1662. "delRations": {
  1663. name: '删除定额',
  1664. disabled: function () {
  1665. let node = bills.tree.selected.guidance.tree.items[target.row];
  1666. return locked || !node
  1667. },
  1668. icon: "fa-remove",
  1669. callback: function (key, opt) {
  1670. $('#delRationAlert').modal('show');
  1671. }
  1672. },
  1673. "del": {
  1674. name: '删除',
  1675. disabled: function () {
  1676. let node = bills.tree.selected.guidance.tree.items[target.row];
  1677. return locked || !node
  1678. },
  1679. icon: "fa-remove",
  1680. callback: function (key, opt) {
  1681. $('#delAlert').modal('show');
  1682. }
  1683. },
  1684. "insertSibling": {
  1685. name: '插入行',
  1686. disabled: function () {
  1687. let node = bills.tree.selected.guidance.tree.items[target.row];
  1688. return locked || !node || node.data.type !== itemType.job;
  1689. },
  1690. icon: "fa-arrow-left",
  1691. callback: function (key, opt) {
  1692. insert([{type: itemType.job, name: ''}], false);
  1693. }
  1694. },
  1695. "insertChild": {
  1696. name: '插入子项',
  1697. disabled: function () {
  1698. let node = bills.tree.selected.guidance.tree.items[target.row];
  1699. return locked || !node || node.data.type !== itemType.job || !allJobChildren(node);
  1700. },
  1701. icon: 'fa-arrow-left',
  1702. callback: function (key, opt) {
  1703. insert([{type: itemType.job, name: ''}], true);
  1704. }
  1705. }
  1706. }
  1707. };
  1708. }
  1709. else{
  1710. return false;
  1711. }
  1712. }
  1713. });
  1714. }
  1715. // 替换定额
  1716. function replaceRation(newRation, orgItem) {
  1717. if (newRation.ID === orgItem.data.rationID) {
  1718. return;
  1719. }
  1720. $.bootstrapLoading.start();
  1721. const updateAttrs = {name: getRationItemName(newRation), rationID: newRation.ID};
  1722. const updateData = [{updateType: updateType.update, findData: {ID: orgItem.getID()}, updateData: updateAttrs }];
  1723. updateGuideItems(updateData, function () {
  1724. Object.assign(orgItem.data, updateAttrs);
  1725. const row = orgItem.serialNo();
  1726. guideItem.workBook.getSheet(0).setValue(row, 0, orgItem.data.name);
  1727. $.bootstrapLoading.end();
  1728. });
  1729. }
  1730. // 初始化定额右键菜单
  1731. function initRationContextMenu() {
  1732. $.contextMenu({
  1733. selector: '#rationSpread',
  1734. build: function($triggerElement, e){
  1735. //控制允许右键菜单在哪个位置出现
  1736. let sheet = ration.workBook.getActiveSheet();;
  1737. let offset = $("#rationSpread").offset(),
  1738. x = e.pageX - offset.left,
  1739. y = e.pageY - offset.top;
  1740. let target = sheet.hitTest(x, y);
  1741. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  1742. let sel = sheet.getSelections()[0];
  1743. if(sel && sel.rowCount === 1){
  1744. sheet.setActiveCell(target.row, target.col);
  1745. }
  1746. sel = sheet.getSelections()[0];
  1747. if(sel){
  1748. sel.row = sel.row === -1 ? 0 : sel.row;
  1749. }
  1750. //右键在多选内则不重设焦点
  1751. if(!sel || sel.rowCount === 1 || !(target.row >= sel.row && target.row <= sel.row + sel.rowCount - 1)){
  1752. sheet.setActiveCell(target.row, target.col);
  1753. }
  1754. return {
  1755. callback: function(){},
  1756. items: {
  1757. "replace": {
  1758. name: "替换定额",
  1759. disabled: function () {
  1760. return locked || !bills.tree.selected || !bills.tree.selected.guidance || !bills.tree.selected.guidance.tree || !bills.tree.selected.guidance.tree.selected || bills.tree.selected.guidance.tree.selected.data.type !== itemType.ration;
  1761. },
  1762. icon: "fa-copy",
  1763. callback: function (key, opt) {
  1764. const row = sheet.getActiveRowIndex();
  1765. if (!ration.cache[row]) {
  1766. return;
  1767. }
  1768. replaceRation(ration.cache[row], bills.tree.selected.guidance.tree.selected);
  1769. // copyBlocks(blockNodes);
  1770. }},
  1771. }
  1772. };
  1773. }
  1774. else{
  1775. return false;
  1776. }
  1777. }
  1778. });
  1779. }
  1780. //展开至搜索出来点的节点
  1781. //@param {Array}nodes @return {void}
  1782. function expandSearchNodes(sheet, nodes, roots){
  1783. renderSheetFunc(sheet, function () {
  1784. function expParentNode(node){
  1785. if(node.parent){
  1786. if (!node.parent.expanded) {
  1787. node.parent.setExpanded(true);
  1788. }
  1789. expParentNode(node.parent);
  1790. }
  1791. }
  1792. for(let node of nodes){
  1793. expParentNode(node);
  1794. }
  1795. TREE_SHEET_HELPER.refreshNodesVisible(roots, sheet, true);
  1796. });
  1797. }
  1798. // 清空搜索高亮
  1799. function clearHighLight(sheet) {
  1800. renderSheetFunc(sheet, () => {
  1801. for (let i = 0; i < sheet.getRowCount(); i++){
  1802. setBgColor(sheet, i, 'white')
  1803. }
  1804. });
  1805. }
  1806. // 清空搜索状态
  1807. // 关闭搜索清单结果
  1808. function closeSearchBills(sheet) {
  1809. if (!bills.tree) {
  1810. return;
  1811. }
  1812. $('#searchBillsResult').hide();
  1813. bills.tree.items.forEach(node => {
  1814. node.isSearch = false;
  1815. });
  1816. clearHighLight(sheet);
  1817. setBgColor(sheet, sheet.getActiveRowIndex(), selectedBgColor);
  1818. }
  1819. // 搜索清单
  1820. function searchBills() {
  1821. if (!bills.tree || !bills.workBook) {
  1822. return;
  1823. }
  1824. const sheet = bills.workBook.getActiveSheet();
  1825. const str = $('#searchBillText').val().trim();
  1826. // 空搜索字符,关闭搜索
  1827. if (!str) {
  1828. if ($('#searchBillsResult').is(':visible')) {
  1829. closeSearchBills(sheet);
  1830. }
  1831. return;
  1832. }
  1833. // 过滤清单
  1834. const result = bills.tree.items.filter(item => {
  1835. const codeIs = item.data.code ? item.data.code.indexOf(str) !== -1 : false;
  1836. const nameIs = item.data.name ? item.data.name.indexOf(str) !== -1 : false;
  1837. return codeIs || nameIs;
  1838. });
  1839. if (!result.length) {
  1840. closeSearchBills(sheet);
  1841. return;
  1842. }
  1843. // 显示搜索结果
  1844. $("#searchBillsResult").show();
  1845. $('#searchBillsCount').text(result.length);
  1846. //展开搜索出来的节点
  1847. expandSearchNodes(sheet, result, bills.tree.roots);
  1848. // 标黄结果
  1849. clearHighLight(sheet);
  1850. const col = sheet.getActiveColumnIndex();
  1851. renderSheetFunc(sheet, function () {
  1852. bills.controller.setTreeSelected(result[0]);
  1853. bills.tree.items.forEach(node => {
  1854. if (result.includes(node)) {
  1855. setBgColor(sheet, node.serialNo(), searchBgColor);
  1856. node.isSearch = true; // 标记为搜索结果,防止被焦点行变更恢复颜色
  1857. } else {
  1858. node.isSearch = false;
  1859. }
  1860. })
  1861. });
  1862. //搜索初始定位
  1863. const row = sheet.getActiveRowIndex();
  1864. sheet.setSelection(result[0].serialNo(), col, 1, 1);
  1865. billsInitSel(result[0].serialNo(), { row });
  1866. sheet.showRow(result[0].serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  1867. let curIndex = 0;
  1868. // 上一条
  1869. $('#preBill').unbind('click');
  1870. $('#preBill').bind('click', () => {
  1871. const node = result[curIndex - 1];
  1872. if (!node) {
  1873. return;
  1874. }
  1875. curIndex -= 1;
  1876. const col = sheet.getActiveColumnIndex();
  1877. const row = node.serialNo();
  1878. const orgRow = sheet.getActiveRowIndex();
  1879. sheet.setSelection(row, col, 1, 1);
  1880. billsInitSel(row, { row: orgRow });
  1881. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  1882. });
  1883. // 下一条
  1884. $('#nextBills').unbind('click');
  1885. $('#nextBills').bind('click', () => {
  1886. const node = result[curIndex + 1];
  1887. if (!node) {
  1888. return;
  1889. }
  1890. curIndex += 1;
  1891. const col = sheet.getActiveColumnIndex();
  1892. const row = node.serialNo();
  1893. const orgRow = sheet.getActiveRowIndex();
  1894. sheet.setSelection(row, col, 1, 1);
  1895. billsInitSel(row, { row: orgRow });
  1896. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  1897. });
  1898. }
  1899. //初始化个按钮点击
  1900. //@return {void}
  1901. function initBtn(){
  1902. $('#insert').click(function () {
  1903. insert([{type: itemType.job, name: '', outputItemCharacter: true }], false);
  1904. });
  1905. $('#delConfirm').click(function () {
  1906. del();
  1907. $('#delAlert').modal('hide');
  1908. });
  1909. $('#delRationConfirm').click(function () {
  1910. delRations();
  1911. $('#delRationAlert').modal('hide');
  1912. });
  1913. $('#del').click(function () {
  1914. $('#delAlert').modal('show');
  1915. });
  1916. $('#upLevel').click(function () {
  1917. upLevel();
  1918. });
  1919. $('#downLevel').click(function () {
  1920. downLevel();
  1921. });
  1922. $('#upMove').click(function () {
  1923. upMove();
  1924. });
  1925. $('#downMove').click(function () {
  1926. downMove();
  1927. });
  1928. // 收起清单至第二层
  1929. $('#expandToSecond').click(function() {
  1930. if (!bills.tree || !bills.workBook) {
  1931. return;
  1932. }
  1933. bills.tree.roots.forEach(root => {
  1934. root.setExpanded(true);
  1935. root.children.forEach(item => {
  1936. item.setExpanded(false);
  1937. });
  1938. });
  1939. const billSheet = bills.workBook.getActiveSheet();
  1940. renderSheetFunc(billSheet, function () {
  1941. TREE_SHEET_HELPER.refreshNodesVisible(bills.tree.roots, billSheet, true);
  1942. });
  1943. })
  1944. //收起定额、展开全部
  1945. $('#expandContract').click(function () {
  1946. //目前状态时展开全部节点状态,点击则收起定额
  1947. let tree = bills.tree.selected.guidance.tree,
  1948. itemSheet = guideItem.workBook.getActiveSheet();
  1949. if(curExpandState === itemExpandState.expand){
  1950. curExpandState = itemExpandState.contract;
  1951. $(this).html('<i class="fa fa-plus-square-o" aria-hidden="true"></i> 展开全部');
  1952. setNodesExpandState(tree.items, itemExpandState.contract);
  1953. } else {
  1954. curExpandState = itemExpandState.expand;
  1955. $(this).html('<i class="fa fa-minus-square-o" aria-hidden="true"></i> 收起定额');
  1956. setNodesExpandState(tree.items, itemExpandState.expand);
  1957. }
  1958. renderSheetFunc(itemSheet, function () {
  1959. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, itemSheet, true);
  1960. });
  1961. });
  1962. // 插入选中定额
  1963. $('#insertRation').click(function () {
  1964. let checkedRows = getCheckedRationRows();
  1965. let insertDatas = getInsertRations(checkedRows);
  1966. if(insertDatas.length > 0){
  1967. insert(insertDatas, false, function () {
  1968. //清空选择
  1969. clearCheckedRation(checkedRows);
  1970. });
  1971. }
  1972. else {
  1973. clearCheckedRation(checkedRows);
  1974. }
  1975. });
  1976. // 插入全部定额
  1977. $('#insertAll').click(function () {
  1978. let isAll = true;
  1979. let checkedRows = getCheckedRationRows(isAll);
  1980. let insertDatas = getInsertRations(checkedRows);
  1981. if(insertDatas.length > 0){
  1982. insert(insertDatas, false);
  1983. }
  1984. });
  1985. // 搜索清单
  1986. $("#searchBillBtn").click(searchBills);
  1987. $('#searchBillText').keyup(function (e) {
  1988. delayKeyup(function () {
  1989. $('#searchBillBtn').click();
  1990. });
  1991. });
  1992. // 关闭搜索清单
  1993. $('#closeSearchBills').click(() => {
  1994. if (!bills.workBook) {
  1995. return;
  1996. }
  1997. const sheet = bills.workBook.getActiveSheet();
  1998. closeSearchBills(sheet);
  1999. })
  2000. //搜索定额
  2001. $('#searchBtn').click(function () {
  2002. let searchStr = $('#searchText').val();
  2003. if(!searchStr || searchStr === ''){
  2004. ration.cache = ration.datas;
  2005. }
  2006. else{
  2007. let reg = new RegExp(searchStr, 'i');
  2008. ration.cache = _.filter(ration.datas, function (data) {
  2009. return reg.test(data.code) || reg.test(data.name);
  2010. });
  2011. }
  2012. $('.top-content').hide();
  2013. $('#deResize').hide();
  2014. $('#searchCount').text(`搜索结果: ${ration.cache.length}`);
  2015. $('#rationSearchResult').show();
  2016. autoFlashHeight();
  2017. ration.workBook.refresh();
  2018. let rationSheet = ration.workBook.getActiveSheet();
  2019. renderSheetFunc(rationSheet, function () {
  2020. clearCheckedRation(getCheckedRationRows());
  2021. showData(rationSheet, ration.headers, ration.cache);
  2022. })
  2023. });
  2024. //关闭搜索
  2025. $('#rationSearchResult a').click(function () {
  2026. $('.top-content').show();
  2027. $('#deResize').show();
  2028. $('#rationSearchResult').hide();
  2029. autoFlashHeight();
  2030. renderSheetFunc(ration.workBook.getActiveSheet(), function () {
  2031. clearCheckedRation(getCheckedRationRows());
  2032. });
  2033. section.workBook.refresh();
  2034. ration.workBook.refresh();
  2035. $('#searchText').val('');
  2036. //恢复章节树下的定额
  2037. sectionInitSel(section.workBook.getActiveSheet().getActiveRowIndex());
  2038. });
  2039. let keyupTime = 0,
  2040. delayTime = 500;
  2041. function delayKeyup(callback) {
  2042. let nowTime = Date.now();
  2043. keyupTime = nowTime;
  2044. setTimeout(function () {
  2045. if (nowTime - keyupTime == 0) {
  2046. callback();
  2047. }
  2048. }, delayTime);
  2049. }
  2050. //执行搜索
  2051. $('#searchText').keyup(function (e) {
  2052. delayKeyup(function () {
  2053. $('#searchBtn').click();
  2054. });
  2055. });
  2056. //编辑清单备注
  2057. $('.main-side-bottom').find('textarea').keyup(function () {
  2058. let me = this;
  2059. let node = bills.tree.selected;
  2060. let comment = $(me).val();
  2061. delayKeyup(function () {
  2062. if (node) {
  2063. let updateData = {lastOperator: userAccount, billsLibId: billsLibId, updateId: node.getID(), field: 'comment', data: comment};
  2064. updateBillsComment(updateData, function () {
  2065. node.data.comment = comment;
  2066. }, function() {
  2067. $(me).val(node.data.comment || '');
  2068. })
  2069. }
  2070. });
  2071. });
  2072. //编辑选项备注
  2073. $('.main-bottom-content').find('textarea').keyup(function () {
  2074. let me = this;
  2075. let node = bills.tree.selected.guidance.tree.selected;
  2076. let comment = $(me).val();
  2077. delayKeyup(function () {
  2078. if(node){
  2079. let updateDatas = [{updateType: updateType.update, findData: {ID: node.getID()}, updateData: {comment: comment}}];
  2080. updateGuideItems(updateDatas, function (rstData) {
  2081. node.data.comment = comment;
  2082. }, function() {
  2083. $(me).val(node.data.comment || '');
  2084. });
  2085. }
  2086. });
  2087. });
  2088. //定额高度拖动调整
  2089. let heightEleObj = {
  2090. module: moduleName,
  2091. resize: $('#deResize'),
  2092. top: $('#topContent'),
  2093. topSpread: $('#sectionSpread'),
  2094. bottom: $('#bottomContent'),
  2095. bottomSpread: $('#rationSpread')
  2096. },
  2097. heightLimit = {
  2098. min: 150,
  2099. max: `$(window).height()-$('.header').height()-$('.sidebar-tools-bar').height()-150-10`,
  2100. notTopSpread: 0,
  2101. notBottomSpread: 0,
  2102. };
  2103. SlideResize.verticalSlide(heightEleObj, heightLimit, function () {
  2104. if(section.workBook){
  2105. section.workBook.refresh();
  2106. }
  2107. if(ration.workBook){
  2108. ration.workBook.refresh();
  2109. }
  2110. });
  2111. /*slideResize(rationLibResizeEles, {min: 147, max: 680}, 'height', function() {
  2112. //autoFlashHeight();
  2113. if(section.workBook){
  2114. section.workBook.refresh();
  2115. }
  2116. if(ration.workBook){
  2117. ration.workBook.refresh();
  2118. }
  2119. });*/
  2120. //左右拖动
  2121. //清单表与项目指引表
  2122. let leftElesObj = {};
  2123. leftElesObj.module = moduleName;
  2124. leftElesObj.resize = $('#slideResizeLeft');
  2125. leftElesObj.parent = $('#dataRow');
  2126. leftElesObj.left = $('#leftContent');
  2127. leftElesObj.right = $('#midContent');
  2128. SlideResize.horizontalSlide(leftElesObj, {min: 200, max: `$('#dataRow').width() - $('#rightContent').width() - 200`}, function () {
  2129. refreshALlWorkBook();
  2130. });
  2131. //人材机表与人材机组成物表
  2132. let rightElesObj = {};
  2133. rightElesObj.module = moduleName;
  2134. rightElesObj.resize = $('#slideResizeRight');
  2135. rightElesObj.parent = $('#dataRow');
  2136. rightElesObj.left = $('#midContent');
  2137. rightElesObj.right = $('#rightContent');
  2138. SlideResize.horizontalSlide(rightElesObj, {min: 200, max: `$('#dataRow').width() - $('#leftContent').width() - 200`}, function () {
  2139. refreshALlWorkBook();
  2140. });
  2141. }
  2142. //刷新全部工作簿
  2143. //@return {void}
  2144. function refreshALlWorkBook() {
  2145. if (bills.workBook) {
  2146. bills.workBook.refresh();
  2147. }
  2148. if (guideItem.workBook) {
  2149. guideItem.workBook.refresh();
  2150. }
  2151. if (section.workBook) {
  2152. section.workBook.refresh();
  2153. }
  2154. if (ration.workBook) {
  2155. ration.workBook.refresh();
  2156. }
  2157. $('.main-side-bottom').find('textarea').height($('.main-side-bottom').height() - 20);
  2158. $('.main-side-bottom').find('textarea').width($('.main-side-bottom').width() - 25);
  2159. $('.main-bottom-content').find('textarea').height($('.main-bottom-content').height() - 20);
  2160. $('.main-bottom-content').find('textarea').width($('.main-bottom-content').width() - 25);
  2161. }
  2162. //读取拖动相关
  2163. //@return {void}
  2164. function initSlideSize() {
  2165. //定额表上下
  2166. let heightEleObj = {
  2167. module: moduleName,
  2168. top: $('#topContent'),
  2169. topSpread: $('#sectionSpread'),
  2170. bottom: $('#bottomContent'),
  2171. bottomSpread: $('#rationSpread')
  2172. };
  2173. if (!$('#rationSearchResult').is(':visible')) {
  2174. SlideResize.loadVerticalHeight(heightEleObj.module, heightEleObj,
  2175. {totalHeight: `$(window).height()-$('.header').height()-$('.sidebar-tools-bar').height()-10`,
  2176. notTopSpread: 0, notBottomSpread: 0}, function () {
  2177. if(section.workBook){
  2178. section.workBook.refresh();
  2179. }
  2180. if(ration.workBook){
  2181. ration.workBook.refresh();
  2182. }
  2183. });
  2184. }
  2185. //水平
  2186. SlideResize.loadHorizonWidth(moduleName, [$('#slideResizeLeft'), $('#slideResizeRight')], [$('#leftContent'), $('#midContent'), $('#rightContent')], function () {
  2187. refreshALlWorkBook();
  2188. });
  2189. }
  2190. //初始化视图
  2191. //@param {void} @return {void}
  2192. function initViews(){
  2193. let modules = [bills, guideItem, section, ration];
  2194. initWorkBooks(modules);
  2195. lockUtil.lockTools($(document.body), locked);
  2196. getLibWithBills(libID);
  2197. initBtn();
  2198. initContextMenu();
  2199. initRationContextMenu();
  2200. initSlideSize();
  2201. }
  2202. return {initViews, initSlideSize};
  2203. })();
  2204. $(document).ready(function () {
  2205. billsGuidance.initViews();
  2206. });