using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Data;
using Edu.Common.Enum.Course;
using Edu.Common.Enum.Question;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Question;
using Edu.Module.Question;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class QuestionController : BaseController
    {
        /// <summary>
        /// 题库处理类对象
        /// </summary>
        private readonly QuestionModule questionModule = AOP.AOPHelper.CreateAOPObject<QuestionModule>();

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

        #region 题库管理
        /// <summary>
        /// 获取课程问题分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionBankPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Question_Bank_ViewModel()
            {
                BankId = base.ParmJObj.GetInt("BankId"),
                BankName = base.ParmJObj.GetStringValue("BankName"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            var list = questionModule.GetQuestionBankPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            List<object> resultList = new List<object>();
            int index = 1;
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                resultList.Add(new
                {
                    RowNum = (pageModel.PageIndex - 1) * pageModel.PageSize + index,
                    item.BankId,
                    item.BankNo,
                    item.BankName,
                    item.CreateByName,
                    item.QuestionCount
                });
                index++;
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = resultList;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取题库实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionBank()
        {
            var BankId = base.ParmJObj.GetInt("BankId", 0);
            var extModel = questionModule.GetQuestionBankModule(BankId);
            return ApiResult.Success(data: extModel);
        }


        /// <summary>
        /// 添加修改题库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetQuestionBank()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Question_Bank_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Status = Common.Enum.DateStateEnum.Normal;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.School_Id = base.UserInfo.School_Id;
            bool flag = questionModule.SetQuestionBankModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 删除题库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveQuestionBank()
        {
            var bankId = base.ParmJObj.GetInt("bankId", 0);
            int status = base.ParmJObj.GetInt("status", 0);
            var flag = questionModule.RemoveQuestionBankModule(bankId, status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }


        /// <summary>
        /// 获取题目类型列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionTypeList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Question_Type_ViewModel>(RequestParm.Msg.ToString());
            var list = questionModule.GetQuestionTypeListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取课程问题分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Question_ViewModel()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
                Title = base.ParmJObj.GetStringValue("Title"),
                PointName = base.ParmJObj.GetStringValue("PointName"),
                BankId = base.ParmJObj.GetInt("BankId"),
                IsShowAllQuestion = base.ParmJObj.GetInt("IsShowAllQuestion"),
            };
            try
            {
                var DifficultyTypeList = Common.Plugin.JsonHelper.DeserializeObject<List<int>>(base.ParmJObj.GetStringValue("DifficultyType"));
                if (DifficultyTypeList != null && DifficultyTypeList.Count > 0 && !DifficultyTypeList.Contains(0))
                {
                    query.QDifficultyType = string.Join(",", DifficultyTypeList);
                }
            }
            catch
            {
            }
            try
            {
                var QuestionTypeIdList = Common.Plugin.JsonHelper.DeserializeObject<List<int>>(base.ParmJObj.GetStringValue("QuestionTypeId"));
                if (QuestionTypeIdList != null && QuestionTypeIdList.Count > 0)
                {
                    query.QQuestionTypeStr = string.Join(",", QuestionTypeIdList);
                }
            }
            catch
            {
            }
            try
            {
                var categoryList = Common.Plugin.JsonHelper.DeserializeObject<List<int>>(base.ParmJObj.GetStringValue("QCategoryId"));
                if (categoryList != null && categoryList.Count > 0)
                {
                    query.QCategoryId = string.Join(",", categoryList);
                }
            }
            catch
            {

            }
            var list = questionModule.GetQuestionPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            List<object> resultList = new List<object>();
            int index = 1;
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
                // 定义正则表达式用来匹配 img 标签    
                string str = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
                string newTitle = item.Title;
                try
                {
                    newTitle = Regex.Replace(newTitle, str, "[图片]");
                    newTitle = Regex.Replace(newTitle, @"<iframe\s*[^>]*>", "[音频", RegexOptions.IgnoreCase);
                    newTitle = Regex.Replace(newTitle, @"</iframe>", "]", RegexOptions.IgnoreCase);
                }
                catch
                {
                    newTitle = item.Title;
                }

                var obj = new
                {
                    Number = (pageModel.PageIndex - 1) * pageModel.PageSize + index,
                    item.QuestionId,
                    item.CourseId,
                    Title = newTitle,
                    SourceTitle=item.Title,
                    item.QuestionTypeId,
                    item.QuestionTypeKey,
                    QuestionTypeName = item.QuestionTypeKey != "single-number" ? item.QuestionTypeName : "选择题",
                    item.CreateByName,
                    item.CreateTimeStr,
                    item.DifficultyType,
                    item.CategoryName,
                    DifficultyTypeName = item.DifficultyType.ToName(),
                    item.LevelTypeName,
                    QuestionContent = query.IsShowAllQuestion == 1 ? item.QuestionContent : "",
                    AnswerParse = query.IsShowAllQuestion == 1 ? item.AnswerParse : "",
                    Knowledge = query.IsShowAllQuestion == 1 ? item.Knowledge : "",
                    IsUpdateJobExam = query.IsShowAllQuestion == 1 ? item.IsUpdateJobExam : 0,
                    Answer = query.IsShowAllQuestion == 1 ? item.Answer : "",
                    IsMutex = query.IsShowAllQuestion == 1 ? item.IsMutex : 0,
                    Category = query.IsShowAllQuestion == 1 ? item.Category : 0,
                    LevelType = query.IsShowAllQuestion == 1 ? item.LevelType : 0,
                    BankId = query.IsShowAllQuestion == 1 ? item.BankId : 0,
                };
                resultList.Add(obj);
                index++;
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = resultList;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 根据题库编号获取题库题型列表【试卷组卷使用】
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionTypeStaticList()
        {
            var query = new RB_Question_ViewModel()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
                Title = base.ParmJObj.GetStringValue("Title"),
                PointName = base.ParmJObj.GetStringValue("PointName"),
                BankId = base.ParmJObj.GetInt("BankId"),
                QBankIds = base.ParmJObj.GetStringValue("QBankIds"),
            };
            var data = questionModule.GetQuestionTypeStaticListModule(query);
            return ApiResult.Success(data: data);
        }

        /// <summary>
        /// 根据题库编号获取题库题型分类列表【试卷组卷使用】
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionCategoryStaticList()
        {
            var query = new RB_Question_ViewModel()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
                Title = base.ParmJObj.GetStringValue("Title"),
                PointName = base.ParmJObj.GetStringValue("PointName"),
                BankId = base.ParmJObj.GetInt("BankId"),
                QBankIds = base.ParmJObj.GetStringValue("QBankIds"),
            };
            var data = questionModule.GetQuestionCategoryStaticListModule(query);
            return ApiResult.Success(data: data);
        }

        /// <summary>
        /// 获取问题难易列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HttpGet]
        public ApiResult GetDifficultyType()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(DifficultyTypeEnum));
            if (list != null)
            {
                list.Insert(0, new EnumItem()
                {
                    Id = 0,
                    Name = "全部难度"
                });
            }
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取问题大类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HttpGet]
        public ApiResult GetQuestionCategory()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(QuestionCategoryEnum));
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 问题考级程度
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HttpGet]
        public ApiResult GetQuestionLevelType()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(LevelTypeEnum));
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取课程问题列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Question_ViewModel>(RequestParm.Msg.ToString());
            var list = questionModule.GetQuestionListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改问题
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetQuestion()
        {
            var extModel = new RB_Question_ViewModel()
            {
                Answer = base.ParmJObj.GetStringValue("Answer"),
                AnswerParse = base.ParmJObj.GetStringValue("AnswerParse"),
                CourseId = base.ParmJObj.GetInt("CourseId"),
                DifficultyType = (DifficultyTypeEnum)base.ParmJObj.GetInt("DifficultyType"),
                IsMutex = base.ParmJObj.GetInt("IsMutex"),
                IsUpdateJobExam = base.ParmJObj.GetInt("IsUpdateJobExam"),
                Knowledge = base.ParmJObj.GetStringValue("Knowledge"),
                QuestionContent = base.ParmJObj.GetStringValue("QuestionContent"),
                QuestionId = base.ParmJObj.GetInt("QuestionId"),
                QuestionTypeId = base.ParmJObj.GetInt("QuestionTypeId"),
                QuestionTypeKey = base.ParmJObj.GetStringValue("QuestionTypeKey"),
                SortNum = base.ParmJObj.GetInt("SortNum"),
                Title = base.ParmJObj.GetStringValue("Title"),
                Category = (QuestionCategoryEnum)base.ParmJObj.GetInt("Category"),
                LevelType = (LevelTypeEnum)base.ParmJObj.GetInt("LevelType"),
                BankId = base.ParmJObj.GetInt("BankId")
            };
            if (string.IsNullOrEmpty(extModel.Title))
            {
                return ApiResult.Failed(message: "请填写问题题干!");
            }
            string str = analysisQuestion.CheckQuestion(extModel.QuestionTypeKey, extModel.QuestionContent,extModel.Answer,out string newAnswer);
            if (!string.IsNullOrEmpty(str))
            {
                return ApiResult.Failed(message: str);
            }
            else
            {
                extModel.Answer = newAnswer;
            }
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            bool flag = questionModule.SetQuestionModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 批量修改问题考级程度
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetBatchQuestionLevel()
        {
            var LevelType = (LevelTypeEnum)base.ParmJObj.GetInt("LevelType");
            List<int> questionIds = Common.Plugin.JsonHelper.DeserializeObject<List<int>>(base.ParmJObj.GetStringValue("questionIds"));
            var flag = questionModule.SetBatchQuestionLevelModule(LevelType, questionIds);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程问题
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestion()
        {
            var QuestionId = base.ParmJObj.GetInt("QuestionId", 0);
            int isReplaceClozeTitle = base.ParmJObj.GetInt("isReplaceClozeTitle", 0);
            var extModel = questionModule.GetQuestionModule(QuestionId, isReplaceClozeTitle: isReplaceClozeTitle);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 批量添加修改问题
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetQuestionBeatch()
        {
            bool flag = false;
            var list = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Question_ViewModel>>(RequestParm.Msg.ToString());
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    flag = questionModule.SetQuestionModule(item);
                }
            }
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 删除课程问题
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveQuestion()
        {
            var QuestionId = base.ParmJObj.GetInt("QuestionId", 0);
            var flag = questionModule.RemoveQuestionModule(QuestionId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 更新问题排序
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult UpdateQuestionSort()
        {
            var curQId = base.ParmJObj.GetInt("curQId", 0);
            var targetQId = base.ParmJObj.GetInt("targetQId", 0);
            var flag = questionModule.UpdateQuestionSortModule(curQId, targetQId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 复制问题
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CopyQuestion()
        {
            var questionId = base.ParmJObj.GetInt("QuestionId", 0);
            if (questionId > 0)
            {
                var copyModel = questionModule.GetQuestionModule(questionId);
                if (copyModel != null)
                {
                    copyModel.QuestionId = 0;
                    copyModel.CreateTime = DateTime.Now;
                    copyModel.CreateBy = base.UserInfo.Id;
                    copyModel.UpdateBy = base.UserInfo.Id;
                    copyModel.UpdateTime = DateTime.Now;
                }
                var flag = questionModule.SetQuestionModule(copyModel);
                return flag ? ApiResult.Success() : ApiResult.Failed();
            }
            else
            {
                return ApiResult.Failed(message: "请选择要复制的问题!");
            }
        }

        /// <summary>
        /// 获取课程问题知识点列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPointList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Question_Point_ViewModel>(RequestParm.Msg.ToString());
            var list = questionModule.GetPointListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取知识点分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPointPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Question_Point_ViewModel>(RequestParm.Msg.ToString());
            var list = questionModule.GetPointPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取课程问题知识点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetQuestionPoint()
        {
            var PointId = base.ParmJObj.GetInt("PointId", 0);
            var extModel = questionModule.GetQuestionPointModule(PointId);
            return ApiResult.Success(data: extModel);
        }


        /// <summary>
        /// 添加修改问题知识点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetQuestionPoint()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Question_Point_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            bool flag = questionModule.SetQuestionPointModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 删除课程问题知识点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveQuestionPoint()
        {
            var PointId = base.ParmJObj.GetInt("PointId", 0);
            var flag = questionModule.RemoveQuestionPointModule(PointId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 问题导入
        /// <summary>
        /// 导入Excel问题
        /// </summary>
        /// <returns></returns>
        public ApiResult ImportExcelQuestion(string filePath, int CourseId, int Uid)
        {
            var userInfo = base.GetUserInfo(Uid);
            var resultList = new List<RB_Question_ViewModel>();
            var list = Common.Data.QuestionHelper.GetXlsQuestionData(filePath);
            if (list != null && list.Count > 0)
            {
                var questionTypeList = questionModule.GetQuestionTypeListModule(new RB_Question_Type_ViewModel());
                foreach (var item in list)
                {
                    var model = GetQuestionModel(item, questionTypeList);
                    model.CourseId = CourseId;
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = userInfo.Id;
                    model.UpdateBy = userInfo.Id;
                    model.UpdateTime = DateTime.Now;
                    resultList.Add(model);
                }
            }
            System.IO.File.Delete(filePath);
            return ApiResult.Success(data: resultList);
        }

        /// <summary>
        /// 解析Word
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="CourseId"></param>
        /// <param name="Uid"></param>
        /// <returns></returns>
        public ApiResult ImportWordQuestion(string filePath, int CourseId, int Uid, bool isDelete = false)
        {
            var userInfo = base.GetUserInfo(Uid);
            var resultList = new List<RB_Question_ViewModel>();
            var list = Common.Data.QuestionHelper.GetWordQuestionData(filePath);
            if (list != null && list.Count > 0)
            {
                var questionTypeList = questionModule.GetQuestionTypeListModule(new RB_Question_Type_ViewModel());
                foreach (var item in list)
                {
                    var model = GetQuestionModel(item, questionTypeList);
                    model.CourseId = CourseId;
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = userInfo.Id;
                    model.UpdateBy = userInfo.Id;
                    model.UpdateTime = DateTime.Now;
                    resultList.Add(model);
                }
            }
            if (isDelete)
            {
                System.IO.File.Delete(filePath);
            }
            return ApiResult.Success(data: resultList);
        }

        /// <summary>
        /// 获取导入数据
        /// </summary>
        /// <param name="item"></param>
        /// <param name="difficultyList"></param>
        /// <param name="questionTypeList"></param>
        /// <returns></returns>
        private RB_Question_ViewModel GetQuestionModel(ImportModel item, List<RB_Question_Type_ViewModel> questionTypeList)
        {
            var DifficultyType = DifficultyTypeEnum.Easy;
            if (item.EasyType == "中")
            {
                DifficultyType = DifficultyTypeEnum.Middle;
            }
            if (item.EasyType == "难")
            {
                DifficultyType = DifficultyTypeEnum.Difficult;
            }
            var Category = QuestionCategoryEnum.Default;
            if (!string.IsNullOrEmpty(item.Category))
            {
                switch (item.Category)
                {
                    case "读音选择题":
                        Category = QuestionCategoryEnum.JapaneseSoundChoose;
                        break;
                    case "汉字选择题":
                        Category = QuestionCategoryEnum.ChineseChoose;
                        break;
                    case "选择最适当的单词":
                        Category = QuestionCategoryEnum.ChooseWord;
                        break;
                    case "选择意义最接近的单词":
                        Category = QuestionCategoryEnum.ChooseMean;
                        break;
                    case "选择单词正确用法":
                        Category = QuestionCategoryEnum.ChooseWordUse;
                        break;
                    case "选择语法正确的一项":
                        Category = QuestionCategoryEnum.ChooseGrammarUse;
                        break;
                    case "选择★应该填入的词":
                        Category = QuestionCategoryEnum.ChooseFillInWord;
                        break;
                    case "阅读选择":
                        Category = QuestionCategoryEnum.ReadingChoose;
                        break;
                }
            }
            var questionTypeModel = questionTypeList?.Where(qitem => qitem.Name == item.QuestionTypeName)?.FirstOrDefault();
            RB_Question_ViewModel model = new RB_Question_ViewModel
            {
                Title = item.QuestionTitle,
                DifficultyType = DifficultyType,
                AnswerParse = item.AnswerAnalysis,
                QuestionTypeId = questionTypeModel?.QId ?? 0,
                QuestionTypeKey = questionTypeModel?.Key ?? "",
                QuestionTypeName = questionTypeModel?.Name ?? "",
                Category = Category
            };
            Int32.TryParse(item.ChooseOptionCount, out int ChooseOptionCount);
            switch (item.QuestionTypeName)
            {
                case "单选题":
                    AnalysisSingle(ChooseOptionCount, item, model, questionTypeList);
                    break;
                case "多选题":
                    AnalysisMultiple(ChooseOptionCount, item, model, questionTypeList);
                    break;

                case "填空题":
                    if (ChooseOptionCount > 0)
                    {
                        List<fillInItem> fillInList = new List<fillInItem>();
                        for (var i = 0; i < ChooseOptionCount; i++)
                        {
                            var singleModel = new fillInItem()
                            {
                                Content = "",
                            };
                            if (i == 0)
                            {
                                singleModel.Content = item.OptionA;
                            }
                            if (i == 1)
                            {
                                singleModel.Content = item.OptionB;
                            }
                            if (i == 2)
                            {
                                singleModel.Content = item.OptionC;
                            }
                            if (i == 3)
                            {
                                singleModel.Content = item.OptionD;
                            }
                            if (i == 4)
                            {
                                singleModel.Content = item.OptionE;
                            }
                            if (i == 5)
                            {
                                singleModel.Content = item.OptionF;
                            }
                            if (i == 6)
                            {
                                singleModel.Content = item.OptionG;
                            }
                            if (i == 7)
                            {
                                singleModel.Content = item.OptionH;
                            }
                            fillInList.Add(singleModel);
                        }
                        string f_answer = "";
                        foreach (var fItem in fillInList)
                        {
                            f_answer += "★" + fItem.Content;
                        }
                        if (!string.IsNullOrEmpty(f_answer))
                        {
                            f_answer = f_answer[1..];
                        }
                        model.Answer = f_answer;
                        model.QuestionContent = Common.Plugin.JsonHelper.Serialize(fillInList);
                    }
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "fill-in")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;

                case "判断题":
                    if (ChooseOptionCount > 0)
                    {
                        List<optionItem> judgeList = new List<optionItem>();
                        for (var i = 0; i < ChooseOptionCount; i++)
                        {
                            var singleModel = new optionItem()
                            {
                                Name = Common.ConvertHelper.GetEnChar(i),
                                Content = "",
                            };
                            if (Common.Plugin.StringHelper.IsUpperLetter(item.QuestionAnswer))
                            {
                                singleModel.IsAnswer = item.QuestionAnswer.Contains(Common.ConvertHelper.GetEnChar(i));
                            }
                            else if (item.QuestionAnswer.Contains("对"))
                            {
                                singleModel.IsAnswer = (i == 0);
                            }
                            else if (item.QuestionAnswer.Contains("错"))
                            {
                                singleModel.IsAnswer = (i > 0);
                            }
                            if (i == 0)
                            {
                                singleModel.Content = item.OptionA;
                            }
                            if (i == 1)
                            {
                                singleModel.Content = item.OptionB;
                            }
                            if (i == 2)
                            {
                                singleModel.Content = item.OptionC;
                            }
                            if (i == 3)
                            {
                                singleModel.Content = item.OptionD;
                            }
                            if (i == 4)
                            {
                                singleModel.Content = item.OptionE;
                            }
                            if (i == 5)
                            {
                                singleModel.Content = item.OptionF;
                            }
                            if (i == 6)
                            {
                                singleModel.Content = item.OptionG;
                            }
                            if (i == 7)
                            {
                                singleModel.Content = item.OptionH;
                            }
                            if (singleModel.IsAnswer)
                            {
                                model.Answer = singleModel.Name;
                            }
                            judgeList.Add(singleModel);
                        }
                        model.QuestionContent = Common.Plugin.JsonHelper.Serialize(judgeList);
                    }
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "judge")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;

                case "简答题":
                    model.Answer = item.QuestionAnswer;
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "short-answer")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;

                case "名词解释":
                    model.Answer = item.QuestionAnswer;
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "noun-explanation")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;
                case "论述题":
                    model.Answer = item.QuestionAnswer;
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "essay-question")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;

                case "分录题":
                    if (ChooseOptionCount > 0)
                    {
                        List<fillInItem> fillInList = new List<fillInItem>();
                        for (var i = 0; i < ChooseOptionCount; i++)
                        {
                            var singleModel = new fillInItem()
                            {
                                Content = "",
                            };
                            if (i == 0)
                            {
                                singleModel.Content = item.OptionA;
                            }
                            if (i == 1)
                            {
                                singleModel.Content = item.OptionB;
                            }
                            if (i == 2)
                            {
                                singleModel.Content = item.OptionC;
                            }
                            if (i == 3)
                            {
                                singleModel.Content = item.OptionD;
                            }
                            if (i == 4)
                            {
                                singleModel.Content = item.OptionE;
                            }
                            if (i == 5)
                            {
                                singleModel.Content = item.OptionF;
                            }
                            if (i == 6)
                            {
                                singleModel.Content = item.OptionG;
                            }
                            if (i == 7)
                            {
                                singleModel.Content = item.OptionH;
                            }
                            fillInList.Add(singleModel);
                        }
                        string f_answer = "";
                        foreach (var fItem in fillInList)
                        {
                            f_answer += "★" + fItem.Content;
                        }
                        if (!string.IsNullOrEmpty(f_answer))
                        {
                            f_answer = f_answer[1..];
                        }
                        model.Answer = f_answer;
                        model.QuestionContent = Common.Plugin.JsonHelper.Serialize(fillInList);
                    }
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "entry-problem")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;

                case "资料题":
                    if (ChooseOptionCount > 0)
                    {
                        List<fillInItem> fillInList = new List<fillInItem>();
                        for (var i = 0; i < ChooseOptionCount; i++)
                        {
                            var singleModel = new fillInItem()
                            {
                                Content = "",
                            };
                            if (i == 0)
                            {
                                singleModel.Content = item.OptionA;
                            }
                            if (i == 1)
                            {
                                singleModel.Content = item.OptionB;
                            }
                            if (i == 2)
                            {
                                singleModel.Content = item.OptionC;
                            }
                            if (i == 3)
                            {
                                singleModel.Content = item.OptionD;
                            }
                            if (i == 4)
                            {
                                singleModel.Content = item.OptionE;
                            }
                            if (i == 5)
                            {
                                singleModel.Content = item.OptionF;
                            }
                            if (i == 6)
                            {
                                singleModel.Content = item.OptionG;
                            }
                            if (i == 7)
                            {
                                singleModel.Content = item.OptionH;
                            }
                            fillInList.Add(singleModel);
                        }
                        string f_answer = "";
                        foreach (var fItem in fillInList)
                        {
                            f_answer += "★" + fItem.Content;
                        }
                        if (!string.IsNullOrEmpty(f_answer))
                        {
                            f_answer = f_answer[1..];
                        }
                        model.Answer = f_answer;
                        model.QuestionContent = Common.Plugin.JsonHelper.Serialize(fillInList);
                    }
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "data-question")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;

                case "阅读理解":
                    List<readingComprehensioItem> readingList = new List<readingComprehensioItem>();
                    foreach (var subItem in item.SubQuestionList)
                    {
                        Int32.TryParse(subItem.ChooseOptionCount, out int subChooseCount);
                        List<optionItem> singleList = new List<optionItem>();
                        switch (subItem.QuestionTypeName)
                        {
                            //单选题
                            case "单选题":
                                readingComprehensioItem readingModel = new readingComprehensioItem()
                                {
                                    QuestionKey = "single",
                                    QuestionName = "单选题",
                                    QuestionType = 1,
                                    SubTitle = subItem.QuestionTitle,
                                    SubAnwser = new object()
                                };

                                for (var i = 0; i < subChooseCount; i++)
                                {
                                    var singleModel = new optionItem()
                                    {
                                        Name = Common.ConvertHelper.GetEnChar(i),
                                        Content = "",
                                        IsAnswer = subItem.QuestionAnswer.Contains(Common.ConvertHelper.GetEnChar(i))
                                    };
                                    if (i == 0)
                                    {
                                        singleModel.Content = subItem.OptionA;
                                    }
                                    if (i == 1)
                                    {
                                        singleModel.Content = subItem.OptionB;
                                    }
                                    if (i == 2)
                                    {
                                        singleModel.Content = subItem.OptionC;
                                    }
                                    if (i == 3)
                                    {
                                        singleModel.Content = subItem.OptionD;
                                    }
                                    if (singleModel.IsAnswer)
                                    {
                                        model.Answer += singleModel.Name + ",";
                                    }
                                    singleList.Add(singleModel);
                                }
                                readingModel.SubAnwser = singleList;
                                readingList.Add(readingModel);
                                break;
                            case "单选题(数字)":
                                readingComprehensioItem readingModel2 = new readingComprehensioItem()
                                {
                                    QuestionKey = "single-number",
                                    QuestionName = "单选题(数字)",
                                    QuestionType = 19,
                                    SubTitle = subItem.QuestionTitle,
                                    SubAnwser = new object()
                                };

                                for (var i = 0; i < subChooseCount; i++)
                                {
                                    var singleModel = new optionItem()
                                    {
                                        Name = (i + 1).ToString(),
                                        Content = "",
                                        IsAnswer = subItem.QuestionAnswer.Trim() == (i + 1).ToString()
                                    };
                                    if (i == 0)
                                    {
                                        singleModel.Content = subItem.OptionA;
                                    }
                                    if (i == 1)
                                    {
                                        singleModel.Content = subItem.OptionB;
                                    }
                                    if (i == 2)
                                    {
                                        singleModel.Content = subItem.OptionC;
                                    }
                                    if (i == 3)
                                    {
                                        singleModel.Content = subItem.OptionD;
                                    }
                                    if (singleModel.IsAnswer)
                                    {
                                        model.Answer += singleModel.Name + ",";
                                    }
                                    singleList.Add(singleModel);
                                }
                                readingModel2.SubAnwser = singleList;
                                readingList.Add(readingModel2);
                                break;
                        }
                    }
                    model.QuestionContent = Common.Plugin.JsonHelper.Serialize(readingList);
                    if (model.QuestionTypeId == 0)
                    {
                        var tempModel = questionTypeList.Where(qitem => qitem.Key == "reading-comprehensio")?.FirstOrDefault();
                        model.QuestionTypeId = tempModel?.QId ?? 0;
                        model.QuestionTypeKey = tempModel?.Key ?? "";
                    }
                    break;

                case "单选题(数字)":
                    AnalysisSingle(ChooseOptionCount, item, model, questionTypeList, isNumChoose: true);
                    break;
            }
            model.QuestionContentObj = questionModule.ParsingQuestionModule(model.QuestionTypeKey, model.QuestionContent);
            return model;
        }

        /// <summary>
        /// 解析单选题
        /// </summary>
        /// <param name="ChooseOptionCount"></param>
        /// <param name="item"></param>
        /// <param name="model"></param>
        /// <param name="questionTypeList"></param>
        ///  <param name="isNumChoose">是否数字选择题</param>
        private void AnalysisSingle(int ChooseOptionCount, ImportModel item, RB_Question_ViewModel model, List<RB_Question_Type_ViewModel> questionTypeList, bool isNumChoose = false)
        {
            if (ChooseOptionCount > 0)
            {
                List<optionItem> singleList = new List<optionItem>();
                for (var i = 0; i < ChooseOptionCount; i++)
                {
                    var singleModel = new optionItem()
                    {
                        Name = Common.ConvertHelper.GetEnChar(i),
                        Content = "",
                        IsAnswer = item.QuestionAnswer.Contains(Common.ConvertHelper.GetEnChar(i))
                    };
                    if (isNumChoose)
                    {
                        singleModel.Name = (i + 1).ToString();
                        singleModel.IsAnswer = item.QuestionAnswer.Contains((i + 1).ToString());
                    }
                    if (i == 0)
                    {
                        singleModel.Content = item.OptionA;
                    }
                    if (i == 1)
                    {
                        singleModel.Content = item.OptionB;
                    }
                    if (i == 2)
                    {
                        singleModel.Content = item.OptionC;
                    }
                    if (i == 3)
                    {
                        singleModel.Content = item.OptionD;
                    }
                    if (i == 4)
                    {
                        singleModel.Content = item.OptionE;
                    }
                    if (i == 5)
                    {
                        singleModel.Content = item.OptionF;
                    }
                    if (i == 6)
                    {
                        singleModel.Content = item.OptionG;
                    }
                    if (i == 7)
                    {
                        singleModel.Content = item.OptionH;
                    }
                    if (singleModel.IsAnswer)
                    {
                        model.Answer = singleModel.Name;
                    }
                    singleList.Add(singleModel);
                }
                model.QuestionContent = Common.Plugin.JsonHelper.Serialize(singleList);
            }
            if (model.QuestionTypeId == 0)
            {
                string key = isNumChoose ? "single-number" : "single";
                var tempModel = questionTypeList.Where(qitem => qitem.Key == key)?.FirstOrDefault();
                model.QuestionTypeId = tempModel?.QId ?? 0;
                model.QuestionTypeKey = tempModel?.Key ?? "";
            }
        }

        /// <summary>
        /// 解析多选题
        /// </summary>
        /// <param name="ChooseOptionCount"></param>
        /// <param name="item"></param>
        /// <param name="model"></param>
        /// <param name="questionTypeList"></param>
        private void AnalysisMultiple(int ChooseOptionCount, ImportModel item, RB_Question_ViewModel model, List<RB_Question_Type_ViewModel> questionTypeList)
        {
            if (ChooseOptionCount > 0)
            {
                List<optionItem> multipleList = new List<optionItem>();
                string answer = "";
                for (var i = 0; i < ChooseOptionCount; i++)
                {
                    var singleModel = new optionItem()
                    {
                        Name = Common.ConvertHelper.GetEnChar(i),
                        Content = "",
                        IsAnswer = item.QuestionAnswer.Contains(Common.ConvertHelper.GetEnChar(i))
                    };
                    if (i == 0)
                    {
                        singleModel.Content = item.OptionA;
                    }
                    if (i == 1)
                    {
                        singleModel.Content = item.OptionB;
                    }
                    if (i == 2)
                    {
                        singleModel.Content = item.OptionC;
                    }
                    if (i == 3)
                    {
                        singleModel.Content = item.OptionD;
                    }
                    if (i == 4)
                    {
                        singleModel.Content = item.OptionE;
                    }
                    if (i == 5)
                    {
                        singleModel.Content = item.OptionF;
                    }
                    if (i == 6)
                    {
                        singleModel.Content = item.OptionG;
                    }
                    if (i == 7)
                    {
                        singleModel.Content = item.OptionH;
                    }
                    if (singleModel.IsAnswer)
                    {
                        answer += "," + singleModel.Name;
                    }
                    multipleList.Add(singleModel);
                }
                if (!string.IsNullOrEmpty(answer))
                {
                    answer = answer[1..];
                }
                model.Answer = answer;
                model.QuestionContent = Common.Plugin.JsonHelper.Serialize(multipleList);
            }
            if (model.QuestionTypeId == 0)
            {
                var tempModel = questionTypeList.Where(qitem => qitem.Key == "multiple")?.FirstOrDefault();
                model.QuestionTypeId = tempModel?.QId ?? 0;
                model.QuestionTypeKey = tempModel?.Key ?? "";
            }
        }

        #endregion
    }
}