diff --git a/Controllers/WhitListController.cs b/Controllers/WhitListController.cs new file mode 100644 index 0000000..701c27b --- /dev/null +++ b/Controllers/WhitListController.cs @@ -0,0 +1,71 @@ +using LY.App.Model; +using LY.App.Service; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System.Drawing.Printing; + +namespace LY.App.Controllers +{ + /// + /// WhitListController + /// + [Route("api/[controller]")] + [ApiController] + public class WhitListController : ControllerBase + { + private readonly WhitListService _whitListService; + /// + /// Constructor + /// + /// + public WhitListController(WhitListService whitListService) + { + _whitListService = whitListService; + } + /// + /// GetList + /// + /// + /// + /// + /// + [HttpGet("list")] + public async Task List(int pageNum, int pageSize, string key) + { + var result = await _whitListService.GetList(pageNum, pageSize, key); + return Ok(result); + } + /// + /// Add + /// + /// + /// + [HttpPost("add")] + public async Task add(AddWhitelist input) + { + var result = await _whitListService.Add(input); + return Ok(result); + } + /// + /// Delete + /// + /// + /// + [HttpDelete("delete")] + public async Task delete(long id) + { + var result = await _whitListService.Delete(id); + return Ok(result); + } + /// + /// update + /// + /// + [HttpPost("update")] + public async Task Update(UpdateWhitelist input) + { + var result = await _whitListService.Update(input); + return Ok(result); + } + } +} diff --git a/Model/Whitelist.cs b/Model/Whitelist.cs new file mode 100644 index 0000000..bf38c10 --- /dev/null +++ b/Model/Whitelist.cs @@ -0,0 +1,55 @@ +using Newtonsoft.Json; +using SqlSugar; + +namespace LY.App.Model +{ + [SugarTable("ly_white_list")] + public class Whitelist : BaseEntity + { + /// + /// 设备序列号 + /// + public string sn { get; set; } + [SugarColumn(ColumnName = "all_day", ColumnDescription = "是否全天")] + public bool allDay { get; set; } + [SugarColumn(IsNullable = true)] + public DateTime? startTime { get; set; } + [SugarColumn(IsNullable = true)] + public DateTime? endTime { get; set; } + [JsonConverter(typeof(ValueToStringConverter))] + [SugarColumn(ColumnName = "position_id", ColumnDescription = "阵地id")] + public long positionId { get; set; } + public string positionName { get; set; } + } + public class AddWhitelist + { + /// + /// 设备序列号 + /// + public string sn { get; set; } + /// + /// 是否全天 + /// + public bool allDay { get; set; } + /// + /// 开始时间 + /// + public DateTime? startTime { get; set; } + /// + /// 结束时间 + /// + public DateTime? endTime { get; set; } + /// + /// 阵地id + /// + public long positionId { get; set; } + /// + /// 阵地名称 + /// + public string positionName { get; set; } + } + public class UpdateWhitelist : AddWhitelist + { + public long id { get; set; } + } +} diff --git a/Program.cs b/Program.cs index 64a2896..5206bc2 100644 --- a/Program.cs +++ b/Program.cs @@ -85,7 +85,7 @@ builder.Services.AddTransient(sp => }; //ݿͱִһ //db.DbMaintenance.CreateDatabase(); - //db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(UserEntity)); + db.CodeFirst.SetStringDefaultLength(2000).InitTables(typeof(Whitelist)); #endif //д // db.QueryFilter.AddTableFilter(it => it.IsDeleted == false); @@ -125,7 +125,7 @@ app.UseCors("CorsPolicy"); //쳣м app.UseMiddleware(); //token֤м - app.UseMiddleware(); +// app.UseMiddleware(); //ִƥĶ˵ app.UseEndpoints(endpoints => { diff --git a/Service/WhitListService.cs b/Service/WhitListService.cs new file mode 100644 index 0000000..2961c88 --- /dev/null +++ b/Service/WhitListService.cs @@ -0,0 +1,111 @@ +using LY.App.Extensions.DI; +using LY.App.Model; +using Mapster; +using SqlSugar; +using System.Collections.Generic; + +namespace LY.App.Service +{ + /// + /// 业务层白名单服务 + /// + [ServiceInjection(InjectionType.Transient)] + public class WhitListService + { + private readonly SqlSugarClient _db; + /// + /// 构造函数 + /// + /// + public WhitListService(SqlSugarClient sqlSugarClient) + { + _db = sqlSugarClient; + } + /// + /// 新增白名单 + /// + /// + /// + public async Task Add(AddWhitelist input) + { + var result = new ApiResult(); + var entity = input.Adapt(); + var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync(); + result.data = id.ToString(); + return result; + } + /// + /// 删除白名单 + /// + /// + /// + public async Task Delete(long id) + { + var result = new ApiResult(); + var entity = await _db.Queryable().FirstAsync(x => x.Id == id); + if (entity == null) + { + entity.IsDeleted = true; + await _db.Updateable(entity).ExecuteCommandAsync(); + return result; + } + return result; + } + /// + /// 获取白名单列表 + /// + /// + /// + /// + /// + public async Task GetList(int pageNum, int pageSize, string key) + { + var result = new ApiResult(); + RefAsync total = 0; + var items = await _db.Queryable() + .Where(s => s.IsDeleted == false) + .WhereIF(!string.IsNullOrEmpty(key), s => s.sn.Contains(key)) + .OrderByDescending(s => s.Id) + .ToPageListAsync(pageNum, pageSize, total); + return new ApiResult() + { + data = new + { + total = total.Value, + items + } + }; + } + /// + /// 获取白名单详情 + /// + /// + /// + public async Task GetById(long id) + { + var entity = await _db.Queryable().FirstAsync(x => x.Id == id); + if (entity != null) + { + return new ApiResult(true, null); + } + return new ApiResult(false, "未找到该白名单"); + } + /// + /// 更新白名单 + /// + /// + /// + public async Task Update(UpdateWhitelist input) + { + var entity = input.Adapt(); + await _db.Updateable(entity).ExecuteCommandAsync(); + if (entity != null) + { + entity.Adapt(input); + await _db.Updateable(entity).ExecuteCommandAsync(); + return new ApiResult() { code = 0 }; + } + return new ApiResult() { code = 1, msg = "更新失败" }; + } + } +}