using ConfigurationApi.IRepositories; using MadRunFabric.Model; using ConfigurationApi.Model.Dto; using MadRunFabric.Common; using MadRunFabric.Common.Options; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace ConfigurationApi.Repositories { public class Sys_DepartmentRepository: BaseRepository, ISys_DepartmentRepository { protected readonly ILogger> _logger; protected readonly IMongoCollection _collection_pro_project_info; protected readonly IMongoCollection _collection_sys_floor; protected readonly IMongoCollection _collection_sys_building; public Sys_DepartmentRepository(IOptions settings, ILogger> logger) : base(settings, logger) { _collection_pro_project_info = _context.GetCollection();//获取集合 _collection_sys_floor = _context.GetCollection(); _collection_sys_building = _context.GetCollection(); _logger = logger; } /// /// 列表 - Linq 关联查询 分页 /// /// /// /// /// /// /// /// public IEnumerable GetListsByPage(string keyword, string projectid, string building, string floorid, UserAccountInfoModel userinfo, int pageIndex, int pageSize, out int recordCount) { //关联查询 var query = from dep in _collection.AsQueryable() join proje in _collection_pro_project_info.AsQueryable() on dep.projectid equals proje.id into depDefa join floor in _collection_sys_floor.AsQueryable() on dep.floorid equals floor.id into floDefa join build in _collection_sys_building.AsQueryable() on dep.building equals build.id into buildingDefa where dep.isdelete == 0 orderby dep.sortnum ascending, dep.createtime descending select new DepartmentDto { id = dep.id, parent_id = dep.parent_id, projectid = dep.projectid, projectname = depDefa != null && depDefa.Count() > 0 ? depDefa.First().project_name : null, floorid = dep.floorid, floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null, building = dep.building, buildname = buildingDefa != null && buildingDefa.Count() > 0 ? buildingDefa.First().buildname : null, departmenname = dep.departmenname, sortnum = dep.sortnum, remark = dep.remark, telephone = dep.telephone, mobile = dep.mobile, email = dep.email, isdelete = dep.isdelete, createtime = dep.createtime, }; #region 查询条件 if (!string.IsNullOrEmpty(keyword)) query = query.Where(it => it.departmenname.Contains(keyword)); //科室名称 if (!string.IsNullOrEmpty(projectid)) query = query.Where(it => it.projectid == projectid); if (!string.IsNullOrEmpty(building)) query = query.Where(it => it.building == building); if (!string.IsNullOrEmpty(floorid)) query = query.Where(it => it.floorid == floorid); if (userinfo.isallproject == 0) query = query.Where(it => userinfo.projectlist.Contains(it.projectid)); #endregion recordCount = query.Count(); var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize); return list; } /// /// 列表 - Linq 关联查询 /// /// /// /// /// /// /// /// public IEnumerable GetLists(string keyword, string projectid, string building, string floorid, UserAccountInfoModel userinfo) { //关联查询 var query = from dep in _collection.AsQueryable() join proje in _collection_pro_project_info.AsQueryable() on dep.projectid equals proje.id into depDefa join floor in _collection_sys_floor.AsQueryable() on dep.floorid equals floor.id into floDefa join build in _collection_sys_building.AsQueryable() on dep.building equals build.id into buildingDefa where dep.isdelete == 0 orderby dep.projectid ascending, dep.sortnum ascending, dep.createtime descending select new DepartmentDto { id = dep.id, parent_id = dep.parent_id, projectid = dep.projectid, projectname = depDefa != null && depDefa.Count() > 0 ? depDefa.First().project_name : null, floorid = dep.floorid, floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null, building = dep.building, buildname = buildingDefa != null && buildingDefa.Count() > 0 ? buildingDefa.First().buildname : null, departmenname = dep.departmenname, sortnum = dep.sortnum, remark = dep.remark, telephone = dep.telephone, mobile = dep.mobile, email = dep.email, isdelete = dep.isdelete, createtime = dep.createtime, }; #region 查询条件 if (!string.IsNullOrEmpty(keyword)) query = query.Where(it => it.departmenname.Contains(keyword)); //科室名称 if (!string.IsNullOrEmpty(projectid)) query = query.Where(it => it.projectid == projectid); if (!string.IsNullOrEmpty(building)) query = query.Where(it => it.building == building); if (!string.IsNullOrEmpty(floorid)) query = query.Where(it => it.floorid == floorid); if (userinfo.isallproject == 0) query = query.Where(it => userinfo.projectlist.Contains(it.projectid)); #endregion var list = query; return list; } /// /// Linq 关联查询详情 /// /// /// public DepartmentDetailsDto GetDetails(string id) { //关联查询 var query = from dep in _collection.AsQueryable() join proje in _collection_pro_project_info.AsQueryable() on dep.projectid equals proje.id into depDefa join floor in _collection_sys_floor.AsQueryable() on dep.floorid equals floor.id into floDefa join build in _collection_sys_building.AsQueryable() on dep.building equals build.id into buildingDefa where dep.isdelete == 0 && dep.id == id orderby dep.createtime select new DepartmentDetailsDto { id = dep.id, parent_id = dep.parent_id, projectid = dep.projectid, projectname = depDefa != null && depDefa.Count() > 0 ? depDefa.First().project_name : null, floorid = dep.floorid, floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null, building = dep.building, buildname = buildingDefa != null && buildingDefa.Count() > 0 ? buildingDefa.First().buildname : null, departmenname = dep.departmenname, sortnum = dep.sortnum, remark = dep.remark, telephone = dep.telephone, mobile = dep.mobile, email = dep.email, isdelete = dep.isdelete, createtime = dep.createtime, }; var model = query.FirstOrDefault(); if (model != null) { return model; } return null; } } }