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

namespace DistributionApi.Repositories
{
    public class Distri_Goods_TypeRepository : BaseRepository<Distri_Goods_Type, string>, IDistri_Goods_TypeRepository
    {
        protected readonly ILogger<BaseRepository<Distri_Goods_Type, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        public Distri_Goods_TypeRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Distri_Goods_Type, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();  //获取集合
            _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, UserAccountInfoModel userinfo, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                 from typ in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on typ.projectid equals proje.id into proDefa
                 where typ.isdelete == 0
                 orderby typ.sortnum ascending, typ.createtime descending
                 select new
                 {
                     id = typ.id,
                     parent_id = typ.parent_id,
                     projectid = typ.projectid,
                     projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                     kindname = typ.kindname,
                     sortnum = typ.sortnum,
                     remark = typ.remark,
                     createtby = typ.createtby,
                     createtime = typ.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.kindname.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="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_Goods_Type> GetLists(string keyword, string projectid, UserAccountInfoModel userinfo)
        {
            //关联查询
            var query =
                 from typ in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on typ.projectid equals proje.id into proDefa
                 where typ.isdelete == 0
                 orderby typ.projectid ascending,typ.sortnum ascending  //, typ.createtime descending
                 select new Distri_Goods_Type
                 {
                     id = typ.id,
                     parent_id = typ.parent_id,
                     projectid = typ.projectid,
                     //projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                     kindname = typ.kindname,
                     sortnum = typ.sortnum,
                     remark = typ.remark,
                     createtby = typ.createtby,
                     createtime = typ.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.kindname.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

            var list = query;
            return list;
        }


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