/** * Created by Tony on 2017/4/24. * 重新构造,不适宜生成多个定额库db,还是得统一在一个表 */ var mongoose = require('mongoose'); var dbm = require("../../../config/db/db_manager"); let async = require("async"); let moment = require('moment'); //var stringUtil = require('../../../public/stringUtil'); var rationLibdb = dbm.getCfgConnection("scConstruct"); var Schema = mongoose.Schema; var RepositoryMapSchema = new Schema({ "ID": Number, "dispName" : String, "appType" : String, //如:"建筑" / "公路" "localeType": Number, //如 各个省份 / 部颁 "descr" : String, "creator": String, "createDate": String, "recentOpr" :[], "deleted": Boolean }); var counter = require('../../../public/counter/counter'); var rationRepository = rationLibdb.model("std_ration_lib_map", RepositoryMapSchema, "std_ration_lib_map"); function createNewLibModel(rationLibObj){ var rst = {}; rst.dispName = rationLibObj.dispName; rst.appType = rationLibObj.appType?rationLibObj.appType:'construct'; rst.localeType = rationLibObj.localeType?rationLibObj.localeType:''; rst.descr = rationLibObj.descr; rst.creator = rationLibObj.creator; rst.createDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'); rst.recentOpr = [{operator: rationLibObj.lastOperator, operateDate: rst.createDate}], rst.deleted = false; return rst; } var rationRepositoryDao = function(){}; rationRepositoryDao.prototype.updateOprArr= function(findSet, oprtor, oprDate, cb){ rationRepository.find(findSet, function (err, result) { if(err){ cb(err); } else{ if(result.length === 1){ let recentOprArr = result[0].recentOpr; let isExist = false; for(let i =0 ; i b.operateDate){ return -1; }else { return 1; } return 0; }); recentOprArr.splice(recentOprArr.length -1, 1); recentOprArr.splice(0, 1, {operator: oprtor, operateDate: oprDate}); } } rationRepository.update(findSet, {$set: {recentOpr: recentOprArr}}, function (err) { if(err){ cb(err); } else{ cb(null); } }); } else{ cb(err); } } }) }; rationRepositoryDao.prototype.getRealLibName = function(dispName,callback){ if (callback) { rationRepository.find({"dispName": dispName}, function(err,data){ if (err) { callback('Error', null); } else { callback(false, data); } }); } else { var rst = rationRepository.find({"dispName": dispName}).exec(); return rst; } }; rationRepositoryDao.prototype.getLibIDByName = function(dispName, callback){ rationRepository.findOne({"dispName": dispName}, function(err,data){ if (err) { callback('Error', null); } else { callback(false, data.ID); } }); }; rationRepositoryDao.prototype.getRepositoryById = function(repId,callback){ if (callback) { rationRepository.find({"ID": repId}, function(err,data){ if (err) { callback('Error', null); } else { callback(false, data); } }); } else { var rst = rationRepository.find({"ID": repId}).exec(); return rst; } }; rationRepositoryDao.prototype.addRationRepository = function( rationLibObj,callback){ counter.counterDAO.getIDAfterCount(counter.moduleName.rationMap, 1, function(err, result){ var rMap = createNewLibModel(rationLibObj); rMap.ID = result.value.sequence_value; new rationRepository(rMap).save(function(err, result) { if (err) callback("Error", null) else callback(false, result); }); }); }; rationRepositoryDao.prototype.getDisplayRationLibs = function(callback) { rationRepository.find({"deleted": false}, function(err, data){ if (err) { callback( 'Error', null); } else { callback( false, data); } }); }; rationRepositoryDao.prototype.updateName = function(orgName,newName, lastOpr, callback){ rationRepository.find({"dispName":newName, "deleted": false}, function(err, data){ if (data.length == 0) { async.waterfall([ function (cb) { rationRepository.update({dispName:orgName},{$set:{dispName:newName}},function(err){ if(err) cb(err); else cb(null) }); }, function (cb) { new rationRepositoryDao().updateOprArr({dispName: newName}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ cb(err) } else{ cb(null); } }); } ], function (err) { if(err){ callback('err', false); } else{ callback(false, 'ok'); } }); } else callback("不可重名!",false); }); } rationRepositoryDao.prototype.deleteRationLib = function(rationName, lastOpr, callback){ rationRepository.find({"dispName":rationName, "deleted": false}, function(err, data){ if (data.length == 1) { async.parallel([ function (cb) { rationRepository.update({dispName:rationName, deleted: false},{$set: {deleted: true}},function(err){ if(err) cb(err); else cb(null); }) }, function (cb) { new rationRepositoryDao().updateOprArr({dispName: rationName}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) { if(err){ cb(err); } else{ cb(null); } }); } ], function (err) { if(err){ callback("err", false) } else{ callback(false, "ok"); } }); } else callback("删除失败!",false); }); } module.exports = new rationRepositoryDao();