block_controller.js 35 KB

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