using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using MadRunFabric.Model;
using MadRunFabric.Model.MessageApi;
using MessageApi.IRepositories;
using MessageApi.Model.Dto;
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 Sys_Msg_NoticeInfoRepository : BaseRepository<Sys_Msg_NoticeInfo, string>, ISys_Msg_NoticeInfoRepository
    {
        protected readonly ILogger<BaseRepository<Sys_Msg_NoticeInfo, string>> _logger;
        protected readonly IMongoCollection<Sys_User_Account> _collection_sys_useraccountvalue;
        public Sys_Msg_NoticeInfoRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_Msg_NoticeInfo, string>> logger) : base(settings, logger)
        {
            _logger = logger;
            _collection_sys_useraccountvalue = _context.GetCollection<Sys_User_Account>();
        }

        /// <summary>
        /// 公告列表 -- 关联查询
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="projectid"></param>
        /// <param name="typeid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetListByPage(string keyword,string rolecode,string usercode, string stime, string etime,int isread, int pageIndex, int pageSize, out int recordCount)
        {
            DateTime dtnow = DateTime.Now;
            var query = from p in _collection.AsQueryable()
                        where p.isdelete == 0 && (p.rolecode == null || p.rolecode == "" || p.rolecode.Contains(rolecode)) && (p.usercode == null || p.usercode == "" || p.usercode.Contains(usercode))   //权限（角色，用户）
                        select new
                        {
                            id = p.id,
                            title = p.title,
                            content = p.content,
                            startdate = p.startdate,
                            enddate = p.enddate,
                            usercode = p.usercode,
                            rolecode = p.rolecode,
                            createby = p.createby,
                            createon = p.createon,
                            modifyby = p.modifyby,
                            modifydate = p.modifydate,
                            isdelete = p.isdelete,
                            deleteuser = p.deleteuser,
                            deletetime = p.deletetime,
                            isread=p.isread 
                        };

            #region 条件
            if (!string.IsNullOrEmpty(keyword))
                query = query.Where(it => it.title.Contains(keyword) || it.content.Contains(keyword));
            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => it.createon >= Convert.ToDateTime(stime + " 00:00:00"));
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => it.createon <= Convert.ToDateTime(etime + " 23:59:59"));
            if (isread > 0)
            {
                query = query.Where(it => it.isread .Equals (isread ));
            }
            #endregion

            recordCount = query.Count();
            var list = query.OrderByDescending(it => it.createon).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return list;
        }

        /// <summary>
        /// 详情 - Linq 关联查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetDetails(string id)
        {
            var query = from p in _collection.AsQueryable()
                        where p.isdelete == 0 && p.id == id
                        select new
                        {
                            id = p.id,
                            title = p.title,
                            content = p.content,
                            startdate = p.startdate,
                            enddate = p.enddate,
                            usercode = p.usercode,
                            rolecode = p.rolecode,
                            createby = p.createby,
                            createon = p.createon,
                            modifyby = p.modifyby,
                            modifydate = p.modifydate,
                            isdelete = p.isdelete,
                            deleteuser = p.deleteuser,
                            deletetime = p.deletetime,
                            isread=p.isread 
                        };

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