| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /**
- * Created by Zhong on 2017/12/21.
- */
- import {compleRationModel} from './schemas';
- import {complementaryGljModel, stdGljModel} from '../../complementary_glj_lib/models/schemas';
- import async from 'async';
- let stdRationModel = require ('../../ration_repository/models/ration_item').Model;
- let counter = require('../../../public/counter/counter');
- class CompleRatoinDao {
- async updateRation(userID, compilationId, updateData, callback){
- try{
- for(let i = 0, len = updateData.length; i < len; i++){
- let updateObj = updateData[i];
- if(updateObj.updateType === 'new'){
- updateObj.updateData.userID = userID;
- updateObj.updateData.compilationId = compilationId;
- await compleRationModel.create(updateObj.updateData);
- }
- else if(updateObj.updateType === 'update'){
- await compleRationModel.update({userID: userID, rationRepId: updateObj.updateData.rationRepId}, updateObj.updateData);
- }
- }
- callback(0, '');
- }
- catch(err){
- callback(err, null);
- }
- }
- async getRationItems(userID, rationRepId, sectionId, callback){
- try{
- let stdRations = await stdRationModel.find({rationRepId: rationRepId, sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
- //mark std
- for(let i = 0, len = stdRations.length; i < len; i++){
- stdRations[i]._doc.type = 'std';
- }
- let compleRations = await compleRationModel.find({userId: userID, rationRepId: rationRepId, sectionId: sectionId, deleteInfo: null});
- //mark complementary
- for(let i = 0, len = compleRations.length; i < len; i++){
- compleRations[i]._doc.type = 'complementary';
- }
- callback(0, stdRations.concat(compleRations));
- }
- catch(err){
- callback(err, null);
- }
- }
- async getRationsCodes(userID, rationRepId, callback){
- try{
- let stdRationCodes = await stdRationModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]}, '-_id code');
- let compleRationCodes = await compleRationModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null}, '-_id code');
- let rstCodes = [];
- stdRationCodes.concat(compleRationCodes).forEach(function (rationItem) {
- rstCodes.push(rationItem.code);
- });
- callback(0, rstCodes);
- }
- catch(err){
- callback(err, null);
- }
- }
- async getGljItems(gljLibId, callback){
- try{
- let stdGljs = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]});
- callback(0, stdGljs);
- }
- catch(err){
- callback(err, null);
- }
- }
- async getGljItemsOccupied(gljLibId, occupation, callback){
- try{
- let stdGls = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]}, occupation);
- callback(0, stdGls);
- }
- catch (err){
- callback(err, null);
- }
- }
- async getGljItemsByIds(userID, ids, callback){
- try{
- let rst = [];
- for(let i = 0, len = ids.length; i < len; i++){
- if(ids[i].type === 'std'){
- let stdGlj = await stdGljModel.find({ID: ids[i].id, deleteInfo: null});
- if(stdGlj.length > 0){
- stdGlj[0]._doc.type = 'std';
- rst.push(stdGlj[0]);
- }
- }
- else if(ids[i].type === 'complementary'){
- let compleGlj = await complementaryGljModel.find({userId: userID, ID: ids[i].id, deleteInfo: null});
- if(compleGlj.length > 0){
- compleGlj[0]._doc.type = 'complementary';
- rst.push(compleGlj[0]);
- }
- }
- }
- callback(0, rst);
- }
- catch(err){
- callback(err, null);
- }
- }
- async getGljItemsByCodes(userID, compilationId, rationRepId, codes, callback){
- try{
- console.log(userID);
- console.log(compilationId);
- console.log(codes);
- let rst = [];
- for(let i = 0, len = codes.length; i < len; i++){
- let stdGlj = await stdGljModel.find({repositoryId: rationRepId, code: codes[i]});
- if(stdGlj.length > 0){
- stdGlj[0]._doc.type = 'std';
- rst.push(stdGlj[0]);
- }
- else {
- console.log(codes[i]);
- let compleGlj = await complementaryGljModel.find({userId: userID, compilationId: compilationId, code: codes[i]});
- console.log(compleGlj);
- if(compleGlj.length > 0){
- compleGlj[0]._doc.type = 'complementary';
- rst.push(compleGlj[0]);
- }
- }
- }
- callback(0, rst);
- }
- catch(err){
- callback(err, null);
- }
- }
- mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
- let me = this;
- if (updateItems.length == 0 && rIds.length == 0) {
- me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
- } else {
- me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
- if (err) {
- callback(true, false);
- } else {
- me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
- if (err) {
- callback(true, false);
- } else {
- if (addItems && addItems.length > 0) {
- me.addRationItems(rationLibId, sectionId, addItems, callback);
- } else {
- callback(0, results);
- }
- }
- });
- }
- })
- }
- }
- removeRationItems(rationRepId, rIds,callback){
- if (rIds.length > 0) {
- compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
- if (err) {
- callback(true, false);
- } else {
- callback(0, docs);
- }
- })
- } else {
- callback(0, null);
- }
- }
- addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
- if (items && items.length > 0) {
- counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
- let maxId = result.value.sequence_value;
- let arr = [];
- for (let i = 0; i < items.length; i++) {
- let obj = new compleRationModel(items[i]);
- obj.ID = (maxId - (items.length - 1) + i);
- obj.sectionId = sectionId;
- obj.rationRepId = rationLibId;
- obj.userId = userID;
- obj.compilationId = compilationId;
- arr.push(obj);
- }
- compleRationModel.collection.insert(arr, null, function(err, docs){
- if (err) {
- callback(true, false);
- } else {
- callback(0, docs);
- }
- })
- });
- } else {
- callback(true, "Source error!", false);
- }
- }
- updateRationItems(userID, rationLibId, sectionId, items,callback){
- let functions = [];
- for (let i=0; i < items.length; i++) {
- functions.push((function(doc) {
- return function(cb) {
- var filter = {};
- if (doc.ID) {
- filter.ID = doc.ID;
- } else {
- filter.sectionId = sectionId;
- filter.userId = userID;
- if (rationLibId) filter.rationRepId = rationLibId;
- filter.code = doc.code;
- }
- compleRationModel.update(filter, doc, cb);
- };
- })(items[i]));
- }
- async.parallel(functions, function(err, results) {
- if(!err){
- err = 0;
- }
- callback(err, results);
- });
- }
- }
- export default CompleRatoinDao;
|