pm_controller.js 30 KB

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