
using System.Data;
using System.Net;
using System.Text.RegularExpressions;
using AgileObjects.AgileMapper;
using Microsoft.AspNetCore.Http;
using MongoDB.Driver.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Tiobon.Core.Common;
using Tiobon.Core.Common.Caches;
using Tiobon.Core.Common.UserManager;
using Tiobon.Core.IRepository.Base;
using Tiobon.Core.IServices;
using Tiobon.Core.Model;
using Tiobon.Core.Model.Models;
using Tiobon.Core.Services.BASE;

namespace Tiobon.Core.Services
{
    /// <summary>
    /// 试卷 (服务)
    /// </summary>
    public class Ghre_ExamPaperServices : BaseServices<Ghre_ExamPaper, Ghre_ExamPaperDto, InsertGhre_ExamPaperInput, EditGhre_ExamPaperInput>, IGhre_ExamPaperServices
    {
        private readonly IGhre_ExamPaperConfigServices _ghre_ExamPaperConfigServices;
        private readonly IGhre_ExamPaperQuestionServices _ghre_ExamPaperQuestionServices;
        private readonly IGhre_QuestionServices _ghre_QuestionServices;
        private readonly IGhrs_UserServices _ghrs_UserServices;
        private readonly IGhre_QuestionAnswerServices _ghre_QuestionAnswerServices;
        private readonly IGhre_CourseServices _ghre_CourseServices;
        private readonly IGhre_CourseSceneServices _ghre_CourseSceneServices;
        private readonly IBaseRepository<Ghre_ExamPaper> _dal;

        public Ghre_ExamPaperServices(IBaseRepository<Ghre_ExamPaper> dal,
            IGhre_ExamPaperConfigServices ghre_ExamPaperConfigServices,
            IGhre_QuestionServices ghre_QuestionServices,
            IGhre_QuestionAnswerServices ghre_QuestionAnswerServices,
            IGhre_ExamPaperQuestionServices ghre_ExamPaperQuestionServices,
            IGhre_CourseServices ghre_CourseServices,
            IGhre_CourseSceneServices ghre_CourseSceneServices,
            ICaching caching,
            IGhrs_UserServices ghrs_UserServices)
        {
            this._dal = dal;
            base.BaseDal = dal;
            base._caching = caching;
            _ghre_ExamPaperConfigServices = ghre_ExamPaperConfigServices;
            _ghre_ExamPaperQuestionServices = ghre_ExamPaperQuestionServices;
            _ghre_QuestionServices = ghre_QuestionServices;
            _ghre_QuestionAnswerServices = ghre_QuestionAnswerServices;
            _ghrs_UserServices = ghrs_UserServices;
            _ghre_CourseServices = ghre_CourseServices;
            _ghre_CourseSceneServices = ghre_CourseSceneServices;

        }
        public override async Task<Ghre_ExamPaperDto> QueryById(object objId)
        {
            var entity = await base.QueryById(objId);
            entity.SetMethodLabel = await GetParaLabel("SetMethod", entity.SetMethod);
            entity.ScoreMethodLabel = await GetParaLabel("ScoreMethod", entity.ScoreMethod);

            return entity;
        }

        public override async Task<ServicePageResult<Ghre_ExamPaperDto>> QueryFilterPage(QueryBody body)
        {
            var data = await BaseDal.QueryFilterPage(body);
            var data1 = Mapper.Map(data.result.DT_TableDataT1).ToANew<List<Ghre_ExamPaperDto>>();

            var linkIds = data1.Where(x => x.LinkId != null).Select(x => x.LinkId.Value).Distinct().ToList();
            var courses = await _ghre_CourseServices.Query(x => linkIds.Contains(x.Id));
            var courseScenes = await _ghre_CourseSceneServices.Query(x => linkIds.Contains(x.Id));

            data1.ForEach(async x =>
            {
                try
                {
                    x.ScoreMethodLabel = await GetParaLabel("ScoreMethod", x.ScoreMethod);
                    x.TotalScore1 = Regex.Replace(x.PassScore.ToString(), @"\.(0+)$", "") + "/" + Regex.Replace(x.TotalScore.ToString(), @"\.(0+)$", "");
                    x.SetMethodLabel = await GetParaLabel("SetMethod", x.SetMethod);

                    if (x.LinkType == "CourseId")
                        x.CourseName = courses.FirstOrDefault(o => o.Id == x.LinkId)?.CourseName;
                    else if (x.LinkType == "CourseSceneId")
                        x.CourseName = courseScenes.FirstOrDefault(o => o.Id == x.LinkId)?.SceneName;
                }
                catch (Exception)
                {

                }

            });

            return new ServicePageResult<Ghre_ExamPaperDto>(body.pageNum, data.result.DT_TablePageInfoT1.TotalCount, body.pageSize, data1);
        }

        public async Task<ServicePageResult<Ghre_ExamPaperDto>> QueryList(QueryBody body, string status)
        {
            var data = await QueryFilterPage1(body, status);
            var data1 = Mapper.Map(data.result.DT_TableDataT1).ToANew<List<Ghre_ExamPaperDto>>();

            var linkIds = data1.Where(x => x.LinkId != null).Select(x => x.LinkId.Value).Distinct().ToList();
            var courses = await _ghre_CourseServices.Query(x => linkIds.Contains(x.Id));
            var courseScenes = await _ghre_CourseSceneServices.Query(x => linkIds.Contains(x.Id));

            data1.ForEach(async x =>
            {
                try
                {
                    x.ScoreMethodLabel = await GetParaLabel("ScoreMethod", x.ScoreMethod);
                    x.TotalScore1 = Regex.Replace(x.PassScore.ToString(), @"\.(0+)$", "") + "/" + Regex.Replace(x.TotalScore.ToString(), @"\.(0+)$", "");
                    x.SetMethodLabel = await GetParaLabel("SetMethod", x.SetMethod);

                    if (x.LinkType == "CourseId")
                        x.CourseName = courses.FirstOrDefault(o => o.Id == x.LinkId)?.CourseName;
                    else if (x.LinkType == "CourseSceneId")
                        x.CourseName = courseScenes.FirstOrDefault(o => o.Id == x.LinkId)?.SceneName;
                }
                catch (Exception)
                {

                }

            });

            return new ServicePageResult<Ghre_ExamPaperDto>(body.pageNum, data.result.DT_TablePageInfoT1.TotalCount, body.pageSize, data1);
        }

        public async Task<ServicePageResult<Ghre_ExamPaper>> QueryFilterPage1(QueryBody filter, string status = null)
        {
            if (string.IsNullOrWhiteSpace(filter.orderBy))
                filter.orderBy = "CreateTime DESC";
            RefAsync<int> totalCount = 0;
            var query = Db.Queryable<Ghre_ExamPaper>();
            if (!string.IsNullOrWhiteSpace(status))
                query = query.Where(x => x.Status == status);
            string conditions = "1=1";
            if (filter.jsonParam != null)
                foreach (JProperty jProperty in filter.jsonParam.Properties())
                {
                    var name = jProperty.Name;
                    var value = jProperty.Value.ToString();
                    if (name == "page" || name == "pageSize")
                        continue;
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        var jsonParam = JsonConvert.DeserializeObject<JsonParam>(value);

                        switch (jsonParam.operationKey)
                        {
                            case "Include":
                                conditions += $" AND {name} LIKE '%{jsonParam.columnValue}%'";
                                break;
                            case "NotInclude":
                                conditions += $" AND {name} NOT LIKE '%{jsonParam.columnValue}%'";
                                break;
                            case "IsNull":
                                conditions += $" AND {name} IS NULL";
                                break;
                            case "NotNull":
                                conditions += $" AND {name} IS NOT NULL";
                                break;
                            case "Equal":
                                conditions += $" AND {name} ='{jsonParam.columnValue}'";
                                break;
                            case "NotEqual":
                                conditions += $" AND {name} !='{jsonParam.columnValue}'";
                                break;
                            default:
                                break;
                        }
                    }
                }
            if (filter.pageSize == 0)
                filter.pageSize = 10000;
            query = query.Where(conditions);
            var list = await query
                .OrderByIF(!string.IsNullOrEmpty(filter.orderBy), filter.orderBy)
                .ToPageListAsync(filter.pageNum, filter.pageSize, totalCount);

            return new ServicePageResult<Ghre_ExamPaper>(filter.pageNum, totalCount, filter.pageSize, list);
        }

        public async Task<ServiceResult<DefaultGhre_ExamPaperInput>> QueryDefault(long Id)
        {
            var input = new DefaultGhre_ExamPaperInput();

            #region baseColumns
            var baseColumns = new List<DefaultGhre_ExamPaperColumn>
            {
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "试卷编号",
                    field = "PaperNo",
                    elementType = "Input",
                    required = false,
                    multipleSelect = false,
                    editable = false,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "试卷名称",
                    field = "PaperName",
                    elementType = "Input",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "答题时间",
                    field = "AnswerTime",
                    elementType = "InputNumber",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "评分方式",
                    field = "ScoreMethod",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    dataSource = "api/Common/GetSelect/ScoreMethod?FW=DOTNETCORE"
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "卷面总分",
                    field = "TotalScore",
                    elementType = "InputNumber",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "及格分",
                    field = "PassScore",
                    elementType = "InputNumber",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "可重考次数",
                    field = "RetakeTimes",
                    elementType = "InputNumber",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "试卷说明",
                    field = "RemarkSz",
                    elementType = "InputTextArea",
                    required = false,
                    multipleSelect = false,
                    editable = true,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "关联课程",
                    field = "LinkType",
                    elementType = "CourseRadioBox",
                    required = false,
                    multipleSelect = false,
                    editable = true,
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "课程名称",
                    field = "CourseId",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    dataSource = "api/Common/GetSelect/Ghre_Course?FW=DOTNETCORE"
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "课程场景",
                    field = "CourseSceneId",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    dataSource = "api/Common/GetSelect/Ghre_CourseScene?FW=DOTNETCORE"
                }
            };
            #endregion

            #region randomSetColumns 
            var randomSetColumns = new List<DefaultGhre_ExamPaperColumn>
            {
                 new DefaultGhre_ExamPaperColumn()
                 {
                     label = "课程名称",
                    field = "CourseId",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    displayType=1,
                    dataSource = "api/Common/GetSelect/Ghre_Course?FW=DOTNETCORE"
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "题目类型",
                    field = "QuestionType",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    displayType=2,
                    dataSource = "api/Common/GetSelect/QuestionType?FW=DOTNETCORE"
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "难易程度",
                    field = "DifficultyLevel",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    displayType=2,
                    width=80,
                    dataSource = "api/Common/GetSelect/DifficultyLevel?FW=DOTNETCORE"
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "数量",
                    field = "Quantity",
                    elementType = "InputNumber",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    width=80,
                    displayType=2
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "分值",
                    field = "Score",
                    elementType = "InputNumber",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    width=80,
                    displayType=2
                }
            };
            #endregion

            #region manualSetColumns 
            var manualSetColumns = new List<DefaultGhre_ExamPaperColumn>
            {
                new DefaultGhre_ExamPaperColumn()
                 {
                    label = "课程名称",
                    field = "CourseId",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    displayType=1
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "题目类型",
                    field = "QuestionType",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    displayType= 2
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "题目内容",
                    field = "QuestionContent",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    displayType=2
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "难易程度",
                    field = "DifficultyLevel",
                    elementType = "ApiSelect",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    width=80,
                    displayType=2
                },
                new DefaultGhre_ExamPaperColumn()
                {
                    label = "分值",
                    field = "Score",
                    elementType = "InputNumber",
                    required = true,
                    multipleSelect = false,
                    editable = true,
                    width=80,
                    displayType=2
                }
            };
            #endregion

            #region pageData
            if (Id == 0)
            {
                var baseData = new DefaultGhre_ExamPaperBaseData();
                baseData.PaperNo = null;
                baseData.PaperName = null;
                baseData.AnswerTime = null;
                baseData.ScoreMethod = null;
                baseData.TotalScore = null;
                baseData.PassScore = null;
                baseData.RetakeTimes = null;
                baseData.RemarkSz = null;
                baseData.LinkType = "CourseId";
                baseData.CourseId = null;
                baseData.CourseSceneId = null;
                input.pageData.baseData = baseData;

                input.pageData.examPaperSetType = "random";
            }
            else if (Id != 0)
            {
                var exampaper = await base.QueryById(Id);

                var baseData = new DefaultGhre_ExamPaperBaseData();
                baseData.PaperNo = exampaper.PaperNo;
                baseData.PaperName = exampaper.PaperName;
                baseData.AnswerTime = exampaper.AnswerTime;
                baseData.ScoreMethod = exampaper.ScoreMethod;
                baseData.TotalScore = exampaper.TotalScore;
                baseData.PassScore = exampaper.PassScore;
                baseData.RetakeTimes = exampaper.RetakeTimes;
                baseData.RemarkSz = exampaper.RemarkSz;
                baseData.LinkType = exampaper.LinkType;
                baseData.CourseId = exampaper.LinkId;
                baseData.CourseSceneId = exampaper.LinkId;
                input.pageData.baseData = baseData;
                input.CreateDataInfo = exampaper.CreateDataInfo;
                input.UpdateDataInfo = exampaper.UpdateDataInfo;


                input.pageData.styleInfo.coverImage = exampaper.CoverUrl;
                input.pageData.styleInfo.paperStyle = exampaper.Style;
                input.pageData.styleInfo.coverBackGround = exampaper.CoverBackGround;

                input.pageData.examPaperSetType = exampaper.SetMethod;

                input.pageData.examPaperSetData = await _ghre_ExamPaperConfigServices.Query(x => x.ExamPaperId == Id, "TaxisNo ASC");

                var questions = await _ghre_ExamPaperQuestionServices.Query(x => x.ExamPaperId == Id, "TaxisNo ASC");
                var previews = questions.Where(x => x.QuestionId != null).Select(x => new DefaultGhre_ExamPaperPreview()
                {
                    Id = x.Id,
                    ConfigId = x.ConfigId,
                    parentId = x.ConfigId,
                    QuestionId = x.QuestionId.Value,
                    ExamPaperId = x.ExamPaperId
                }).ToList();

                var questionIds = previews.Select(x => x.QuestionId).Distinct().ToList();
                var questions1 = await _ghre_QuestionServices.Query(x => questionIds.Contains(x.Id));
                var answers = await _ghre_QuestionAnswerServices.Query(x => x.QuestionId != null && questionIds.Contains(x.QuestionId.Value), "TaxisNo ASC");

                previews.ForEach(x =>
                {
                    var answers1 = answers.Where(y => y.QuestionId == x.QuestionId).ToList();
                    x.QuestionType = questions1.FirstOrDefault(a => a.Id == x.QuestionId)?.QuestionType;
                    x.QuestionContent = questions1.FirstOrDefault(a => a.Id == x.QuestionId)?.QuestionContent;
                    if (!string.IsNullOrEmpty(x.QuestionContent))
                        x.QuestionContent = WebUtility.HtmlDecode(x.QuestionContent);

                    //var detail = questions1.Select(o =>
                    //new FromGhre_QuestionQuestionTypeDetail()
                    //{
                    //    Id = o.Id,
                    //    difficulty = o.DifficultyLevel,
                    //    RemarkSz = o.QuestionAnalysis,
                    //    content = o.QuestionContent,
                    //}).ToList();
                    //detail.ForEach(y =>
                    //{
                    //    y.answer = answers1.Where(x => x.IsCorrect == true).FirstOrDefault()?.QuestionNo;
                    //    y.answer1 = answers1.Where(x => x.IsCorrect == true).Select(x => x.QuestionNo).ToList();
                    //    y.answerList = answers.Select(x => new FromGhre_QuestionQuestionAnswerList()
                    //    {
                    //        No = x.QuestionNo,
                    //        label = x.AnswerContent,
                    //        imageUrl = x.ImageUrl,
                    //        imgWidthPc = x.ImageWidthPc,
                    //        imgWidthApp = x.ImageWidthApp,
                    //    }).ToList();
                    //});

                    var detail = new FromGhre_QuestionQuestionTypeDetail();
                    detail.content = x.QuestionContent;
                    detail.difficulty = questions1.FirstOrDefault(a => a.Id == x.QuestionId)?.DifficultyLevel;
                    detail.answer = answers1.Where(x => x.IsCorrect == true).FirstOrDefault()?.QuestionNo;
                    detail.answer1 = answers1.Where(x => x.IsCorrect == true).Select(x => x.QuestionNo).ToList();
                    detail.answerList = answers1.Select(x => new FromGhre_QuestionQuestionAnswerList()
                    {
                        No = x.QuestionNo,
                        label = x.AnswerContent,
                        imageUrl = x.ImageUrl,
                        imgWidthPc = x.ImageWidthPc,
                        imgWidthApp = x.ImageWidthApp,
                    }).ToList();

                    x.detail = detail;
                });

                input.pageData.previewList = previews;
            }
            #endregion

            input.baseColumns = baseColumns;
            input.randomSetColumns = randomSetColumns;
            input.manualSetColumns = manualSetColumns;
            return ServiceResult<DefaultGhre_ExamPaperInput>.OprateSuccess("查询成功！", input);
        }

        public async Task<ServiceResult<InsertExamPaperConfigInput>> InsertConfig(InsertGhre_ExamPaperConfigInput insert)
        {
            var input = new InsertExamPaperConfigInput();
            long parentId = SnowFlakeSingle.instance.getID();
            input.tableData = Mapper.Map(insert).ToANew<Ghre_ExamPaperConfig>();
            input.tableData.Id = parentId;

            if (insert.QuestionId != null)
                insert.Quantity = 1;
            string sql = $@"SELECT TOP {insert.Quantity} *,NEWID() AS GuidValue FROM Ghre_Question WHERE IsEnable=1";
            if (!string.IsNullOrWhiteSpace(insert.DifficultyLevel))
                sql += $" AND DifficultyLevel='{insert.DifficultyLevel}'";
            if (!string.IsNullOrWhiteSpace(insert.QuestionType))
                sql += $" AND QuestionType='{insert.QuestionType}'";
            if (insert.CourseId != null)
                sql += $" AND CourseId='{insert.CourseId}'";
            if (insert.QuestionId != null)
                sql += $" AND Id='{insert.QuestionId}'";
            sql += $" ORDER BY GuidValue ASC";
            var questions = await Db.Ado.SqlQueryAsync<Ghre_Question>(sql);


            var previews = questions.Select(x => new DefaultGhre_ExamPaperPreview()
            {
                Id = x.Id,
                parentId = parentId,
                QuestionId = x.Id,
                QuestionType = x.QuestionType,
                QuestionContent = x.QuestionContent,
            }).ToList();
            var questionIds = previews.Select(x => x.QuestionId).Distinct().ToList();
            var questions1 = await _ghre_QuestionServices.Query(x => questionIds.Contains(x.Id));
            var answers = await _ghre_QuestionAnswerServices.Query(x => x.QuestionId != null && questionIds.Contains(x.QuestionId.Value), "TaxisNo ASC");

            previews.ForEach(x =>
            {
                var answers1 = answers.Where(y => y.QuestionId == x.QuestionId).ToList();
                var detail = new FromGhre_QuestionQuestionTypeDetail();
                detail.answer = answers1.Where(x => x.IsCorrect == true).FirstOrDefault()?.QuestionNo;
                detail.answer1 = answers1.Where(x => x.IsCorrect == true).Select(x => x.QuestionNo).ToList();
                detail.answerList = answers1.Select(x => new FromGhre_QuestionQuestionAnswerList()
                {
                    No = x.QuestionNo,
                    label = x.AnswerContent,
                    imageUrl = x.ImageUrl,
                    imgWidthPc = x.ImageWidthPc,
                    imgWidthApp = x.ImageWidthApp,
                }).ToList();
                if (!string.IsNullOrEmpty(x.QuestionContent))
                    x.QuestionContent = WebUtility.HtmlDecode(x.QuestionContent);
                detail.content = x.QuestionContent;
                x.detail = detail;
            });
            input.previewList = previews;

            return ServiceResult<InsertExamPaperConfigInput>.OprateSuccess("查询成功！", input);
        }

        public async Task<ServiceResult<long>> Insert1(DefaultGhre_ExamPaperPageData insertModel)
        {

            ValidForm(insertModel);

            await Db.Ado.BeginTranAsync();

            try
            {
                var insert = new InsertGhre_ExamPaperInput();

                insert = Mapper.Map(insertModel.baseData).ToANew<InsertGhre_ExamPaperInput>();
                insert.PaperNo = await GenerateContinuousSequence("Ghre_ExamPaper", "PaperNo", "P");
                insert.LinkId = insertModel.baseData.LinkType == "CourseId" ? insertModel.baseData.CourseId : insertModel.baseData.CourseSceneId;
                insert.CoverUrl = insertModel.styleInfo.coverImage;
                insert.CoverBackGround = insertModel.styleInfo.coverBackGround;
                insert.Style = insertModel.styleInfo.paperStyle;
                insert.SetMethod = insertModel.examPaperSetType;
                insert.Status = "Draft";

                var id = await Add(insert);


                var configs = Mapper.Map(insertModel.examPaperSetData).ToANew<List<Ghre_ExamPaperConfig>>();
                configs.ForEach(x => x.ExamPaperId = id);
                var insertConfigs = Db.Insertable(configs);

                string sql = insertConfigs.ToSqlString();
                long row = await Db.Ado.ExecuteCommandAsync(sql);

                var questions = insertModel.previewList
                    .Select(x =>
                    new InsertGhre_ExamPaperQuestionInput
                    {
                        ExamPaperId = id,
                        QuestionId = x.QuestionId,
                        ConfigId = x.parentId,
                    }).ToList();

                questions.ForEach(x =>
                {
                    x.Score = configs.FirstOrDefault(o => o.Id == x.ConfigId)?.Score;
                });

                await _ghre_ExamPaperQuestionServices.Add(questions);

                await Db.Ado.CommitTranAsync();
                return ServiceResult<long>.OprateSuccess("新增成功！", id);
            }
            catch (Exception)
            {
                await Db.Ado.RollbackTranAsync();
                throw;
            }

        }

        public async Task<ServiceResult> Update1(long id, DefaultGhre_ExamPaperPageData insertModel)
        {

            ValidForm(insertModel);

            await Db.Ado.BeginTranAsync();

            try
            {

                Db.Ado.ExecuteCommand($@"delete from Ghre_ExamPaperConfig WHERE ExamPaperId='{id}';
delete from Ghre_ExamPaperQuestion WHERE ExamPaperId='{id}';");

                var insert = new EditGhre_ExamPaperInput();

                insert = Mapper.Map(insertModel.baseData).ToANew<EditGhre_ExamPaperInput>();
                insert.PaperNo = await GenerateContinuousSequence("Ghre_ExamPaper", "PaperNo", "P");
                insert.LinkId = insertModel.baseData.LinkType == "CourseId" ? insertModel.baseData.CourseId : insertModel.baseData.CourseSceneId;
                insert.CoverUrl = insertModel.styleInfo.coverImage;
                insert.CoverBackGround = insertModel.styleInfo.coverBackGround;
                insert.Style = insertModel.styleInfo.paperStyle;
                insert.SetMethod = insertModel.examPaperSetType;
                insert.Status = "Draft";

                await Update(id, insert);
                var configs = Mapper.Map(insertModel.examPaperSetData).ToANew<List<Ghre_ExamPaperConfig>>();
                configs.ForEach(x => x.ExamPaperId = id);
                var insertConfigs = Db.Insertable(configs);

                string sql = insertConfigs.ToSqlString();
                long row = await Db.Ado.ExecuteCommandAsync(sql);

                var questions = insertModel.previewList
                    .Select(x =>
                    new InsertGhre_ExamPaperQuestionInput
                    {
                        ExamPaperId = id,
                        QuestionId = x.QuestionId,
                        ConfigId = x.parentId,
                    }).ToList();
                questions.ForEach(x =>
                {
                    x.Score = configs.FirstOrDefault(o => o.Id == x.ConfigId)?.Score;
                });

                await _ghre_ExamPaperQuestionServices.Add(questions);

                await Db.Ado.CommitTranAsync();
                return ServiceResult.OprateSuccess("修改成功！");
            }
            catch (Exception)
            {
                await Db.Ado.RollbackTranAsync();
                throw;
            }

        }


        public async Task<ServiceResult> UpdateStatus(long[] ids, string status)
        {
            if (status != "Released" && status != "Disabled" && status != "Draft")
                throw new Exception("无效的状态");

            HttpRequest request = UserContext.Context.Request;
            var api = request.Path.ObjToString().TrimEnd('/').ToLower();
            var ip = GetUserIp(UserContext.Context);

            List<Ghre_ExamPaper> entities = new List<Ghre_ExamPaper>();
            foreach (var id in ids)
            {
                if (id == null || !BaseDal.Any(id))
                    continue;

                var entity = await BaseDal.QueryById(id);

                BasePoco ent = entity;
                ent.UpdateIP = ip;
                ent.UpdateProg = api;
                if (status == "Released" || status == "Disabled" || status == "Draft")
                {
                    entity.Status = status;
                    entities.Add(entity);
                }
            }

            var result = await BaseDal.Update(entities);
            if (status == "Released")
                return ServiceResult.OprateSuccess("发布成功！");
            else if (status == "Draft")
                return ServiceResult.OprateSuccess("取消发布成功！");
            else
                return ServiceResult.OprateSuccess("停用成功！");

        }

        public static void ValidForm(DefaultGhre_ExamPaperPageData model)
        {
            if (model.baseData.PassScore == 0)
                throw new Exception("及格分需大于0！");
            if (model.baseData.PassScore >= model.baseData.TotalScore)
                throw new Exception("及格分需小于卷面总分！");

            if (model.baseData.AnswerTime <= 0)
                throw new Exception("答题时间需大于0！");
        }

        #region 获取通用列表下拉
        /// <summary>
        /// 获取通用列表下拉
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<ServiceResult<CommonSelect>> GetSelectAsync(long? linkId)
        {
            var result = new CommonSelect();
            var TableColumn = new JArray();
            var DT_TablePageInfoT1 = new JArray();
            var DT_TableDataT1 = new JArray();
            var searchItem = new JObject();
            JObject item;
            DataTable dt;
            string sql;
            searchItem = [
                new JProperty("defaultHidden", true),
                new JProperty("field","value"),
                new JProperty("label","试卷ID"),
            ];
            TableColumn.Add(searchItem);
            searchItem = [
                new JProperty("field","label"),
                new JProperty("label","试卷名称"),
            ];
            TableColumn.Add(searchItem);

            sql = $"SELECT Id, PaperNo, PaperName FROM Ghre_ExamPaper WHERE LinkId='{linkId}' AND Status ='Released' AND IsEnable=1 ORDER BY CreateTime DESC";
            dt = await Db.Ado.GetDataTableAsync(sql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                item = [
                    new JProperty("value",long.Parse(dt.Rows[i]["Id"].ToString())),
                    new JProperty("label", dt.Rows[i]["PaperName"].ToString() + "("+dt.Rows[i]["PaperNo"].ToString()+")")
                    ];
                DT_TableDataT1.Add(item);
            }

            item = [
                new JProperty("ListMax",100),
                new JProperty("ListMin",10),
                new JProperty("PageNum",1),
                new JProperty("PageSize",99999),
                new JProperty("TotalCount",dt.Rows.Count),
                new JProperty("UIType","Auto")
                ];
            DT_TablePageInfoT1.Add(item);
            result.JM_TableColumnT1.TableColumn = TableColumn;
            result.DT_TableDataT1 = DT_TableDataT1;
            result.DT_TablePageInfoT1 = DT_TablePageInfoT1;
            return new ServiceResult<CommonSelect>() { Success = true, Message = "查询成功", Data = result, };
        }
        #endregion
    }
}