S8DataSourceService.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Admin.NET.Plugin.AiDOP.Entity.S8;
  2. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  3. public class S8DataSourceService : ITransient
  4. {
  5. private readonly SqlSugarRepository<AdoS8DataSource> _rep;
  6. public S8DataSourceService(SqlSugarRepository<AdoS8DataSource> rep) => _rep = rep;
  7. public async Task<List<AdoS8DataSource>> ListAsync(long tenantId, long factoryId) =>
  8. await _rep.AsQueryable()
  9. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  10. .ToListAsync();
  11. public async Task<AdoS8DataSource> CreateAsync(AdoS8DataSource body)
  12. {
  13. if (string.IsNullOrWhiteSpace(body.DataSourceCode) || string.IsNullOrWhiteSpace(body.Type))
  14. throw new S8BizException("数据源编码和类型必填");
  15. var exists = await _rep.AsQueryable()
  16. .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.DataSourceCode == body.DataSourceCode);
  17. if (exists) throw new S8BizException("数据源编码已存在");
  18. body.Id = 0;
  19. body.CreatedAt = DateTime.Now;
  20. await _rep.InsertAsync(body);
  21. return body;
  22. }
  23. public async Task<AdoS8DataSource> UpdateAsync(long id, AdoS8DataSource body)
  24. {
  25. var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
  26. if (string.IsNullOrWhiteSpace(body.DataSourceCode) || string.IsNullOrWhiteSpace(body.Type))
  27. throw new S8BizException("数据源编码和类型必填");
  28. var exists = await _rep.AsQueryable()
  29. .AnyAsync(x => x.Id != id && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.DataSourceCode == body.DataSourceCode);
  30. if (exists) throw new S8BizException("数据源编码已存在");
  31. body.Id = id;
  32. body.CreatedAt = e.CreatedAt;
  33. body.UpdatedAt = DateTime.Now;
  34. await _rep.UpdateAsync(body);
  35. return body;
  36. }
  37. public async Task DeleteAsync(long id) => await _rep.DeleteByIdAsync(id);
  38. public async Task<object> TestAsync(long id)
  39. {
  40. var entity = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
  41. var success = !string.IsNullOrWhiteSpace(entity.Endpoint);
  42. entity.LastCheckAt = DateTime.Now;
  43. entity.LastCheckStatus = success ? "SUCCESS" : "FAILED: endpoint is empty";
  44. entity.UpdatedAt = DateTime.Now;
  45. await _rep.UpdateAsync(entity);
  46. return new
  47. {
  48. id,
  49. success,
  50. message = success ? "连接信息校验通过" : "连接地址为空,未通过校验",
  51. entity.LastCheckAt,
  52. entity.LastCheckStatus
  53. };
  54. }
  55. }