block_controller.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /**
  2. * Created by zhang on 2018/5/10.
  3. */
  4. let BlockController = {
  5. datas:[],
  6. disabelCopyRightClick:function(){
  7. if (projectReadOnly) {
  8. return true;
  9. }
  10. let selection = projectObj.mainSpread.getActiveSheet().getSelections()[0];
  11. let firstNode = projectObj.project.mainTree.items[selection.row];//当多选的情况,用mainTree.selected判断不正确,要用第一个选中的节点
  12. for(let i = 0;i< selection.rowCount;i++){ //多选的时候判断所有与第一个节点同级的节点
  13. let temNode = projectObj.project.mainTree.items[selection.row + i];
  14. if(firstNode.getParentID() == temNode.getParentID()){
  15. if(BlockController.copyBtnDisable(temNode) == true){
  16. return true;
  17. }
  18. }
  19. }
  20. return false;
  21. },
  22. copyRightClickCallback:function(){
  23. $.bootstrapLoading.start();
  24. //let selected = project.mainTree.selected;
  25. let selections = projectObj.mainSpread.getActiveSheet().getSelections();
  26. setTimeout(function () {
  27. BlockController.copyBlock(selections[0]);
  28. $.bootstrapLoading.end();
  29. },100)
  30. },
  31. copyBtnDisable:function (selected) {
  32. if(this.isDXFYorMainEq(selected)){
  33. return true;
  34. }
  35. if(selected.sourceType == ModuleNames.ration && selected.data.type == rationType.install){
  36. return true;
  37. }
  38. if (OVER_HEIGHT.isOverHeight(selected)) {
  39. return true;
  40. }
  41. if(projectObj.project.Bills.hasFlags(selected)){//所有固定清单项不能复制
  42. return true;
  43. }
  44. //
  45. return false;
  46. },
  47. pasteBtnDisable:function (selected) {
  48. let blockData = this.getBlockData();
  49. let Bills = projectObj.project.Bills;
  50. if(this.isDXFYorMainEq(selected)){
  51. return true;
  52. }
  53. if(blockData == null){
  54. return true;
  55. }
  56. if(blockData.firstNodeType != blockType.RATION ){//复制的不是定额
  57. //&& blockData.isFBFX != Bills.isFBFX(selected)
  58. if (this.blockIsFB(blockData.firstNodeType) && !Bills.isFBFX(selected)) return true; //第一层是分部,不允许复制到非分部分项
  59. //分项\补项不允许粘贴到“施工组织措施项目”下
  60. if (this.blockIsFXorBX(blockData.firstNodeType) && Bills.isOrgMeasure(selected)) return true;
  61. if (blockData.firstNodeType == blockType.BILL && Bills.isFBFX(selected) ) { //如果是清单 到分部分项
  62. for (let d of blockData.datas) {
  63. //如果清单有多层,不允许复制
  64. if (!this.blockIsLeaveBills(d)) return true;
  65. ///如果有清单基数,不允许复制
  66. if (this.ifCalcBase(d.calcBase)) return true;
  67. //如果清单有费率引用,不允许复制
  68. if (gljUtil.isNotEmpty(d.feeRateID)) return true;
  69. }
  70. }
  71. }
  72. if(this.blockIsFXorBX(blockData.firstNodeType) && this.isFB(selected)){//复制块的第一层是分项或补项,焦点行是分部,且分部有子项并且子项不是分项或补项,则无效。
  73. if(selected.children.length && !this.isFXorBX(selected.children[0])){
  74. return true;
  75. }
  76. }
  77. if(this.isFXorBX(selected) && blockData.firstNodeType == blockType.FB){//焦点行是分项或补项,复制块的第一层是分部,则无效。
  78. return true;
  79. }
  80. if(blockData.firstNodeType == blockType.RATION && selected.sourceType == Bills.getSourceType()){ //复制的是定额/量价/工料机、焦点行是分部/清单父项则无效
  81. if( selected.data.type == billType.FB ){
  82. return true;
  83. }
  84. if(MainTreeCol.readOnly.billsParent(selected)){
  85. return true;
  86. }
  87. if(this.haveCalcBase(selected)){//有使用基数计算,disable
  88. return true;
  89. }
  90. }
  91. if(blockData.firstNodeType != blockType.RATION && selected.sourceType == ModuleNames.ration){
  92. return true;
  93. }
  94. return false;
  95. },
  96. // 恢复删除节点有效性
  97. recoverBlockDisabled: function () {
  98. if (projectReadOnly) {
  99. return true;
  100. }
  101. const recycle = getLocalCache('project_recycle');
  102. if (!recycle) {
  103. return true;
  104. }
  105. const recycleObj = JSON.parse(recycle);
  106. const firstData = recycleObj.datas.length ? recycleObj.datas[0] : null;
  107. if (!firstData || firstData.projectID !== projectObj.project.ID()) {
  108. return true;
  109. }
  110. const parentID = firstData.sourceType === projectObj.project.Bills.getSourceType() ? firstData.ParentID : firstData.billsItemID;
  111. const parent = projectObj.project.mainTree.findNode(parentID);
  112. // parentID为-1时,为大项费用
  113. if (parentID !== -1 && !parent) {
  114. return true;
  115. }
  116. return false;
  117. },
  118. haveCalcBase : function (node) {
  119. return this.ifCalcBase(node.data.calcBase);
  120. },
  121. ifCalcBase: function (calcBase) {
  122. if (calcBase == null || calcBase == undefined || calcBase == "") {
  123. return false;
  124. }
  125. return true;
  126. },
  127. isDXFYorMainEq:function (node) {//焦点行是大项费用或定额下的主材设备
  128. if(node.sourceType == ModuleNames.bills && node.data.type == billType.DXFY){//焦点行是大项费用则无效;
  129. return true;
  130. }
  131. if(node.sourceType == ModuleNames.ration_glj){// 焦点行是定额下的主材设备则无效;
  132. return true;
  133. }
  134. return false;
  135. },
  136. getBlockData:function () {
  137. let blockString =getLocalCache('project_block');
  138. if(blockString !=null){
  139. return JSON.parse(blockString);
  140. }
  141. return null;
  142. },
  143. extractBlockData: function (selection) {
  144. let startTime = +new Date();
  145. let blockObj = {
  146. compilationID:projectObj.project.projectInfo.compilation,
  147. datas:[]
  148. };
  149. let firstNode = projectObj.project.mainTree.items[selection.row];
  150. let copyNodes = [firstNode];
  151. if(selection.rowCount > 1){
  152. for(let i = 1;i<selection.rowCount;i ++){
  153. let temNode = projectObj.project.mainTree.items[selection.row + i];
  154. if(this.copyBtnDisable(temNode) == false&&temNode.getParentID() != -1 && temNode.getParentID() == firstNode.getParentID() ){//多选时,与选中的第一个节点同一层次的才复制,即父节点一样的,其它的忽略,copyBtnDisable 判断要为false
  155. copyNodes.push(temNode);
  156. }
  157. }
  158. }
  159. if(firstNode.sourceType == projectObj.project.Bills.getSourceType()){
  160. // 特殊处理大项费用,因为DXFY在billType中为1,与blockType中的RATION冲突了
  161. blockObj.firstNodeType = firstNode.data.type === billType.DXFY ? blockType.DXFY : firstNode.data.type;
  162. }else if(firstNode.sourceType == projectObj.project.Ration.getSourceType()){
  163. blockObj.firstNodeType = blockType.RATION;
  164. }
  165. blockObj.isFBFX = projectObj.project.Bills.isFBFX(firstNode);
  166. let ration_glj_Map = _.groupBy(projectObj.project.ration_glj.datas, 'rationID');
  167. for(let cNode of copyNodes){
  168. blockObj.datas.push(this.getNodeDatas(cNode,ration_glj_Map));
  169. }
  170. let getDataTime = +new Date();
  171. console.log(`复制所用时间——${getDataTime - startTime}`);
  172. blockObj.copyTime = +new Date();//设置复制时间,可以用来做过期处理
  173. setLocalCache('project_block',JSON.stringify(blockObj));
  174. let endTime = +new Date();
  175. console.log(`复制所用时间——${endTime - startTime}`);
  176. return blockObj;
  177. },
  178. copyBlock:function (selection) {
  179. const block = this.extractBlockData(selection);
  180. setLocalCache('project_block', JSON.stringify(block));
  181. },
  182. recycleBlock: function (selection) {
  183. const block = this.extractBlockData(selection);
  184. setLocalCache('project_recycle', JSON.stringify(block));
  185. },
  186. getNodeDatas : function (node,ration_glj_Map) {
  187. let data = node.data;
  188. data.sourceType = node.sourceType;
  189. if(node.sourceType == projectObj.project.Bills.getSourceType()){
  190. data.quantity_details = projectObj.project.quantity_detail.getDetailByBillID(data.ID);
  191. }
  192. if(node.sourceType == projectObj.project.Ration.getSourceType()){
  193. if(node.data.type == rationType.install){ //如果是生成的安装增加费,则跳过
  194. return null;
  195. }
  196. let gljList = ration_glj_Map[data.ID]?ration_glj_Map[data.ID]:[];//gljOprObj.filterGljByRation(node.data, datas);
  197. gljList = gljOprObj.combineWithProjectGlj(gljList,false);
  198. data.ration_gljs = gljList;
  199. data.ration_coes = projectObj.project.ration_coe.getCoeByRationID(data.ID);
  200. data.ration_installations = projectObj.project.ration_installation.getInstallationByRationID(data.ID);
  201. data.quantity_details = projectObj.project.quantity_detail.getDetailByRationID(data.ID);
  202. data.ration_template = projectObj.project.ration_template.getTemplateByRationID(data.ID);
  203. }else if(node.children.length>0){
  204. let children = [];
  205. for(let c of node.children){
  206. let tem_data = this.getNodeDatas(c,ration_glj_Map);
  207. tem_data?children.push(tem_data):'';
  208. }
  209. data.children = children;
  210. }
  211. return data;
  212. },
  213. pasteBlock:function (selected) {
  214. let blockData = this.getBlockData();
  215. let position = this.initOptionDiv(selected,blockData);
  216. if(position == null){
  217. return;
  218. }
  219. this.confirmPaste(blockData,selected,position);
  220. //do paste
  221. },
  222. // 恢复删除节点
  223. recoverBlock: function () {
  224. const recycle = JSON.parse(getLocalCache('project_recycle'));
  225. const firstData = recycle.datas[0];
  226. const parentID = firstData.sourceType === projectObj.project.Bills.getSourceType() ? firstData.ParentID : firstData.billsItemID;
  227. // 恢复到父节点最末子项
  228. if (parentID !== -1) {
  229. const parent = projectObj.project.mainTree.findNode(parentID);
  230. this.confirmPaste(recycle, parent, 'sub');
  231. } else {
  232. // 恢复到最末根节点后项
  233. const lastRoot = projectObj.project.mainTree.roots[projectObj.project.mainTree.roots.length - 1];
  234. this.confirmPaste(recycle, lastRoot, 'next');
  235. }
  236. },
  237. initOptionDiv:function (selected,blockData) {
  238. let Bills = projectObj.project.Bills;
  239. if( blockData.firstNodeType == blockType.RATION){//如果剪切板中是复制的定额块
  240. if(selected.sourceType == ModuleNames.ration && selected.data.type != rationType.install ){//焦点行是定额/量价/工料机,则粘贴为后项
  241. return "next";
  242. }
  243. if(this.isFXorBX(selected)||(selected.sourceType === Bills.getSourceType()&& selected.source.children.length == 0)){//焦点行是分项/叶子清单,且无基数计算,则粘贴到分项/叶子清单下
  244. if(!this.haveCalcBase(selected)){//无基数计算
  245. return "sub";
  246. }
  247. }
  248. }
  249. //设置选项的初始值
  250. setRadioProp('next_node',{checked:true,disabled:false});
  251. setRadioProp('pre_node',{checked:false,disabled:false});
  252. if(Bills.isFBFX(selected)){// (2.1)、焦点行属于“分部分项工程”:
  253. //复制块的第一层、焦点行的类型都是“分部”
  254. if(blockData.firstNodeType == blockType.FB && this.isFB(selected)){
  255. if(selected.children.length > 0 && this.isFXorBX(selected.children[0])){//焦点行分部下有分项补项,弹出“粘贴位置选择”,默认为当前行的后项,可选前项,子项灰显。
  256. setRadioProp('sub_node',{checked:false,disabled:true});
  257. }else {
  258. //默认为当前行的后项,可选前项、子项。
  259. setRadioProp('sub_node',{checked:false,disabled:false});
  260. }
  261. }
  262. //复制块的第一层、焦点行的类型都是“分项”或补项
  263. if(this.blockIsFXorBX(blockData.firstNodeType) && this.isFXorBX(selected)){
  264. //默认为当前行的后项,可选前项,子项灰显不可选。
  265. setRadioProp('sub_node',{checked:false,disabled:true});
  266. }
  267. //复制块的第一层是分项,清单,焦点行是分部,且分部下无子项或者子项是分项
  268. if((blockData.firstNodeType ==blockType.BILL|| this.blockIsFXorBX(blockData.firstNodeType))&& this.isFB(selected)){
  269. if(selected.children.length == 0 || this.isFXorBX(selected.children[0])){
  270. return 'sub';//不弹出选择窗口,直接粘贴为子项。
  271. }
  272. }
  273. }else {//焦点行不属于“分部分项工程”
  274. if(blockData.firstNodeType == blockType.BILL &&selected.sourceType == Bills.getSourceType()){//如果剪切板中是从“分部分项工程”以外复制的清单块
  275. if(selected.children.length==0 || selected.children[0].sourceType == Bills.getSourceType()){//焦点行没有子项,或者子项是清单,判断是否有基数计算
  276. if(this.haveCalcBase(selected)){//如果焦点行有基数计算,则子项灰显不可选。
  277. setRadioProp('sub_node',{checked:false,disabled:true});
  278. }else{//如果焦点行没有基数计算,则子项可选。
  279. setRadioProp('sub_node',{checked:false,disabled:false});
  280. }
  281. }else {
  282. setRadioProp('sub_node',{checked:false,disabled:true});
  283. }
  284. }
  285. }
  286. $("#pastePosition").modal({show:true});
  287. return null;
  288. function setRadioProp(id,options) {
  289. $("#"+id).prop("checked",options.checked);
  290. $("#"+id).prop("disabled",options.disabled);
  291. }
  292. },
  293. /**
  294. * @param blockData
  295. * @param selected
  296. * @param position next/pre/sub
  297. */
  298. confirmPaste:function (blockData,selected,position,callback) {
  299. let me = this;
  300. let project = projectObj.project;
  301. let Bills = project.Bills;
  302. let parent = null,next = null,pre = null;
  303. let firstParentID = null, lastNextID = null;
  304. let updateData = [],billUpdate = null;
  305. let billsIDMap = {};//用来做新旧ID映射
  306. if(blockData.compilationID != projectObj.project.projectInfo.compilation){//如果编办不一样,不能复制
  307. alert("编办不一致,不能粘贴!");
  308. return;
  309. }
  310. if(position == 'next'){ //插入为选中节点的后项,即选中节点为前项
  311. parent = selected.parent;
  312. next = selected.nextSibling;
  313. pre = selected;
  314. }
  315. if(position == 'pre'){//插入为选中节点的前项,即选中节点为后项
  316. parent = selected.parent;
  317. next = selected;
  318. pre = selected.preSibling;
  319. }
  320. if(position == 'sub'){//即选中节点为父项
  321. parent = selected;
  322. pre = selected.lastChild();
  323. }
  324. let parentID = parent?parent.getID():-1;
  325. let nextID = next?next.getID():-1;
  326. if(blockData.firstNodeType == blockType.RATION){ //复制的是定额,需根据新的父项信息和工程量明细计算工程量
  327. for(let d of blockData.datas){
  328. d.billsItemID = parentID;
  329. if (blockData.zeroQuantity){ // 来自克隆块
  330. d.quantity = 0;
  331. d.quantityEXP = '';
  332. d.contain = '';
  333. }else{
  334. let billsQuantity = scMathUtil.roundForObj(parent.data.quantity,getDecimal("quantity",parent));
  335. this.calcRationQuantityAndContain(billsQuantity,d,parent.data.unit);
  336. }
  337. //如果粘贴位置不属于分部分项工程,或者不是安装工程,则把安装增加费内容置空
  338. if(!Bills.isFBFX(parent)|| !project.isInstall()){
  339. d.ration_installations = [];
  340. }
  341. }
  342. //计算序列号
  343. this.calcRationSerialNo(parentID,blockData,updateData,pre,next);
  344. }else {
  345. firstParentID = parentID;
  346. lastNextID = nextID;
  347. /* blockData.data.ParentID = parentID;
  348. blockData.data.NextSiblingID = nextID;*/
  349. if(pre){
  350. billUpdate = {type:blockData.datas[0].sourceType,query:{ID:pre.getID()},doc:{NextSiblingID:blockData.datas[0].ID}};
  351. }
  352. }
  353. let dataMap = this.preparePasteData(blockData.datas,billsIDMap,firstParentID,lastNextID,selected);
  354. if(billUpdate){
  355. billUpdate.doc.NextSiblingID = billsIDMap[billUpdate.doc.NextSiblingID];
  356. updateData.push(billUpdate);
  357. }
  358. dataMap.updateData = updateData;
  359. dataMap.projectID = projectObj.project.ID();
  360. console.log(dataMap);
  361. $.bootstrapLoading.start();
  362. CommonAjax.post('/bills/pasteBlock',dataMap,function (result) {
  363. $.bootstrapLoading.end();
  364. //更新前端缓存
  365. me.updateCache(result);
  366. //插入树节点
  367. let [rationNodes,calcNodes] = me.addToTree(parentID,nextID,result.bills,result.rations);
  368. //主材设备工料机插入主树
  369. project.ration_glj.addToMainTree(result.ration_gljs);
  370. //更新计算程序模板,并进行重新计算
  371. if(callback){
  372. callback(rationNodes,calcNodes);
  373. return;
  374. }
  375. project.calcProgram.calcNodesAndSave(calcNodes,async function () {
  376. installationFeeObj.calcInstallationFee();
  377. await OVER_HEIGHT.reCalcOverHeightFee();
  378. await itemIncreaseFeeObj.calcItemIncreaseFeeByNodes(rationNodes);
  379. });
  380. })
  381. },
  382. updateCache:function(result){
  383. let project = projectObj.project;
  384. let Bills = project.Bills,Ration = project.Ration;
  385. let mainTree = project.mainTree;
  386. Bills.addDatasToList(result.bills);
  387. Ration.addDatasToList(result.rations);
  388. Ration.addSubListOfRation(result);//这里是新插入定额时
  389. /* project.quantity_detail.addDatasToList(result.quantity_details);
  390. project.ration_glj.addDatasToList(result.ration_gljs);
  391. project.ration_coe.addDatasToList(result.ration_coes);
  392. project.ration_installation.addDatasToList(result.ration_installations);*/
  393. for(let u of result.updateData){
  394. let unode = mainTree.findNode(u.query.ID);
  395. if(unode){
  396. for(let ukey in u.doc){
  397. unode.data[ukey] = u.doc[ukey];
  398. }
  399. }
  400. }
  401. //更新项目工料机模块信息-计算消耗量 ---修改了加载方式
  402. // project.projectGLJ.datas = result.gljData;
  403. // project.projectGLJ.calcQuantity();
  404. for(let r of result.rations){
  405. if(r.type == rationType.gljRation){//对于工料机类型的定额,要重新设置下市场单价
  406. gljOprObj.setGLJPrice(r);
  407. }
  408. }
  409. },
  410. addToTree:function (parentID,nextID,bills,rations) {
  411. let project = projectObj.project;
  412. let Bills = project.Bills,mainTree = project.mainTree;
  413. let parentNodeMap={};//用于去重
  414. let parentMap_b = {},parentMap_r = {};
  415. let newNodes = [],firstNode = null,rationNodes=[],billNodes=[],calcNodes=[];
  416. createParentMap(parentMap_b,bills,'bills');
  417. createParentMap(parentMap_r,rations,'ration');
  418. if(parentMap_b[parentID]){
  419. for(let i = 0;i < parentMap_b[parentID].length;i++){
  420. let temNode = loadTreeNode(parentID,nextID,parentMap_b[parentID][i],'bills');
  421. i==0?firstNode = temNode:'';
  422. }
  423. }else if(parentMap_r[parentID]){
  424. for(let i = 0;i<parentMap_r[parentID].length;i++){
  425. let temNode = loadTreeNode(parentID,nextID,parentMap_r[parentID][i],'ration');
  426. i==0?firstNode = temNode:'';
  427. }
  428. }
  429. ProjectController.syncDisplayNewNodes(projectObj.mainController, newNodes,true);
  430. let sels = projectObj.mainController.sheet.getSelections();
  431. //设置选中并更新下方显示
  432. projectObj.mainController.setTreeSelected(firstNode);
  433. const serialNo = firstNode.serialNo();
  434. projectObj.mainController.sheet.setSelection(serialNo, sels[0].col, 1, 1);
  435. projectObj.mainController.sheet.showRow(serialNo, GC.Spread.Sheets.VerticalPosition.center);
  436. //没有子节点的清单父节点要进行汇总计算
  437. for(let b of billNodes){
  438. if(!b.children || b.children.length == 0){
  439. if(b.parent &&!parentNodeMap[b.parent.ID]) calcNodes.push(b.parent);
  440. }
  441. }
  442. return [rationNodes,rationNodes.concat(calcNodes)];
  443. function loadTreeNode(parentID,nextID,data,type) {
  444. let newNode = null;
  445. if(type == 'bills'){
  446. let newSource = Bills.tree.insertByData(data,parentID,nextID,true);
  447. newNode = mainTree.insert(parentID, nextID,data.ID);
  448. if (newNode) {
  449. newNode.source = newSource;
  450. newNode.sourceType = Bills.getSourceType();
  451. newNode.data = data;
  452. newNodes.push(newNode);
  453. let subType = 'bills';
  454. let children = parentMap_b[data.ID];
  455. if(!children){
  456. children = parentMap_r[data.ID];
  457. subType = 'ration';
  458. }
  459. if(children){
  460. for(let c of children){
  461. loadTreeNode(data.ID,-1,c,subType);
  462. }
  463. }
  464. }
  465. billNodes.push(newNode);
  466. }else if(type == 'ration'){
  467. newNode = project.mainTree.insert(parentID, nextID, data.ID);
  468. newNode.source = data;
  469. newNode.sourceType = project.Ration.getSourceType();
  470. newNode.data = data;
  471. newNodes.push(newNode);
  472. rationNodes.push(newNode);
  473. }
  474. return newNode;
  475. }
  476. function createParentMap(parentMap,list,type) {
  477. let parentKey = type=='bills'?'ParentID':'billsItemID';
  478. for(let l of list){
  479. let parentID = l[parentKey];
  480. if(parentMap[parentID]){
  481. parentMap[parentID].push(l);
  482. }else {
  483. parentMap[parentID] = [l];
  484. }
  485. }
  486. }
  487. },
  488. preparePasteData : function (datas,billsIDMap,firstParentID,lastNextID,selected) {
  489. let me = this;
  490. me.datas = _.cloneDeep(projectObj.project.Bills.datas);
  491. let bills = [],rations=[],ration_gljs = [],ration_coes = [],quantity_details = [],ration_installations = [],ration_templates=[];
  492. let firstBillIDs = [],lastBillID = null;//记录第一层清单的ID,和第一层的最后一个清单ID
  493. let firstIDMap = {};
  494. for(let i = 0 ;i< datas.length ;i++){
  495. if(datas[i].sourceType == 'bills'){
  496. firstBillIDs.push(datas[i].ID);
  497. if(i == datas.length -1){//最后一个清单节点
  498. lastBillID = datas[i].ID
  499. }
  500. }
  501. eachData(datas[i],selected);
  502. }
  503. for(let f of firstBillIDs){
  504. firstIDMap[billsIDMap[f]] = f //反向映射
  505. }
  506. for(let b of bills){//更新ID
  507. if(firstIDMap[b.ID]) {//如果是第一层清单节点对应的数据,更新父ID 为当前树中的父ID
  508. b.ParentID = firstParentID;
  509. }else {
  510. billsIDMap[b.ParentID]?b.ParentID = billsIDMap[b.ParentID]:'';
  511. }
  512. if(b.ID == billsIDMap[lastBillID]){//如果是第一层清单最后节点对应的数据,更新下一节点ID为插入位置的下一个节点ID
  513. b.NextSiblingID = lastNextID;
  514. }else {
  515. billsIDMap[b.NextSiblingID]?b.NextSiblingID = billsIDMap[b.NextSiblingID]:'';
  516. }
  517. }
  518. return {bills:bills,rations:rations,ration_gljs:ration_gljs,ration_coes:ration_coes,quantity_details:quantity_details,ration_installations:ration_installations,ration_templates:ration_templates};
  519. function eachData(data,selected) {
  520. if(data.sourceType == 'bills'){
  521. let tem_b = createBillsData(data,selected);
  522. bills.push(tem_b);
  523. for(let d of data.quantity_details){
  524. quantity_details.push(createQuantityDetails(d,tem_b,'bills'));
  525. }
  526. if(data.children && data.children.length>0){
  527. for(let c of data.children){
  528. eachData(c, selected);
  529. }
  530. }
  531. }
  532. if( data.sourceType == 'ration'){
  533. let tem_r = createRationData(data);
  534. rations.push(tem_r);
  535. for(let d of data.quantity_details){
  536. quantity_details.push(createQuantityDetails(d,tem_r, 'ration'));
  537. }
  538. for(let g of data.ration_gljs){
  539. let tem_rg = createSubList(g,tem_r);
  540. tem_rg.billsItemID = tem_r.billsItemID;
  541. ration_gljs.push(tem_rg);
  542. }
  543. for(let o of data.ration_coes){
  544. ration_coes.push(createSubList(o,tem_r));
  545. }
  546. for(let ri of data.ration_installations){
  547. ration_installations.push(createSubList(ri,tem_r));
  548. }
  549. if(data.ration_template){
  550. ration_templates.push(createRationTemplate(data.ration_template,tem_r))
  551. }
  552. }
  553. }
  554. function createRationTemplate(oldTem,rationData) {
  555. let template = _.cloneDeep(oldTem);
  556. delete template._id;
  557. template.ID = uuid.v1();
  558. template.projectID = projectObj.project.ID();
  559. template.rationID = rationData.ID;
  560. //其它值恢复成默认
  561. for(let t of template.templateList){
  562. t.billID = "";
  563. t.fxID="";
  564. t.quantity="0";
  565. t.coe = "";
  566. if(t.defaultLocation) t.billsLocation = t.defaultLocation;
  567. }
  568. return template;
  569. }
  570. function createSubList(subData,rationData) {
  571. let tem_sub = _.cloneDeep(subData);
  572. delete tem_sub._id;
  573. tem_sub.ID = uuid.v1();
  574. tem_sub.projectID = projectObj.project.ID();
  575. tem_sub.rationID = rationData.ID;
  576. return tem_sub;
  577. }
  578. function createQuantityDetails(detailData,pdata,type) {
  579. let tem_detail = _.cloneDeep(detailData);
  580. delete tem_detail._id;
  581. tem_detail.ID = uuid.v1();
  582. tem_detail.projectID = projectObj.project.ID();
  583. if(type == 'bills'){
  584. tem_detail.billID = pdata.ID;
  585. }
  586. if(type == 'ration'){
  587. tem_detail.rationID = pdata.ID;
  588. }
  589. return tem_detail;
  590. }
  591. function createRationData(rationData) {
  592. let tem_ration = _.cloneDeep(rationData);
  593. //删除旧数据
  594. delete tem_ration._id;
  595. delete tem_ration.fees;
  596. delete tem_ration.feesIndex;
  597. delete tem_ration.ration_gljs;
  598. delete tem_ration.ration_coes;
  599. delete tem_ration.ration_installations;
  600. delete tem_ration.quantity_details;
  601. delete tem_ration.referenceRationID;//删除关联的主定额信息
  602. delete tem_ration.__v;
  603. delete tem_ration.sourceType;
  604. delete tem_ration.quantityCoe;
  605. delete tem_ration.rationQuantityCoe;
  606. delete tem_ration.tenderQuantity;
  607. tem_ration.projectID = projectObj.project.ID();
  608. tem_ration.ID = uuid.v1();
  609. billsIDMap[tem_ration.billsItemID]?tem_ration.billsItemID = billsIDMap[tem_ration.billsItemID]:'';
  610. let defaultLibID = rationLibObj.getDefaultStdRationLibID();
  611. if(defaultLibID&&tem_ration.type == rationType.ration){
  612. tem_ration.prefix = projectObj.project.Ration.getRationPrefix(defaultLibID,tem_ration);
  613. }
  614. return tem_ration;
  615. }
  616. function createBillsData(billsData, selected) { //ID、重新生成code
  617. let Bills = projectObj.project.Bills;
  618. let temData = _.cloneDeep(billsData);
  619. //删除旧数据
  620. if(temData.children && temData.children.length>0){//如果是有子项,说明是计算得到的,要删除重新计算,没有子项,但是fees有值,说明是自已输入的,值要一起粘贴
  621. delete temData.fees;
  622. delete temData.feesIndex;
  623. }
  624. delete temData._id;
  625. delete temData.children;
  626. delete temData.quantity_details;
  627. delete temData.__v;
  628. delete temData.sourceType;
  629. delete temData.quantityCoe;
  630. delete temData.rationQuantityCoe;
  631. //从分项、补项 到清单
  632. if ((temData.type == billType.FX || temData.type == billType.BX) && !Bills.isFBFX(selected)) temData.type = billType.BILL;
  633. //从清单到分部分项
  634. if (temData.type == billType.BILL && Bills.isFBFX(selected)) temData.type = billType.FX;
  635. temData.projectID = projectObj.project.ID();
  636. let newID = uuid.v1(); //新的清单ID
  637. billsIDMap[temData.ID] = newID;
  638. temData.ID = newID; //新的清单ID
  639. if(temData.code && temData.code.length == 12){//是从清单库来的 //temData.billsLibId && temData.billsLibId!="" 2019-09-03 修改需求,不做清单库ID判断
  640. let value = temData.code.substr(0,9);
  641. if (value&&value.length === 9) {//&& /^[\d]+$/.test(value) 去掉全数字判断 07-31 zhang
  642. temData.code = me.newFormatCode(value);
  643. }
  644. }
  645. me.datas.push(temData);//为了下一个清单的std code 一样
  646. return temData;
  647. }
  648. },
  649. calcRationSerialNo:function (billsItemID,blockData,updateData,pre,next) {
  650. let br = projectObj.project.Ration.getBillsSortRation(billsItemID);
  651. let firstSerialNo = 0;
  652. if(next == null){ //没有下一树节点,即为最后节点
  653. firstSerialNo = br.length > 0 ? br[br.length - 1].serialNo + 1 : 1;
  654. }else {//有下一节点
  655. let startIndex =0;
  656. if(pre){
  657. startIndex = br.indexOf(pre.data)+1;
  658. }
  659. firstSerialNo = br[startIndex].serialNo;
  660. for(let i = startIndex;i < br.length; i++){
  661. let br_serialNo = br[i].serialNo + blockData.datas.length; //i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  662. updateData.push({type:blockData.datas[0].sourceType,query:{ID:br[i].ID},doc:{serialNo:br_serialNo}});//更新兄弟节点序列号
  663. }
  664. }
  665. for(let i = 0 ; i < blockData.datas.length;i++){
  666. blockData.datas[i].serialNo = firstSerialNo;
  667. firstSerialNo += 1;
  668. }
  669. },
  670. calcRationQuantityAndContain : function (billsQuantity,ration,billsUnit) {//计算定额工程量和含量
  671. let EXPString = ration.quantityEXP+"";
  672. if(EXPString.indexOf("QDL") != -1){
  673. if(EXPString=="QDL"){//定额的工程量是直接通过清单量填进来的;
  674. let t_unit = ration.unit?ration.unit.replace(/^\d+/,""):"";
  675. if(t_unit!=billsUnit){//如果定额的单位去除前面的数字后不等于清单单位,定额工程量保持不变
  676. return ;
  677. }
  678. let times = parseInt(ration.unit);
  679. if(isNaN(times)){
  680. times = 1;
  681. }
  682. ration.quantity = scMathUtil.roundForObj(billsQuantity / times,getDecimal("ration.quantity"));
  683. billsQuantity?ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,getDecimal("process")):ration.contain = 0;
  684. } else {//如果定额的工程量是通过计算出来的,则应该重新计算。
  685. let tem_contain = scMathUtil.roundForObj(ration.contain,getDecimal("process"));
  686. let tem_quantity = scMathUtil.roundForObj(billsQuantity*tem_contain,getDecimal("ration.quantity"));
  687. ration.quantity = tem_quantity;
  688. }
  689. }else {//GCLMXHJ
  690. let tem_contain=0;
  691. if(billsQuantity&&billsQuantity!=0){
  692. let children_quantity = scMathUtil.roundForObj(ration.quantity,getDecimal("ration.quantity"));
  693. // children_quantity = scMathUtil.roundForObj(this.reverseQuantity(children_quantity,rationNode),getDecimal("quantity",rationNode)); 原先是要反算的,现在改成不用反算了
  694. tem_contain =scMathUtil.roundForObj(children_quantity/billsQuantity,getDecimal("process"));
  695. }
  696. ration.contain = tem_contain;
  697. }
  698. },
  699. newFormatCode : function (stdCode, filterCode) {
  700. let matchs = this.sameStdCode(stdCode, filterCode);
  701. let format = function (Number) {
  702. let s = Number + '';
  703. while (s.length < 3) {
  704. s = '0' + s;
  705. }
  706. return s;
  707. };
  708. for (let i = 0; i <= matchs.length; i++) {
  709. let formatCode = stdCode + format(i+1);
  710. if (matchs.indexOf(formatCode) === -1) {
  711. return formatCode;
  712. }
  713. }
  714. },
  715. sameStdCode : function (stdCode, filterCode) {
  716. let reg = new RegExp('^' + stdCode), matchs= [];
  717. for (let data of this.datas) {
  718. if (data.code && data.code.length === 12 && reg.test(data.code) && data.code !== filterCode) {
  719. matchs.push(data.code);
  720. }
  721. }
  722. return matchs;
  723. },
  724. isFB :function (selected) {
  725. return selected.sourceType == projectObj.project.Bills.getSourceType() && selected.data.type == billType.FB;
  726. },
  727. isFXorBX:function (selected) {//是分项或者补项
  728. return projectObj.project.Bills.isFXorBX(selected);
  729. },
  730. blockIsFB: function (type) {
  731. return type == blockType.FB
  732. },
  733. blockIsFXorBX: function (type) {
  734. return type == blockType.FX||type == blockType.BX;
  735. },
  736. blockIsLeaveBills: function (bills) {
  737. if (bills.children && bills.children.length > 0) {
  738. return bills.children[0].sourceType == "ration" //如果子节点是定额,则说明是页子清单
  739. }
  740. return true;
  741. },
  742. removeBlock:function () {
  743. removeLocalCache('project_block');
  744. }
  745. };