project_facade.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /**
  2. * Created by zhang on 2018/1/26.
  3. */
  4. module.exports = {
  5. markUpdateProject:markUpdateProject,
  6. removeProjectMark:removeProjectMark,
  7. updateNodes:updateNodes,
  8. calcInstallationFee:calcInstallationFee,
  9. saveProperty: saveProperty,
  10. getDefaultColSetting: getDefaultColSetting,
  11. markProjectsToChange:markProjectsToChange,
  12. getBudgetSummayDatas:getBudgetSummayDatas,
  13. getGLJSummayDatas:getGLJSummayDatas
  14. };
  15. let mongoose = require('mongoose');
  16. let logger = require("../../../logs/log_helper").logger;
  17. let projectsModel = mongoose.model('projects');
  18. let async_n = require("async");
  19. let _ = require('lodash');
  20. let ration_model = require('../models/ration');
  21. let bill_model = require('../models/bills');
  22. let consts = require('../models/project_consts');
  23. let projectConsts = consts.projectConst;
  24. let ration_glj_model = mongoose.model('ration_glj');
  25. let rationTemplateModel = mongoose.model('ration_template');
  26. let project_glj_model = mongoose.model('glj_list');
  27. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  28. const uuidV1 = require('uuid/v1');
  29. const gljUtil = require('../../../public/gljUtil');
  30. let stdColSettingModel = mongoose.model('std_main_col_lib');
  31. let decimal_facade = require('../../main/facade/decimal_facade');
  32. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  33. import fixedFlag from '../../common/const/bills_fixed';
  34. import GLJListModel from "../../glj/models/glj_list_model";
  35. const projectDao = require('../../pm/models/project_model').project;
  36. async function calcInstallationFee(data) {
  37. let result={};
  38. let billTasks = generateTasks(data.bills,data.useID);
  39. let rationTasks = generateTasks(data.ration,data.useID);
  40. if(billTasks.length>0){
  41. await bill_model.model.bulkWrite(billTasks);
  42. }
  43. console.log(rationTasks);
  44. if(rationTasks.length>0){
  45. await ration_model.model.bulkWrite(rationTasks);
  46. }
  47. //如果删除定额,需要删除对应的工料机
  48. if(data.ration.delete.length>0){
  49. let rationIDS = _.map(data.ration.delete,'ID');
  50. await ration_glj_model.deleteMany({projectID: data.ration.delete[0].projectID, rationID: {"$in": rationIDS}});//删除定额工料机
  51. }
  52. let rationGLJTasks = [];
  53. let updateList = [];
  54. if(data.ration.update.length>0){//如果有需要更新的定额工料机
  55. for(let ur of data.ration.update){
  56. for(let g of ur.glj){
  57. let gTasks = {
  58. updateOne:{
  59. filter:{
  60. ID:g.ID,
  61. projectID:g.projectID
  62. },
  63. update :{
  64. quantity:g.quantity,
  65. rationItemQuantity:g.rationItemQuantity
  66. }
  67. }
  68. };
  69. rationGLJTasks.push(gTasks);
  70. updateList.push(g);
  71. }
  72. }
  73. }
  74. if(rationGLJTasks.length>0){
  75. await ration_glj_model.bulkWrite(rationGLJTasks);
  76. }
  77. let newGljList = [];
  78. if(data.ration.add.length>0){//新增的安装子目要增加对应的工料机
  79. for(let nr of data.ration.add){
  80. for(let tkey in nr.glj){
  81. newGljList.push(await addInstallationGLJ(nr.glj[tkey]));
  82. }
  83. }
  84. }
  85. if(newGljList.length>0){
  86. await ration_glj_model.insertMany(newGljList);
  87. }
  88. result.update = updateList;
  89. result.add = newGljList;
  90. return result;
  91. }
  92. async function addInstallationGLJ(glj) {
  93. glj.ID = uuidV1();
  94. let info = await ration_glj_facade.getInfoFromProjectGLJ(glj);
  95. let newRecode = ration_glj_facade.createNewRecord(info);
  96. return newRecode;
  97. }
  98. function generateTasks(data,userID) {
  99. let tasks=[];
  100. let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: userID};
  101. if(data.delete && data.delete.length > 0){
  102. for(let bd of data.delete){
  103. //原先是假删除,现在改成真删除
  104. let task = {
  105. deleteOne:{
  106. filter:{
  107. ID:bd.ID,
  108. projectID:bd.projectID
  109. }
  110. }
  111. };
  112. /* let task={
  113. updateOne:{
  114. filter:{
  115. ID:bd.ID,
  116. projectID:bd.projectID
  117. },
  118. update :{
  119. deleteInfo:deleteInfo
  120. }
  121. }
  122. };*/
  123. tasks.push(task);
  124. }
  125. }
  126. if(data.add && data.add.length > 0){
  127. for(let n_data of data.add){
  128. let task = {
  129. insertOne :{
  130. document:n_data
  131. }
  132. };
  133. tasks.push(task);
  134. }
  135. }
  136. return tasks;
  137. }
  138. async function updateNodes(datas){
  139. let nodeGroups = _.groupBy(datas,'type');
  140. let rationTasks = [];
  141. let billTasks = [];
  142. let rationGLJTasks = [];
  143. let projectGLJTasks = [];
  144. let projectTasks = [];
  145. let rationTemplateTasks = [];
  146. let asyncTasks = [];
  147. for(let type in nodeGroups){
  148. for(let node of nodeGroups[type]){
  149. if(type == projectConsts.BILLS){
  150. billTasks.push(getTask(node));
  151. }else if(type == projectConsts.RATION){
  152. rationTasks.push(getTask(node));
  153. }else if(type == projectConsts.RATION_GLJ){
  154. rationGLJTasks.push(getTask(node));
  155. }else if(type == projectConsts.PROJECTGLJ){
  156. projectGLJTasks.push(getTask(node,'id'));
  157. }else if(type == projectConsts.PROJECT){
  158. projectTasks.push(getTask(node));
  159. }else if(type == projectConsts.RATION_TEMPLATE){
  160. rationTemplateTasks.push(getTask(node))
  161. }
  162. }
  163. }
  164. rationTasks.length>0?asyncTasks.push(ration_model.model.bulkWrite(rationTasks)):'';
  165. billTasks.length>0?asyncTasks.push(bill_model.model.bulkWrite(billTasks)):"";
  166. rationGLJTasks.length>0?asyncTasks.push(ration_glj_model.bulkWrite(rationGLJTasks)):"";
  167. projectGLJTasks.length>0?asyncTasks.push(project_glj_model.bulkWrite(projectGLJTasks)):"";
  168. projectTasks.length>0?asyncTasks.push(projectsModel.bulkWrite(projectTasks)):"";
  169. rationTemplateTasks.length>0?asyncTasks.push(rationTemplateModel.bulkWrite(rationTemplateTasks)):"";
  170. return asyncTasks.length>0?await Promise.all(asyncTasks):"";
  171. function getTask(node,idFiled = 'ID') {
  172. let task={
  173. updateOne:{
  174. filter:{},
  175. update :_.cloneDeep(node.data)
  176. }
  177. };
  178. task.updateOne.filter[idFiled] = node.data[idFiled];//现在复制项目也重新生成一个新的ID了,所以ID是唯一的
  179. delete task.updateOne.update[idFiled];//防止误操作
  180. return task;
  181. }
  182. }
  183. /*function updateNodes(datas,callback) {
  184. let tasks = [];
  185. for(let node of datas){
  186. tasks.push(updateOne(node))
  187. }
  188. async_n.parallel(tasks, function(err, results) {
  189. if (!err){
  190. callback(0, '', results);
  191. }
  192. else{
  193. console.log(err);
  194. callback(1, 'save project failed'+err.message, null);
  195. }
  196. });
  197. function updateOne(node) {
  198. if(node.type == projectConsts.BILLS){
  199. return function (asCallback) {
  200. bill_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  201. }
  202. }else if(node.type ==projectConsts.RATION){
  203. return function (asCallback) {
  204. ration_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  205. }
  206. }
  207. }
  208. }*/
  209. //data = {feeRateID:111111,projectID:1245}; type = feeRate
  210. async function markUpdateProject(data,type) {
  211. let query = {deleteInfo:null};
  212. if(type=="feeRate"){//更改了费率
  213. query['property.feeFile.id'] = data.feeRateID;
  214. }
  215. if(type=="unitFile"){//更改了单价文件
  216. query['property.unitPriceFile.id'] = data.unitFileID;//unitPriceFile
  217. }
  218. let projects = await projectsModel.find(query);
  219. return await markProjectsToChange(projects,type,data.projectID);
  220. }
  221. async function markProjectsToChange(projects,type,extProjectID){
  222. let tasks=[];
  223. for(let p of projects){
  224. if(extProjectID && p.ID===extProjectID) continue;//排除当前项目
  225. tasks.push(generateMarkTask(type,p.ID));
  226. }
  227. return tasks.length>0 ? await projectsModel.bulkWrite(tasks):null;
  228. }
  229. async function removeProjectMark(projectID) {
  230. return await projectsModel.findOneAndUpdate({ID:projectID},{"$unset":{"changeMark":1}});
  231. }
  232. function generateMarkTask(value,projectID) {
  233. let task = {
  234. updateOne:{
  235. filter:{
  236. ID:projectID
  237. },
  238. update:{
  239. changeMark:value
  240. }
  241. }
  242. };
  243. return task
  244. }
  245. // {projectID: 5, propertyName: 'aaa', propertyValue: 1}
  246. function saveProperty(data, callback){
  247. let obj = {};
  248. let pn = 'property.' + data.propertyName;
  249. obj[pn] = data.propertyValue;
  250. projectsModel.update({"ID": data.projectID}, obj, function (err) {
  251. if (err) {
  252. logger.err(pn + ' save error: ' + err);
  253. callback(err, null)
  254. } else {
  255. logger.info(pn + ' saved.');
  256. callback('', null);
  257. }}
  258. );
  259. }
  260. async function getDefaultColSetting(libID){
  261. return await stdColSettingModel.findOne({ID: libID, deleted: false}, '-_id main_tree_col');
  262. }
  263. async function getBudgetSummayDatas(projectIDs){
  264. try {
  265. let projects = [];
  266. let names = [];
  267. let prjTypeNames = [];
  268. let compilationScopes = [];
  269. let decimal = null;
  270. for(let ID of projectIDs){
  271. projects.push(await getBillsByProjectID(ID)) ;
  272. }
  273. if(projects.length == 0){
  274. return [];
  275. }
  276. let mp = projects[0];
  277. names.push(mp.name);
  278. prjTypeNames.push(mp.prjTypeName);
  279. compilationScopes.push(mp.compilationScope);
  280. for(let i = 1;i<projects.length;i++){
  281. names.push(projects[i].name);
  282. prjTypeNames.push(projects[i].prjTypeName);
  283. compilationScopes.push(projects[i].compilationScope);
  284. decimal = await mergeProject(mp.roots,projects[i].roots)
  285. }
  286. let SummaryAuditDetail = getReportData(names,mp.roots,prjTypeNames,compilationScopes,decimal);
  287. let parentProject = await projectsModel.findOne({ID:mp.ParentID});
  288. let result = {
  289. prj: {},
  290. SummaryAudit:{
  291. "name": parentProject?parentProject.name:"",
  292. "编制": mp.author,
  293. "复核": mp.auditor,
  294. "编制范围":mp.compilationScope
  295. },
  296. SummaryAuditDetail:SummaryAuditDetail
  297. };
  298. return result;
  299. }catch (e){
  300. console.log(e)
  301. }
  302. }
  303. function getReportData(nameList,items,prjTypeNames,compilationScopes,decimal) {
  304. let datas = [],totalItem = null;
  305. setChildrenDatas(items,datas);
  306. for(let d of datas){
  307. if(d.billsTtlPrice&&totalItem.billsTtlPrice){
  308. d['各项费用比例'] = scMathUtil.roundForObj(d.billsTtlPrice/totalItem.billsTtlPrice * 100,2)
  309. }
  310. d['prjNames'] = nameList;
  311. d['prjTypeNames'] = prjTypeNames;
  312. d['编制范围明细'] = compilationScopes;
  313. }
  314. return datas;
  315. function setChildrenDatas(children,arr,level = 0) {
  316. for(let c of children){
  317. arr.push(getBillDatas(c,level));
  318. setChildrenDatas(c.children,arr,level+1);
  319. }
  320. }
  321. function getBillDatas(bills,level) {
  322. let tem = {
  323. billsName:bills.name,
  324. billsCode:bills.code,
  325. billsUnit:bills.unit,
  326. billsTtlAmt:bills.quantity,
  327. billsPrices:[],
  328. billsUnitPrices:[],
  329. billsAmounts:[],
  330. '技术经济指标':[],
  331. billsLevel:level,
  332. billsMemos:bills.remark
  333. };
  334. let total = 0;
  335. for(let n of nameList){
  336. let p = 0;//金额
  337. let up =0;//单价
  338. if(bills.unitPrices[n]) up = scMathUtil.roundForObj(bills.unitPrices[n],decimal.bills.unitPrice);
  339. tem.billsUnitPrices.push(up);
  340. if(bills.prices[n]){
  341. p = scMathUtil.roundForObj(bills.prices[n],decimal.bills.totalPrice);
  342. total = scMathUtil.roundForObj(p+total,decimal.process);
  343. }
  344. tem.billsPrices.push(p);
  345. if(bills.quantityMap[n]){
  346. tem.billsAmounts.push(bills.quantityMap[n]);
  347. tem['技术经济指标'].push(scMathUtil.roundForObj(p/bills.quantityMap[n],2));
  348. }else {
  349. tem.billsAmounts.push(0);
  350. tem['技术经济指标'].push(0);
  351. }
  352. }
  353. tem.billsTtlPrice = scMathUtil.roundForObj(total,decimal.bills.totalPrice);
  354. tem['技术经济综合指标'] = tem.billsTtlAmt?scMathUtil.roundForObj(tem.billsTtlPrice/tem.billsTtlAmt,2):0;
  355. if(bills.flag == fixedFlag.TOTAL_COST) totalItem = tem;
  356. return tem
  357. }
  358. }
  359. async function mergeProject(main,sub) {//合并两个项目
  360. let decimal = await decimal_facade.getProjectDecimal(main[0].projectID);
  361. let project = await projectsModel.findOne({ID:main[0].projectID});
  362. let notMatchList = [];
  363. for(let s of sub){
  364. //先找有没有相同的大项费用
  365. let same = findTheSameItem(main,s);
  366. same?await mergeItem(same,s,decimal,project._doc):notMatchList.push(s);//如果找到,则合并,找不到就放在未匹配表
  367. }
  368. for(let n of notMatchList){
  369. main.push(n);
  370. }
  371. return decimal;
  372. }
  373. async function mergeItem(a,b,decimal,project) {
  374. let bqDecimal = await decimal_facade.getBillsQuantityDecimal(a.projectID,a.unit,project);
  375. a.quantity = a.quantity?scMathUtil.roundForObj(a.quantity,bqDecimal):0;
  376. b.quantity = b.quantity?scMathUtil.roundForObj(b.quantity,bqDecimal):0;
  377. a.quantity = scMathUtil.roundForObj(a.quantity+b.quantity,decimal.process);
  378. for(let name in b.prices){
  379. a.prices[name] = b.prices[name];
  380. a.quantityMap[name] = b.quantityMap[name];
  381. a.unitPrices[name]=b.unitPrices[name];
  382. }
  383. for(let name in a.quantityMap){
  384. a.quantityMap[name] = a.quantityMap[name]?scMathUtil.roundForObj(a.quantityMap[name],bqDecimal):0;
  385. }
  386. await mergeChildren(a,b,decimal,project);
  387. }
  388. async function mergeChildren(a,b,decimal,project) {
  389. let notMatchList = [];
  390. if(a.children.length > 0 && b.children.length ==0){
  391. return;
  392. }else if(a.children.length == 0 && b.children.length > 0){
  393. a.children = b.children;
  394. return;
  395. }
  396. //=============剩下的是两者都有的情况
  397. for(let s of b.children){
  398. let same = findTheSameItem(a.children,s);
  399. same?await mergeItem(same,s,decimal,project):notMatchList.push(s);//如果找到,则合并,找不到就放在未匹配表
  400. }
  401. for(let n of notMatchList){
  402. let match = false;//符合插入标记
  403. //对于未匹配的子项,如果是固定清单:第100章至700章清单的子项,要匹配名字中的数字来做排充
  404. if(a.flag == fixedFlag.ONE_SEVEN_BILLS){
  405. for(let i = 0;i< a.children.length;i++){
  406. let m_name = a.children[i].name.replace(/[^0-9]/ig,"");
  407. let s_name = n.name.replace(/[^0-9]/ig,"");
  408. m_name = parseFloat(m_name);
  409. s_name = parseFloat(s_name);
  410. if(m_name&&s_name){
  411. if(m_name == s_name){
  412. await mergeItem(a.children[i],n,project);
  413. match = true;
  414. break;
  415. }
  416. if(m_name > s_name){//主节点名字中的数字大于被插节点,则被插节点放在主节点前面
  417. a.children.splice(i,0,n);
  418. match = true;
  419. break;
  420. }
  421. }
  422. }
  423. }else {//其它的子项按编号进行排序
  424. for(let i = 0;i< a.children.length ; i++){
  425. let m_code = a.children[i].code;
  426. let s_code = n.code;
  427. if(m_code && s_code && m_code!=""&&s_code!=""){
  428. if(m_code > s_code){
  429. a.children.splice(i,0,n);
  430. match = true;
  431. break;
  432. }
  433. }
  434. }
  435. }
  436. if(match == false)a.children.push(n) //没有插入成功,直接放到最后面
  437. }
  438. }
  439. function findTheSameItem(main,item) {//编号名称单位三个相同,认为是同一条清单
  440. return _.find(main,function (tem) {
  441. return isEqual(tem.code,item.code)&&isEqual(tem.name,item.name)&&isEqual(tem.unit,item.unit);
  442. })
  443. }
  444. function isEqual(a,b) {//粗略匹配,null undefind "" 认为相等
  445. return getValue(a)==getValue(b);
  446. function getValue(t) {
  447. if(t==null||t==undefined||t=="") return null;
  448. return t;
  449. }
  450. }
  451. async function getBillsByProjectID(projectID){
  452. let roots=[],parentMap={};
  453. let bills = await bill_model.model.find({projectID: projectID}, '-_id');//取出所有清单
  454. let project = await projectsModel.findOne({ID:projectID});
  455. if(!project) throw new Error(`找不到项目:${projectID}`);
  456. let projectName = project.name;
  457. let author='';//编制人
  458. let auditor='';//审核人
  459. let compilationScope='';//编制范围
  460. let engineering='';//养护类别
  461. if(project.property&&project.property.projectFeature){
  462. for(let f of project.property.projectFeature){
  463. if(f.key == 'author') author = f.value;
  464. if(f.key == 'auditor') auditor = f.value;
  465. if(f.key =='compilationScope') compilationScope = f.value;
  466. if(f.key == 'engineering') engineering = f.value;
  467. }
  468. }
  469. for(let b of bills){
  470. let commonFee =_.find(b._doc.fees,{"fieldName":"common"});
  471. let prices = {};
  472. let quantityMap={};
  473. let unitPrices ={};
  474. if(commonFee&&commonFee.totalFee) prices[projectName] = commonFee.totalFee;
  475. if (commonFee&&commonFee.unitFee) unitPrices[projectName] = commonFee.unitFee;
  476. quantityMap[projectName] = b.quantity;
  477. let flagIndex = _.find(b._doc.flags,{'fieldName':'fixed'});
  478. let doc = {ID:b.ID,name:b.name,code:b.code,unit:b.unit,projectID:b.projectID, ParentID:b.ParentID,NextSiblingID:b.NextSiblingID,unitPrices:unitPrices,quantity:b.quantity,prices:prices,quantityMap:quantityMap,flag:flagIndex?flagIndex.flag:-99,remark:b.remark};//选取有用字段
  479. if(b.ParentID == -1) roots.push(doc);
  480. parentMap[b.ParentID]?parentMap[b.ParentID].push(doc):parentMap[b.ParentID]=[doc];
  481. }//设置子节点
  482. for(let r of roots){
  483. setChildren(r,parentMap,1);
  484. }
  485. roots = sortChildren(roots);
  486. return {name:projectName,roots:roots,author:author,auditor:auditor,compilationScope:compilationScope,ParentID:project.ParentID,prjTypeName:engineering}
  487. }
  488. function setChildren(bill,parentMap,level) {
  489. let children = parentMap[bill.ID];
  490. if(children){
  491. for(let c of children){
  492. setChildren(c,parentMap,level+1)
  493. }
  494. bill.children = children;
  495. }else {
  496. bill.children = [];
  497. }
  498. }
  499. function sortChildren(lists) {
  500. let IDMap ={},nextMap = {}, firstNode = null,newList=[];
  501. for(let l of lists){
  502. if(l.children&&l.children.length > 0) l.children = sortChildren(l.children);//递规排序
  503. IDMap[l.ID] = l;
  504. if(l.NextSiblingID!=-1) nextMap[l.NextSiblingID] = l;
  505. }
  506. for(let t of lists){
  507. if(!nextMap[t.ID]){ //如果在下一节点映射没找到,则是第一个节点
  508. firstNode = t;
  509. break;
  510. }
  511. }
  512. if(firstNode){
  513. newList.push(firstNode);
  514. delete IDMap[firstNode.ID];
  515. setNext(firstNode,newList);
  516. }
  517. //容错处理,如果链断了的情况,直接添加到后面
  518. for(let key in IDMap){
  519. if(IDMap[key]) newList.push(IDMap[key])
  520. }
  521. return newList;
  522. function setNext(node,array) {
  523. if(node.NextSiblingID != -1){
  524. let next = IDMap[node.NextSiblingID];
  525. if(next){
  526. array.push(next);
  527. delete IDMap[next.ID];
  528. setNext(next,array);
  529. }
  530. }
  531. }
  532. }
  533. async function getGLJSummayDatas(projectIDs) {
  534. let projects = [];
  535. let names = [];
  536. let prjTypeNames = [];
  537. try {
  538. for(let ID of projectIDs){
  539. projects.push(await getProjectData(ID)) ;
  540. }
  541. if(projects.length == 0){
  542. return [];
  543. }
  544. let mp = projects[0];
  545. for(let p of projects){
  546. names.push(p.name);
  547. prjTypeNames.push(p.prjTypeName);
  548. p.gljList = await getProjectGLJData(p.ID,p.unitPriceFileId,mp.property);
  549. }
  550. let mList = mergeGLJ(mp,projects,names,prjTypeNames);
  551. mList = gljUtil.sortProjectGLJ(mList,_);
  552. let summaryGLJDatas = getSummaryGLJDatas(mList,mp.property.decimal,names,prjTypeNames);
  553. let parentProject = await projectsModel.findOne({ID:mp.ParentID});
  554. let result = {
  555. prj: {},
  556. SummaryAudit:{
  557. "name": parentProject?parentProject.name:"",
  558. "编制": mp.author,
  559. "复核": mp.auditor,
  560. "编制范围":mp.compilationScope
  561. },
  562. SummaryAuditDetail:summaryGLJDatas
  563. };
  564. return result;
  565. }catch (e){
  566. console.log(e);
  567. }
  568. }
  569. function getSummaryGLJDatas(gljList,decimal,nameList,prjTypeNames) {
  570. let datas = [],qdecimal = decimal.glj.quantity,process = decimal.process;
  571. for(let tem of gljList){
  572. let d = {
  573. code:tem.code,
  574. name:tem.name,
  575. type:tem.type,
  576. unit:tem.unit,
  577. specs:tem.specs,
  578. marketPrice:tem.marketPrice,
  579. prjNames:nameList,
  580. prjTypeNames:prjTypeNames,
  581. quantityList:[]
  582. };
  583. let totalQuantity = 0;
  584. for(let n of nameList){
  585. let q = tem.quantityMap[n]?scMathUtil.roundForObj(tem.quantityMap[n],qdecimal):0;
  586. totalQuantity = scMathUtil.roundForObj(q+totalQuantity,process);
  587. d.quantityList.push(q);
  588. }
  589. d.totalQuantity = scMathUtil.roundForObj(totalQuantity,qdecimal);
  590. datas.push(d);
  591. }
  592. return datas;
  593. }
  594. function mergeGLJ(mp,projects) {
  595. let gljMap = {},gljList=[];
  596. for(let g of mp.gljList){
  597. g.quantityMap={};
  598. g.quantityMap[mp.name] = g.quantity;
  599. gljMap[gljUtil.getIndex(g)] = g;
  600. gljList.push(g);
  601. }
  602. for(let i = 1;i<projects.length;i++){
  603. let temList = projects[i].gljList;
  604. for(let t of temList){
  605. t.quantityMap={};
  606. t.quantityMap[projects[i].name] = t.quantity;
  607. //这里除了5个属性相同判断为同一个之外,还要判断市场价相同,才认为是同一个工料机
  608. let connect_key = gljUtil.getIndex(t);
  609. let g = gljMap[connect_key];
  610. if(g&&g.marketPrice == t.marketPrice){
  611. g.quantityMap[projects[i].name] = t.quantity;
  612. }else {
  613. gljMap[connect_key] = t;
  614. gljList.push(t);
  615. }
  616. }
  617. }
  618. return gljList;
  619. }
  620. async function getProjectGLJData(projectID,unitPriceFileId,property){
  621. //取项目工料机数据
  622. let projectGLJDatas = await getProjectGLJPrice(projectID,unitPriceFileId,property);
  623. await calcProjectGLJQuantity(projectID,projectGLJDatas,property);
  624. _.remove(projectGLJDatas.gljList,{'quantity':0});
  625. return projectGLJDatas.gljList;
  626. }
  627. async function getProjectData(projectID){
  628. let project = await projectsModel.findOne({ID:projectID});
  629. if(!project) throw new Error(`找不到项目:${projectID}`);
  630. let projectName = project.name;
  631. let author='';//编制人
  632. let auditor='';//审核人
  633. let compilationScope='';//编制范围
  634. let engineering='';//养护类别
  635. if(project.property&&project.property.projectFeature){
  636. for(let f of project.property.projectFeature){
  637. if(f.key == 'author') author = f.value;
  638. if(f.key == 'auditor') auditor = f.value;
  639. if(f.key =='compilationScope') compilationScope = f.value;
  640. if(f.key == 'engineering') engineering = f.value;
  641. }
  642. }
  643. if(!(project.property&&project.property.unitPriceFile)) throw new Error(`找不到单价文件:${projectID}`);
  644. let unitPriceFileId = project.property.unitPriceFile.id;
  645. return {ID:projectID,name:projectName,author:author,auditor:auditor,compilationScope:compilationScope,ParentID:project.ParentID,prjTypeName:engineering,property:project.property,unitPriceFileId:unitPriceFileId}
  646. }
  647. async function getProjectGLJPrice(projectID,unitPriceFileId,property){
  648. //取项目工料机数据
  649. let calcOptions=property.calcOptions;
  650. let decimalObj = property.decimal;
  651. let labourCoeDatas = [];//取调整价才需要用到
  652. let gljListModel = new GLJListModel();
  653. let [gljList, mixRatioConnectData,mixRatioMap,unitPriceMap] = await gljListModel.getListByProjectId(projectID, unitPriceFileId);
  654. gljList = JSON.parse(JSON.stringify(gljList));
  655. for(let glj of gljList){
  656. let result = gljUtil.getGLJPrice(glj,{gljList:gljList},calcOptions,labourCoeDatas,decimalObj,false,_,scMathUtil);
  657. glj.marketPrice = result.marketPrice;
  658. glj.basePrice = result.basePrice;
  659. }
  660. return {gljList:gljList,mixRatioMap:mixRatioMap};
  661. }
  662. async function calcProjectGLJQuantity(projectID,projectGLJDatas,property){
  663. let q_decimal = property.decimal.glj.quantity;
  664. let rationGLJDatas = await ration_glj_model.find({'projectID':projectID});
  665. let rationDatas = await ration_model.model.find({'projectID':projectID});
  666. gljUtil.calcProjectGLJQuantity(projectGLJDatas,rationGLJDatas,rationDatas,[],q_decimal)
  667. }