pm_controller.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /**
  2. * Created by Mai on 2017/1/18.
  3. */
  4. import UnitPriceFileModel from "../../glj/models/unit_price_file_model";
  5. import moment from 'moment';
  6. import CompilationModel from "../../users/models/compilation_model";
  7. let mongoose = require('mongoose');
  8. let ProjectsData = require('../models/project_model').project;
  9. let labourCoe = require('../../main/facade/labour_coe_facade');
  10. let projType = require('../models/project_model').projType;
  11. let fileType = require('../models/project_model').fileType;
  12. const engineering = require("../../common/const/engineering");
  13. let EngineeringLibModel = require("../../users/models/engineering_lib_model");
  14. let fee_rate_facade = require("../../fee_rates/facade/fee_rates_facade");
  15. let billsModel = require('../../main/models/bills').model;
  16. let rationsModel = require('../../main/models/ration').model;
  17. let projectModel = mongoose.model('projects');
  18. let unitPriceFileModel = mongoose.model('unit_price_file');
  19. let feeRateFileModel = mongoose.model('fee_rate_file');
  20. let asyncTool = require('async');
  21. let pm_facade = require('../facade/pm_facade');
  22. const userModel = mongoose.model('user');
  23. let config = require("../../../config/config.js");
  24. const optionModel = mongoose.model('options');
  25. const stdBillsGuidanceLibModel = mongoose.model('std_billsGuidance_lib');
  26. const fs = require('fs');
  27. const _ = require('lodash');
  28. import SectionTreeDao from '../../complementary_ration_lib/models/sectionTreeModel';
  29. let sectionTreeDao = new SectionTreeDao();
  30. //统一回调函数
  31. let callback = function(req, res, err, message, data){
  32. res.json({error: err, message: message, data: data});
  33. };
  34. module.exports = {
  35. checkRight: function (req, res) {
  36. if(typeof req.body.data === 'object'){
  37. req.body.data = JSON.stringify(req.body.data);
  38. }
  39. let data = JSON.parse(req.body.data);
  40. if (data.user_id) {
  41. return data.user_id === req.session.sessionUser.id;
  42. } else {
  43. return false;
  44. }
  45. },
  46. checkProjectRight: function (userId, projectId, callback) {
  47. ProjectsData.getProject(projectId).then(async function (result) {
  48. /**
  49. * result._doc.userID(Number): MongoDB
  50. * userId(String): Session.userID
  51. */
  52. let isShare = false;
  53. //判断是否是打开分享的项目
  54. if(userId !== result.userID){
  55. isShare = await pm_facade.isShare(userId, result);
  56. }
  57. if ((userId === result.userID || isShare) && result._doc.projType === projType.tender) {
  58. callback(true);
  59. } else {
  60. callback(false);
  61. }
  62. }).catch(function (err) {
  63. callback(false);
  64. });
  65. },
  66. getProjects: async function(req, res){
  67. console.log(`req.session.sessionCompilation`);
  68. console.log(req.session.sessionCompilation);
  69. await ProjectsData.getUserProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, function(err, message, projects){
  70. if (projects) {
  71. callback(req, res, err, message, projects);
  72. } else {
  73. callback(req, res, err, message, null);
  74. }
  75. });
  76. },
  77. updateProjects: async function (req, res) {
  78. let data = JSON.parse(req.body.data);
  79. await ProjectsData.updateUserProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, req.session.sessionCompilation.name, data.updateData, function (err, message, data) {
  80. if (err === 0) {
  81. callback(req, res, err, message, data);
  82. } else {
  83. callback(req, res, err, message, null);
  84. }
  85. });
  86. },
  87. // CSL, 2017-12-14 该方法用于项目属性:提交保存混合型数据,这些数据来自不同的表,包括projects.property、ration、bills、labour_coes.
  88. updateMixDatas: async function(req, res){
  89. let datas = JSON.parse(req.body.data).mixDataArr;
  90. let functions = [];
  91. function updateFunc(model, cod, doc) {
  92. return function (cb) {
  93. model.update(cod, doc, cb);
  94. }
  95. };
  96. function updateLC(){
  97. return function (cb) {
  98. datas.labourCoes.updateData.projectID = datas.projectID;
  99. labourCoe.save(datas.labourCoes.updateData, cb);
  100. }
  101. };
  102. // 项目属性
  103. if (Object.keys(datas.properties).length > 0){
  104. //基本信息特殊处理,更新建设项目
  105. if(datas.properties['property.basicInformation']){
  106. let constructionProject = await pm_facade.getConstructionProject(datas.projectID);
  107. if(constructionProject){
  108. functions.push(updateFunc(projectModel, {ID: constructionProject.ID}, {'property.basicInformation': datas.properties['property.basicInformation']}));
  109. }
  110. delete datas.properties['property.basicInformation'];
  111. }
  112. functions.push(updateFunc(projectModel, {ID: datas.projectID}, datas.properties));
  113. };
  114. //选项
  115. if(datas.options && datas.options.updateData){
  116. functions.push(updateFunc(optionModel, {user_id: req.session.sessionUser.id, compilation_id: req.session.sessionCompilation._id}, {'options.GENERALOPTS': datas.options.updateData}));
  117. }
  118. // 人工系数
  119. if (datas.labourCoes&&datas.labourCoes.updateData){
  120. functions.push(updateLC());
  121. };
  122. // 清单:每文档doc只存储一条清单,每条清单都必须定位一次文档,无法合并处理
  123. if (datas.bills.length > 0){
  124. for (let bill of datas.bills){
  125. functions.push(updateFunc(billsModel, {projectID: datas.projectID, ID: bill.ID, deleteInfo: null}, bill));
  126. };
  127. };
  128. // 定额:每文档doc只存储一条定额,每条定额都必须定位一次文档,无法合并处理
  129. if (datas.rations.length > 0){
  130. for (let ration of datas.rations){
  131. functions.push(updateFunc(rationsModel, {projectID: datas.projectID, ID: ration.ID, deleteInfo: null}, ration));
  132. };
  133. };
  134. asyncTool.parallel(functions, function(err, result){
  135. {
  136. if (!err) {
  137. res.json({error: 0, message: err, data: result});
  138. } else {
  139. res.json({error: 1, message: err, data: null});
  140. }
  141. }
  142. });
  143. },
  144. updateFiles: async function(req, res){
  145. let data = JSON.parse(req.body.data);
  146. let updateDatas = data.updateDatas;
  147. await ProjectsData.udpateUserFiles(req.session.sessionUser.id, updateDatas, function (err, message, data) {
  148. callback(req, res, err, message, data);
  149. });
  150. },
  151. defaultSettings: async function(req, res){
  152. try{
  153. let data = JSON.parse(req.body.data);
  154. let projectID = data.projectID;
  155. let defaultSettingSc = await ProjectsData.defaultSettings(req.session.sessionUser.id, req.session.sessionCompilation._id, projectID);
  156. if(!defaultSettingSc){
  157. throw '恢复失败';
  158. }
  159. res.json({error: 0, message: '恢复成功', data: null});
  160. }
  161. catch(error){
  162. console.log(error);
  163. res.json({error: 1, message: error, data: null});
  164. }
  165. },
  166. /* copyProjects: function (req, res) {
  167. let data = JSON.parse(req.body.data);
  168. ProjectsData.copyUserProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, data.updateData, function (err, message, data) {
  169. if (err === 0) {
  170. callback(req, res, err, message, data);
  171. } else {
  172. callback(req, res, err, message, null);
  173. }
  174. });
  175. },*/
  176. rename: function (req, res) {
  177. let data = JSON.parse(req.body.data);
  178. ProjectsData.rename(req.session.sessionUser.id, req.session.sessionCompilation._id, data, function (err, message) {
  179. callback(req, res, err, message, null);
  180. });
  181. },
  182. getProject: function(req, res){
  183. let data = JSON.parse(req.body.data);
  184. let projectID = data.proj_id;
  185. ProjectsData.getUserProject(req.session.sessionUser.id, data.proj_id, async function(err, message, data){
  186. if (err === 0) {
  187. let engineeringLibModel = new EngineeringLibModel();
  188. let engineeringInfo = data !== null && data.property.engineering_id !== undefined ?
  189. await engineeringLibModel.getEngineering(data.property.engineering_id) : null;
  190. let strData = JSON.stringify(data);
  191. let projInfo = JSON.parse(strData);
  192. if (engineeringInfo !== null) {
  193. if(engineeringInfo.billsGuidance_lib){
  194. for(let billsGuidanceLib of engineeringInfo.billsGuidance_lib){
  195. let stdBillsGuidanceLib = await stdBillsGuidanceLibModel.findOne({ID: billsGuidanceLib.id});
  196. if(stdBillsGuidanceLib){
  197. billsGuidanceLib.type = stdBillsGuidanceLib.type ? stdBillsGuidanceLib.type : 1;
  198. }
  199. }
  200. }
  201. projInfo.engineeringInfo = engineeringInfo;
  202. }
  203. //读取建设项目的基本信息
  204. let basicInfo = await ProjectsData.getBasicInfo(projectID);
  205. if(basicInfo !== null){
  206. projInfo.property.basicInformation = basicInfo;
  207. }
  208. //获取单位工程完整目录结构
  209. let fullPath = await pm_facade.getFullPath(projectID);
  210. projInfo.fullPath = fullPath;
  211. callback(req, res, err, message, projInfo);
  212. } else {
  213. callback(req, res, err, message, null);
  214. }
  215. });
  216. },
  217. beforeOpenProject: function (req, res) {
  218. let data = JSON.parse(req.body.data);
  219. ProjectsData.beforeOpenProject(req.session.sessionUser.id, data.proj_id, data.updateData, function (err, message, data) {
  220. callback(req, res, err, message, data);
  221. });
  222. },
  223. getNewProjectID: function (req, res) {
  224. let data = JSON.parse(req.body.data);
  225. ProjectsData.getNewProjectID(data.count, function (err, message, data) {
  226. callback(req, res, err, message, data);
  227. });
  228. },
  229. // 项目管理首页
  230. index: async function(request, response) {
  231. // 获取编办信息
  232. let sessionCompilation = request.session.sessionCompilation;
  233. if (sessionCompilation === undefined ||sessionCompilation ===null) {
  234. return response.redirect('/logout');
  235. }
  236. let compilationModel = new CompilationModel();
  237. //更新编办信息
  238. let compilationData = await compilationModel.getCompilationById(sessionCompilation._id);
  239. request.session.sessionCompilation = compilationData;
  240. sessionCompilation = request.session.sessionCompilation;
  241. //更新用户的使用过的费用定额列表
  242. let userData = await userModel.findOne({_id: mongoose.Types.ObjectId(request.session.sessionUser.id)}, '-_id used_list');
  243. //是否第一次进入该费用定额
  244. let isFirst = false;
  245. if (userData) {
  246. isFirst = !_.find(userData.used_list, function (o) {
  247. return o.compilationId === compilationData._id.toString();
  248. });;
  249. }
  250. // 清单计价
  251. let billValuation = sessionCompilation.bill_valuation !== undefined ?
  252. sessionCompilation.bill_valuation : [];
  253. // 获取标准库数据
  254. let engineeringLibModel = new EngineeringLibModel();
  255. billValuation = await engineeringLibModel.getLib(billValuation);
  256. // 定额计价
  257. let rationValuation = sessionCompilation.ration_valuation !== undefined ?
  258. sessionCompilation.ration_valuation : [];
  259. rationValuation = await engineeringLibModel.getLib(rationValuation);
  260. let absoluteUrl = compilationData.overWriteUrl ? request.app.locals.rootDir + compilationData.overWriteUrl : request.app.locals.rootDir;
  261. let overWriteUrl = fs.existsSync(absoluteUrl) && fs.statSync(absoluteUrl).isFile()? compilationData.overWriteUrl : null;
  262. let renderData = {
  263. isFirst: isFirst,
  264. userAccount: request.session.userAccount,
  265. userID: request.session.sessionUser.id,
  266. compilationData: JSON.stringify(sessionCompilation),
  267. overWriteUrl: overWriteUrl,
  268. billValuation: JSON.stringify(billValuation),
  269. rationValuation: JSON.stringify(rationValuation),
  270. engineeringList: JSON.stringify(engineering.List),
  271. compilationName: sessionCompilation.name,
  272. versionName: `纵横建筑云计价(${request.session.compilationVersion})`,
  273. LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
  274. };
  275. response.render('building_saas/pm/html/project-management.html', renderData);
  276. },
  277. //第一次进入该费用定额时准备的初始数据
  278. prepareInitialData: async function(request, response) {
  279. try {
  280. let sessionCompilation = request.session.sessionCompilation;
  281. await pm_facade.prepareInitialData(request.session.sessionUser.id, sessionCompilation._id, sessionCompilation.example);
  282. callback(request, response, 0, 'success', null);
  283. } catch(err) {
  284. callback(request, response, 1, err, null);
  285. }
  286. },
  287. // 获取单价文件列表
  288. getUnitFileList: async function(request, response) {
  289. let data = request.body.data;
  290. try {
  291. data = JSON.parse(data);
  292. let projectId = data.parentID !== undefined ? data.parentID : 0;
  293. if (isNaN(projectId) && projectId <= 0) {
  294. throw {msg: 'id数据有误!', err: 1};
  295. }
  296. /*// 获取对应建设项目下所有的单位工程id
  297. let idList = await ProjectsData.getTenderByProjectId(projectId);
  298. if (idList.length <= 0) {
  299. throw {msg: '不存在对应单位工程', err: 0};
  300. }*/
  301. // 获取对应的单价文件
  302. let unitPriceFileModel = new UnitPriceFileModel();
  303. let unitPriceFileData = await unitPriceFileModel.getDataByRootProject(projectId);
  304. if (unitPriceFileData === null) {
  305. throw {msg: '不存在对应单价文件', err: 0};
  306. }
  307. // 整理数据
  308. let unitPriceFileList = [];
  309. for (let unitPriceFile of unitPriceFileData) {
  310. let tmp = {
  311. name: unitPriceFile.name,
  312. id: unitPriceFile.id
  313. };
  314. unitPriceFileList.push(tmp);
  315. }
  316. callback(request, response, 0, '', unitPriceFileList);
  317. } catch (error) {
  318. console.log(error);
  319. let responseData = error.err === 1 ? null : [];
  320. callback(request, response, error.err, error.msg, responseData);
  321. }
  322. },
  323. getFeeRateFileList:async function(request, response) {
  324. let data = request.body.data;
  325. try {
  326. data = JSON.parse(data);
  327. let projectId = data.parentID !== undefined ? data.parentID : 0;
  328. if (isNaN(projectId) && projectId <= 0) {
  329. throw {msg: 'id数据有误!', err: 1};
  330. }
  331. // 获取对应建设项目下所有的单位工程id
  332. let feeRateFileList = await fee_rate_facade.getFeeRatesByProject(projectId);
  333. callback(request, response, 0, '',feeRateFileList );
  334. } catch (error) {
  335. console.log(error);
  336. let responseData = error.err === 1 ? null : [];
  337. callback(request, response, error.err, error.msg, responseData);
  338. }
  339. },
  340. getGCDatas: async function(request, response) {
  341. let userID = request.session.sessionUser.id;
  342. let compilatoinId = request.session.sessionCompilation._id;
  343. let rst = [];
  344. let _projs = Object.create(null), _engs = Object.create(null), prefix = 'ID_';
  345. try{
  346. let gc_unitPriceFiles = await ProjectsData.getGCFiles(fileType.unitPriceFile, userID);
  347. let gc_feeRateFiles = await ProjectsData.getGCFiles(fileType.feeRateFile, userID);
  348. let gc_tenderFiles = await ProjectsData.getGCFiles(projType.tender, userID);
  349. for(let i = 0, len = gc_unitPriceFiles.length; i < len; i++){
  350. let gc_uf = gc_unitPriceFiles[i];
  351. let theProj = _projs[prefix + gc_uf.root_project_id] || null;
  352. if(!theProj){
  353. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_uf.root_project_id]);
  354. if(tempProj.length > 0 && tempProj[0].projType !== projType.folder){
  355. theProj = _projs[prefix + gc_uf.root_project_id] = tempProj[0]._doc;
  356. buildProj(theProj);
  357. }
  358. }
  359. if(theProj){
  360. theProj.unitPriceFiles.push(gc_uf);
  361. }
  362. }
  363. for(let i = 0, len = gc_feeRateFiles.length; i < len; i++){
  364. let gc_ff = gc_feeRateFiles[i];
  365. let theProj = _projs[prefix + gc_ff.rootProjectID] || null;
  366. if(!theProj){
  367. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_ff.rootProjectID]);
  368. if(tempProj.length > 0 && tempProj[0].projType !== projType.folder){
  369. theProj = _projs[prefix + gc_ff.rootProjectID] = tempProj[0]._doc;
  370. buildProj(theProj);
  371. }
  372. }
  373. if(theProj) {
  374. theProj.feeRateFiles.push(gc_ff);
  375. }
  376. }
  377. if(gc_tenderFiles.length > 0){
  378. for(let i = 0, len = gc_tenderFiles.length; i < len; i++){
  379. let gc_t = gc_tenderFiles[i];
  380. let theEng = _engs[prefix + gc_t.ParentID] || null;
  381. if(!theEng){
  382. let tempEngs = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_t.ParentID]);
  383. if(tempEngs.length > 0 && tempEngs[0].projType === projType.engineering){
  384. theEng = _engs[prefix + gc_t.ParentID] = tempEngs[0]._doc;
  385. theEng.children = [];
  386. }
  387. }
  388. if(theEng) {
  389. theEng.children.push(gc_t);
  390. let theProj = _projs[prefix + theEng.ParentID] || null;
  391. if(!theProj){
  392. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [theEng.ParentID]);
  393. if(tempProj.length > 0 && tempProj[0].projType === projType.project){
  394. theProj = _projs[prefix + theEng.ParentID] = tempProj[0]._doc;
  395. buildProj(theProj);
  396. }
  397. }
  398. if(theProj) {
  399. let isExist = false;
  400. for(let j = 0, jLen = theProj.children.length; j < jLen; j++){
  401. if(theProj.children[j].ID === theEng.ID){
  402. isExist = true;
  403. break;
  404. }
  405. }
  406. if(!isExist){
  407. theProj.children.push(theEng);
  408. }
  409. }
  410. }
  411. }
  412. }
  413. for(let i in _projs){
  414. rst.push(_projs[i]);
  415. }
  416. function buildProj(proj){
  417. proj.children = [];
  418. proj.unitPriceFiles = [];
  419. proj.feeRateFiles = [];
  420. }
  421. callback(request, response, 0, 'success', rst);
  422. }
  423. catch (error){
  424. callback(request, response, true, error, null);
  425. }
  426. },
  427. recGC: function(request, response){
  428. let userID = request.session.sessionUser.id;
  429. let data = JSON.parse(request.body.data);
  430. let nodes = data.nodes;
  431. ProjectsData.recGC(userID, nodes, function (err, msg, data) {
  432. callback(request, response, err, msg, data);
  433. });
  434. },
  435. delGC: async function(request, response){
  436. let data = JSON.parse(request.body.data);
  437. let delDatas = data.delDatas;
  438. let bulkProjs = [], bulkUFs = [], bulkFFs = [];
  439. try{
  440. for(let data of delDatas){
  441. if(data.updateType === 'Project'){
  442. bulkProjs.push({updateOne: {filter: {ID: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  443. }
  444. else if(data.updateType === fileType.unitPriceFile){
  445. bulkUFs.push({updateOne: {filter: {id: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  446. }
  447. else{
  448. bulkFFs.push({updateOne: {filter: {ID: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  449. }
  450. }
  451. if(bulkProjs.length > 0){
  452. await projectModel.bulkWrite(bulkProjs);
  453. }
  454. if(bulkUFs.length > 0){
  455. await unitPriceFileModel.bulkWrite(bulkUFs);
  456. }
  457. if(bulkFFs.length > 0){
  458. await feeRateFileModel.bulkWrite(bulkFFs);
  459. }
  460. callback(request, response, 0, 'success', null);
  461. } catch(err){
  462. callback(request, response, 1, err, null);
  463. }
  464. },
  465. moveProject:async function(req,res){
  466. let result={
  467. error:0
  468. };
  469. try {
  470. let data = req.body.data;
  471. let rdata= await pm_facade.moveProject(data);
  472. result.data= rdata;
  473. }catch (err){
  474. console.log(err);
  475. result.error=1;
  476. result.message = err.message;
  477. }
  478. res.json(result);
  479. },
  480. copyProjects:async function (req, res) {
  481. let result={
  482. error:0
  483. };
  484. try {
  485. let data = JSON.parse(req.body.data);
  486. result.data = await pm_facade.copyProject(req.session.sessionUser.id, req.session.sessionCompilation._id,data);
  487. }catch (err){
  488. console.log(err);
  489. result.error=1;
  490. result.message = err.message;
  491. }
  492. res.json(result);
  493. },
  494. projectShareInfo: async function(req, res){
  495. try{
  496. let data = JSON.parse(req.body.data);
  497. let shareInfo = await projectModel.findOne({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, '-_id shareInfo');
  498. callback(req, res, 0, 'success', shareInfo);
  499. }
  500. catch (err){
  501. callback(req, res, 1, err, null);
  502. }
  503. },
  504. share: async function(req, res){
  505. try{
  506. let data = JSON.parse(req.body.data);
  507. let shareDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'),
  508. shareUserIDs = [];
  509. for (let data of data.shareData) {
  510. shareUserIDs.push(data.userID);
  511. data.shareDate = shareDate;
  512. }
  513. //添加分享
  514. if(data.type === 'create'){
  515. //新增
  516. for (let sData of data.shareData) {
  517. await projectModel.update({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, {$addToSet: {shareInfo: sData}});
  518. }
  519. }
  520. //取消分享
  521. else {
  522. await projectModel.update({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, {$pull: {shareInfo: {userID: {$in: shareUserIDs}}}});
  523. }
  524. callback(req, res, 0, 'success', null);
  525. }
  526. catch (err){
  527. callback(req, res, 1, err, null);
  528. }
  529. },
  530. receiveProjects: async function(req, res) {
  531. try {
  532. let rst = {grouped: [], ungrouped: [], summaryInfo: null};
  533. let userID = req.session.sessionUser.id;
  534. let receiveProjects = await projectModel.find({
  535. $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}], compilation: req.session.sessionCompilation._id, 'shareInfo.userID': userID}, '-_id');
  536. //设置原项目用户信息
  537. if(receiveProjects.length > 0){
  538. let orgUserIDs = [];
  539. for(let proj of receiveProjects){
  540. orgUserIDs.push(proj.userID);
  541. if (proj.projType === projType.tender) {
  542. //设置工程专业
  543. proj._doc.feeStandardName = proj.property.feeStandardName || '';
  544. }
  545. delete proj._doc.property;
  546. }
  547. orgUserIDs = Array.from(new Set(orgUserIDs));
  548. let userObjIDs = [];
  549. for(let uID of orgUserIDs){
  550. userObjIDs.push(mongoose.Types.ObjectId(uID));
  551. }
  552. let orgUsersInfo = await userModel.find({_id: {$in : userObjIDs}});
  553. //建设项目
  554. let consProjIDs = [],
  555. ungroupedTenders = [];
  556. for(let proj of receiveProjects){
  557. if (proj.projType === projType.project) {
  558. consProjIDs.push(proj.ID);
  559. }
  560. //获取分享项目子项
  561. if (proj.projType !== projType.tender) {
  562. proj._doc.children = await pm_facade.getPosterityProjects([proj.ID]);
  563. for (let projC of proj._doc.children) {
  564. if (projC.projType === projType.project) {
  565. consProjIDs.push(projC.ID);
  566. } else if (projC.projType === projType.tender) {
  567. //设置工程专业
  568. projC._doc.feeStandardName = projC.property.feeStandardName || '';
  569. if (proj.projType === projType.engineering) {
  570. ungroupedTenders.push(projC._doc);
  571. }
  572. }
  573. delete projC._doc.property;
  574. }
  575. } else {//未分类的单位工程不进行汇总,只取价格信息
  576. ungroupedTenders.push(proj._doc);
  577. }
  578. //设置分组,单位工程及单项工程分到未分组那
  579. if (proj.projType === projType.tender || proj.projType === projType.engineering) {
  580. rst.ungrouped.push(proj);
  581. } else {
  582. rst.grouped.push(proj);
  583. }
  584. //设置项目类型为来自别人分享
  585. proj._doc.shareType = 'receive';
  586. for(let userData of orgUsersInfo){
  587. if(proj.userID == userData._id.toString()){
  588. let userInfo = {name: userData.real_name, mobile: userData.mobile, company: userData.company, email: userData.email};
  589. proj._doc.userInfo = userInfo;
  590. }
  591. }
  592. }
  593. consProjIDs = Array.from(new Set(consProjIDs));
  594. let summaryInfo = await pm_facade.getSummaryInfo(consProjIDs);
  595. let tendersFeeInfo = await pm_facade.getTendersFeeInfo(ungroupedTenders);
  596. rst.summaryInfo = {grouped: summaryInfo, ungrouped: tendersFeeInfo};
  597. }
  598. callback(req, res, 0, 'success', rst);
  599. }
  600. catch (err){
  601. console.log(err);
  602. callback(req, res, 1, err, null);
  603. }
  604. },
  605. getProjectsByQuery: async function (req, res) {
  606. try{
  607. let data = JSON.parse(req.body.data);
  608. let compilation = req.session.sessionCompilation._id;
  609. let query = data.query;
  610. query.compilation = compilation;
  611. let options = data.options;
  612. let projects = await projectModel.find(query, options);
  613. callback(req, res, 0, 'success', projects);
  614. }
  615. catch (err){
  616. callback(req, res, 1, err, null);
  617. }
  618. },
  619. getSummaryInfo: async function(req, res){
  620. try{
  621. let data = JSON.parse(req.body.data);
  622. let summaryInfo = await pm_facade.getSummaryInfo(data.projectIDs);
  623. callback(req, res, 0, 'success', summaryInfo);
  624. }
  625. catch (err){
  626. callback(req, res, 1, err, null);
  627. }
  628. },
  629. };