using Edu.AOP.CustomerAttribute;
using Edu.Model.ViewModel.Exam;
using Edu.Repository.Exam;
using System;
using System.Collections.Generic;
using VT.FW.DB;
using System.Linq;
using Edu.Cache.User;
using Edu.Common.Plugin;
using Edu.Module.Question;
using Edu.Repository.Question;
using Edu.Model.ViewModel.Question;
using System.Text.RegularExpressions;
using Edu.Model.CacheModel;
using Edu.Common.Enum;
using Edu.Common.Enum.EduTask;
using Edu.Model.ViewModel.EduTask;
using Edu.Common.Enum.Finance;
using Edu.Repository.EduTask;
using Edu.Common.Enum.Exam;
using Edu.Common.Enum.Course;

namespace Edu.Module.Exam
{
    /// <summary>
    /// 试卷处理类
    /// </summary>
    public partial class PaperModule
    {
        /// <summary>
        /// 试卷题目仓储层对象
        /// </summary>
        private readonly RB_Examination_DetailsRepository examination_DetailsRepository = new RB_Examination_DetailsRepository();

        /// <summary>
        /// 试卷仓储层对象
        /// </summary>
        private readonly RB_Examination_PaperRepository examination_PaperRepository = new RB_Examination_PaperRepository();

        /// <summary>
        /// 试卷分组仓储层对象
        /// </summary>
        private readonly RB_Examination_GroupRepository examination_GroupRepository = new RB_Examination_GroupRepository();

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

        /// <summary>
        /// 试卷模板仓储层对象
        /// </summary>
        private readonly RB_Examination_TemplateRepository examination_TemplateRepository = new RB_Examination_TemplateRepository();

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

        /// <summary>
        /// 考试发布仓储层对象
        /// </summary>
        private readonly RB_Examination_PublishRepository examination_PublishRepository = new RB_Examination_PublishRepository();

        /// <summary>
        /// 考试配置仓储层对象
        /// </summary>
        private readonly RB_Examination_ConfigureRepository examination_ConfigureRepository = new RB_Examination_ConfigureRepository();

        /// 试卷考试学员仓储层对象
        /// </summary>
        private readonly RB_Examination_StudentRepository examination_StudentRepository = new RB_Examination_StudentRepository();

        /// <summary>
        /// 教务单据仓储层对象
        /// </summary>
        private readonly RB_Education_ReceiptRepository education_ReceiptRepository = new RB_Education_ReceiptRepository();

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


        /// <summary>
        /// 获取试卷分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<ExaminationPaperTreeModel> GetExaminationPaperPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Paper_ViewModel query)
        {
            List<ExaminationPaperTreeModel> treeList = new List<ExaminationPaperTreeModel>();
            var data = examination_PaperRepository.GetExaminationPaperPageRepository(pageIndex, pageSize, out rowsCount, query);
            if (data != null && data.Count > 0)
            {
                var childList = new List<RB_Examination_Paper_ViewModel>();
                string ids = string.Join(",", data.Select(qitem => qitem.PaperId));
                if (!string.IsNullOrEmpty(ids))
                {
                    childList = examination_PaperRepository.GetExaminationPaperChildRepository(ids);
                }
                List<int> qIdList = new List<int>();
                if (childList != null && childList.Count > 0)
                {
                    var childIds = Common.ConvertHelper.StringToList(string.Join(",", childList.Select(qitem => qitem.PaperId)));
                    if (childIds != null && childIds.Count > 0)
                    {
                        qIdList.AddRange(childIds);
                    }
                }
                var idList = Common.ConvertHelper.StringToList(ids);
                if (idList != null && idList.Count > 0)
                {
                    qIdList.AddRange(idList);
                }
                var detailsList = new List<RB_Examination_Details_ViewModel>();
                if (qIdList != null && qIdList.Count > 0)
                {
                    detailsList = examination_DetailsRepository.GetExaminationDetailsStaticListRepository(new RB_Examination_Details_ViewModel()
                    {
                        QPaperIds = string.Join(",", qIdList)
                    });
                }
                foreach (var item in data)
                {
                    ExaminationPaperTreeModel model = new ExaminationPaperTreeModel()
                    {
                        PaperId = item.PaperId,
                        PaperName = item.PaperName,
                        QuestionBandIds = item.QuestionBandIds,
                        DifficultyTypeName = item.DifficultyType.ToName(),
                        CreateTimeStr = Common.ConvertHelper.FormatTime(item.CreateTime),
                        CreateBy = item.CreateBy,
                        CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "",
                        PublishCount = item.PublishCount,
                        GenerateType = item.GenerateType,
                        CreateNum = item.CreateNum,
                        PaperType = item.PaperType,
                        ParentId = item.ParentId,
                        QuestionCount = detailsList?.Where(qitem => qitem.PaperId == item.PaperId)?.FirstOrDefault()?.QuestionCount ?? 0,
                        ChildList = new List<ExaminationPaperTreeModel>(),
                        ExamineStatus = item.ExamineStatus,
                        Reviewer = item.Reviewer,
                        IsOpen = item.IsOpen,
                        ExamineRemark = item.ExamineRemark,
                        ReviewerName = item.Reviewer > 0 ? (UserReidsCache.GetUserLoginInfo(item.Reviewer)?.AccountName ?? "") : ""
                    };
                    //model.ChildList = GetPaperChild(childList, detailsList, item.PaperId);
                    treeList.Add(model);
                }
            }
            return treeList;
        }

        /// <summary>
        /// 获取当前节点的所有父级节点
        /// </summary>
        /// <param name="QPaperIds"></param>
        /// <returns></returns>
        public List<object> GetExaminationPaperParentModule(string QPaperIds)
        {
            List<object> list = new List<object>();
            var dataList = examination_PaperRepository.GetExaminationPaperParentRepository(QPaperIds);
            if (dataList != null && dataList.Count > 0)
            {
                var groupList = dataList.Where(qitem => qitem.ParentId == 0)?.ToList();
                if (groupList != null && groupList.Count > 0)
                {
                    foreach (var item in groupList)
                    {
                        list.Add(new
                        {
                            item.PaperId,
                            item.PaperName,
                        });
                        GetFolderChild(list, dataList, item.PaperId);
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 递归遍历所有下级文件夹
        /// </summary>
        /// <param name="sourceList">源数据</param>
        /// <param name="parentId">父节点编号</param>
        /// <returns></returns>
        public void GetFolderChild(List<object> list, List<RB_Examination_Paper_ViewModel> sourceList, int parentId)
        {
            //获取下级节点
            var subList = sourceList?.Where(qItem => qItem.ParentId == parentId).ToList();
            //如果存在下级节点
            if (subList != null && subList.Count > 0)
            {
                foreach (var childItem in subList)
                {
                    list.Add(new
                    {
                        childItem.PaperId,
                        childItem.PaperName,
                    });
                    GetFolderChild(list, sourceList, childItem.PaperId);
                }
            }
        }

        /// <summary>
        /// 递归遍历所有下级文件夹或试卷
        /// </summary>
        /// <param name="sourceList">源数据</param>
        /// <param name="parentId">父节点编号</param>
        /// <returns></returns>
        public List<ExaminationPaperTreeModel> GetPaperChild(List<RB_Examination_Paper_ViewModel> sourceList, List<RB_Examination_Details_ViewModel> detailsList, int parentId)
        {
            List<ExaminationPaperTreeModel> resultList = new List<ExaminationPaperTreeModel>();
            //获取下级节点
            var subList = sourceList?.Where(qItem => qItem.ParentId == parentId).ToList();
            //如果存在下级节点
            if (subList != null && subList.Count > 0)
            {
                foreach (var childItem in subList)
                {
                    var childModel = new ExaminationPaperTreeModel()
                    {
                        PaperId = childItem.PaperId,
                        PaperName = childItem.PaperName,
                        QuestionBandIds = childItem.QuestionBandIds,
                        DifficultyTypeName = childItem.DifficultyType.ToName(),
                        CreateTimeStr = Common.ConvertHelper.FormatTime(childItem.CreateTime),
                        CreateByName = UserReidsCache.GetUserLoginInfo(childItem.CreateBy)?.AccountName ?? "",
                        PublishCount = childItem.PublishCount,
                        GenerateType = childItem.GenerateType,
                        CreateNum = childItem.CreateNum,
                        PaperType = childItem.PaperType,
                        ParentId = childItem.ParentId,
                        QuestionCount = detailsList?.Where(qitem => qitem.PaperId == childItem.PaperId)?.FirstOrDefault()?.QuestionCount ?? 0,
                        ChildList = new List<ExaminationPaperTreeModel>(),
                        ExamineStatus = childItem.ExamineStatus,
                        Reviewer = childItem.Reviewer,
                        ExamineStatusStr = childItem.PaperType == 2 ? EnumHelper.ToName(childItem.ExamineStatus) : "",
                        ReviewerName = childItem.Reviewer > 0 ? (UserReidsCache.GetUserLoginInfo(childItem.Reviewer)?.AccountName ?? "") : ""
                    };
                    childModel.ChildList.AddRange(GetPaperChild(sourceList, detailsList, childItem.PaperId));
                    resultList.Add(childModel);
                }
            }
            return resultList;
        }

        /// <summary>
        /// 获取试卷题目列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Group_ViewModel> GetExaminationGroupListModule(RB_Examination_Group_ViewModel query)
        {
            return examination_GroupRepository.GetExaminationGroupListRepository(query);
        }

        /// <summary>
        /// 获取试卷题目列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Details_ViewModel> GetExaminationDetailsListModule(RB_Examination_Details_ViewModel query)
        {
            return examination_DetailsRepository.GetExaminationDetailsListRepository(query);
        }

        /// <summary>
        /// 获取试卷模板分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Template_ViewModel> GetExaminationTemplatePageModule(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Template_ViewModel query)
        {
            return examination_TemplateRepository.GetExaminationTemplatePageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 新增/修改试卷
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetExaminationPaperModule(RB_Examination_Paper_ViewModel model)
        {
            bool flag = SetExaminationPaperFolderModule(model);
            if (model.GroupList != null && model.GroupList.Count > 0)
            {
                foreach (var gitem in model.GroupList)
                {
                    gitem.PaperId = model.PaperId;
                    if (flag)
                    {
                        if (gitem.GId > 0)
                        {
                            Dictionary<string, object> fileds = new Dictionary<string, object>()
                            {
                                {nameof(RB_Examination_Group_ViewModel.PaperId),gitem.PaperId },
                                {nameof(RB_Examination_Group_ViewModel.GroupName),gitem.GroupName },
                                {nameof(RB_Examination_Group_ViewModel.QuestionTypeId),gitem.QuestionTypeId },
                                {nameof(RB_Examination_Group_ViewModel.QuestionTypeKey),gitem.QuestionTypeKey },
                                {nameof(RB_Examination_Group_ViewModel.GSortNum),gitem.GSortNum },
                                {nameof(RB_Examination_Group_ViewModel.GScore),gitem.GScore },
                                {nameof(RB_Examination_Group_ViewModel.GroupType),gitem.GroupType },
                                {nameof(RB_Examination_Group_ViewModel.CategoryId),gitem.CategoryId },
                            };
                            flag = examination_GroupRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Group_ViewModel.GId), gitem.GId));
                        }
                        else
                        {
                            var newGroupId = examination_GroupRepository.Insert(gitem);
                            gitem.GId = newGroupId;
                            flag = newGroupId > 0;
                        }
                    }
                    if (flag && gitem.DetailsList != null && gitem.DetailsList.Count > 0)
                    {
                        foreach (var dItem in gitem.DetailsList)
                        {
                            dItem.PaperId = model.PaperId;
                            dItem.GId = gitem.GId;
                            if (dItem.Id > 0)
                            {
                                Dictionary<string, object> fileds = new Dictionary<string, object>()
                                {
                                    {nameof(RB_Examination_Details_ViewModel.PaperId),dItem.PaperId },
                                    {nameof(RB_Examination_Details_ViewModel.BankId),dItem.BankId },
                                    {nameof(RB_Examination_Details_ViewModel.QuestionId),dItem.QuestionId },
                                    {nameof(RB_Examination_Details_ViewModel.Title),dItem.Title },
                                    {nameof(RB_Examination_Details_ViewModel.QuestionContent),dItem.QuestionContent },
                                    {nameof(RB_Examination_Details_ViewModel.DifficultyType),dItem.DifficultyType },
                                    {nameof(RB_Examination_Details_ViewModel.AnswerParse),dItem.AnswerParse },
                                    {nameof(RB_Examination_Details_ViewModel.QuestionTypeId),dItem.QuestionTypeId },
                                    {nameof(RB_Examination_Details_ViewModel.QuestionTypeKey),dItem.QuestionTypeKey },
                                    {nameof(RB_Examination_Details_ViewModel.Knowledge),dItem.Knowledge },
                                    {nameof(RB_Examination_Details_ViewModel.UpdateBy),dItem.UpdateBy },
                                    {nameof(RB_Examination_Details_ViewModel.UpdateTime),dItem.UpdateTime },
                                    {nameof(RB_Examination_Details_ViewModel.IsUpdateJobExam),dItem.IsUpdateJobExam },
                                    {nameof(RB_Examination_Details_ViewModel.SortNum),dItem.SortNum },
                                    {nameof(RB_Examination_Details_ViewModel.Answer),dItem.Answer },
                                    {nameof(RB_Examination_Details_ViewModel.IsMutex),dItem.IsMutex },
                                    {nameof(RB_Examination_Details_ViewModel.Category),dItem.Category },
                                    {nameof(RB_Examination_Details_ViewModel.LevelType),dItem.LevelType },
                                    {nameof(RB_Examination_Details_ViewModel.Score),dItem.Score },
                                    {nameof(RB_Examination_Details_ViewModel.GId),dItem.GId },
                                };
                                flag = examination_DetailsRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Details_ViewModel.Id), dItem.Id));
                            }
                            else
                            {
                                var newDetailsId = examination_DetailsRepository.Insert(dItem);
                                dItem.Id = newDetailsId;
                                flag = newDetailsId > 0;
                            }
                        }
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 智能组卷(根据模板生成试卷)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual bool SetPaperTemplateModule(RB_Examination_Template_ViewModel model)
        {
            bool flag = true;
            if (model.IsSaveTemplate)
            {
                if (model.TemplateId > 0)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Examination_Template_ViewModel.TemplateName),model.TemplateName },
                        {nameof(RB_Examination_Template_ViewModel.TemplateScore),model.TemplateScore },
                        {nameof(RB_Examination_Template_ViewModel.TemplateNum),model.TemplateNum },
                        {nameof(RB_Examination_Template_ViewModel.TemplateDifficultyType),model.TemplateDifficultyType },
                        {nameof(RB_Examination_Template_ViewModel.TemplateData),model.TemplateData },
                        {nameof(RB_Examination_Template_ViewModel.TemplateBankIds),model.TemplateBankIds },
                        {nameof(RB_Examination_Template_ViewModel.GroupType),model.GroupType },
                    };
                    flag = examination_TemplateRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Template_ViewModel.TemplateId), model.TemplateId));
                }
                else
                {
                    var newTempId = examination_TemplateRepository.Insert(model);
                    model.TemplateId = newTempId;
                    flag = newTempId > 0;
                }
            }
            //获取问题列表
            var questionList = new List<RB_Question_ViewModel>();

            string Q_QuestionTypeIds = "";
            if (model.TempDataList != null && model.TempDataList.Count > 0)
            {
                Q_QuestionTypeIds = string.Join(",", model.TempDataList.Select(qitem => qitem.QuestionTypeId));
            }
            questionList = questionRepository.GetQuestionListRepository(new RB_Question_ViewModel()
            {
                QBankIds = model.TemplateBankIds,
                Q_QuestionTypeIds = model.GroupType == ExamGroupTypeEnum.QuestionType ? Q_QuestionTypeIds : "",
                QCategoryId = model.GroupType == ExamGroupTypeEnum.QuestionClassify ? Q_QuestionTypeIds : "",
            });
            var newPaperId = 0;
            if (model.TemplateNum > 1)
            {
                //文件夹
                var paperModel = new RB_Examination_Paper_ViewModel()
                {
                    PaperId = 0,
                    PaperName = model.TemplateName,
                    QuestionBandIds = model.TemplateBankIds,
                    DifficultyType = model.TemplateDifficultyType,
                    CreateTime = model.CreateTime,
                    CreateBy = model.CreateBy,
                    PublishCount = 0,
                    Group_Id = model.Group_Id,
                    School_Id = model.School_Id,
                    GenerateType = 1,
                    CreateNum = model.TemplateNum,
                    PaperType = 1,
                    ParentId = model.ParentId,
                    IsOpen = model.IsOpen,
                    GroupType = model.GroupType,
                };
                newPaperId = examination_PaperRepository.Insert(paperModel);
                paperModel.PaperId = newPaperId;
                flag = newPaperId > 0;
            }

            if (model.TemplateNum > 0)
            {
                for (var i = 0; i < model.TemplateNum; i++)
                {
                    var subModel = new RB_Examination_Paper_ViewModel()
                    {
                        PaperId = 0,
                        PaperName = model.TemplateName + (i + 1),
                        QuestionBandIds = model.TemplateBankIds,
                        DifficultyType = model.TemplateDifficultyType,
                        CreateTime = model.CreateTime,
                        CreateBy = model.CreateBy,
                        PublishCount = 0,
                        Group_Id = model.Group_Id,
                        School_Id = model.School_Id,
                        GenerateType = 1,
                        CreateNum = model.TemplateNum,
                        PaperType = 2,
                        ParentId = newPaperId,
                        IsOpen = model.IsOpen,
                        GroupType = model.GroupType,
                        PaperScore = model.TemplateScore,
                    };
                    var newSubPaperId = examination_PaperRepository.Insert(subModel);
                    subModel.PaperId = newSubPaperId;
                    flag = newSubPaperId > 0;
                    if (flag && model.TempDataList != null && model.TempDataList.Count > 0)
                    {
                        int G_Sort = 1;
                        foreach (var item in model.TempDataList)
                        {
                            var tempQuestioinList = new List<RB_Question_ViewModel>();
                            if (model.GroupType == ExamGroupTypeEnum.QuestionType)
                            {
                                tempQuestioinList = questionList?.Where(qitem => qitem.QuestionTypeId == item.QuestionTypeId)?.ToList();
                            }
                            else if (model.GroupType == ExamGroupTypeEnum.QuestionClassify)
                            {
                                tempQuestioinList = questionList?.Where(qitem => qitem.Category.ToInt() == item.QuestionTypeId)?.ToList();
                            }
                            var subTempQuestionList = new List<RB_Question_ViewModel>();
                            //题库随机选题
                            if (item.ChooseType == 1)
                            {
                                subTempQuestionList = tempQuestioinList?.OrderBy(qitem => Guid.NewGuid())?.Take(item.ChooseNum)?.ToList();
                            }
                            //题库难易程度随机选题
                            else if (item.ChooseType == 2)
                            {
                                foreach (var subItem in item.ChooseList.Where(qitem => qitem.ChooseNum > 0))
                                {
                                    var tempList = tempQuestioinList?.Where(qitem => qitem.DifficultyType == subItem.DifficultyType)?.OrderBy(qitem => Guid.NewGuid())?.Take(subItem.ChooseNum)?.ToList();
                                    if (tempList != null && tempList.Count > 0)
                                    {
                                        subTempQuestionList.AddRange(tempList);
                                    }
                                }
                            }
                            var groupModel = new RB_Examination_Group_ViewModel()
                            {
                                GId = 0,
                                PaperId = subModel.PaperId,
                                GroupName = !string.IsNullOrEmpty(item.QuestionDesc) ? item.QuestionDesc : item.QuestionTypeName,
                                GSortNum = G_Sort,
                                GScore = item.QuestionScore,
                                QuestionTypeKey = item.QuestionTypeKey,
                                GroupType = model.GroupType,
                            };
                            if (model.GroupType == ExamGroupTypeEnum.QuestionType)
                            {
                                groupModel.QuestionTypeId = item.QuestionTypeId;
                                groupModel.CategoryId = 0;
                            }
                            else if (model.GroupType == ExamGroupTypeEnum.QuestionClassify)
                            {
                                groupModel.QuestionTypeId = 0;
                                groupModel.CategoryId = (QuestionCategoryEnum)item.QuestionTypeId;
                            }
                            G_Sort++;
                            var newGId = examination_GroupRepository.Insert(groupModel);
                            groupModel.GId = newGId;
                            flag = newGId > 0;
                            int Q_SortNum = 1;
                            foreach (var qItem in subTempQuestionList)
                            {
                                var detailsModel = new RB_Examination_Details_ViewModel()
                                {
                                    Id = 0,
                                    GId = groupModel.GId,
                                    PaperId = subModel.PaperId,
                                    BankId = qItem.BankId,
                                    QuestionId = qItem.QuestionId,
                                    Title = qItem.Title,
                                    QuestionContent = qItem.QuestionContent,
                                    DifficultyType = qItem.DifficultyType,
                                    AnswerParse = qItem.AnswerParse,
                                    QuestionTypeId = qItem.QuestionTypeId,
                                    QuestionTypeKey = qItem.QuestionTypeKey,
                                    Knowledge = qItem.Knowledge,
                                    CreateBy = model.CreateBy,
                                    CreateTime = model.CreateTime,
                                    UpdateBy = model.CreateBy,
                                    UpdateTime = model.CreateTime,
                                    IsUpdateJobExam = qItem.IsUpdateJobExam,
                                    Status = DateStateEnum.Normal,
                                    SortNum = Q_SortNum,
                                    Answer = qItem.Answer,
                                    IsMutex = qItem.IsMutex,
                                    Category = qItem.Category,
                                    LevelType = qItem.LevelType,
                                };
                                detailsModel.Score = item.QuestionScore / subTempQuestionList.Count();
                                //听力题、完型填空、阅读理解
                                if (qItem.QuestionTypeKey == "cloze")
                                {
                                    List<colzeItem> clozeList = JsonHelper.DeserializeObject<List<colzeItem>>(qItem.QuestionContent);
                                    decimal tempCScore = 0;
                                    int cIndex = 0;
                                    var sScore = Math.Floor(detailsModel.Score / clozeList.Count * 100) / 100;
                                    foreach (var cItem in clozeList)
                                    {
                                        if (cIndex != clozeList.Count - 1)
                                        {
                                            tempCScore += sScore;
                                            cItem.SubScore = sScore;
                                        }
                                        else
                                        {
                                            cItem.SubScore = detailsModel.Score - tempCScore;
                                        }
                                        cIndex++;
                                    }
                                    detailsModel.QuestionContent = JsonHelper.Serialize(clozeList);
                                }
                                else if (qItem.QuestionTypeKey == "listening" || qItem.QuestionTypeKey == "reading-comprehensio")
                                {
                                    try
                                    {
                                        List<readingComprehensioItem> readingList = JsonHelper.DeserializeObject<List<readingComprehensioItem>>(qItem.QuestionContent);
                                        decimal tempRScore = 0;
                                        int rIndex = 0;
                                        var rScore = Math.Floor(detailsModel.Score / readingList.Count * 100) / 100;
                                        foreach (var rItem in readingList)
                                        {
                                            if (rIndex != readingList.Count - 1)
                                            {
                                                tempRScore += rScore;
                                                rItem.SubScore = rScore;
                                            }
                                            else
                                            {
                                                rItem.SubScore = detailsModel.Score - tempRScore;
                                            }
                                            rIndex++;
                                        }
                                        detailsModel.QuestionContent = JsonHelper.Serialize(readingList);
                                    }
                                    catch (Exception ex)
                                    {
                                        Common.Plugin.LogHelper.Write(ex, "SetPaperTemplateModule_" + JsonHelper.Serialize(detailsModel));
                                    }
                                }
                                string msg = analysisQuestion.CheckQuestion(detailsModel.QuestionTypeKey, detailsModel.QuestionContent, detailsModel.Answer, out string newAnswer, IsRequire: false);
                                if (!string.IsNullOrEmpty(newAnswer))
                                {
                                    detailsModel.Answer = newAnswer;
                                }
                                Q_SortNum++;
                                var newDetailsId = examination_DetailsRepository.Insert(detailsModel);
                                detailsModel.Id = newDetailsId;
                                flag = newDetailsId > 0;

                            }
                        }
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 新增修改文件夹
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual bool SetExaminationPaperFolderModule(RB_Examination_Paper_ViewModel model)
        {
            return SetExaminationPaperCommonModule(model);
        }

        /// <summary>
        /// 新增修改试卷基础信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetExaminationPaperCommonModule(RB_Examination_Paper_ViewModel model)
        {
            bool flag;
            if (model.PaperId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Examination_Paper_ViewModel.PaperName),model.PaperName },
                    {nameof(RB_Examination_Paper_ViewModel.QuestionBandIds),model.QuestionBandIds },
                    {nameof(RB_Examination_Paper_ViewModel.DifficultyType),model.DifficultyType },
                    {nameof(RB_Examination_Paper_ViewModel.GenerateType),model.GenerateType },
                    {nameof(RB_Examination_Paper_ViewModel.CreateNum),model.CreateNum },
                    {nameof(RB_Examination_Paper_ViewModel.PaperType),model.PaperType },
                    {nameof(RB_Examination_Paper_ViewModel.ParentId),model.ParentId },
                    {nameof(RB_Examination_Paper_ViewModel.IsOpen),model.IsOpen },
                    {nameof(RB_Examination_Paper_ViewModel.GroupType),model.GroupType },
                    {nameof(RB_Examination_Paper_ViewModel.PaperScore),model.PaperScore },
                };
                flag = examination_PaperRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Paper_ViewModel.PaperId), model.PaperId));
            }
            else
            {
                var newPaperId = examination_PaperRepository.Insert(model);
                model.PaperId = newPaperId;
                flag = newPaperId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 提交组卷审批
        /// </summary>
        /// <param name="PaperId"></param>
        /// <returns></returns>
        public bool SubmitApprovalModule(int PaperId)
        {
            bool flag = false;
            var model = GetExaminationPaperModule(PaperId);
            var configureModel = new RB_Examination_Configure_ViewModel()
            {
                Group_Id = model.Group_Id,
            };
            var examConfig = GetExaminationConfigureModel(configureModel);
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Examination_Paper_ViewModel.ExamineRemark),"" },
            };
            if (examConfig != null && examConfig.Id > 0)
            {
                if (examConfig.IsOpenZuJuan == 0)
                {
                    fileds.Add(nameof(RB_Examination_Paper_ViewModel.Reviewer), 0);
                    fileds.Add(nameof(RB_Examination_Paper_ViewModel.ExamineStatus), Edu.Common.Enum.EduTask.EduTaskRrocessStatus.AuditThrough);
                }
                else
                {
                    fileds.Add(nameof(RB_Examination_Paper_ViewModel.Reviewer), examConfig.ZuJuanReviewer);
                    fileds.Add(nameof(RB_Examination_Paper_ViewModel.ExamineStatus), Edu.Common.Enum.EduTask.EduTaskRrocessStatus.AuditIng);
                }
            }
            else
            {
                fileds.Add(nameof(RB_Examination_Paper_ViewModel.Reviewer), 0);
                fileds.Add(nameof(RB_Examination_Paper_ViewModel.ExamineStatus), Edu.Common.Enum.EduTask.EduTaskRrocessStatus.AuditThrough);
            }
            var educationReceipt = new Model.Entity.EduTask.RB_Education_Receipt()
            {
                Id = 0,
                Title = "组卷审批",
                ReceiptType = ReceiptTypeEnum.ZuJuan,
                RelationId = model.PaperId,
                ClassId = 0,
                Group_Id = model.Group_Id,
                School_Id = model.School_Id,
                CreateBy = model.CreateBy,
                CreateTime = model.CreateTime,
                UpdateBy = model.CreateBy,
                UpdateTime = model.CreateTime,
                VerifyStatus = Common.Enum.EduTask.EduTaskRrocessStatus.AuditIng,
                ReceiptFile = ""
            };
            flag = examination_PaperRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Paper_ViewModel.PaperId), model.PaperId));
            if (flag)
            {
                flag = education_ReceiptRepository.SetEducationReceiptRepository(educationReceipt, out string message);
            }
            return flag;
        }


        /// <summary>
        /// 根据试卷编号获取试卷实体
        /// </summary>
        /// <param name="PaperId"></param>
        /// <param name="isShowAnswer">是否查询答案</param>
        /// <returns></returns>
        public RB_Examination_Paper_ViewModel GetExaminationPaperModule(object PaperId)
        {
            // 定义正则表达式用来匹配 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]*>";
            var extModel = examination_PaperRepository.GetEntity<RB_Examination_Paper_ViewModel>(PaperId);
            if (extModel != null && extModel.PaperId > 0)
            {
                extModel.CreateByName = UserReidsCache.GetUserLoginInfo(extModel.CreateBy)?.AccountName ?? "";
                extModel.GroupList = GetExaminationGroupListModule(new RB_Examination_Group_ViewModel()
                {
                    PaperId = extModel.PaperId
                })?.OrderBy(qitem => qitem.GSortNum)?.ToList();
                var detailsList = GetExaminationDetailsListModule(new RB_Examination_Details_ViewModel()
                {
                    PaperId = extModel.PaperId
                });
                if (extModel.GroupList != null && extModel.GroupList.Count > 0 && detailsList != null && detailsList.Count > 0)
                {
                    List<int> pointsList = new List<int>();
                    var tempPoints = Common.ConvertHelper.StringToList(string.Join(",", detailsList?.Where(qitem => !string.IsNullOrEmpty(qitem.Knowledge))?.Select(qitem => qitem.Knowledge)));
                    if (tempPoints != null && tempPoints.Count > 0)
                    {
                        pointsList.AddRange(tempPoints);
                    }
                    List<RB_Question_Point_ViewModel> pointList = new List<RB_Question_Point_ViewModel>();
                    if (pointsList != null && pointsList.Count > 0)
                    {
                        pointList = question_PointRepository.GetPointListRepository(new RB_Question_Point_ViewModel()
                        {
                            QPointIds = string.Join(",", pointsList)
                        });
                    }
                    foreach (var gItem in extModel.GroupList)
                    {
                        gItem.DetailsList = detailsList?.Where(qitem => qitem.GId == gItem.GId)?.OrderBy(qitem => qitem.SortNum)?.ToList() ?? new List<RB_Examination_Details_ViewModel>();
                        if (gItem.DetailsList != null && gItem.DetailsList.Count > 0)
                        {
                            gItem.GScore = gItem?.DetailsList?.Sum(qitem => qitem.Score) ?? 0;
                            foreach (var sItem in gItem.DetailsList)
                            {
                                List<object> quesAnswerList = new List<object>();
                                //完型填空
                                if (sItem.QuestionTypeKey == "cloze")
                                {
                                    if (sItem.Answer != null && !string.IsNullOrEmpty(sItem.Answer.ToString()))
                                    {
                                        try
                                        {
                                            var qAnsList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(sItem.Answer.ToString());
                                            if (qAnsList != null && qAnsList.Count() > 0)
                                            {
                                                foreach (var tItem in qAnsList)
                                                {
                                                    quesAnswerList.Add(tItem);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Common.Plugin.LogHelper.Write(ex, "GetExaminationPaperModule:" + string.Format("PaperId_{0}:DetailsId:{1}", PaperId, sItem.Id));
                                        }
                                    }
                                }
                                else if (sItem.QuestionTypeKey == "reading-comprehensio" || sItem.QuestionTypeKey == "listening")
                                {
                                    if (sItem.Answer != null && !string.IsNullOrEmpty(sItem.Answer.ToString()))
                                    {
                                        try
                                        {
                                            var qAnsList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(sItem.Answer.ToString());
                                            if (qAnsList != null && qAnsList.Count() > 0)
                                            {
                                                foreach (var tItem in qAnsList)
                                                {
                                                    quesAnswerList.Add(tItem);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Common.Plugin.LogHelper.Write(ex, "GetExaminationPaperModule:" + string.Format("PaperId_{0}:DetailsId:{1}", PaperId, sItem.Id));
                                        }
                                    }
                                }
                                sItem.QuestionAnswerList = quesAnswerList;
                                sItem.QuestionPointList = pointList?.Where(qitem => sItem.Knowledge.Contains(qitem.PointId.ToString()))?.ToList() ?? new List<RB_Question_Point_ViewModel>();

                                try
                                {
                                    sItem.QuestionContentObj = analysisQuestion.ParsingQuestion(sItem.QuestionTypeKey, sItem.QuestionContent);
                                }
                                catch (Exception ex)
                                {
                                    Common.Plugin.LogHelper.Write(ex, "GetExaminationPaperModule— " + Common.Plugin.JsonHelper.Serialize(sItem));
                                }
                                string newTitle = sItem.Title;
                                try
                                {
                                    newTitle = Regex.Replace(newTitle, str, "[图片]");
                                    newTitle = Regex.Replace(newTitle, @"<iframe\s*[^>]*>", "[音频", RegexOptions.IgnoreCase);
                                    newTitle = Regex.Replace(newTitle, @"</iframe>", "]", RegexOptions.IgnoreCase);
                                }
                                catch
                                {
                                    newTitle = sItem.Title;
                                }
                                sItem.ShowTitle = newTitle;
                            }
                        }
                    }
                }
            }
            return extModel;
        }

        /// <summary>
        /// 根据编号删除试卷题目
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool RemoveExaminationDetailsModule(int Id)
        {
            return examination_DetailsRepository.Delete(Id) > 0;
        }

        /// <summary>
        /// 根据分组删除分组和题目
        /// </summary>
        /// <param name="GId"></param>
        /// <param name="QuestionTypeId">问题类型编号</param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool RemoveExamnationGroupModule(int GId, int QuestionTypeId, int PaperId)
        {
            var flag = true;
            if (GId > 0)
            {
                List<WhereHelper> gList = new List<WhereHelper>()
                {
                    new WhereHelper(nameof(RB_Examination_Group_ViewModel.GId), GId),
                    new WhereHelper(nameof(RB_Examination_Group_ViewModel.PaperId), PaperId),
                };
                flag = examination_GroupRepository.Delete(gList);
            }
            if (flag && QuestionTypeId > 0)
            {
                List<WhereHelper> dList = new List<WhereHelper>()
                {
                  new WhereHelper(nameof(RB_Examination_Details_ViewModel.QuestionTypeId), QuestionTypeId),
                  new WhereHelper(nameof(RB_Examination_Details_ViewModel.PaperId), PaperId),
                };
                examination_DetailsRepository.Delete(dList);
            }
            return flag;
        }

        /// <summary>
        /// 根据试卷编号删除试卷
        /// </summary>
        /// <param name="PaperIds"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool RemoveExamnationPaperModule(List<int> PaperIds)
        {
            var flag = true;
            foreach (var PaperId in PaperIds)
            {
                var model = GetExaminationPaperModule(PaperId);
                var childList = examination_PaperRepository.GetExaminationPaperChildRepository(PaperId.ToString());
                if (PaperId > 0)
                {
                    flag = examination_PaperRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Paper_ViewModel.PaperId), PaperId));
                }
                if (flag && model.GroupList != null && model.GroupList.Count > 0)
                {
                    flag = examination_GroupRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Group_ViewModel.PaperId), PaperId));
                }
                if (flag && model.GroupList != null && model.GroupList.Count > 0 && model.GroupList[0].DetailsList != null && model.GroupList[0].DetailsList.Count > 0)
                {
                    flag = examination_DetailsRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Details_ViewModel.PaperId), PaperId));
                }
                //删除所有的子节点信息
                if (childList != null && childList.Count > 0)
                {
                    foreach (var item in childList)
                    {
                        examination_PaperRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Paper_ViewModel.PaperId), item.PaperId));
                        examination_GroupRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Group_ViewModel.PaperId), item.PaperId));
                        examination_DetailsRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Details_ViewModel.PaperId), item.PaperId));
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 根据试卷编号复制试卷
        /// </summary>
        /// <param name="PaperId"></param>
        /// <param name="UserInfo">用户信息</param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool CopyExamnationPaperModule(int PaperId, UserInfo UserInfo)
        {
            var flag = true;
            var model = GetExaminationPaperModule(PaperId);
            if (model == null)
            {
                flag = false;
                return flag;
            }
            model.PaperId = 0;
            model.CreateBy = UserInfo.Id;
            model.Group_Id = UserInfo.Group_Id;
            model.School_Id = UserInfo.School_Id;
            model.CreateTime = DateTime.Now;

            if (model.GroupList != null && model.GroupList.Count > 0)
            {
                foreach (var gItem in model.GroupList)
                {
                    gItem.GId = 0;
                    gItem.PaperId = 0;
                    if (gItem.DetailsList != null && gItem.DetailsList.Count > 0)
                    {
                        foreach (var sItem in gItem.DetailsList)
                        {
                            sItem.Id = 0;
                            sItem.PaperId = 0;
                        }
                    }
                }
            }
            model.ExamineRemark = "";
            model.Reviewer = 0;
            model.ExamineStatus = 0;
            flag = SetExaminationPaperModule(model);
            return flag;
        }

        /// <summary>
        /// 移动试卷
        /// </summary>
        /// <param name="ParentId">父节点编号</param>
        /// <param name="PaperIds">试卷编号</param>
        /// <returns></returns>
        public bool MoveExamnationPaperModule(int ParentId, List<int> PaperIds)
        {
            var flag = true;
            flag = examination_PaperRepository.BatchMovePaperRepository(ParentId, PaperIds) > 0;
            return flag;
        }

        /// <summary>
        /// 获取文件夹树形结构
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public object GetFolderTreeModule(RB_Examination_Paper_ViewModel query)
        {
            List<ExamFolderTreeModel> list = new List<ExamFolderTreeModel>();
            var dataList = examination_PaperRepository.GetExaminationPaperListRepository(query);
            ExamFolderTreeModel root = new ExamFolderTreeModel()
            {
                PaperId = 0,
                ParentId = 0,
                PaperName = "根目录",
                ChildList = new List<ExamFolderTreeModel>()
            };
            if (dataList != null && dataList.Count > 0)
            {
                var rootList = dataList?.Where(qitem => qitem.ParentId == 0)?.ToList();
                foreach (var rItem in rootList)
                {
                    ExamFolderTreeModel fTreeModel = new ExamFolderTreeModel()
                    {
                        PaperId = rItem.PaperId,
                        ParentId = rItem.ParentId,
                        PaperName = rItem.PaperName,
                        ChildList = new List<ExamFolderTreeModel>()
                    };
                    fTreeModel.ChildList = GetFolderChild(dataList, rItem.PaperId);
                    root.ChildList.Add(fTreeModel);
                }
            }
            list.Add(root);
            return list;
        }

        /// <summary>
        /// 递归遍历所有下级文件夹
        /// </summary>
        /// <param name="sourceList">源数据</param>
        /// <param name="parentId">父节点编号</param>
        /// <returns></returns>
        public List<ExamFolderTreeModel> GetFolderChild(List<RB_Examination_Paper_ViewModel> sourceList, int parentId)
        {
            List<ExamFolderTreeModel> resultList = new List<ExamFolderTreeModel>();
            //获取下级节点
            var subList = sourceList?.Where(qItem => qItem.ParentId == parentId).ToList();
            //如果存在下级节点
            if (subList != null && subList.Count > 0)
            {
                foreach (var childItem in subList)
                {
                    var childModel = new ExamFolderTreeModel()
                    {
                        PaperId = childItem.PaperId,
                        ParentId = childItem.ParentId,
                        PaperName = childItem.PaperName,
                        ChildList = new List<ExamFolderTreeModel>()
                    };
                    childModel.ChildList.AddRange(GetFolderChild(sourceList, childItem.PaperId));
                    resultList.Add(childModel);
                }
            }
            return resultList;
        }


        /// <summary>
        /// 批量更新试卷是否开放
        /// </summary>
        /// <param name="paperIds"></param>
        /// <param name="IsOpen"></param>
        /// <returns></returns>
        public bool BatchSetIsOpenModule(List<int> paperIds, int IsOpen)
        {
            var list = examination_PaperRepository.GetExaminationPaperParentRepository(string.Join(",", paperIds));
            foreach (var item in paperIds)
            {

            }
            return examination_PaperRepository.BatchSetIsOpenRepository(paperIds, IsOpen) > 0;
        }


        /// <summary>
        /// 获取发布试卷分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Publish_ViewModel> GetExaminationPublishPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Publish_ViewModel query)
        {
            var list = examination_PublishRepository.GetExaminationPublishPageRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                string ids = string.Join(",", list.Select(qitem => qitem.Id));
                List<RB_Examination_Student_ViewModel> stuList = new List<RB_Examination_Student_ViewModel>();
                if (!string.IsNullOrEmpty(ids))
                {
                    stuList = examination_StudentRepository.GetExaminationStudentListRepository(new RB_Examination_Student_ViewModel()
                    {
                        QPublishIds = ids
                    });
                }
                foreach (var item in list)
                {
                    item.StudentCount = stuList?.Where(qitem => qitem.PublishId == item.Id)?.Count() ?? 0;
                }
            }
            return list;
        }

        /// <summary>
        /// 新增修改考试发布
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetExaminationPublishModule(RB_Examination_Publish_ViewModel model)
        {
            bool flag = true;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Examination_Publish_ViewModel.PaperId), model.PaperId },
                    {nameof(RB_Examination_Publish_ViewModel.PublishType), model.PublishType },
                    {nameof(RB_Examination_Publish_ViewModel.ExamStartTime), model.ExamStartTime },
                    {nameof(RB_Examination_Publish_ViewModel.ExamEndTime), model.ExamEndTime },
                    {nameof(RB_Examination_Publish_ViewModel.ExamTimes), model.ExamTimes },
                    {nameof(RB_Examination_Publish_ViewModel.SubmitTimes), model.SubmitTimes },
                    {nameof(RB_Examination_Publish_ViewModel.EnterTimes), model.EnterTimes },
                    {nameof(RB_Examination_Publish_ViewModel.IsQuestionRandom), model.IsQuestionRandom },
                    {nameof(RB_Examination_Publish_ViewModel.IsOptionRandom), model.IsOptionRandom },
                    {nameof(RB_Examination_Publish_ViewModel.IsLeaveAnswer), model.IsLeaveAnswer },
                    {nameof(RB_Examination_Publish_ViewModel.LeaveTimes), model.LeaveTimes },
                    {nameof(RB_Examination_Publish_ViewModel.IsDisableMultiTerminal), model.IsDisableMultiTerminal },
                    {nameof(RB_Examination_Publish_ViewModel.IsExamType), model.IsExamType },
                    {nameof(RB_Examination_Publish_ViewModel.ExamTypeValue), model.ExamTypeValue },
                    {nameof(RB_Examination_Publish_ViewModel.ExamNotice), model.ExamNotice },
                    {nameof(RB_Examination_Publish_ViewModel.PassScore), model.PassScore },
                    {nameof(RB_Examination_Publish_ViewModel.IsAutoSubmit), model.IsAutoSubmit },
                    {nameof(RB_Examination_Publish_ViewModel.FillInIsSubject), model.FillInIsSubject },
                    {nameof(RB_Examination_Publish_ViewModel.FillInIsIgnore), model.FillInIsIgnore },
                    {nameof(RB_Examination_Publish_ViewModel.IsHalfScore), model.IsHalfScore },
                    {nameof(RB_Examination_Publish_ViewModel.ExamineStatus), model.ExamineStatus },
                    {nameof(RB_Examination_Publish_ViewModel.PublishPic), model.PublishPic },
                };
                flag = examination_PublishRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Publish_ViewModel.Id), model.Id));
            }
            else
            {
                var newId = examination_PublishRepository.Insert(model);
                model.Id = newId;
                flag = newId > 0;
                examination_PaperRepository.UpdateExaminationPaperPublishCountRepository(model.PaperId);
            }

            var oldStudentList = examination_StudentRepository.GetExaminationStudentListRepository(new RB_Examination_Student_ViewModel() { PublishId = model.Id });
            //以前没有学员【直接新增】
            if (oldStudentList == null || (oldStudentList != null && oldStudentList.Count == 0))
            {
                if (model.StudentList != null && model.StudentList.Count > 0)
                {
                    foreach (var item in model.StudentList)
                    {
                        item.CreateBy = model.CreateBy;
                        item.CreateTime = DateTime.Now;
                        item.PublishId = model.Id;
                        item.PaperId = model.PaperId;
                        item.Group_Id = model.Group_Id;
                        item.School_Id = model.School_Id;
                        item.ExamStatus = ExamTestStatusEnum.NotStarted;
                        examination_StudentRepository.Insert(item);
                    }
                }
            }
            else//以前有学员
            {
                //现在没有学员了【直接删除以前的学员】
                if (model.StudentList == null || (model.StudentList != null && model.StudentList.Count == 0))
                {
                    examination_StudentRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Student_ViewModel.PublishId), model.Id));
                }
                //找出差异的数据
                var deleteList = oldStudentList.Where(qitem => !model.StudentList.Any(oldItem => qitem.Id == oldItem.Id)).ToList();
                foreach (var dItem in deleteList)
                {
                    if (dItem.Id > 0)
                    {
                        examination_StudentRepository.DeleteOne(new WhereHelper(nameof(RB_Examination_Student_ViewModel.Id), dItem.Id));
                    }
                }
                foreach (var dItem in model.StudentList)
                {
                    dItem.CreateBy = model.CreateBy;
                    dItem.CreateTime = DateTime.Now;
                    dItem.PublishId = model.Id;
                    dItem.PaperId = model.PaperId;
                    dItem.Group_Id = model.Group_Id;
                    dItem.School_Id = model.School_Id;

                    if (dItem.Id == 0)
                    {
                        dItem.ExamStatus = ExamTestStatusEnum.NotStarted;
                        examination_StudentRepository.Insert(dItem);
                    }
                    else
                    {
                        examination_StudentRepository.Update(dItem);
                    }
                }
            }
            return flag;
        }


        /// <summary>
        /// 获取学生发布试卷分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Publish_ViewModel> GetGuestExaminationPublishPage(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Publish_ViewModel query)
        {
            return examination_PublishRepository.GetGuestExaminationPublishPage(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取学生发布试卷分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Publish_ViewModel> GetGuestExaminationPublishPage_V2(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Publish_ViewModel query)
        {
            return examination_PublishRepository.GetGuestExaminationPublishPage_V2(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 提交考试申请
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool SubmitExamApplyModule(int Id)
        {
            bool flag = false;
            var model = GetExaminationPublishModule(Id);
            Dictionary<string, object> fileds = new Dictionary<string, object>();
            //当前试卷考试试卷已超过当前试卷更新为作废状态
            if (model.ExamEndTime < DateTime.Now)
            {
                fileds.Add(nameof(RB_Examination_Publish_ViewModel.ExamineStatus), (int)EduTaskRrocessStatus.Invalid);
                fileds.Add(nameof(RB_Examination_Publish_ViewModel.PublishRemark), "考试时间已超过当前时间，更新为作废");
                flag = examination_PublishRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Publish_ViewModel.Id), model.Id));
                return flag;
            }
            var obj = GetExaminationConfigureModel(new RB_Examination_Configure_ViewModel()
            {
                Group_Id = model.Group_Id,
            });

            fileds.Add(nameof(RB_Examination_Publish_ViewModel.PublishRemark), "");
            if (obj.IsOpenExamApply == 0)
            {
                fileds.Add(nameof(RB_Examination_Publish_ViewModel.Reviewer), 0);
                fileds.Add(nameof(RB_Examination_Publish_ViewModel.ExamineStatus), (int)Common.Enum.EduTask.EduTaskRrocessStatus.AuditThrough);
            }
            else
            {
                fileds.Add(nameof(RB_Examination_Publish_ViewModel.Reviewer), obj.ExamApplyReviewer);
                fileds.Add(nameof(RB_Examination_Publish_ViewModel.ExamineStatus), (int)Common.Enum.EduTask.EduTaskRrocessStatus.AuditIng);
            }
            flag = examination_PublishRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Publish_ViewModel.Id), model.Id));
            if (flag && obj.IsOpenExamApply == 1)
            {
                var educationReceipt = new Model.Entity.EduTask.RB_Education_Receipt()
                {
                    Id = 0,
                    Title = "考试申请",
                    ReceiptType = ReceiptTypeEnum.ExamApply,
                    RelationId = model.Id,
                    ClassId = 0,
                    Group_Id = model.Group_Id,
                    School_Id = model.School_Id,
                    CreateBy = model.CreateBy,
                    CreateTime = model.CreateTime,
                    UpdateBy = model.CreateBy,
                    UpdateTime = model.CreateTime,
                    VerifyStatus = Common.Enum.EduTask.EduTaskRrocessStatus.AuditIng,
                    ReceiptFile = ""
                };
                flag = education_ReceiptRepository.SetEducationReceiptRepository(educationReceipt, out string message);
            }
            return flag;
        }

        #region 考试配置
        /// <summary>
        /// 获取考试配置实体
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>

        public RB_Examination_Configure_ViewModel GetExaminationConfigureModel(RB_Examination_Configure_ViewModel query)
        {
            return examination_ConfigureRepository.GetExaminationConfigureListRepository(query).FirstOrDefault();
        }

        /// <summary>
        /// 新增考试配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetExaminationConfigure(RB_Examination_Configure_ViewModel model)
        {
            bool flag = false;
            if (model.Id == 0)
            {
                flag = examination_ConfigureRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Examination_Configure_ViewModel.IsOpenAutoMarking), model.IsOpenAutoMarking },
                    {nameof(RB_Examination_Configure_ViewModel.IsOpenExamApply), model.IsOpenExamApply },
                    {nameof(RB_Examination_Configure_ViewModel.IsOpenZuJuan), model.IsOpenZuJuan },
                    {nameof(RB_Examination_Configure_ViewModel.ZuJuanReviewer), model.ZuJuanReviewer },
                    {nameof(RB_Examination_Configure_ViewModel.ExamApplyReviewer), model.ExamApplyReviewer }

                };
                flag = examination_ConfigureRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Configure_ViewModel.Id), model.Id));
            }
            return flag;
        }
        #endregion

        /// <summary>
        /// 根据编号获取考试发布详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Examination_Publish_ViewModel GetExaminationPublishModule(object Id)
        {
            var extModel = examination_PublishRepository.GetEntity<RB_Examination_Publish_ViewModel>(Id);
            if (extModel == null)
            {
                extModel = new RB_Examination_Publish_ViewModel();
            }
            if (extModel != null && extModel.Id > 0)
            {
                extModel.StudentList = examination_StudentRepository.GetExaminationStudentListRepository(new RB_Examination_Student_ViewModel()
                {
                    PublishId = extModel.Id
                });
            }
            return extModel;
        }

        /// <summary>
        /// 删除考试
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool RemoveExamPublishModule(int Id)
        {
            bool flag = false;
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Examination_Publish_ViewModel.Status),(int)DateStateEnum.Delete }
            };
            flag = examination_PublishRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Publish_ViewModel.Id), Id));
            return flag;
        }

        #region 学生管理
        /// <summary>
        /// 获取考试学生分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Student_ViewModel> GetExaminationStudentPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Student_ViewModel query)
        {
            return examination_StudentRepository.GetExaminationStudentPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// App小程序获取试卷详情
        /// </summary>
        /// <param name="PaperId"></param>
        /// <param name="GuestId"></param>
        /// <param name="PublishId"></param>
        /// <returns></returns>
        public object AppGetExamPaperInfoModule(object PaperId, bool isShowAnswer = false, int GuestId = 0, int PublishId = 0)
        {
            var examPublish = GetExaminationPublishModule(PublishId);
            var extModel = GetExaminationPaperModule(PaperId);
            var paperObj = new object();
            decimal ExamScore = 0;
            RB_Examination_Student_ViewModel examinationStudentModel = new RB_Examination_Student_ViewModel();
            if (extModel != null && extModel.PaperId > 0)
            {
                List<RB_Examination_StudentDetails_ViewModel> studentDetailsList = new List<RB_Examination_StudentDetails_ViewModel>();

                if (GuestId > 0 && PublishId > 0)
                {
                    examinationStudentModel = examination_StudentRepository.GetExaminationStudentListRepository(new RB_Examination_Student_ViewModel { PaperId = extModel.PaperId, PublishId = PublishId, GuestId = GuestId }).FirstOrDefault();
                    studentDetailsList = examination_StudentDetailsRepository.GetExaminationStudentListRepository(new RB_Examination_StudentDetails_ViewModel { PaperId = extModel.PaperId, PublishId = PublishId, GuestId = GuestId });
                }
                List<object> paperTypeList = new List<object>();
                if (extModel.GroupList != null && extModel.GroupList.Count > 0)
                {
                    foreach (var gItem in extModel.GroupList)
                    {
                        List<object> questionList = new List<object>();
                        //学生答题详问题列表
                        if (studentDetailsList != null && studentDetailsList.Count > 0)
                        {
                            var stuTempList = studentDetailsList.Where(qitem => qitem.StuPaperGroupId == gItem.GId);
                            foreach (var sItem in stuTempList)
                            {
                                var questionModel = gItem?.DetailsList.Where(qitem => qitem.Id == sItem.DetailsId)?.FirstOrDefault();
                                if (questionModel != null)
                                {
                                    ExamScore += questionModel.Score;
                                    var QuestionContentObj = new object();
                                    if (isShowAnswer)
                                    {
                                        QuestionContentObj = analysisQuestion.ParsingQuestion(questionModel.QuestionTypeKey, questionModel.QuestionContent, isEdit: true);
                                    }
                                    else
                                    {
                                        QuestionContentObj = analysisQuestion.ParsingQuestion(questionModel.QuestionTypeKey, sItem.StudentContent, isEdit: true);
                                    }
                                    List<object> answerList = new List<object>();
                                    List<object> quesAnswerList = new List<object>();
                                    //填空题、分录题、资料题
                                    if (questionModel.QuestionTypeKey == "fill-in" || questionModel.QuestionTypeKey == "entry-problem" || questionModel.QuestionTypeKey == "data-question")
                                    {
                                        var tempList = sItem.StundetAnswer.Split('★');
                                        if (tempList != null && tempList.Count() > 0)
                                        {
                                            foreach (var tItem in tempList)
                                            {
                                                answerList.Add(tItem);
                                            }
                                        }
                                    }
                                    //完型填空
                                    else if (questionModel.QuestionTypeKey == "cloze")
                                    {
                                        if (!string.IsNullOrEmpty(questionModel.Answer.ToString()))
                                        {
                                            var qAnsList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(questionModel.Answer.ToString());
                                            if (qAnsList != null && qAnsList.Count() > 0)
                                            {
                                                foreach (var tItem in qAnsList)
                                                {
                                                    quesAnswerList.Add(tItem);
                                                }
                                            }
                                        }
                                        if (!string.IsNullOrEmpty(sItem.StundetAnswer.ToString()))
                                        {
                                            var tempList = JsonHelper.DeserializeObject<List<SubAnswerItem>>(sItem.StundetAnswer.ToString());
                                            if (tempList != null && tempList.Count() > 0)
                                            {
                                                foreach (var tItem in tempList)
                                                {
                                                    answerList.Add(tItem);
                                                }
                                            }
                                        }
                                    }
                                    else if (questionModel.QuestionTypeKey == "reading-comprehensio" || questionModel.QuestionTypeKey == "listening")
                                    {
                                        if (!string.IsNullOrEmpty(questionModel.Answer.ToString()))
                                        {
                                            var qAnsList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(questionModel.Answer.ToString());
                                            if (qAnsList != null && qAnsList.Count() > 0)
                                            {
                                                foreach (var tItem in qAnsList)
                                                {
                                                    quesAnswerList.Add(tItem);
                                                }
                                            }
                                        }
                                        if (!string.IsNullOrEmpty(sItem.StundetAnswer.ToString()))
                                        {
                                            var qList = JsonHelper.DeserializeObject<List<SubAnswerItem>>(sItem.StundetAnswer.ToString());
                                            foreach (var mItem in qList)
                                            {
                                                answerList.Add(mItem);
                                            }
                                        }
                                    }
                                    var qObj = new
                                    {
                                        PaperDetailsId = sItem.DetailsId,//试卷问题编号
                                        StundetDetailsId = sItem.Id,//考生答题编号
                                        sItem.PaperId,
                                        questionModel.Title,
                                        questionModel.ShowTitle,
                                        QuestionContentObj,
                                        questionModel.QuestionTypeId,
                                        questionModel.QuestionTypeKey,
                                        questionModel.DifficultyType,
                                        questionModel.Score,
                                        Answer = isShowAnswer ? questionModel.Answer : sItem.StundetAnswer,
                                        sItem.StundetAnswer,
                                        QuestionAnswerList = quesAnswerList,
                                        AnswerList = isShowAnswer ? answerList : new List<object>(),
                                        questionModel.AnswerParse,
                                        sItem.StudentScore,
                                        sItem.IsMarking
                                    };
                                    questionList.Add(qObj);
                                }
                            }
                        }
                        else
                        {
                            var tempDetailsList = new List<RB_Examination_Details_ViewModel>();
                            //题目乱序
                            if (examPublish.IsQuestionRandom == 1)
                            {
                                tempDetailsList = gItem.DetailsList.OrderBy(qitem => Guid.NewGuid()).ToList();
                            }
                            else
                            {
                                tempDetailsList = gItem.DetailsList;
                            }
                            List<RB_Examination_StudentDetails_ViewModel> tempStudentList = new List<RB_Examination_StudentDetails_ViewModel>();
                            foreach (var sItem in tempDetailsList)
                            {
                                ExamScore += sItem.Score;
                                sItem.QuestionContentObj = analysisQuestion.ParsingQuestion(sItem.QuestionTypeKey, sItem.QuestionContent, isNoAnswer: true, isOptionRandom: examPublish.IsOptionRandom);
                                var qObj = new
                                {
                                    PaperDetailsId = sItem.Id,//试卷问题编号
                                    StundetDetailsId = 0,//考生答题编号
                                    sItem.PaperId,
                                    sItem.Title,
                                    sItem.ShowTitle,
                                    sItem.QuestionContentObj,
                                    sItem.QuestionTypeId,
                                    sItem.QuestionTypeKey,
                                    sItem.DifficultyType,
                                    sItem.Score,
                                    Answer = "",
                                    StundetAnswer = "",
                                    AnswerParse = "",
                                    StundetScore = 0,
                                    IsMarking = 0,
                                };
                                questionList.Add(qObj);
                            }
                        }
                        paperTypeList.Add(new
                        {
                            gItem.GId,
                            gItem.QuestionTypeKey,
                            gItem.QuestionTypeId,
                            gItem.GroupName,
                            gItem.GScore,
                            DetailsList = questionList
                        });
                    }
                }
                paperObj = new
                {
                    extModel.PaperId,
                    extModel.PaperName,
                    GuestId,
                    extModel.Group_Id,
                    PublishId = examPublish.Id,
                    GroupList = paperTypeList,
                };
            }
            examPublish.GuestId = GuestId;
            extModel.GuestId = GuestId;
            if (examinationStudentModel == null)
            {
                examinationStudentModel = new RB_Examination_Student_ViewModel
                {
                    ExamStatus = ExamTestStatusEnum.NotStarted
                };
            }
            if (examPublish.ExamStartTime < System.DateTime.Now && examPublish.ExamEndTime > System.DateTime.Now)
            {
                if (examinationStudentModel.ExamStatus != ExamTestStatusEnum.FinishTest && examinationStudentModel.ExamStatus != ExamTestStatusEnum.FinishScoring)
                {
                    examinationStudentModel.ExamStatus = ExamTestStatusEnum.FinishStarted;
                }
            }
            else if (examPublish.ExamEndTime < System.DateTime.Now)
            {
                if (examinationStudentModel.ExamStatus != ExamTestStatusEnum.FinishTest && examinationStudentModel.ExamStatus != ExamTestStatusEnum.FinishScoring)
                {
                    examinationStudentModel.ExamStatus = ExamTestStatusEnum.MissedTest;
                }
            }
            return new
            {
                Publish = new
                {
                    examPublish.PicList,
                    examPublish.GuestId,
                    ExamStartTime = Common.ConvertHelper.FormatTime(examPublish.ExamStartTime),
                    ExamEndTime = Common.ConvertHelper.FormatTime(examPublish.ExamEndTime),
                    examPublish.ExamTimes,
                    examPublish.SubmitTimes,
                    examPublish.EnterTimes,
                    examPublish.IsQuestionRandom,
                    examPublish.IsOptionRandom,
                    examPublish.IsLeaveAnswer,
                    examPublish.LeaveTimes,
                    examPublish.IsDisableMultiTerminal,
                    examPublish.IsExamType,
                    examPublish.ExamTypeValue,
                    examPublish.ExamNotice,
                    examPublish.IsAutoSubmit,
                    ExamScore,
                },
                TotalScore = examinationStudentModel?.TotalScore ?? 0,
                StuComment = examinationStudentModel?.StuComment ?? "",
                ExamStatus = examinationStudentModel?.ExamStatus ?? 0,
                ExamStatusName = examinationStudentModel?.ExamStatus.ToName() ?? "",
                Paper = paperObj
            };
        }

        /// <summary>
        /// 更新学员考试考试时间
        /// </summary>
        /// <param name="PublishId"></param>
        /// <param name="GuestId"></param>
        /// <param name="PaperId"></param>
        /// <returns></returns>
        public virtual bool SetStuExamStartTimeModule(int PublishId, int GuestId, int PaperId)
        {
            bool flag = true;
            var model = examination_StudentRepository.GetExaminationStudentListRepository(new RB_Examination_Student_ViewModel()
            {
                PaperId = PaperId,
                PublishId = PublishId,
                GuestId = GuestId
            })?.FirstOrDefault();
            if (model != null)
            {
                if (model.ExamStartTime == null)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Examination_Student_ViewModel.ExamStartTime), DateTime.Now},
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>()
                    {
                            new WhereHelper(nameof(RB_Examination_Student_ViewModel.PaperId),PaperId),
                            new WhereHelper(nameof(RB_Examination_Student_ViewModel.PublishId),PublishId),
                            new WhereHelper(nameof(RB_Examination_Student_ViewModel.GuestId),GuestId),
                    };
                    flag = examination_StudentRepository.Update(fileds, wheres);
                }
            }
            return flag;
        }

        /// <summary>
        /// 学员提交考试
        /// </summary>
        /// <param name="PublishId">发布考试编号</param>
        /// <param name="GuestId">学员编号</param>
        /// <param name="PaperId">试卷编号</param>
        /// <param name="UserGroupId">集团编号</param>
        /// <param name="list">学生题目数据</param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetStuExamPaperDetailsModule(int PublishId, int GuestId, int PaperId, int UserGroupId, List<RB_Examination_Details_ViewModel> list, int Exam_Student_Id)
        {
            bool flag = false;
            var modelPublish = GetExaminationPublishModule(PublishId);
            List<RB_Examination_StudentDetails_ViewModel> stuList = new List<RB_Examination_StudentDetails_ViewModel>();
            if (list != null && list.Any())
            {
                foreach (var item in list)
                {
                    analysisQuestion.CheckQuestion(item.QuestionTypeKey, item.QuestionContent, item.Answer, out string newAnswer, IsRequire: false);
                    RB_Examination_StudentDetails_ViewModel model = new RB_Examination_StudentDetails_ViewModel()
                    {
                        Id = item.StundetDetailsId,
                        PublishId = PublishId,
                        PaperId = PaperId,
                        DetailsId = item.Id,
                        GuestId = GuestId,
                        CreateTime = DateTime.Now,
                        Status = DateStateEnum.Normal,
                        StundetAnswer = newAnswer,
                        StudentScore = 0,
                        IsMarking = 0,
                        StudentContent = item.QuestionContent,
                        StuPaperGroupId = item.StuPaperGroupId,
                    };
                    if (model.Id > 0)
                    {
                        Dictionary<string, object> stuFileds = new Dictionary<string, object>()
                        {
                            {nameof(RB_Examination_StudentDetails_ViewModel.StundetAnswer),model.StundetAnswer },
                            {nameof(RB_Examination_StudentDetails_ViewModel.StudentContent),model.StudentContent },
                        };
                        flag = examination_StudentDetailsRepository.Update(stuFileds, new WhereHelper(nameof(RB_Examination_StudentDetails_ViewModel.Id), model.Id));
                    }
                    else
                    {
                        var newId = examination_StudentDetailsRepository.Insert(model);
                        model.Id = newId;
                        flag = newId > 0;
                    }
                    stuList.Add(model);
                }
                if (flag)//试卷提交成功，更新考试最后时间
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Examination_Student_ViewModel.ExamEndTime), DateTime.Now},
                        {nameof(RB_Examination_Student_ViewModel.ExamStatus ), (int)ExamTestStatusEnum.FinishTest},
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>()
                    {
                         new WhereHelper(nameof(RB_Examination_Student_ViewModel.PaperId),PaperId),
                         new WhereHelper(nameof(RB_Examination_Student_ViewModel.PublishId),PublishId),
                    };
                    if (Exam_Student_Id > 0)
                    {
                        wheres.Add(new WhereHelper(nameof(RB_Examination_Student_ViewModel.Id), Exam_Student_Id));
                    }
                    else
                    {
                        wheres.Add(new WhereHelper(nameof(RB_Examination_Student_ViewModel.GuestId), GuestId));
                    }
                    flag = examination_StudentRepository.Update(fileds, wheres);
                }
                var config = GetExaminationConfigureModel(new RB_Examination_Configure_ViewModel()
                {
                    Group_Id = UserGroupId
                });
                //自动阅卷
                if (config.IsOpenAutoMarking == 1)
                {
                    AutoReviewPaperModule(modelPublish.IsHalfScore, PaperId, stuList);
                }
            }
            return flag;
        }

        /// <summary>
        /// 老师阅卷
        /// </summary>
        /// <param name="PublishId">发布考试编号</param>
        /// <param name="GuestId">学员编号</param>
        /// <param name="PaperId">试卷编号</param>
        /// <param name="StuComment">老师评语</param>
        /// <param name="list">学生题目数据</param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetTeacherScoringModule(int PublishId, int GuestId, int PaperId, string StuComment, List<RB_Examination_Details_ViewModel> list)
        {
            bool flag = false;
            if (list != null && list.Any())
            {
                decimal totalScore = 0;
                foreach (var item in list)
                {
                    decimal tempScore = item.StudentScore;
                    if (item.SubList != null && item.SubList.Count > 0)
                    {
                        totalScore += item.SubList.Sum(qitem => qitem.StudentScore);
                        tempScore = item.SubList.Sum(qitem => qitem.StudentScore);
                    }
                    else
                    {
                        totalScore += item.StudentScore;
                    }

                    RB_Examination_StudentDetails_ViewModel model = new RB_Examination_StudentDetails_ViewModel()
                    {
                        Id = item.StundetDetailsId,
                        StudentScore = item.StudentScore,
                    };
                    if (model.Id > 0)
                    {
                        Dictionary<string, object> stuFileds = new Dictionary<string, object>()
                        {
                            {nameof(RB_Examination_StudentDetails_ViewModel.StudentScore),tempScore },
                            {nameof(RB_Examination_StudentDetails_ViewModel.IsMarking),1 },
                        };
                        if (item.SubList != null && item.SubList.Count > 0)
                        {
                            stuFileds.Add(nameof(RB_Examination_StudentDetails_ViewModel.StundetAnswer), Common.Plugin.JsonHelper.Serialize(item.SubList));
                        }
                        flag = examination_StudentDetailsRepository.Update(stuFileds, new WhereHelper(nameof(RB_Examination_StudentDetails_ViewModel.Id), model.Id));
                    }
                }
                if (flag)//试卷提交成功，更新考试最后时间
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Examination_Student_ViewModel.ExamEndTime), DateTime.Now},
                        {nameof(RB_Examination_Student_ViewModel.ExamStatus ), ExamTestStatusEnum.FinishScoring},
                        {nameof(RB_Examination_Student_ViewModel.StuComment ), StuComment},
                        {nameof(RB_Examination_Student_ViewModel.TotalScore ), totalScore},
                    };
                    List<WhereHelper> wheres = new List<WhereHelper>()
                    {
                         new WhereHelper(nameof(RB_Examination_Student_ViewModel.PaperId),PaperId),
                         new WhereHelper(nameof(RB_Examination_Student_ViewModel.PublishId),PublishId),
                         new WhereHelper(nameof(RB_Examination_Student_ViewModel.GuestId),GuestId),
                    };
                    flag = examination_StudentRepository.Update(fileds, wheres);
                }
            }
            return flag;
        }

        #endregion

        /// <summary>
        /// 获取状态字符串
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public string GetExamineStatusStr(EduTaskRrocessStatus status)
        {
            string str = "草稿";
            switch (status)
            {
                case EduTaskRrocessStatus.NotAudit: str = "草稿"; break;
                case EduTaskRrocessStatus.AuditIng: str = "审核中"; break;
                case EduTaskRrocessStatus.AuditThrough: str = "正常"; break;
                case EduTaskRrocessStatus.AuditNotThrough: str = "驳回"; break;
                case EduTaskRrocessStatus.Rejected: str = "驳回"; break;
                case EduTaskRrocessStatus.OtherHaveAudit: str = "正常"; break;
                case EduTaskRrocessStatus.Invalid: str = "作废"; break;
            }
            return str;
        }

        /// <summary>
        /// 定时处理考卷过期数据
        /// </summary>
        public void DealExamPublish()
        {
            var list = examination_PublishRepository.GetExaminationPublishListRepository(new RB_Examination_Publish_ViewModel()
            {
                IsQNotNormal = 1
            });
            if (list != null && list.Count > 0)
            {
                string ids = string.Join(",", list.Select(qitem => qitem.Id));
                List<RB_Education_Receipt_ViewModel> receiptList = new List<RB_Education_Receipt_ViewModel>();
                if (!string.IsNullOrEmpty(ids))
                {
                    //查询待审核的考试审批信息
                    receiptList = education_ReceiptRepository.GetEducationReceiptListRepository(new EducationReceiptQuery()
                    {
                        ReceiptType = (int)ReceiptTypeEnum.ExamApply,
                        QRelationIds = ids,
                        VerifyStatus = 1
                    });
                }
                if (receiptList != null && receiptList.Count > 0)
                {
                    //修改过期的考试申请单据为作废
                    foreach (var item in receiptList)
                    {
                        Dictionary<string, object> fileds = new Dictionary<string, object>()
                        {
                            {nameof(RB_Education_Receipt_ViewModel.VerifyStatus), (int) EduTaskRrocessStatus.Invalid }
                        };
                        education_ReceiptRepository.Update(fileds, new WhereHelper(nameof(RB_Education_Receipt_ViewModel.Id), item.Id));
                    }
                }
                //修改过期的考试申请未作废
                foreach (var item in list)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Examination_Publish_ViewModel.ExamineStatus), (int) EduTaskRrocessStatus.Invalid }
                    };
                    examination_PublishRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_Publish_ViewModel.Id), item.Id));
                }
            }
        }
    }
}