pm_controller.js 28 KB

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