pm_controller.js 33 KB

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