using Furion; using Furion.DependencyInjection; using Furion.DynamicApiController; using Furion.FriendlyException; using GDZZ.Core; using GDZZ.FlowCenter.Entity; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace GDZZ.FlowCenter.Service; /// /// 表单管理 /// [ApiDescriptionSettings("FlowCenter", Name = "FormManage", Order = 100)] public class FlcFormManageService : IFlcFormManageService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _flcFormRep; public FlcFormManageService(SqlSugarRepository flcFormRep) { _flcFormRep = flcFormRep; } /// /// 分页查询表单 /// /// /// [HttpGet("/flcForm/page")] public async Task QueryFormPageList([FromQuery] PageFlcFormInput input) { var orgs = await _flcFormRep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.Id), u => u.Id == long.Parse(input.Id.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.OrgId), u => u.OrgId == long.Parse(input.OrgId) || u.OrgId == null || u.OrgId == 0) .Where(u => u.Status != CommonStatus.DELETED) .OrderBy(u => u.Sort) .Select() .ToPagedListAsync(input.PageNo, input.PageSize); return orgs.XnPagedResult(); } /// /// 获取表单列表 /// /// /// [HttpGet("/flcForm/list")] public async Task> GetFormList([FromQuery] FlcFormInput input) { var dataScopeList = await DataFilterExtensions.GetDataScopeIdList(FilterType.Org); var forms = await _flcFormRep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.OrgId), u => u.OrgId == long.Parse(input.OrgId) || u.OrgId == null || u.OrgId == 0) .WhereIF(dataScopeList.Any(), u => dataScopeList.Contains(u.OrgId??0)|| u.OrgId == null || u.OrgId == 0) .Where(u => u.Status == CommonStatus.ENABLE) .OrderBy(u => u.Sort) .ToListAsync(); return forms.Adapt>(); } /// /// 增加表单 /// /// /// [HttpPost("/flcForm/add")] public async Task AddForm(AddFlcFormInput input) { var isExist = await _flcFormRep.AnyAsync(u => u.Name == input.Name); if (isExist) throw Oops.Oh(ErrorCode.D2002); var flcForm = input.Adapt(); flcForm.Status = CommonStatus.ENABLE; //反射获取这个表单的所有参数和备注,排除主键 if (flcForm.FrmType == FormType.CUSTOMFORM) { var dataname = input.WebId.Substring(0, 1).ToUpper() + input.WebId.Substring(1); var t = App.Assemblies .SelectMany(a => a.GetTypes().Where(t => t.FullName.Contains("GDZZ.FlowCenter.Entity") && t.FullName.Contains("." + dataname))).First(); List list = new List(); List parses = new List(); dynamic obj = Activator.CreateInstance(t); foreach (PropertyInfo info in obj.GetType().GetProperties()) { if (info.Name != "Id") { list.Add(info.Name); parses.Add(new { id = info.Name, name = info.Name }); } } flcForm.ContentData = string.Join(',', list); flcForm.Fields = list.Count(); flcForm.ContentParse = parses.ToJsonString(); } await _flcFormRep.InsertAsync(flcForm); } /// /// 删除表单 /// /// /// [HttpPost("/flcForm/delete")] public async Task DeleteForm(DeleteFlcFormInput input) { var flcForm = await _flcFormRep.FirstOrDefaultAsync(u => u.Id == long.Parse(input.Id)); (flcForm.CreatedUserId ?? 0).CheckDataScope(); //假删除 await _flcFormRep.Change().UpdateAsync(u => u.Id == long.Parse(input.Id),a => new FlcForm { Status = CommonStatus.DELETED, IsDeleted = true, }); } /// /// 更新表单 /// /// /// [HttpPost("/flcForm/edit")] public async Task UpdateForm(UpdateFlcFormInput input) { if (input.Id != "0" && !string.IsNullOrEmpty(input.Id)) { var org = await _flcFormRep.FirstOrDefaultAsync(u => u.Id == long.Parse(input.Id)); _ = org ?? throw Oops.Oh(ErrorCode.D2000); } var flcForm = await _flcFormRep.FirstOrDefaultAsync(u => u.Id == long.Parse(input.Id)); // 检测数据范围能不能操作这个表单 (flcForm.CreatedUserId ?? 0).CheckDataScope(); var isExist = await _flcFormRep.AnyAsync(u => (u.Name == input.Name) && u.Id != flcForm.Id); if (isExist) throw Oops.Oh(ErrorCode.D2002); flcForm = input.Adapt(); //反射获取这个表单的所有参数和备注,排除主键 if (flcForm.FrmType == FormType.CUSTOMFORM) { var dataname = input.WebId.Substring(0, 1).ToUpper() + input.WebId.Substring(1); var t = App.Assemblies .SelectMany(a => a.GetTypes().Where(t => t.FullName.Contains("GDZZ.FlowCenter.Entity") && t.FullName.Contains("." + dataname))).First(); List list = new List(); List parses = new List(); dynamic obj = Activator.CreateInstance(t); foreach (PropertyInfo info in obj.GetType().GetProperties()) { if (info.Name != "Id") { list.Add(info.Name); parses.Add(new { id = info.Name, name = info.Name }); } } flcForm.ContentData = string.Join(',', list); flcForm.Fields = list.Count(); flcForm.ContentParse = parses.ToJsonString(); } await _flcFormRep.AsUpdateable(flcForm).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 获取表单信息 /// /// /// [HttpGet("/flcForm/detail")] public async Task GetForm([FromQuery] QueryFlcFormInput input) { return await _flcFormRep.FirstOrDefaultAsync(u => u.Id == long.Parse(input.Id)); } }