new_proj_controller.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 billsUtil = require('../../../public/billsUtil');
  12. const BillsTemplateModel = require("../models/templates/bills_template_model");
  13. const EngineeringLibModel = require("../../users/models/engineering_lib_model");
  14. module.exports = {
  15. copyTemplateData: async function (property, newProjID, callback) {
  16. async.parallel([
  17. async function (cb) {
  18. // 获取清单模板数据
  19. let billsTemplateModel = new BillsTemplateModel();
  20. let templateData = JSON.stringify(await billsTemplateModel.getTemplateDataForNewProj(property.templateLibID));
  21. let billsDatas = JSON.parse(templateData);
  22. let uuidMaping = Object.create(null);
  23. uuidMaping['-1'] = -1;
  24. //建立uuid-ID映射
  25. for(let bill of billsDatas){
  26. uuidMaping[bill.ID] = uuidV1();
  27. }
  28. const reg = /@\d+/;
  29. billsDatas.forEach(function (template) {
  30. template.projectID = newProjID;
  31. template.ID = uuidMaping[template.ID] ? uuidMaping[template.ID] : -1;
  32. template.ParentID = uuidMaping[template.ParentID] ? uuidMaping[template.ParentID] : -1;
  33. template.NextSiblingID = uuidMaping[template.NextSiblingID] ? uuidMaping[template.NextSiblingID] : -1;
  34. const needToParseCalcBase = template.calcBase && reg.test(template.calcBase);
  35. if (needToParseCalcBase) {
  36. template.calcBase = billsUtil.parseCalcBase(template.calcBase, uuidMaping);
  37. }
  38. });
  39. billsData.insertData(billsDatas, callback);
  40. },
  41. async function (cb) {
  42. let engineeringModel = new EngineeringLibModel();
  43. let engineering = await engineeringModel.getEngineering(property.engineering_id);
  44. let mainTreeCol = await mainColLibModel.findOne({'ID':property.colLibID});
  45. projSetting.insertData({"projectID": newProjID, main_tree_col: mainTreeCol.main_tree_col,glj_col:engineering.glj_col}, cb);
  46. }
  47. ], (err) => callback(err));
  48. }
  49. };