/**
     * Created by zhang on 2018/4/17.
     */
    const projectType = {
        folder: 'Folder',
        tender: 'Tender',
        project: 'Project',
        engineering: 'Engineering',
    };
//先导出后require可以解决循环引用问题
module.exports={
    prepareShareList,
    getShareList,
    addShareList,
    updateShareList,
    deleteShareList,
    getShareInfoMap,
    getRecentShareList,
    getProjectShareList,
    getUnreadShareListByCompilation,
    getShareTip,
    getShareState,
    markShareItemsRead,
    getShareInfoAfterChangePermission,
    moveProject:moveProject,
    accessToCopyProject,
    copyProject:copyProject,
    copyExample: copyExample,
    setupSummaryFields: setupSummaryFields,
    getSummaryInfo: getSummaryInfo,
    getSummaryInfoByTender: getSummaryInfoByTender,
    getTendersFeeInfo: getTendersFeeInfo,
    getConstructionProject: getConstructionProject,
    getFullPath: getFullPath,
    getProjectFeature:getProjectFeature,
    getProgressiveInterval: getProgressiveInterval,
    projectType: projectType,
    getPosterityProjects: getPosterityProjects,
    isShare: isShare,
    isFirst: isFirst,
    getShareInfo: getShareInfo,
    prepareInitialData: prepareInitialData,
    changeFile:changeFile,
    checkFiles:checkFiles,
    copyForSectionError: copyForSectionError,
    exportProject:exportProject,
    importProject:importProject,
    uploadToken:uploadToken,
    downLoadProjectFile:downLoadProjectFile,
    importProcessChecking:importProcessChecking,
    isTenderOverrun,
    getWelcomeInfo:getWelcomeInfo
};



let mongoose = require('mongoose');
let _ = require("lodash");
let feeRate_facade = require('../../fee_rates/facade/fee_rates_facade');
let glj_facade = require('../../glj/facade/glj_facade');
let project_facade = require('../../main/facade/project_facade');
let logger = require("../../../logs/log_helper").logger;
const uuidV1 = require('uuid/v1');
let projectModel = mongoose.model('projects');
let projectSettingModel =  mongoose.model('proj_setting');
let billsModel = mongoose.model('bills');
let rationModel = mongoose.model('ration');
let gljListModel = mongoose.model("glj_list");
let calcProgramsModel = mongoose.model('calc_programs');
let labourCoesModel = mongoose.model('labour_coes');
let feeRateModel = mongoose.model('fee_rates');
let feeRateFileModel = mongoose.model('fee_rate_file');
let unitPriceFileModel = mongoose.model("unit_price_file");
let mixRatioModel = mongoose.model("mix_ratio");
let freightModel = mongoose.model("freight_calc");
let originalModel = mongoose.model("original_calc");
let comElectrovalenceModel = mongoose.model("com_electrovalence");
let unitPriceModel = mongoose.model("unit_price");
let installationFeeModel = mongoose.model("installation_fee");
let rationGLJModel = mongoose.model('ration_glj');
let rationCoeModel = mongoose.model('ration_coe');
let rationInstallationModel = mongoose.model('ration_installation');
let quantityDetailModel = mongoose.model('quantity_detail');
let rationTemplateModel = mongoose.model('ration_template');
let userModel = mongoose.model('user');
let compleGljSectionModel = mongoose.model('complementary_glj_section');
let compleGljSectionTModel = mongoose.model('complementary_glj_section_templates');
let progressiveModel = mongoose.model('std_progressive_lib');
let importLogsModel = mongoose.model("import_logs");
const shareListModel = mongoose.model('share_list');
let welcomeModel = mongoose.model("welcome_setting");


let featureLibModel =  mongoose.model("std_project_feature_lib");
let scMathUtil = require('../../../public/scMathUtil').getUtil();
let counter = require('../../../public/counter/counter');
import SectionTreeDao from '../../complementary_ration_lib/models/sectionTreeModel';
let sectionTreeDao = new SectionTreeDao();
import CounterModel from "../../glj/models/counter_model";
import moment from 'moment-timezone';
const { fixedFlag, SharePermissionChangeType } = require('../../../public/common_constants');
const notDeleted = [{deleteInfo: null}, {'deleteInfo.deleted': false}];
let cipher = require('../../../public/cipher');
const compilationModel = mongoose.model('compilation');
let qiniu = require("qiniu");
let fs = require("fs");
let path = require("path");
let request = require("request");
const systemSettingModel = mongoose.model('system_setting');

let qiniu_config = {
    "AccessKey": "_gR1ed4vi1vT2G2YITGSf4_H0fJu_nRS9Tzk3T4z",
    "SecretKey": "ty4zd0FHqgEDaiVzSLC8DfHlai99aS7bspLkw6s6",
    "Bucket": "yun-update",
    "Domain": "http://serverupdate.smartcost.com.cn"
};
let mac = new qiniu.auth.digest.Mac(qiniu_config.AccessKey, qiniu_config.SecretKey);

// 重构了分享的数据结构，原本存在projects表的shareInfo数组中
// 为了更好地适应新功能及以后拓展功能，现将分享信息存放在单独的share_list表中
// 为了适应旧数据，需要在share_list没有数据的时候，将原本的分享信息复制到share_list中，同时生成联系人
// TODO: 上线后这个方法执行过后，应该删除此方法
async function prepareShareList() {
    const shareListCount = await shareListModel.count({});
    console.log(shareListCount);
    if (shareListCount) {
        return;
    }
    logger.info('================================enterPrepareShareList================================================');
    // 将项目的shareInfo数据复制到share_list表中
    const projects = await projectModel.find({'shareInfo.0': {$exists: true}}, '-_id ID userID shareInfo').lean();
    if (!projects.length) {
        return;
    }
    const shareList = [];
    const userMap = {}; // 给生成联系人做准备
    projects.forEach(project => {
        const ownerContacts = userMap[project.userID] || (userMap[project.userID] = []);
        project.shareInfo.forEach(shareItem => {
            if (!shareItem.shareDate) {
                return;
            }
            const receiverContacts = userMap[shareItem.userID] || (userMap[shareItem.userID] = []);
            receiverContacts.push(project.userID);
            const newItem = {
                ID: uuidV1(),
                projectID: project.ID,
                owner: project.userID,
                receiver: shareItem.userID,
                allowCopy: shareItem.allowCopy,
                allowCooperate: shareItem.allowCooperate,
                shareDate: shareItem.shareDate,
                updateDate: shareItem.shareDate
            };
            ownerContacts.push(shareItem.userID);
            shareList.push(newItem);
        });
    });
    // 插入分享信息
    const shareListPromise = shareListModel.insertMany(shareList);
    const tasks = [shareListPromise];
    // 更新用户信息
    const userTasks = [];
    Object.entries(userMap).forEach(([userID, contacts]) => {
        contacts = [...new Set(contacts)]
            .map(user => ({ userID: user }));
        const task = {
            updateOne: {
                filter: {
                    _id: mongoose.Types.ObjectId(userID)
                },
                update: {
                    contacts
                }
            }
        };
        userTasks.push(task);
    });
    if (userTasks.length) {
        tasks.push(userModel.bulkWrite(userTasks));
    }
    await Promise.all(tasks);
}


// 获取分享列表
async function getShareList(query) {
    return await shareListModel.find(query, '-_id').lean();
}

async function addShareList(docs) {
    await shareListModel.insertMany(docs);
}

async function deleteShareList(query) {
    await shareListModel.deleteMany(query);
}

async function updateShareList(updateData) {
    const bulks = updateData.map(item => (
        {
            updateOne: {
                filter: item.query,
                update: item.update
            }
        }
    ));
    if (bulks.length) {
        await shareListModel.bulkWrite(bulks);
    }
}

// 根据项目ID获取项目shareInfo字段的数据映射（前端旧逻辑依赖项目本身的shareInfo字段）
async function getShareInfoMap(projectIDs, shareList = null) {
    if (!shareList) {
        shareList = await getShareList({projectID: {$in: projectIDs}});
    }
    // 将相同项目的分享信息进行映射
    const map = {};
    shareList.forEach(item => {
        const shareInfo = map[item.projectID] || (map[item.projectID] = []);
        shareInfo.push({
            userID: item.receiver,
            shareDate: item.shareDate,
            updateDate: item.updateDate,
            allowCopy: item.allowCopy,
            allowCooperate: item.allowCooperate
        });
    });
    return map;
}

// 获取最近分享(只算分享出去的记录)
async function getRecentShareList(userID, count) {
    const shareList = await getShareList({owner: userID});
    shareList.sort((a, b) => Date.parse(b.shareDate) - Date.parse(a.shareDate));
    const set = new Set();
    for(const item of shareList) {
        if (set.size === count) {
            break;
        }
        set.add(item.receiver);
    }
    const sortedIDList = [...set];
    const userIDList = sortedIDList.map(userID => mongoose.Types.ObjectId(userID));
    const users = await userModel.find({_id: {$in: userIDList}}, 'real_name mobile company').lean();
    users.sort((a, b) => sortedIDList.indexOf(a._id.toString()) - sortedIDList.indexOf(b._id.toString()));
    return users;
}

// 获取某项目的分享记录
async function getProjectShareList(projectID, limit = null) {
    const sharedList = limit 
        ? await shareListModel.find({ projectID }).lean().sort({ shareDate: -1 }).limit(limit)
        : await shareListModel.find({ projectID }).lean().sort({ shareDate: -1 });
    const userIDs = [];
    const userMap = {};
    sharedList.forEach((item, index) => {
        userIDs.push(item.receiver);
        userMap[item.receiver] = item;
        userMap[item.receiver].index = index;
    });
    const userObjectIDs = userIDs.map(userID => mongoose.Types.ObjectId(userID));
    const users = await userModel.find({_id: {$in: userObjectIDs}}, 'real_name mobile company').lean();
    users.forEach(user => {
        const matched = userMap[user._id];
        if (matched) {
            user.index = matched.index;
            user.allowCopy = matched.allowCopy;
            user.allowCooperate = matched.allowCooperate;
        }
    });
    users.sort((a, b) => a.index - b.index);
    return users;
}

// 获取某用户某费用定额的未读的被分享记录
async function getUnreadShareListByCompilation(userID, compilationID, isGetCount) {
    const fields = isGetCount ? '-_id projectID ' : '-_id';
    const list = await shareListModel.find({ receiver: userID, isRead: false }, fields);
    const idList = list.reduce((acc, cur) => {
        acc.push(cur.projectID);
        return acc;
    }, []);
    const notDeleted = [{ deleteInfo: null }, { 'deleteInfo.deleted': false }];
    const query = { ID: { $in: idList }, compilation: compilationID, $or: notDeleted };
    if (isGetCount) {
        return await projectModel.count(query)
    }
    const items = await projectModel.find(query, '-_id ID').lean();
    return items.map(item => item.ID);
}

// 获取分享的提示（造价书分享按钮tooltip使用）
async function getShareTip(projectID, limit) {
    const task = [
        getProjectShareList(projectID, limit),
        shareListModel.count({ projectID })
    ];
    const [users, count] = await Promise.all(task);
    return users.reduce((acc, user, index) => {
        if (index === 0) {
            acc += '已分享给';
            acc += user.real_name;
        } else {
            acc += ` ${user.real_name}`;
        }
        if (index === users.length - 1 && count > limit) {
            acc += `等${count}人`;
        }
        return acc;
    }, '');
}

// 获取项目的分享状态
// 分享项目的拷贝和编辑性，需要参考父项
// 以项目链上最新更新的分享数据为准
async function getShareState(projectID, receiver) {
    const projectIDs = await getUpChainIDs(projectID);
    const shareList = await shareListModel.find({ projectID: { $in: projectIDs }, receiver }).lean();
    shareList.sort((a, b) => Date.parse(b.updateDate) - Date.parse(a.updateDate));
    return {
        allowCopy: shareList[0] && shareList[0].allowCopy || false,
        allowCooperate: shareList[0] && shareList[0].allowCooperate || false
    };
}

// 自身及父链上的分享数据（链距离最近的一个）
async function getUpChainShareData(projectID, receiver) {
    while (projectID != -1) {
        const shareData = await shareListModel.findOne({ projectID, receiver }).lean();
        if (shareData) {
            return shareData;
        }
        const project = await projectModel.findOne({ ID: projectID }, '-_id ParentID').lean();
        projectID = project ? project.ParentID : -1;
    }
    return null;
}

// 标记已读分享条目
// 打开任意的单位工程，都会将其父分享条目标记和自身为已读 
// eg:被分享了一个建设项目，和其下一个单位工程，打开该一个单位工程，建设项目标记为已读、该单位工程也标记为已读
async function markShareItemsRead(projectID, userID) {
    const upChainIDs = await getUpChainIDs(projectID);
    const unreadList = await shareListModel.find({ projectID: { $in: upChainIDs }, receiver: userID, isRead: false }, '-_id ID projectID').lean();
    const markReadProjectIDs = [];
    const bulks = [];
    unreadList.forEach(item => {
        markReadProjectIDs.push(item.projectID);
        bulks.push({
            updateOne: {
                filter: { ID: item.ID },
                update: { isRead: true }
            }
        });
    });
    if (bulks.length) {
        await shareListModel.bulkWrite(bulks);
    }
    return markReadProjectIDs;
}

/**
 * 分享权限变更后，根据变更操作，获取相关项目的分享权限信息
 * 不能简单根据某个项目的分享信息获得其权限，因为目前的分享可以重复分享（分享单位工程A、分享包含单位工程A的父项） 
 * 最新的分享权限是由项目链上(所有子项、自身、父项...)最新的分享信息决定的
 * @param {Number} permissionType - 分享权限变更类型
 * @param {String} receiver - 用户ID
 * @param {Number} projectID - 项目ID
 */
async function getShareInfoAfterChangePermission(permissionType, receiver, projectID) {
    const project = await projectModel.findOne({ ID: projectID }, '-_id ID projType').lean();
    if (!project) {
        return [];
    }
    // UPDATE_COPY不触发推送给主页面的消息
    const propChange = [SharePermissionChangeType.UPDATE_COOPERATE].includes(permissionType);
    const cancelShare = permissionType === SharePermissionChangeType.CANCEL;
    // 需要处理的单位工程
    if (propChange && project.projType === projectType.tender) {
        const shareData = await shareListModel.findOne({ projectID, receiver }, '-_id allowCooperate allowCopy updateDate').lean();
        const tender = { ID: projectID };
        if (shareData) {
            Object.assign(tender, shareData);
        }
        return [tender];
    }
    if (propChange && project.projType !== projectType.tender) {
        const projects = await getPosterityProjects([projectID], false, '-_id ID projType');
        const tenders = projects.filter(p => p.projType === projectType.tender);
        const shareData = await shareListModel.findOne({ projectID, receiver }, '-_id allowCooperate allowCopy updateDate').lean();
        if (shareData) {
            tenders.forEach(tender => Object.assign(tender, shareData));
        }
        return tenders
    }
    if (cancelShare && project.projType === projectType.tender) {
        const shareData = await getUpChainShareData(projectID, receiver);
        return shareData ? [] : [{ ID: projectID }];
    }
    if (cancelShare && project.projType !== projectType.tender) {
        const projects = await getPosterityProjects([projectID], false, '-_id ID projType');
        const tenders = [];
        for (const p of projects) {
            if (p.projType === projectType.tender) {
                const shareData = await getUpChainShareData(p.ID, receiver);
                if (!shareData) {
                    tenders.push({ ID: p.ID });
                }
            }
        }
        return tenders;
    }
    return [];
}

//拷贝例题项目
//@param {String}userID {Array}projIDs拷贝的例题项目ID（建设项目、文件夹）@return {Boolean}
async function copyExample(userID, compilation, projIDs){
    let allProjs = [],
        IDMapping = {},
        projMapping = {};
    //例题项目不可为单项工程、单位工程、只能是建设项目、文件夹，否则不自动新建例题项目（这里不报错，让用户没有感觉）
    let parentExample = await projectModel.find({ID: {$in: projIDs}, $or: notDeleted}).lean();
    if (parentExample.length === 0) {
        return false;
    }
    allProjs = allProjs.concat(parentExample);
    for (let i = 0; i < parentExample.length; i++) {
        let data = parentExample[i];
        if (data.projType === projectType.tender || data.projType === projectType.engineering) {
            return false;
        }
        //设置成顶节点，最后一个节点设置成末节
        data.ParentID = -1;
        if (i === parentExample.length - 1) {
            data.NextSiblingID = -1;
        }
    }
    //获取所有的子项目
    let posterityProjs = await getPosterityProjects(projIDs);
    allProjs = allProjs.concat(posterityProjs);
    let projCounter = await counter.counterDAO.getIDAfterCountSync(counter.moduleName.project, allProjs.length);
    //旧ID与新ID映射
    for (let i = 0; i < allProjs.length; i++) {
        let data = allProjs[i];
        let newID = projCounter.sequence_value - (allProjs.length - 1) + i;
        IDMapping[data.ID] = newID;
        projMapping[newID] = data;
    }
    //return;
    //设置新的树结构数据
    let newDate = new Date(),
        parentBulks = [];
    const projectMaps = [];
    for (let data of allProjs) {
        let orgID = data.ID;
        data.ID = IDMapping[data.ID];
        data.ParentID = IDMapping[data.ParentID] ? IDMapping[data.ParentID] : -1;
        data.NextSiblingID = IDMapping[data.NextSiblingID] ? IDMapping[data.NextSiblingID] : -1;
        data.createDateTime = newDate;
        data.userID = userID;
        data.compilation = compilation;
        data.shareInfo = [];
        if (data.projType !== projectType.tender) {
            let newData = _.cloneDeep(data);
            delete newData._id;
          //  await projectModel.create(newData);
            parentBulks.push({insertOne: {document: newData}});
        } else {
            //拷贝单位工程
            let rootProjectID = projMapping[data.ParentID].ID;
            let projectMap = {
                copy: {
                    document: {userID: userID, ID: orgID, NextSiblingID: data.NextSiblingID, ParentID: data.ParentID, name: data.name, shareInfo: [],
                                compilation: compilation, fileVer: data.fileVer, projType: data.projType, property: {rootProjectID: rootProjectID}}
                }
            };
            await copyProject(userID, compilation, {projectMap}, data.ID, true);
            projectMaps.push(projectMap);
        }
    }
    //最末顶层项目（兼容测试时已存在有项目，正常用户第一次进费用定额，该费用定额不存在项目）
    let lastProj = await projectModel.findOne({userID: userID, compilation: compilation, ParentID: -1, NextSiblingID: -1, $or: notDeleted});
    if (lastProj) {
        parentBulks.push({updateOne: {filter: {ID: lastProj.ID}, update: {$set: {NextSiblingID: parentExample[0].ID}}}});
    }
    // 处理项目数据
    const projectTasks = [projectModel.bulkWrite(parentBulks), createProject(projectMaps)];
    await Promise.all(projectTasks);
    return true;
}

async function accessToCopyProject(userID, compilationID, data, newProjectID) {
    const logData = {
        key: uuidV1(),
        content: '正在拷贝项目，请稍候……',
        userID,
        compilationID,
        status: 'start',
        create_time: +new Date()
    };
    await importLogsModel.create(logData);
    handleCopyProject(logData.key, userID, compilationID, data, newProjectID, false);
    return 'copying project';
}

async function handleCopyProject(key, userID, compilationID, data, newProjectID) {
    const doc = { status: 'finish' };
    try {
        const projectMap = await copyProject(userID, compilationID, data, newProjectID, false);
        doc.projectID = projectMap.copy.document.ID;
    } catch (err) {
        doc.status = 'error';
        doc.errorMsg = String(err);
    } finally {
        await importLogsModel.update({ key }, doc);
    }
}

async function copyProject(userID, compilationID, data, newProjectID = null, delayProjectTask = false) {
    let projectMap = data.projectMap;
    const isSaveAs =  isDef(projectMap.isSaveAs) ? projectMap.isSaveAs : false; // 只有另存为的行为为true，这里需要区分是因为，另存为中另存费率单价文件时，若有重名需要直接报错提示，而不是自动加（xxx复制）的后缀
    const saveAsFile = isDef(projectMap.saveAsFile) ? projectMap.saveAsFile : true; // 是否另存费率、单价文件，默认是另存的。
    const projectParentID = projectMap['copy'].document.ParentID;
    const projectName = projectMap['copy'].document.name;
    if (isSaveAs) {
        const isExist = await isProjectNameExist(projectParentID, projectName);
        if (isExist) {
            throw `分段文件[${projectName}]已存在。`;
        }
    }
    let originalID = projectMap['copy'].document.ID;
    if (!newProjectID) {
        newProjectID = await getCounterID("projects");
    }
    let calcProgramFileID = uuidV1();//新的计算程序文件ID
    let labourCoeFileID = uuidV1();//新的人工调整系数文件ID
    let originalProject = await projectModel.findOne({'ID':originalID});//查找最新的那项目信息，前端缓存的数据有可能不是最新的
    if(!originalProject){
        throw new Error('没有找到对应的项目，复制失败!');
    }
    let property = originalProject.property;
    if(projectMap['copy'].document.property){//更新项目属性信息
        setProperty(property,projectMap['copy'].document.property);
    }
    let originalProperty = _.cloneDeep(property);
    projectMap['copy'].document.property = property;
    logger.info("复制项目： 旧项目ID: "+originalID+ " ------- 新项目ID: "+newProjectID);
    //更新项目的属性;
    projectMap['copy'].document.ID = newProjectID;
    if(projectMap['copy'].document.property.calcProgramFile){
        projectMap['copy'].document.property.calcProgramFile.ID = calcProgramFileID;
    }
    if(projectMap['copy'].document.property.labourCoeFile){
        projectMap['copy'].document.property.labourCoeFile.ID = labourCoeFileID;
    }
    projectMap['copy'].document.userID = userID;
    projectMap['copy'].document.compilation = compilationID;
    projectMap['copy'].document.createDateTime = new Date();
    //清单、定额ID生成任务
    let IDtasks = [
        createIDsAndReturn(originalID,billsModel),
        createIDsAndReturn(originalID,rationModel),
        getProjectGLJIDAndReturn(originalID,newProjectID)
    ];
    let [billMap,rationMap,projectGLJMap] = await Promise.all(IDtasks);
    //所有复制任务异步处理，提升效率  复制清单，定额，4个文件，项目工料机， 定额工料机，人工系数，工程量明细，定额安装增加费，安装增加费
    let copyTasks = [
        copyProjectSetting(originalID,newProjectID),
        copyBills(newProjectID,billMap),
        copyRations(newProjectID,billMap.uuidMaping,rationMap,projectGLJMap.IDMap),
        copyProjectGLJ(projectGLJMap.datas),
        copyInstallFee(originalID,newProjectID),
        copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,rationGLJModel),
        copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,rationCoeModel),
        copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,quantityDetailModel),
        copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,rationInstallationModel),
        copyRationSubList(originalID,newProjectID,billMap.uuidMaping,rationMap.uuidMaping,projectGLJMap.IDMap,rationTemplateModel)
    ];
    if(originalProperty.calcProgramFile){
        copyTasks.push(commonCopy(newProjectID,originalProperty.calcProgramFile.ID,calcProgramFileID,calcProgramsModel));
    }
    if(originalProperty.labourCoeFile){
        copyTasks.push(commonCopy(newProjectID,originalProperty.labourCoeFile.ID,labourCoeFileID,labourCoesModel));
    }
    // 处理单价、费率文件
    if (saveAsFile) {
        let feeRateFileName = isSaveAs ? projectName : originalProperty.feeFile.name;
        let unitPriceFileName = isSaveAs ? projectName : originalProperty.unitPriceFile.name;
        let feeRateFileID = uuidV1();//新的费率文件ID
        let unitPriceFileID = await getCounterID("unit_price_file");//新的单价文件ID
        //费率文件、单价文件重名检查
        let feeRate = await feeRateFileModel.findOne({ rootProjectID: originalProperty.rootProjectID, name: feeRateFileName, deleteInfo: null });
        if (feeRate) {//存在重名的文件
            feeRateFileName = feeRateFileName + '（' + moment(Date.now()).tz("Asia/Shanghai").format('MM-DD HH:mm:ss') + '复制）';
        }
        projectMap['copy'].document.property.feeFile.name = feeRateFileName;
        let unitPriceFile = await unitPriceFileModel.findOne({ root_project_id: originalProperty.rootProjectID, name: unitPriceFileName, deleteInfo: null });
        if (unitPriceFile) {//存在重名的文件
            unitPriceFileName = unitPriceFileName + '（' + moment(Date.now()).tz("Asia/Shanghai").format('MM-DD HH:mm:ss') + '复制）';
        }
        projectMap['copy'].document.property.unitPriceFile.name = unitPriceFileName;
        if(projectMap['copy'].document.property.feeFile){
            projectMap['copy'].document.property.feeFile.id = feeRateFileID;
        }
        if(projectMap['copy'].document.property.unitPriceFile){
            projectMap['copy'].document.property.unitPriceFile.id = unitPriceFileID;
        }
        if(originalProperty.feeFile){
            copyTasks.push(copyFeeRate(originalProperty.rootProjectID,userID,originalProperty.feeFile.id,feeRateFileID,feeRateFileName));
        }
        if(originalProperty.unitPriceFile){
            copyTasks.push(copyUnitPriceFile(newProjectID,originalProperty.rootProjectID,userID,originalProperty.unitPriceFile.id,unitPriceFileID,unitPriceFileName));
        }
    }
    await Promise.all(copyTasks);
    // 最后再处理项目数据
    // 可能会有最后再在外层方法插入项目数据的需求，如拷贝例题，暂时先不在这里插入项目数据
    if (delayProjectTask) {
        return [projectMap];
    } else {
        const projectMaps = await createProject([projectMap]);
        return projectMaps[0];
    }
}

// 判断项目名称是否存在
async function isProjectNameExist(parentID, name) {
    const notDeleted = [{deleteInfo: null}, {'deleteInfo.deleted': false}];
    const count = await projectModel.count({ ParentID: parentID, name, $or: notDeleted });
    return !!count;
}

async function createIDsAndReturn(originalID,model) {
    let uuidMaping = {};//做ID映射
    let datas = [];
    let result = await model.find({"projectID": originalID}, '-_id');
    uuidMaping['-1'] = -1;
    //建立uuid-ID映射
    for(let d of result){
        uuidMaping[d.ID] = uuidV1();
        datas.push(d._doc);
    }
    return{uuidMaping:uuidMaping,datas:datas};
}

async function getProjectGLJIDAndReturn(originalID,newProjectID) {
    let IDMap = {};
    let datas = [];
    let result = await gljListModel.find({project_id:originalID}, '-_id');
    let gljCount = await counter.counterDAO.getIDAfterCountSync(counter.moduleName.glj_list, result.length);
    for(let i = 0; i < result.length; i++){
        let d = result[i];
        let newID = gljCount.sequence_value - (result.length - 1) + i;
        //let newID = await getCounterID("glj_list");
        IDMap[d.id] = newID;
        d._doc.project_id = newProjectID;
        d._doc.id = newID;
        datas.push(d._doc);
    }
    return{IDMap:IDMap,datas:datas};
}

async function createProject(projectMaps) {//复制项目
    let tasks = [];
    for (const projectMap of projectMaps) {
        if(projectMap['update']){//如果复制后存在前一个节点，则更新其NextSiblingID
            tasks.push({updateOne:{filter : projectMap['update'].query, update : {NextSiblingID:projectMap['copy'].document.ID}}});
        }
        tasks.push({insertOne: {document: projectMap['copy'].document}});//复制任务
    }
    await projectModel.bulkWrite(tasks);
    return projectMaps;
}

async function copyProjectSetting(originalID,newProjectID) {
    let result = null;
    let setting = await projectSettingModel.findOne({"projectID": originalID}, '-_id');
    if(setting){
        setting._doc.projectID =newProjectID;
        result = await projectSettingModel.create(setting._doc);
    }
    return result;
}

async function copyBills(newProjectID,billMap) {
     let uuidMaping = billMap.uuidMaping;//做ID映射
     for(let doc of billMap.datas){
         doc = getCopyBillDatas(doc,newProjectID,uuidMaping)
     }
    await insertMany(billMap.datas,billsModel);
    return billMap.datas;
}


function getCopyBillDatas(doc,newProjectID,uuidMaping) {
    doc.projectID = newProjectID;
    doc.ID = uuidMaping[doc.ID] ? uuidMaping[doc.ID] : -1;
    doc.ParentID = uuidMaping[doc.ParentID] ? uuidMaping[doc.ParentID] : -1;
    doc.NextSiblingID = uuidMaping[doc.NextSiblingID] ? uuidMaping[doc.NextSiblingID] : -1;
    //对于有基数计算的节点，需要替换计算基数中引用的ID为新的ID
    if(doc.calcBase && doc.calcBase != ""){
        let idArr = scMathUtil.getFIDArr(doc.calcBase);
        for(let re of idArr){
            let oID = re.substr(1);//去掉开头的@字符
            if(!uuidMaping[oID]) continue;
            doc.calcBase = doc.calcBase.replace(new RegExp(oID, "g"),uuidMaping[oID]);
        }
    }
    return doc;
}

async function copyRations(newProjectID,billsIDMap,rationMap,projectGLJIDMap) {
    let uuidMaping = rationMap.uuidMaping;
    for(let doc of rationMap.datas){
        doc = getCopyRationData(doc,newProjectID,billsIDMap,uuidMaping,projectGLJIDMap);
    }
    if(rationMap.datas.length > 0){
        await insertMany(rationMap.datas,rationModel);
    }
    return rationMap.datas;
}


function getCopyRationData(doc,newProjectID,billsIDMap,uuidMaping,projectGLJIDMap){
    doc.projectID = newProjectID;
    doc.ID = uuidMaping[doc.ID] ? uuidMaping[doc.ID] : -1;
    if(doc.billsItemID){
        doc.billsItemID = billsIDMap[doc.billsItemID]?billsIDMap[doc.billsItemID]:-1;
    }
    if(doc.referenceRationID) doc.referenceRationID = uuidMaping[doc.referenceRationID]?uuidMaping[doc.referenceRationID]:undefined;
    //绑定定类型的工料机 项目工料机ID
    doc.type==3&&doc.projectGLJID&&projectGLJIDMap[doc.projectGLJID]?doc.projectGLJID = projectGLJIDMap[doc.projectGLJID]:'';
    return doc;
}


async function copyProjectGLJ(gljList) {
    await insertMany(gljList,gljListModel);
}

async  function commonCopy(newProjectID,oldID,newID,model) { //对于只需更新ID和projectID的文件的复制
    let result = null;
    let file = await model.findOne({"ID": oldID}, '-_id');
    if(file){
        file._doc.ID = newID;
        file._doc.projectID =newProjectID;
        result = await model.create(file._doc);
    }
    return result;
}

async function copyFeeRate(rootProjectID,userID,originalFeeRateFileID,feeRateFileID,newName) {//复制费率和费率文件
    let [feeRateFile,feeRate] =await feeRate_facade.getFeeRateByID(originalFeeRateFileID);
    let newFeeRateID = uuidV1();
    if(feeRate){
        feeRate._doc.ID = newFeeRateID;
        await feeRateModel.create(feeRate._doc);
    }
    if(feeRateFile){
        feeRateFile._doc.ID = feeRateFileID;
        newName?feeRateFile._doc.name = newName:'';
        feeRateFile._doc.userID = userID;
        feeRateFile._doc.rootProjectID = rootProjectID;
        feeRateFile._doc.feeRateID = newFeeRateID;
        await feeRateFileModel.create(feeRateFile._doc);
    }
}

async function copyUnitPriceFile(newProjectID,rootProjectID,userID,originaluUnitPriceFileID,unitPriceFileID,newName) {//复制单价文件、组成物

    let taskList = [
        copyFile(newProjectID,rootProjectID,userID,originaluUnitPriceFileID,unitPriceFileID,newName),
        copySubList(originaluUnitPriceFileID,unitPriceFileID,mixRatioModel),
        copySubList(originaluUnitPriceFileID,unitPriceFileID,freightModel,true),
        copySubList(originaluUnitPriceFileID,unitPriceFileID,originalModel,true),
        copySubList(originaluUnitPriceFileID,unitPriceFileID,comElectrovalenceModel,true),
        copySubList(originaluUnitPriceFileID,unitPriceFileID,unitPriceModel)
    ];

    return await Promise.all(taskList);

    async function copyFile(newProjectID,rootProjectID,userID,originaluUnitPriceFileID,unitPriceFileID,newName){
        let unitPriceFile = await unitPriceFileModel.findOne({id:originaluUnitPriceFileID}, '-_id');
        if(unitPriceFile){
            unitPriceFile._doc.id = unitPriceFileID;
            newName?unitPriceFile._doc.name = newName:'';
            unitPriceFile._doc.project_id = newProjectID;
            unitPriceFile._doc.user_id = userID;
            unitPriceFile._doc.root_project_id = rootProjectID
        }
        await unitPriceFileModel.create(unitPriceFile._doc);
    }

    async function copySubList(srcFID,newFID,model,UUID=false) {
        let mList = await model.find({unit_price_file_id: srcFID}, '-_id').lean();
        let rList = [];
        if(UUID == true){
          for(let m of mList){
            m.unit_price_file_id = newFID;
            m.ID = uuidV1();
            //自采自办的情况，项目工料机要跟着改变：
            if(m.ration_gljs){
              for(let rg of m.ration_gljs){
                rg.ID = uuidV1();
              }
            }
            //运费和原价定额ID不能更新，不然定额工料机找不到对应的定额
            rList.push(m);
          }
        }else{
          let IDcounter = await counter.counterDAO.getIDAfterCountSync(model.modelName, mList.length);
          let firstID = IDcounter.sequence_value - (mList.length - 1);
          for(let m of mList){
              m.unit_price_file_id = newFID;
              m.id = firstID;    
              firstID+=1
              rList.push(m);
          }
        }    
        await insertMany(rList,model)
    }
}

async function copyInstallFee(originalPID,newProjectID) {
    let result = null;
    let installationFee = await installationFeeModel.find({projectID:originalPID}, '-_id');
    let newList = [];
    for(let i of installationFee ){
        i._doc.ID = uuidV1();
        i._doc.projectID = newProjectID;
        newList.push(i._doc);
    }
    if(newList.length >0){
        result =  await installationFeeModel.insertMany(newList);
    }
    return result;
}

async function copyRationSubList(originalPID,newProjectID,billIDMap,rationIDMap,projectGLJIDMap,model) {// 定额工料机，附注条件，工程量明细，定额安装增加费
    let subList = await model.find({projectID:originalPID}, '-_id');
    let newList =[];
    for(let s of subList){
        s._doc = getCopyRationSubData(s._doc,newProjectID,billIDMap,rationIDMap,projectGLJIDMap);
   /*     s._doc.ID = uuidV1();
        s._doc.projectID = newProjectID;
        s._doc.rationID&&rationIDMap[s._doc.rationID]?s._doc.rationID = rationIDMap[s._doc.rationID]:'';
        s._doc.billID&&billIDMap[s._doc.billID]?s._doc.billID = billIDMap[s._doc.billID]:'';
        s._doc.billsItemID&&billIDMap[s._doc.billsItemID]?s._doc.billsItemID = billIDMap[s._doc.billsItemID]:'';
        s._doc.projectGLJID&&projectGLJIDMap[s._doc.projectGLJID]?s._doc.projectGLJID = projectGLJIDMap[s._doc.projectGLJID]:'';
        if(s._doc.templateList && s._doc.templateList.length > 0 ){
            for(let t of s._doc.templateList){
                if(t.billID && billIDMap[t.billID]) t.billID =  billIDMap[t.billID];
                if(t.fxID && billIDMap[t.fxID]) t.fxID =  billIDMap[t.fxID];
            }
        }*/
        newList.push(s._doc);
    }
    await insertMany(newList,model);
}

function getCopyRationSubData(doc,newProjectID,billIDMap,rationIDMap,projectGLJIDMap){
    doc.ID = uuidV1();
    doc.projectID = newProjectID;
    doc.rationID&&rationIDMap[doc.rationID]?doc.rationID = rationIDMap[doc.rationID]:'';
    doc.billID&&billIDMap[doc.billID]?doc.billID = billIDMap[doc.billID]:'';
    doc.billsItemID&&billIDMap[doc.billsItemID]?doc.billsItemID = billIDMap[doc.billsItemID]:'';
    doc.projectGLJID&&projectGLJIDMap[doc.projectGLJID]?doc.projectGLJID = projectGLJIDMap[doc.projectGLJID]:'';
    if(doc.templateList && doc.templateList.length > 0 ){
        for(let t of doc.templateList){
            if(t.billID && billIDMap[t.billID]) t.billID =  billIDMap[t.billID];
            if(t.fxID && billIDMap[t.fxID]) t.fxID =  billIDMap[t.fxID];
        }
    }
    return doc;
}



async function moveProject(data) {
    data = JSON.parse(data);
    let projectMap = data.projectMap,feeRateMap = data.feeRateMap,unitPriceMap = data.unitPriceMap;
    let projectTasks = [],feeRateTask=[],feeRateFileTask=[],unitPriceTask=[],unitPriceFileTask=[],mixRatioTask=[];
    let feeUniqMap=[],priceUniqMap={};//费率、单价文件唯一映射表当移动多个项目时，任务有可能出现重复的情况
    if(!_.isEmpty(feeRateMap)&&data.rootProjectID){//如果费率有修改
        let feeRates =await feeRate_facade.getFeeRatesByProject(data.rootProjectID);//取目标建设项目的所有费率文件，重名检查
        for(let projectID in feeRateMap){
            let rename = feeRateMap[projectID].name;
            let feeRateID =  feeRateMap[projectID].query.ID;
            let checkFee = _.find(feeRates,{'name':rename});
            let newID = feeRateID;
            if(checkFee){//说明费率名字已存在，需要重命名
                rename = feeUniqMap[feeRateID]?feeUniqMap[feeRateID].name:rename + '（' + new Date().Format('MM-dd hh:mm:ss') + '移动）';
                projectMap[projectID]['update']['property.feeFile.name'] = rename;
                if(feeRateMap[projectID].action == 'move'){
                    feeRateMap[projectID].update.name = rename;
                }
            }
            if(feeRateMap[projectID].action == 'copy'){
                newID = feeUniqMap[feeRateID]?feeUniqMap[feeRateID].ID:uuidV1();
                feeRateMap[projectID].name = rename;
                feeRateMap[projectID].ID =  newID;
                projectMap[projectID]['update']['property.feeFile.id'] = newID;
            }
            if(!feeUniqMap[feeRateID]){
                await generateFeeRateTask(feeRateMap[projectID],feeRateTask,feeRateFileTask);
                feeUniqMap[feeRateID] = {name:rename,ID:newID};
            }
        }
    }
    if(!_.isEmpty(unitPriceMap)&&data.rootProjectID) {//如果单价文件有修改
        let unitPriceFiles = await unitPriceFileModel.find({root_project_id: data.rootProjectID, deleteInfo: null});
        for(let projectID in unitPriceMap){
            let checkUn = _.find(unitPriceFiles,{'name':unitPriceMap[projectID].name});
            let rename = unitPriceMap[projectID].name;
            let unitPriceID = unitPriceMap[projectID].query.id;
            let newID = unitPriceID;
            if(checkUn){//重名检查
                rename = priceUniqMap[unitPriceID]?priceUniqMap[unitPriceID].name:rename + '（' + new Date().Format('MM-dd hh:mm:ss') + '移动）';
                projectMap[projectID]['update']['property.unitPriceFile.name'] = rename;
                if(unitPriceMap[projectID].action =='move'){
                    unitPriceMap[projectID].update.name = rename;
                }
            }
            if(unitPriceMap[projectID].action =='copy'){
                newID =  priceUniqMap[unitPriceID]?priceUniqMap[unitPriceID].id: await getCounterID("unit_price_file");
                unitPriceMap[projectID].name = rename;
                unitPriceMap[projectID].id = newID;
                projectMap[projectID]['update']['property.unitPriceFile.id'] = newID;
            }
            if(!priceUniqMap[unitPriceID]){
                await generateUnitPriceTask(unitPriceMap[projectID],projectID,data.user_id,unitPriceTask,unitPriceFileTask,mixRatioTask);
                priceUniqMap[unitPriceID] = {name:rename,id:newID};
            }
        }
    }

    if(!_.isEmpty(projectMap)){
        for(let projectID in projectMap){
            projectTasks.push({updateOne:{filter : projectMap[projectID].query, update : projectMap[projectID].update}});
        }
    }

    projectTasks.length>0?await projectModel.bulkWrite(projectTasks):'';
    feeRateTask.length>0?await feeRateModel.bulkWrite(feeRateTask):'';
    feeRateFileTask.length>0?await feeRateFileModel.bulkWrite(feeRateFileTask):'';
    unitPriceFileTask.length>0?await unitPriceFileModel.bulkWrite(unitPriceFileTask):'';
    unitPriceTask.length>0?await insertMany(unitPriceTask,unitPriceModel):'';
    mixRatioTask.length>0?await insertMany(mixRatioTask,mixRatioModel):'';
    return projectMap;
}

async function generateFeeRateTask(data,feeRateTask,feeRateFileTask) {
    if(data.action == 'move'){
        let task = {
            updateOne:{
                filter : data.query,
                update : data.update
            }
        };
        feeRateFileTask.push(task);
    }
    if(data.action == 'copy'){//copy 费率的时候用insertOne方法
        let [feeRateFile,feeRate] =await feeRate_facade.getFeeRateByID(data.query.ID);
        let newFeeRateID =  uuidV1();
        let newFeeRate = {
            ID:newFeeRateID,
            rates:feeRate.rates
        };
        let newFeeRateFile = {
            ID:data.ID,
            rootProjectID:data.rootProjectID,
            userID:feeRateFile.userID,
            name:data.name,
            libID:feeRateFile.libID,
            libName:feeRateFile.libName,
            feeRateID:newFeeRateID
        };
        feeRateTask.push({insertOne :{document:newFeeRate}});
        feeRateFileTask.push({insertOne :{document:newFeeRateFile}});
    }
}

async function generateUnitPriceTask(data,projectID,user_id,unitPriceTask,unitPriceFileTask,mixRatioTask){
    if(data.action == 'move'){
        let task = {
            updateOne:{
                filter : data.query,
                update : data.update
            }
        };
        unitPriceFileTask.push(task);
    }
    if(data.action == 'copy'){
        let newUnitFile = {
            id:data.id,
            name: data.name,
            project_id:projectID,
            user_id:user_id,
            root_project_id: data.rootProjectID
        };
        unitPriceFileTask.push({insertOne :{document:newUnitFile}});
        let mixRatios = await mixRatioModel.find({unit_price_file_id: data.query.id});
        mixRatios = JSON.stringify(mixRatios);
        mixRatios = JSON.parse(mixRatios);
        for(let m of mixRatios){
            delete m._id;  // 删除原有id信息
            delete m.id;
            m.unit_price_file_id = data.id;
            m.id = await getCounterID('mix_ratio');
            mixRatioTask.push(m);
        }
        let unitPrices = await unitPriceModel.find({unit_price_file_id: data.query.id});//unit_price_file_id
        unitPrices = JSON.stringify(unitPrices);
        unitPrices = JSON.parse(unitPrices);
        for(let u of unitPrices){
            delete u._id;  // 删除原有id信息
            delete u.id;
            u.unit_price_file_id = data.id;
            u.id = await getCounterID('unit_price');
            unitPriceTask.push(u);
        }
    }
}


async function getCounterID(collectionName){
    let counterModel = new CounterModel();
    return  await counterModel.getId(collectionName);
}

async function insertMany(datas,model) {
   /*  while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
        let newList = datas.splice(0,1000);//一次插入1000条
        await model.insertMany(newList);
    } */
    await model.insertMany(datas);

}

function setProperty(Obj,updateData) {
    for(let ukey in updateData){
        if(_.isObject(updateData[ukey]) && _.isObject(Obj[ukey])){
            setProperty(Obj[ukey],updateData[ukey]);
        }else {
            Obj[ukey] = updateData[ukey];
        }
    }


}

function isDef(v){
    return typeof v !== 'undefined' && v !== null;
}

function getTotalFee(bills, feeName) {
    if(!isDef(bills)){
        return 0;
    }
    if(!isDef(bills.fees) || bills.fees.length <= 0){
        return 0;
    }
    for(let fee of bills.fees){
        if(isDef(fee.fieldName) && fee.fieldName === feeName){
            return isDef(fee.totalFee) ? fee.totalFee : 0;
        }
    }
    return 0;
}

function getCommonTotalFee(bills) {
    if(!isDef(bills)){
        return 0;
    }
    if(!isDef(bills.fees) || bills.fees.length <= 0){
        return 0;
    }
    for(let fee of bills.fees){
        if(isDef(fee.fieldName) && fee.fieldName === 'common'){
            return isDef(fee.totalFee) ? fee.totalFee : 0;
        }
    }
    return 0;
}

function summarizeToParent(parent, child, flagFieldMapping) {
    const decimal = -2;
    let costFields = [];
    for (let flag in flagFieldMapping) {
        let costItems = flagFieldMapping[flag];
        for (let costItem of costItems) {
            costFields.push(costItem.name);
        }
    }
    for (let field of costFields) {
        parent[field] = scMathUtil.roundTo(parseFloat(parent[field]) + parseFloat(child[field]), decimal);
    }
}

function getBuildingArea(projFeature){
    if(!projFeature || projFeature.length === 0){
        return null;
    }
    for(let f of projFeature){
        if(f.key === 'buildingArea'){
            return f.value;
        }
    }
    return null;
}

// 根据树结构数据排序
function getSortedDataByTree(data) {
    // NextSiblingID-数据映射
    const mapping = {};
    data.forEach(item => {
        // 错误数据兼容处理
        // 由于之前的导入建设项目，会导致最后的一个项目的NextSiblingID为空，为非正确的-1
        // 因此把项目中NextSibling为空的数据设置为NextSiblingID为-1
        if (!item.NextSiblingID) {
            item.NextSiblingID = -1;
        }
        mapping[item.NextSiblingID] = item;
    });
    let lastItem = data.find(item => +item.NextSiblingID === -1);
    if (!lastItem) {
        return data;
    }
    const rst = [lastItem];
    let preItem = mapping[lastItem.ID];
    while (preItem) {
        rst.unshift(preItem);
        preItem = mapping[preItem.ID];
    }
    // 根据树结构排序形成的新数组与旧数组项数不同，说明树结构数据有问题（断链），则返回原本数据。
    return rst.length === data.length ? rst : data;
}

//根据单位工程ID获取汇总信息
//@param {Number}tenderID {String}summaryType @return {Object}
async function getSummaryInfoByTender(tenderID, summaryType) {
    const notDeleted = [{deleteInfo: null}, {'deleteInfo.deleted': false}];
    let tender = await projectModel.findOne({ID: tenderID, $or: notDeleted});
    let parentName;
    let summaryList = [];
    if(!tender){
        return null;
    }
    let project = await projectModel.findOne({ID: tender.ParentID, $or: notDeleted});
    if(!project){
        return null;
    }
    let flagFieldMapping = {
        [fixedFlag.TOTAL_COST]: [{name: 'totalCost', feeField: 'common'}], // 总造价
        [fixedFlag.CONSTRUCTION_INSTALL_FEE]: [{name: 'constructionInstallFee', feeField: 'rationCommon'},], // 建安费
    };
    let summaryInfo = await getSummaryInfo([project.ID], flagFieldMapping);
    // 汇总到建设项目层（没有单项工程层了）
    if (summaryType === projectType.project) {
        parentName = project.name ? project.name : '';
        let tenders = await projectModel.find({ParentID: project.ID, $or: notDeleted}).lean();
        tenders = getSortedDataByTree(tenders);
        for(let t of tenders){
            if(summaryInfo[t.ID]){
                summaryInfo[t.ID]['name'] = t.name ? t.name : '';
                summaryList.push(summaryInfo[t.ID]);
            }
        }
    }
    return {parent: {name: parentName}, subList: summaryList};
}

//获取单位工程的各标段费用信息(不进行汇总)
async function getTendersFeeInfo(tenders) {
    let IDMapping = {};
    //固定清单类别与汇总金额字段映射
    let flagFieldMapping = {
        [fixedFlag.TOTAL_COST]: [{name: 'totalCost', feeField: 'common'}]
    };
    let tenderIDs = [];
    if(tenders.length > 0){
        for(let tender of tenders){
            tenderIDs.push(tender.ID);
            IDMapping[tender.ID] = {totalCost: 0};
        }
        //需要获取的清单固定类别综合合价:总造价
        let needFlags = Object.getOwnPropertyNames(flagFieldMapping);
        //获取单位工程汇总金额需要用到的所有清单
        let allBills = await billsModel.find({projectID: {$in: tenderIDs}, 'flags.flag': {$in: needFlags}, $or: notDeleted},
            '-_id projectID fees flags');
        //进行单位工程级别的汇总
        for(let bills of allBills){
            let billsFlag = bills.flags[0]['flag'];
            let costItems = flagFieldMapping[billsFlag];
            for (let costItem of costItems) {
                IDMapping[bills.projectID][costItem.name] = getTotalFee(bills, costItem.feeField);
            }
        }
    }
    return IDMapping;
}

// 项目管理界面需要显示的汇总字段
function setupSummaryFields(summaryInfo, projects, fields = null) {
    if (!fields) {
        fields = [
            'totalCost'
        ];
    }
    for(const proj of projects){
        const summaryProj = summaryInfo[proj.ID];
        if(summaryProj){
            for (const field of fields) {
                proj[field] = summaryProj[field];
            }
        }
    }
}

//@param {Array}projectIDs(建设项目ID) @return {Object}
async function getSummaryInfo(projectIDs, flagFieldMapping = null){
    //ID与汇总信息映射
    let IDMapping = {};
    //固定清单类别与汇总金额字段映射
    if (!flagFieldMapping) {
        flagFieldMapping = {
            [fixedFlag.TOTAL_COST]: [{name: 'totalCost', feeField: 'common'}], // 总造价
        };
    }
    let projects = await projectModel.find({ID: {$in : projectIDs}, projType: projectType.project, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
    for(let project of projects){
        IDMapping[project.ID] = {totalCost: 0};
    }
    //单位工程
    let tenders = [];
    if(projectIDs.length > 0){
        tenders = await projectModel.find({ParentID: {$in : projectIDs}, projType: projectType.tender, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
    }
    let tenderIDs = [];
    if(tenders.length > 0){
        for(let tender of tenders){
            tenderIDs.push(tender.ID);
            IDMapping[tender.ID] = {totalCost: 0, changeMark:tender.changeMark,property:tender.property};//property:tender.property
        }
        //需要获取的清单固定类别综合合价:总造价
        let needFlags = Object.getOwnPropertyNames(flagFieldMapping);
        //获取单位工程汇总金额需要用到的所有清单
        let allBills = await billsModel.find({projectID: {$in: tenderIDs}, 'flags.flag': {$in: needFlags}, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]},
                                            '-_id projectID fees flags');
        //进行单位工程级别的汇总
        for(let bills of allBills){
            let billsFlag = bills.flags[0]['flag'];
            let costItems = flagFieldMapping[billsFlag];
            for (let costItem of costItems) {
                IDMapping[bills.projectID][costItem.name] = getTotalFee(bills, costItem.feeField);
            }
        }
        //进行建设项目级别的汇总
        for(let tender of tenders){
            summarizeToParent(IDMapping[tender.ParentID], IDMapping[tender.ID], flagFieldMapping);
        }
    }
    return IDMapping;
}

//根据项目ID获取所属建设项目
//@param {Number}projectID @return {Object}
async function getConstructionProject(projectID){
    function returnProject(project){
        if(!project || project.projType === projectType.folder){
            return null;
        }
        if(project.projType === projectType.project){
            return project;
        }
    }
    let project = await projectModel.findOne({ID: projectID, $or: notDeleted});
    let returnV = returnProject(project);
    if(returnV !== undefined){
        return returnV;
    }
    let parent = await projectModel.findOne({ID: project.ParentID, $or: notDeleted});
    returnV = returnProject(parent);
    if(returnV !== undefined){
        return returnV;
    }
    let grandParent = await projectModel.findOne({ID: parent.ParentID, $or: notDeleted});
    returnV = returnProject(grandParent);
    return returnV !== undefined ? returnV : null;
}

//获取单位工程完整目录结构
//@param {Number}projectID @return {Arry}
async function getFullPath(projectID) {
    let fullPath = [];
    let project = await projectModel.findOne({ID: projectID, $or: notDeleted}, '-_id ParentID name');
    if(project){
        fullPath.push(project.name);
        await getParent(project.ParentID);
    }
    fullPath = fullPath.reverse();
    return fullPath;
    async function getParent(ParentID) {
        if(ParentID != -1){
            let parent = await projectModel.findOne({ID: ParentID, $or: notDeleted}, '-_id ParentID name');
            if(parent){
                fullPath.push(parent.name);
                await getParent(parent.ParentID);
            }
        }
    }
}

//@param {String}libID(工程特征库ID) {Object}assign(需要赋值的key-value)
async function getProjectFeature(libID, assign) {
    let lib = await featureLibModel.findOne({'ID':libID});
    if (lib) {
        for (let key in assign) {
            let obj = _.find(lib.feature, {key: key});
            if (obj) {
                obj.value = assign[key];
            }
        }
        return lib.feature;

    } else {
        return [];
    }
}

//获取累进区间数据
async function getProgressiveInterval(libID) {
    let lib = await progressiveModel.findOne({ID: libID});
    return lib ? lib.data : [];
}

// 获取项目链上，自身、父项、爷爷项...的ID
async function getUpChainIDs(projectID) {
    const rst = [];
    while (projectID != -1) {
        rst.push(projectID);
        const project = await projectModel.findOne({ ID: projectID }, '-_id ParentID').lean();
        projectID = project ? project.ParentID : -1;
    }
    return rst;
}

//获取projectIDs文件下所有子项目（默认不包括projectIDs本身）
async function getPosterityProjects(projectIDs, includeSelf = false) {
    let rst = [];
    if (includeSelf) {
        const projects = await projectModel.find({ID: {$in: projectIDs}, $or: notDeleted}, '-_id').lean();
        if (projects) {
            rst.push(...projects);
        }
    }
    async function getProjects(parentIDs) {
        if (parentIDs.length > 0) {
            let newIDs = [];
            let projs = await projectModel.find({ParentID: {$in: parentIDs}, $or: notDeleted}, '-_id').lean();
            for (let proj of projs) {
                // 兼容旧的错误数据，可能ParentID和ID相同
                if (parentIDs.includes(proj.ID)) {
                    continue;
                }
                rst.push(proj);
                newIDs.push(proj.ID);
            }
            await getProjects(newIDs);
        }
    }
    await getProjects(projectIDs);
    return rst;
}


//根据项目获得分享信息(分享层级不一定在项目级)(以最新为准)
async function getShareInfo(userID, projectID) {
    // 接受到的分享项目
    const upChainIDs = await getUpChainIDs(projectID);
    const shareList = await getShareList({receiver: userID, projectID: {$in: upChainIDs}});
    // 返回最新的分享
    //return shareList.sort((a, b) => Date.parse(b.shareDate) - Date.parse(a.shareDate))[0];
    return shareList.sort((a, b) => Date.parse(b.updateDate) - Date.parse(a.updateDate))[0];
}

//打开的单位工程是否是被分享的.
async function isShare(userId, project){
    //判断是否是打开分享的项目，属于分享文件的子项也算
    while (project) {
        for(let shareData of project.shareInfo){
            if(shareData.userID === userId){
                return true;
            }
        }
        project = await projectModel.findOne({ID: project.ParentID}, '-_id shareInfo ID ParentID');
    }
    return false;
}

//用户是否第一次进入费用定额
async function isFirst(userId, compilationId) {
    let userData = await userModel.findOne({_id: mongoose.Types.ObjectId(userId)}, '-_id used_list');
    let isFirst = false;
    if (userData) {
        isFirst = !_.find(userData.used_list, function (o) {
            return o.compilationId === compilationId;
        })
    }
    return isFirst;
}

function testTimeout(time) {
    return new Promise(resolve => setTimeout(resolve, time));
}

//用户第一次进入费用定额的数据准备
async function prepareInitialData(userId, compilation, example) {
    let first = await isFirst(userId, compilation);
    if (first) {
        await updateUsedList(userId, compilation);
        const logData = {
            key: uuidV1(),
            content: '正在加载例题，请稍候……',
            userID: userId,
            compilationID: compilation,
            status: 'start',
            create_time: +new Date()
        };
        await importLogsModel.create(logData);
        initData(logData.key, userId, compilation, example);
        return 'start prepareInitialData';
    }
    async function initData(key, userId, compilation, example) {
        const doc = { status: 'finish' };
        try {
            let prepareTask = [
                copyCompleRationSection(userId, compilation),
                copyCompleGljSection(userId, compilation)
            ];
            if (example && example.length > 0) {
                prepareTask.push(copyExample(userId, compilation, example));
            }
            await Promise.all(prepareTask);
        } catch (error) {
            doc.errorMsg = '加载例题失败，请检查例题项目是否存在！';
            doc.status = 'error';
        } finally {
            await importLogsModel.update({ key }, doc);
        }
    }
}

async function updateUsedList(userId, compilation) {
    await userModel.update({_id: mongoose.Types.ObjectId(userId)}, {$push: {used_list: {compilationId: compilation}}});
}

//拷贝补充定额章节树
async function copyCompleRationSection(userId, compilationId) {
    await sectionTreeDao.copyDataFromTemplate(userId, compilationId);
}

/*
 * 解决问题：
 * 发布了编办之后，才设置了人材机库模板，导致已登录的用户不存在人材机库的分类树
 * 为该编办下，人材机分类模板为空的用户，拷贝模板数据
 * */
async function copyForSectionError(compilation) {
    let users = await userModel.find({}, '_id');
    for (let user of users) {
        let userId = user._id.toString();
        let existCount = await compleGljSectionModel.find({userId}).count();
        if (existCount === 0) {
            await copyCompleGljSection(userId, compilation);
        }
    }
}

//拷贝补充人材机分类树
async function copyCompleGljSection(userId, compilationId) {
    const count = await compleGljSectionModel.count({userId, compilationId});
    if (count > 0) {
        return;
    }
    let templateData = await compleGljSectionTModel.find({compilationId: compilationId});
    if (templateData.length > 0) {
        let insertDatas = [],
            uuidMapping = {};
        //将ID替换成UUID
        for (let temData of templateData) {
            uuidMapping[temData.ID] = uuidV1();
        }
        for(let temData of templateData) {
            let insertD = {
                userId: userId,
                compilationId: compilationId,
                Name: temData.Name,
                ID: uuidMapping[temData.ID],
                ParentID: uuidMapping[temData.ParentID] || -1,
                NextSiblingID: uuidMapping[temData.NextSiblingID] || -1,
            };
            insertDatas.push(insertD);
        }
        //插入数据
        await compleGljSectionModel.insertMany(insertDatas);
    }
}

async function changeFile(datas,userID,fileID,name,from,type){//from 费率或单价文件，type从单前建设项目还是从其它建设项目中复制
    let projectIDs = [],newFile = {id:fileID,name:name};//计录从其它项目中复制的文件，选中多个的时候只需复制一次
    let projectUpdateType = from == "feeRateFile"?"feeRate":"unitFile";
    for(let d of datas){
        let tem_file = from == "feeRateFile"? await feeRate_facade.changeFeeRateFile(d, newFile,type,userID):await glj_facade.changeUnitFile(d, newFile,type,userID);
        if(type == 1 && tem_file){//从建设项目复制时，只有第一次需要复制，剩下的就相当于使用同个建设项目的情况了
            let newID =  from == "feeRateFile"?tem_file.ID:tem_file.id;
            newFile = {id:newID,name:tem_file.name};
            type = 0;
        }
        projectIDs.push({ID:d.projectID})
    }
    await project_facade.markProjectsToChange(projectIDs,projectUpdateType)//项目标记为待刷新状态

}

async function exportProject(userID,data){//导出建设项目
    if(data.type == 'main'){
        return await exportMainData(userID,data.projectID);
    }else {
        return await  exportTenderData(data);
    }
}

async function exportMainData(userID,projectID) {
    let result = {userID:userID,projects:[],type:'Project'};//type 备用属性，表示导出的类型，目前导出的都是整个建设项目
    let tenderIDs = [];
    let project = await projectModel.findOne({ID:projectID});
    if(!project) throw  new Error("没有找到该建设项目："+projectID);
    result['compilationID'] = project.compilation;
    result['from'] = "yanghu";
    result.projects.push(project);
    let subProjects = await  projectModel.find({"$or": [{'ParentID':projectID}, {"property.rootProjectID": projectID}]});
    for(let s of subProjects){
        if(!s.deleteInfo || !s.deleteInfo.deleted){
            result.projects.push(s);
            if(s.projType =="Tender") tenderIDs.push(s.ID);
        }
    }
    let files = {unitFiles:await exportUnitFiles(projectID),feeRateFiles:await exportFeeRateFiles(projectID)};
    result.files = files;
    result = cipher.aesEncrypt(JSON.stringify(result));
    result +="|----|" +JSON.stringify(tenderIDs);
    return result;
}

async function exportUnitFiles(rootProjectID){
    let unitFiles = [];
    let files = await unitPriceFileModel.find({root_project_id:rootProjectID});
    for(let f of  files){
         if(f.deleteInfo && f.deleteInfo.deleted == true) continue;
         let tem = {unitFile:f};
         tem.unitPrices = await unitPriceModel.find({unit_price_file_id:f.id});
         tem.mixRatios = await mixRatioModel.find({unit_price_file_id:f.id});
         tem.freights = await freightModel.find({unit_price_file_id:f.id});
         tem.originals = await originalModel.find({unit_price_file_id:f.id});
         tem.com_electrovalence = await comElectrovalenceModel.find({unit_price_file_id:f.id});
         unitFiles.push(tem);
    }
    return unitFiles;
}

async function exportFeeRateFiles(rootProjectID) {
    let feeRateFiles = [];
    let files = await feeRateFileModel.find({rootProjectID:rootProjectID});
    for(let f of files){
        if(f.deleteInfo && f.deleteInfo.deleted == true) continue;
        let tem = {feeRateFile:f};
        tem.feeRate = await feeRateModel.findOne({ID:f.feeRateID});
        feeRateFiles.push(tem);
    }
    return feeRateFiles;
}



async function exportTenderData(data){
    let result = {};
    let projectSetting =  await projectSettingModel.findOne({"projectID": data.projectID}, '-_id');
    if(projectSetting) result['projSetting'] = projectSetting;
    result.bills = await billsModel.find({"projectID": data.projectID});
    result.rations = await rationModel.find({'$or': [{projectID: data.projectID, deleteInfo: null}, {projectID: data.projectID, 'deleteInfo.deleted': {$in: [null, false]}}]});
    result.projectGLJs = await gljListModel.find({'project_id':data.projectID});
    result.installationFees =  await installationFeeModel.find({projectID:data.projectID});
    result.rationGLJs = await rationGLJModel.find({projectID:data.projectID});
    result.rationCoes = await rationCoeModel.find({projectID:data.projectID});
    result.quantityDetails = await quantityDetailModel.find({projectID:data.projectID});
    result.rationInstallations = await rationInstallationModel.find({projectID:data.projectID});
    result.rationTemplates = await rationTemplateModel.find({projectID:data.projectID});
    result.calcProgramsFile = await calcProgramsModel.findOne({projectID:data.projectID});
    result.labourCoes = await labourCoesModel.findOne({projectID:data.projectID});

    return cipher.aesEncrypt(JSON.stringify(result));
}

async function downLoadProjectFile(info) {
    let result = {error:0};
    let bucketManager2 = new qiniu.rs.BucketManager(mac, null);
    let publicBucketDomain = qiniu_config.Domain;// "http://serverupdate.smartcost.com.cn";//这里不支持https
    let deadline = parseInt(Date.now() / 1000) + 3600; // 1小时过期
    let privateDownloadUrl = bucketManager2.privateDownloadUrl(publicBucketDomain, info.key, deadline);

    let data = {
        key:info.key,
        content: '正在导入建设项目，请稍候……',
        userID:info.session.sessionUser.id,
        compilationID: info.session.sessionCompilation._id,
        status:"start",
        create_time:+new Date()
    };
    await importLogsModel.create(data);
    doDownLoadAndImport(privateDownloadUrl,info);
    return result;
}


async function doDownLoadAndImport(privateDownloadUrl,info) {
    let stream = fs.createWriteStream(path.join(__dirname, "../../../tmp/"+info.key));//
    request(privateDownloadUrl).pipe(stream).on("close", async function (err) {
        console.log("文件[" + info.key + "]下载完毕");
        let doc = {status:"finish"};
        try {
            let data = fs.readFileSync(stream.path,'utf-8');
            let result = await importProject(data,{session:info.session},info.updateData);
            if(result.error == 1){
                doc.errorMsg = result.msg;
                doc.status = "error";
            } else {
                doc.projectID = result.constructionProjectID;
            }
        }catch (error){
            console.log(error);
            doc.errorMsg = "导入失败，请检查文件！";
            doc.status = "error";
        }finally {
            await importLogsModel.update({key:info.key},doc);
            fs.unlinkSync(stream.path);
        }
    });
}

async function importProcessChecking(data){
    let result = {error:0};
    const query = data.key ? { key: data.key } : { userID: data.userID, compilationID: data.compilationID };
    let log = await importLogsModel.findOne(query);
    if(log){
        if(log.status == "finish"){
            result.status = "complete";
            await importLogsModel.remove(query);
            // 获取导入的项目数据
            if (log.projectID) {
                const projects = await getPosterityProjects([log.projectID], true);
                if (!(projects.length === 1 && projects[0].projType === projectType.tender)) { // 处理的是建设项目数据
                    const summaryInfo = await getSummaryInfo([log.projectID]);
                    setupSummaryFields(summaryInfo, projects);
                }
                result.data = projects;
            }
        }else if(log.status == "start"){
            result.status = "processing";
            result.content = log.content;
        }else if(log.status == "error"){
            result.error = 1;
            result.msg = log.errorMsg;
            await importLogsModel.remove(query);
        }
    }else {
        result.status = "complete";
    }
    return result;

}


async function importProject(data,req,updateData) {
    let result = {error:0};
    let stringArr = data.split("|----|");
    let datas = [];
    for(let s of stringArr){
        datas.push(JSON.parse(cipher.aesDecrypt(s)));
    }
    let mainData = datas.length > 0 ?datas[0]:null;
    if(mainData){
         if(mainData.from && mainData.from != "yanghu"){
             result.error = 1;
             result.msg = "导入失败：您要导入的文件是由“大司空云计价”导出，当前软件是“纵横公路养护云版”，请选择正确的软件再进行操作！";
         }else if(mainData.compilationID != req.session.sessionCompilation._id){
             const fileCompilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(mainData.compilationID)}, 'name');
             const fileCompilationName = fileCompilation ? fileCompilation.name : '未知的编制办法';
             const curCompilationName = req.session.sessionCompilation.name;
             result.error = 1;
             result.msg = `导入失败：您要导入的文件是由“${fileCompilationName}”导出，当前软件是“${curCompilationName}”，请选择正确的编制办法再进行操作！`;
         }else {
            const tenders = mainData.projects.filter(item => item.projType === projectType.tender);
            const tenderOverrun = await isTenderOverrun(tenders.length, req.session);
            if (tenderOverrun) {
                result.error = 1;
                result.msg = `您创建的项目个数超限，请联系我们的客服人员，或者导出建设项目保存到本地备份，删除云上数据。`;
                return result;
            }
            let [constructionProjectID,projectIDMap,labourCoeFileIDMap,calcProgramFileIDMap] = await handleMainProjectDatas(mainData,updateData,req.session.sessionUser.id);
            result.constructionProjectID = constructionProjectID;
            if(datas.length > 1 ){//生成后统一次插入 2020-05-29
              let newProjectSettings=[],bills=[],rations=[],projectGLJs=[],rationGLJs=[],rationCoes=[],quantityDetails=[],rationInstallations=[],rationTemplates=[],newCalcProgramsFiles=[],newLabourCoes=[];
                for(let i = 1;i<datas.length;i++){
                  let [newProjectSetting,tbills,trations,tprojectGLJs,trationGLJs,trationCoes,tquantityDetails,trationInstallations,trationTemplates,newCalcProgramsFile,newLabourCoe] =  await handleEachProject(datas[i],projectIDMap,labourCoeFileIDMap,calcProgramFileIDMap)
                  if(newProjectSetting) newProjectSettings.push(newProjectSetting);
                  if(tbills.length > 0) bills = bills.concat(tbills);
                  if(trations.length > 0) rations = rations.concat(trations);
                  if(tprojectGLJs.length > 0) projectGLJs = projectGLJs.concat(tprojectGLJs); 
                  if(trationGLJs.length > 0) rationGLJs = rationGLJs.concat(trationGLJs);
                  if(trationCoes.length > 0) rationCoes = rationCoes.concat(trationCoes);
                  if(tquantityDetails.length > 0) quantityDetails = quantityDetails.concat(tquantityDetails);
                  if(trationInstallations.length > 0) rationInstallations= rationInstallations.concat(trationInstallations);
                  if(trationTemplates.length > 0) rationTemplates = rationTemplates.concat(trationTemplates); 
                  if(newCalcProgramsFile) newCalcProgramsFiles.push(newCalcProgramsFile);
                  if(newLabourCoe) newLabourCoes.push(newLabourCoe);
                }

                if(newProjectSettings.length > 0) await insertMany(newProjectSettings,projectSettingModel);
                if(bills.length > 0) await insertMany(bills,billsModel);
                if(rations.length > 0) await insertMany(rations,rationModel);
                if(projectGLJs.length > 0) await insertMany(projectGLJs,gljListModel);
                if(rationGLJs.length > 0) await insertMany(rationGLJs,rationGLJModel);
                if(rationCoes.length > 0) await insertMany(rationCoes,rationCoeModel);
                if(quantityDetails.length > 0) await insertMany(quantityDetails,quantityDetailModel);
                if(rationInstallations.length > 0) await insertMany(rationInstallations,rationInstallationModel);
                if(rationTemplates.length > 0) await insertMany(rationTemplates,rationTemplateModel);
                if(newCalcProgramsFiles.length > 0) await insertMany(newCalcProgramsFiles,calcProgramsModel);
                if(newLabourCoes.length>0) await insertMany(newLabourCoes,labourCoesModel);
            }

         }
    }
    return result;
}

async function handleEachProject(data,projectIDMap,labourCoeFileIDMap,calcProgramFileIDMap){
    let bills = [],rations = [],projectGLJs = [],rationGLJs=[],rationCoes=[],quantityDetails=[],rationInstallations=[],rationTemplates=[];
    let newProjectSetting =null,newCalcProgramsFile = null,newLabourCoe = null;
    let billsIDMap = {},projectGLJIDMap={},rationIDMap = {};
    let newProjectID = projectIDMap[data.projSetting.projectID];
    //生成新的清单;
    if(data.bills && data.bills.length > 0){
        for(let b of data.bills){
            billsIDMap[b.ID] = uuidV1();
        }
        for(let b of data.bills){
            delete b._id;
            b = getCopyBillDatas(b,newProjectID,billsIDMap);
            bills.push(b);
        }
    }

    //生成项目工料机数据
    if(data.projectGLJs && data.projectGLJs.length > 0){
        let gljCount = await counter.counterDAO.getIDAfterCountSync(counter.moduleName.glj_list, data.projectGLJs.length);
        for(let i = 0; i < data.projectGLJs.length; i++){
            let d = data.projectGLJs[i];
            delete d._id;
            let newID = gljCount.sequence_value - (data.projectGLJs.length - 1) + i;
            projectGLJIDMap[d.id] = newID;
            d.project_id = newProjectID;
            d.id = newID;
            projectGLJs.push(d);
        }
    }

    //生成新的定额
    if(data.rations && data.rations.length > 0){
        for(let r of data.rations){
            rationIDMap[r.ID] = uuidV1();
        }
        for(let r of data.rations){
            delete r._id;
            r = getCopyRationData(r,newProjectID,billsIDMap,rationIDMap,projectGLJIDMap);
            rations.push(r);
        }
    }

    //生成定额下挂的定额工料机等等数据
    if(data.rationGLJs && data.rationGLJs.length > 0) rationGLJs = setRationSubList(data.rationGLJs,newProjectID,billsIDMap,rationIDMap,projectGLJIDMap);
    if(data.rationCoes && data.rationCoes.length > 0) rationCoes = setRationSubList(data.rationCoes,newProjectID,billsIDMap,rationIDMap,projectGLJIDMap);
    if(data.quantityDetails && data.quantityDetails.length > 0) quantityDetails = setRationSubList(data.quantityDetails,newProjectID,billsIDMap,rationIDMap,projectGLJIDMap);
    if(data.rationInstallations && data.rationInstallations.length > 0) rationInstallations = setRationSubList(data.rationInstallations,newProjectID,billsIDMap,rationIDMap,projectGLJIDMap);
    if(data.rationTemplates && data.rationTemplates.length > 0) rationTemplates = setRationSubList(data.rationTemplates,newProjectID,billsIDMap,rationIDMap,projectGLJIDMap);

    //生成projectSetting 文件
    if(data.projSetting){
        data.projSetting.projectID =newProjectID;
        newProjectSetting = data.projSetting;
        delete newProjectSetting._id;
    }

    //生成计算程序文件，系数文件
    if(data.calcProgramsFile && calcProgramFileIDMap[data.calcProgramsFile.ID]){
        data.calcProgramsFile.ID = calcProgramFileIDMap[data.calcProgramsFile.ID];
        data.calcProgramsFile.projectID = newProjectID;
        newCalcProgramsFile =  data.calcProgramsFile;
        delete newCalcProgramsFile._id;

    }
    if(data.labourCoes && labourCoeFileIDMap[data.labourCoes.ID]){
        data.labourCoes.ID = labourCoeFileIDMap[data.labourCoes.ID];
        data.labourCoes.projectID = newProjectID;
        newLabourCoe = data.labourCoes;
        delete newLabourCoe._id;
    }
    return [newProjectSetting,bills,rations,projectGLJs,rationGLJs,rationCoes,quantityDetails,rationInstallations,rationTemplates,newCalcProgramsFile,newLabourCoe]

}


function setRationSubList(datas,newProjectID,billIDMap,rationIDMap,projectGLJIDMap) {
    let arrs = [];
    for(let d of datas){
        delete d._id;
        d = getCopyRationSubData(d,newProjectID,billIDMap,rationIDMap,projectGLJIDMap);
        arrs.push(d);
    }
    return arrs;
}

async function handleMainProjectDatas(mainData,updateData,userID) {
    let mainProjectID = -1;
    let projectIDMap = {},feeRateFileIDMap={},unitPriceFileIDMap={},labourCoeFileIDMap={},calcProgramFileIDMap={};
    let tasks = [];
    projectIDMap[-1] = -1;//最后一个项目的nextID为-1的情况
    let constructionProjectID;
    //生成新的projectID
    for(let p of mainData.projects){
        let newProjectID = await getCounterID("projects");
        projectIDMap[p.ID] = newProjectID;
        if(p.projType == "Project") {
            mainProjectID =  newProjectID;
            constructionProjectID = newProjectID;
        }
        if(p.projType == "Tender"){
            if(p.property.calcProgramFile) calcProgramFileIDMap[p.property.calcProgramFile.ID] = uuidV1();//新的计算程序文件ID
            if(p.property.labourCoeFile) labourCoeFileIDMap[p.property.labourCoeFile.ID] = uuidV1();//新的人工调整系数文件ID
            if(p.property.feeFile) feeRateFileIDMap[p.property.feeFile.id] = uuidV1();//新的费率文件ID
            if(p.property.unitPriceFile) unitPriceFileIDMap[p.property.unitPriceFile.id] = await getCounterID("unit_price_file");//新的单价文件ID
        }
    }
    if(mainProjectID == -1) throw new Error("文件里面没包含建设项目信息！");

    //处理项目信息  ----- 费率文件，单价文件ID信息要重新生成----to do
    for(let p of mainData.projects){
        delete p._id;
        delete p.__v;
        p.ID = projectIDMap[p.ID];
        if(p.ID == mainProjectID){//对于建设项目，要父和下一节点ID要使用前端传入的位置ID
            p.ParentID = updateData.self.ParentID;
            p.NextSiblingID = updateData.self.NextSiblingID;
            if(updateData.update){//树节构中的上一节点的下一节点设置为本建设项目ID;
                tasks.push({updateOne:{filter : updateData.update.query, update : {NextSiblingID:mainProjectID}}});
            }
            //查看是否重名;
            let temp = await projectModel.findOne({userID:userID,ParentID:p.ParentID,name:p.name});
            if(temp) p.name = p.name + '（' + moment(Date.now()).tz("Asia/Shanghai").format('MM-DD HH:mm:ss') + '导入）';
        }else {
            p.ParentID = projectIDMap[p.ParentID];
            p.NextSiblingID = projectIDMap[p.NextSiblingID];
        }
        p.userID =userID;
        p.shareInfo=[];
        if(p.projType == "Tender"){
            if(p.property.calcProgramFile) p.property.calcProgramFile.ID = calcProgramFileIDMap[p.property.calcProgramFile.ID];
            if(p.property.labourCoeFile) p.property.labourCoeFile.ID = labourCoeFileIDMap[p.property.labourCoeFile.ID];
            if(p.property.feeFile) p.property.feeFile.id = feeRateFileIDMap[p.property.feeFile.id];
            if(p.property.unitPriceFile) p.property.unitPriceFile.id = unitPriceFileIDMap[p.property.unitPriceFile.id];
            p.property.rootProjectID = mainProjectID
        }
        tasks.push({insertOne: {document: p}})
    }
    //项目树节构数据生成：
      await projectModel.bulkWrite(tasks);

    //生成所有的费率文件
    await importFeeRateFiles(mainData,projectIDMap,feeRateFileIDMap,userID);

    //生成所有的单价文件
    await importUnitPriceFiles(mainData,projectIDMap,unitPriceFileIDMap,userID);


    return [constructionProjectID,projectIDMap,labourCoeFileIDMap,calcProgramFileIDMap]
}


async function importUnitPriceFiles(mainData,projectIDMap,unitPriceFileIDMap,userID) {
    if(!mainData.files.unitFiles) return;
    let unitFiles = [],unitPrices =[],mixRatios=[],freights=[],originals=[],com_electrovalences=[];
    for(let f of mainData.files.unitFiles){
        let file = f.unitFile,prices = f.unitPrices,mixs = f.mixRatios,fres = f.freights,ors = f.originals,ec=f.com_electrovalence;
        //生成单价文件
        delete file._id;
        file.id = unitPriceFileIDMap[file.id]?unitPriceFileIDMap[file.id]:await getCounterID("unit_price_file");
        file.project_id = projectIDMap[file.project_id];
        file.root_project_id = projectIDMap[file.root_project_id];
        file.user_id = userID;
        unitFiles.push(file);

        //生成子数据
        if(prices) await  setSubList(prices,unitPrices,file.id,unitPriceModel);
        if(mixs) await setSubList(mixs,mixRatios,file.id,mixRatioModel);
        if(fres) await setSubList(fres,freights,file.id,freightModel,true);
        if(ors) await setSubList(ors,originals,file.id,originalModel,true);
        if(ec) await setSubList(ec,com_electrovalences,file.id,comElectrovalenceModel,true);
    }
    if(unitFiles.length > 0) await insertMany(unitFiles,unitPriceFileModel);
    if(unitPrices.length > 0) await insertMany(unitPrices,unitPriceModel);
    if(mixRatios.length > 0) await insertMany(mixRatios,mixRatioModel);
    if(freights.length > 0) await insertMany(freights,freightModel);
    if(originals.length > 0) await insertMany(originals,originalModel);
    if(com_electrovalences.length > 0) await insertMany(com_electrovalences,comElectrovalenceModel);
    async function setSubList(oList,nList,fileID,model,UUID=false) {
        if(UUID == true){
          for(let o of oList){
            delete o._id;
            o.unit_price_file_id = fileID;
            o.ID = uuidV1();
            if(o.ration_gljs){
              for(let rg of o.ration_gljs){
                rg.ID = uuidV1();
              }
            }
            nList.push(o)
          }
        }else{
          let IDcounter = await counter.counterDAO.getIDAfterCountSync(model.modelName, oList.length);
          let firstID = IDcounter.sequence_value - (oList.length - 1);
            for(let o of oList){
                delete o._id;
                o.unit_price_file_id = fileID;
                o.id = firstID;
                firstID+=1
                nList.push(o)
            }
        }
        
    }


}


async function importFeeRateFiles(mainData,projectIDMap,feeRateFileIDMap,userID) {
    let feeRateFiles = [], feeRates = [];
    if(!mainData.files.feeRateFiles) return;
    for(let f of mainData.files.feeRateFiles){
        //生成费率记录
        let rate = f.feeRate;
        delete rate._id;
        rate.ID = uuidV1();
        feeRates.push(rate);

        //生成费率文件记录
        let file = f.feeRateFile;
        delete file._id;
        file.ID = feeRateFileIDMap[file.ID]?feeRateFileIDMap[file.ID]:uuidV1();
        file.userID = userID;
        file.rootProjectID = projectIDMap[file.rootProjectID];
        file.feeRateID = rate.ID;
        feeRateFiles.push(file);
    }
    if(feeRateFiles.length > 0) await insertMany(feeRateFiles,feeRateFileModel);
    if(feeRates.length > 0) await insertMany(feeRates,feeRateModel);
}

// 检查单价、费率文件，处理rootProjectID为-1的旧数据（以前的初始化例题bug造成）
async function checkFiles(projects) {
    // fileID - tenderID 映射
    const mapping = {};
    const tenderItems = [];
    const unitFileIDs = [];
    const rateFileIDs = [];
    const task = [];
    projects.forEach(project => {
        const isErrorRootProjectTender = project.projType === projectType.tender && project.property.rootProjectID === -1;
        if (isErrorRootProjectTender) {
            tenderItems.push({ ID: project.ID, ParentID: project.ParentID });
            const priceFileID = project.property.unitPriceFile.id;
            const feeFileID = project.property.feeFile.id;
            if (!mapping[priceFileID]) {
                mapping[priceFileID] = project;
                unitFileIDs.push(priceFileID);
            }
            if (!mapping[feeFileID]) {
                mapping[feeFileID] = project;
                rateFileIDs.push(feeFileID);
            }
        }
    });
    // 检查是否存在rootProjectID为-1的
    const errorUnitFiles = await unitPriceFileModel.find({id: {$in: unitFileIDs}, root_project_id: -1});
    const errorRateFiles = await feeRateFileModel.find({ID: {$in: rateFileIDs}, rootProjectID: -1});
    const unitFileTask = errorUnitFiles.map(file => {
        const rootProjectID = mapping[file.id].ParentID;
        return {
            updateOne: {
                filter: {id: file.id},
                update: {$set: {root_project_id: rootProjectID}}
            }
        };
    });
    if (unitFileTask.length) {
        task.push(unitPriceFileModel.bulkWrite(unitFileTask));
    }
    const rateFileTask = errorRateFiles.map(file => {
        const rootProjectID = mapping[file.ID].ParentID;
        return {
            updateOne: {
                filter: {ID: file.ID},
                update: {$set: {rootProjectID: rootProjectID}}
            }
        };
    });
    if (rateFileTask.length) {
        task.push(feeRateFileModel.bulkWrite(rateFileTask));
    }
    const tenderTask = tenderItems.map(item => {
        return {
            updateOne: {
                filter: {ID: item.ID},
                update: {$set: {'property.rootProjectID': item.ParentID}}
            }
        }
    });
    if (tenderTask.length) {
        task.push(projectModel.bulkWrite(tenderTask));
    }
    await Promise.all(task);
}

function uploadToken() {
    let options = {
        scope: qiniu_config.Bucket,
        deleteAfterDays: 1,
        returnBody:
            '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}'
    };
    let putPolicy = new qiniu.rs.PutPolicy(options);
    let token = putPolicy.uploadToken(mac);
    let result = {
            uptoken: token,
            domain: qiniu_config.Domain
        }
    return result
}

// 获取系统设置，这个系统设置正常情况下有存在session中
async function getSystemSetting() {
    return await systemSettingModel.findOne({}).lean();
}

// 有些方法无法通过中间件就检查单位工程数量是否超限
// 需要到具体的业务代码中进行判断
// 这个方法就是具体业务代码中，需要检查单位工程数量是否超限用
async function isTenderOverrun(tenderCount, session) {
    const userID = session.sessionUser.id;
    const compilation = session.sessionCompilation._id;
    const compilationVersion = session.compilationVersion || '免费';
    let systemSetting = session.systemSetting || (session.systemSetting = await getSystemSetting());
    // 这种情况只有在刚上线此功能时会出现，不考虑时间差
    if (!systemSetting) {
        return false;
    }
    const type = compilationVersion.includes('免费') ? 'normal' : 'professional';
    const limit = systemSetting[type].project;
    const curTenderCount = await projectModel.count({userID, compilation, projType: 'Tender', '$or':[{deleteInfo: null}, {'deleteInfo.completeDeleted': false}]});
    return tenderCount + curTenderCount > limit;
}

async function getWelcomeInfo(compilationId,sessionUser,isFree=true) {
    let welcom_setting = await welcomeModel.findOne({compilationId:compilationId});
    let isShow = false;
    let context = "";
    let showTime = 0;
    let setting = null
    if(welcom_setting){
      setting = isFree? welcom_setting.normal:welcom_setting.professional;//区分专业版和免费版
    }

    if(setting){
        if(setting.showType == 0) return [false,"",showTime];//关闭
        context = setting.context;
        showTime = setting.showTime && setting.showTime!=""?setting.showTime:0;
        if(setting.showType == 1){//每天一次
            let dataString =moment(Date.now()).tz("Asia/Shanghai").format('YYYY-MM-DD');//今天的日期字符串
            let userInfo =await userModel.findOne({_id: mongoose.Types.ObjectId(sessionUser.id)});
            if (dataString != userInfo.welcomeShowTime){//今天没登录过
                isShow = true;
                await userModel.update({_id: mongoose.Types.ObjectId(sessionUser.id)},{welcomeShowTime:dataString})
            }
        }else if(setting.showType == 2){//每次登录显示
            isShow = sessionUser.newLogin?true:false;
            sessionUser.newLogin = false
        }

    }

    return [isShow,context,showTime]

}