pm_controller.js 29 KB

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