using System.Drawing; using System.Dynamic; using Mysqlx.Crud; namespace Tiobon.Core.Api.Controllers; /// /// 题目(Controller) /// [Route("api/[controller]")] [ApiController, GlobalActionFilter] [Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Ghre)] public class Ghre_QuestionController : BaseController { IGhre_QuestionAnswerServices _ghre_QuestionAnswerServices; public Ghre_QuestionController(IGhre_QuestionServices service, IGhre_QuestionAnswerServices ghre_QuestionAnswerServices) : base(service) { _ghre_QuestionAnswerServices = ghre_QuestionAnswerServices; } #region 基础接口 #region 查询 /// /// 根据Id查询数据 /// /// 主键ID /// [HttpPost("Query/{Id}")] public override async Task> QueryById(long Id) { var entity = await _service.QueryById(Id); entity.Answers = await _ghre_QuestionAnswerServices.Query(x => x.QuestionId == Id, "TaxisNo ASC"); if (entity is null) return Failed("获取失败", 500); else return Success(entity, "获取成功"); } /// /// 根据Id查询数据 /// /// /// [HttpPost("QueryFrom/{Id}")] public async Task> QueryFrom(long Id) { return await _service.QueryFrom(Id); } #endregion #region 新增 /// /// 新增数据 /// /// /// [HttpPost("Insert")] public override async Task> Insert([FromBody] InsertGhre_QuestionInput insertModel) { var id = await _service.Add(insertModel); var answers = insertModel.Answers; if (answers.Any()) { int i = 100; answers.ForEach(x => { x.TaxisNo = i; x.QuestionId = id; }); i = i + 100; await _ghre_QuestionAnswerServices.Add(answers); } var data = Success(null, "新增成功"); data.Success = id > 0; if (data.Success) data.Data = id.ObjToString(); else return Failed("新增失败"); return data; } [HttpPost("InsertFrom")] public async Task InsertFrom([FromBody] FromGhre_QuestionPageData insertModel) { return await _service.InsertFrom(insertModel); } #endregion #region 更新 /// /// 更新数据 /// /// 主键ID /// /// [HttpPost("Update/{Id}")] public override async Task Put(long Id, [FromBody] EditGhre_QuestionInput editModel) { await _ghre_QuestionAnswerServices.Delete(x => x.QuestionId == Id); var answers = editModel.Answers; if (answers.Any()) { int i = 100; answers.ForEach(x => { x.TaxisNo = i; x.QuestionId = Id; }); i = i + 100; await _ghre_QuestionAnswerServices.Add(answers); } return await base.Put(Id, editModel); } [HttpPost("UpdareFrom/{Id}")] public async Task UpdareFrom(long Id, [FromBody] FromGhre_QuestionPageData insertModel) { return await _service.UpdareFrom(Id, insertModel); } #endregion #endregion }