using Edu.Common.Enum;
using Edu.Common.Enum.User;
using Edu.Model.Entity.User;
using Edu.Model.ViewModel.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.User
{
    /// <summary>
    /// 公告仓储层
    /// </summary>
    public class RB_NoticeRepository : BaseRepository<RB_Notice>
    {
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Notice_ViewModel> GetPageList(int pageIndex, int pageSize, out long count, RB_Notice_ViewModel demodel)
        {
            var parameters = new DynamicParameters();
            string where = $@" 1=1 AND {nameof(RB_Notice.Status)}=0 ";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_Notice.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and {nameof(RB_Notice.School_Id)} ={demodel.School_Id}";
            }
            if (!string.IsNullOrEmpty(demodel.Title))
            {
                where += $@" AND {nameof(RB_Notice.Title)} LIKE @Title ";
                parameters.Add("Title", "%" + demodel.Title.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(demodel.Number))
            {
                where += $@" and {nameof(RB_Notice.Number)} LIKE @Number ";
                parameters.Add("Number", "%" + demodel.Number.Trim() + "%");
            }
            if (demodel.NoticeState > 0)
            {
                where += $@" and {nameof(RB_Notice.NoticeState)} ={(int)demodel.NoticeState}";
            }
            if (demodel.Is_Top > 0)
            {
                where += $@" and {nameof(RB_Notice.Is_Top)} ={demodel.Is_Top}";
            }
            if (demodel.UpdateBy > 0)
            {
                where += $@" and {nameof(RB_Notice.UpdateBy)} ={demodel.UpdateBy}";
            }

            string OrderBy = " Id desc";
            if (demodel.OrderBy == 1)
            {
                OrderBy = " UpdateTime desc";
            }
            else if (demodel.OrderBy == 2)
            {
                OrderBy = " Is_Top asc,UpdateTime desc";
            }
            string sql = $@" select * from RB_Notice where {where} order by {OrderBy}";
            return GetPage<RB_Notice_ViewModel>(pageIndex, pageSize, out count, sql, parameters).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Notice_ViewModel> GetList(RB_Notice_ViewModel demodel)
        {
            string where = $@" 1=1 and {nameof(RB_Notice.Status)}=0 ";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_Notice.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and {nameof(RB_Notice.School_Id)} ={demodel.School_Id}";
            }
            if (!string.IsNullOrEmpty(demodel.Title))
            {
                where += $@" and {nameof(RB_Notice.Title)} like '%{demodel.Title}%'";
            }
            if (demodel.NoticeState > 0)
            {
                where += $@" and {nameof(RB_Notice.NoticeState)} ={(int)demodel.NoticeState}";
            }
            if (demodel.Is_Top > 0)
            {
                where += $@" and {nameof(RB_Notice.Is_Top)} ={demodel.Is_Top}";
            }

            string sql = $@" select * from RB_Notice where {where}";
            return Get<RB_Notice_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取最大的编码
        /// </summary>
        /// <param name="numberRule"></param>
        /// <returns></returns>
        public string GetNoticeNumberMax(NoticeRuleEnum numberRule, int GroupId)
        {
            string sql = $@" select Number from RB_Notice where NumberRule ={(int)numberRule} and Group_Id ={GroupId} and Status=0 order by Id desc limit 1";
            var obj = ExecuteScalar(sql);
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Notice_ViewModel> GetMyPageList(int pageIndex, int pageSize, out long count, RB_Notice_ViewModel demodel)
        {
            string where = $@" 1=1 and n.{nameof(RB_Notice.Status)}=0 ";
            if (demodel.Group_Id > 0)
            {
                where += $@" and n.{nameof(RB_Notice.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and n.{nameof(RB_Notice.School_Id)} ={demodel.School_Id}";
            }
            if (!string.IsNullOrEmpty(demodel.Title))
            {
                where += $@" and n.{nameof(RB_Notice.Title)} like '%{demodel.Title}%'";
            }
            if (!string.IsNullOrEmpty(demodel.Number))
            {
                where += $@" and n.{nameof(RB_Notice.Number)} like '%{demodel.Number}%'";
            }
            if (demodel.NoticeState > 0)
            {
                where += $@" and n.{nameof(RB_Notice.NoticeState)} ={(int)demodel.NoticeState}";
            }
            if (demodel.Is_Top > 0)
            {
                where += $@" and n.{nameof(RB_Notice.Is_Top)} ={demodel.Is_Top}";
            }
            if (demodel.UpdateBy > 0)
            {
                where += $@" and n.{nameof(RB_Notice.UpdateBy)} ={demodel.UpdateBy}";
            }
            if (!string.IsNullOrEmpty(demodel.To))
            {
                where += $@" and (FIND_IN_SET(d.DeptId,'{demodel.To}') or n.To =-1)";
            }
            if (!string.IsNullOrEmpty(demodel.LookTime))
            {
                where += $@" and n.{nameof(RB_Notice.UpdateTime)} >'{demodel.LookTime}'";
            }

            string OrderBy = " n.Id desc";
            if (demodel.OrderBy == 1)
            {
                OrderBy = " n.UpdateTime desc";
            }
            else if (demodel.OrderBy == 2)
            {
                OrderBy = " n.Is_Top asc,n.UpdateTime desc";
            }

            string sql = $@" 
SELECT n.* 
FROM RB_Notice n LEFT JOIN rb_notice_dept d on n.Id = d.NoticeId
WHERE {where} 
GROUP BY n.Id 
ORDER BY {OrderBy}";
            return GetPage<RB_Notice_ViewModel>(pageIndex, pageSize, out count, sql).ToList();
        }





        /// <summary>
        /// 我审批的公告
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Notice_ViewModel> GetMyPageLogList(int pageIndex, int pageSize, out long count, RB_Notice_ViewModel demodel)
        {
            var parameters = new DynamicParameters();
            string where = $@" 1=1 AND b.{nameof(RB_Notice.Status)}=0 ";
            if (demodel.Group_Id > 0)
            {
                where += $@" and b.{nameof(RB_Notice.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and b.{nameof(RB_Notice.School_Id)} ={demodel.School_Id}";
            }
            if (!string.IsNullOrEmpty(demodel.Title))
            {
                where += $@" AND b.{nameof(RB_Notice.Title)} LIKE @Title ";
                parameters.Add("Title", "%" + demodel.Title.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(demodel.Number))
            {
                where += $@" and b.{nameof(RB_Notice.Number)} LIKE @Number ";
                parameters.Add("Number", "%" + demodel.Number.Trim() + "%");
            }
            //if (demodel.NoticeState > 0)
            //{
            //    where += $@" and b.{nameof(RB_Notice.NoticeState)} ={(int)demodel.NoticeState}";
            //}
            if (demodel.Is_Top > 0)
            {
                where += $@" and b.{nameof(RB_Notice.Is_Top)} ={demodel.Is_Top}";
            }
            if (demodel.CreateBy > 0)//审核人
            {
                where += $@" and a.CreateBy ={demodel.CreateBy}";
            }
            if (demodel.UpdateBy > 0)
            {
                where += $@" and b.{nameof(RB_Notice.UpdateBy)} ={demodel.UpdateBy}";
            }
            string OrderBy = " a.CLogId desc";
            if (demodel.OrderBy == 1)
            {
                OrderBy = " b.UpdateTime desc";
            }
            else if (demodel.OrderBy == 2)
            {
                OrderBy = " b.Is_Top asc,b.UpdateTime desc";
            }
            string sql = $@" select b.*,a.ReviewStatus,a.CreateTime as ReviewTime,a.LogContent from rb_notice_log as a LEFT   JOIN rb_notice as b on a.NoticeId=b.Id   where {where} order by {OrderBy}";
            return GetPage<RB_Notice_ViewModel>(pageIndex, pageSize, out count, sql, parameters).ToList();
        }
    }
}