123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /**
- * 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<recentOprArr.length; i++){
- if(recentOprArr[i].operator === oprtor){
- recentOprArr[i].operateDate = oprDate;
- isExist = true;
- }
- }
- if(!isExist){
- if(recentOprArr.length < 5){
- recentOprArr.push({operator: oprtor, operateDate: oprDate});
- }
- else if(recentOprArr.length === 5){
- recentOprArr.sort(function (a, b) {
- if(a.operateDate > 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();
|