⚡ 优化代码生成删除操作
This commit is contained in:
parent
925440716b
commit
bfe8b68a94
@ -1,51 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace ZR.Infrastructure.ModelBinder
|
|
||||||
{
|
|
||||||
public class CommaSeparatedArrayModelBinder<T> : IModelBinder
|
|
||||||
{
|
|
||||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
|
||||||
{
|
|
||||||
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
|
||||||
if (valueProviderResult == ValueProviderResult.None)
|
|
||||||
{
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
var value = valueProviderResult.FirstValue;
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var array = value.Split(',').Select(x => (T)Convert.ChangeType(x, typeof(T))).ToArray();
|
|
||||||
bindingContext.Result = ModelBindingResult.Success(array);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "Invalid value format");
|
|
||||||
}
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class CommaSeparatedArrayModelBinderProvider<T> : IModelBinderProvider
|
|
||||||
{
|
|
||||||
public IModelBinder? GetBinder(ModelBinderProviderContext context)
|
|
||||||
{
|
|
||||||
if (context.Metadata.ModelType == typeof(T[]))
|
|
||||||
{
|
|
||||||
return new BinderTypeModelBinder(typeof(CommaSeparatedArrayModelBinder<T>));
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -9,4 +9,3 @@ global using Infrastructure.Extensions;
|
|||||||
global using Infrastructure.Controllers;
|
global using Infrastructure.Controllers;
|
||||||
global using ZR.ServiceCore.Middleware;
|
global using ZR.ServiceCore.Middleware;
|
||||||
global using ZR.ServiceCore.Services;
|
global using ZR.ServiceCore.Services;
|
||||||
global using ZR.Infrastructure.ModelBinder;
|
|
||||||
@ -16,12 +16,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
builder.Host.UseNLog();
|
builder.Host.UseNLog();
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddControllers(options =>
|
builder.Services.AddControllers();
|
||||||
{
|
|
||||||
options.ModelBinderProviders.Insert(0, new CommaSeparatedArrayModelBinderProvider<string>());
|
|
||||||
options.ModelBinderProviders.Insert(0, new CommaSeparatedArrayModelBinderProvider<int>());
|
|
||||||
options.ModelBinderProviders.Insert(0, new CommaSeparatedArrayModelBinderProvider<long>());
|
|
||||||
});
|
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|||||||
@ -86,8 +86,8 @@ $if(replaceDto.ShowBtnAdd)
|
|||||||
|
|
||||||
return SUCCESS(response);
|
return SUCCESS(response);
|
||||||
}
|
}
|
||||||
$end
|
|
||||||
|
|
||||||
|
$end
|
||||||
$if(replaceDto.ShowBtnEdit)
|
$if(replaceDto.ShowBtnEdit)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新${genTable.FunctionName}
|
/// 更新${genTable.FunctionName}
|
||||||
@ -103,24 +103,24 @@ $if(replaceDto.ShowBtnEdit)
|
|||||||
|
|
||||||
return ToResponse(response);
|
return ToResponse(response);
|
||||||
}
|
}
|
||||||
$end
|
|
||||||
|
|
||||||
|
$end
|
||||||
$if(replaceDto.ShowBtnDelete || replaceDto.ShowBtnMultiDel)
|
$if(replaceDto.ShowBtnDelete || replaceDto.ShowBtnMultiDel)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除${genTable.FunctionName}
|
/// 删除${genTable.FunctionName}
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpDelete("{ids}")]
|
[HttpDelete("delete/{ids}")]
|
||||||
[ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:delete")]
|
[ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:delete")]
|
||||||
[Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.DELETE)]
|
[Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.DELETE)]
|
||||||
public IActionResult Delete${replaceDto.ModelTypeName}([ModelBinder(typeof(CommaSeparatedArrayModelBinder<${replaceDto.PKType}>))] ${replaceDto.PKType}[] ids)
|
public IActionResult Delete${replaceDto.ModelTypeName}([FromRoute]string ids)
|
||||||
{
|
{
|
||||||
if (ids.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
|
var idArr = Tools.SplitAndConvert<${replaceDto.PKType}>(ids);
|
||||||
|
|
||||||
return ToResponse(_${replaceDto.ModelTypeName}Service.Delete(ids$if(replaceDto.enableLog), "删除${genTable.FunctionName}"$end));
|
return ToResponse(_${replaceDto.ModelTypeName}Service.Delete(idArr$if(replaceDto.enableLog), "删除${genTable.FunctionName}"$end));
|
||||||
}
|
}
|
||||||
$end
|
|
||||||
|
|
||||||
|
$end
|
||||||
$if(replaceDto.ShowBtnExport)
|
$if(replaceDto.ShowBtnExport)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 导出${genTable.FunctionName}
|
/// 导出${genTable.FunctionName}
|
||||||
@ -141,8 +141,8 @@ $if(replaceDto.ShowBtnExport)
|
|||||||
var result = ExportExcelMini(list, "${genTable.FunctionName}", "${genTable.FunctionName}");
|
var result = ExportExcelMini(list, "${genTable.FunctionName}", "${genTable.FunctionName}");
|
||||||
return ExportExcel(result.Item2, result.Item1);
|
return ExportExcel(result.Item2, result.Item1);
|
||||||
}
|
}
|
||||||
$end
|
|
||||||
|
|
||||||
|
$end
|
||||||
$if(replaceDto.ShowBtnTruncate)
|
$if(replaceDto.ShowBtnTruncate)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 清空${genTable.FunctionName}
|
/// 清空${genTable.FunctionName}
|
||||||
@ -159,8 +159,8 @@ $if(replaceDto.ShowBtnTruncate)
|
|||||||
}
|
}
|
||||||
return SUCCESS(_${replaceDto.ModelTypeName}Service.Truncate${replaceDto.ModelTypeName}());
|
return SUCCESS(_${replaceDto.ModelTypeName}Service.Truncate${replaceDto.ModelTypeName}());
|
||||||
}
|
}
|
||||||
$end
|
|
||||||
|
|
||||||
|
$end
|
||||||
$if(replaceDto.ShowBtnImport)
|
$if(replaceDto.ShowBtnImport)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 导入
|
/// 导入
|
||||||
@ -193,8 +193,8 @@ $if(replaceDto.ShowBtnImport)
|
|||||||
var result = DownloadImportTemplate(new List<${replaceDto.ModelTypeName}Dto>() { }, "${replaceDto.ModelTypeName}");
|
var result = DownloadImportTemplate(new List<${replaceDto.ModelTypeName}Dto>() { }, "${replaceDto.ModelTypeName}");
|
||||||
return ExportExcel(result.Item2, result.Item1);
|
return ExportExcel(result.Item2, result.Item1);
|
||||||
}
|
}
|
||||||
$end
|
|
||||||
|
|
||||||
|
$end
|
||||||
$if(showCustomInput)
|
$if(showCustomInput)
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 保存排序
|
/// 保存排序
|
||||||
|
|||||||
@ -80,7 +80,7 @@ $if(replaceDto.ShowBtnDelete || replaceDto.ShowBtnMultiDel)
|
|||||||
*/
|
*/
|
||||||
export function del${genTable.BusinessName}(pid) {
|
export function del${genTable.BusinessName}(pid) {
|
||||||
return request({
|
return request({
|
||||||
url: '${genTable.ModuleName}/${genTable.BusinessName}/' + pid,
|
url: '${genTable.ModuleName}/${genTable.BusinessName}/delete/' + pid,
|
||||||
method: 'delete'
|
method: 'delete'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,6 +29,18 @@ namespace ZR.Common
|
|||||||
int[] infoIdss = Array.ConvertAll(strIds, s => int.Parse(s));
|
int[] infoIdss = Array.ConvertAll(strIds, s => int.Parse(s));
|
||||||
return infoIdss;
|
return infoIdss;
|
||||||
}
|
}
|
||||||
|
public static T[] SplitAndConvert<T>(string input, char split = ',')
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(input)) { return Array.Empty<T>(); }
|
||||||
|
string[] parts = input.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
T[] result = Array.ConvertAll(parts, s => (T)Convert.ChangeType(s, typeof(T)));
|
||||||
|
//for (int i = 0; i < parts.Length; i++)
|
||||||
|
//{
|
||||||
|
// result[i] = (T)Convert.ChangeType(parts[i], typeof(T));
|
||||||
|
//}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据日期获取星期几
|
/// 根据日期获取星期几
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user