操控平台后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
9.5 KiB

5 months ago
using Admin.NET.Core.Service;
using @(@Model.NameSpace).Const;
using @(@Model.NameSpace).Entity;
using Microsoft.AspNetCore.Http;
@{
string joinTableName = "u";
Dictionary<string, int> definedObjects = new Dictionary<string, int>();
bool haveLikeCdt = false;
foreach (var column in Model.TableField){
if (column.QueryWhether == "Y" && column.QueryType == "like"){
haveLikeCdt = true;
}
}
}
namespace @Model.NameSpace;
/// <summary>
/// @(@Model.BusName)服务
/// </summary>
[ApiDescriptionSettings(@(@Model.ProjectLastName)Const.GroupName, Order = 100)]
public class @(@Model.ClassName)Service : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<@(@Model.ClassName)> _rep;
public @(@Model.ClassName)Service(SqlSugarRepository<@(@Model.ClassName)> rep)
{
_rep = rep;
}
/// <summary>
/// 分页查询@(@Model.BusName)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<@(@Model.ClassName)Output>> Page(@(@Model.ClassName)Input input)
{
var query = _rep.AsQueryable()
@{string conditionFlag = "";}
@if (haveLikeCdt) {
@:.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
@foreach (var column in Model.TableField){
if (@column.QueryWhether == "Y" && column.QueryType == "like"){
@:@(conditionFlag)u.@(@column.PropertyName).Contains(input.SearchKey.Trim())
conditionFlag="|| ";
}
}
@:)
}
@foreach (var column in Model.TableField){
if (@column.QueryWhether == "Y"){
if (@column.NetType?.TrimEnd('?') == "string"){
if(@column.QueryType == "like"){
@:.WhereIF(!string.IsNullOrWhiteSpace(input.@column.PropertyName), u => u.@(@column.PropertyName).Contains(input.@(@column.PropertyName).Trim()))
}else{
@:.WhereIF(!string.IsNullOrWhiteSpace(input.@column.PropertyName), u => u.@(@column.PropertyName) @column.QueryType input.@(@column.PropertyName))
}
}else if(@column.NetType?.TrimEnd('?') == "int" || @column.NetType?.TrimEnd('?') == "long"){
@:.WhereIF(input.@column.PropertyName>0, u => u.@(@column.PropertyName) @column.QueryType input.@(@column.PropertyName))
}else if(@column.NetType?.TrimEnd('?') == "DateTime" && @column.QueryType == "~"){
}else if(@column.NetType?.TrimEnd('?').EndsWith("Enum") == true) {
@:.WhereIF(input.@(@column.PropertyName).HasValue, u => u.@(@column.PropertyName) @column.QueryType input.@(@column.PropertyName))
}
}
}
@if(Model.IsJoinTable){
@://处理外键和TreeSelector相关字段的连接
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk"){
joinTableName += ", " + column.PropertyName.ToLower();
@:.LeftJoin<@(@column.FkEntityName)>((@(@joinTableName)) => u.@(@column.PropertyName) == @(@column.PropertyName.ToLower()).Id )
} else if(@column.EffectType == "ApiTreeSelect"){
joinTableName += ", " + column.PropertyName.ToLower();
@:.LeftJoin<@(@column.FkEntityName)>((@(@joinTableName)) => u.@(@column.PropertyName) == @(@column.PropertyName.ToLower()).@(@column.ValueColumn) )
}
}
@:.OrderBy(u => u.CreateTime)
@:.Select((@(@joinTableName)) => new @(@Model.ClassName)Output
@:{
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk"){
@:@(@column.PropertyName) = u.@(@column.PropertyName),
@:@(@column.PropertyName)@(@column.FkColumnName) = @(@column.PropertyName.ToLower()).@(@column.FkColumnName),
} else if(@column.EffectType == "ApiTreeSelect"){
@:@(@column.PropertyName) = u.@(@column.PropertyName),
@:@(@column.PropertyName)@(@column.DisplayColumn) = @(@column.PropertyName.ToLower()).@(@column.DisplayColumn),
} else if(@column.NetType?.TrimEnd('?').EndsWith("Enum") == true){
@:@(@column.PropertyName) = (@(@column.NetType))u.@(@column.PropertyName),
} else {
@:@(@column.PropertyName) = u.@(@column.PropertyName),
}
}
@:});
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk"){
}else if(@column.EffectType == "Upload"){
@://.Mapper(c => c.@(@column.PropertyName)Attachment, c => c.@(@column.PropertyName))
}
}
} else {
@:.Select<@(@Model.ClassName)Output>();
}
@foreach (var column in Model.TableField){
if (@column.QueryWhether == "Y"){
if(@column.NetType?.TrimEnd('?') == "DateTime" && @column.QueryType == "~"){
@:if(input.@(@column.PropertyName)Range != null && input.@(@column.PropertyName)Range.Count >0)
@:{
@:DateTime? start= input.@(@column.PropertyName)Range[0];
@:query = query.WhereIF(start.HasValue, u => u.@column.PropertyName > start);
@:if (input.@(@column.PropertyName)Range.Count >1 && input.@(@column.PropertyName)Range[1].HasValue)
@:{
@:var end = input.@(@column.PropertyName)Range[1].Value.AddDays(1);
@:query = query.Where(u => u.@(@column.PropertyName) < end);
@:}
@:}
}
}
}
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
/// <summary>
/// 增加@(@Model.BusName)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(Add@(@Model.ClassName)Input input)
{
var entity = input.Adapt<@(@Model.ClassName)>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除@(@Model.BusName)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(Delete@(@Model.ClassName)Input input)
{
@foreach (var column in Model.TableField){
if (@column.ColumnKey == "True"){
@:var entity = await _rep.GetFirstAsync(u => u.@(@column.PropertyName) == input.@(@column.PropertyName)) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
}
}
await _rep.FakeDeleteAsync(entity); //假删除
//await _rep.DeleteAsync(entity); //真删除
}
/// <summary>
/// 更新@(@Model.BusName)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(Update@(@Model.ClassName)Input input)
{
var entity = input.Adapt<@(@Model.ClassName)>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取@(@Model.BusName)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<@(@Model.ClassName)> Detail([FromQuery] QueryById@(@Model.ClassName)Input input)
{
@foreach (var column in Model.TableField){
if (@column.ColumnKey == "True"){
@:return await _rep.GetFirstAsync(u => u.@(@column.PropertyName) == input.@(@column.PropertyName));
}
}
}
/// <summary>
/// 获取@(@Model.BusName)列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<@(@Model.ClassName)Output>> List([FromQuery] @(@Model.ClassName)Input input)
{
return await _rep.AsQueryable().Select<@(@Model.ClassName)Output>().ToListAsync();
}
@foreach (var column in Model.TableField){
if(@column.EffectType == "fk" && (@column.WhetherAddUpdate == "Y" || column.QueryWhether == "Y")){
@:/// <summary>
@:/// 获取@(@column.ColumnComment)列表
@:/// </summary>
@:/// <param name="input"></param>
@:/// <returns></returns>
@:[ApiDescriptionSettings(Name = "@(@column.FkEntityName)@(@column.PropertyName)Dropdown"), HttpGet]
@:public async Task<dynamic> @(@column.FkEntityName)@(@column.PropertyName)Dropdown()
@:{
@:return await _rep.Context.Queryable<@(@column.FkEntityName)>()
@:.Select(u => new
@:{
@:Label = u.@(@column.FkColumnName),
@:Value = u.Id
@:}
@:).ToListAsync();
@:}
}
}
@foreach (var column in Model.TableField){
if(@column.EffectType == "Upload"){
@:/// <summary>
@:/// 上传@(@column.ColumnComment)
@:/// </summary>
@:/// <param name="file"></param>
@:/// <returns></returns>
@:[ApiDescriptionSettings(Name = "Upload@(@column.PropertyName)"), HttpPost]
@:public async Task<SysFile> Upload@(@column.PropertyName)([Required] IFormFile file)
@:{
@:var service = App.GetService<SysFileService>();
@:return await service.UploadFile(file, "upload/@(@column.PropertyName)");
@:}
}
}
@foreach (var column in Model.TableField){
if(@column.EffectType == "ApiTreeSelect" && !definedObjects.ContainsKey("@(@column.FkEntityName)Tree")){
@{definedObjects.Add("@(@column.FkEntityName)Tree", 1);}
@:[HttpGet("@(@Model.LowerClassName)/@(@column.FkEntityName)Tree")]
@:public async Task<dynamic> @(@column.FkEntityName)Tree()
@:{
@:return await _rep.Context.Queryable<@(@column.FkEntityName)>().ToTreeAsync(u => u.Children, u => u.@(@column.PidColumn), 0);
@:}
}
}
}