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

namespace EquipmentApi.Repositories
{
    public class Wo_MattersRepository : BaseRepository<Wo_Matters, string>, IWo_MattersRepository
    {
        protected readonly ILogger<BaseRepository<Wo_Matters, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        protected readonly IMongoCollection<Sys_User_Account> _collection_sys_user_account;
        public Wo_MattersRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Wo_Matters, string>> logger) : base(settings, logger)
        {
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();//获取集合
            _collection_sys_user_account = _context.GetCollection<Sys_User_Account>();
            _logger = logger;
        }
        public IEnumerable<object> GetListsByPage(string projectid, string key, int type, int pageindex, int pagesize, out int recordCount)
        {
            try
            {
                //关联查询
                var query =
                    from mat in _collection.AsQueryable()
                    join proje in _collection_pro_project_info.AsQueryable() on mat.projectid equals proje.id into projeDefa
                    join users in _collection_sys_user_account.AsQueryable() on mat.createuser equals users.usercode into createuserDefa
                    where mat.isdelete == false
                    orderby mat.createtime descending
                    select new 
                    {
                        id = mat.id,
                        checktype = mat.checktype,
                        matter = mat.matter,
                        projectid = mat.projectid,
                        projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        contents = mat.contents,
                        createtime = mat.createtime,
                        createuser = mat.createuser,
                        createusername = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.First().username : null,
                        isdelete = mat.isdelete
                    };
                #region 查询条件
                if (!string.IsNullOrWhiteSpace(projectid))
                    query = query.Where(it => it.projectid == projectid);
                if (!string.IsNullOrEmpty(key))
                    query = query.Where(it => it.matter.Contains(key) || it.contents.Contains(key));
                if (type > 0)
                    query = query.Where(it => it.checktype == type);

                #endregion

                recordCount = query.Count();
                var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
                return list;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                throw ex;
            }
        }
        public IEnumerable<object> GetLists(string projectid, int type)
        {
            try
            {
                //关联查询
                var query =
                    from mat in _collection.AsQueryable()
                    join proje in _collection_pro_project_info.AsQueryable() on mat.projectid equals proje.id into projeDefa
                    join users in _collection_sys_user_account.AsQueryable() on mat.createuser equals users.usercode into createuserDefa
                    where mat.isdelete == false
                    orderby mat.createtime descending
                    select new 
                    {
                        id = mat.id,
                        checktype = mat.checktype,
                        matter = mat.matter,
                        projectid = mat.projectid,
                        projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        contents = mat.contents,
                        createtime = mat.createtime,
                        createuser = mat.createuser,
                        createusername = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.First().username : null,
                        isdelete = mat.isdelete
                    };
                #region 查询条件
                if (!string.IsNullOrWhiteSpace(projectid))
                    query = query.Where(it => it.projectid == projectid);
                if (type > 0)
                    query = query.Where(it => it.checktype == type);
                #endregion

                var list = query;
                return list;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                throw ex;
            }
        }
        public object GetDetails(string id)
        {
            try
            {
                //关联查询
                var query =
                    from mat in _collection.AsQueryable()
                    join proje in _collection_pro_project_info.AsQueryable() on mat.projectid equals proje.id into projeDefa
                    join users in _collection_sys_user_account.AsQueryable() on mat.createuser equals users.usercode into createuserDefa
                    where mat.isdelete == false && mat.id == id
                    orderby mat.createtime descending
                    select new 
                    {
                        id = mat.id,
                        checktype = mat.checktype,
                        matter = mat.matter,
                        projectid = mat.projectid,
                        projectname = projeDefa != null && projeDefa.Count() > 0 ? projeDefa.First().project_name : null,
                        contents = mat.contents,
                        createtime = mat.createtime,
                        createuser = mat.createuser,
                        createusername = createuserDefa != null && createuserDefa.Count() > 0 ? createuserDefa.Where(c => c.delete_flag == false).First().username : null,
                        isdelete = mat.isdelete
                    };

                var info = query.FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                throw ex;
            }
        }
    }
}
