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

namespace EquipmentApi.Repositories
{
    public class Eqp_Import_InfoRepository : BaseRepository<Eqp_Import_Info, string>, IEqp_Import_InfoRepository
    {
        protected readonly ILogger<BaseRepository<Eqp_Import_Info, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        protected readonly IMongoCollection<Sys_Building> _collection_sys_buliding;
        protected readonly IMongoCollection<Sys_Floor> _collection_sys_floor;

        public Eqp_Import_InfoRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Eqp_Import_Info, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();
            _collection_sys_buliding = _context.GetCollection<Sys_Building>();
            _collection_sys_floor = _context.GetCollection<Sys_Floor>();
            _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> GetImportListsByPage(string keyword, string stime, string etime, int pageindex, int pagesize, out int recordCount)
        {
            //关联查询
            var query = from p in _collection.AsQueryable()
                        join proje in _collection_pro_project_info.AsQueryable() on p.projectid equals proje.id into proinfo
                        join bld in _collection_sys_buliding.AsQueryable() on p.buildid equals bld.id into bldinfo
                        where p.isdelete==0 orderby p.createtime descending
                        select new {
                           p.id,
                            projectname= proinfo.Count()>0? proinfo.First().project_name:"",
                            bulidingname= bldinfo.Count()>0? bldinfo.First().buildname:"",
                            p.fileext,
                            p.filemd5,
                            p.filename,
                            p.filesize,
                            p.fileurl,
                            p.createby,
                            p.createtime
                        };

            #region 查询条件

            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.filename.Contains(keyword));

            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => DateTime.Compare(it.createtime, DateTime.Parse(stime + " 00:00:00")) >= 0);
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => DateTime.Compare(it.createtime, DateTime.Parse(etime + " 23:59:59")) <= 0);
            #endregion

            recordCount = query.Count();
            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();

            return list;
        }

        /// <summary>
        /// Linq 查询楼层id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetFloorId(string floorname)
        {
            var flrDefa = from flr in _collection_sys_floor.AsQueryable() select flr;

            var model = flrDefa.Where(p => p.floorname == floorname && p.isdelete == 0).FirstOrDefault();
            if (model != null)
            {
                return model.id;
            }
            return null;
        }
    }
}
