pm_facade.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /**
  2. * Created by zhang on 2018/4/17.
  3. */
  4. let mongoose = require('mongoose');
  5. let _ = require("lodash");
  6. let feeRate_facade = require('../../fee_rates/facade/fee_rates_facade');
  7. let logger = require("../../../logs/log_helper").logger;
  8. const uuidV1 = require('uuid/v1');
  9. let projectModel = mongoose.model('projects');
  10. let projectSettingModel = mongoose.model('proj_setting');
  11. let billsModel = mongoose.model('bills');
  12. let rationModel = mongoose.model('ration');
  13. let gljListModel = mongoose.model("glj_list");
  14. let calcProgramsModel = mongoose.model('calc_programs');
  15. let labourCoesModel = mongoose.model('labour_coes');
  16. let feeRateModel = mongoose.model('fee_rates');
  17. let feeRateFileModel = mongoose.model('fee_rate_file');
  18. let unitPriceFileModel = mongoose.model("unit_price_file");
  19. let mixRatioModel = mongoose.model("mix_ratio");
  20. let unitPriceModel = mongoose.model("unit_price");
  21. let installationFeeModel = mongoose.model("installation_fee");
  22. let rationGLJModel = mongoose.model('ration_glj');
  23. let rationCoeModel = mongoose.model('ration_coe');
  24. let rationInstallationModel = mongoose.model('ration_installation');
  25. let quantityDetailModel = mongoose.model('quantity_detail');
  26. let scMathUtil = require('../../../public/scMathUtil').getUtil();
  27. import CounterModel from "../../glj/models/counter_model";
  28. import moment from 'moment';
  29. import billsFlags from '../../common/const/bills_fixed';
  30. const projectType = {
  31. folder: 'Folder',
  32. tender: 'Tender',
  33. project: 'Project',
  34. engineering: 'Engineering',
  35. };
  36. module.exports={
  37. moveProject:moveProject,
  38. copyProject:copyProject,
  39. getSummaryInfo: getSummaryInfo
  40. };
  41. async function copyProject(userID, compilationID,data) {
  42. let projectMap = data.projectMap;
  43. let originalID = projectMap['copy'].document.ID;
  44. let newProjectID = await getCounterID("projects");
  45. let newFeeName = null,newUnitName = null;
  46. let calcProgramFileID = uuidV1();//新的计算程序文件ID
  47. let labourCoeFileID = uuidV1();//新的人工调整系数文件ID
  48. let feeRateFileID = uuidV1();//新的费率文件ID
  49. let unitPriceFileID = await getCounterID("unit_price_file");//新的单价文件ID
  50. let originalProject = await projectModel.findOne({'ID':originalID});//查找最新的那项目信息,前端缓存的数据有可能不是最新的
  51. if(!originalProject){
  52. throw new Error('没有找到对应的项目,复制失败!');
  53. }
  54. let property = originalProject.property;
  55. if(projectMap['copy'].document.property){//更新项目属性信息
  56. setProperty(property,projectMap['copy'].document.property);
  57. }
  58. let originalProperty = _.cloneDeep(property);
  59. projectMap['copy'].document.property = property;
  60. logger.info("复制项目: 旧项目ID: "+originalID+ " ------- 新项目ID: "+newProjectID);
  61. //费率文件、单价文件重名检查
  62. let feeRate = await feeRateFileModel.findOne({rootProjectID:originalProperty.rootProjectID,name:originalProperty.feeFile.name,deleteInfo:null});
  63. if(feeRate){//存在重名的文件
  64. newFeeName = originalProperty.feeFile.name + '(' + moment(Date.now()).format('MM-DD HH:mm:ss') + '复制)';
  65. projectMap['copy'].document.property.feeFile.name = newFeeName;
  66. }
  67. let unitPriceFile = await unitPriceFileModel.findOne({root_project_id: originalProperty.rootProjectID,name:originalProperty.unitPriceFile.name,deleteInfo: null});
  68. if(unitPriceFile){//存在重名的文件
  69. newUnitName = originalProperty.unitPriceFile.name + '(' + moment(Date.now()).format('MM-DD HH:mm:ss') + '复制)';
  70. projectMap['copy'].document.property.unitPriceFile.name = newUnitName;
  71. }
  72. //更新项目的属性;
  73. projectMap['copy'].document.ID = newProjectID;
  74. projectMap['copy'].document.property.calcProgramFile.ID = calcProgramFileID;
  75. projectMap['copy'].document.property.labourCoeFile.ID = labourCoeFileID;
  76. projectMap['copy'].document.property.feeFile.id = feeRateFileID;
  77. projectMap['copy'].document.property.unitPriceFile.id = unitPriceFileID;
  78. projectMap['copy'].document.userID = userID;
  79. projectMap['copy'].document.compilation = compilationID;
  80. projectMap['copy'].document.createDateTime = new Date();
  81. //清单、定额ID生成任务
  82. let IDtasks = [
  83. createIDsAndReturn(originalID,billsModel),
  84. createIDsAndReturn(originalID,rationModel),
  85. getProjectGLJIDAndReturn(originalID,newProjectID)
  86. ];
  87. let [billMap,rationMap,projectGLJMap] = await Promise.all(IDtasks);
  88. //所有复制任务异步处理,提升效率 复制清单,定额,4个文件,项目工料机, 定额工料机,人工系数,工程量明细,定额安装增加费,安装增加费
  89. let copyTasks = [
  90. createProject(projectMap),
  91. copyProjectSetting(originalID,newProjectID),
  92. copyBills(newProjectID,billMap),
  93. copyRations(newProjectID,billMap.uuidMaping,rationMap,projectGLJMap.IDMap),
  94. copyProjectGLJ(projectGLJMap.datas),
  95. commonCopy(newProjectID,originalProperty.calcProgramFile.ID,calcProgramFileID,calcProgramsModel),
  96. commonCopy(newProjectID,originalProperty.labourCoeFile.ID,labourCoeFileID,labourCoesModel),
  97. copyFeeRate(originalProperty.rootProjectID,userID,originalProperty.feeFile.id,feeRateFileID,newFeeName),
  98. copyUnitPriceFile(newProjectID,originalProperty.rootProjectID,userID,originalProperty.unitPriceFile.id,unitPriceFileID,newUnitName),
  99. copyInstallFee(originalID,newProjectID),
  100. copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,rationGLJModel),
  101. copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,rationCoeModel),
  102. copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,quantityDetailModel),
  103. copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,rationInstallationModel)
  104. ];
  105. let p = await Promise.all(copyTasks);
  106. return p[0];
  107. }
  108. async function createIDsAndReturn(originalID,model) {
  109. let uuidMaping = {};//做ID映射
  110. let datas = [];
  111. let result = await model.find({"projectID": originalID}, '-_id');
  112. uuidMaping['-1'] = -1;
  113. //建立uuid-ID映射
  114. for(let d of result){
  115. uuidMaping[d.ID] = uuidV1();
  116. datas.push(d._doc);
  117. }
  118. return{uuidMaping:uuidMaping,datas:datas};
  119. }
  120. async function getProjectGLJIDAndReturn(originalID,newProjectID) {
  121. let IDMap = {};
  122. let datas = [];
  123. let result = await gljListModel.find({project_id:originalID}, '-_id');
  124. for(let d of result){
  125. let newID = await getCounterID("glj_list");
  126. IDMap[d.id] = newID;
  127. d._doc.project_id = newProjectID;
  128. d._doc.id = newID;
  129. datas.push(d._doc);
  130. }
  131. return{IDMap:IDMap,datas:datas};
  132. }
  133. async function createProject(projectMap) {//复制项目
  134. let tasks = [];
  135. if(projectMap['update']){//如果复制后存在前一个节点,则更新其NextSiblingID
  136. tasks.push({updateOne:{filter : projectMap['update'].query, update : {NextSiblingID:projectMap['copy'].document.ID}}});
  137. }
  138. tasks.push({insertOne: {document: projectMap['copy'].document}});//复制任务
  139. await projectModel.bulkWrite(tasks);
  140. return projectMap;
  141. }
  142. async function copyProjectSetting(originalID,newProjectID) {
  143. let result = null;
  144. let setting = await projectSettingModel.findOne({"projectID": originalID}, '-_id');
  145. if(setting){
  146. setting._doc.projectID =newProjectID;
  147. result = await projectSettingModel.create(setting._doc);
  148. }
  149. return result;
  150. }
  151. async function copyBills(newProjectID,billMap) {
  152. let uuidMaping = billMap.uuidMaping;//做ID映射
  153. for(let doc of billMap.datas){
  154. doc.projectID = newProjectID;
  155. doc.ID = uuidMaping[doc.ID] ? uuidMaping[doc.ID] : -1;
  156. doc.ParentID = uuidMaping[doc.ParentID] ? uuidMaping[doc.ParentID] : -1;
  157. doc.NextSiblingID = uuidMaping[doc.NextSiblingID] ? uuidMaping[doc.NextSiblingID] : -1;
  158. }
  159. await insertMany(billMap.datas,billsModel);
  160. return billMap.datas;
  161. }
  162. async function copyRations(newProjectID,billsIDMap,rationMap,projectGLJIDMap) {
  163. let uuidMaping = rationMap.uuidMaping;
  164. for(let doc of rationMap.datas){
  165. doc.projectID = newProjectID;
  166. doc.ID = uuidMaping[doc.ID] ? uuidMaping[doc.ID] : -1;
  167. if(doc.billsItemID){
  168. doc.billsItemID = billsIDMap[doc.billsItemID]?billsIDMap[doc.billsItemID]:-1;
  169. }
  170. //绑定定类型的工料机 项目工料机ID
  171. doc.type==3&&doc.projectGLJID&&projectGLJIDMap[doc.projectGLJID]?doc.projectGLJID = projectGLJIDMap[doc.projectGLJID]:'';
  172. }
  173. if(rationMap.datas.length > 0){
  174. await insertMany(rationMap.datas,rationModel);
  175. }
  176. return rationMap.datas;
  177. }
  178. async function copyProjectGLJ(gljList) {
  179. await insertMany(gljList,gljListModel);
  180. }
  181. async function commonCopy(newProjectID,oldID,newID,model) { //对于只需更新ID和projectID的文件的复制
  182. let result = null;
  183. let file = await model.findOne({"ID": oldID}, '-_id');
  184. if(file){
  185. file._doc.ID = newID;
  186. file._doc.projectID =newProjectID;
  187. result = await model.create(file._doc);
  188. }
  189. return result;
  190. }
  191. async function copyFeeRate(rootProjectID,userID,originalFeeRateFileID,feeRateFileID,newName) {//复制费率和费率文件
  192. let [feeRateFile,feeRate] =await feeRate_facade.getFeeRateByID(originalFeeRateFileID);
  193. let newFeeRateID = uuidV1();
  194. if(feeRate){
  195. feeRate._doc.ID = newFeeRateID;
  196. await feeRateModel.create(feeRate._doc);
  197. }
  198. if(feeRateFile){
  199. feeRateFile._doc.ID = feeRateFileID;
  200. newName?feeRateFile._doc.name = newName:'';
  201. feeRateFile._doc.userID = userID;
  202. feeRateFile._doc.rootProjectID = rootProjectID;
  203. feeRateFile._doc.feeRateID = newFeeRateID;
  204. await feeRateFileModel.create(feeRateFile._doc);
  205. }
  206. }
  207. async function copyUnitPriceFile(newProjectID,rootProjectID,userID,originaluUnitPriceFileID,unitPriceFileID,newName) {//复制单价文件、组成物
  208. let taskList = [
  209. copyFile(newProjectID,rootProjectID,userID,originaluUnitPriceFileID,unitPriceFileID,newName),
  210. copySubList(originaluUnitPriceFileID,unitPriceFileID,mixRatioModel),
  211. copySubList(originaluUnitPriceFileID,unitPriceFileID,unitPriceModel)
  212. ];
  213. return await Promise.all(taskList);
  214. async function copyFile(newProjectID,rootProjectID,userID,originaluUnitPriceFileID,unitPriceFileID,newName){
  215. let unitPriceFile = await unitPriceFileModel.findOne({id:originaluUnitPriceFileID}, '-_id');
  216. if(unitPriceFile){
  217. unitPriceFile._doc.id = unitPriceFileID;
  218. newName?unitPriceFile._doc.name = newName:'';
  219. unitPriceFile._doc.project_id = newProjectID;
  220. unitPriceFile._doc.user_id = userID;
  221. unitPriceFile._doc.root_project_id = rootProjectID
  222. }
  223. await unitPriceFileModel.create(unitPriceFile._doc);
  224. }
  225. async function copySubList(srcFID,newFID,model) {
  226. let mList = await model.find({unit_price_file_id: srcFID}, '-_id');
  227. let rList = [];
  228. for(let m of mList){
  229. m._doc.unit_price_file_id = newFID;
  230. m._doc.id = await getCounterID(model.modelName);
  231. rList.push(m._doc);
  232. }
  233. await insertMany(rList,model)
  234. }
  235. }
  236. async function copyInstallFee(originalPID,newProjectID) {
  237. let result = null;
  238. let installationFee = await installationFeeModel.find({projectID:originalPID}, '-_id');
  239. let newList = [];
  240. for(let i of installationFee ){
  241. i._doc.ID = uuidV1();
  242. i._doc.projectID = newProjectID;
  243. newList.push(i._doc);
  244. }
  245. if(newList.length >0){
  246. result = await installationFeeModel.insertMany(newList);
  247. }
  248. return result;
  249. }
  250. async function copyRationSubList(originalPID,newProjectID,billIDMap,rationIDMap,projectGLJIDMap,model) {// 定额工料机,附注条件,工程量明细,定额安装增加费
  251. let subList = await model.find({projectID:originalPID}, '-_id');
  252. let newList =[];
  253. for(let s of subList){
  254. s._doc.ID = uuidV1();
  255. s._doc.projectID = newProjectID;
  256. s._doc.rationID&&rationIDMap[s._doc.rationID]?s._doc.rationID = rationIDMap[s._doc.rationID]:'';
  257. s._doc.billID&&billIDMap[s._doc.billID]?s._doc.billID = billIDMap[s._doc.billID]:'';
  258. s._doc.billsItemID&&billIDMap[s._doc.billsItemID]?s._doc.billsItemID = billIDMap[s._doc.billsItemID]:'';
  259. s._doc.projectGLJID&&projectGLJIDMap[s._doc.projectGLJID]?s._doc.projectGLJID = projectGLJIDMap[s._doc.projectGLJID]:'';
  260. newList.push(s._doc);
  261. }
  262. await insertMany(newList,model);
  263. }
  264. async function moveProject(data) {
  265. data = JSON.parse(data);
  266. let projectMap = data.projectMap,feeRateMap = data.feeRateMap,unitPriceMap = data.unitPriceMap;
  267. let projectTasks = [],feeRateTask=[],feeRateFileTask=[],unitPriceTask=[],unitPriceFileTask=[],mixRatioTask=[];
  268. let feeUniqMap=[],priceUniqMap={};//费率、单价文件唯一映射表当移动多个项目时,任务有可能出现重复的情况
  269. if(!_.isEmpty(feeRateMap)&&data.rootProjectID){//如果费率有修改
  270. let feeRates =await feeRate_facade.getFeeRatesByProject(data.rootProjectID);//取目标建设项目的所有费率文件,重名检查
  271. for(let projectID in feeRateMap){
  272. let rename = feeRateMap[projectID].name;
  273. let feeRateID = feeRateMap[projectID].query.ID;
  274. let checkFee = _.find(feeRates,{'name':rename});
  275. let newID = feeRateID;
  276. if(checkFee){//说明费率名字已存在,需要重命名
  277. rename = feeUniqMap[feeRateID]?feeUniqMap[feeRateID].name:rename + '(' + new Date().Format('MM-dd hh:mm:ss') + '移动)';
  278. projectMap[projectID]['update']['property.feeFile.name'] = rename;
  279. if(feeRateMap[projectID].action == 'move'){
  280. feeRateMap[projectID].update.name = rename;
  281. }
  282. }
  283. if(feeRateMap[projectID].action == 'copy'){
  284. newID = feeUniqMap[feeRateID]?feeUniqMap[feeRateID].ID:uuidV1();
  285. feeRateMap[projectID].name = rename;
  286. feeRateMap[projectID].ID = newID;
  287. projectMap[projectID]['update']['property.feeFile.id'] = newID;
  288. }
  289. if(!feeUniqMap[feeRateID]){
  290. await generateFeeRateTask(feeRateMap[projectID],feeRateTask,feeRateFileTask);
  291. feeUniqMap[feeRateID] = {name:rename,ID:newID};
  292. }
  293. }
  294. }
  295. if(!_.isEmpty(unitPriceMap)&&data.rootProjectID) {//如果单价文件有修改
  296. let unitPriceFiles = await unitPriceFileModel.find({root_project_id: data.rootProjectID, deleteInfo: null});
  297. for(let projectID in unitPriceMap){
  298. let checkUn = _.find(unitPriceFiles,{'name':unitPriceMap[projectID].name});
  299. let rename = unitPriceMap[projectID].name;
  300. let unitPriceID = unitPriceMap[projectID].query.id;
  301. let newID = unitPriceID;
  302. if(checkUn){//重名检查
  303. rename = priceUniqMap[unitPriceID]?priceUniqMap[unitPriceID].name:rename + '(' + new Date().Format('MM-dd hh:mm:ss') + '移动)';
  304. projectMap[projectID]['update']['property.unitPriceFile.name'] = rename;
  305. if(unitPriceMap[projectID].action =='move'){
  306. unitPriceMap[projectID].update.name = rename;
  307. }
  308. }
  309. if(unitPriceMap[projectID].action =='copy'){
  310. newID = priceUniqMap[unitPriceID]?priceUniqMap[unitPriceID].id: await getCounterID("unit_price_file");
  311. unitPriceMap[projectID].name = rename;
  312. unitPriceMap[projectID].id = newID;
  313. projectMap[projectID]['update']['property.unitPriceFile.id'] = newID;
  314. }
  315. if(!priceUniqMap[unitPriceID]){
  316. await generateUnitPriceTask(unitPriceMap[projectID],projectID,data.user_id,unitPriceTask,unitPriceFileTask,mixRatioTask);
  317. priceUniqMap[unitPriceID] = {name:rename,id:newID};
  318. }
  319. }
  320. }
  321. if(!_.isEmpty(projectMap)){
  322. for(let projectID in projectMap){
  323. projectTasks.push({updateOne:{filter : projectMap[projectID].query, update : projectMap[projectID].update}});
  324. }
  325. }
  326. projectTasks.length>0?await projectModel.bulkWrite(projectTasks):'';
  327. feeRateTask.length>0?await feeRateModel.bulkWrite(feeRateTask):'';
  328. feeRateFileTask.length>0?await feeRateFileModel.bulkWrite(feeRateFileTask):'';
  329. unitPriceFileTask.length>0?await unitPriceFileModel.bulkWrite(unitPriceFileTask):'';
  330. unitPriceTask.length>0?await insertMany(unitPriceTask,unitPriceModel):'';
  331. mixRatioTask.length>0?await insertMany(mixRatioTask,mixRatioModel):'';
  332. return projectMap;
  333. }
  334. async function generateFeeRateTask(data,feeRateTask,feeRateFileTask) {
  335. if(data.action == 'move'){
  336. let task = {
  337. updateOne:{
  338. filter : data.query,
  339. update : data.update
  340. }
  341. };
  342. feeRateFileTask.push(task);
  343. }
  344. if(data.action == 'copy'){//copy 费率的时候用insertOne方法
  345. let [feeRateFile,feeRate] =await feeRate_facade.getFeeRateByID(data.query.ID);
  346. let newFeeRateID = uuidV1();
  347. let newFeeRate = {
  348. ID:newFeeRateID,
  349. rates:feeRate.rates
  350. };
  351. let newFeeRateFile = {
  352. ID:data.ID,
  353. rootProjectID:data.rootProjectID,
  354. userID:feeRateFile.userID,
  355. name:data.name,
  356. libID:feeRateFile.libID,
  357. libName:feeRateFile.libName,
  358. feeRateID:newFeeRateID
  359. };
  360. feeRateTask.push({insertOne :{document:newFeeRate}});
  361. feeRateFileTask.push({insertOne :{document:newFeeRateFile}});
  362. }
  363. }
  364. async function generateUnitPriceTask(data,projectID,user_id,unitPriceTask,unitPriceFileTask,mixRatioTask){
  365. if(data.action == 'move'){
  366. let task = {
  367. updateOne:{
  368. filter : data.query,
  369. update : data.update
  370. }
  371. };
  372. unitPriceFileTask.push(task);
  373. }
  374. if(data.action == 'copy'){
  375. let newUnitFile = {
  376. id:data.id,
  377. name: data.name,
  378. project_id:projectID,
  379. user_id:user_id,
  380. root_project_id: data.rootProjectID
  381. };
  382. unitPriceFileTask.push({insertOne :{document:newUnitFile}});
  383. let mixRatios = await mixRatioModel.find({unit_price_file_id: data.query.id});
  384. mixRatios = JSON.stringify(mixRatios);
  385. mixRatios = JSON.parse(mixRatios);
  386. for(let m of mixRatios){
  387. delete m._id; // 删除原有id信息
  388. delete m.id;
  389. m.unit_price_file_id = data.id;
  390. m.id = await getCounterID('mix_ratio');
  391. mixRatioTask.push(m);
  392. }
  393. let unitPrices = await unitPriceModel.find({unit_price_file_id: data.query.id});//unit_price_file_id
  394. unitPrices = JSON.stringify(unitPrices);
  395. unitPrices = JSON.parse(unitPrices);
  396. for(let u of unitPrices){
  397. delete u._id; // 删除原有id信息
  398. delete u.id;
  399. u.unit_price_file_id = data.id;
  400. u.id = await getCounterID('unit_price');
  401. unitPriceTask.push(u);
  402. }
  403. }
  404. }
  405. async function getCounterID(collectionName){
  406. let counterModel = new CounterModel();
  407. return await counterModel.getId(collectionName);
  408. }
  409. async function insertMany(datas,model) {
  410. while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
  411. let newList = datas.splice(0,1000);//一次插入1000条
  412. await model.insertMany(newList);
  413. }
  414. await model.insertMany(datas);
  415. }
  416. function setProperty(Obj,updateData) {
  417. for(let ukey in updateData){
  418. if(_.isObject(updateData[ukey]) && _.isObject(Obj[ukey])){
  419. setProperty(Obj[ukey],updateData[ukey]);
  420. }else {
  421. Obj[ukey] = updateData[ukey];
  422. }
  423. }
  424. }
  425. function isDef(v){
  426. return typeof v !== 'undefined' && v !== null;
  427. }
  428. function getCommonTotalFee(bills) {
  429. if(!isDef(bills)){
  430. return 0;
  431. }
  432. if(!isDef(bills.fees) || bills.fees.length <= 0){
  433. return 0;
  434. }
  435. for(let fee of bills.fees){
  436. if(isDef(fee.fieldName) && fee.fieldName === 'common'){
  437. return isDef(fee.totalFee) ? fee.totalFee : 0;
  438. }
  439. }
  440. return 0;
  441. }
  442. function summarizeToParent(parent, child) {
  443. const decimal = -2;
  444. parent.engineeringCost = scMathUtil.roundTo(parseFloat(parent.engineeringCost) + parseFloat(child.engineeringCost), decimal);
  445. parent.subEngineering = scMathUtil.roundTo(parseFloat(parent.subEngineering) + parseFloat(child.subEngineering), decimal);
  446. parent.measure = scMathUtil.roundTo(parseFloat(parent.measure) + parseFloat(child.measure), decimal);
  447. parent.safetyConstruction = scMathUtil.roundTo(parseFloat(parent.safetyConstruction) + parseFloat(child.safetyConstruction), decimal);
  448. parent.other = scMathUtil.roundTo(parseFloat(parent.other) + parseFloat(child.other), decimal);
  449. parent.charge = scMathUtil.roundTo(parseFloat(parent.charge) + parseFloat(child.charge), decimal);
  450. parent.tax = scMathUtil.roundTo(parseFloat(parent.tax) + parseFloat(child.tax), decimal);
  451. }
  452. async function getSummaryInfo(projectIDs){
  453. //ID与汇总信息映射
  454. let IDMapping = {};
  455. //固定清单类别与汇总金额字段映射
  456. let flagFieldMapping = {};
  457. flagFieldMapping[billsFlags.ENGINEERINGCOST] = 'engineeringCost';
  458. flagFieldMapping[billsFlags.SUB_ENGINERRING] = 'subEngineering';
  459. flagFieldMapping[billsFlags.MEASURE] = 'measure';
  460. flagFieldMapping[billsFlags.SAFETY_CONSTRUCTION] = 'safetyConstruction';
  461. flagFieldMapping[billsFlags.OTHER] = 'other';
  462. flagFieldMapping[billsFlags.CHARGE] = 'charge';
  463. flagFieldMapping[billsFlags.TAX] = 'tax';
  464. for(let projectID of projectIDs){
  465. IDMapping[projectID] = {engineeringCost: 0, subEngineering: 0, measure: 0, safetyConstruction: 0, other: 0, charge: 0, tax: 0, rate: 0, buildingArea: '', perCost: ''};
  466. }
  467. //let projects = await projectModel.find({ID: {$in : projectIDs}, projType: projectType.project, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
  468. //单项工程
  469. let engineerings = await projectModel.find({ParentID: {$in : projectIDs}, projType: projectType.engineering, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
  470. //单位工程
  471. let tenders = [];
  472. let engIDs = [];
  473. for(let eng of engineerings){
  474. engIDs.push(eng.ID);
  475. IDMapping[eng.ID] = {engineeringCost: 0, subEngineering: 0, measure: 0, safetyConstruction: 0, other: 0, charge: 0, tax: 0, rate: 0, buildingArea: '', perCost: ''};
  476. }
  477. if(engIDs.length > 0){
  478. tenders = await projectModel.find({ParentID: {$in : engIDs}, projType: projectType.tender, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
  479. }
  480. let tenderIDs = [];
  481. if(tenders.length > 0){
  482. for(let tender of tenders){
  483. tenderIDs.push(tender.ID);
  484. IDMapping[tender.ID] = {engineeringCost: 0, subEngineering: 0, measure: 0, safetyConstruction: 0, other: 0, charge: 0, tax: 0, rate: 0, buildingArea: '', perCost: ''};
  485. }
  486. //需要获取的清单固定类别综合合价:工程造价、分部分项、措施项目、安全文明施工专项、规费、其他项目、税金
  487. let needFlags = [billsFlags.ENGINEERINGCOST, billsFlags.SUB_ENGINERRING, billsFlags.MEASURE,
  488. billsFlags.SAFETY_CONSTRUCTION, billsFlags.CHARGE, billsFlags.OTHER, billsFlags.TAX];
  489. //获取单位工程汇总金额需要用到的所有清单
  490. let allBills = await billsModel.find({projectID: {$in: tenderIDs}, 'flags.flag': {$in: needFlags}, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]},
  491. '-_id projectID fees flags');
  492. //进行单位工程级别的汇总
  493. for(let bills of allBills){
  494. let billsFlag = bills.flags[0]['flag'];
  495. let costField = flagFieldMapping[billsFlag];
  496. IDMapping[bills.projectID][costField] = getCommonTotalFee(bills);
  497. }
  498. //进行单项工程级别的汇总
  499. for(let tender of tenders){
  500. summarizeToParent(IDMapping[tender.ParentID], IDMapping[tender.ID]);
  501. }
  502. //进行建设项目级别的汇总
  503. for(let eng of engineerings){
  504. summarizeToParent(IDMapping[eng.ParentID], IDMapping[eng.ID]);
  505. }
  506. //占造价比例
  507. const rateDecimal = -2;
  508. for(let tender of tenders){
  509. let tenderInfo = IDMapping[tender.ID];
  510. let engInfo = IDMapping[tender.ParentID];
  511. tenderInfo.rate = engInfo.engineeringCost == 0 ? 0 : scMathUtil.roundTo(tenderInfo.engineeringCost * 100 / engInfo.engineeringCost, rateDecimal);
  512. }
  513. for(let eng of engineerings){
  514. let engInfo = IDMapping[eng.ID];
  515. let projInfo = IDMapping[eng.ParentID];
  516. engInfo.rate = !isDef(projInfo) || projInfo.engineeringCost == 0 ? 0 : scMathUtil.roundTo(engInfo.engineeringCost * 100 / projInfo.engineeringCost, rateDecimal);
  517. }
  518. for(let projectID of projectIDs){
  519. IDMapping[projectID].rate = 100;
  520. }
  521. }
  522. console.log(IDMapping);
  523. return IDMapping;
  524. }