123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /**
- * Created by Zhong on 2018/1/24.
- */
- const mongoose = require('mongoose');
- const rationItemModel = mongoose.model('std_ration_lib_ration_items');
- const installFeeItemModel = mongoose.model('std_ration_lib_installation');
- const installSectionModel = mongoose.model('std_ration_lib_installationSection');
- class InstallationDao{
- async getInstallation(rationRepId, callback){
- try {
- const feeItems = await installFeeItemModel.find({ rationRepId }).lean();
- const feeItemMap = {};
- const sectionIds = [];
- feeItems.forEach(item => {
- feeItemMap[item.ID] = item;
- item.section.forEach(s => sectionIds.push(s.ID));
- item.section = [];
- });
- const sections = await installSectionModel.find({ID: {$in: sectionIds}});
- sections.forEach(section => {
- const matchFeeItem = feeItemMap[section.feeItemId];
- if (matchFeeItem) {
- matchFeeItem.section.push(section);
- }
- });
- if (!callback) {
- return feeItems;
- }
- callback(0, feeItems);
- }
- catch(err){
- if (!callback) {
- return [];
- }
- callback(err, null);
- }
- }
- /*async getInstallation(rationRepId, callback){
- try {
- let feeItems = await installFeeItemModel.find({rationRepId: rationRepId});
- for(let feeItem of feeItems){
- let sids = [];
- for(let sec of feeItem.section){
- sids.push(sec.ID);
- }
- if(sids.length > 0){
- let sections = await installSectionModel.find({ID: {$in: sids}});
- feeItem._doc.section = sections;
- }
- }
- callback(0, feeItems);
- }
- catch(err){
- callback(err, null);
- }
- }*/
- async updateSection(updateData, callback){
- try{
- for(let data of updateData){
- if(data.updateType === 'new'){
- await installSectionModel.create(data.updateData);
- }
- else if(data.updateType === 'update' && !data.updateData.deleted){
- await installSectionModel.update({ID: data.updateData.ID}, data.updateData);
- }
- else {
- await installSectionModel.remove({ID: data.updateData.ID});
- }
- }
- callback(0, null);
- }
- catch(err){
- callback(err, null);
- }
- }
- async updateFeeItem(updateData, callback){
- try{
- for(let data of updateData){
- if(data.updateType === 'new'){
- await installFeeItemModel.create(data.updateData);
- }
- else if(data.updateType === 'update' && !data.updateData.deleted){
- await installFeeItemModel.update({ID: data.updateData.ID}, data.updateData);
- }
- else{
- await installFeeItemModel.remove({ID: data.updateData.ID});
- }
- }
- callback(0, null);
- }
- catch(err){
- callback(err, null);
- }
- }
- async batchUpdateInst(rationSection, inst, callback){
- try{
- for(let sectionId of rationSection){
- await rationItemModel.update({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]},
- {$addToSet: {rationInstList: {feeItemId: inst.feeItemId, sectionId: inst.sectionId}}}, {multi: true});
- }
- callback(0, null);
- }
- catch(err){
- callback(err, null);
- }
- }
- }
- export default InstallationDao;
|