using DistributionApi.IRepositories; using MadRunFabric.Common; using MadRunFabric.Common.Options; using MadRunFabric.Model; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace DistributionApi.Repositories { public class Distri_GoodsRepository : BaseRepository, IDistri_GoodsRepository { protected readonly ILogger> _logger; protected readonly IMongoCollection _collection_distri_goods_type; protected readonly IMongoCollection _collection_pro_project_info; protected readonly IMongoCollection _collection_sys_building; protected readonly IMongoCollection _collection_sys_floor; public Distri_GoodsRepository(IOptions settings, ILogger> logger) : base(settings, logger) { _collection_distri_goods_type = _context.GetCollection(); //获取集合 _collection_pro_project_info = _context.GetCollection(); //获取集合 _collection_sys_building = _context.GetCollection(); _collection_sys_floor = _context.GetCollection(); _logger = logger; } /// /// 列表 - Linq 关联查询 分页 /// /// /// /// /// /// /// /// public IEnumerable GetListsByPage(string keyword, string projectid, UserAccountInfoModel userinfo, List kindids, int pageIndex, int pageSize, out int recordCount) { //关联查询 var query = from goods in _collection.AsQueryable() join proje in _collection_pro_project_info.AsQueryable() on goods.projectid equals proje.id into proje_Defa join goodstype in _collection_distri_goods_type.AsQueryable() on goods.kindid equals goodstype.id into goodstype_Defa join building in _collection_sys_building.AsQueryable() on goods.buildingid equals building.id into building_Defa join floor in _collection_sys_floor.AsQueryable() on goods.floorid equals floor.id into floDefa where goods.isdelete == 0 orderby goods.createtime descending select new { goods.id, goods.goodsname, goods.projectid, projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null, goods.kindid, kindidname = goodstype_Defa != null && goodstype_Defa.Count() > 0 ? goodstype_Defa.First().kindname : null, goods.typeid, goods.isremind, goods.buildingid, buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null, goods.floorid, floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null, departmentname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().remark : null, goods.remark, goods.sortnum, goods.createtby, goods.isdelete, goods.createtime }; #region 查询条件 if (!string.IsNullOrEmpty(keyword)) query = query.Where(it => it.goodsname.Contains(keyword)); //物品名称 if (!string.IsNullOrEmpty(projectid)) query = query.Where(it => it.projectid == projectid); if (kindids != null && kindids.Count > 0) query = query.Where(it => kindids.Contains(it.kindid)); if (userinfo != null) { 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, UserInfoModel userinfo, List kindids) { //关联查询 var query = from goods in _collection.AsQueryable() join proje in _collection_pro_project_info.AsQueryable() on goods.projectid equals proje.id into proje_Defa join goodstype in _collection_distri_goods_type.AsQueryable() on goods.kindid equals goodstype.id into goodstype_Defa join building in _collection_sys_building.AsQueryable() on goods.buildingid equals building.id into building_Defa join floor in _collection_sys_floor.AsQueryable() on goods.floorid equals floor.id into floDefa where goods.isdelete == 0 orderby goods.createtime descending select new { goods.id, goods.goodsname, goods.projectid, projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null, goods.kindid, kindidname = goodstype_Defa != null && goodstype_Defa.Count() > 0 ? goodstype_Defa.First().kindname : null, goods.typeid, goods.isremind, goods.buildingid, buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null, goods.floorid, floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null, departmentname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().remark : null, goods.remark, goods.sortnum, goods.createtby, goods.isdelete, goods.createtime }; #region 查询条件 if (!string.IsNullOrEmpty(keyword)) query = query.Where(it => it.goodsname.Contains(keyword)); //物品名称 if (!string.IsNullOrEmpty(projectid)) query = query.Where(it => it.projectid == projectid); if (kindids != null && kindids.Count > 0) query = query.Where(it => kindids.Contains(it.kindid)); if (userinfo.isallproject == 0) query = query.Where(it => userinfo.projectlist.Contains(it.projectid)); #endregion var list = query; return list; } /// /// Linq 关联查询详情 /// /// /// public object GetDetails(string id) { //关联查询 var query = from goods in _collection.AsQueryable() join proje in _collection_pro_project_info.AsQueryable() on goods.projectid equals proje.id into proje_Defa join goodstype in _collection_distri_goods_type.AsQueryable() on goods.kindid equals goodstype.id into goodstype_Defa join building in _collection_sys_building.AsQueryable() on goods.buildingid equals building.id into building_Defa join floor in _collection_sys_floor.AsQueryable() on goods.floorid equals floor.id into floDefa where goods.isdelete == 0 && goods.id == id orderby goods.createtime descending select new { goods.id, goods.goodsname, goods.projectid, projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null, goods.kindid, kindidname = goodstype_Defa != null && goodstype_Defa.Count() > 0 ? goodstype_Defa.First().kindname : null, goods.typeid, goods.isremind, goods.remark, goods.buildingid, buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null, goods.floorid, floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null, goods.sortnum, goods.createtby, goods.isdelete, goods.createtime }; var model = query.FirstOrDefault(); if (model != null) { return model; } return null; } } }