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

namespace MessageApi.Repositories
{
    public class App_Version3DRepository : BaseRepository<App_Version3D, string>, IApp_Version3DRepository
    {
        protected readonly ILogger<BaseRepository<App_Version3D, string>> _logger;
        protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
        public App_Version3DRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<App_Version3D, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_pro_project_info = _context.GetCollection<Pro_Project_Info>();
        }

        /// <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> GetListsByPage(int apptype, string projectid, string keyword, string stime, string etime, int pageindex, int pagesize, out int recordCount)
        {
            var query = from p in _collection.AsQueryable()
                        join pro in _collection_pro_project_info.AsQueryable() on p.projectid equals pro.id into proDefa
                        where p.isdelete == 0
                        orderby p.createtime descending
                        select new
                        {
                            p.id,
                            p.apptype,
                            p.projectid,
                            projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                            p.versionname,
                            p.versioncode,
                            p.downurl,
                            p.createby,
                            p.createtime,
                            p.updatetime,
                            p.note
                        };

            #region 查询条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.versionname.Contains(keyword) || it.versioncode.Contains(keyword));
            if (apptype > 0)
                query = query.Where(it => it.apptype == apptype);
            if (!string.IsNullOrEmpty(projectid))
                query = query.Where(it => it.projectid == projectid);
            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"));

            #endregion

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

        /// <summary>
        /// 详情 - 获取工单详情 by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            var query = from p in _collection.AsQueryable()
                        join pro in _collection_pro_project_info.AsQueryable() on p.projectid equals pro.id into proDefa
                        where p.id == id
                        select new
                        {
                            p.id,
                            p.apptype,
                            p.projectid,
                            projectname = proDefa != null && proDefa.Count() > 0 ? proDefa.First().project_name : null,
                            p.versionname,
                            p.versioncode,
                            p.downurl,
                            p.createby,
                            p.createtime,
                            p.updatetime,
                            p.note
                        };
            var info = query.FirstOrDefault();
            return info;
        }
    }
}
