| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- namespace Admin.NET.Plugin.AiDOP.Service.S8;
- public class S8DataSourceService : ITransient
- {
- private readonly SqlSugarRepository<AdoS8DataSource> _rep;
- public S8DataSourceService(SqlSugarRepository<AdoS8DataSource> rep) => _rep = rep;
- public async Task<List<AdoS8DataSource>> ListAsync(long tenantId, long factoryId) =>
- await _rep.AsQueryable()
- .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
- .ToListAsync();
- public async Task<AdoS8DataSource> 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<AdoS8DataSource> 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<object> 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
- };
- }
- }
|