new_proj_controller.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Created by Mai on 2017/4/24.
  3. */
  4. let billsData = require('../../main/models/bills');
  5. let projCounter = require('../../main/models/proj_counter_model');
  6. let projSetting = require('../../main/models/proj_setting_model');
  7. let async = require('async');
  8. const uuidV1 = require('uuid/v1');
  9. const mongoose = require('mongoose');
  10. let mainColLibModel = mongoose.model('std_main_col_lib');
  11. const { ValuationType } = require('../../../public/common_constants');
  12. import BillsTemplateModel from "../models/templates/bills_template_model";
  13. import EngineeringLibModel from "../../users/models/engineering_lib_model";
  14. module.exports = {
  15. copyTemplateData: async function (property, newProjID, callback) {
  16. // 原ID引用更新成新ID引用
  17. function parseCalcBase(calcBase, uuidMapping) {
  18. const orgIDRefs = [...new Set(calcBase.match(/@\d+/g))];
  19. orgIDRefs.forEach(orgRef => {
  20. const orgID = orgRef.match(/\d+/)[0];
  21. const newID = uuidMapping[orgID] || null;
  22. // ID匹配不上则不转换这个引用
  23. if (!newID) {
  24. return;
  25. }
  26. const replaceStr = `@${newID}`;
  27. calcBase = calcBase.replace(new RegExp(`${orgRef}\\b`, 'g'), replaceStr);
  28. });
  29. return calcBase;
  30. }
  31. async.parallel([
  32. async function (cb) {
  33. // 获取清单模板数据
  34. let billsTemplateModel = new BillsTemplateModel();
  35. let templateData = JSON.stringify(await billsTemplateModel.getTemplateDataForNewProj(property.templateLibID));
  36. let billsDatas = JSON.parse(templateData);
  37. let uuidMaping = Object.create(null);
  38. uuidMaping['-1'] = -1;
  39. //建立uuid-ID映射
  40. for(let bill of billsDatas){
  41. uuidMaping[bill.ID] = uuidV1();
  42. }
  43. const reg = /@\d+/;
  44. billsDatas.forEach(function (template) {
  45. // 工程量清单单价分析默认勾选
  46. if (property.valuationType === ValuationType.BOQ) {
  47. template.unitPriceAnalysis = 1;
  48. }
  49. template.projectID = newProjID;
  50. template.ID = uuidMaping[template.ID] ? uuidMaping[template.ID] : -1;
  51. template.ParentID = uuidMaping[template.ParentID] ? uuidMaping[template.ParentID] : -1;
  52. template.NextSiblingID = uuidMaping[template.NextSiblingID] ? uuidMaping[template.NextSiblingID] : -1;
  53. const needToParseCalcBase = template.calcBase && reg.test(template.calcBase);
  54. if (needToParseCalcBase) {
  55. template.calcBase = parseCalcBase(template.calcBase, uuidMaping);
  56. }
  57. });
  58. billsData.insertData(billsDatas, callback);
  59. },
  60. async function (cb) {
  61. let engineeringModel = new EngineeringLibModel();
  62. let engineering = await engineeringModel.getEngineering(property.engineering_id);
  63. let mainTreeCol = await mainColLibModel.findOne({'ID':property.colLibID});
  64. projSetting.insertData({"projectID": newProjID, main_tree_col: mainTreeCol.main_tree_col,glj_col:engineering.glj_col}, cb);
  65. }
  66. ], (err) => callback(err));
  67. }
  68. };