using Admin.NET.Plugin.AiDOP.Entity.S8; namespace Admin.NET.Plugin.AiDOP.Service.S8; public class S8DataSourceService : ITransient { private readonly SqlSugarRepository _rep; public S8DataSourceService(SqlSugarRepository rep) => _rep = rep; public async Task> ListAsync(long tenantId, long factoryId) => await _rep.AsQueryable() .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId) .ToListAsync(); public async Task CreateAsync(AdoS8DataSource body) { if (string.IsNullOrWhiteSpace(body.DataSourceCode) || string.IsNullOrWhiteSpace(body.Type)) throw new S8BizException("数据源编码和类型必填"); var exists = await _rep.AsQueryable() .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.DataSourceCode == body.DataSourceCode); if (exists) throw new S8BizException("数据源编码已存在"); body.Id = 0; body.CreatedAt = DateTime.Now; await _rep.InsertAsync(body); return body; } public async Task UpdateAsync(long id, AdoS8DataSource body) { var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在"); if (string.IsNullOrWhiteSpace(body.DataSourceCode) || string.IsNullOrWhiteSpace(body.Type)) throw new S8BizException("数据源编码和类型必填"); var exists = await _rep.AsQueryable() .AnyAsync(x => x.Id != id && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.DataSourceCode == body.DataSourceCode); if (exists) throw new S8BizException("数据源编码已存在"); body.Id = id; body.CreatedAt = e.CreatedAt; body.UpdatedAt = DateTime.Now; await _rep.UpdateAsync(body); return body; } public async Task DeleteAsync(long id) => await _rep.DeleteByIdAsync(id); public async Task TestAsync(long id) { var entity = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在"); var success = !string.IsNullOrWhiteSpace(entity.Endpoint); entity.LastCheckAt = DateTime.Now; entity.LastCheckStatus = success ? "SUCCESS" : "FAILED: endpoint is empty"; entity.UpdatedAt = DateTime.Now; await _rep.UpdateAsync(entity); return new { id, success, message = success ? "连接信息校验通过" : "连接地址为空,未通过校验", entity.LastCheckAt, entity.LastCheckStatus }; } }