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

namespace Edu.Repository.OKR
{
    /// <summary>
    /// OKR评论仓储层
    /// </summary>
    public class RB_OKR_CommentRepository : BaseRepository<RB_OKR_Comment>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_Comment_ViewModel> GetPageList(int pageIndex, int pageSize, out long rowsCount, RB_OKR_Comment_ViewModel demodel)
        {
            string where = $@" 1=1 and State <> 3";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.State > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.State)} ={demodel.State}";
            }
            if (demodel.CreateBy > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.CreateBy)} ={demodel.CreateBy}";
            }
            if (demodel.AccountId > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.AccountId)} ={demodel.AccountId}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.Type)} ={demodel.Type}";
            }
            if (demodel.TargetId > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.TargetId)} ={demodel.TargetId}";
            }
            if (demodel.ParentId > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.ParentId)} ={demodel.ParentId}";
            }
            string sql = $@" select * from RB_OKR_Comment where {where} order by Id desc";
            return GetPage<RB_OKR_Comment_ViewModel>(pageIndex, pageSize, out rowsCount, sql).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_Comment_ViewModel> GetList(RB_OKR_Comment_ViewModel demodel)
        {
            string where = $@" 1=1 and State <> 3";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.State > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.State)} ={demodel.State}";
            }
            if (demodel.CreateBy > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.CreateBy)} ={demodel.CreateBy}";
            }
            if (demodel.AccountId > 0)
            {
                where += $@" and {nameof(RB_OKR_Comment_ViewModel.AccountId)} ={demodel.AccountId}";
            }
            string sql = $@" select * from RB_OKR_Comment where {where} order by Id desc";
            return Get<RB_OKR_Comment_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取分页列表(历史评论+选中评论)
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_OKR_Comment_ViewModel> GetCommentPageList_V2(int pageIndex, int pageSize, out long rowsCount, RB_OKR_Comment_ViewModel demodel)
        {
            string where = $@" 1=1 and c.State <> 3 and c.ParentId =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.State > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.State)} ={demodel.State}";
            }
            if (demodel.CreateBy > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.CreateBy)} ={demodel.CreateBy}";
            }
            if (demodel.AccountId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.AccountId)} ={demodel.AccountId}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.Type)} ={demodel.Type}";
            }
            if (demodel.TargetId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.TargetId)} ={demodel.TargetId}";
            }
            if (demodel.ParentId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.ParentId)} ={demodel.ParentId}";
            }
            if (demodel.SelectType == 1)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.Type)} in(1,2,3)";
            }

            string sql = $@" select c.*,e.EmName as UpdateByName from RB_OKR_Comment c
left join rb_employee e on c.UpdateBy = e.EmployeeId
where {where} order by c.Id asc";
            return GetPage<RB_OKR_Comment_ViewModel>(pageIndex, pageSize, out rowsCount, sql).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_Comment_ViewModel> GetList_V2(RB_OKR_Comment_ViewModel demodel)
        {
            string where = $@" 1=1 and c.State <> 3";
            if (demodel.Group_Id > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.State > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.State)} ={demodel.State}";
            }
            if (demodel.CreateBy > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.CreateBy)} ={demodel.CreateBy}";
            }
            if (demodel.AccountId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.AccountId)} ={demodel.AccountId}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.Type)} ={demodel.Type}";
            }
            if (demodel.TargetId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.TargetId)} ={demodel.TargetId}";
            }
            if (demodel.ParentId > 0)
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.ParentId)} ={demodel.ParentId}";
            }
            if (!string.IsNullOrEmpty(demodel.ParentIds))
            {
                where += $@" and c.{nameof(RB_OKR_Comment_ViewModel.ParentId)} in({demodel.ParentIds})";
            }

            string sql = $@" select c.*,e.EmName as CreateByName,e.EmPhoto as CreateByIco from RB_OKR_Comment c
left join rb_employee e on c.CreateBy = e.EmployeeId
where {where} order by c.Id asc";
            return Get<RB_OKR_Comment_ViewModel>(sql).ToList();
        }
    }
}
