using CallCenterApi.IRepositories;
using MadRunFabric.Model;
using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace CallCenterApi.Repositories
{
    public class Sys_WorkTimesRepository : BaseRepository<Sys_WorkTimes, string>, ISys_WorkTimesRepository
    {
        protected readonly ILogger<BaseRepository<Sys_WorkTimes, string>> _logger;
        protected readonly IMongoCollection<Sys_SeatGroup> _collection_seatgroup;
        public Sys_WorkTimesRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_WorkTimes, string>> logger) : base(settings, logger)
        {
            _collection_seatgroup = _context.GetCollection<Sys_SeatGroup>();
            _logger = logger;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<object> GetListsByPage(string groupcode, string key, int pageIndex, int pageSize, out int recordCount)
        {
            //关联查询
            var query =
                from wt in _collection.AsQueryable()
                join sg in _collection_seatgroup.AsQueryable() on wt.groupcode equals sg.zxzcode into sgDefa
                where wt.isdelete == false
                orderby wt.createtime descending
                select new 
                {
                    wt.id,
                    wt.groupcode,
                    groupname= sgDefa != null && sgDefa.Count() > 0 ? sgDefa.First().zxzname : null,
                    wt.workstarttimes,
                    wt.workendtimes,
                    wt.type,
                    wt.remark,
                    
                };
            #region 查询条件
            if (!string.IsNullOrEmpty(groupcode))
                query = query.Where(it => it.groupcode == groupcode);
            if (!string.IsNullOrEmpty(key))
                query = query.Where(it => it.remark == key);
            #endregion
            recordCount = query.Count();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            //关联查询
            var query =
                from wt in _collection.AsQueryable()
                join sg in _collection_seatgroup.AsQueryable() on wt.groupcode equals sg.zxzcode into sgDefa
                where wt.isdelete == false && wt.id.Equals(id)
                select new
                {
                    wt.id,
                    wt.groupcode,
                    groupname = sgDefa != null && sgDefa.Count() > 0 ? sgDefa.First().zxzname : null,
                    wt.workstarttimes,
                    wt.workendtimes,
                    wt.type,
                    wt.remark,

                };
            var info = query.FirstOrDefault();
            return info;
        }
    }
}
