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;

namespace DistributionApi.Repositories
{
    public class Distri_GoodsRepository : BaseRepository<Distri_Goods, string>, IDistri_GoodsRepository
    {
        protected readonly ILogger<BaseRepository<Distri_Goods, string>> _logger;
        protected readonly IMongoCollection<Distri_Goods_Type> _collection_distri_goods_type;
        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;
        public Distri_GoodsRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Distri_Goods, string>> logger) : base(settings, logger)
        {
            _collection_distri_goods_type = _context.GetCollection<Distri_Goods_Type>();  //获取集合
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();  //获取集合
            _collection_sys_building = _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> GetListsByPage(string keyword, string projectid, UserAccountInfoModel userinfo, List<string> kindids, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                 from goods in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on goods.projectid equals proje.id into proje_Defa
                 join goodstype in _collection_distri_goods_type.AsQueryable() on goods.kindid equals goodstype.id into goodstype_Defa
                 join building in _collection_sys_building.AsQueryable() on goods.buildingid equals building.id into building_Defa
                 join floor in _collection_sys_floor.AsQueryable() on goods.floorid equals floor.id into floDefa
                 where goods.isdelete == 0
                 orderby goods.createtime descending
                 select new
                 {
                     goods.id,
                     goods.goodsname,
                     goods.projectid,
                     projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null,
                     goods.kindid,
                     kindidname = goodstype_Defa != null && goodstype_Defa.Count() > 0 ? goodstype_Defa.First().kindname : null,
                     goods.typeid,
                     goods.isremind,
                     goods.buildingid,
                     buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null,
                     goods.floorid,
                     floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null,
                     departmentname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().remark : null,
                     goods.remark,
                     goods.sortnum,
                     goods.createtby,
                     goods.isdelete,
                     goods.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.goodsname.Contains(keyword));     //物品名称       
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (kindids != null && kindids.Count > 0)
                query = query.Where(it => kindids.Contains(it.kindid));
            if (userinfo != null)
            {
                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<object> GetLists(string keyword, string projectid, UserInfoModel userinfo, List<string> kindids)
        {
            //关联查询
            var query =
                 from goods in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on goods.projectid equals proje.id into proje_Defa
                 join goodstype in _collection_distri_goods_type.AsQueryable() on goods.kindid equals goodstype.id into goodstype_Defa
                 join building in _collection_sys_building.AsQueryable() on goods.buildingid equals building.id into building_Defa
                 join floor in _collection_sys_floor.AsQueryable() on goods.floorid equals floor.id into floDefa
                 where goods.isdelete == 0
                 orderby goods.createtime descending
                 select new
                 {
                     goods.id,
                     goods.goodsname,
                     goods.projectid,
                     projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null,
                     goods.kindid,
                     kindidname = goodstype_Defa != null && goodstype_Defa.Count() > 0 ? goodstype_Defa.First().kindname : null,
                     goods.typeid,
                     goods.isremind,
                     goods.buildingid,
                     buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null,
                     goods.floorid,
                     floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null,
                     departmentname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().remark : null,
                     goods.remark,
                     goods.sortnum,
                     goods.createtby,
                     goods.isdelete,
                     goods.createtime
                 };
            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.goodsname.Contains(keyword));     //物品名称       
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            if (kindids != null && kindids.Count > 0)
                query = query.Where(it => kindids.Contains(it.kindid));
            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 goods in _collection.AsQueryable()
                 join proje in _collection_pro_project_info.AsQueryable() on goods.projectid equals proje.id into proje_Defa
                 join goodstype in _collection_distri_goods_type.AsQueryable() on goods.kindid equals goodstype.id into goodstype_Defa
                 join building in _collection_sys_building.AsQueryable() on goods.buildingid equals building.id into building_Defa
                 join floor in _collection_sys_floor.AsQueryable() on goods.floorid equals floor.id into floDefa
                 where goods.isdelete == 0 && goods.id == id
                 orderby goods.createtime descending
                 select new
                 {
                     goods.id,
                     goods.goodsname,
                     goods.projectid,
                     projectname = proje_Defa != null && proje_Defa.Count() > 0 ? proje_Defa.First().project_name : null,
                     goods.kindid,
                     kindidname = goodstype_Defa != null && goodstype_Defa.Count() > 0 ? goodstype_Defa.First().kindname : null,
                     goods.typeid,
                     goods.isremind,
                     goods.remark,
                     goods.buildingid,
                     buildingname = building_Defa != null && building_Defa.Count() > 0 ? building_Defa.First().buildname : null,
                     goods.floorid,
                     floorname = floDefa != null && floDefa.Count() > 0 ? floDefa.First().floorname : null,
                     goods.sortnum,
                     goods.createtby,
                     goods.isdelete,
                     goods.createtime
                 };
            var model = query.FirstOrDefault();
            if (model != null)
            {
                return model;
            }
            return null;
        }
    }
}
