123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /**
- * Created by CSL on 2017/5/3.
- * 系数表。
- */
- //modiyied by zhong on 2017/9/21
- const mongoose = require('mongoose');
- const coeListModel = mongoose.model('std_ration_lib_coe_list');
- let counter = require('../../../public/counter/counter');
- const rationModel = mongoose.model('std_ration_lib_ration_items');
- import async from 'async';
- let coeListDAO = function(){};
- coeListDAO.prototype.getCoeReference = async function (rationRepId, coeID) {
- return await rationModel.find({rationRepId, 'rationCoeList.ID': coeID}, '-_id code').sort({code: 1});
- };
- coeListDAO.prototype.getCoe = function (data, callback) {
- coeListModel.findOne({
- "libID": data.libID,
- "ID": data.ID,
- "$or": [{"isDeleted": null}, {"isDeleted": false}]
- },
- function (err, doc) {
- if (err) callback("获取系数明细错误!", null)
- else callback(null, doc);
- })
- };
- coeListDAO.prototype.getCoeItemsByIDs = function (data, callback) {
- coeListModel.find({
- "libID": data.libID,
- "ID": {"$in":data.coeIDs}
- }, ["libID","ID","serialNo","name","content","-_id"],
- function (err, doc) {
- if (err) callback("批量获取系数明细错误!", null)
- else callback(null, doc);
- })
- };
- coeListDAO.prototype.getCoeItemsByNos = function (data, callback) {
- coeListModel.find({
- "libID": data.libID,
- "serialNo": {"$in":data.coeNos}
- }, ["libID","ID","serialNo","name","content","-_id"],
- function (err, doc) {
- if (err) callback("批量获取系数明细错误!", null)
- else callback(null, doc);
- })
- };
- coeListDAO.prototype.getCoesByLibID = function (libID, callback) {
- coeListModel.find({ "libID": libID },
- function (err, doc) {
- if (err) callback("获取定额库系数表错误", null)
- else callback(null, doc);
- })
- };
- coeListDAO.prototype.saveToCoeList = function(data, callback) {
- let me = this;
- /*if (data.addArr.length > 0) {
- me.addItems(data.addArr, callback);
- }
- if (data.deleteArr.length > 0) {
- me.deleteItems(data.deleteArr, callback);
- }
- if (data.updateArr.length > 0) {
- me.updateItems(data.updateArr, callback);
- }*/
- if (data.addArr.length > 0 && data.updateArr.length > 0) {
- async.parallel([
- function (cb) {
- me.addItems(data.addArr, cb);
- },
- function (cb) {
- me.updateItems(data.updateArr, cb);
- }
- ], function (err, result) {
- callback(err, 'mixed', result);
- });
- }
- else if(data.addArr.length > 0){
- me.addItems(data.addArr, callback);
- }
- else if (data.updateArr.length > 0) {
- me.updateItems(data.updateArr, callback);
- }
- else if (data.deleteArr.length > 0) {
- me.deleteItems(data.deleteArr, callback);
- }
- };
- coeListDAO.prototype.addItems = function(addArr, callback) {
- if (addArr && addArr.length > 0) {
- counter.counterDAO.getIDAfterCount(counter.moduleName.coeList, addArr.length, function(err, result){
- let maxId = result.sequence_value;
- let functions = [];
- for (let i = 0; i < addArr.length; i++) {
- let obj = new coeListModel(addArr[i]);
- obj.ID = (maxId - (addArr.length - 1) + i);
- functions.push((function (obj) {
- return function (cb) {
- coeListModel.create(obj, function(err) {
- if (err) {
- cb(true, null);
- } else {
- cb(false, obj);
- };
- });
- }
- })(obj));
- }
- async.parallel(functions, function (err, result) {
- if(err) callback(true, 'add fail', null);
- else callback(false, 'addSc', result);
- })
- });
- } else {
- callback(true, "no source", null);
- }
- };
- coeListDAO.prototype.updateItems = function(updateArr, callback) {
- if (updateArr && updateArr.length > 0) {
- let functions = [];
- for (let i = 0; i < updateArr.length; i++) {
- let obj = updateArr[i];
- let needSet = {};
- for(let attr in obj){
- if(attr !== 'libID' || attr !== 'ID'){
- needSet[attr] = obj[attr];
- }
- }
- functions.push((function (obj) {
- return function (cb) {
- coeListModel.update({"libID": obj.libID, "ID": obj.ID}, needSet, function(err) {
- if (err) {
- cb(true);
- } else {
- cb(false);
- }
- });
- }
- })(obj));
- }
- async.parallel(functions, function (err, result) {
- if(err) callback(true, 'update fail', null);
- else callback(false, 'updateSc', null);
- })
- } else {
- callback(true, "no source", null);
- }
- };
- coeListDAO.prototype.deleteItems = function(deleteArr, callback) {
- if (deleteArr && deleteArr.length > 0) {
- let functions = [];
- for (let i = 0; i < deleteArr.length; i++) {
- let obj = deleteArr[i];
- functions.push((function (obj) {
- return function (cb) {
- coeListModel.remove({"libID": obj.libID, "ID": obj.ID}, function(err) {
- if (err) {
- cb(true, null);
- } else {
- cb(false, obj.ID);
- }
- });
- }
- })(obj));
- }
- async.parallel(functions, function (err, result) {
- if(err) callback(true, 'delete fail', null);
- else callback(false, 'sc', result);
- })
- } else {
- callback(true, "no source", null);
- }
- };
- module.exports = new coeListDAO();
|