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<Sys_Department,string>, ISys_DepartmentRepository
    {
        protected readonly ILogger<BaseRepository<Sys_Department, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        protected readonly IMongoCollection<Sys_Floor> _collection_sys_floor;
        protected readonly IMongoCollection<Sys_Building> _collection_sys_building;
        public Sys_DepartmentRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_Department, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();//获取集合
            _collection_sys_floor = _context.GetCollection<Sys_Floor>();
            _collection_sys_building = _context.GetCollection<Sys_Building>();
            _logger = logger;
        }

        /// <summary>
        /// 列表 -  Linq 关联查询 分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<DepartmentDto> 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;
        }
        /// <summary>
        /// 列表 -  Linq 关联查询
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<DepartmentDto> 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;
        }


        /// <summary>
        /// Linq 关联查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        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;
        }

    }
}
