using ConfigurationApi.IRepositories;
using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MadRunFabric.Model;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ConfigurationApi.Repositories
{
    public class Sys_BuildingRepository : BaseRepository<Sys_Building, string>, ISys_BuildingRepository
    {
        protected readonly ILogger<BaseRepository<Sys_Building, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        public Sys_BuildingRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_Building, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();//获取集合
            _logger = logger;
        }

        #region 楼
        /// <summary>
        /// 获取楼层列表 分页 - Linq 关联查询
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="projectid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetBuildListsByPage(string keyword, string projectid, UserAccountInfoModel userinfo, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                from build in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on build.projectid equals proje.id into projeDefa
                where build.isdelete == 0
                orderby build.sortnum ascending, build.createtime descending
                select new
                {
                    id = build.id,
                    buildname = build.buildname,
                    projectid = build.projectid,
                    projectname = projeDefa != null&& projeDefa.Count()>0 ? projeDefa.First().project_name : null,
                    sortnum = build.sortnum,
                    remark = build.remark,
                    isdelete = build.isdelete,
                    createtby = build.createtby,
                    createtime = build.createtime,
                };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.buildname.Contains(keyword));  // || it.projectname.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));
            #endregion
            recordCount = query.Count();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }

        /// <summary>
        /// 获取楼详情 - Linq 关联查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetBuildDetails(string id)
        {
            //关联查询
            var query =
                from build in _collection.AsQueryable()
                join proje in _collection_pro_project_info.AsQueryable() on build.projectid equals proje.id into projeDefa
                where build.isdelete == 0 && build.id == id
                orderby build.createtime
                select new
                {
                    id = build.id,
                    buildname = build.buildname,
                    projectid = build.projectid,
                    projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                    sortnum = build.sortnum,
                    remark = build.remark,
                    isdelete = build.isdelete,
                    createtby = build.createtby,
                    createtime = build.createtime,
                };
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }
        #endregion

    }
}
