new_proj_controller.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import BillsTemplateModel from "../models/templates/bills_template_model";
  12. import EngineeringLibModel from "../../users/models/engineering_lib_model";
  13. module.exports = {
  14. copyTemplateData: async function (property, newProjID, callback) {
  15. //转换ID引用,原本@ID ID为原ID,需要替换为新的uuid
  16. function parseCalcBase(calcBase, uuidMapping) {
  17. let rst = '';
  18. let reg = /@\d+/g,
  19. numberData = Array.from(new Set(calcBase.match(reg))); //eg: @1
  20. let regForOpr = /[\+,\-,\*,\/]/g,
  21. oprData = calcBase.match(regForOpr); //eg: +
  22. let regForID = /\d+/g;
  23. let uuidArr = [];
  24. for (let data of numberData) {
  25. let orgID = data.match(regForID);
  26. if (orgID && orgID[0] && uuidMapping[orgID[0]]) {
  27. uuidArr.push(uuidMapping[orgID[0]]);
  28. }
  29. }
  30. for (let i = 0; i < uuidArr.length; i++) {
  31. let uid = uuidArr[i],
  32. opr = oprData[i - 1];
  33. if (opr) {
  34. rst += opr;
  35. }
  36. rst += `@${uid}`;
  37. }
  38. return rst;
  39. }
  40. async.parallel([
  41. async function (cb) {
  42. // 获取清单模板数据
  43. let billsTemplateModel = new BillsTemplateModel();
  44. let templateData = JSON.stringify(await billsTemplateModel.getTemplateDataForNewProj(property.templateLibID));
  45. let billsDatas = JSON.parse(templateData);
  46. let uuidMaping = Object.create(null);
  47. uuidMaping['-1'] = -1;
  48. //建立uuid-ID映射
  49. for(let bill of billsDatas){
  50. uuidMaping[bill.ID] = uuidV1();
  51. }
  52. let needParseReg = /@/g;
  53. billsDatas.forEach(function (template) {
  54. template.projectID = newProjID;
  55. template.ID = uuidMaping[template.ID] ? uuidMaping[template.ID] : -1;
  56. template.ParentID = uuidMaping[template.ParentID] ? uuidMaping[template.ParentID] : -1;
  57. template.NextSiblingID = uuidMaping[template.NextSiblingID] ? uuidMaping[template.NextSiblingID] : -1;
  58. //需要转换ID引用
  59. if (template.calcBase && needParseReg.test(template.calcBase)) {
  60. template.calcBase = parseCalcBase(template.calcBase, uuidMaping);
  61. }
  62. });
  63. billsData.insertData(billsDatas, callback);
  64. },
  65. async function (cb) {
  66. let engineeringModel = new EngineeringLibModel();
  67. let engineering = await engineeringModel.getEngineering(property.engineering_id);
  68. let mainTreeCol = await mainColLibModel.findOne({'ID':property.colLibID});
  69. projSetting.insertData({"projectID": newProjID, main_tree_col: mainTreeCol.main_tree_col,glj_col:engineering.glj_col}, cb);
  70. }
  71. ], (err) => callback(err));
  72. }
  73. };