| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 | /** * Created by Zhong on 2017/12/21. */const mongoose = require('mongoose');const compleRationSectionTreeModel = mongoose.model('complementary_ration_section_tree');const compleRationModel = mongoose.model('complementary_ration_items');const installSectionModel = mongoose.model("std_ration_lib_installationSection");const installFeeItemModel = mongoose.model("std_ration_lib_installation");const complementaryGljModel = mongoose.model('complementary_glj_lib');const stdGljModel = mongoose.model('std_glj_lib_gljList');import async from 'async';let stdRationModel = require ('../../ration_repository/models/ration_item').Model;let counter = require('../../../public/counter/counter');const scMathUtil = require('../../../public/scMathUtil').getUtil();class CompleRatoinDao {    async updateRation(userID, compilationId, updateData, callback){        try{            for(let i = 0, len = updateData.length; i < len; i++){                let updateObj = updateData[i];                if(updateObj.updateType === 'new'){                    updateObj.updateData.userID = userID;                    updateObj.updateData.compilationId = compilationId;                    await compleRationModel.create(updateObj.updateData);                }                else if(updateObj.updateType === 'update'){                    await compleRationModel.update({userID: userID, rationRepId: updateObj.updateData.rationRepId}, updateObj.updateData);                }            }            callback(0, '');        }        catch(err){            callback(err, null);        }    }    async getRationItems(sectionId, callback){        try{            let compleRations = await compleRationModel.find({sectionId: sectionId, deleteInfo: null});            callback(0, compleRations);        }        catch(err){            callback(err, null);        }    }    async getRationsCodes(userID, rationRepId, callback){        try{            let stdRationCodes = await stdRationModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]}, '-_id code');            let compleRationCodes = await compleRationModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null}, '-_id code');            let rstCodes = [];            stdRationCodes.concat(compleRationCodes).forEach(function (rationItem) {                rstCodes.push(rationItem.code);            });            callback(0, rstCodes);        }        catch(err){            callback(err, null);        }    }    async getGljItems(gljLibId, callback){        try{            let stdGljs = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]});            callback(0, stdGljs);        }        catch(err){            callback(err, null);        }    }    async getGljItemsOccupied(gljLibId, occupation, callback){        try{            let stdGls = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]}, occupation);            callback(0, stdGls);        }        catch (err){            callback(err, null);        }    }    async getGljItemsByIds(userID, ids, callback){        try{            let rst = [];            for(let i = 0, len = ids.length; i < len; i++){                if(ids[i].type === 'std'){                    let stdGlj = await stdGljModel.find({ID: ids[i].id, deleteInfo: null});                    if(stdGlj.length > 0){                        stdGlj[0]._doc.type = 'std';                        rst.push(stdGlj[0]);                    }                }                else if(ids[i].type === 'complementary'){                    let compleGlj = await complementaryGljModel.find({userId: userID, ID: ids[i].id, deleteInfo: null});                    if(compleGlj.length > 0){                        compleGlj[0]._doc.type = 'complementary';                        rst.push(compleGlj[0]);                    }                }            }            callback(0, rst);        }        catch(err){            callback(err, null);        }    }    async getGljItemsByCodes(userID, compilationId, rationRepId, codes, callback){        try{            let rst = [];            for(let i = 0, len = codes.length; i < len; i++){                let stdGlj = await stdGljModel.find({repositoryId: rationRepId, code: codes[i]});                if(stdGlj.length > 0){                    stdGlj[0]._doc.type = 'std';                    rst.push(stdGlj[0]);                }                else {                    let compleGlj = await complementaryGljModel.find({userId: userID, compilationId: compilationId, code: codes[i]});                    if(compleGlj.length > 0){                        compleGlj[0]._doc.type = 'complementary';                        rst.push(compleGlj[0]);                    }                }            }            callback(0, rst);        }        catch(err){            callback(err, null);        }    }    //根据章节树获取补充定额    async getCompleRationBySection(userId, sectionId) {        const deleteQuery = [{deleteInfo: null}, {'deleteInfo.deleted': false}];        let compleRations = await compleRationModel.find({sectionId: sectionId, $or: deleteQuery});        for(let ration of compleRations){            ration._doc.type = 'complementary';            let hintsArr = [];            let stdGljIds = [],                comGljIds = [],                stdGljs = [],                comGljs = [];            let gljAmtMapping = {};            for(let rationGlj of ration.rationGljList){                gljAmtMapping[rationGlj.gljId] = rationGlj.consumeAmt;                if(!isDef(rationGlj.type) || rationGlj.type === 'std'){                    stdGljIds.push(rationGlj.gljId);                }                else {                    comGljIds.push(rationGlj.gljId);                }            }            if(stdGljIds.length > 0) {                stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}});            }            if(comGljIds.length > 0) {                comGljs = await complementaryGljModel.find({userId: userId, ID: {$in: comGljIds}});            }            let gljDatas = stdGljs.concat(comGljs);            gljDatas.sort(function (a, b) {                let aV = a.gljType + a.code,                    bV = b.gljType + b.code;                if(aV > bV) {                    return 1;                } else if (aV < bV) {                    return -1;                }                return 0;            });            for(let glj of gljDatas){                hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? '   ' + glj.specs : ''}   ${glj.unit}   ${gljAmtMapping[glj.ID]}`)            }            hintsArr.push(`基价 元 ${ration.basePrice}`);            if(ration.jobContent && ration.jobContent.toString().trim() !== ''){                hintsArr.push(`工作内容:`);                hintsArr = hintsArr.concat(ration.jobContent.split('\n'));            }            if(ration.annotation && ration.annotation.toString().trim() !== ''){                hintsArr.push(`附注:`);                hintsArr = hintsArr.concat(ration.annotation.split('\n'));            }            ration._doc.hint = hintsArr.join('<br>');        }        return compleRations;    }    //造价书定额库 根据章节树过去标准定额    async getRationGljItemsBySection(sectionId, callback){        let stdRations = await stdRationModel.find({sectionId: sectionId});        for(let ration of stdRations){            ration._doc.type = 'std';        }        function sortByCode(arr) {            function recurCompare(a, b, index){                if (a[index] && !b[index]) {                    return 1;                } else if (!a[index] && b[index]) {                    return -1;                } else if (a[index] && b[index]) {                    let aV = a[index],                        bV = b[index];                    if (!isNaN(aV) && !isNaN(bV)) {                        aV = parseFloat(a[index]);                        bV = parseFloat(b[index]);                    }                    if (aV > bV) {                        return 1;                    } else if (aV < bV) {                        return -1;                    } else {                        return recurCompare(a, b, index + 1);                    }                }                return 0;            }            arr.sort(function (a, b) {                let aArr = a.code.split('-'),                    bArr = b.code.split('-');                return recurCompare(aArr, bArr, 0);            });        }        /*stdRations.sort(function (a, b) {            let rst = 0;            if(a.code > b.code){                rst = 1;            }            else if(a.code < b.code){                rst = -1;            }            return rst;        });*/        sortByCode(stdRations);        for(let ration of stdRations){            let hintsArr = [];            let stdGljIds = [],                stdGljs = [];            let gljAmtMapping = {};            for(let rationGlj of ration.rationGljList){                gljAmtMapping[rationGlj.gljId] = rationGlj.consumeAmt;                stdGljIds.push(rationGlj.gljId);            }            if(stdGljIds.length > 0) {                stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}});            }            let gljDatas = stdGljs;            gljDatas.sort(function (a, b) {                let aV = a.gljType + a.code,                    bV = b.gljType + b.code;                if(aV > bV) {                    return 1;                } else if (aV < bV) {                    return -1;                }                return 0;            });            for(let glj of gljDatas){                hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? '   ' + glj.specs : ''}   ${glj.unit}   ${gljAmtMapping[glj.ID]}`)            }            hintsArr.push(`基价 元 ${ration.basePrice}`);            if(ration.jobContent && ration.jobContent.toString().trim() !== ''){                hintsArr.push(`工作内容:`);                hintsArr = hintsArr.concat(ration.jobContent.split('\n'));            }            if(ration.annotation && ration.annotation.toString().trim() !== ''){                hintsArr.push(`附注:`);                hintsArr = hintsArr.concat(ration.annotation.split('\n'));            }            ration._doc.hint = hintsArr.join('<br>');        }        return stdRations;    }    updateRationBasePrc(userID, basePrcArr, callback){        let me  = this;        async.each(basePrcArr, function (basePrcObj, finalCb) {            let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;            async.waterfall([                function (cb) {                    if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){                        //补充定额                        compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {                            if(err){                                cb(err);                            }                            else {                                compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {                                    if(err){                                        cb(err);                                    }                                    else {                                        cb(null, compleRst);                                    }                                });                            }                        });                    }                    else{                        compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {                            if(err){                                cb(err);                            }                            else {                                cb(null, compleRst);                            }                        });                    }                },                function (result, cb) {                    async.each(result, function (rationItem, ecb) {                        let rationGljList = rationItem.rationGljList,                            gljIds = [];                        rationGljList.forEach(function (rationGlj) {                            let idObj = Object.create(null);                            idObj.id = rationGlj.gljId;                            idObj.type = rationGlj.type;                            gljIds.push(idObj);                        });                        me.getGljItemsByIds(userID, gljIds, function(err, gljItems){                            if(err){                                ecb(err);                            }                            else{                                let gljArr = [];                                for(let i=0; i<gljItems.length; i++){                                    let gljParentType = -1;                                    if(gljItems[i].ID === adjGljId){                                        gljItems[i].gljType = adjGljType;                                    }                                    if(gljItems[i].gljType <= 3){                                        gljParentType = gljItems[i].gljType;                                    }                                    if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){                                        gljParentType = 2;                                    }                                    if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){                                        gljParentType = 3;                                    }                                    if(gljItems[i].ID === adjGljId){                                        gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});                                    }                                    else {                                        gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});                                    }                                }                                gljArr.forEach(function (gljItem) {                                    rationGljList.forEach(function (rationGlj) {                                        if(gljItem.gljId === rationGlj.gljId){                                            gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);                                        }                                    })                                });                                //recalculate the price of ration                                let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};                                gljArr.forEach(function (gljItem) {                                    if(gljItem.gljParentType !== -1){                                        singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);                                        if(gljItem.gljParentType === 1){                                            labourPrc.push(singlePrc);                                        }                                        else if(gljItem.gljParentType ===2){                                            materialPrc.push(singlePrc);                                        }                                        else{                                            machinePrc.push(singlePrc);                                        }                                    }                                });                                if(labourPrc.length > 0){                                    let sumLaP = 0;                                    for(let i=0; i<labourPrc.length; i++){                                        sumLaP += labourPrc[i];                                    }                                    updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);                                }                                if(materialPrc.length > 0){                                    let sumMtP = 0;                                    for(let i= 0; i<materialPrc.length; i++){                                        sumMtP += materialPrc[i];                                    }                                    updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);                                }                                if(machinePrc.length > 0){                                    let sumMaP = 0;                                    for(let i =0; i< machinePrc.length; i++){                                        sumMaP += machinePrc[i];                                    }                                    updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);                                }                                updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);                                //updateDataBase                                compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),                                        machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},                                    function (err, result) {                                        if(err){                                            ecb(err);                                        }                                        else {                                            ecb(null);                                        }                                    });                            }                        });                    }, function(err){                        if(err){                            cb(err);                        }                        else {                            cb(null);                        }                    });                },            ], function (err) {                if(err){                    finalCb(err);                }                else{                    finalCb(null);                }            });        }, function (err) {            if(err){                callback(err, 'Error');            }            else{                callback(0, '');            }        });    }    mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){        let me = this;        if (updateItems.length == 0 && rIds.length == 0) {            me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);        } else {            me.removeRationItems(rationLibId, rIds, function(err, message, docs) {                if (err) {                    callback(true, false);                } else {                    me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){                        if (err) {                            callback(true, false);                        } else {                            if (addItems && addItems.length > 0) {                                me.addRationItems(rationLibId, sectionId, addItems, callback);                            } else {                                callback(0, results);                            }                        }                    });                }            })        }    }     removeRationItems(rationRepId, rIds,callback){        if (rIds.length > 0) {            compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){                if (err) {                    callback(true, false);                } else {                    callback(0, docs);                }            })        } else {            callback(0,  null);        }    }     addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){        if (items && items.length > 0) {            counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){                let maxId = result.sequence_value;                let arr = [];                for (let i = 0; i < items.length; i++) {                    let obj = new compleRationModel(items[i]);                    obj.ID = (maxId - (items.length - 1) + i);                    obj.sectionId = sectionId;                    obj.rationRepId = rationLibId;                    obj.userId = userID;                    obj.compilationId = compilationId;                    arr.push(obj);                }                compleRationModel.collection.insert(arr, null, function(err, docs){                    if (err) {                        callback(true, false);                    } else {                        callback(0, docs);                    }                })            });        } else {            callback(true, "Source error!", false);        }    }     updateRationItems(userID, rationLibId, sectionId, items,callback){        let functions = [];        for (let i=0; i < items.length; i++) {            functions.push((function(doc) {                return function(cb) {                    var filter = {};                    if (doc.ID) {                        filter.ID = doc.ID;                    } else {                        filter.sectionId = sectionId;                        filter.userId = userID;                        if (rationLibId) filter.rationRepId = rationLibId;                        filter.code = doc.code;                    }                    compleRationModel.update(filter, doc, cb);                };            })(items[i]));        }        async.parallel(functions, function(err, results) {            if(!err){                err = 0;            }            callback(err, results);        });    }    async getInstallation(rationRepId, callback){        try {            let feeItems = await installFeeItemModel.find({rationRepId: rationRepId, $or: [{deleted: false}, {deleted: null}]});            for(let feeItem of feeItems){                let sids = [];                for(let sec of feeItem.section){                    sids.push(sec.ID);                }                if(sids.length > 0){                    let sections = await installSectionModel.find({ID: {$in: sids}, $or: [{deleted: false}, {deleted: null}]});                    feeItem._doc.section = sections;                }            }            callback(0, feeItems);        }        catch(err){            callback(err, null);        }    }}function isDef(v){    return v !== undefined && v !== null;}export default CompleRatoinDao;
 |