using Edu.AOP.CustomerAttribute;
using Edu.Common.Enum;
using Edu.Common.Enum.Question;
using Edu.Model.ViewModel.Question;
using Edu.Repository.Question;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using VT.FW.DB;

namespace Edu.Module.Question
{
    /// <summary>
    /// 题库处理类
    /// </summary>
    public class QuestionModule
    {
        /// <summary>
        /// 题目类型仓储层对象
        /// </summary>
        private readonly RB_Question_TypeRepository question_TypeRepository = new RB_Question_TypeRepository();

        /// <summary>
        /// 问题仓储层对象
        /// </summary>
        private readonly RB_QuestionRepository questionRepository = new RB_QuestionRepository();

        /// <summary>
        /// 知识点仓储层对象
        /// </summary>
        private readonly RB_Question_PointRepository question_PointRepository = new RB_Question_PointRepository();

        /// <summary>
        /// 问题解析处理类
        /// </summary>
        private readonly AnalysisQuestionTypeModule analysisQuestion = new AnalysisQuestionTypeModule();

        /// <summary>
        /// 题库仓储层对象
        /// </summary>
        private readonly RB_Question_BankRepository question_BankRepository = new RB_Question_BankRepository();

        /// <summary>
        /// 获取题库分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_Bank_ViewModel> GetQuestionBankPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Question_Bank_ViewModel query)
        {
            return question_BankRepository.GetQuestionBankPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 新增修改题库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetQuestionBankModule(RB_Question_Bank_ViewModel model)
        {
            bool flag;
            if (model.BankId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Question_Bank_ViewModel.BankName), model.BankName },
                    {nameof(RB_Question_Bank_ViewModel.BankNo), model.BankNo },
                };
                flag = question_BankRepository.Update(fileds, new WhereHelper(nameof(RB_Question_Bank_ViewModel.BankId), model.BankId));
            }
            else
            {
                var newId = question_BankRepository.Insert(model);
                model.BankId = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取题库
        /// </summary>
        /// <param name="BankId"></param>
        /// <returns></returns>
        public RB_Question_Bank_ViewModel GetQuestionBankModule(object BankId)
        {
            return question_BankRepository.GetEntity<RB_Question_Bank_ViewModel>(BankId);
        }

        /// <summary>
        /// 删除题库
        /// </summary>
        /// <param name="bankId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool RemoveQuestionBankModule(int bankId, int status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Question_Bank_ViewModel.Status),status }
            };
            return question_BankRepository.Update(fileds, new WhereHelper(nameof(RB_Question_Bank_ViewModel.BankId), bankId));
        }

        /// <summary>
        /// 获取题目类型列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_Type_ViewModel> GetQuestionTypeListModule(RB_Question_Type_ViewModel query)
        {
            return question_TypeRepository.GetQuestionTypeListRepository(query);
        }

        /// <summary>
        /// 获取问题分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_ViewModel> GetQuestionPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Question_ViewModel query)
        {
            return questionRepository.GetQuestionPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取问题列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_ViewModel> GetQuestionListModule(RB_Question_ViewModel query)
        {
            return questionRepository.GetQuestionListRepository(query);
        }

        /// <summary>
        /// 新增修改题目
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual bool SetQuestionModule(RB_Question_ViewModel model)
        {
            bool flag;
            if (model.QuestionId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Question_ViewModel.Title),model.Title },
                    {nameof(RB_Question_ViewModel.QuestionContent),(!string.IsNullOrEmpty(model.QuestionContent)?model.QuestionContent:"") },
                    {nameof(RB_Question_ViewModel.DifficultyType),(int)model.DifficultyType },
                    {nameof(RB_Question_ViewModel.AnswerParse),model.AnswerParse },
                    {nameof(RB_Question_ViewModel.QuestionTypeId),model.QuestionTypeId },
                    {nameof(RB_Question_ViewModel.QuestionTypeKey),model.QuestionTypeKey },
                    {nameof(RB_Question_ViewModel.Knowledge),model.Knowledge },
                    {nameof(RB_Question_ViewModel.IsUpdateJobExam),model.IsUpdateJobExam },
                    {nameof(RB_Question_ViewModel.UpdateBy),model.UpdateBy },
                    {nameof(RB_Question_ViewModel.UpdateTime),model.UpdateTime },
                    {nameof(RB_Question_ViewModel.Answer),model.Answer },
                    {nameof(RB_Question_ViewModel.IsMutex),model.IsMutex },
                    {nameof(RB_Question_ViewModel.Category),model.Category },
                    {nameof(RB_Question_ViewModel.LevelType),model.LevelType },
                };
                flag = questionRepository.Update(fileds, new WhereHelper(nameof(RB_Question_ViewModel.QuestionId), model.QuestionId));
            }
            else
            {
                var newId = questionRepository.Insert(model);
                model.QuestionId = newId;
                flag = newId > 0;
                if (flag)
                {
                    Dictionary<string, object> sortFileds = new Dictionary<string, object>()
                    {
                       {nameof(RB_Question_ViewModel.SortNum),newId },
                    };
                    flag = questionRepository.Update(sortFileds, new WhereHelper(nameof(RB_Question_ViewModel.QuestionId), newId));
                }
            }
            return flag;
        }

        /// <summary>
        /// 批量修改问题考级程度
        /// </summary>
        /// <param name="levelTypeEnum"></param>
        /// <param name="questionIdList"></param>
        /// <returns></returns>
        public bool SetBatchQuestionLevelModule(LevelTypeEnum levelTypeEnum, List<int> questionIdList)
        {
            return questionRepository.SetBatchQuestionLevelRepository(levelTypeEnum, questionIdList);
        }

        /// <summary>
        /// 批量添加问题
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public virtual bool SetQuestionBatchModule(List<RB_Question_ViewModel> list)
        {
            return questionRepository.InsertBatch(list);
        }

        /// <summary>
        /// 删除问题
        /// </summary>
        /// <param name="QuestionId"></param>
        /// <returns></returns>
        public bool RemoveQuestionModule(object QuestionId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Question_ViewModel.Status),(int)DateStateEnum.Delete},
            };
            bool flag = questionRepository.Update(fileds, new WhereHelper(nameof(RB_Question_ViewModel.QuestionId), QuestionId));
            return flag;
        }

        /// <summary>
        /// 更新问题排序
        /// </summary>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool UpdateQuestionSortModule(int curQId, int targetQId)
        {
            var currentModel = GetQuestionModule(curQId);
            var targetModel = GetQuestionModule(targetQId);
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Question_ViewModel.SortNum),targetModel.SortNum},
            };
            bool flag = questionRepository.Update(fileds, new WhereHelper(nameof(RB_Question_ViewModel.QuestionId), curQId));
            if (flag)
            {
                //排序修改
                Dictionary<string, object> fileds2 = new Dictionary<string, object>()
                {
                    {nameof(RB_Question_ViewModel.SortNum),currentModel.SortNum},
                };
                flag = questionRepository.Update(fileds2, new WhereHelper(nameof(RB_Question_ViewModel.QuestionId), targetQId));
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取问题实体
        /// </summary>
        /// <param name="QuestionId"></param>
        /// <returns></returns>
        public RB_Question_ViewModel GetQuestionModule(object QuestionId,int isReplaceClozeTitle=0)
        {
            var extModel = questionRepository.GetEntity<RB_Question_ViewModel>(QuestionId);
            if (extModel != null)
            {
                if (extModel.QuestionTypeId > 0)
                {
                   extModel.QuestionTypeName= question_TypeRepository.GetEntity(extModel.QuestionTypeId)?.Name ?? "";
                }
                extModel.QuestionContentObj = ParsingQuestionModule(extModel.QuestionTypeKey, extModel.QuestionContent);
                if (!string.IsNullOrEmpty(extModel.Knowledge))
                {
                    extModel.QuestionPointList = GetPointListModule(new RB_Question_Point_ViewModel() { QPointIds = extModel.Knowledge });
                }
                if (extModel.QuestionTypeKey == "cloze" && !string.IsNullOrEmpty(extModel.Title)&& isReplaceClozeTitle==1)
                {
                    string categoryRule1 = @"##[1234567890]##";
                    Regex categoryReg = new Regex(categoryRule1);
                    MatchCollection matchs = categoryReg.Matches(extModel.Title);
                    foreach (var item in matchs)
                    {
                        if (item != null && !string.IsNullOrEmpty(item.ToString()))
                        {
                            var content = item.ToString().Replace("#", "");
                            extModel.Title = extModel.Title.Replace(item.ToString(), Common.ConvertHelper.GetSpanString(content, isAddNbsp: true));
                        }
                    }
                }
            }
            return extModel;
        }

        /// <summary>
        /// 获取问题选项相关信息
        /// </summary>
        /// <param name="questionKey"></param>
        /// <param name="questionContent"></param>
        /// <returns></returns>
        public object ParsingQuestionModule(string questionKey, string questionContent)
        {
            return analysisQuestion.ParsingQuestion(questionKey, questionContent);
        }


        /// <summary>
        /// 获取问题知识点列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_Point_ViewModel> GetPointListModule(RB_Question_Point_ViewModel query)
        {
            return question_PointRepository.GetPointListRepository(query);
        }

        /// <summary>
        /// 获取知识点分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_Point_ViewModel> GetPointPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Question_Point_ViewModel query)
        {
            return question_PointRepository.GetPointPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 根据知识点编号获取知识点
        /// </summary>
        /// <param name="PointId"></param>
        /// <returns></returns>
        public RB_Question_Point_ViewModel GetQuestionPointModule(object PointId)
        {
            return question_PointRepository.GetEntity<RB_Question_Point_ViewModel>(PointId);
        }

        /// <summary>
        /// 新增修改知识点
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetQuestionPointModule(RB_Question_Point_ViewModel model)
        {
            bool flag;
            if (model.PointId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Question_Point_ViewModel.PointName),model.PointName.Trim() }
                };
                flag = question_PointRepository.Update(fileds, new WhereHelper(nameof(RB_Question_Point_ViewModel.PointId), model.PointId));
            }
            else
            {
                var newId = question_PointRepository.Insert(model);
                model.PointId = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 根据编号删除知识点
        /// </summary>
        /// <param name="PointId"></param>
        /// <returns></returns>
        public bool RemoveQuestionPointModule(object PointId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Question_Point_ViewModel.Status), (int)DateStateEnum.Delete }
            };
            bool flag = question_PointRepository.Update(fileds, new WhereHelper(nameof(RB_Question_Point_ViewModel.PointId), PointId));
            return flag;
        }
    }
}