pm_controller.js 34 KB

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