new_proj_controller.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const billsModel = mongoose.model("bills");
  15. module.exports = {
  16. copyBillsTemplate: async (newProjID, billsDatas) => {
  17. let uuidMaping = Object.create(null);
  18. uuidMaping['-1'] = -1;
  19. //建立uuid-ID映射
  20. for(let bill of billsDatas){
  21. uuidMaping[bill.ID] = uuidV1();
  22. }
  23. const reg = /@\d+/;
  24. billsDatas.forEach(function (template) {
  25. template.projectID = newProjID;
  26. template.ID = uuidMaping[template.ID] ? uuidMaping[template.ID] : -1;
  27. template.ParentID = uuidMaping[template.ParentID] ? uuidMaping[template.ParentID] : -1;
  28. template.NextSiblingID = uuidMaping[template.NextSiblingID] ? uuidMaping[template.NextSiblingID] : -1;
  29. const needToParseCalcBase = template.calcBase && reg.test(template.calcBase);
  30. if (needToParseCalcBase) {
  31. template.calcBase = billsUtil.parseCalcBase(template.calcBase, uuidMaping);
  32. }
  33. });
  34. await billsModel.insertMany(billsDatas);
  35. },
  36. copyTemplateData: async function (property, newProjID, callback) {
  37. async.parallel([
  38. async function (cb) {
  39. // 获取清单模板数据
  40. let billsTemplateModel = new BillsTemplateModel();
  41. let templateData = JSON.stringify(await billsTemplateModel.getTemplateDataForNewProj(property.templateLibID));
  42. let billsDatas = JSON.parse(templateData);
  43. let uuidMaping = Object.create(null);
  44. uuidMaping['-1'] = -1;
  45. //建立uuid-ID映射
  46. for(let bill of billsDatas){
  47. uuidMaping[bill.ID] = uuidV1();
  48. }
  49. const reg = /@\d+/;
  50. billsDatas.forEach(function (template) {
  51. template.projectID = newProjID;
  52. template.ID = uuidMaping[template.ID] ? uuidMaping[template.ID] : -1;
  53. template.ParentID = uuidMaping[template.ParentID] ? uuidMaping[template.ParentID] : -1;
  54. template.NextSiblingID = uuidMaping[template.NextSiblingID] ? uuidMaping[template.NextSiblingID] : -1;
  55. const needToParseCalcBase = template.calcBase && reg.test(template.calcBase);
  56. if (needToParseCalcBase) {
  57. template.calcBase = billsUtil.parseCalcBase(template.calcBase, uuidMaping);
  58. }
  59. });
  60. billsData.insertData(billsDatas, callback);
  61. },
  62. async function (cb) {
  63. let engineeringModel = new EngineeringLibModel();
  64. let engineering = await engineeringModel.getEngineering(property.engineering_id);
  65. let mainTreeCol = await mainColLibModel.findOne({'ID':property.colLibID});
  66. projSetting.insertData({"projectID": newProjID, main_tree_col: mainTreeCol.main_tree_col,glj_col:engineering.glj_col}, cb);
  67. }
  68. ], (err) => callback(err));
  69. }
  70. };