using LangGuan.Command.Model; using Microsoft.Extensions.Configuration; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace LangGuan.Services { /// /// baseservice /// /// public class BaseService where T : BaseModel { private readonly IMongoCollection _collection; //数据表操作对象 /// /// 构造 /// /// /// public BaseService(IConfiguration config, string tableName) { var client = new MongoClient(config.GetSection("MongoDBConn").Value); //获取链接字符串 var database = client.GetDatabase(config.GetSection("MongoDBSetting:DBName").Value); _collection = database.GetCollection(tableName); } /// /// 获取所有 /// /// public List Get() { return _collection.Find(T => true).ToList(); } /// /// 获取单个 /// /// /// public T Get(string id) { return _collection.Find(T => T.Id == id).FirstOrDefault(); } /// /// 创建 /// /// /// public T Create(T T) { _collection.InsertOne(T); return T; } /// /// 更新 /// /// /// public void Update(string id, T TIn) { _collection.ReplaceOne(T => T.Id == id, TIn); } /// /// 删除 /// /// public void Remove(T TIn) { _collection.DeleteOne(T => T.Id == TIn.Id); } /// /// 根据id删除 /// /// public void Remove(string id) { _collection.DeleteOne(T => T.Id == id); } #region 异步操作 /// /// 取列表 /// /// public async Task> GetAsync() { return await _collection.Find(T => true).ToListAsync(); } /// /// 取单条 /// /// /// public async Task GetAsync(string id) { return await _collection.Find(T => T.Id == id).FirstOrDefaultAsync(); } /// /// 新增 /// /// /// public async Task CreateAsync(T T) { await _collection.InsertOneAsync(T); return T; } /// /// 新增 /// /// /// public async Task CreateManyAsync(IEnumerable T) { await _collection.InsertManyAsync(T); } /// /// 更新 /// /// /// /// public async Task UpdateAsync(string id, T TIn) { await _collection.ReplaceOneAsync(T => T.Id == id, TIn); } /// /// 删除 /// /// /// public async Task RemoveAsync(string id) { await _collection.DeleteOneAsync(T => T.Id == id); } #endregion /// /// 表达式取数据 /// /// /// public Task> GetListWithExp(Expression> expression) { // var temp = _collection.AsQueryable().Where(expression).ToList(); return Task.FromResult(_collection.AsQueryable().Where(expression)); } /// /// filter查找 /// /// /// public async Task> FindListByFilter(Expression> filter) { FilterDefinition filters = Builders.Filter.Where(filter); return await _collection.Find(filters).ToListAsync(); } /// /// filterdefinition /// /// /// public async Task> FindListyFilter(FilterDefinition filter) { return await _collection.Find(filter).ToListAsync(); } /// /// 是否存在 /// /// /// public Task Exist(Expression> expression) { var result = _collection.AsQueryable().Where(expression).Any(); return Task.FromResult(result); } /// /// 分页取数据 /// /// /// /// public async Task GetPager(RqeustPaging req, Expression> exp = null) { req.pageSize = req.pageSize == 0 ? 10 : req.pageSize; var query = await GetListWithExp(exp); var total = query.Count(); var items = query.OrderByDescending(s => s.CreateDateTime) .Skip(req.pageSize * (req.current - 1)).Take(req.pageSize).ToList(); return new ApiResult() { code = 0, data = new { total = total, items = items } }; } } }