using @(@Model.NameSpace).Const; namespace @Model.NameSpace; /// /// @(@Model.BusName)服务 /// [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; } /// /// 分页查询@(@Model.BusName) /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Page")] public async Task> Page(@(@Model.ClassName)Input input) { var query= _rep.AsQueryable() @foreach (var column in Model.TableField){ if (@column.QueryWhether == "Y"){ if (@column.NetType?.TrimEnd('?') == "string"){ if(@column.QueryType == "like"){ @:.WhereIF(!string.IsNullOrWhiteSpace(input.@column.ColumnName), u => u.@(@column.ColumnName).Contains(input.@(@column.ColumnName).Trim())) }else{ @:.WhereIF(!string.IsNullOrWhiteSpace(input.@column.ColumnName), u => u.@(@column.ColumnName) @column.QueryType input.@(@column.ColumnName)) } }else if(@column.NetType?.TrimEnd('?') == "int" || @column.NetType?.TrimEnd('?') == "long"){ @:.WhereIF(input.@column.ColumnName>0, u => u.@(@column.ColumnName) @column.QueryType input.@(@column.ColumnName)) }else if(@column.NetType?.TrimEnd('?') == "DateTime" && @column.QueryType == "~"){ } } } @if(Model.IsJoinTable){ @:.Select(u=> new @(@Model.ClassName)Output{ @foreach (var column in Model.TableField){ @:@(@column.ColumnName) = u.@(@column.ColumnName), if(@column.EffectType == "fk"){ @:@(@column.FkEntityName)@(@column.FkColumnName) = u.@(@column.FkEntityName).@(@column.FkColumnName), } } @:}) @foreach (var column in Model.TableField){ if(@column.EffectType == "fk"){ }else if(@column.EffectType == "Upload"){ @://.Mapper(c => c.@(@column.ColumnName)Attachment, c => c.@(@column.ColumnName)) } } } 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.ColumnName)Range != null && input.@(@column.ColumnName)Range.Count >0) @:{ @:DateTime? start= input.@(@column.ColumnName)Range[0]; @:query = query.WhereIF(start.HasValue, u => u.@column.ColumnName > start); @:if (input.@(@column.ColumnName)Range.Count >1 && input.@(@column.ColumnName)Range[1].HasValue) @:{ @:var end = input.@(@column.ColumnName)Range[1].Value.AddDays(1); @:query = query.Where(u => u.@(@column.ColumnName) < end); @:} @:} } } } query = query.OrderBuilder(input); return await query.ToPagedListAsync(input.Page, input.PageSize); } /// /// 增加@(@Model.BusName) /// /// /// [HttpPost] [ApiDescriptionSettings(Name = "Add")] public async Task Add(Add@(@Model.ClassName)Input input) { var entity = input.Adapt<@(@Model.ClassName)>(); await _rep.InsertAsync(entity); } /// /// 删除@(@Model.BusName) /// /// /// [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.ColumnName) == input.@(@column.ColumnName)) ?? throw Oops.Oh(ErrorCodeEnum.D1002); } } await _rep.FakeDeleteAsync(entity); //假删除 } /// /// 更新@(@Model.BusName) /// /// /// [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(); } /// /// 获取@(@Model.BusName) /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "Detail")] public async Task<@(@Model.ClassName)> Get([FromQuery] QueryById@(@Model.ClassName)Input input) { @foreach (var column in Model.TableField){ if (@column.ColumnKey == "True"){ @:return await _rep.GetFirstAsync(u => u.@(@column.ColumnName) == input.@(@column.ColumnName)); } } } /// /// 获取@(@Model.BusName)列表 /// /// /// [HttpGet] [ApiDescriptionSettings(Name = "List")] public async Task> 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.ColumnComment)列表 @:/// @:/// @:/// @:[ApiDescriptionSettings(Name = "@(@column.FkEntityName)Dropdown"), HttpGet] @:public async Task @(@column.FkEntityName)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"){ @:/// @:/// 上传@(@column.ColumnComment) @:/// @:/// @:/// @:[ApiDescriptionSettings(Name = "Upload@(@column.ColumnName)"), HttpPost] @:public async Task Upload@(@column.ColumnName)([Required] IFormFile file) @:{ @:var service = App.GetService(); @:return await service.UploadFile(file, "upload/@(@column.ColumnName)"); @:} } } @foreach (var column in Model.TableField){ if(@column.EffectType == "ApiTreeSelect"){ @:[HttpGet("/@(@Model.LowerClassName)/@(@column.FkEntityName)Tree")] @:public async Task @(@column.FkEntityName)Tree() @:{ @:return await _rep.Context.Queryable<@(@column.FkEntityName)TreeOutput>().ToTreeAsync(u => u.Children, u => u.@(@column.PidColumn), 0); @:} } } }