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

namespace WorkOrderApi.Repositories
{
    public class Cus_Customer_ProjectRepository : BaseRepository<Cus_Customer_Project, string>, ICus_Customer_ProjectRepository
    {
        protected readonly ILogger<BaseRepository<Cus_Customer_Project, string>> _logger;
        protected readonly IMongoCollection<Sys_Provinces> _collection_sys_provinces;
        protected readonly IMongoCollection<Sys_City> _collection_sys_city;
        protected readonly IMongoCollection<Sys_DictionaryValue> _collection_sys_dictionaryvalue;
        protected readonly IMongoCollection<Sys_Department> _collection_sys_department;
        protected readonly IMongoCollection<Cus_Customer_Base> _collection_cus_customer_base;
        public Cus_Customer_ProjectRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Cus_Customer_Project, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_sys_department = _context.GetCollection<Sys_Department>();
            _collection_sys_provinces = _context.GetCollection<Sys_Provinces>();
            _collection_sys_city = _context.GetCollection<Sys_City>();
            _collection_sys_dictionaryvalue = _context.GetCollection<Sys_DictionaryValue>();
            _collection_cus_customer_base = _context.GetCollection<Cus_Customer_Base>();
        }

        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="ordercode"></param>
        /// <param name="key"></param>
        /// <param name="phone"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="sourceid"></param>
        /// <param name="typeid"></param>
        /// <param name="deptid"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="state"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetProListsByPage(string key, string customerid, string stime, string etime, string projecttypeid, string qdstime, string qdetime, string dqstime, string dqetime,string customertype,string gjstime, string gjetime, int? isend, int pageindex, int pagesize, out int recordCount)
        {
            var query = from p in _collection.AsQueryable()
                        join d in _collection_sys_dictionaryvalue.AsQueryable() on p.project_typeid equals d.id into sys_dictionaryDefa
                        where p.isdelete == 0 orderby p.createtime descending 
                        select new 
                        {
                            p.id,
                            p.customerid,
                            p.name,
                            p.mobilephone,
                            p.telephone,

                            p.customername,
                            p.companyame,
                            //model.province = input.province;    //省
                            //model.city = input.city;            //市
                            p.address,
                            p.project_address,
                            p.project_cusname,
                            p.project_mobilephone,
                            p.project_telephone,
                            p.project_name,
                            p.ordertime,
                            p.acceptancetime,
                            p.orderendtime,
                            p.guarantee_period,
                            p.isend,
                            p.project_amount,
                            p.project_typeid,
                            project_type = sys_dictionaryDefa != null && sys_dictionaryDefa.Count() > 0 ? sys_dictionaryDefa.First().name : null,
                            p.oem_name,
                            p.content,
                            p.servicecontent,
                            p.customertype,
                            p.customerdept,
                            p.customeremail,
                            p.createtime,
                            p.createuser,
                            p.createusername,
                            p.isdelete,
                            p.followtime,
                            p.followcontent
                        };

            #region 查询条件
            if (!string.IsNullOrEmpty(key))
                query = query.Where(it => it.name.Contains(key) || it.mobilephone.Contains(key) || it.address.Contains(key) || it.project_name.Contains(key) || it.project_cusname.Contains(key)||it.companyame.Contains(key));
            if (!string.IsNullOrEmpty(customerid))
                query = query.Where(it => it.customerid == customerid);
            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => it.createtime >= Convert.ToDateTime(stime + " 00:00:00"));
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => it.createtime <= Convert.ToDateTime(etime + " 23:59:59"));
            if (!string.IsNullOrEmpty(projecttypeid))
                query = query.Where(it => it.project_typeid == projecttypeid);
            if (!string.IsNullOrEmpty(qdstime))
                query = query.Where(it => it.ordertime >= Convert.ToDateTime(qdstime + " 00:00:00"));
            if (!string.IsNullOrEmpty(qdetime))
                query = query.Where(it => it.ordertime <= Convert.ToDateTime(qdetime + " 23:59:59"));
            if (!string.IsNullOrEmpty(dqstime))
                query = query.Where(it => it.orderendtime >= Convert.ToDateTime(dqstime + " 00:00:00"));
            if (!string.IsNullOrEmpty(dqetime))
                query = query.Where(it => it.orderendtime <= Convert.ToDateTime(dqetime + " 23:59:59"));
            if (isend !=null )
                query = query.Where(it => it.isend == isend);
            if (!string.IsNullOrEmpty(customertype) )
                query = query.Where(it => it.customertype == customertype);
            
            if (!string.IsNullOrEmpty(gjstime))
                query = query.Where(it =>it.followtime !=null && it.followtime >= Convert.ToDateTime(gjstime + " 00:00:00"));
            if (!string.IsNullOrEmpty(gjetime))
                query = query.Where(it => it.followtime != null  && it.followtime <= Convert.ToDateTime(gjetime + " 23:59:59"));
            #endregion

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

            return list;
        }

        public IEnumerable<Cus_Customer_ProjectDto> GetProLists(string key, string customerid, string stime, string etime, string projecttypeid, string qdstime, string qdetime, string dqstime, string dqetime, string customertype, string gjstime, string gjetime, int? isend, out int recordCount)
        {
            var query = from p in _collection.AsQueryable()
                        join d in _collection_sys_dictionaryvalue.AsQueryable() on p.project_typeid equals d.id into sys_dictionaryDefa
                        where p.isdelete == 0
                        select new Cus_Customer_ProjectDto
                        {
                            id=p.id,
                            customerid=p.customerid,
                            name=p.name,
                            mobilephone=p.mobilephone,
                            telephone=p.telephone,

                            customername=p.customername,
                            companyame=p.companyame,
                            //model.province = input.province;    //省
                            //model.city = input.city;            //市
                            address=p.address,
                            project_address=p.project_address,
                            project_cusname=p.project_cusname,
                            project_mobilephone=p.project_mobilephone,
                            project_telephone=p.project_telephone,
                            project_name=p.project_name,
                            ordertime=p.ordertime,
                            acceptancetime=p.acceptancetime,
                            orderendtime=p.orderendtime,
                            guarantee_period=p.guarantee_period,
                            isend=p.isend,
                            project_amount=p.project_amount,
                            project_typeid=p.project_typeid,
                            project_type = sys_dictionaryDefa != null && sys_dictionaryDefa.Count() > 0 ? sys_dictionaryDefa.First().name : null,
                            oem_name=p.oem_name,
                            content=p.content,
                            servicecontent=p.servicecontent,
                            customertype=p.customertype,
                            customerdept=p.customerdept,
                            customeremail=p.customeremail,
                            createtime=p.createtime,
                            createuser=p.createuser,
                            createusername=p.createusername,
                            isdelete=p.isdelete,
                            followtime=p.followtime,
                            followcontent=p.followcontent
                        };

            #region 查询条件
            if (!string.IsNullOrEmpty(key))
                query = query.Where(it => it.name.Contains(key) || it.mobilephone.Contains(key) || it.address.Contains(key) || it.project_name.Contains(key) || it.project_cusname.Contains(key) || it.companyame.Contains(key));
            if (!string.IsNullOrEmpty(customerid))
                query = query.Where(it => it.customerid == customerid);
            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => it.createtime >= Convert.ToDateTime(stime + " 00:00:00"));
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => it.createtime <= Convert.ToDateTime(etime + " 23:59:59"));
            if (!string.IsNullOrEmpty(projecttypeid))
                query = query.Where(it => it.project_typeid == projecttypeid);
            if (!string.IsNullOrEmpty(qdstime))
                query = query.Where(it => it.ordertime >= Convert.ToDateTime(qdstime + " 00:00:00"));
            if (!string.IsNullOrEmpty(qdetime))
                query = query.Where(it => it.ordertime <= Convert.ToDateTime(qdetime + " 23:59:59"));
            if (!string.IsNullOrEmpty(dqstime))
                query = query.Where(it => it.orderendtime >= Convert.ToDateTime(dqstime + " 00:00:00"));
            if (!string.IsNullOrEmpty(dqetime))
                query = query.Where(it => it.orderendtime <= Convert.ToDateTime(dqetime + " 23:59:59"));
            if (isend != null)
                query = query.Where(it => it.isend == isend);
            //if (!string.IsNullOrEmpty(customertype))
            //    query = query.Where(it => it.customertype == customertype);
            //if (!string.IsNullOrEmpty(customertype) && customertype == "潜在客户")
            //    query = query.Where(it => it.customertype == customertype);
            //else
            //    query = query.Where(it => it.customertype != "潜在客户");
            if (!string.IsNullOrEmpty(customertype))
                query = query.Where(it => it.customertype == customertype);

            if (!string.IsNullOrEmpty(gjstime))
                query = query.Where(it => it.followtime != null && it.followtime >= Convert.ToDateTime(gjstime + " 00:00:00"));
            if (!string.IsNullOrEmpty(gjetime))
                query = query.Where(it => it.followtime != null && it.followtime <= Convert.ToDateTime(gjetime + " 23:59:59"));
            #endregion

            recordCount = query.Count();
            var list = query.ToList();

            return list;
        }

        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="ordercode"></param>
        /// <param name="key"></param>
        /// <param name="phone"></param>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="sourceid"></param>
        /// <param name="typeid"></param>
        /// <param name="deptid"></param>
        /// <param name="stime"></param>
        /// <param name="etime"></param>
        /// <param name="state"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetProLists()
        {
            var query = from p in _collection.AsQueryable()
                        join d in _collection_sys_dictionaryvalue.AsQueryable() on p.project_typeid equals d.id into sys_dictionaryDefa
                        where p.isdelete == 0
                        select new
                        {
                            p.id,
                            p.customerid,
                            p.name,
                            p.mobilephone,
                            p.telephone,

                            p.customername,
                            p.companyame,
                            //model.province = input.province;    //省
                            //model.city = input.city;            //市
                            p.address,
                            p.project_address,
                            p.project_cusname,
                            p.project_mobilephone,
                            p.project_telephone,
                            p.project_name,
                            p.ordertime,
                            p.acceptancetime,
                            p.orderendtime,
                            p.guarantee_period,
                            p.isend,
                            p.project_amount,
                            p.project_typeid,
                            project_type = sys_dictionaryDefa != null && sys_dictionaryDefa.Count() > 0 ? sys_dictionaryDefa.First().name : null,
                            p.oem_name,
                            p.content,
                            p.servicecontent,
                            p.customertype,
                            p.customerdept,
                            p.customeremail,
                            p.createtime,
                            p.createuser,
                            p.createusername,
                            p.isdelete
                        };
            
            var list = query.ToList();

            return list;
        }

        /// <summary>
        /// 详情 - 获取工单详情 by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetProDetails(string id)
        {
            var query = from p in _collection.AsQueryable()
                        join d in _collection_sys_dictionaryvalue.AsQueryable() on p.project_typeid equals d.id into sys_dictionaryDefa
                        where p.id == id
                        select new
                        {
                            p.id,
                            p.customerid,
                            p.name,
                            p.mobilephone,
                            p.telephone,

                            p.customername,
                            p.companyame,
                            //model.province = input.province;    //省
                            //model.city = input.city;            //市
                            p.address,
                           p.project_address,
                           p.project_cusname,
                           p.project_mobilephone,
                           p.project_telephone,
                            p.project_name,
                            p.ordertime,
                            p.acceptancetime,
                            p.orderendtime,
                            p.guarantee_period,
                            p.isend,
                            p.project_amount,
                            p.project_typeid,
                            project_type = sys_dictionaryDefa != null && sys_dictionaryDefa.Count() > 0 ? sys_dictionaryDefa.First().name : null,
                            p.oem_name,
                            p.content,
                            p.servicecontent,
                            p.customertype,
                            p.customerdept,
                            p.customeremail,
                            p.createtime,
                            p.createuser,
                            p.createusername,
                            p.isdelete,
                            p.followtime ,
                            p.followcontent,
                            p.attachment
                        };
            var info = query.FirstOrDefault();
            return info;
        }

        public IEnumerable<object> GetPro()
        {
            var query = from p in _collection.AsQueryable()
                        join d in _collection_sys_dictionaryvalue.AsQueryable() on p.project_typeid equals d.id into sys_dictionaryDefa
                        where p.isdelete == 0
                        select new
                        {
                            //p.id,
                            //p.customerid,
                            //p.name,
                            //p.mobilephone,
                            //p.telephone,

                           // p.customername,
                            //p.companyame,
                            
                            //p.address,
                            //p.project_address,
                            //p.project_cusname,
                            //p.project_mobilephone,
                            //p.project_telephone,
                            p.project_name,
                            //p.ordertime,
                            //p.acceptancetime,
                            //p.orderendtime,
                            //p.guarantee_period,
                            //p.isend,
                            //p.project_amount,
                            //p.project_typeid,
                            //project_type = sys_dictionaryDefa != null && sys_dictionaryDefa.Count() > 0 ? sys_dictionaryDefa.First().name : null,
                            //p.oem_name,
                            //p.content,
                            //p.servicecontent,
                            //p.customertype,
                            //p.customerdept,
                            //p.customeremail,
                            //p.createtime,
                            //p.createuser,
                            //p.createusername,
                            //p.isdelete
                        };

            var list = query.ToList();

            return list;
        }
    }
}
