using Tiobon.Core.IServices; using Tiobon.Core.Model.Models; using Tiobon.Core.Services.BASE; using Tiobon.Core.IRepository.Base; using Tiobon.Core.Model; using System.Dynamic; using Newtonsoft.Json.Linq; using AgileObjects.AgileMapper; using Mysqlx.Crud; using AgileObjects.AgileMapper.Extensions; using System.Data; using Tiobon.Core.Common; using Tiobon.Core.Common.DB.Dapper; using MySqlX.XDevAPI.Common; namespace Tiobon.Core.Services { /// /// 题目 (服务) /// public class Ghre_QuestionServices : BaseServices, IGhre_QuestionServices { private readonly IBaseRepository _dal; private IGhre_QuestionAnswerServices _ghre_QuestionAnswerServices; public Ghre_QuestionServices(IBaseRepository dal, IGhre_QuestionAnswerServices ghre_QuestionAnswerServices) { this._dal = dal; base.BaseDal = dal; _ghre_QuestionAnswerServices = ghre_QuestionAnswerServices; } /// /// /// /// /// public async Task> QueryFrom(long Id) { var data = new FromGhre_QuestionInput(); #region Column data.Column.Add(new FromGhre_QuestionColumn() { label = "课程名称", field = "courseID", elementType = "ApiSelect", required = true, multipleSelect = true, editable = true, dataSource = "api/Common/GetSelect?type=Ghre_Course" }); data.Column.Add(new FromGhre_QuestionColumn() { label = "题目编号", field = "questionNo", elementType = "Input", required = false, multipleSelect = false, editable = false, dataSource = "", placeholder = "保存后自动生成" }); #endregion #region PageData data.PageData.questionType = new List { new FromGhre_QuestionQuestionType() { label = "单选题", type = "Single", isActive = 1 }, new FromGhre_QuestionQuestionType() { label = "多选题", type = "Multiple", isActive = 0, }, new FromGhre_QuestionQuestionType() { label = "判断题", type = "TrueOrFalse", isActive = 0, }, new FromGhre_QuestionQuestionType() { label = "填空题", type = "Completion", isActive = 0, }, new FromGhre_QuestionQuestionType() { label = "简答题", type = "ShortAnswer", isActive = 0, } }; if (Id != 0) { var question = await base.QueryById(Id); data.PageData.questionType.ForEach(x => x.isActive = 0); data.PageData.baseData.questionNo = question.QuestionNo; if (!string.IsNullOrEmpty(question.CourseIds)) { var list = question.CourseIds.Split(';'); list.ForEach(x => { try { data.PageData.baseData.courseID.Add(long.Parse(x)); } catch (Exception) { } }); } var questionType = data.PageData.questionType.Where(x => x.type == question.QuestionType).FirstOrDefault(); if (questionType != null) { var answers = await _ghre_QuestionAnswerServices.Query(x => x.QuestionId == Id, "TaxisNo ASC"); questionType.isActive = 1; questionType.detail.difficulty = question.DifficultyLevel; questionType.detail.answer = answers.Where(x => x.IsCorrect == true).FirstOrDefault()?.QuestionNo; questionType.detail.answer1 = answers.Where(x => x.IsCorrect == true).Select(x => x.QuestionNo).ToList(); questionType.detail.answer1 = answers.Where(x => x.IsCorrect == true).Select(x => x.QuestionNo).ToList(); questionType.detail.content = question.QuestionContent; questionType.detail.RemarkSz = question.QuestionAnalysis; questionType.detail.answerList = answers.Select(x => new FromGhre_QuestionQuestionAnswerList() { No = x.QuestionNo, label = x.AnswerContent, imageUrl = x.ImageUrl, imgWidthPc = x.ImageWidthPc, imgWidthApp = x.ImageWidthApp, }).ToList(); } } #endregion return ServiceResult.OprateSuccess("查询成功!", data); } public async Task> InsertFrom(FromGhre_QuestionPageData insertModel) { var insert = new InsertGhre_QuestionInput(); insert.CourseId = insertModel.baseData.courseID; insert.CourseIds = string.Join(";", insert.CourseId.Select(x => x)); var questionType = insertModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); insert.QuestionType = questionType.type; insert.DifficultyLevel = questionType.detail.difficulty; insert.QuestionContent = questionType.detail.content; insert.QuestionAnalysis = questionType.detail.RemarkSz; insert.QuestionNo = GenerateContinuousSequence(insert.QuestionType.Substring(0, 1)); var id = await base.Add(insert); var insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() { QuestionNo = x.No, AnswerContent = x.label, ImageUrl = x.imageUrl, ImageWidthPc = x.imgWidthPc, ImageWidthApp = x.imgWidthApp, }).ToList(); int i = 100; insertAnswers.ForEach(x => { x.TaxisNo = i; x.QuestionId = id; if (questionType.detail.answer == x.QuestionNo && insert.QuestionType == "Single") x.IsCorrect = true; i = i + 100; }); await _ghre_QuestionAnswerServices.Add(insertAnswers); return ServiceResult.OprateSuccess("查询成功!", id); } public async Task UpdareFrom(long Id, FromGhre_QuestionPageData editModel) { var data = await base.QueryById(Id); await _ghre_QuestionAnswerServices.Delete(x => x.QuestionId == Id); var edit = Mapper.Map(data).ToANew(); edit.CourseId = editModel.baseData.courseID; edit.CourseIds = string.Join(";", edit.CourseId.Select(x => x)); edit.QuestionNo = ""; var questionType = editModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); edit.QuestionType = questionType.type; edit.DifficultyLevel = questionType.detail.difficulty; edit.QuestionContent = questionType.detail.content; edit.QuestionAnalysis = questionType.detail.RemarkSz; await base.Update(Id, edit); var insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() { QuestionNo = x.No, AnswerContent = x.label, ImageUrl = x.imageUrl, ImageWidthPc = x.imgWidthPc, ImageWidthApp = x.imgWidthApp, }).ToList(); int i = 100; insertAnswers.ForEach(x => { x.TaxisNo = i; x.QuestionId = Id; if (questionType.detail.answer == x.QuestionNo && edit.QuestionType == "Single") x.IsCorrect = true; i = i + 100; }); await _ghre_QuestionAnswerServices.Add(insertAnswers); return ServiceResult.OprateSuccess("更新成功!"); } public static string GenerateContinuousSequence(string prefixTemp) { try { string result = string.Empty; string tableCode = "Ghre_Question"; string columnCode = "QuestionNo"; int length = 6; int tempLength = 5; int sequence; #region 查询 DbSelect dbSelect = new DbSelect(tableCode + " A", "A", null); dbSelect.IsInitDefaultValue = false; if (!string.IsNullOrEmpty(prefixTemp)) dbSelect.Select("MAX(SUBSTRING(A." + columnCode + "," + (prefixTemp.Length + 1).ToString() + "," + tempLength.ToString() + "))"); else dbSelect.Select("MAX(A." + columnCode + ")"); //} //dbSelect.Select("MAX(CONVERT(DECIMAL,SUBSTRING(A.ISSUE_NO," + (prefix.Length + dateString.Length + 1).ToString() + "," + tempLength.ToString() + ")))"); if (!string.IsNullOrEmpty(prefixTemp)) dbSelect.Where("SUBSTRING(A." + columnCode + ",1," + (prefixTemp.Length).ToString() + ")", " = ", prefixTemp); dbSelect.Where("LEN(A." + columnCode + ")", "=", length); string sql = dbSelect.GetSql(); string maxSequence = Convert.ToString(DbAccess.Instance.ExecuteScalar(sql)); #endregion //tempLength = tempLength - dateString.Length; if (string.IsNullOrEmpty(maxSequence)) result = prefixTemp + Convert.ToString(1).PadLeft(tempLength, '0'); else { if (!string.IsNullOrEmpty(prefixTemp)) { if (int.TryParse(maxSequence, out sequence)) { sequence += 1; if (sequence.ToString().Length > tempLength) throw new Exception("自动生成字串长度已经超过设定长度!"); } else throw new Exception("表中的数据无法进行自动编号,请联系软件开发商!"); result = prefixTemp + sequence.ToString().PadLeft(tempLength, '0'); } else { if (int.TryParse(maxSequence, out sequence)) { sequence += 1; if (sequence.ToString().Length > length) throw new Exception("自动生成字串长度已经超过设定长度!"); } else throw new Exception("表中的数据无法进行自动编号,请联系软件开发商!"); result = sequence.ToString().PadLeft(length, '0'); } } return result; } catch (Exception) { throw; } } } }