using DocumentFormat.OpenXml.Spreadsheet; using Flurl.Http.Content; using Infrastructure; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using NetTaste; using NPOI.SS.Formula.Functions; using OpenAuth.App.FormModule; using OpenAuth.App.FormModule.Request; using OpenAuth.App.FormScheme; using OpenAuth.App.FormScheme.Request; using OpenAuth.App.Request; using OpenAuth.Repository.Domain; using Org.BouncyCastle.Asn1.Ocsp; using SqlSugar; using Newtonsoft.Json.Converters; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace OpenAuth.WebApi.Controllers { /// /// 表单发布 /// [Route("api/[controller]/[action]")] [ApiController] public class FormModuleController : ControllerBase { private readonly FormModuleApp _app; private readonly FormSchemeApp _schemeapp; public FormModuleController(FormModuleApp app, FormSchemeApp schemeapp) { _app = app; _schemeapp = schemeapp; } #region 查询数据 /// /// 获取分页列表 /// 2024 03.02 /// /// 筛选条件 /// 模板父级id /// 当前页 /// 每页条数 /// [HttpGet] [AllowAnonymous] public async Task>>> GetPageList(string keyword,string pmoduleid,int page=1,int limit=15) { return await _app.GetModulePageList(keyword, pmoduleid, page, limit); } /// /// 获取列表(单纯获取表单发布信息,不分页) /// 2024.03.06 /// /// [HttpGet] public async Task>> GetList() { return await _app.GetList(); } /// /// 根据主键获取实体数据(编辑时使用) /// /// 主键 /// [HttpGet] public async Task> GetForm(string id) { return await _app.GetForm(id); } /// /// 根据菜单主键获取实体数据 /// /// 菜单主键 /// [HttpGet] public async Task> GetFormByModuleId(string id) { return await _app.GetFormByModuleId(id); } /// /// 根据编号获取实体数据 /// /// 表单发布模板编号 /// [HttpGet] public async Task> GetFormByCode(string code) { return await _app.GetFormByCode(code); } /// /// 获取设计数据 /// /// 编码 /// [HttpGet] public async Task> GetEntityByCode(string code) { return await _app.GetEntityByCode(code); } #endregion #region 增删改 /// /// 新增实体数据 /// 2024 03.05 /// /// 模板数据 /// [HttpPost] public async Task> AddForm(FormModuleReq entity) { var result = new Response(); try { result = await _app.SaveEntity(string.Empty, entity.FormModuleEntity, entity.SysModule, entity.SysModuleElement, entity.SysModuleColumn, entity.SysModuleForm); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 更新实体数据 /// 2024 03.05 /// 主键(表单发布模板id) /// 模板数据 /// [HttpPost] public async Task> UpdateForm(string id, FormModuleReq entity) { var result = new Response(); try { result = await _app.SaveEntity(id, entity.FormModuleEntity, entity.SysModule, entity.SysModuleElement, entity.SysModuleColumn, entity.SysModuleForm); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 更新状态 /// 2024 03.05 /// /// 表单发布模板主键 /// 状态(发布、未发布) /// [HttpPost] public async Task> UpdateModuleState(string id, int state) { var result = new Response(); try { result = await _app.UpdateState(id,state); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 根据菜单主键id删除实体数据 /// /// 菜单id(Sysmodule的主键) /// [HttpPost] public async Task> DeleteFormByModuleId(string moduleId) { var result = new Response(); try { result = await _app.DeleteEntityByModuleId(moduleId); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } /// /// 删除实体数据(表单发布模板id) /// /// 表单发布主键(FormModule的主键) /// [HttpPost] public async Task> DeleteForm(string keyValue) { var result = new Response(); try { result = await _app.DeleteForm(keyValue); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } #endregion #region 扩展方法 /// /// 判断数据表字段重复 /// /// 主键值 /// 表名 /// 主键名 /// 数据字段 /// 是否关闭开启 /// [HttpGet] public async Task> ExistFiled(string keyValue, string tableName, string keyName, string filedsJson) { var result = new Response(); try { result = await _app.ExistFiled(keyValue, tableName, keyName, filedsJson); } catch (Exception ex) { result.Code = 500; result.Message = ex.InnerException?.Message ?? ex.Message; } return result; } #endregion #region 获取数据库表 /// /// 获取数据库表 /// /// 数据库编码 /// [HttpGet] [AllowAnonymous] public Response> GetTableList(string code,string key) { return _app.GetTableList(code,key); } #endregion #region 获取导出数据 /// /// 获取导出数据 /// /// 功能id /// id数据 /// 查询参数 /// 编号 /// [HttpPost] public async Task Export(string mid, string id, string code, FormQueryReq query) { var res = new Response(); //获取列表数据 var result = await _schemeapp.GetFormDataNewPage(mid, query.Ldparam, id, null, query.QueryJson); //获取模板数据 var module = await _app.GetEntityByCode(code); //数据处理 var data = result.Result.Items; var header = module.Result.Entity.Scheme; var obj = JsonConvert.DeserializeObject(header); List cl = new List(); foreach (var item in obj["table"]["columns"]) { ModuleColumn c = new ModuleColumn(); c.value = item["label"].ToString(); c.key = item["key"].ToString(); cl.Add(c); } var excelRes = _app.ListToExcel(data, cl); if (excelRes.Code == 200) { return File(excelRes.Result.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "数据导出" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); } else { res.Code = excelRes.Code; res.Message = "导出失败"; } return Ok(res); } #endregion } }