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;
using DistributionApi.Model.Dto;

namespace DistributionApi.Repositories
{
    public class Distri_AddressRepository : BaseRepository<Distri_Address, string>, IDistri_AddressRepository
    {
        protected readonly ILogger<BaseRepository<Distri_Address, string>> _logger;
        protected readonly IMongoCollection<Distri_Address> _collection_distri_address;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        protected readonly IMongoCollection<Sys_Building> _collection_sys_building;
        protected readonly IMongoCollection<Sys_Floor> _collection_sys_floor;
        protected readonly IMongoCollection<Sys_Department> _collection_sys_department;
        public Distri_AddressRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Distri_Address, string>> logger) : base(settings, logger)
        {
            _collection_distri_address = _context.GetCollection<Distri_Address>();  //获取集合
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();  //获取集合
            _collection_sys_building = _context.GetCollection<Sys_Building>();
            _collection_sys_floor = _context.GetCollection<Sys_Floor>();
            _collection_sys_department = _context.GetCollection<Sys_Department>();
            _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<object> GetListsByPage(string keyword, string projectid, string buildingid, string floorid, UserInfoModel userinfo,int addresstype, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                 from addr in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on addr.projectid equals proje.id into proDefa
                 join building in _collection_sys_building.AsQueryable() on addr.buildingid equals building.id into building_Defa
                 join floor in _collection_sys_floor.AsQueryable() on addr.floorid equals floor.id into floDefa
                 //join dep in _collection_sys_department.AsQueryable() on addr.departmentid equals dep.id into depDefa
                 where addr.isdelete == 0
                 orderby addr.createtime descending
                 select new
                 {
                     addr.id,
                     addr.addresstype,
                     addr.projectid,
                     projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                     addr.buildingid,
                     buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null,
                     addr.floorid,
                     floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null,
                     //addr.departmentid,
                     //departmentname = depDefa != null && depDefa.Count() > 0 ? depDefa.First().departmenname : null,
                     departmentname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().remark : null,
                     addr.fullname,
                     addr.mobile,
                     addr.remark,
                     addr.isdefault,
                     addr.createtby,
                     addr.isdelete,
                     addr.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.mobile.Contains(keyword) || it.fullname.Contains(keyword));     //科室名称       
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (addresstype>=0)
                query = query.Where(it => it.addresstype == addresstype);
            if (!string.IsNullOrEmpty(buildingid))
                query = query.Where(it => it.buildingid == buildingid);
            if (!string.IsNullOrEmpty(floorid))
                query = query.Where(it => it.floorid == floorid);
            if (userinfo.isallproject == 0)
            {
                query = query.Where(it => userinfo.projectlist.Contains(it.projectid));
                query = query.Where(it => it.createtby == userinfo.usercode);
            }
            //微信用户 - 自己看自己的收发地址
            if (userinfo.type == 2)
                query = query.Where(it => it.createtby == userinfo.usercode);
            #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<Distri_AddressDto> GetLists(string keyword, string projectid, UserInfoModel userinfo,int addresstype)
        {
            //关联查询
            var query =
                 from addr in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on addr.projectid equals proje.id into proDefa
                 join building in _collection_sys_building.AsQueryable() on addr.buildingid equals building.id into building_Defa
                 join floor in _collection_sys_floor.AsQueryable() on addr.floorid equals floor.id into floDefa
                 //join dep in _collection_sys_department.AsQueryable() on addr.departmentid equals dep.id into depDefa
                 where addr.isdelete == 0
                 orderby addr.createtime descending
                 select new Distri_AddressDto
                 {
                     id = addr.id,
                     addresstype = addr.addresstype,
                     projectid = addr.projectid,
                     projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                     buildingid = addr.buildingid,
                     buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null,
                     floorid = addr.floorid,
                     floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null,
                     //departmentid = addr.departmentid,
                     //departmentname = depDefa != null && depDefa.Count() > 0 ? depDefa.First().departmenname : null,
                     departmentname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().remark : null,
                     fullname = addr.fullname,
                     mobile = addr.mobile,
                     remark = addr.remark,
                     isdefault = addr.isdefault,
                     createtby = addr.createtby,
                     createtime = addr.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.mobile.Contains(keyword) || it.fullname.Contains(keyword));     //科室名称       
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (userinfo.isallproject == 0)
            {
                query = query.Where(it => userinfo.projectlist.Contains(it.projectid));
            }
            if (userinfo.type == 2) //微信用户 - 自己看自己的收发地址                
                query = query.Where(it => it.createtby == userinfo.usercode);
            if (addresstype >= 0)
                query = query.Where(it => it.addresstype == addresstype);
            #endregion

            var list = query;
            return list;
        }

        /// <summary>
        /// Linq 关联查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            //关联查询
            var query =
                from addr in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on addr.projectid equals proje.id into proDefa
                join building in _collection_sys_building.AsQueryable() on addr.buildingid equals building.id into building_Defa
                join floor in _collection_sys_floor.AsQueryable() on addr.floorid equals floor.id into floDefa
                //join dep in _collection_sys_department.AsQueryable() on addr.departmentid equals dep.id into depDefa
                where addr.isdelete == 0 && addr.id == id
                select new
                {
                    addr.id,
                    addr.addresstype,
                    addr.projectid,
                    projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                    addr.buildingid,
                    buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null,
                    addr.floorid,
                    floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null,
                    //addr.departmentid,
                    //departmentname = depDefa != null && depDefa.Count() > 0 ? depDefa.First().departmenname : null,
                    departmentname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().remark : null,
                    addr.fullname,
                    addr.mobile,
                    addr.remark,
                    addr.isdefault,
                    addr.isdelete,
                    addr.createtime
                };
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }

    }
}
