/** * Created by Zhong on 2017/8/11. */ import {gljMapModel, gljModel, gljClassModel, gljClassTemplateModel} from "./schemas"; import {OprDao} from "./gljMapModel"; import moment from "moment"; import counter from "../../../public/counter/counter"; import async from "async"; class GljDao extends OprDao{ getGljTypes (gljLibId, callback){ gljClassModel.find({"repositoryId": gljLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){ if(data.length) { callback(false,data); } else if(err) callback("获取工料机类型错误!",false) else { gljClassTemplateModel.find({'$or': [{isDeleted: null}, {isDeleted: false}]}, function (err, datas) { if(err){ callback("获取工料机类型错误!", false); } else{ let rst = []; async.each(datas, function (data, cb) { let newClassObj = {}; newClassObj.ID = data.ID; newClassObj.ParentID = data.ParentID; newClassObj.NextSiblingID = data.NextSiblingID; newClassObj.Name = data.Name; newClassObj.repositoryId = gljLibId; gljClassModel.create(newClassObj, function(err){ if(err)cb(err); else{ rst.push(newClassObj); cb(null); } }); }, function (err) { if(err) callback("新增工料机类型错误!", false); else callback(false, rst); }); } }); } }) } getGljItemsByRep(repositoryId,callback){ gljModel.find({"repositoryId": repositoryId},function(err,data){ if(err) callback(true, "") else callback(false,data); }) } getGljItemByType (repositoryId, type, callback){ gljModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){ if(err) callback(true, "") else callback(false, data); }) }; getGljItem (repositoryId, code, callback){ gljModel.find({"repositoryId": repositoryId, "code": code},function(err,data){ if(err) callback(true, "") else callback(false, data); }) }; getGljItems (gljIds, callback){ gljModel.find({"ID": {"$in": gljIds}},function(err,data){ if(err) callback(true, "") else callback(false, data); }) }; getGljItemsByCode (repositoryId, codes, callback){ gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){ if(err) callback(true, "") else callback(false, data); }) }; updateComponent(libId, oprtor, 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 = []; } gljModel.update({repositoryId: libId, ID: obj.ID}, obj, function (err, result) { if(err){ cb(err); } else{ cb(null, obj); } }) } })(updateArr[i])); } parallelFucs.push((function () { return function (cb) { GljDao.updateOprArr({ID: libId}, oprtor, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ cb(err); } else{ cb(null); } }) } })()); async.parallel(parallelFucs, function (err, result) { if(err){ callback(err, '更新组成物错误!', null); } else{ callback(null, '成功!', result); } }); } mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) { if (updateItems.length == 0 && rIds.length == 0) { GljDao.addGljItems(repId, lastOpr, addItems, callback); } else if(rIds.length > 0 && updateItems.length > 0){ async.parallel([ function (cb) { GljDao.removeGljItems(repId, lastOpr, rIds, cb); }, function (cb) { GljDao.updateGljItems(repId, lastOpr, 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(repId, lastOpr, rIds, callback); } else if(updateItems.length > 0 || addItems.length > 0){ GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){ if (err) { callback(true, "Fail to update", false); } else { if (addItems && addItems.length > 0) { GljDao.addGljItems(repId, lastOpr, addItems, callback); } else { callback(false, "Save successfully", results); } } }); } } /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) { if (updateItems.length == 0 && rIds.length == 0) { GljDao.addGljItems(repId, lastOpr, addItems, callback); } else if (rIds.length > 0) { GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) { }); }else{ GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){ if (err) { callback(true, "Fail to update", false); } else { if (addItems && addItems.length > 0) { GljDao.addGljItems(repId, lastOpr, addItems, callback); } else { callback(false, "Save successfully", results); } } }); } };*/ static removeGljItems (repId, lastOpr, rIds, callback) { if (rIds && rIds.length > 0) { gljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){ if (err) { callback(true, "Fail to remove", false); } else { GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ callback(true, "Fail to update operator", false); } else{ callback(false, "Remove successfully", docs); } }); } }) } else { callback(false, "No records were deleted!", null); } } static addGljItems (repId, lastOpr, items, callback) { if (items && items.length > 0) { counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){ var maxId = result.value.sequence_value; var arr = []; for (var i = 0; i < items.length; i++) { var obj = new gljModel(items[i]); obj.ID = (maxId - (items.length - 1) + i); obj.repositoryId = repId; arr.push(obj); } gljModel.collection.insert(arr, null, function(err, docs){ if (err) { callback(true, "Fail to add", false); } else { GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ callback(true, "Fail to update Operator", false); } else{ callback(false, "Add successfully", docs); } }); } }) }); } else { callback(true, "No source", false); } } static updateGljItems(repId, lastOpr, 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.repositoryId = repId; filter.code = doc.code; } gljModel.update(filter, doc, cb); }; })(items[i])); } functions.push((function () { return function (cb) { GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ cb(err); } else{ cb(null); } }) } })()); async.parallel(functions, function(err, results) { callback(err, results); }); } getRationGljIds(rationLibs, callback){ } updateNodes (repId, lastOpr, nodes, callback) { var functions = []; for (var i=0; i < nodes.length; i++) { functions.push((function(doc) { return function(cb) { gljClassModel.update({ID: doc.ID}, doc, cb); }; })(nodes[i])); } functions.push((function () { return function (cb) { GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ cb(err); } else{ cb(null); } }) } })()); async.parallel(functions, function(err, results) { callback(err, results); }); } removeNodes (repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){ var functions = []; if (preNodeId != -1) { functions.push((function(nodeId, nextId) { return function(cb) { gljClassModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb); }; })(preNodeId, preNodeNextId)); } for (var i=0; i < nodeIds.length; i++) { functions.push((function(nodeId) { return function(cb) { gljClassModel.update({ID: nodeId}, {"isDeleted": true}, cb); }; })(nodeIds[i])); } functions.push((function () { return function (cb) { GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ cb(err); } else{ cb(null); } }) } })()); async.parallel(functions, function(err, results) { callback(err, results); }); } createNewNode(repId, lastOpr, lastNodeId, nodeData, callback) { return counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, 1, function(err, result){ nodeData.repositoryId = repId; nodeData.ID = result.value.sequence_value; var node = new gljModel(nodeData); async.parallel([ function (cb) { node.save(function (err, result) { if (err) { cb("章节树ID错误!", false); } else { if (lastNodeId > 0) { gljClassModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){ if (err) { cb("章节树ID错误!", false); } else { cb(false, result); } }); } else cb(false, result); } }); }, function (cb) { GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ cb(err); } else{ cb(null); } }) } ], function (err, result) { if(err){ callback(true, "章节树错误!", false); } else{ callback(false, '', result[0]); } }) }); } } export default GljDao;