/** * Created by Zhong on 2017/12/21. */ const mongoose = require('mongoose'); const compleRationSectionTreeModel = mongoose.model('complementary_ration_section_tree'); const compleRationModel = mongoose.model('complementary_ration_items'); let counter = require('../../../public/counter/counter'); let stdSectionTreeModel = require ('../../ration_repository/models/ration_section_tree').Model; class SectionTreeDao { getNewTreeID(callback){ counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function (err, result) { if(err){ callback(err, null); } else { callback(0, result.sequence_value); } }); } //获取补充定额拼接章节树 async getRationTree(userID, rationRepId, callback){ try{ let stdSectionTree = await stdSectionTreeModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]}); let compleSectionTree = await compleRationSectionTreeModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null}); let dropPids = [], rstCompleSectionTree = []; //mark std for(let i = 0, len = stdSectionTree.length; i < len; i++){ stdSectionTree[i]._doc.type = 'std'; } for(let i = 0, len = compleSectionTree.length; i < len; i++){ //mark complementary compleSectionTree[i]._doc.type = 'complementary'; if(compleSectionTree[i]['isFirst']){ let updateSection = getUpdateSection(compleSectionTree[i]['ParentID'], stdSectionTree); if(updateSection) { updateSection._doc.NextSiblingID = compleSectionTree[i]['ID']; } else if(!updateSection && compleSectionTree[i]['ParentID'] !== -1){ dropPids.push(compleSectionTree[i]['ParentID']); } } } function getUpdateSection(pid, datas){ for(let i = 0, len = datas.length; i < len; i++){ if(datas[i]['ParentID'] === pid && datas[i]['NextSiblingID'] === -1){ return datas[i]; } } return null; } //返回父节点未被删除的 for(let i = 0, len = compleSectionTree.length; i < len; i++){ if(dropPids.indexOf(compleSectionTree[i]['ParentID']) === -1){ rstCompleSectionTree.push(compleSectionTree[i]); } } callback(0, stdSectionTree.concat(rstCompleSectionTree)); } catch (err){ callback(err, null); } } async updateSection(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 compleRationSectionTreeModel.create(updateObj.updateData); } else if(updateObj.updateType === 'update'){ await compleRationSectionTreeModel.update({userId: userID, rationRepId: updateObj.updateData.rationRepId, ID: updateObj.updateData.ID}, updateObj.updateData); if(updateObj.updateData.deleteInfo){ await compleRationModel.update({userId: userID, sectionId: updateObj.updateData.ID}, {$set: {deleteInfo: {deleted: true, deleteBy: userID, deleteDateTime: Date.now()}}}, {multi: true}); } } } callback(0, 'success'); } catch(err){ callback(err, null); } } } export default SectionTreeDao;