pm_controller.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. //建设项目-编制说明,更新建设项目
  111. if (datas.properties['property.compilationIllustrationProject']) {
  112. let constructionProject = await pm_facade.getConstructionProject(datas.projectID);
  113. if (constructionProject) {
  114. functions.push(updateFunc(projectModel, {ID: constructionProject.ID}, {'property.compilationIllustration': datas.properties['property.compilationIllustrationProject']}));
  115. }
  116. delete datas.properties['property.compilationIllustrationProject'];
  117. }
  118. functions.push(updateFunc(projectModel, {ID: datas.projectID}, datas.properties));
  119. };
  120. //选项
  121. if(datas.options && datas.options.updateData){
  122. functions.push(updateFunc(optionModel, {user_id: req.session.sessionUser.id, compilation_id: req.session.sessionCompilation._id}, {'options.GENERALOPTS': datas.options.updateData}));
  123. }
  124. // 人工系数
  125. if (datas.labourCoes&&datas.labourCoes.updateData){
  126. functions.push(updateLC());
  127. };
  128. // 清单:每文档doc只存储一条清单,每条清单都必须定位一次文档,无法合并处理
  129. if (datas.bills.length > 0){
  130. for (let bill of datas.bills){
  131. functions.push(updateFunc(billsModel, {projectID: datas.projectID, ID: bill.ID, deleteInfo: null}, bill));
  132. };
  133. };
  134. // 定额:每文档doc只存储一条定额,每条定额都必须定位一次文档,无法合并处理
  135. if (datas.rations.length > 0){
  136. for (let ration of datas.rations){
  137. functions.push(updateFunc(rationsModel, {projectID: datas.projectID, ID: ration.ID, deleteInfo: null}, ration));
  138. };
  139. };
  140. asyncTool.parallel(functions, function(err, result){
  141. {
  142. if (!err) {
  143. res.json({error: 0, message: err, data: result});
  144. } else {
  145. res.json({error: 1, message: err, data: null});
  146. }
  147. }
  148. });
  149. },
  150. updateFiles: async function(req, res){
  151. let data = JSON.parse(req.body.data);
  152. let updateDatas = data.updateDatas;
  153. await ProjectsData.udpateUserFiles(req.session.sessionUser.id, updateDatas, function (err, message, data) {
  154. callback(req, res, err, message, data);
  155. });
  156. },
  157. defaultSettings: async function(req, res){
  158. try{
  159. let data = JSON.parse(req.body.data);
  160. let projectID = data.projectID;
  161. let defaultSettingSc = await ProjectsData.defaultSettings(req.session.sessionUser.id, req.session.sessionCompilation._id, projectID);
  162. if(!defaultSettingSc){
  163. throw '恢复失败';
  164. }
  165. res.json({error: 0, message: '恢复成功', data: null});
  166. }
  167. catch(error){
  168. console.log(error);
  169. res.json({error: 1, message: error, data: null});
  170. }
  171. },
  172. /* copyProjects: function (req, res) {
  173. let data = JSON.parse(req.body.data);
  174. ProjectsData.copyUserProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, data.updateData, function (err, message, data) {
  175. if (err === 0) {
  176. callback(req, res, err, message, data);
  177. } else {
  178. callback(req, res, err, message, null);
  179. }
  180. });
  181. },*/
  182. rename: function (req, res) {
  183. let data = JSON.parse(req.body.data);
  184. ProjectsData.rename(req.session.sessionUser.id, req.session.sessionCompilation._id, data, function (err, message) {
  185. callback(req, res, err, message, null);
  186. });
  187. },
  188. getProject: function(req, res){
  189. let data = JSON.parse(req.body.data);
  190. let projectID = data.proj_id;
  191. ProjectsData.getUserProject(req.session.sessionUser.id, data.proj_id, async function(err, message, data){
  192. if (err === 0) {
  193. let engineeringLibModel = new EngineeringLibModel();
  194. let engineeringInfo = data !== null && data.property.engineering_id !== undefined ?
  195. await engineeringLibModel.getEngineering(data.property.engineering_id) : null;
  196. let strData = JSON.stringify(data);
  197. let projInfo = JSON.parse(strData);
  198. if (engineeringInfo !== null) {
  199. if(engineeringInfo.billsGuidance_lib){
  200. for(let billsGuidanceLib of engineeringInfo.billsGuidance_lib){
  201. let stdBillsGuidanceLib = await stdBillsGuidanceLibModel.findOne({ID: billsGuidanceLib.id});
  202. if(stdBillsGuidanceLib){
  203. billsGuidanceLib.type = stdBillsGuidanceLib.type ? stdBillsGuidanceLib.type : 1;
  204. }
  205. }
  206. }
  207. projInfo.engineeringInfo = engineeringInfo;
  208. }
  209. //读取建设项目的项目属性
  210. let constructionProperty = await ProjectsData.getConstructionProperty(projectID);
  211. console.log(projectID);
  212. console.log(constructionProperty);
  213. //基本信息
  214. projInfo.property.basicInformation = constructionProperty && constructionProperty.basicInformation ? constructionProperty.basicInformation : [];
  215. //编制说明
  216. projInfo.property.compilationIllustrationProject = constructionProperty && constructionProperty.compilationIllustration ? constructionProperty.compilationIllustration : '';
  217. //获取单位工程完整目录结构
  218. let fullPath = await pm_facade.getFullPath(projectID);
  219. projInfo.fullPath = fullPath;
  220. callback(req, res, err, message, projInfo);
  221. } else {
  222. callback(req, res, err, message, null);
  223. }
  224. });
  225. },
  226. beforeOpenProject: function (req, res) {
  227. let data = JSON.parse(req.body.data);
  228. ProjectsData.beforeOpenProject(req.session.sessionUser.id, data.proj_id, data.updateData, function (err, message, data) {
  229. callback(req, res, err, message, data);
  230. });
  231. },
  232. getNewProjectID: function (req, res) {
  233. let data = JSON.parse(req.body.data);
  234. ProjectsData.getNewProjectID(data.count, function (err, message, data) {
  235. callback(req, res, err, message, data);
  236. });
  237. },
  238. // 项目管理首页
  239. index: async function(request, response) {
  240. // 获取编办信息
  241. let sessionCompilation = request.session.sessionCompilation;
  242. if (sessionCompilation === undefined ||sessionCompilation ===null) {
  243. return response.redirect('/logout');
  244. }
  245. let compilationModel = new CompilationModel();
  246. //更新编办信息
  247. let compilationData = await compilationModel.getCompilationById(sessionCompilation._id);
  248. request.session.sessionCompilation = compilationData;
  249. sessionCompilation = request.session.sessionCompilation;
  250. //更新用户的使用过的费用定额列表
  251. let isFirst = await pm_facade.isFirst(request.session.sessionUser.id, compilationData._id.toString());
  252. // 清单计价
  253. let billValuation = sessionCompilation.bill_valuation !== undefined ?
  254. sessionCompilation.bill_valuation : [];
  255. // 获取标准库数据
  256. let engineeringLibModel = new EngineeringLibModel();
  257. billValuation = await engineeringLibModel.getLib(billValuation);
  258. // 定额计价
  259. let rationValuation = sessionCompilation.ration_valuation !== undefined ?
  260. sessionCompilation.ration_valuation : [];
  261. rationValuation = await engineeringLibModel.getLib(rationValuation);
  262. let absoluteUrl = compilationData.overWriteUrl ? request.app.locals.rootDir + compilationData.overWriteUrl : request.app.locals.rootDir;
  263. let overWriteUrl = fs.existsSync(absoluteUrl) && fs.statSync(absoluteUrl).isFile()? compilationData.overWriteUrl : null;
  264. let renderData = {
  265. isFirst: isFirst,
  266. userAccount: request.session.userAccount,
  267. userID: request.session.sessionUser.id,
  268. compilationData: JSON.stringify(sessionCompilation),
  269. overWriteUrl: overWriteUrl,
  270. billValuation: JSON.stringify(billValuation),
  271. rationValuation: JSON.stringify(rationValuation),
  272. engineeringList: JSON.stringify(engineering.List),
  273. compilationName: sessionCompilation.name,
  274. versionName: request.session.compilationVersion,
  275. LicenseKey:config.getLicenseKey(process.env.NODE_ENV)
  276. };
  277. response.render('building_saas/pm/html/project-management.html', renderData);
  278. },
  279. //第一次进入该费用定额时准备的初始数据
  280. prepareInitialData: async function(request, response) {
  281. try {
  282. let sessionCompilation = request.session.sessionCompilation;
  283. await pm_facade.prepareInitialData(request.session.sessionUser.id, sessionCompilation._id, sessionCompilation.example);
  284. callback(request, response, 0, 'success', null);
  285. } catch(err) {
  286. callback(request, response, 1, err, null);
  287. }
  288. },
  289. // 获取单价文件列表
  290. getUnitFileList: async function(request, response) {
  291. let data = request.body.data;
  292. try {
  293. data = JSON.parse(data);
  294. let projectId = data.parentID !== undefined ? data.parentID : 0;
  295. if (isNaN(projectId) && projectId <= 0) {
  296. throw {msg: 'id数据有误!', err: 1};
  297. }
  298. /*// 获取对应建设项目下所有的单位工程id
  299. let idList = await ProjectsData.getTenderByProjectId(projectId);
  300. if (idList.length <= 0) {
  301. throw {msg: '不存在对应单位工程', err: 0};
  302. }*/
  303. // 获取对应的单价文件
  304. let unitPriceFileModel = new UnitPriceFileModel();
  305. let unitPriceFileData = await unitPriceFileModel.getDataByRootProject(projectId);
  306. if (unitPriceFileData === null) {
  307. throw {msg: '不存在对应单价文件', err: 0};
  308. }
  309. // 整理数据
  310. let unitPriceFileList = [];
  311. for (let unitPriceFile of unitPriceFileData) {
  312. let tmp = {
  313. name: unitPriceFile.name,
  314. id: unitPriceFile.id
  315. };
  316. unitPriceFileList.push(tmp);
  317. }
  318. callback(request, response, 0, '', unitPriceFileList);
  319. } catch (error) {
  320. console.log(error);
  321. let responseData = error.err === 1 ? null : [];
  322. callback(request, response, error.err, error.msg, responseData);
  323. }
  324. },
  325. getFeeRateFileList:async function(request, response) {
  326. let data = request.body.data;
  327. try {
  328. data = JSON.parse(data);
  329. let projectId = data.parentID !== undefined ? data.parentID : 0;
  330. if (isNaN(projectId) && projectId <= 0) {
  331. throw {msg: 'id数据有误!', err: 1};
  332. }
  333. // 获取对应建设项目下所有的单位工程id
  334. let feeRateFileList = await fee_rate_facade.getFeeRatesByProject(projectId);
  335. callback(request, response, 0, '',feeRateFileList );
  336. } catch (error) {
  337. console.log(error);
  338. let responseData = error.err === 1 ? null : [];
  339. callback(request, response, error.err, error.msg, responseData);
  340. }
  341. },
  342. getGCDatas: async function(request, response) {
  343. let userID = request.session.sessionUser.id;
  344. let compilatoinId = request.session.sessionCompilation._id;
  345. let rst = [];
  346. let _projs = Object.create(null), _engs = Object.create(null), prefix = 'ID_';
  347. try{
  348. let gc_unitPriceFiles = await ProjectsData.getGCFiles(fileType.unitPriceFile, userID);
  349. let gc_feeRateFiles = await ProjectsData.getGCFiles(fileType.feeRateFile, userID);
  350. let gc_tenderFiles = await ProjectsData.getGCFiles(projType.tender, userID);
  351. for(let i = 0, len = gc_unitPriceFiles.length; i < len; i++){
  352. let gc_uf = gc_unitPriceFiles[i];
  353. let theProj = _projs[prefix + gc_uf.root_project_id] || null;
  354. if(!theProj){
  355. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_uf.root_project_id]);
  356. if(tempProj.length > 0 && tempProj[0].projType !== projType.folder){
  357. theProj = _projs[prefix + gc_uf.root_project_id] = tempProj[0]._doc;
  358. buildProj(theProj);
  359. }
  360. }
  361. if(theProj){
  362. theProj.unitPriceFiles.push(gc_uf);
  363. }
  364. }
  365. for(let i = 0, len = gc_feeRateFiles.length; i < len; i++){
  366. let gc_ff = gc_feeRateFiles[i];
  367. let theProj = _projs[prefix + gc_ff.rootProjectID] || null;
  368. if(!theProj){
  369. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_ff.rootProjectID]);
  370. if(tempProj.length > 0 && tempProj[0].projType !== projType.folder){
  371. theProj = _projs[prefix + gc_ff.rootProjectID] = tempProj[0]._doc;
  372. buildProj(theProj);
  373. }
  374. }
  375. if(theProj) {
  376. theProj.feeRateFiles.push(gc_ff);
  377. }
  378. }
  379. if(gc_tenderFiles.length > 0){
  380. for(let i = 0, len = gc_tenderFiles.length; i < len; i++){
  381. let gc_t = gc_tenderFiles[i];
  382. let theEng = _engs[prefix + gc_t.ParentID] || null;
  383. if(!theEng){
  384. let tempEngs = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_t.ParentID]);
  385. if(tempEngs.length > 0 && tempEngs[0].projType === projType.engineering){
  386. theEng = _engs[prefix + gc_t.ParentID] = tempEngs[0]._doc;
  387. theEng.children = [];
  388. }
  389. }
  390. if(theEng) {
  391. theEng.children.push(gc_t);
  392. let theProj = _projs[prefix + theEng.ParentID] || null;
  393. if(!theProj){
  394. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [theEng.ParentID]);
  395. if(tempProj.length > 0 && tempProj[0].projType === projType.project){
  396. theProj = _projs[prefix + theEng.ParentID] = tempProj[0]._doc;
  397. buildProj(theProj);
  398. }
  399. }
  400. if(theProj) {
  401. let isExist = false;
  402. for(let j = 0, jLen = theProj.children.length; j < jLen; j++){
  403. if(theProj.children[j].ID === theEng.ID){
  404. isExist = true;
  405. break;
  406. }
  407. }
  408. if(!isExist){
  409. theProj.children.push(theEng);
  410. }
  411. }
  412. }
  413. }
  414. }
  415. for(let i in _projs){
  416. rst.push(_projs[i]);
  417. }
  418. function buildProj(proj){
  419. proj.children = [];
  420. proj.unitPriceFiles = [];
  421. proj.feeRateFiles = [];
  422. }
  423. callback(request, response, 0, 'success', rst);
  424. }
  425. catch (error){
  426. callback(request, response, true, error, null);
  427. }
  428. },
  429. recGC: function(request, response){
  430. let userID = request.session.sessionUser.id;
  431. let data = JSON.parse(request.body.data);
  432. let nodes = data.nodes;
  433. ProjectsData.recGC(userID, nodes, function (err, msg, data) {
  434. callback(request, response, err, msg, data);
  435. });
  436. },
  437. delGC: async function(request, response){
  438. let data = JSON.parse(request.body.data);
  439. let delDatas = data.delDatas;
  440. let bulkProjs = [], bulkUFs = [], bulkFFs = [];
  441. try{
  442. for(let data of delDatas){
  443. if(data.updateType === 'Project'){
  444. bulkProjs.push({updateOne: {filter: {ID: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  445. }
  446. else if(data.updateType === fileType.unitPriceFile){
  447. bulkUFs.push({updateOne: {filter: {id: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  448. }
  449. else{
  450. bulkFFs.push({updateOne: {filter: {ID: data.ID}, update: {'deleteInfo.completeDeleted': true}}});
  451. }
  452. }
  453. if(bulkProjs.length > 0){
  454. await projectModel.bulkWrite(bulkProjs);
  455. }
  456. if(bulkUFs.length > 0){
  457. await unitPriceFileModel.bulkWrite(bulkUFs);
  458. }
  459. if(bulkFFs.length > 0){
  460. await feeRateFileModel.bulkWrite(bulkFFs);
  461. }
  462. callback(request, response, 0, 'success', null);
  463. } catch(err){
  464. callback(request, response, 1, err, null);
  465. }
  466. },
  467. moveProject:async function(req,res){
  468. let result={
  469. error:0
  470. };
  471. try {
  472. let data = req.body.data;
  473. let rdata= await pm_facade.moveProject(data);
  474. result.data= rdata;
  475. }catch (err){
  476. console.log(err);
  477. result.error=1;
  478. result.message = err.message;
  479. }
  480. res.json(result);
  481. },
  482. copyProjects:async function (req, res) {
  483. let result={
  484. error:0
  485. };
  486. try {
  487. let data = JSON.parse(req.body.data);
  488. result.data = await pm_facade.copyProject(req.session.sessionUser.id, req.session.sessionCompilation._id,data);
  489. }catch (err){
  490. console.log(err);
  491. result.error=1;
  492. result.message = err.message;
  493. }
  494. res.json(result);
  495. },
  496. projectShareInfo: async function(req, res){
  497. try{
  498. let data = JSON.parse(req.body.data);
  499. let shareInfo = await projectModel.findOne({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, '-_id shareInfo');
  500. callback(req, res, 0, 'success', shareInfo);
  501. }
  502. catch (err){
  503. callback(req, res, 1, err, null);
  504. }
  505. },
  506. share: async function(req, res){
  507. try{
  508. let data = JSON.parse(req.body.data);
  509. let shareDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'),
  510. shareUserIDs = [];
  511. for (let data of data.shareData) {
  512. shareUserIDs.push(data.userID);
  513. data.shareDate = shareDate;
  514. }
  515. //添加分享
  516. if(data.type === 'create'){
  517. //新增
  518. for (let sData of data.shareData) {
  519. await projectModel.update({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, {$addToSet: {shareInfo: sData}});
  520. }
  521. } else if (data.type === 'update') {
  522. await projectModel.update({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, {$set: {shareInfo: data.shareData}});
  523. }
  524. //取消分享
  525. else {
  526. await projectModel.update({ID: data.projectID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]}, {$pull: {shareInfo: {userID: {$in: shareUserIDs}}}});
  527. }
  528. callback(req, res, 0, 'success', data.shareData);
  529. }
  530. catch (err){
  531. callback(req, res, 1, err, null);
  532. }
  533. },
  534. receiveProjects: async function(req, res) {
  535. try {
  536. let rst = {grouped: [], ungrouped: [], summaryInfo: null};
  537. let userID = req.session.sessionUser.id;
  538. let receiveProjects = await projectModel.find({
  539. $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}], compilation: req.session.sessionCompilation._id, 'shareInfo.userID': userID}, '-_id');
  540. //设置原项目用户信息
  541. if(receiveProjects.length > 0){
  542. let orgUserIDs = [];
  543. for(let proj of receiveProjects){
  544. orgUserIDs.push(proj.userID);
  545. if (proj.projType === projType.tender) {
  546. //设置工程专业
  547. proj._doc.feeStandardName = proj.property.feeStandardName || '';
  548. }
  549. delete proj._doc.property;
  550. }
  551. orgUserIDs = Array.from(new Set(orgUserIDs));
  552. let userObjIDs = [];
  553. for(let uID of orgUserIDs){
  554. userObjIDs.push(mongoose.Types.ObjectId(uID));
  555. }
  556. let orgUsersInfo = await userModel.find({_id: {$in : userObjIDs}});
  557. //建设项目
  558. let consProjIDs = [],
  559. ungroupedTenders = [];
  560. for(let proj of receiveProjects){
  561. if (proj.projType === projType.project) {
  562. consProjIDs.push(proj.ID);
  563. }
  564. //获取分享项目子项
  565. if (proj.projType !== projType.tender) {
  566. proj._doc.children = await pm_facade.getPosterityProjects([proj.ID]);
  567. for (let projC of proj._doc.children) {
  568. if (projC.projType === projType.project) {
  569. consProjIDs.push(projC.ID);
  570. } else if (projC.projType === projType.tender) {
  571. //设置工程专业
  572. projC._doc.feeStandardName = projC.property.feeStandardName || '';
  573. if (proj.projType === projType.engineering) {
  574. ungroupedTenders.push(projC._doc);
  575. }
  576. }
  577. delete projC._doc.property;
  578. }
  579. } else {//未分类的单位工程不进行汇总,只取价格信息
  580. ungroupedTenders.push(proj._doc);
  581. }
  582. //设置分组,单位工程及单项工程分到未分组那
  583. if (proj.projType === projType.tender || proj.projType === projType.engineering) {
  584. rst.ungrouped.push(proj);
  585. } else {
  586. rst.grouped.push(proj);
  587. }
  588. //设置项目类型为来自别人分享
  589. proj._doc.shareType = 'receive';
  590. for(let userData of orgUsersInfo){
  591. if(proj.userID == userData._id.toString()){
  592. let userInfo = {name: userData.real_name, mobile: userData.mobile, company: userData.company, email: userData.email};
  593. proj._doc.userInfo = userInfo;
  594. }
  595. }
  596. }
  597. consProjIDs = Array.from(new Set(consProjIDs));
  598. let summaryInfo = await pm_facade.getSummaryInfo(consProjIDs);
  599. let tendersFeeInfo = await pm_facade.getTendersFeeInfo(ungroupedTenders);
  600. rst.summaryInfo = {grouped: summaryInfo, ungrouped: tendersFeeInfo};
  601. }
  602. callback(req, res, 0, 'success', rst);
  603. }
  604. catch (err){
  605. console.log(err);
  606. callback(req, res, 1, err, null);
  607. }
  608. },
  609. getProjectsByQuery: async function (req, res) {
  610. try{
  611. let data = JSON.parse(req.body.data);
  612. let compilation = req.session.sessionCompilation._id;
  613. let query = data.query;
  614. query.compilation = compilation;
  615. let options = data.options;
  616. let projects = await projectModel.find(query, options);
  617. callback(req, res, 0, 'success', projects);
  618. }
  619. catch (err){
  620. callback(req, res, 1, err, null);
  621. }
  622. },
  623. getSummaryInfo: async function(req, res){
  624. try{
  625. let data = JSON.parse(req.body.data);
  626. let summaryInfo = await pm_facade.getSummaryInfo(data.projectIDs);
  627. callback(req, res, 0, 'success', summaryInfo);
  628. }
  629. catch (err){
  630. callback(req, res, 1, err, null);
  631. }
  632. },
  633. changeFile:async function(req,res){
  634. try{
  635. let data = JSON.parse(req.body.data);
  636. console.log(data);
  637. await pm_facade.changeFile(data.projects,data.user_id,data.fileID,data.name,data.from,data.type);
  638. callback(req, res, 0, 'success', []);
  639. }
  640. catch (err){
  641. console.log(err);
  642. callback(req, res, 1, err, null);
  643. }
  644. },
  645. getBasicInfo: async function(req, res) {
  646. try {
  647. let infoLib = await pm_facade.getBasicInfo(req.session.sessionCompilation._id);
  648. callback(req, res, 0, 'success', infoLib ? infoLib.info : []);
  649. } catch (err) {
  650. console.log(err);
  651. callback(req, res, 1, err, []);
  652. }
  653. },
  654. getProjectFeature: async function(req, res) {
  655. try {
  656. let data = JSON.parse(req.body.data);
  657. let featureLib = await pm_facade.getProjectFeature(data.valuationID, data.engineeringName, data.feeName);
  658. //工程专业设置为费用标准名称
  659. if (featureLib) {
  660. let engData = featureLib.feature.find(function (d) {
  661. return d.key === 'engineering';
  662. });
  663. if (engData) {
  664. engData.value = data.feeName;
  665. }
  666. }
  667. callback(req, res, 0, 'success', featureLib ? featureLib.feature : []);
  668. } catch (err) {
  669. console.log(err);
  670. callback(req, res, 1, err, []);
  671. }
  672. },
  673. getProjectByGranularity: async function(req, res) {
  674. try {
  675. let data = JSON.parse(req.body.data);
  676. let projData = await pm_facade.getProjectByGranularity(data.tenderID, data.granularity, req.session.sessionUser.id, req.session.compilationVersion);
  677. callback(req, res, 0, 'success', projData);
  678. } catch (err) {
  679. callback(req, res, 1, err, null);
  680. }
  681. }
  682. };