using Edu.Model.Entity.Exam;
using Edu.Model.ViewModel.Exam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Exam
{
    /// <summary>
    /// 试卷仓储层
    /// </summary>
    public class RB_Examination_PaperRepository : BaseRepository<RB_Examination_Paper>
    {
        /// <summary>
        /// 获取试卷分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Paper_ViewModel> GetExaminationPaperPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Paper_ViewModel query)
        {
            string tempSql = string.Format(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.ParentId), 0);
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Examination_Paper AS A
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.Status),(int)Common.Enum.DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.PaperId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.PaperId), query.PaperId);
                }
                else if (!string.IsNullOrEmpty(query.QPaperIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Examination_Paper_ViewModel.PaperId), query.QPaperIds);
                }
                else
                {
                    if (!string.IsNullOrEmpty(query.QStartTime))
                    {
                        builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Examination_Paper_ViewModel.CreateTime), query.QStartTime);
                    }
                    if (!string.IsNullOrEmpty(query.QEndTime))
                    {
                        builder.AppendFormat(@" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Examination_Paper_ViewModel.CreateTime), query.QEndTime);
                    }
                    if (!string.IsNullOrEmpty(query.PaperName))
                    {
                        builder.AppendFormat(" AND A.{0} LIKE @PaperName ", nameof(RB_Examination_Paper_ViewModel.PaperName));
                        parameters.Add("PaperName", "%" + query.PaperName.Trim() + "%");
                        tempSql = "";
                    }
                    if (query.ParentId > 0)
                    {
                        builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.ParentId), query.ParentId);
                        tempSql = "";
                    }
                    //没有权限的时候查询
                    if (query.CreateBy > 0)
                    {
                        builder.AppendFormat(@" AND 
                                (  
                                    (A.{0}={1}  {2} ) 
                                 OR (A.{0}<>{1} AND A.IsOpen=2 )
                                ) ", nameof(RB_Examination_Paper_ViewModel.CreateBy), query.CreateBy, (query.ParentId > 0 ? "" : " AND A.ParentId=0 "));
                        tempSql = "";
                    }
                    //查询条件中的创建人
                    if (query.QCreateBy > 0)
                    {
                        builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.CreateBy), query.QCreateBy);
                    }
                    if (query.QExamineStatus > -1)
                    {
                        tempSql = "";
                        //正常
                        if (query.QExamineStatus == 2)
                        {
                            builder.AppendFormat(@" AND A.{0} IN(2,5) AND A.PaperType=2 ", nameof(RB_Examination_Paper_ViewModel.ExamineStatus));
                        }
                        //驳回
                        else if (query.QExamineStatus == 3)
                        {
                            builder.AppendFormat(@" AND A.{0} IN(3,4) AND A.PaperType=2 ", nameof(RB_Examination_Paper_ViewModel.ExamineStatus));
                        }
                        else
                        {
                            builder.AppendFormat(@" AND A.{0}={1} AND A.PaperType=2 ", nameof(RB_Examination_Paper_ViewModel.ExamineStatus), query.QExamineStatus);
                        }
                    }
                    builder.AppendFormat(tempSql);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} ASC,A.{0} DESC,A.CreateTime DESC ", nameof(RB_Examination_Paper_ViewModel.PaperType),nameof(RB_Examination_Paper_ViewModel.PaperId));
            return GetPage<RB_Examination_Paper_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取试卷列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Paper_ViewModel> GetExaminationPaperListRepository(RB_Examination_Paper_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Examination_Paper AS A
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.Status), (int)Common.Enum.DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.PaperName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @PaperName ", nameof(RB_Examination_Paper_ViewModel.PaperName));
                    parameters.Add("PaperName", "%" + query.PaperName.Trim() + "%");
                }
                if (query.ParentId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.ParentId), query.ParentId);
                }
                if (query.PaperId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.PaperId), query.PaperId);
                }
                if (!string.IsNullOrEmpty(query.QPaperIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Examination_Paper_ViewModel.PaperId), query.QPaperIds);
                }
                //查询文件夹
                if (query.IsQueryFolder == 1)
                {
                    builder.AppendFormat(@" AND A.{0}=1 ", nameof(RB_Examination_Paper_ViewModel.PaperType));
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Paper_ViewModel.CreateBy), query.CreateBy);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Examination_Paper_ViewModel.PaperId));
            return Get<RB_Examination_Paper_ViewModel>(builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取试卷所有下级列表
        /// </summary>
        /// <param name="QPaperIds"></param>
        /// <returns></returns>
        public List<RB_Examination_Paper_ViewModel> GetExaminationPaperChildRepository(string QPaperIds)
        {
            var parameters = new DynamicParameters();
            string procName = "proc_getchildpaper";
            parameters.Add("QPaperIds", QPaperIds, direction: ParameterDirection.Input);
            return Get<RB_Examination_Paper_ViewModel>(procName, parameters, commandType: CommandType.StoredProcedure).ToList();
        }

        /// <summary>
        /// 获取当前节点的所有父级节点
        /// </summary>
        /// <param name="QPaperIds"></param>
        /// <returns></returns>
        public List<RB_Examination_Paper_ViewModel> GetExaminationPaperParentRepository(string QPaperIds)
        {
            var parameters = new DynamicParameters();
            string procName = "proc_getparentpaper";
            parameters.Add("QPaperIds", QPaperIds, direction: ParameterDirection.Input);
            return Get<RB_Examination_Paper_ViewModel>(procName, parameters, commandType: CommandType.StoredProcedure).ToList();
        }

        /// <summary>
        /// 修改试卷发布次数
        /// </summary>
        /// <param name="PaperId"></param>
        /// <returns></returns>
        public int UpdateExaminationPaperPublishCountRepository(int PaperId)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" UPDATE RB_Examination_Paper SET PublishCount=PublishCount+1 WHERE PaperId={0} ", PaperId);
            return base.Execute(builder.ToString());
        }

        /// <summary>
        /// 批量修改试卷【开放/私有】状态
        /// </summary>
        /// <param name="paperIds"></param>
        /// <param name="IsOpen"></param>
        /// <returns></returns>

        public int BatchSetIsOpenRepository(List<int> paperIds, int IsOpen)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" UPDATE RB_Examination_Paper SET IsOpen={0} WHERE PaperId IN({1}) ", IsOpen, string.Join(",", paperIds));
            return base.Execute(builder.ToString());
        }

        /// <summary>
        /// 批量移动文件夹
        /// </summary>
        /// <param name="ParentId"></param>
        /// <param name="paperIds"></param>
        /// <returns></returns>
        public int BatchMovePaperRepository(int ParentId, List<int> paperIds)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" UPDATE RB_Examination_Paper SET ParentId={0} WHERE PaperId IN({1}) ", ParentId, string.Join(",", paperIds));
            return base.Execute(builder.ToString());
        }
    }
}
