123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /**
- * 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 {
- // 安装增加费库映射(12修缮定额需要直接用18定额的安装数据)
- getInstLibID(libID) {
- return +libID === 248 ? 214 : +libID;
- }
- async getInstallation(rationRepId, callback) {
- try {
- rationRepId = this.getInstLibID(rationRepId);
- 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, libID, callback) {
- try {
- const delIDs = [];
- for (let data of updateData) {
- if (data.updateData.rationRepId) {
- data.updateData.rationRepId = this.getInstLibID(data.updateData.rationRepId);
- }
- 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 {
- delIDs.push(data.updateData.ID);
- await installSectionModel.remove({ ID: data.updateData.ID });
- }
- }
- if (delIDs.length) {
- await rationItemModel.updateMany({ rationRepId: libID }, { $pull: { rationInstList: { sectionId: { $in: delIDs } } } });
- }
- callback(0, null);
- }
- catch (err) {
- callback(err, null);
- }
- }
- async updateFeeItem(updateData, callback) {
- try {
- for (let data of updateData) {
- if (data.updateData.rationRepId) {
- data.updateData.rationRepId = this.getInstLibID(data.updateData.rationRepId);
- }
- 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;
|