| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- /**
- * Created by Mai on 2017/4/24.
- */
- let billsData = require('../../main/models/bills');
- let projCounter = require('../../main/models/proj_counter_model');
- let projSetting = require('../../main/models/proj_setting_model');
- let async = require('async');
- const uuidV1 = require('uuid/v1');
- const mongoose = require('mongoose');
- let mainColLibModel = mongoose.model('std_main_col_lib');
- import BillsTemplateModel from "../models/templates/bills_template_model";
- import EngineeringLibModel from "../../users/models/engineering_lib_model";
- module.exports = {
- copyTemplateData: async function (property, newProjID, callback) {
- //转换ID引用,原本@ID ID为原ID,需要替换为新的uuid
- function parseCalcBase(calcBase, uuidMapping) {
- let rst = '';
- let reg = /@\d+/g,
- numberData = Array.from(new Set(calcBase.match(reg))); //eg: @1
- let regForOpr = /[+,\-,*,/]/g,
- oprData = calcBase.match(regForOpr); //eg: +
- let regForID = /\d+/g;
- let uuidArr = [];
- for (let data of numberData) {
- let orgID = data.match(regForID);
- if (orgID && orgID[0] && uuidMapping[orgID[0]]) {
- uuidArr.push(uuidMapping[orgID[0]]);
- }
- }
- for (let i = 0; i < uuidArr.length; i++) {
- let uid = uuidArr[i],
- opr = oprData[i - 1];
- if (opr) {
- rst += opr;
- }
- rst += `@${uid}`;
- }
- return rst;
- }
- async.parallel([
- async function (cb) {
- // 获取清单模板数据
- let billsTemplateModel = new BillsTemplateModel();
- let templateData = JSON.stringify(await billsTemplateModel.getTemplateDataForNewProj(property.templateLibID));
- let billsDatas = JSON.parse(templateData);
- let uuidMaping = Object.create(null);
- uuidMaping['-1'] = -1;
- //建立uuid-ID映射
- for(let bill of billsDatas){
- uuidMaping[bill.ID] = uuidV1();
- }
- let needParseReg = /@/g;
- billsDatas.forEach(function (template) {
- template.projectID = newProjID;
- template.ID = uuidMaping[template.ID] ? uuidMaping[template.ID] : -1;
- template.ParentID = uuidMaping[template.ParentID] ? uuidMaping[template.ParentID] : -1;
- template.NextSiblingID = uuidMaping[template.NextSiblingID] ? uuidMaping[template.NextSiblingID] : -1;
- //需要转换ID引用
- if (template.calcBase && needParseReg.test(template.calcBase)) {
- template.calcBase = parseCalcBase(template.calcBase, uuidMaping);
- }
- });
- billsData.insertData(billsDatas, callback);
- },
- async function (cb) {
- let engineeringModel = new EngineeringLibModel();
- let engineering = await engineeringModel.getEngineering(property.engineering_id);
- let mainTreeCol = await mainColLibModel.findOne({'ID':property.colLibID});
- projSetting.insertData({"projectID": newProjID, main_tree_col: mainTreeCol.main_tree_col,glj_col:engineering.glj_col}, cb);
- }
- ], (err) => callback(err));
- }
- };
|