pm_controller.js 28 KB

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