pm_controller.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. if (userData) {
  244. let usedCompilation = _.find(userData.used_list, function (o) {
  245. return o.compilationId === compilationData._id.toString();
  246. });
  247. //第一次使用该费用定额
  248. if (!usedCompilation) {
  249. await userModel.update({_id: mongoose.Types.ObjectId(request.session.sessionUser.id)}, {$push: {used_list: {compilationId: compilationData._id}}});
  250. //拷贝补充定额模板数据
  251. await sectionTreeDao.copyDataFromTemplate(request.session.sessionUser.id, compilationData._id);
  252. //拷贝例题数据
  253. if (sessionCompilation.example && sessionCompilation.example.length > 0) {
  254. await pm_facade.copyExample(request.session.sessionUser.id, sessionCompilation._id, sessionCompilation.example);
  255. }
  256. }
  257. }
  258. // 清单计价
  259. let billValuation = sessionCompilation.bill_valuation !== undefined ?
  260. sessionCompilation.bill_valuation : [];
  261. // 获取标准库数据
  262. let engineeringLibModel = new EngineeringLibModel();
  263. billValuation = await engineeringLibModel.getLib(billValuation);
  264. // 定额计价
  265. let rationValuation = sessionCompilation.ration_valuation !== undefined ?
  266. sessionCompilation.ration_valuation : [];
  267. rationValuation = await engineeringLibModel.getLib(rationValuation);
  268. let absoluteUrl = compilationData.overWriteUrl ? request.app.locals.rootDir + compilationData.overWriteUrl : request.app.locals.rootDir;
  269. let overWriteUrl = fs.existsSync(absoluteUrl) && fs.statSync(absoluteUrl).isFile()? compilationData.overWriteUrl : null;
  270. let renderData = {
  271. userAccount: request.session.userAccount,
  272. userID: request.session.sessionUser.id,
  273. compilationData: JSON.stringify(sessionCompilation),
  274. overWriteUrl: overWriteUrl,
  275. billValuation: JSON.stringify(billValuation),
  276. rationValuation: JSON.stringify(rationValuation),
  277. engineeringList: JSON.stringify(engineering.List),
  278. compilationName: sessionCompilation.name,
  279. versionName: `纵横建筑云计价(${request.session.compilationVersion})`,
  280. LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
  281. };
  282. response.render('building_saas/pm/html/project-management.html', renderData);
  283. },
  284. // 获取单价文件列表
  285. getUnitFileList: async function(request, response) {
  286. let data = request.body.data;
  287. try {
  288. data = JSON.parse(data);
  289. let projectId = data.parentID !== undefined ? data.parentID : 0;
  290. if (isNaN(projectId) && projectId <= 0) {
  291. throw {msg: 'id数据有误!', err: 1};
  292. }
  293. /*// 获取对应建设项目下所有的单位工程id
  294. let idList = await ProjectsData.getTenderByProjectId(projectId);
  295. if (idList.length <= 0) {
  296. throw {msg: '不存在对应单位工程', err: 0};
  297. }*/
  298. // 获取对应的单价文件
  299. let unitPriceFileModel = new UnitPriceFileModel();
  300. let unitPriceFileData = await unitPriceFileModel.getDataByRootProject(projectId);
  301. if (unitPriceFileData === null) {
  302. throw {msg: '不存在对应单价文件', err: 0};
  303. }
  304. // 整理数据
  305. let unitPriceFileList = [];
  306. for (let unitPriceFile of unitPriceFileData) {
  307. let tmp = {
  308. name: unitPriceFile.name,
  309. id: unitPriceFile.id
  310. };
  311. unitPriceFileList.push(tmp);
  312. }
  313. callback(request, response, 0, '', unitPriceFileList);
  314. } catch (error) {
  315. console.log(error);
  316. let responseData = error.err === 1 ? null : [];
  317. callback(request, response, error.err, error.msg, responseData);
  318. }
  319. },
  320. getFeeRateFileList:async function(request, response) {
  321. let data = request.body.data;
  322. try {
  323. data = JSON.parse(data);
  324. let projectId = data.parentID !== undefined ? data.parentID : 0;
  325. if (isNaN(projectId) && projectId <= 0) {
  326. throw {msg: 'id数据有误!', err: 1};
  327. }
  328. // 获取对应建设项目下所有的单位工程id
  329. let feeRateFileList = await fee_rate_facade.getFeeRatesByProject(projectId);
  330. callback(request, response, 0, '',feeRateFileList );
  331. } catch (error) {
  332. console.log(error);
  333. let responseData = error.err === 1 ? null : [];
  334. callback(request, response, error.err, error.msg, responseData);
  335. }
  336. },
  337. getGCDatas: async function(request, response) {
  338. let userID = request.session.sessionUser.id;
  339. let compilatoinId = request.session.sessionCompilation._id;
  340. let rst = [];
  341. let _projs = Object.create(null), _engs = Object.create(null), prefix = 'ID_';
  342. try{
  343. let gc_unitPriceFiles = await ProjectsData.getGCFiles(fileType.unitPriceFile, userID);
  344. let gc_feeRateFiles = await ProjectsData.getGCFiles(fileType.feeRateFile, userID);
  345. let gc_tenderFiles = await ProjectsData.getGCFiles(projType.tender, userID);
  346. for(let i = 0, len = gc_unitPriceFiles.length; i < len; i++){
  347. let gc_uf = gc_unitPriceFiles[i];
  348. let theProj = _projs[prefix + gc_uf.root_project_id] || null;
  349. if(!theProj){
  350. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_uf.root_project_id]);
  351. if(tempProj.length > 0 && tempProj[0].projType !== projType.folder){
  352. theProj = _projs[prefix + gc_uf.root_project_id] = tempProj[0]._doc;
  353. buildProj(theProj);
  354. }
  355. }
  356. if(theProj){
  357. theProj.unitPriceFiles.push(gc_uf);
  358. }
  359. }
  360. for(let i = 0, len = gc_feeRateFiles.length; i < len; i++){
  361. let gc_ff = gc_feeRateFiles[i];
  362. let theProj = _projs[prefix + gc_ff.rootProjectID] || null;
  363. if(!theProj){
  364. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_ff.rootProjectID]);
  365. if(tempProj.length > 0 && tempProj[0].projType !== projType.folder){
  366. theProj = _projs[prefix + gc_ff.rootProjectID] = tempProj[0]._doc;
  367. buildProj(theProj);
  368. }
  369. }
  370. if(theProj) {
  371. theProj.feeRateFiles.push(gc_ff);
  372. }
  373. }
  374. if(gc_tenderFiles.length > 0){
  375. for(let i = 0, len = gc_tenderFiles.length; i < len; i++){
  376. let gc_t = gc_tenderFiles[i];
  377. let theEng = _engs[prefix + gc_t.ParentID] || null;
  378. if(!theEng){
  379. let tempEngs = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_t.ParentID]);
  380. if(tempEngs.length > 0 && tempEngs[0].projType === projType.engineering){
  381. theEng = _engs[prefix + gc_t.ParentID] = tempEngs[0]._doc;
  382. theEng.children = [];
  383. }
  384. }
  385. if(theEng) {
  386. theEng.children.push(gc_t);
  387. let theProj = _projs[prefix + theEng.ParentID] || null;
  388. if(!theProj){
  389. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [theEng.ParentID]);
  390. if(tempProj.length > 0 && tempProj[0].projType === projType.project){
  391. theProj = _projs[prefix + theEng.ParentID] = tempProj[0]._doc;
  392. buildProj(theProj);
  393. }
  394. }
  395. if(theProj) {
  396. let isExist = false;
  397. for(let j = 0, jLen = theProj.children.length; j < jLen; j++){
  398. if(theProj.children[j].ID === theEng.ID){
  399. isExist = true;
  400. break;
  401. }
  402. }
  403. if(!isExist){
  404. theProj.children.push(theEng);
  405. }
  406. }
  407. }
  408. }
  409. }
  410. for(let i in _projs){
  411. rst.push(_projs[i]);
  412. }
  413. function buildProj(proj){
  414. proj.children = [];
  415. proj.unitPriceFiles = [];
  416. proj.feeRateFiles = [];
  417. }
  418. callback(request, response, 0, 'success', rst);
  419. }
  420. catch (error){
  421. callback(request, response, true, error, null);
  422. }
  423. },
  424. recGC: function(request, response){
  425. let userID = request.session.sessionUser.id;
  426. let data = JSON.parse(request.body.data);
  427. let nodes = data.nodes;
  428. ProjectsData.recGC(userID, nodes, function (err, msg, data) {
  429. callback(request, response, err, msg, data);
  430. });
  431. },
  432. delGC: async function(request, response){
  433. let data = JSON.parse(request.body.data);
  434. let delDatas = data.delDatas;
  435. let bulkProjs = [], bulkUFs = [], bulkFFs = [];
  436. try{
  437. for(let data of delDatas){
  438. if(data.updateType === 'Project'){
  439. bulkProjs.push({updateOne: {filter: {ID: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  440. }
  441. else if(data.updateType === fileType.unitPriceFile){
  442. bulkUFs.push({updateOne: {filter: {id: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  443. }
  444. else{
  445. bulkFFs.push({updateOne: {filter: {ID: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  446. }
  447. }
  448. if(bulkProjs.length > 0){
  449. await projectModel.bulkWrite(bulkProjs);
  450. }
  451. if(bulkUFs.length > 0){
  452. await unitPriceFileModel.bulkWrite(bulkUFs);
  453. }
  454. if(bulkFFs.length > 0){
  455. await feeRateFileModel.bulkWrite(bulkFFs);
  456. }
  457. callback(request, response, 0, 'success', null);
  458. } catch(err){
  459. callback(request, response, 1, err, null);
  460. }
  461. },
  462. moveProject:async function(req,res){
  463. let result={
  464. error:0
  465. };
  466. try {
  467. let data = req.body.data;
  468. let rdata= await pm_facade.moveProject(data);
  469. result.data= rdata;
  470. }catch (err){
  471. console.log(err);
  472. result.error=1;
  473. result.message = err.message;
  474. }
  475. res.json(result);
  476. },
  477. copyProjects:async function (req, res) {
  478. let result={
  479. error:0
  480. };
  481. try {
  482. let data = JSON.parse(req.body.data);
  483. result.data = await pm_facade.copyProject(req.session.sessionUser.id, req.session.sessionCompilation._id,data);
  484. }catch (err){
  485. console.log(err);
  486. result.error=1;
  487. result.message = err.message;
  488. }
  489. res.json(result);
  490. },
  491. projectShareInfo: async function(req, res){
  492. try{
  493. let data = JSON.parse(req.body.data);
  494. let shareInfo = await projectModel.findOne({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, '-_id shareInfo');
  495. callback(req, res, 0, 'success', shareInfo);
  496. }
  497. catch (err){
  498. callback(req, res, 1, err, null);
  499. }
  500. },
  501. share: async function(req, res){
  502. try{
  503. let data = JSON.parse(req.body.data);
  504. let shareDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'),
  505. shareUserIDs = [];
  506. for (let data of data.shareData) {
  507. shareUserIDs.push(data.userID);
  508. data.shareDate = shareDate;
  509. }
  510. //添加分享
  511. if(data.type === 'create'){
  512. //新增
  513. for (let sData of data.shareData) {
  514. await projectModel.update({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, {$addToSet: {shareInfo: sData}});
  515. }
  516. }
  517. //取消分享
  518. else {
  519. await projectModel.update({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, {$pull: {shareInfo: {userID: {$in: shareUserIDs}}}});
  520. }
  521. callback(req, res, 0, 'success', null);
  522. }
  523. catch (err){
  524. callback(req, res, 1, err, null);
  525. }
  526. },
  527. receiveProjects: async function(req, res) {
  528. try {
  529. let rst = {grouped: [], ungrouped: [], summaryInfo: null};
  530. let userID = req.session.sessionUser.id;
  531. let receiveProjects = await projectModel.find({
  532. $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}], compilation: req.session.sessionCompilation._id, 'shareInfo.userID': userID}, '-_id');
  533. //设置原项目用户信息
  534. if(receiveProjects.length > 0){
  535. let orgUserIDs = [];
  536. for(let proj of receiveProjects){
  537. orgUserIDs.push(proj.userID);
  538. if (proj.projType === projType.tender) {
  539. //设置工程专业
  540. proj._doc.feeStandardName = proj.property.feeStandardName || '';
  541. }
  542. delete proj._doc.property;
  543. }
  544. orgUserIDs = Array.from(new Set(orgUserIDs));
  545. let userObjIDs = [];
  546. for(let uID of orgUserIDs){
  547. userObjIDs.push(mongoose.Types.ObjectId(uID));
  548. }
  549. let orgUsersInfo = await userModel.find({_id: {$in : userObjIDs}});
  550. //建设项目
  551. let consProjIDs = [],
  552. ungroupedTenders = [];
  553. for(let proj of receiveProjects){
  554. if (proj.projType === projType.project) {
  555. consProjIDs.push(proj.ID);
  556. }
  557. //获取分享项目子项
  558. if (proj.projType !== projType.tender) {
  559. proj._doc.children = await pm_facade.getPosterityProjects([proj.ID]);
  560. for (let projC of proj._doc.children) {
  561. if (projC.projType === projType.project) {
  562. consProjIDs.push(projC.ID);
  563. } else if (projC.projType === projType.tender) {
  564. //设置工程专业
  565. projC._doc.feeStandardName = projC.property.feeStandardName || '';
  566. if (proj.projType === projType.engineering) {
  567. ungroupedTenders.push(projC._doc);
  568. }
  569. }
  570. delete projC._doc.property;
  571. }
  572. } else {//未分类的单位工程不进行汇总,只取价格信息
  573. ungroupedTenders.push(proj._doc);
  574. }
  575. //设置分组,单位工程及单项工程分到未分组那
  576. if (proj.projType === projType.tender || proj.projType === projType.engineering) {
  577. rst.ungrouped.push(proj);
  578. } else {
  579. rst.grouped.push(proj);
  580. }
  581. //设置项目类型为来自别人分享
  582. proj._doc.shareType = 'receive';
  583. for(let userData of orgUsersInfo){
  584. if(proj.userID == userData._id.toString()){
  585. let userInfo = {name: userData.real_name, mobile: userData.mobile, company: userData.company, email: userData.email};
  586. proj._doc.userInfo = userInfo;
  587. }
  588. }
  589. }
  590. consProjIDs = Array.from(new Set(consProjIDs));
  591. let summaryInfo = await pm_facade.getSummaryInfo(consProjIDs);
  592. let tendersFeeInfo = await pm_facade.getTendersFeeInfo(ungroupedTenders);
  593. rst.summaryInfo = {grouped: summaryInfo, ungrouped: tendersFeeInfo};
  594. }
  595. callback(req, res, 0, 'success', rst);
  596. }
  597. catch (err){
  598. console.log(err);
  599. callback(req, res, 1, err, null);
  600. }
  601. },
  602. getProjectsByQuery: async function (req, res) {
  603. try{
  604. let data = JSON.parse(req.body.data);
  605. let compilation = req.session.sessionCompilation._id;
  606. let query = data.query;
  607. query.compilation = compilation;
  608. let options = data.options;
  609. let projects = await projectModel.find(query, options);
  610. callback(req, res, 0, 'success', projects);
  611. }
  612. catch (err){
  613. callback(req, res, 1, err, null);
  614. }
  615. },
  616. getSummaryInfo: async function(req, res){
  617. try{
  618. let data = JSON.parse(req.body.data);
  619. let summaryInfo = await pm_facade.getSummaryInfo(data.projectIDs);
  620. callback(req, res, 0, 'success', summaryInfo);
  621. }
  622. catch (err){
  623. callback(req, res, 1, err, null);
  624. }
  625. },
  626. };