pm_controller.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /**
  2. * Created by Mai on 2017/1/18.
  3. */
  4. const UnitPriceFileModel = require("../../glj/models/unit_price_file_model");
  5. const moment = require('moment');
  6. const CompilationModel = require("../../users/models/compilation_model");
  7. const UserModel = require("../../users/models/user_model");
  8. const userModelObj = new UserModel();
  9. let mongoose = require('mongoose');
  10. let ProjectsData = require('../models/project_model').project;
  11. let labourCoe = require('../../main/facade/labour_coe_facade');
  12. let projType = require('../models/project_model').projType;
  13. let fileType = require('../models/project_model').fileType;
  14. const engineering = require("../../common/const/engineering");
  15. let EngineeringLibModel = require("../../users/models/engineering_lib_model");
  16. const engineeringModel = mongoose.model('engineering_lib');
  17. let fee_rate_facade = require("../../fee_rates/facade/fee_rates_facade");
  18. let billsModel = require('../../main/models/bills').model;
  19. let rationsModel = require('../../main/models/ration').model;
  20. let projectModel = mongoose.model('projects');
  21. let unitPriceFileModel = mongoose.model('unit_price_file');
  22. let feeRateFileModel = mongoose.model('fee_rate_file');
  23. let asyncTool = require('async');
  24. let pm_facade = require('../facade/pm_facade');
  25. const userModel = mongoose.model('user');
  26. let config = require("../../../config/config.js");
  27. const optionModel = mongoose.model('options');
  28. const stdBillsGuidanceLibModel = mongoose.model('std_billsGuidance_lib');
  29. const fs = require('fs');
  30. const _ = require('lodash');
  31. const SectionTreeDao = require('../../complementary_ration_lib/models/sectionTreeModel');
  32. const uuidV1 = require('uuid/v1');
  33. let sectionTreeDao = new SectionTreeDao();
  34. let consts = require('../../main/models/project_consts');
  35. let rp = require('request-promise');
  36. const commonUtil = require('../../../public/common_util');
  37. const { ValuationText, AdminLevelType } = require('../../../public/common_constants');
  38. const { getTitle } = require("../../../public/titleUtil");
  39. //统一回调函数
  40. let callback = function (req, res, err, message, data) {
  41. res.json({ error: err, message: message, data: data });
  42. };
  43. module.exports = {
  44. redirectToImportServer,
  45. checkRight: function (req, res) {
  46. if (typeof req.body.data === 'object') {
  47. req.body.data = JSON.stringify(req.body.data);
  48. }
  49. let data = JSON.parse(req.body.data);
  50. if (data.user_id) {
  51. return data.user_id === req.session.sessionUser.id;
  52. } else {
  53. return false;
  54. }
  55. },
  56. checkProjectRight: function (userId, projectId, callback) {
  57. ProjectsData.getProject(projectId).then(async function (result) {
  58. /**
  59. * result._doc.userID(Number): MongoDB
  60. * userId(String): Session.userID
  61. */
  62. let shareInfo = null;
  63. let isOpenShareProject = false;
  64. //判断是否是打开分享的项目,分享项目shareInfo不为null
  65. if (userId !== result.userID) {
  66. shareInfo = await pm_facade.getShareInfo(userId, result.ID);
  67. isOpenShareProject = true;
  68. }
  69. if ((userId === result.userID || shareInfo) && result._doc.projType === projType.tender) {
  70. const allowCooperate = (shareInfo || {}).allowCooperate;
  71. callback(true, result, isOpenShareProject, allowCooperate);
  72. } else {
  73. callback(false);
  74. }
  75. }).catch(function (err) {
  76. callback(false);
  77. });
  78. },
  79. getProjects: async function (req, res) {
  80. await ProjectsData.getUserProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, function(err, message, projects){
  81. if (projects) {
  82. callback(req, res, err, message, projects);
  83. } else {
  84. callback(req, res, err, message, null);
  85. }
  86. });
  87. },
  88. getAllUnitProjects: async function (req, res) {
  89. const rootProjectID = JSON.parse(req.body.data).rootProjectID;
  90. await ProjectsData.getAllUnitProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, rootProjectID, function (err, message, data) {
  91. console.log(err);
  92. callback(req, res, err, message, data);
  93. });
  94. },
  95. updateProjects: async function (req, res) {
  96. let data = JSON.parse(req.body.data);
  97. try {
  98. await ProjectsData.updateUserProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, req.session.sessionCompilation, data.updateData, function (err, message, data) {
  99. if (err === 0) {
  100. callback(req, res, err, message, data);
  101. } else {
  102. callback(req, res, err, message, null);
  103. }
  104. });
  105. } catch (err) {
  106. console.log(err);
  107. callback(req, res, 1, String(err), null);
  108. }
  109. },
  110. // CSL, 2017-12-14 该方法用于项目属性:提交保存混合型数据,这些数据来自不同的表,包括projects.property、ration、bills、labour_coes.
  111. updateMixDatas: async function (req, res) {
  112. let datas = JSON.parse(req.body.data).mixDataArr;
  113. let functions = [];
  114. let specialResult = {};
  115. function updateFunc(model, cod, doc) {
  116. return function (cb) {
  117. model.update(cod, doc, cb);
  118. }
  119. };
  120. function updateLC() {
  121. return function (cb) {
  122. datas.labourCoes.updateData.projectID = datas.projectID;
  123. labourCoe.save(datas.labourCoes.updateData, cb);
  124. }
  125. };
  126. // 项目属性
  127. if (Object.keys(datas.properties).length > 0) {
  128. //基本信息特殊处理,更新建设项目
  129. if (datas.properties['property.basicInformation']) {
  130. let constructionProject = await pm_facade.getConstructionProject(datas.projectID);
  131. if (constructionProject) {
  132. functions.push(updateFunc(projectModel, { ID: constructionProject.ID }, { 'property.basicInformation': datas.properties['property.basicInformation'] }));
  133. }
  134. delete datas.properties['property.basicInformation'];
  135. }
  136. // 工程特征特殊处理
  137. if (datas.properties['property.projectFeature']) {
  138. const engineeringItem = _.find(datas.properties['property.projectFeature'], { key: 'engineering' });
  139. const feeStandardItem = _.find(datas.properties['property.projectFeature'], { key: 'feeStandard' });
  140. if (engineeringItem && engineeringItem.value && feeStandardItem && feeStandardItem.value) {
  141. const project = await projectModel.findOne({ ID: datas.projectID }, { 'property.valuation': 1, 'property.engineering_id': 1 }).lean();
  142. if (project) {
  143. const orgEngineeringID = project.property.engineering_id;
  144. const engineering = await engineeringModel.findOne({ valuationID: project.property.valuation, name: engineeringItem.value, feeName: feeStandardItem.value }, { _id: 1, tax_group: 1, bill_lib: 1 }).lean();
  145. if (engineering && orgEngineeringID !== engineering._id.toString()) {
  146. datas.properties['property.engineeringName'] = engineeringItem.value;
  147. datas.properties['property.feeStandardName'] = feeStandardItem.value;
  148. datas.properties['property.engineering_id'] = engineering._id.toString();
  149. if (engineering.tax_group && engineering.tax_group[0] && engineering.tax_group[0].fee_lib) {
  150. // 工程专业变更,需要更改费率
  151. specialResult.newFeeLibID = engineering.tax_group[0].fee_lib.id;
  152. }
  153. specialResult.billLibs = engineering.bill_lib;
  154. }
  155. }
  156. }
  157. }
  158. functions.push(updateFunc(projectModel, { ID: datas.projectID }, datas.properties));
  159. };
  160. //选项
  161. if (datas.options && datas.options.updateData) {
  162. functions.push(updateFunc(optionModel, { user_id: req.session.sessionUser.id, compilation_id: req.session.sessionCompilation._id }, { 'options.GENERALOPTS': datas.options.updateData }));
  163. }
  164. // 人工系数
  165. if (datas.labourCoes && datas.labourCoes.updateData) {
  166. functions.push(updateLC());
  167. };
  168. // 清单:每文档doc只存储一条清单,每条清单都必须定位一次文档,无法合并处理
  169. if (datas.bills && datas.bills.length > 0) {
  170. for (let bill of datas.bills) {
  171. functions.push(updateFunc(billsModel, { projectID: datas.projectID, ID: bill.ID, deleteInfo: null }, bill));
  172. };
  173. };
  174. // 定额:每文档doc只存储一条定额,每条定额都必须定位一次文档,无法合并处理
  175. if (datas.rations && datas.rations.length > 0) {
  176. for (let ration of datas.rations) {
  177. functions.push(updateFunc(rationsModel, { projectID: datas.projectID, ID: ration.ID, deleteInfo: null }, ration));
  178. };
  179. };
  180. asyncTool.parallel(functions, function (err, result) {
  181. {
  182. if (!err) {
  183. res.json({ error: 0, message: err, data: specialResult});
  184. } else {
  185. res.json({ error: 1, message: err, data: null });
  186. }
  187. }
  188. });
  189. },
  190. updateFiles: async function (req, res) {
  191. let data = JSON.parse(req.body.data);
  192. let updateDatas = data.updateDatas;
  193. await ProjectsData.udpateUserFiles(req.session.sessionUser.id, updateDatas, function (err, message, data) {
  194. callback(req, res, err, message, data);
  195. });
  196. },
  197. defaultSettings: async function (req, res) {
  198. try {
  199. let data = JSON.parse(req.body.data);
  200. let projectID = data.projectID;
  201. let defaultSettingSc = await ProjectsData.defaultSettings(req.session.sessionUser.id, req.session.sessionCompilation, projectID);
  202. if (!defaultSettingSc) {
  203. throw '恢复失败';
  204. }
  205. res.json({ error: 0, message: '恢复成功', data: null });
  206. }
  207. catch (error) {
  208. console.log(error);
  209. res.json({ error: 1, message: error, data: null });
  210. }
  211. },
  212. /* copyProjects: function (req, res) {
  213. let data = JSON.parse(req.body.data);
  214. ProjectsData.copyUserProjects(req.session.sessionUser.id, req.session.sessionCompilation._id, data.updateData, function (err, message, data) {
  215. if (err === 0) {
  216. callback(req, res, err, message, data);
  217. } else {
  218. callback(req, res, err, message, null);
  219. }
  220. });
  221. },*/
  222. rename: function (req, res) {
  223. let data = JSON.parse(req.body.data);
  224. ProjectsData.rename(req.session.sessionUser.id, req.session.sessionCompilation._id, data, function (err, message) {
  225. callback(req, res, err, message, null);
  226. });
  227. },
  228. //project getData接口
  229. getData: function (projectID, callback, isReport, userID) {
  230. projectModel.findOne({ $or: [{ deleteInfo: null }, { 'deleteInfo.deleted': false }], ID: projectID }, '-_id').then(async function (project) {
  231. if (!project) {
  232. callback('', consts.projectConst.PROJECT_INFO, {});
  233. }
  234. let engineeringLibModel = new EngineeringLibModel();
  235. let engineeringInfo = project !== null && project.property.engineering_id !== undefined ?
  236. await engineeringLibModel.getEngineering(project.property.engineering_id) : null;
  237. let projInfo = project._doc;
  238. if (engineeringInfo !== null) {
  239. if (engineeringInfo.billsGuidance_lib) {
  240. for (let billsGuidanceLib of engineeringInfo.billsGuidance_lib) {
  241. let stdBillsGuidanceLib = await stdBillsGuidanceLibModel.findOne({ ID: billsGuidanceLib.id });
  242. if (stdBillsGuidanceLib) {
  243. billsGuidanceLib.type = stdBillsGuidanceLib.type ? stdBillsGuidanceLib.type : 1;
  244. }
  245. }
  246. }
  247. projInfo.engineeringInfo = engineeringInfo;
  248. }
  249. // 获取建设项目相关信息
  250. const constructionProject = await pm_facade.getConstructionProject(projectID);
  251. if (constructionProject) {
  252. projInfo.property.basicInformation = constructionProject.property && constructionProject.property.basicInformation || null;
  253. projInfo.property.projectValuationType = constructionProject.property && constructionProject.property.valuationType || null;
  254. }
  255. //获取单位工程完整目录结构
  256. let fullPath = await pm_facade.getFullPath(projectID);
  257. projInfo.fullPath = fullPath;
  258. // 获取分享的tip
  259. projInfo.shareTip = await pm_facade.getShareTip(projectID, 2);
  260. // 获取分享状态
  261. if (typeof userID === 'string') {
  262. projInfo.shareState = await pm_facade.getShareState(projectID, userID);
  263. }
  264. // 获取项目所属用户
  265. projInfo.owner = await userModel.findOne({_id: mongoose.Types.ObjectId(project.userID)}, 'real_name mobile').lean();
  266. projInfo.opener = await userModel.findOne({_id: mongoose.Types.ObjectId(userID)}, 'real_name mobile').lean();
  267. callback('', consts.projectConst.PROJECT_INFO, project);
  268. }, function (err) {
  269. callback(err, consts.projectConst.PROJECT_INFO, {});
  270. });
  271. },
  272. getProject: function (req, res) {
  273. let data = JSON.parse(req.body.data);
  274. let projectID = data.proj_id;
  275. ProjectsData.getUserProject(req.session.sessionUser.id, data.proj_id, async function (err, message, data) {
  276. if (err === 0) {
  277. let engineeringLibModel = new EngineeringLibModel();
  278. let engineeringInfo = data !== null && data.property.engineering_id !== undefined ?
  279. await engineeringLibModel.getEngineering(data.property.engineering_id) : null;
  280. let strData = JSON.stringify(data);
  281. let projInfo = JSON.parse(strData);
  282. if (engineeringInfo !== null) {
  283. if (engineeringInfo.billsGuidance_lib) {
  284. for (let billsGuidanceLib of engineeringInfo.billsGuidance_lib) {
  285. let stdBillsGuidanceLib = await stdBillsGuidanceLibModel.findOne({ ID: billsGuidanceLib.id });
  286. if (stdBillsGuidanceLib) {
  287. billsGuidanceLib.type = stdBillsGuidanceLib.type ? stdBillsGuidanceLib.type : 1;
  288. }
  289. }
  290. }
  291. projInfo.engineeringInfo = engineeringInfo;
  292. }
  293. //读取建设项目的基本信息
  294. let basicInfo = await ProjectsData.getBasicInfo(projectID);
  295. if(basicInfo !== null){
  296. projInfo.property.basicInformation = basicInfo;
  297. }
  298. //获取单位工程完整目录结构
  299. let fullPath = await pm_facade.getFullPath(projectID);
  300. projInfo.fullPath = fullPath;
  301. callback(req, res, err, message, projInfo);
  302. } else {
  303. callback(req, res, err, message, null);
  304. }
  305. });
  306. },
  307. beforeOpenProject: function (req, res) {
  308. let data = JSON.parse(req.body.data);
  309. ProjectsData.beforeOpenProject(req.session.sessionUser.id, data.proj_id, data.updateData, function (err, message, data) {
  310. callback(req, res, err, message, data);
  311. });
  312. },
  313. getNewProjectID: function (req, res) {
  314. let data = JSON.parse(req.body.data);
  315. ProjectsData.getNewProjectID(data.count, function (err, message, data) {
  316. callback(req, res, err, message, data);
  317. });
  318. },
  319. // 项目管理首页
  320. index: async function (request, response) {
  321. //await pm_facade.prepareShareList();
  322. // 获取编办信息
  323. let sessionCompilation = request.session.sessionCompilation;
  324. if (sessionCompilation === undefined || sessionCompilation === null) {
  325. return response.redirect('/logout');
  326. }
  327. let compilationModel = new CompilationModel();
  328. //更新编办信息
  329. let compilationData = await compilationModel.getCompilationById(sessionCompilation._id);
  330. request.session.sessionCompilation = compilationData;
  331. sessionCompilation = request.session.sessionCompilation;
  332. //更新用户的使用过的费用定额列表
  333. let isFirst = await pm_facade.isFirst(request.session.sessionUser.id, compilationData._id.toString());
  334. let engineeringLibModel = new EngineeringLibModel();
  335. // 建议估算
  336. let suggestionValuation = sessionCompilation.suggestion_valuation !== undefined ?
  337. sessionCompilation.suggestion_valuation : [];
  338. suggestionValuation = await engineeringLibModel.getLib(suggestionValuation);
  339. // 可行性估算
  340. let feasibilityValuation = sessionCompilation.feasibility_valuation !== undefined ?
  341. sessionCompilation.feasibility_valuation : [];
  342. feasibilityValuation = await engineeringLibModel.getLib(feasibilityValuation);
  343. // 估算
  344. let estimationValuation = sessionCompilation.estimation_valuation !== undefined ?
  345. sessionCompilation.estimation_valuation : [];
  346. estimationValuation = await engineeringLibModel.getLib(estimationValuation);
  347. // 概算
  348. let roughValuation = sessionCompilation.rough_valuation !== undefined ?
  349. sessionCompilation.rough_valuation : [];
  350. roughValuation = await engineeringLibModel.getLib(roughValuation);
  351. // 清单计价
  352. let billValuation = sessionCompilation.bill_valuation !== undefined ?
  353. sessionCompilation.bill_valuation : [];
  354. billValuation = await engineeringLibModel.getLib(billValuation);
  355. // 定额计价
  356. let rationValuation = sessionCompilation.ration_valuation !== undefined ?
  357. sessionCompilation.ration_valuation : [];
  358. rationValuation = await engineeringLibModel.getLib(rationValuation);
  359. let absoluteUrl = compilationData.overWriteUrl ? request.app.locals.rootDir + compilationData.overWriteUrl : request.app.locals.rootDir;
  360. let overWriteUrl = fs.existsSync(absoluteUrl) && fs.statSync(absoluteUrl).isFile() ? compilationData.overWriteUrl : null;
  361. //欢迎页显示控制
  362. let [isShow,context,showTime] = await pm_facade.getWelcomeInfo(sessionCompilation._id,request.session.sessionUser,request.session.compilationVersion.includes('学习'));
  363. const unreadShareList = await pm_facade.getUnreadShareListByCompilation(request.session.sessionUser.id, sessionCompilation._id);
  364. let renderData = {
  365. unreadShareList: JSON.stringify(unreadShareList),
  366. isFirst: isFirst,
  367. isShow:isShow,
  368. context:context,
  369. showTime:showTime,
  370. userAccount: request.session.userAccount,
  371. userID: request.session.sessionUser.id,
  372. compilationData: JSON.stringify(sessionCompilation),
  373. overWriteUrl: overWriteUrl,
  374. suggestionValuation: JSON.stringify(suggestionValuation),
  375. feasibilityValuation: JSON.stringify(feasibilityValuation),
  376. estimationValuation: JSON.stringify(estimationValuation),
  377. roughValuation: JSON.stringify(roughValuation),
  378. billValuation: JSON.stringify(billValuation),
  379. rationValuation: JSON.stringify(rationValuation),
  380. engineeringList: JSON.stringify(engineering.List),
  381. adminLevelType: JSON.stringify(AdminLevelType),
  382. compilationName: sessionCompilation.name,
  383. versionName: request.session.compilationVersion,
  384. socketPort:config[process.env.NODE_ENV].socketPort?config[process.env.NODE_ENV].socketPort:5500,
  385. LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
  386. title:getTitle(request.headers.host)
  387. };
  388. response.render('building_saas/pm/html/project-management.html', renderData);
  389. },
  390. //第一次进入该费用定额时准备的初始数据
  391. prepareInitialData: async function (request, response) {
  392. try {
  393. const data = {
  394. userID: request.session.sessionUser.id,
  395. compilationID: request.session.sessionCompilation._id,
  396. example: request.session.sessionCompilation.example
  397. };
  398. await redirectToImportServer(data, 'prepareInitialData', request);
  399. callback(request, response, 0, 'success', null);
  400. } catch (err) {
  401. console.log(err);
  402. callback(request, response, 1, err.toString(), null);
  403. }
  404. },
  405. // 获取单价文件列表
  406. getUnitFileList: async function (request, response) {
  407. let data = request.body.data;
  408. try {
  409. data = JSON.parse(data);
  410. let projectId = data.parentID !== undefined ? data.parentID : 0;
  411. if (isNaN(projectId) && projectId <= 0) {
  412. throw { msg: 'id数据有误!', err: 1 };
  413. }
  414. /*// 获取对应建设项目下所有的单位工程id
  415. let idList = await ProjectsData.getTenderByProjectId(projectId);
  416. if (idList.length <= 0) {
  417. throw {msg: '不存在对应单位工程', err: 0};
  418. }*/
  419. // 获取对应的单价文件
  420. let unitPriceFileModel = new UnitPriceFileModel();
  421. let unitPriceFileData = await unitPriceFileModel.getDataByRootProject(projectId);
  422. if (unitPriceFileData === null) {
  423. throw { msg: '不存在对应单价文件', err: 0 };
  424. }
  425. // 整理数据
  426. let unitPriceFileList = [];
  427. for (let unitPriceFile of unitPriceFileData) {
  428. let tmp = {
  429. name: unitPriceFile.name,
  430. id: unitPriceFile.id
  431. };
  432. unitPriceFileList.push(tmp);
  433. }
  434. callback(request, response, 0, '', unitPriceFileList);
  435. } catch (error) {
  436. console.log(error);
  437. let responseData = error.err === 1 ? null : [];
  438. callback(request, response, error.err, error.msg, responseData);
  439. }
  440. },
  441. getFeeRateFileList: async function (request, response) {
  442. let data = request.body.data;
  443. try {
  444. data = JSON.parse(data);
  445. let projectId = data.parentID !== undefined ? data.parentID : 0;
  446. if (isNaN(projectId) && projectId <= 0) {
  447. throw { msg: 'id数据有误!', err: 1 };
  448. }
  449. // 获取对应建设项目下所有的单位工程id
  450. let feeRateFileList = await fee_rate_facade.getFeeRatesByProject(projectId);
  451. callback(request, response, 0, '', feeRateFileList);
  452. } catch (error) {
  453. console.log(error);
  454. let responseData = error.err === 1 ? null : [];
  455. callback(request, response, error.err, error.msg, responseData);
  456. }
  457. },
  458. getGC: async function(req, res) {
  459. const userID = req.session.sessionUser.id;
  460. const compilationId = req.session.sessionCompilation._id;
  461. try {
  462. const rst = [];
  463. const priceFilesTask = ProjectsData.getGCFiles(fileType.unitPriceFile, userID);
  464. const feeRateFilesTask = ProjectsData.getGCFiles(fileType.feeRateFile, userID);
  465. const tendersTask = ProjectsData.getGCFiles(projType.tender, userID);
  466. const [priceFiles, rateFiles, tenders] = await Promise.all([priceFilesTask, feeRateFilesTask, tendersTask]);
  467. // 建设项目
  468. let projectIDs = tenders.reduce((acc, cur) => {
  469. acc.push(cur.ParentID);
  470. return acc;
  471. }, []);
  472. projectIDs = [...new Set(projectIDs)];
  473. const projects = await ProjectsData.getProjectsByIds(userID, compilationId, projectIDs);
  474. projects.forEach(project => {
  475. const projectDoc = project._doc;
  476. const relTenders = tenders.filter(tender => tender.ParentID === projectDoc.ID);
  477. const unitPriceFiles = priceFiles.filter(file => relTenders.find(tender => tender.property.unitPriceFile.id === file.id));
  478. const feeRateFiles = rateFiles.filter(file => relTenders.find(tender => tender.property.feeFile.id === file.ID));
  479. projectDoc.children = relTenders;
  480. projectDoc.unitPriceFiles = unitPriceFiles;
  481. projectDoc.feeRateFiles = feeRateFiles;
  482. rst.push(projectDoc);
  483. });
  484. callback(req, res, 0, 'success', rst);
  485. } catch (err) {
  486. console.log(err);
  487. callback(req, res, true, err.message, null);
  488. }
  489. },
  490. getGCDatas: async function (request, response) {
  491. let userID = request.session.sessionUser.id;
  492. let compilatoinId = request.session.sessionCompilation._id;
  493. let rst = [];
  494. let _projs = Object.create(null), _engs = Object.create(null), prefix = 'ID_';
  495. try {
  496. let gc_unitPriceFiles = await ProjectsData.getGCFiles(fileType.unitPriceFile, userID);
  497. let gc_feeRateFiles = await ProjectsData.getGCFiles(fileType.feeRateFile, userID);
  498. let gc_tenderFiles = await ProjectsData.getGCFiles(projType.tender, userID);
  499. for (let i = 0, len = gc_unitPriceFiles.length; i < len; i++) {
  500. let gc_uf = gc_unitPriceFiles[i];
  501. let theProj = _projs[prefix + gc_uf.root_project_id] || null;
  502. if (!theProj) {
  503. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_uf.root_project_id]);
  504. if (tempProj.length > 0 && tempProj[0].projType !== projType.folder) {
  505. theProj = _projs[prefix + gc_uf.root_project_id] = tempProj[0]._doc;
  506. buildProj(theProj);
  507. }
  508. }
  509. if (theProj) {
  510. theProj.unitPriceFiles.push(gc_uf);
  511. }
  512. }
  513. for (let i = 0, len = gc_feeRateFiles.length; i < len; i++) {
  514. let gc_ff = gc_feeRateFiles[i];
  515. let theProj = _projs[prefix + gc_ff.rootProjectID] || null;
  516. if (!theProj) {
  517. let tempProj = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_ff.rootProjectID]);
  518. if (tempProj.length > 0 && tempProj[0].projType !== projType.folder) {
  519. theProj = _projs[prefix + gc_ff.rootProjectID] = tempProj[0]._doc;
  520. buildProj(theProj);
  521. }
  522. }
  523. if (theProj) {
  524. theProj.feeRateFiles.push(gc_ff);
  525. }
  526. }
  527. if (gc_tenderFiles.length > 0) {
  528. for (let i = 0, len = gc_tenderFiles.length; i < len; i++) {
  529. let gc_t = gc_tenderFiles[i];
  530. let theProj = _projs[prefix + gc_t.ParentID] || null;
  531. if(!theProj){
  532. let tempProjs = await ProjectsData.getProjectsByIds(userID, compilatoinId, [gc_t.ParentID]);
  533. if(tempProjs.length > 0 && tempProjs[0].projType === projType.project){
  534. theProj = _projs[prefix + gc_t.ParentID] = tempProjs[0]._doc;
  535. buildProj(theProj);
  536. }
  537. }
  538. if(theProj) {
  539. theProj.children.push(gc_t);
  540. }
  541. }
  542. }
  543. for (let i in _projs) {
  544. rst.push(_projs[i]);
  545. }
  546. function buildProj(proj) {
  547. proj.children = [];
  548. proj.unitPriceFiles = [];
  549. proj.feeRateFiles = [];
  550. }
  551. callback(request, response, 0, 'success', rst);
  552. }
  553. catch (error) {
  554. callback(request, response, true, error, null);
  555. }
  556. },
  557. recGC: function (request, response) {
  558. let userID = request.session.sessionUser.id,
  559. compilationId = request.session.sessionCompilation._id;
  560. let data = JSON.parse(request.body.data);
  561. let nodes = data.nodes;
  562. ProjectsData.recGC(userID, compilationId, nodes, function (err, msg, data) {
  563. callback(request, response, err, msg, data);
  564. });
  565. },
  566. delGC: async function (request, response) {
  567. let data = JSON.parse(request.body.data);
  568. let delDatas = data.delDatas;
  569. let bulkProjs = [], bulkUFs = [], bulkFFs = [];
  570. try {
  571. for (let data of delDatas) {
  572. if (data.updateType === 'Project') {
  573. bulkProjs.push({ updateOne: { filter: { ID: data.ID }, update: { 'deleteInfo.completeDeleted': true } } });
  574. }
  575. else if (data.updateType === fileType.unitPriceFile) {
  576. bulkUFs.push({ updateOne: { filter: { id: data.ID }, update: { 'deleteInfo.completeDeleted': true } } });
  577. }
  578. else {
  579. bulkFFs.push({ updateOne: { filter: { ID: data.ID }, update: { 'deleteInfo.completeDeleted': true } } });
  580. }
  581. }
  582. if (bulkProjs.length > 0) {
  583. await projectModel.bulkWrite(bulkProjs);
  584. }
  585. if (bulkUFs.length > 0) {
  586. await unitPriceFileModel.bulkWrite(bulkUFs);
  587. }
  588. if (bulkFFs.length > 0) {
  589. await feeRateFileModel.bulkWrite(bulkFFs);
  590. }
  591. callback(request, response, 0, 'success', null);
  592. } catch (err) {
  593. callback(request, response, 1, err, null);
  594. }
  595. },
  596. moveProject: async function (req, res) {
  597. let result = {
  598. error: 0
  599. };
  600. try {
  601. let data = req.body.data;
  602. let rdata = await pm_facade.moveProject(data);
  603. result.data = rdata;
  604. } catch (err) {
  605. console.log(err);
  606. result.error = 1;
  607. result.message = err.message;
  608. }
  609. res.json(result);
  610. },
  611. copyProjects: async function (req, res) {
  612. let result = {
  613. error: 0
  614. };
  615. try {
  616. let data = { dataString: req.body.data, userID: req.session.sessionUser.id, compilationID: req.session.sessionCompilation._id };
  617. result = await redirectToImportServer(data, "copyProject", req);
  618. } catch (err) {
  619. console.log(err);
  620. result.error = 1;
  621. result.message = err.message;
  622. }
  623. res.json(result);
  624. },
  625. projectShareInfo: async function (req, res) {
  626. try {
  627. const data = JSON.parse(req.body.data);
  628. const shareList = await pm_facade.getShareList({ projectID: data.projectID });
  629. const shareInfoMap = await pm_facade.getShareInfoMap(null, shareList);
  630. const shareInfo = shareInfoMap[data.projectID] || [];
  631. callback(req, res, 0, 'success', shareInfo);
  632. }
  633. catch (err) {
  634. callback(req, res, 1, err, null);
  635. }
  636. },
  637. getRecentShareInfo: async function (req, res) {
  638. try {
  639. const { count } = JSON.parse(req.body.data);
  640. const userID = req.session.sessionUser.id;
  641. const recentUsers = await pm_facade.getRecentShareList(userID, count);
  642. const contacts = await userModelObj.getContacts(userID);
  643. callback(req, res, 0, 'success', { recentUsers, contacts });
  644. } catch (err) {
  645. console.log(err);
  646. callback(req, res, 1, err.message, null);
  647. }
  648. },
  649. getInitialShareData: async function (req, res) {
  650. try {
  651. const { count, projectID } = JSON.parse(req.body.data);
  652. // 分享建设项目,仅提供给专业版用户
  653. /* const proShareProjType = [projType.folder, projType.project];
  654. const project = await projectModel.findOne({ ID: projectID }, 'projType');
  655. if (project && proShareProjType.indexOf(project.projType) >= 0) {
  656. const isFree = userModelObj.isFreeFromSession(req.session.compilationVersion);
  657. if (isFree) {
  658. callback(req, res, 0, 'success', { isFree });
  659. return;
  660. }
  661. } */
  662. const userID = req.session.sessionUser.id;
  663. // 最近分享
  664. const recentUsers = await pm_facade.getRecentShareList(userID, count);
  665. // 联系人
  666. const contacts = await userModelObj.getContacts(userID);
  667. // 分享过的人
  668. const sharedUsers = await pm_facade.getProjectShareList(projectID);
  669. callback(req, res, 0, 'success', { recentUsers, contacts, sharedUsers });
  670. } catch (err) {
  671. console.log(err);
  672. callback(req, res, 1, err.message, null);
  673. }
  674. },
  675. getReceiveLibList: async function (req, res) {
  676. try {
  677. const { libType } = JSON.parse(req.body.data);
  678. const userID = req.session.sessionUser.id;
  679. const compilationID = req.session.sessionCompilation._id;
  680. const list = await pm_facade.getReceiveLibList(userID, compilationID, libType);
  681. callback(req, res, 0, 'success', list);
  682. } catch (err) {
  683. callback(req, res, 1, err.message, null);
  684. }
  685. },
  686. getInitialShareLibData: async function (req, res) {
  687. try {
  688. const { libType, count } = JSON.parse(req.body.data);
  689. const userID = req.session.sessionUser.id;
  690. const compilationID = req.session.sessionCompilation._id;
  691. // 最近分享
  692. const recentUsers = await pm_facade.getRecentShareList(userID, count);
  693. // 联系人
  694. const contacts = await userModelObj.getContacts(userID);
  695. // 分享过的人
  696. const sharedUsers = await pm_facade.getLibShareList(userID, compilationID, libType, count);
  697. callback(req, res, 0, 'success', { recentUsers, contacts, sharedUsers });
  698. } catch (err) {
  699. console.log(err);
  700. callback(req, res, 1, err.message, null);
  701. }
  702. },
  703. shareLib: async function (req, res) {
  704. try {
  705. const { type, libType, shareData, count } = JSON.parse(req.body.data);
  706. const owner = req.session.sessionUser.id;
  707. const compilationID = req.session.sessionCompilation._id;
  708. const shareDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
  709. shareData.forEach(item => item.shareDate = shareDate);
  710. const task = [];
  711. // 是否只是单纯的更新分享选项,如果不是,需要重新获取最近分享和联系人
  712. let isSimpleUpdate = true;
  713. if (type === 'create') {
  714. isSimpleUpdate = false;
  715. // 生成分享记录
  716. const docs = shareData.map(item => ({
  717. compilationID,
  718. owner,
  719. libType,
  720. ID: uuidV1(),
  721. receiver: item.receiver,
  722. allowCooperate: false,
  723. shareDate: item.shareDate,
  724. updateDate: item.shareDate
  725. }));
  726. task.push(pm_facade.addShareList(docs, true));
  727. // 分享即互相添加为联系人
  728. task.push(userModelObj.addContact(docs[0].owner, docs[0].receiver));
  729. } else if (type === 'cancel') { // 取消分享
  730. const cancelReceivers = shareData.map(item => item.receiver);
  731. task.push(pm_facade.deleteShareList({ owner, libType, compilationID, receiver: { $in: cancelReceivers } }, true));
  732. }
  733. await Promise.all(task);
  734. const rst = {};
  735. if (!isSimpleUpdate) {
  736. const rstTask = [
  737. pm_facade.getRecentShareList(owner, count),
  738. userModelObj.getContacts(owner)
  739. ];
  740. const [recentUsers, contacts] = await Promise.all(rstTask);
  741. Object.assign(rst, { recentUsers, contacts });
  742. }
  743. callback(req, res, 0, 'success', rst);
  744. } catch (err) {
  745. console.log(err);
  746. logger.info(err);
  747. callback(req, res, 1, err, null);
  748. }
  749. },
  750. share: async function (req, res) {
  751. try {
  752. const data = JSON.parse(req.body.data);
  753. const { type, permissionType, shareData, projectID, count } = data;
  754. const userID = req.session.sessionUser.id;
  755. const shareDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
  756. shareData.forEach(item => item.shareDate = shareDate);
  757. const task = [];
  758. // 是否只是单纯的更新分享选项,如果不是,需要重新获取最近分享和联系人
  759. let isSimpleUpdate = true;
  760. if (type === 'create') {
  761. isSimpleUpdate = false;
  762. // 生成分享记录
  763. const docs = shareData.map(item => ({
  764. ID: uuidV1(),
  765. projectID,
  766. owner: userID,
  767. receiver: item.userID,
  768. allowCooperate: item.allowCooperate,
  769. allowCopy: item.allowCopy,
  770. shareDate: item.shareDate,
  771. updateDate: item.shareDate
  772. }));
  773. task.push(pm_facade.addShareList(docs));
  774. // 分享即互相添加为联系人
  775. task.push(userModelObj.addContact(docs[0].owner, docs[0].receiver));
  776. } else if (type === 'update') {
  777. // 取消分享(以前项目管理界面可一次进行更新和取消)
  778. const cancelReceivers = shareData
  779. .filter(item => item.isCancel)
  780. .map(item => item.userID);
  781. if (cancelReceivers.length) {
  782. isSimpleUpdate = false;
  783. task.push(pm_facade.deleteShareList({ projectID, receiver: { $in: cancelReceivers } }));
  784. }
  785. // 更新分享选项
  786. const updateData = shareData
  787. .filter(item => !item.isCancel)
  788. .map(item => {
  789. const update = { updateDate: shareDate };
  790. if (commonUtil.isDef(item.allowCopy)) {
  791. update.allowCopy = item.allowCopy;
  792. }
  793. if (commonUtil.isDef(item.allowCooperate)) {
  794. update.allowCooperate = item.allowCooperate;
  795. }
  796. return {
  797. query: {
  798. projectID,
  799. receiver: item.userID
  800. },
  801. update
  802. };
  803. });
  804. if (updateData.length) {
  805. task.push(pm_facade.updateShareList(updateData))
  806. }
  807. } else { // 取消分享
  808. const cancelReceivers = shareData.map(item => item.userID);
  809. task.push(pm_facade.deleteShareList({ projectID, receiver: { $in: cancelReceivers } }));
  810. }
  811. await Promise.all(task);
  812. // 获取需要广播推送的单位工程
  813. // shareData数组的形式是以前需求需要,现在的需求下,shareData数组必定只有一个元素
  814. const emitTenders = await pm_facade.getShareInfoAfterChangePermission(permissionType, shareData[0].userID, projectID);
  815. let rst = { emitTenders };
  816. if (!isSimpleUpdate) {
  817. const rstTask = [
  818. pm_facade.getRecentShareList(userID, count),
  819. userModelObj.getContacts(userID)
  820. ];
  821. const [recentUsers, contacts] = await Promise.all(rstTask);
  822. Object.assign(rst, { recentUsers, contacts });
  823. }
  824. callback(req, res, 0, 'success', rst);
  825. }
  826. catch (err) {
  827. console.log(err);
  828. callback(req, res, 1, String(err), null);
  829. }
  830. },
  831. receiveProjects: async function (req, res) {
  832. try {
  833. let rst = { grouped: [], ungrouped: [], summaryInfo: null };
  834. let userID = req.session.sessionUser.id;
  835. const shareList = await pm_facade.getShareList({ receiver: userID });
  836. const receiveProjectIDs = shareList.map(item => item.projectID);
  837. const compilation = req.session.sessionCompilation._id;
  838. const notDeleted = [{ deleteInfo: null }, { 'deleteInfo.deleted': false }];
  839. const receiveProjects = await projectModel.find({ ID: { $in: receiveProjectIDs }, compilation, $or: notDeleted }, '-_id').lean();
  840. //设置原项目用户信息
  841. const shareInfoMap = await pm_facade.getShareInfoMap(null, shareList);
  842. if (receiveProjects.length > 0) {
  843. let orgUserIDs = [];
  844. for (let proj of receiveProjects) {
  845. orgUserIDs.push(proj.userID);
  846. if (proj.projType === projType.tender) {
  847. // 设置项目类别
  848. proj.valuationType = ValuationText[proj.property.valuationType] || '';
  849. proj._valuationType = proj.property.valuationType;
  850. //设置工程专业
  851. proj.feeStandardName = proj.property.feeStandardName || '';
  852. //设置计税方法
  853. proj.taxType = proj.property.taxType;
  854. }
  855. delete proj.property;
  856. }
  857. orgUserIDs = Array.from(new Set(orgUserIDs));
  858. let userObjIDs = [];
  859. for (let uID of orgUserIDs) {
  860. userObjIDs.push(mongoose.Types.ObjectId(uID));
  861. }
  862. let orgUsersInfo = await userModel.find({ _id: { $in: userObjIDs } });
  863. //建设项目
  864. let consProjIDs = [],
  865. ungroupedTenders = [];
  866. for (let proj of receiveProjects) {
  867. // 设置分享信息
  868. proj.shareInfo = shareInfoMap[proj.ID] || [];
  869. if (proj.projType === projType.project) {
  870. consProjIDs.push(proj.ID);
  871. }
  872. //获取分享项目子项
  873. if (proj.projType !== projType.tender) {
  874. proj.children = await pm_facade.getPosterityProjects([proj.ID]);
  875. for (let projC of proj.children) {
  876. // 设置分享信息
  877. projC.shareInfo = shareInfoMap[projC.ID] || [];
  878. if (projC.projType === projType.project) {
  879. consProjIDs.push(projC.ID);
  880. } else if (projC.projType === projType.tender) {
  881. // 设置项目类别
  882. projC.valuationType = ValuationText[projC.property.valuationType] || '';
  883. projC._valuationType = projC.property.valuationType;
  884. //设置工程专业
  885. projC.feeStandardName = projC.property.feeStandardName || '';
  886. }
  887. delete projC.property;
  888. }
  889. } else {//未分类的单位工程不进行汇总,只取价格信息
  890. ungroupedTenders.push(proj);
  891. }
  892. //设置分组,单位工程及单项工程分到未分组那
  893. if (proj.projType === projType.tender || proj.projType === projType.engineering) {
  894. rst.ungrouped.push(proj);
  895. } else {
  896. rst.grouped.push(proj);
  897. }
  898. //设置项目类型为来自别人分享
  899. proj.shareType = 'receive';
  900. for (let userData of orgUsersInfo) {
  901. if (proj.userID == userData._id.toString()) {
  902. let userInfo = { name: userData.real_name, mobile: userData.mobile, company: userData.company, email: userData.email };
  903. proj.userInfo = userInfo;
  904. }
  905. }
  906. }
  907. consProjIDs = Array.from(new Set(consProjIDs));
  908. let summaryInfo = await pm_facade.getSummaryInfo(consProjIDs);
  909. let tendersFeeInfo = await pm_facade.getTendersFeeInfo(ungroupedTenders);
  910. rst.summaryInfo = { grouped: summaryInfo, ungrouped: tendersFeeInfo };
  911. }
  912. callback(req, res, 0, 'success', rst);
  913. }
  914. catch (err) {
  915. console.log(err);
  916. callback(req, res, 1, err, null);
  917. }
  918. },
  919. getProjectsByQuery: async function (req, res) {
  920. try {
  921. let data = JSON.parse(req.body.data);
  922. let compilation = req.session.sessionCompilation._id;
  923. let query = data.query;
  924. query.compilation = compilation;
  925. let options = data.options;
  926. let projects = await projectModel.find(query, options);
  927. callback(req, res, 0, 'success', projects);
  928. }
  929. catch (err) {
  930. callback(req, res, 1, err, null);
  931. }
  932. },
  933. getSummaryInfo: async function (req, res) {
  934. try {
  935. let data = JSON.parse(req.body.data);
  936. let summaryInfo = await pm_facade.getSummaryInfo(data.projectIDs);
  937. callback(req, res, 0, 'success', summaryInfo);
  938. }
  939. catch (err) {
  940. callback(req, res, 1, err, null);
  941. }
  942. },
  943. changeFile: async function (req, res) {
  944. try {
  945. let data = JSON.parse(req.body.data);
  946. console.log(data);
  947. await pm_facade.changeFile(data.projects, data.user_id, data.fileID, data.name, data.from, data.type);
  948. callback(req, res, 0, 'success', []);
  949. }
  950. catch (err) {
  951. console.log(err);
  952. callback(req, res, 1, err, null);
  953. }
  954. },
  955. exportProject: async function (req, res) {
  956. let result = {
  957. error: 0
  958. };
  959. try {
  960. let data = { dataString: req.body.data, userID: req.session.sessionUser.id };
  961. result = await redirectToImportServer(data, "exportProject", req);
  962. } catch (err) {
  963. console.log(err);
  964. result.error = 1;
  965. result.message = err.message;
  966. }
  967. res.json(result);
  968. },
  969. importProject: async function (req, res) {
  970. let data = JSON.parse(req.body.data);
  971. let result = {
  972. error: 0
  973. };
  974. try {
  975. data.session = req.session;
  976. result = await redirectToImportServer(data,"importProject",req);
  977. } catch (err) {
  978. console.log(err);
  979. result.error = 1;
  980. result.message = err.message;
  981. }
  982. res.json(result);
  983. },
  984. copyConstructionProject: async function (req, res) {
  985. let data = JSON.parse(req.body.data);
  986. let result = {
  987. error: 0
  988. };
  989. try {
  990. data.session = req.session;
  991. result.data = await redirectToImportServer(data, "copyConstructionProject", req);
  992. } catch (err) {
  993. console.log(err);
  994. result.error = 1;
  995. result.message = err.message;
  996. }
  997. res.json(result);
  998. },
  999. importProcessChecking: async function (req, res) {
  1000. let result = {
  1001. error: 0
  1002. };
  1003. try {
  1004. let data = JSON.parse(req.body.data);
  1005. data.userID = req.session.sessionUser.id;
  1006. data.compilationID = req.session.sessionCompilation._id;
  1007. result.data = await pm_facade.importProcessChecking(data);
  1008. } catch (err) {
  1009. console.log(err);
  1010. result.error = 1;
  1011. result.message = err.message;
  1012. }
  1013. res.json(result);
  1014. },
  1015. getUploadToken: function (req, res) {
  1016. let result = {
  1017. error: 0
  1018. };
  1019. try {
  1020. result = pm_facade.uploadToken();
  1021. } catch (err) {
  1022. console.log(err);
  1023. result.error = 1;
  1024. result.message = err.message;
  1025. }
  1026. res.json(result);
  1027. },
  1028. getProjectByGranularity: async function (req, res) {
  1029. try {
  1030. const data = JSON.parse(req.body.data);
  1031. const userID = req.session.sessionUser.id;
  1032. const version = req.session.compilationVersion;
  1033. const projData = await pm_facade.getProjectByGranularity(data.tenderID, data.granularity, data.requestForSummaryInfo, userID, version);
  1034. callback(req, res, 0, 'success', projData);
  1035. } catch (err) {
  1036. callback(req, res, 1, err, null);
  1037. }
  1038. },
  1039. getImportTemplateData: async function (req, res) {
  1040. try {
  1041. const data = JSON.parse(req.body.data);
  1042. const templateData = await pm_facade.getImportTemplateData(req.session.sessionCompilation._id, data.valuationID, data.engineeringID, data.projectCount);
  1043. callback(req, res, 0, 'success', templateData);
  1044. } catch (err) {
  1045. callback(req, res, 1, err, null);
  1046. }
  1047. },
  1048. importInterface: async function (req, res) {
  1049. const data = JSON.parse(req.body.data);
  1050. let result = {
  1051. error: 0
  1052. };
  1053. try {
  1054. data.session = req.session;
  1055. result = await redirectToImportServer(data, "importInterface", req);
  1056. } catch (err) {
  1057. console.log(err);
  1058. result.error = 1;
  1059. result.message = err.message;
  1060. }
  1061. res.json(result);
  1062. },
  1063. };
  1064. async function redirectToImportServer(data, action, req) {
  1065. let importURL = config.getImportURL(process.env.NODE_ENV, req.headers.origin);
  1066. let options = {
  1067. method: 'POST',
  1068. uri: `http://${importURL}/import/${action}`,
  1069. body: data,
  1070. timeout: 220000,
  1071. json: true
  1072. };
  1073. console.log("post import data to:" + options.uri);
  1074. return await rp.post(options);
  1075. }