| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 | 
							- /**
 
-  * Created by Zhong on 2017/8/22.
 
-  */
 
- const mongoose = require('mongoose');
 
- const complementaryGljModel = mongoose.model('complementary_glj_lib');
 
- const stdGljModel = mongoose.model('std_glj_lib_gljList');
 
- const gljClassModel = mongoose.model('std_glj_lib_gljClass');
 
- const compleClassModel = mongoose.model('complementary_glj_section');
 
- import counter from "../../../public/counter/counter";
 
- import async from "async";
 
- import STDGLJLibGLJListModel from "../../common/std/std_glj_lib_glj_list_model";
 
- class GljDao {
 
-     getGljTypes (gljLibId, callback){
 
-         gljClassModel.find({"repositoryId": gljLibId},function(err,data){
 
-             if(data.length) {
 
-                 callback(0,data);
 
-             }
 
-             else  if(err) callback("获取人材机类型错误!",false);
 
-         })
 
-     }
 
-     _exist(data, attr){
 
-         return data && data[attr] !== 'undefined' && data[attr];
 
-     }
 
-     sortToNumber(datas){
 
-         for(let i = 0, len = datas.length; i < len; i++){
 
-             let data = datas[i]._doc;
 
-             if(this._exist(data, 'basePrice')){
 
-                 data['basePrice'] = parseFloat(data['basePrice']);
 
-             }
 
-             if(this._exist(data, 'component')){
 
-                 for(let j = 0, jLen = data['component'].length; j < jLen; j++){
 
-                     let comGljObj = data['component'][j]._doc;
 
-                     if(this._exist(comGljObj, 'consumeAmt')){
 
-                         comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-     }
 
-     //获得用户的补充工料机和用户当前所在编办的标准工料机
 
-     async getGljItems (stdGljLibId, userId, compilationId, callback){
 
-         let me = this;
 
-         let rst = {stdGljs: [], complementaryGljs: []};
 
-         //批量获取异步
 
-        /* let functions = [];
 
-         let count = await stdGljModel.find({repositoryId: stdGljLibId, $or: [{deleted: null}, {deleted: false}]}).count();
 
-         let findCount = Math.ceil(count/500);
 
-         for(let i = 0, len = findCount; i < len; i++){
 
-             functions.push((function(flag) {
 
-                 return function (cb) {
 
-                     stdGljModel.find({repositoryId: stdGljLibId, deleted: null}, cb).skip(flag).sort({ID: 1}).limit(500);
 
-                 }
 
-             })(i*500));
 
-         }
 
-         async.parallel(functions,  function (err, results) {
 
-             if(err){
 
-                 callback(err, null);
 
-             }
 
-             else{
 
-                 for(let stdGljs of results){
 
-                     rst.stdGljs = rst.stdGljs.concat(stdGljs);
 
-                 }
 
-                 callback(0, rst);
 
-             }
 
-         });*/
 
-         async.parallel([
 
-            async function (cb) {
 
-                try{
 
-                    let stdGljs = [];
 
-                    let first = await stdGljModel.find({repositoryId: stdGljLibId}).sort({ID: 1}).limit(1);
 
-                    let count = await stdGljModel.find({repositoryId: stdGljLibId, $or: [{deleted: null}, {deleted: false}]}).count();
 
-                    let findCount = Math.ceil(count/500);
 
-                    let flag = first[0].ID;
 
-                    //let flag = 0;
 
-                    //批量获取,非skip
 
-                    for(let i = 0, len = findCount; i < len; i++){
 
-                        let tempStdGlj;
 
-                        if(i === 0){
 
-                            tempStdGlj = await stdGljModel.find({repositoryId: stdGljLibId, deleted: null, ID: {$gte: flag}}).sort({ID: 1}).limit(500);
 
-                            if(tempStdGlj.length > 0){
 
-                                flag = tempStdGlj[tempStdGlj.length - 1].ID;
 
-                            }
 
-                        }
 
-                        else {
 
-                            tempStdGlj = await stdGljModel.find({repositoryId: stdGljLibId, deleted: null, ID: {$gt: flag}}).sort({ID: 1}).limit(500);
 
-                            if(tempStdGlj.length > 0){
 
-                                flag = tempStdGlj[tempStdGlj.length - 1].ID;
 
-                            }
 
-                        }
 
-                        if(tempStdGlj){
 
-                            stdGljs = stdGljs.concat(tempStdGlj);
 
-                        }
 
-                    }
 
-                    me.sortToNumber(stdGljs);
 
-                    rst.stdGljs = stdGljs;
 
-                    cb(null);
 
-                }
 
-                catch (err){
 
-                    cb(err);
 
-                }
 
-             },
 
-             function (cb) {
 
-                 complementaryGljModel.find({userId: userId, compilationId: compilationId}, function (err, complementaryGljs) {
 
-                     if(err){
 
-                         cb(err);
 
-                     }
 
-                     else{
 
-                         me.sortToNumber(complementaryGljs);
 
-                         rst.complementaryGljs = complementaryGljs;
 
-                         cb(null);
 
-                     }
 
-                 });
 
-             }
 
-         ], function (err) {
 
-             if(err){
 
-                 callback(err, null);
 
-             }
 
-             else{
 
-                 callback(0, rst);
 
-             }
 
-         })
 
-     };
 
-     getGljItemsByCode (repositoryId, codes, callback){
 
-         gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
 
-             if(err) callback(true, "")
 
-             else callback(false, data);
 
-         })
 
-     };
 
-     updateComponent(userId, updateArr, callback){
 
-         let parallelFucs = [];
 
-         for(let i = 0; i < updateArr.length; i++){
 
-             parallelFucs.push((function(obj){
 
-                 return function (cb) {
 
-                     if(typeof obj.component === 'undefined'){
 
-                         obj.component = [];
 
-                     }
 
-                     complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
 
-                         if(err){
 
-                             cb(err);
 
-                         }
 
-                         else{
 
-                             cb(null, obj);
 
-                         }
 
-                     })
 
-                 }
 
-             })(updateArr[i]));
 
-         }
 
-         async.parallel(parallelFucs, function (err, result) {
 
-             if(err){
 
-                 callback(err, '更新组成物错误!', null);
 
-             }
 
-             else{
 
-                 callback(0, '成功!', result);
 
-             }
 
-         });
 
-     }
 
-     //-oprtor
 
-     mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
 
-         if (updateItems.length == 0 && rIds.length == 0) {
 
-             GljDao.addGljItems(userId, compilationId, addItems, callback);
 
-         }
 
-         else if(rIds.length > 0 && updateItems.length > 0){
 
-             async.parallel([
 
-                 function (cb) {
 
-                     GljDao.removeGljItems(rIds, cb);
 
-                 },
 
-                 function (cb) {
 
-                     GljDao.updateGljItems(userId, compilationId, updateItems, cb);
 
-                 }
 
-             ], function (err) {
 
-                 if(err){
 
-                     callback(true, "Fail to update and delete", false)
 
-                 }
 
-                 else{
 
-                     callback(false, "Save successfully", false);
 
-                 }
 
-             })
 
-         }
 
-         else if (rIds.length > 0 && updateItems.length === 0) {
 
-             GljDao.removeGljItems(rIds, callback);
 
-         }
 
-         else if(updateItems.length > 0 || addItems.length > 0){
 
-             GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
 
-                 if (err) {
 
-                     callback(true, "Fail to update", false);
 
-                 } else {
 
-                     if (addItems && addItems.length > 0) {
 
-                         GljDao.addGljItems(userId, compilationId, addItems, callback);
 
-                     } else {
 
-                         callback(false, "Save successfully", results);
 
-                     }
 
-                 }
 
-             });
 
-         }
 
-     }
 
-     static removeGljItems (rIds, callback) {
 
-         if (rIds && rIds.length > 0) {
 
-             complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
 
-                 if (err) {
 
-                     callback(true, "Fail to remove", false);
 
-                 } else {
 
-                     callback(false, "Remove successfully", docs);
 
-                 }
 
-             })
 
-         } else {
 
-             callback(false, "No records were deleted!", null);
 
-         }
 
-     }
 
-     static addGljItems (userId, compilationId, items, callback) {
 
-         if (items && items.length > 0) {
 
-             counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
 
-                 var maxId = result.sequence_value;
 
-                 var arr = [];
 
-                 for (var i = 0; i < items.length; i++) {
 
-                     var obj = new complementaryGljModel(items[i]);
 
-                     obj.ID = (maxId - (items.length - 1) + i);
 
-                     obj.userId = userId;
 
-                     obj.compilationId = compilationId;
 
-                     arr.push(obj);
 
-                 }
 
-                 complementaryGljModel.collection.insert(arr, null, function(err, docs){
 
-                     if (err) {
 
-                         callback(true, "Fail to add", false);
 
-                     } else {
 
-                         callback(false, "Add successfully", docs);
 
-                     }
 
-                 });
 
-             });
 
-         } else {
 
-             callback(true, "No source", false);
 
-         }
 
-     }
 
-     static updateGljItems(userId, compilationId, items, callback) {
 
-         var functions = [];
 
-         for (var i=0; i < items.length; i++) {
 
-             functions.push((function(doc) {
 
-                 return function(cb) {
 
-                     var filter = {};
 
-                     if (doc.ID) {
 
-                         filter.ID = doc.ID;
 
-                     } else {
 
-                         filter.userId = userId;
 
-                         filter.compilationId = compilationId;
 
-                         filter.code = doc.code;
 
-                     }
 
-                     complementaryGljModel.update(filter, doc, cb);
 
-                 };
 
-             })(items[i]));
 
-         }
 
-         async.parallel(functions, function(err, results) {
 
-             callback(err, results);
 
-         });
 
-     }
 
-     /**
 
-      * 获取组成物数据
 
-      *
 
-      * @param {Number} gljId
 
-      * @return {Promise}
 
-      */
 
-     async getComponent(gljId) {
 
-         let result = [];
 
-         let libGljData = await complementaryGljModel.findOne({ID: gljId});
 
-         if (libGljData === null || libGljData.component.length <= 0) {
 
-             return result;
 
-         }
 
-         // 标准工料机库
 
-         let componentIdListStd = [];
 
-         // 补充工料机库
 
-         let componentIdListCpt = [];
 
-         let componentConsume = {};
 
-         // 整理数据
 
-         for (let component of libGljData.component) {
 
-             if (component.isStd) {
 
-                 componentIdListStd.push(component.ID);
 
-             } else {
 
-                 componentIdListCpt.push(component.ID);
 
-             }
 
-             let isStdFlag = component.isStd ? 'std' : 'cpt';
 
-             //对于补充人材机的组成物,不会有多单价的情况
 
-             componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
 
-         }
 
-         // 查找标准库数据
 
-         let condition = {};
 
-         let componentStdGljData = [];
 
-         if (componentIdListStd.length > 0) {
 
-             let gljListModel = new STDGLJLibGLJListModel();
 
-             condition = {ID: {$in: componentIdListStd}};
 
-             componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
 
-         }
 
-         // 查找补充库数据
 
-         let componentCptGljData = [];
 
-         if (componentIdListCpt.length > 0) {
 
-             condition = {ID: {$in: componentIdListCpt}};
 
-             componentCptGljData = await complementaryGljModel.find(condition);
 
-         }
 
-         if (componentCptGljData === null && componentStdGljData === null) {
 
-             return result;
 
-         }
 
-         // 整理数据
 
-         if (componentStdGljData.length > 0) {
 
-             componentStdGljData = JSON.stringify(componentStdGljData);
 
-             componentStdGljData = JSON.parse(componentStdGljData);
 
-             for(let gljData of componentStdGljData) {
 
-                 gljData.connectCode = libGljData.code;
 
-                 gljData.consumption = componentConsume[gljData.ID + '_std'];
 
-                 gljData.from='std';
 
-                 result.push(gljData);
 
-             }
 
-         }
 
-         if (componentCptGljData.length > 0) {
 
-             componentCptGljData = JSON.stringify(componentCptGljData);
 
-             componentCptGljData = JSON.parse(componentCptGljData);
 
-             for(let gljData of componentCptGljData) {
 
-                 gljData.connectCode = libGljData.code;
 
-                 gljData.consumption = componentConsume[gljData.ID + '_cpt'];
 
-                 gljData.from='cpt';
 
-                 result.push(gljData);
 
-             }
 
-         }
 
-         return result;
 
-     }
 
-     async getMixedTree(gljLibId, userId, compilationId){
 
-         let rst = {std: [], comple: []};
 
-         rst.std = await gljClassModel.find({repositoryId: gljLibId});
 
-         rst.comple = await compleClassModel.find({userId: userId, compilationId: compilationId});
 
-         return rst;
 
-     }
 
- }
 
- export default GljDao;
 
 
  |