pm_controller.js 31 KB

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