using Amazon.Runtime.Internal;
using Business.Core.Attributes;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
namespace Business.Core.MongoDBHelper
{
///
/// MongoDB帮助类
///
public class MongoDBTools : IMongoDB
{
public readonly IMongoCollection mongoCollection;
public readonly IMongoDatabase dataBase;
public IOptionsSnapshot _config;
public CollectionNameAttribute collectonName;
///
/// MongoDB链接
///
///
///
public MongoDBTools(IOptionsSnapshot config)
{
_config = config;
collectonName = typeof(T).GetCustomAttributes(typeof(CollectionNameAttribute), true).FirstOrDefault() as CollectionNameAttribute;
if (collectonName == null)
{
throw new NotImplementedException("请配置Attribute属性!");
}
//数据库链接
MongoClient client = new MongoClient(config.Value.connectstring + collectonName.DatabaseName);
//数据库
dataBase = client.GetDatabase(collectonName.DatabaseName);
//表名
mongoCollection = dataBase.GetCollection(collectonName.CollectionName);
}
///
/// 插入一条数据
///
///
///
public Task InsertOne(T document)
{
return mongoCollection.InsertOneAsync(document);
}
///
/// 插入多条数据
///
///
///
///
public Task InsertMany(List documents)
{
return mongoCollection.InsertManyAsync(documents,new InsertManyOptions() { IsOrdered = false});
}
///
/// 获取所有数据
///
///
///
public Task> GetAll()
{
return mongoCollection.AsQueryable().ToListAsync();
}
///
/// 根据条件获取数据
///
///
public Task> GetManyByCondition(Expression> filter)
{
return mongoCollection.Find(filter).ToListAsync();
}
///
/// 根据条件获取数据
///
///
public Task> GetManyByIds(FilterDefinition filter)
{
return mongoCollection.Find(filter).ToListAsync();
}
///
/// 删除数据
///
///
///
///
public Task Delete(Expression> expression, bool isOne = false)
{
if (isOne)
return mongoCollection.DeleteOneAsync(expression);
else
return mongoCollection.DeleteManyAsync(expression);
}
///
/// 删除数据
///
///
///
///
public Task Delete(FilterDefinition filter, bool isOne = false)
{
if (isOne)
return mongoCollection.DeleteOneAsync(filter);
else
return mongoCollection.DeleteManyAsync(filter);
}
///
/// 根据条件获取结果列表
///
/// 条件Expression
/// 结果列表
public Task> Find(Expression> expression, ProjectionDefinition projecter = null, SortDefinition sorter = null)
{
//Include表示包含那些字段
//ProjectionDefinitionBuilder project = new ProjectionDefinitionBuilder();
//var templates = voucherRepository.Find(x => x.AppId == this.AppId, project.Include(x => x.FContentId).
// Include(x => x.TemplateName)).Select(x => new Template { TemplateId = x.FContentId, TemplateName = x.TemplateName }).AsQueryable();
//Exclude表示包含那些字段
//List exchangeRates = exchangeRateRepository.Find(x => x.AppId == this.AppId, project.Exclude(x => x.CurrencyXRates).Exclude(y => y.ExchangeRateId));
return mongoCollection.Find(expression).Project(projecter).Sort(sorter).ToListAsync();
}
///
/// 批处理操作,操作的数据条数需要大于0
///
///
///
public Task> BulkWrite(List> updates, BulkWriteOptions options = null)
{
//eg:使用示例
//List> rules = new List>();
//foreach (var ruleName in ruleNames)
//{
// var lockRule = lockRules.FirstOrDefault(x => x.RuleName == ruleName);
// var dimDLMember = string.Join("-", ruleName.Split("-").Skip(3));
// if (lockRule == null)
// {
// lockRule = new BusinessDataLockRule();
// lockRule.RuleName = ruleName;
// lockRule.Locked = true;
// var insertOneModel = new InsertOneModel(lockRule);
// rules.Add(insertOneModel);
// }
// else
// {
// var filterDefinition = Builders.Filter.Eq(x => x.Id, lockRule.Id);
// var updateDefinition = Builders.Update.Set(x => x.Locked, lockRule.Locked)
// .Set(x => x.LockDimensions, lockRule.LockDimensions)
// .Set(x => x.LockTime, DateTime.Now);
// var updateOneModel = new UpdateOneModel(filterDefinition, updateDefinition);
// rules.Add(updateOneModel);
// }
//}
//if (!rules.IsNullOrEmpty())
//{
// mongoCollection.BulkWriteAsync(rules, new BulkWriteOptions { IsOrdered = false });
//}
return mongoCollection.BulkWriteAsync(updates,options);
}
///
/// 根据条件获取结果列表
///
/// 条件FilterDefinition
/// 结果列表
public Task> Find(FilterDefinition filter, ProjectionDefinition projecter = null, SortDefinition sorter = null)
{
return mongoCollection.Find(filter).Project(projecter).Sort(sorter).ToListAsync();
}
}
}