project_facade.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /**
  2. * Created by zhang on 2018/1/26.
  3. */
  4. module.exports = {
  5. sortChildren,
  6. markUpdateProject:markUpdateProject,
  7. removeProjectMark:removeProjectMark,
  8. updateNodes:updateNodes,
  9. calcInstallationFee:calcInstallationFee,
  10. calcOverHeightFee: calcOverHeightFee,
  11. saveProperty: saveProperty,
  12. getDefaultColSetting: getDefaultColSetting,
  13. markProjectsToChange:markProjectsToChange,
  14. getSEIProjects:getSEIProjects,
  15. loadSEIProjectData:loadSEIProjectData,
  16. setSEILibData:setSEILibData,
  17. getIndexReportData:getIndexReportData,
  18. setDefaultItemIncrease:setDefaultItemIncrease,
  19. getCompareProjects:getCompareProjects,
  20. };
  21. let mongoose = require('mongoose');
  22. let logger = require("../../../logs/log_helper").logger;
  23. let projectsModel = mongoose.model('projects');
  24. let async_n = require("async");
  25. let _ = require('lodash');
  26. let ration_model = require('../models/ration');
  27. let bill_model = require('../models/bills');
  28. let consts = require('../models/project_consts');
  29. let projectConsts = consts.projectConst;
  30. let ration_glj_model = mongoose.model('ration_glj');
  31. let rationTemplateModel = mongoose.model('ration_template');
  32. let project_glj_model = mongoose.model('glj_list');
  33. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  34. let glj_calculate_facade = require("../../ration_glj/facade/glj_calculate_facade");
  35. const uuidV1 = require('uuid/v1');
  36. const gljUtil = require('../../../public/gljUtil');
  37. let stdColSettingModel = mongoose.model('std_main_col_lib');
  38. const GLJListModel = require("../../glj/models/glj_list_model");
  39. let economicLib = mongoose.model('std_economic_lib');
  40. let engineerFeatureLib = mongoose.model('std_engineer_feature_lib');
  41. let engineerInfoLib = mongoose.model('std_engineer_info_lib');
  42. let mainQuantityLib = mongoose.model('std_main_quantity_lib');
  43. let materialLib = mongoose.model('std_material_lib');
  44. const { fixedFlag } = require('../../../public/common_constants');
  45. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  46. const billsLibDao = require("../../bills_lib/models/bills_lib_interfaces");
  47. async function calcInstallationFee(data) {
  48. let result={};
  49. let projectGLJList = [];
  50. let billTasks = await generateTasks(data.bills,data.useID,true);
  51. let rationTasks =await generateTasks(data.ration,data.useID);
  52. if(billTasks.length>0){
  53. await bill_model.model.bulkWrite(billTasks);
  54. }
  55. if(rationTasks.length>0){
  56. await ration_model.model.bulkWrite(rationTasks);
  57. }
  58. //如果删除定额,需要删除对应的工料机
  59. if(data.ration.delete.length>0){
  60. let rationIDS = _.map(data.ration.delete,'ID');
  61. await ration_glj_model.deleteMany({projectID: data.ration.delete[0].projectID, rationID: {"$in": rationIDS}});//删除定额工料机
  62. }
  63. let rationGLJTasks = [];
  64. let updateList = [];
  65. if(data.ration.update.length>0){//如果有需要更新的定额工料机
  66. for(let ur of data.ration.update){
  67. for(let g of ur.glj){
  68. let gTasks = {
  69. updateOne:{
  70. filter:{
  71. ID:g.ID,
  72. projectID:g.projectID
  73. },
  74. update :{
  75. quantity:g.quantity,
  76. rationItemQuantity:g.rationItemQuantity
  77. }
  78. }
  79. };
  80. rationGLJTasks.push(gTasks);
  81. updateList.push(g);
  82. }
  83. }
  84. }
  85. if(rationGLJTasks.length>0){
  86. await ration_glj_model.bulkWrite(rationGLJTasks);
  87. }
  88. let newGljList = [];
  89. if(data.ration.add.length>0){//新增的安装子目要增加对应的工料机
  90. for(let nr of data.ration.add){
  91. for(let tkey in nr.glj){
  92. let [newRecode,projectGLJ] = await createRationGLJData(nr.glj[tkey])
  93. newGljList.push(newRecode);
  94. projectGLJList.push(projectGLJ);
  95. }
  96. }
  97. }
  98. if(newGljList.length>0){
  99. await ration_glj_model.insertMany(newGljList);
  100. }
  101. result.update = updateList;
  102. result.add = newGljList;
  103. result.projectGLJList = projectGLJList;
  104. result.updateData = data;
  105. return result;
  106. }
  107. async function createRationGLJData(glj) {
  108. glj.ID = uuidV1();
  109. let [info,projectGLJ ] = await ration_glj_facade.getInfoFromProjectGLJ(glj);
  110. let newRecode = ration_glj_facade.createNewRecord(info);
  111. return [newRecode,projectGLJ];
  112. }
  113. async function generateTasks(data,userID,isBills=false) {
  114. let tasks=[];
  115. let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: userID};
  116. if(data.delete && data.delete.length > 0){
  117. for(let bd of data.delete){
  118. //原先是假删除,现在改成真删除
  119. let task = {
  120. deleteOne:{
  121. filter:{
  122. ID:bd.ID,
  123. projectID:bd.projectID
  124. }
  125. }
  126. };
  127. /* let task={
  128. updateOne:{
  129. filter:{
  130. ID:bd.ID,
  131. projectID:bd.projectID
  132. },
  133. update :{
  134. deleteInfo:deleteInfo
  135. }
  136. }
  137. };*/
  138. tasks.push(task);
  139. }
  140. }
  141. if(data.add && data.add.length > 0){
  142. for(let n_data of data.add){
  143. if(isBills == true){
  144. let stdBills = await billsLibDao.getStdBillsByCode({userId: n_data.userID, billsLibId: n_data.billsLibId, code: n_data.stdCode});
  145. stdBills = stdBills ? stdBills._doc : null;
  146. if (stdBills) {
  147. // 获取项目清单所需要的数据
  148. const projectBillsData = billsLibDao.getDataToProjectBills(stdBills);
  149. n_data.jobContent = projectBillsData.jobContent;
  150. n_data.itemCharacter = projectBillsData.itemCharacter;
  151. n_data.itemCharacterText = projectBillsData.itemCharacterText;
  152. n_data.jobContentText = projectBillsData.ijobContentText;
  153. } else {
  154. delete n_data.billsLibId;
  155. }
  156. delete n_data.userID;
  157. delete n_data.stdCode;
  158. }
  159. let task = {
  160. insertOne :{
  161. document:n_data
  162. }
  163. };
  164. tasks.push(task);
  165. }
  166. }
  167. return tasks;
  168. }
  169. /**
  170. * 计取超高降效费,可能包含更新清单、更新定额、新增清单、新增定额及其子数据、删除定额及其子数据
  171. * @param {Object} data - {updateData: {ration: [],..}, addData: {ration: [],...}, deleteData: {ration: [],...}}
  172. * @return {Object} - {bills: Array, rationGLJ: Array}
  173. */
  174. async function calcOverHeightFee(data) {
  175. // 更新数据
  176. async function update({ project, bills, ration }) {
  177. const tasks = [];
  178. // 更新项目
  179. if (project && project.ID) {
  180. const projectTask = projectsModel.update({ ID: project.ID },
  181. { 'property.overHeightOption': project.overHeightOption, 'property.overHeightSpecificID': project.overHeightSpecificID });
  182. tasks.push(projectTask);
  183. }
  184. // 更新清单和定额的超高降效列
  185. const models = [];
  186. if (bills.length) {
  187. models.push({ model: bill_model.model, items: bills });
  188. }
  189. if (ration.length) {
  190. models.push({ model: ration_model.model, items: ration });
  191. }
  192. models.forEach(modelData => {
  193. const bulkTask = modelData.items.map(item => (
  194. {
  195. updateOne: {
  196. filter: { ID: item.ID },
  197. update: { overHeight: item.overHeight }
  198. }
  199. }));
  200. const task = modelData.model.bulkWrite(bulkTask);
  201. tasks.push(task);;
  202. });
  203. if (!tasks.length) {
  204. return;
  205. }
  206. await Promise.all(tasks);
  207. }
  208. // 插入数据
  209. async function add({ bills, ration, rationGLJ}) {
  210. const tasks = [];
  211. // 匹配标准清单,加上工作内容等数据
  212. for (const billsItem of bills) {
  213. let stdBills = await billsLibDao.getStdBillsByCode(billsItem);
  214. stdBills = stdBills ? stdBills._doc : null;
  215. if (stdBills) {
  216. // 获取项目清单所需要的数据
  217. const projectBillsData = billsLibDao.getDataToProjectBills(stdBills);
  218. Object.assign(billsItem, projectBillsData);
  219. } else {
  220. delete billsItem.billsLibId;
  221. }
  222. billsItem.code += '001';
  223. }
  224. // 新增清单
  225. if (bills.length) {
  226. tasks.push(bill_model.model.insertMany(bills));
  227. }
  228. // 新增定额
  229. if (ration.length) {
  230. tasks.push(ration_model.model.insertMany(ration));
  231. }
  232. // 完整的定额人材机数据
  233. const completeRationGLJList = [];
  234. // 完整的项目人材机数据
  235. const completeProjectGLJList = [];
  236. // createRationGLJData方法不能并行,涉及到项目人材机的新增,如果并行可能会重复插入数据
  237. for (const rGLJ of rationGLJ) {
  238. const [completeRGLJ, completePGLJ] = await createRationGLJData(rGLJ);
  239. completeRationGLJList.push(completeRGLJ);
  240. completeProjectGLJList.push(completePGLJ);
  241. }
  242. if (completeRationGLJList.length) {
  243. tasks.push(ration_glj_model.insertMany(completeRationGLJList))
  244. }
  245. // 返回新的清单、定额人材机、项目人材机数据数据
  246. const rst = {
  247. bills,
  248. rationGLJ: completeRationGLJList,
  249. projectGLJ: completeProjectGLJList,
  250. }
  251. if (!tasks.length) {
  252. return rst;
  253. }
  254. await Promise.all(tasks);
  255. return rst;
  256. }
  257. // 删除数据
  258. async function del({ ration }) {
  259. const tasks = [];
  260. const rationIDList = ration.map(item => item.ID);
  261. if (!rationIDList.length) {
  262. return;
  263. }
  264. // 删除定额数据
  265. const rationTask = ration_model.model.deleteMany({ ID: {$in: rationIDList} });
  266. tasks.push(rationTask);
  267. // 删除定额人材机数据
  268. const rationGLJTask = ration_glj_model.deleteMany({ rationID: {$in: rationIDList} });
  269. tasks.push(rationGLJTask);
  270. await Promise.all(tasks);
  271. }
  272. // 处理任务
  273. const { updateData, addData, deleteData } = data;
  274. const updateTask = update(updateData);
  275. const addTask = add(addData);
  276. const delTask = del(deleteData);
  277. const [updateRst, addRst, delRst] = await Promise.all([updateTask, addTask, delTask]);
  278. return addRst;
  279. }
  280. async function updateNodes(datas){
  281. let nodeGroups = _.groupBy(datas,'type');
  282. let asyncTasks = [];
  283. let deleteRationIDs=[];
  284. let taskMap = {};
  285. taskMap[projectConsts.BILLS] = {tasks:[],model:bill_model.model};
  286. taskMap[projectConsts.RATION] = {tasks:[],model:ration_model.model};
  287. taskMap[projectConsts.RATION_GLJ] = {tasks:[],model:ration_glj_model};
  288. taskMap[projectConsts.PROJECTGLJ] = {tasks:[],model:project_glj_model};
  289. taskMap[projectConsts.PROJECT] = {tasks:[],model:projectsModel};
  290. taskMap[projectConsts.RATION_TEMPLATE] = {tasks:[],model:rationTemplateModel};
  291. for(let type in nodeGroups){
  292. for(let node of nodeGroups[type]){
  293. if(taskMap[type]){
  294. if(type == projectConsts.RATION){
  295. //处理面积增加费的数据
  296. if(node.data.hasOwnProperty("areaIncreaseFee")){
  297. let areaSetting = null;
  298. if(nodeGroups[projectConsts.PROJECT]){
  299. for(let pn of nodeGroups[projectConsts.PROJECT]){
  300. if(pn.data['property.areaSetting']) areaSetting = pn.data['property.areaSetting'];
  301. }
  302. }
  303. let t = await glj_calculate_facade.calculateQuantity({rationID:node.data.ID},null,false,node.data.areaIncreaseFee,areaSetting);
  304. if(t.glj_result && t.glj_result.length > 0){//这里要更新工料机的消耗量,修改数据库的操作已经在上一步中完成了,现在只需刷新前端即可
  305. for(let gr of t.glj_result){
  306. let tdata = gr.doc;
  307. tdata.ID = gr.query.ID;
  308. datas.push({type:projectConsts.RATION_GLJ,data:tdata});
  309. }
  310. }
  311. node.data.adjustState = t.adjustState;
  312. }
  313. if(node.action == "delete") deleteRationIDs.push(node.data.ID);
  314. }
  315. if(type == projectConsts.RATION_GLJ){
  316. if(node.action == "add"){//添加定額工料机的時候,要先走项目工料机的逻辑
  317. let [newRecode,projectGLJ] = await createRationGLJData(node.data);
  318. node.data =newRecode;
  319. node.projectGLJ = projectGLJ;
  320. }
  321. }
  322. taskMap[type].tasks.push(getTask(node));
  323. }
  324. }
  325. }
  326. for(let key in taskMap){
  327. if(taskMap[key].tasks.length> 0) asyncTasks.push(taskMap[key].model.bulkWrite(taskMap[key].tasks))
  328. }
  329. if(asyncTasks.length>0) await Promise.all(asyncTasks);
  330. if(deleteRationIDs.length > 0) await ration_glj_model.deleteMany({rationID: {$in: deleteRationIDs}});
  331. return datas;
  332. function getTask(node,idFiled = 'ID') {
  333. let task={};
  334. if(node.action == "add"){
  335. task.insertOne ={
  336. document:node.data
  337. }
  338. }else if(node.action =="delete"){
  339. task.deleteOne ={
  340. filter:{}
  341. };
  342. task.deleteOne.filter[idFiled] = node.data[idFiled];
  343. }else {
  344. task.updateOne = {
  345. filter:{},
  346. update :_.cloneDeep(node.data)
  347. };
  348. task.updateOne.filter[idFiled] = node.data[idFiled];//现在复制项目也重新生成一个新的ID了,所以ID是唯一的
  349. delete task.updateOne.update[idFiled];//防止误操作
  350. }
  351. return task;
  352. }
  353. }
  354. /*function updateNodes(datas,callback) {
  355. let tasks = [];
  356. for(let node of datas){
  357. tasks.push(updateOne(node))
  358. }
  359. async_n.parallel(tasks, function(err, results) {
  360. if (!err){
  361. callback(0, '', results);
  362. }
  363. else{
  364. console.log(err);
  365. callback(1, 'save project failed'+err.message, null);
  366. }
  367. });
  368. function updateOne(node) {
  369. if(node.type == projectConsts.BILLS){
  370. return function (asCallback) {
  371. bill_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  372. }
  373. }else if(node.type ==projectConsts.RATION){
  374. return function (asCallback) {
  375. ration_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  376. }
  377. }
  378. }
  379. }*/
  380. //data = {feeRateID:111111,projectID:1245}; type = feeRate
  381. async function markUpdateProject(data,type) {
  382. let query = {deleteInfo:null};
  383. if(type=="feeRate"){//更改了费率
  384. query['property.feeFile.id'] = data.feeRateID;
  385. }
  386. if(type=="unitFile"){//更改了单价文件
  387. query['property.unitPriceFile.id'] = data.unitFileID;//unitPriceFile
  388. }
  389. let projects = await projectsModel.find(query);
  390. return await markProjectsToChange(projects,type,data.projectID);
  391. }
  392. async function markProjectsToChange(projects,type,extProjectID){
  393. let tasks=[];
  394. for(let p of projects){
  395. if(extProjectID && p.ID===extProjectID) continue;//排除当前项目
  396. tasks.push(generateMarkTask(type,p.ID));
  397. }
  398. return tasks.length>0 ? await projectsModel.bulkWrite(tasks):null;
  399. }
  400. async function removeProjectMark(projectID) {
  401. return await projectsModel.findOneAndUpdate({ID:projectID},{"$unset":{"changeMark":1}});
  402. }
  403. function generateMarkTask(value,projectID) {
  404. let task = {
  405. updateOne:{
  406. filter:{
  407. ID:projectID
  408. },
  409. update:{
  410. changeMark:value
  411. }
  412. }
  413. };
  414. return task
  415. }
  416. // {projectID: 5, propertyName: 'aaa', propertyValue: 1}
  417. function saveProperty(data, callback){
  418. let obj = {};
  419. let pn = 'property.' + data.propertyName;
  420. obj[pn] = data.propertyValue;
  421. projectsModel.update({"ID": data.projectID}, obj, function (err) {
  422. if (err) {
  423. logger.err(pn + ' save error: ' + err);
  424. callback(err, null)
  425. } else {
  426. // logger.info(pn + ' saved.');
  427. callback('', null);
  428. }}
  429. );
  430. }
  431. async function getDefaultColSetting(libID){
  432. return await stdColSettingModel.findOne({ID: libID, deleted: false}, '-_id main_tree_col');
  433. }
  434. function sortChildren(lists) {
  435. let IDMap ={},nextMap = {}, firstNode = null,newList=[];
  436. for(let l of lists){
  437. if(l.children&&l.children.length > 0) l.children = sortChildren(l.children);//递规排序
  438. IDMap[l.ID] = l;
  439. if(l.NextSiblingID!=-1) nextMap[l.NextSiblingID] = l;
  440. }
  441. for(let t of lists){
  442. if(!nextMap[t.ID]){ //如果在下一节点映射没找到,则是第一个节点
  443. firstNode = t;
  444. break;
  445. }
  446. }
  447. if(firstNode){
  448. newList.push(firstNode);
  449. delete IDMap[firstNode.ID];
  450. setNext(firstNode,newList);
  451. }
  452. //容错处理,如果链断了的情况,直接添加到后面
  453. for(let key in IDMap){
  454. if(IDMap[key]) newList.push(IDMap[key])
  455. }
  456. return newList;
  457. function setNext(node,array) {
  458. if(node.NextSiblingID != -1){
  459. let next = IDMap[node.NextSiblingID];
  460. if(next){
  461. array.push(next);
  462. delete IDMap[next.ID];
  463. setNext(next,array);
  464. }
  465. }
  466. }
  467. }
  468. async function getSEIProjects(projectID) {
  469. let project = await projectsModel.findOne({ID:projectID});
  470. if(!project) throw new Error(`找不到建设项目:${projectID}`);
  471. let tem_e = await projectsModel.find({ParentID:project.ID});
  472. let engineers = [];
  473. for(let e of tem_e){
  474. if(!e.deleteInfo || !e.deleteInfo.deleted){
  475. let tenders = await projectsModel.find({ParentID:e.ID});
  476. let children = [];
  477. for(let t of tenders){
  478. if(!t.deleteInfo || !t.deleteInfo.deleted){
  479. children.push(t);
  480. }
  481. }
  482. e._doc.children = children;
  483. engineers.push(e);
  484. }
  485. }
  486. engineers = sortChildren(engineers);
  487. project._doc.children = engineers;
  488. return project;
  489. }
  490. async function loadSEIProjectData(projectID) {
  491. let gljListModel = new GLJListModel();
  492. let [gljList, mixRatioConnectData,mixRatioMap,unitPriceMap] = await gljListModel.getListByProjectId(projectID);
  493. let rations = await ration_model.getDataSync(projectID);
  494. let bills = await bill_model.getDataSync(projectID);
  495. let ration_gljs = await ration_glj_model.find({projectID:projectID});
  496. let projectGLJs = {
  497. gljList : gljList,
  498. mixRatioConnectData:mixRatioConnectData,
  499. mixRatioMap:mixRatioMap,
  500. unitPriceMap:unitPriceMap
  501. };
  502. return{bills:bills,rations:rations,ration_gljs:ration_gljs,projectGLJs:projectGLJs}
  503. }
  504. //从建设项目中取工程信息默认值
  505. async function setDefaultInfo(rootProjectID,engineerInfos){
  506. let rootProject = await projectsModel.findOne({ID:rootProjectID}).lean();
  507. if(rootProject){
  508. let baseInfo = _.find(rootProject.property.basicInformation,{key:"basicInfo"});
  509. if(!baseInfo) return;
  510. let baseMap = {};
  511. let attrs = ["工程地点","开工日期","竣工日期","编制日期"];
  512. for(let item of baseInfo.items){
  513. baseMap[item.dispName] = item;
  514. }
  515. for(let en of engineerInfos){
  516. if(attrs.indexOf(en.dispName) != -1){
  517. let key = en.dispName;
  518. if(key == "工程地点") key = "工程所在地";
  519. en.value = baseMap[key] && baseMap[key].value || '';
  520. }
  521. }
  522. }
  523. }
  524. async function setSEILibData(property){
  525. if(property.engineerInfoLibID){//工程信息指标
  526. let engineerInfo = await engineerInfoLib.findOne({'ID':property.engineerInfoLibID}).lean();
  527. if(engineerInfo){
  528. property.engineerInfos = engineerInfo.info;
  529. await setDefaultInfo(property.rootProjectID,property.engineerInfos);
  530. }
  531. }
  532. if(property.engineerFeatureLibID){//工程特征指标
  533. let engineerFeature = await engineerFeatureLib.findOne({'ID':property.engineerFeatureLibID});
  534. if(engineerFeature) property.engineerFeatures = engineerFeature.features;
  535. }
  536. if(property.materialLibID){//主要工料指标
  537. let material = await materialLib.findOne({'ID':property.materialLibID});
  538. if(material) property.materials = material.materials;
  539. }
  540. if(property.mainQuantityLibID){//主要工程量指标
  541. let mainQuantity = await mainQuantityLib.findOne({'ID':property.mainQuantityLibID});
  542. if(mainQuantity) property.mainQuantities = mainQuantity.index;
  543. }
  544. if(property.economicLibID){//主要工程量指标
  545. let economic = await economicLib.findOne({'ID':property.economicLibID});
  546. if(economic) property.economics = economic.index;
  547. }
  548. }
  549. function setDefaultItemIncrease(property) {
  550. property.itemIncreaseSetting = {
  551. isCalc : false,
  552. setting:[
  553. {"name":"在生产车间边生产边施工","base":"人工费","coe":10,"scope":{}},
  554. {"name":"在有害身体健康的场所内施工","base":"人工费","coe":10,"scope":{}},
  555. {"name":"在洞内、地下室内、库内或暗室内进行施工","base":"人工费","coe":40,"scope":{}},
  556. {"name":"在洞内、地下室内、库内或暗室内进行施工(模板拆除)","base":"人工费","coe":10,"scope":{}}
  557. ]
  558. }
  559. }
  560. async function getIndexReportData(projectID,keyArr) {
  561. let project = await projectsModel.findOne({ID:projectID});
  562. let bills = await bill_model.getDataSync(projectID);
  563. let result = {};
  564. for(let key of keyArr){
  565. switch (key) {
  566. case 'ProjectCostFields':
  567. result[key] =getEngineerCostData(project.property,bills);
  568. break;
  569. case 'ProjectEcoFields':
  570. result[key] = getEconomicDatas(project.property,bills);
  571. break;
  572. case 'ProjectLabMaterialFields':
  573. result[key] = await getMainMaterialDatas(projectID,project.property);
  574. break;
  575. case 'ProjectQtyFields':
  576. result[key] = await getQuantityDatas(project.property,bills);
  577. break;
  578. case 'ProjectInfoFields':
  579. result[key] = getEngineerInfoData(project.property.engineerInfos);
  580. break;
  581. case 'ProjectFeatureFields':
  582. result[key] = getEngineerFeaturesDatas(project.property.engineerFeatures);
  583. break;
  584. }
  585. }
  586. return result
  587. }
  588. function getEngineerInfoData(engineerInfos) {
  589. let datas = [];
  590. for(let info of engineerInfos){
  591. let d = {
  592. name:info.dispName,
  593. value:info.value
  594. };
  595. datas.push(d);
  596. }
  597. return datas;
  598. }
  599. function getEngineerFeaturesDatas(engineerFeatures) {
  600. let datas = [];
  601. if (engineerFeatures !== null && engineerFeatures !== undefined) {
  602. for(let f of engineerFeatures){
  603. let tem = {
  604. ID:f.ID,
  605. name:f.name,
  606. value:f.value,
  607. ParentID:f.ParentID
  608. }
  609. datas.push(tem);
  610. }
  611. }
  612. return datas;
  613. }
  614. function getQuantityDatas(property,bills) {
  615. return gljUtil.getQuantityDatas(property.engineerFeatures,property.mainQuantities,bills,fixedFlag,_,scMathUtil,property.decimal)
  616. }
  617. function getEconomicDatas(property,bills) {
  618. return gljUtil.getEconomicDatas(property.engineerFeatures,property.economics,bills,fixedFlag,_,scMathUtil,property.decimal)
  619. }
  620. function getEngineerCostData(property,bills){
  621. let datas = [];
  622. let priceIndex = gljUtil.getEngineerCostData(property,bills,fixedFlag,scMathUtil);
  623. for(let c of priceIndex.children){
  624. let tem = {
  625. name:c.name,
  626. cost:parseFloat(c.attrs[0].value),
  627. unitCost:parseFloat(c.attrs[1].value),
  628. per:parseFloat(c.attrs[2].value)
  629. };
  630. datas.push(tem);
  631. }
  632. return datas;
  633. }
  634. async function getMainMaterialDatas(projectID,property) {
  635. let gljListModel = new GLJListModel();
  636. let [gljList, mixRatioConnectData,mixRatioMap,unitPriceMap] = await gljListModel.getListByProjectId(projectID, property.unitPriceFile?property.unitPriceFile.id:null);
  637. gljList = JSON.parse(JSON.stringify(gljList));
  638. await calcProjectGLJQuantity(projectID,{gljList:gljList,mixRatioMap:mixRatioMap},property);
  639. return gljUtil.getMainMaterialDatas(property,property.engineerFeatures,property.materials,{gljList:gljList},property.calcOptions,property.decimal,false,_,scMathUtil)
  640. }
  641. async function calcProjectGLJQuantity(projectID,projectGLJDatas,property){
  642. let q_decimal = property.decimal.glj.quantity;
  643. let rationGLJDatas = await ration_glj_model.find({'projectID':projectID});
  644. let rationDatas = await ration_model.model.find({'projectID':projectID});
  645. gljUtil.calcProjectGLJQuantity(projectGLJDatas,rationGLJDatas,rationDatas,[],q_decimal)
  646. }
  647. async function getCompareProjects(userID, compilationID){
  648. let projects = await projectsModel.find({
  649. '$or': [{
  650. 'userID': userID,
  651. 'compilation': compilationID,
  652. 'deleteInfo': null
  653. }, {'userID': userID, 'compilation': compilationID, 'deleteInfo.deleted': {'$in': [null, false]}}]
  654. }, 'ID ParentID NextSiblingID name projType compareID', {lean: true});
  655. return projects;
  656. }