using System.Data; using Infrastructure; using Infrastructure.Helpers.Excel; using Infrastructure.Helpers.Excel.Model; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using OpenAuth.App.FormScheme; using OpenAuth.App.FormScheme.Request; using OpenAuth.App.FormScheme.Response; using OpenAuth.App.Request; using OpenAuth.Repository.Domain; using SqlSugar; namespace OpenAuth.WebApi.Controllers { /// /// 日 期: 2024-03-02 /// 描 述: 表单设计 /// [Route("api/[controller]/[action]")] [ApiController] public class FormSchemeController : ControllerBase { private readonly FormSchemeApp _app; public FormSchemeController(FormSchemeApp app) { _app = app; } /// /// 查询表单分页信息 custmerform/scheme/page /// /// 关键字 /// 分类 /// 状态1启用0未启用2草稿3全部 /// /// /// /// [HttpGet] public async Task>>> LoadFormPage(string keyWord, string category, int page, int limit, int isEnabled = 3) { return await _app.LoadFormPage(keyWord, category, page, limit, isEnabled); } /// /// 获取表单数据 /// /// /// [HttpGet] public async Task> LoadFormScheme(string schemeId) { try { return new Response() { Result = await _app.GetScheme(schemeId), Code = 200, Message = "获取数据成功" }; } catch (Exception e) { return new Response() { Result = null, Code = 500, Message = "获取数据失败" }; } } /// /// 新增自定义表单custmerform/scheme /// /// 模板数据 /// [HttpPost] public async Task> AddForm(FormSchemeReq entity) { var result = new Response(); try { result = await _app.SaveEntity(string.Empty, entity.Info, entity.Scheme); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 获取自定义表单设计数据custmerform/scheme/{id} /// /// id /// [HttpGet] public async Task> GetForm(string id) { try { FormSchemeReq data = new FormSchemeReq(); data.Info = await _app.GetSchemeInfo(id); if (data.Info != null) { data.Scheme = await _app.GetScheme(data.Info.SchemeId); } return new Response { Result = data }; } catch (Exception ex) { return new Response { Code = 500, Message = "获取数据失败", }; } } /// /// 获取自定义表单基础信息custmerform/scheme/info/{id} /// /// id /// [HttpGet] public async Task> GetSchemeInfoEntity(string id) { try { var data = await _app.GetSchemeInfo(id); return new Response { Result = data }; } catch (Exception ex) { return new Response { Code = 500, Message = "获取数据失败", }; } } /// /// 更新自定义表单custmerform/scheme/{id} /// /// id /// 模板数据 /// [HttpPost] public async Task> UpdateForm(string id, FormSchemeReq entity) { var result = new Response(); try { await _app.SaveSchEntity(id, entity.Info, entity.Scheme); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 更新表单模板版本 custmerform/scheme/history/{id} /// /// 信息主键 /// 模板主键 /// [HttpPost] public async Task> UpdateScheme(string id, string schemeId) { var result = new Response(); try { result = await _app.UpdateScheme(id, schemeId); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 启用/停用表单custmerform/scheme/state/{id} /// /// 主键 /// 状态1启用0禁用 /// [HttpPost] public async Task> UpdateState(string id, int state) { var result = new Response(); try { result = await _app.UpdateState(id, state); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 删除自定义表单custmerform/scheme/{id} /// /// id /// [HttpPost] public async Task> DeleteForm(string id) { var result = new Response(); try { result = await _app.Delete(id); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 获取数据源列名custmerform/sql/colnames/{dbCode} /// /// 数据源编码 /// [HttpPost] public async Task>> GetDataColName(string dbCode, string sql) { var result = new Response>(); try { result.Result = await _app.GetDataColName(dbCode, sql); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return result; } /// /// 获取自定义表单预览 /// /// id /// [HttpGet] public async Task> GetPreviewForm(string keyValue) { try { var scheme = await _app.GetScheme(keyValue); return new Response { Result = scheme }; } catch (Exception ex) { return new Response { Code = 500, Message = "获取数据失败" }; } } /// /// 获取自定义表单模板历史数据custmerform/scheme/history/page/{id} /// /// 分页参数 /// 自定义表单信息主键 /// [HttpGet] public async Task>>> GetSchemePageList([FromQuery] PageReq pagination, string schemeInfoId) { try { return await _app.GetSchemePageList(pagination, schemeInfoId); } catch (Exception ex) { return new Response>> { Code = 500, Message = "获取数据失败" }; } } /// /// 获取表单分页数据custmerform/data/page/{mid}/{id} /// /// 分页参数 /// 功能id /// id数据 /// 查询参数 /// 桌面跳转参数 /// [HttpPost] public async Task>> GetFormDataPage(string mid, string id, FormQueryReq query) { try { //return await _app.GetFormDataPage(mid, query.Ldparam, id, query.PaginationInputDto, query.QueryJson); return await _app.GetFormDataNewPage(mid, query.Ldparam, id, query.PaginationInputDto, query.QueryJson); } catch (Exception ex) { return new Response> { Code = 500, Message = ex.Message.ToString() }; } } /// /// 获取表单列表数据custmerform/datas/{mid}/{id} /// /// 功能id /// id数据 /// 查询参数 /// 排序字段 /// 桌面跳转参数 /// [HttpPost] public async Task> GetFormDataList(string mid, string id, FormQueryReq query) { try { return new Response { Result = await _app.GetFormDataList(mid, id, query.QueryJson, query.Sidx) }; } catch (Exception ex) { return new Response { Code = 500, Message = "获取数据失败" }; } } /// /// 获取表单数据 "custmerform/data/{id}" /// /// 模板id /// 条件字段 /// 条件值 /// [HttpGet] public async Task>> GetFormData(string id, string key, string keyValue) { var response = new Response>(); try { response.Result = await _app.GetFormDataNew(id, key, keyValue); return response; } catch (Exception ex) { response.Code = 500; response.Message = ex.Message.ToString(); return response; } } /// /// 保存自定义表单数据 custmerform/data /// /// 提交参数 /// [HttpPost] public async Task> SaveForm(FormDataReq dto) { var result = new Response(); try { result = await _app.SaveFormDataNew(dto); } catch (Exception ex) { result.Code = 500; result.Message = ex.Message.ToString(); } return result; } /// /// 删除表单数据 /// /// /// /// /// [HttpPost] public async Task> DeleteFormData(string id, string key, string keyValue) { var result = new Response(); try { result = await _app.DeleteFormData(id, key, keyValue); } catch (Exception ex) { result.Code = 500; result.Message = "提交失败"; } return result; } #region 导入 /// /// 导入数据 /// /// /// /// [HttpPost] public async Task> ImportExcel(string id, string pkey, string path) { var result = new Response(); try { // 获取excel文件数据(暂时只支持单sheet导入,后续升级) DataTable dt = ExcelHelper.ExcelImport(path); var res2 = await _app.ImportTable(id, dt, pkey); var data = new { Success = res2.snum, Fail = res2.fnum, data = res2.elist }; result.Result = data; return result; } catch (Exception ex) { result.Result = false; result.Message = ex.Message.ToString(); return result; } } /// /// 下载模板 /// /// 文件id /// [HttpGet] public async Task DownTemplateFile(string id) { //解析表单 List list = new List(); Repository.Domain.FormScheme schemeEntity = await _app.GetScheme(id); FormSchemeNewModel formSchemeModel = schemeEntity.Scheme.ToObject(); FormDbTableInfo mainTable = formSchemeModel.Db.Find(t => t.Type == "main"); foreach (var tab in formSchemeModel.FormInfo.TabList) { foreach (var component in tab.Schemas) { if (component == null || component.ComponentProps == null || string.IsNullOrEmpty(component.ComponentProps.DataTable)) { continue; } ExcelImportFileds excelImportFileds = new ExcelImportFileds(); excelImportFileds.ColName = component.Label; excelImportFileds.Name = component.ComponentProps.FieldName; excelImportFileds.CsType = component.CsType; excelImportFileds.RelationType = 0; list.Add(excelImportFileds); } } //设置导出格式 ExcelConfig excelconfig = new ExcelConfig(); excelconfig.FileName = "数据模板.xls"; excelconfig.IsAllSizeColumn = true; excelconfig.ColumnEntity = new List(); //表头 DataTable dt = new DataTable(); foreach (var col in list) { if (col.RelationType != 1 && col.RelationType != 4 && col.RelationType != 5 && col.RelationType != 6 && col.RelationType != 7) { excelconfig.ColumnEntity.Add(new ColumnModel() { Column = col.Name, ExcelColumn = col.ColName, Alignment = "center", }); dt.Columns.Add(col.Name, typeof(string)); } } return File(ExcelHelper.ExportMemoryStream(dt, excelconfig), "application/ms-excel", excelconfig.FileName); } #endregion } }