using JNPF.Common.Enums; using JNPF.Common.Extension; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.VisualData.Entity; using JNPF.VisualData.Entitys.Dto.ScreenDataSource; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Yitter.IdGenerator; namespace JNPF.VisualData; /// /// 业务实现:大屏. /// [ApiDescriptionSettings(Tag = "BladeVisual", Name = "db", Order = 160)] [Route("api/blade-visual/[controller]")] public class ScreenDataSourceService : IDynamicApiController, ITransient { /// /// 客户端初始化. /// public ISqlSugarClient _sqlSugarClient; /// /// 服务基础仓储. /// private readonly ISqlSugarRepository _visualDBRepository; /// /// 初始化一个类型的新实例. /// public ScreenDataSourceService( ISqlSugarRepository visualDBRepository, ISqlSugarClient context) { _visualDBRepository = visualDBRepository; _sqlSugarClient = context; } #region Get /// /// 分页. /// /// [HttpGet("list")] public async Task GetList([FromQuery] ScreenDataSourceListQueryInput input) { SqlSugarPagedList? data = await _visualDBRepository.AsQueryable().Where(v => v.IsDeleted == 0) .Select(v => new ScreenDataSourceListOutput { id = v.Id, name = v.Name, driverClass = v.DriverClass, password = v.Password, remark = v.Remark, url = v.Url, username = v.UserName, isDeleted = v.IsDeleted }) .ToPagedListAsync(input.current, input.size); return new { current = data.pagination.CurrentPage, pages = data.pagination.Total / data.pagination.PageSize, records = data.list, size = data.pagination.PageSize, total = data.pagination.Total }; } /// /// 详情. /// /// [HttpGet("detail")] public async Task GetInfo(string id) { VisualDBEntity? entity = await _visualDBRepository.AsQueryable().FirstAsync(v => v.Id == id && v.IsDeleted == 0); return entity.Adapt(); } /// /// 下拉数据源. /// /// [HttpGet("db-list")] public async Task GetDBList() { return await _visualDBRepository.AsQueryable().Select(v => new ScreenDataSourceSeleectOutput { id = v.Id, name = v.Name, driverClass = v.DriverClass }).ToListAsync(); } #endregion #region Post /// /// 新增与修改. /// /// [HttpPost("submit")] public async Task Submit([FromBody] ScreenDataSourceUpInput input) { VisualDBEntity? entity = input.Adapt(); int isOk = 0; if (input.id == null) isOk = await _visualDBRepository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Create()).ExecuteCommandAsync(); else isOk = await _visualDBRepository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1000); } /// /// 新增. /// /// /// [HttpPost("save")] public async Task Create([FromBody] ScreenDataSourceCrInput input) { VisualDBEntity? entity = input.Adapt(); int isOk = await _visualDBRepository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Create()).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1000); } /// /// 修改. /// /// /// [HttpPost("update")] public async Task Update([FromBody] ScreenDataSourceUpInput input) { VisualDBEntity? entity = input.Adapt(); int isOk = await _visualDBRepository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1001); } /// /// 删除. /// /// [HttpPost("remove")] public async Task Delete(string ids) { VisualDBEntity? entity = await _visualDBRepository.AsQueryable().FirstAsync(v => v.Id == ids && v.IsDeleted == 0); _ = entity ?? throw Oops.Oh(ErrorCode.COM1005); int isOk = await _visualDBRepository.AsUpdateable(entity).CallEntityMethod(m => m.Delete()).UpdateColumns(it => new { it.IsDeleted }).ExecuteCommandAsync(); if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1002); } /// /// 数据源测试连接. /// /// [HttpPost("db-test")] public dynamic Test([FromBody] ScreenDataSourceUpInput input) { if (input.id == null) input.id = YitIdHelper.NextId().ToString(); _sqlSugarClient.AsTenant().AddConnection(new ConnectionConfig() { ConfigId = input.id, DbType = ToDbTytpe(input.driverClass), ConnectionString = ToConnectionString(input.driverClass, input.url, input.username, input.password), InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true }); _sqlSugarClient = _sqlSugarClient.AsTenant().GetConnectionScope(input.id); if (_sqlSugarClient.Ado.IsValidConnection()) return Task.FromResult(true); else throw Oops.Oh(ErrorCode.D1507); } /// /// 动态执行SQL. /// /// [HttpPost("dynamic-query")] public async Task Query([FromBody] ScreenDataSourceDynamicQueryInput input) { if (input.sql.IsNullOrEmpty()) throw Oops.Oh(ErrorCode.D1513); if (!string.IsNullOrWhiteSpace(input.id)) { VisualDBEntity? entity = await _visualDBRepository.AsQueryable().FirstAsync(v => v.Id == input.id && v.IsDeleted == 0); _sqlSugarClient.AsTenant().AddConnection(new ConnectionConfig() { ConfigId = input.id, DbType = ToDbTytpe(entity.DriverClass), ConnectionString = ToConnectionString(entity.DriverClass, entity.Url, entity.UserName, entity.Password), InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true }); _sqlSugarClient = _sqlSugarClient.AsTenant().GetConnectionScope(input.id); try { return await _sqlSugarClient.Ado.GetDataTableAsync(input.sql); } catch (Exception ex) { throw Oops.Oh(ErrorCode.D1512, ex.Message); } } return Task.FromResult(true); } #endregion #region PrivateMethod /// /// 转换数据库类型. /// /// /// private SqlSugar.DbType ToDbTytpe(string dbType) { switch (dbType) { case "org.postgresql.Driver": return SqlSugar.DbType.PostgreSQL; case "com.mysql.cj.jdbc.Driver": return SqlSugar.DbType.MySql; case "oracle.jdbc.OracleDriver": return SqlSugar.DbType.Oracle; case "com.microsoft.sqlserver.jdbc.SQLServerDriver": return SqlSugar.DbType.SqlServer; default: throw Oops.Oh(ErrorCode.D1505); } } /// /// 转换连接字符串. /// /// private string ToConnectionString(string driverClass, string url, string name, string password) { switch (driverClass) { case "org.postgresql.Driver": return string.Format(url, name, password); case "com.mysql.cj.jdbc.Driver": return string.Format(url, name, password); case "oracle.jdbc.OracleDriver": return string.Format(url, name, password); case "com.microsoft.sqlserver.jdbc.SQLServerDriver": return string.Format(url, name, password); default: throw Oops.Oh(ErrorCode.D1505); } } #endregion }