Implement API security baseline (sys_user, JWT, audit fields, transactions, unique constraints) and Web real login with 401 handling. Document setup and development workflows in root, WmsLite.Api, and WmsLite.Web README files. Co-authored-by: Cursor <cursoragent@cursor.com>
97 lines
3.3 KiB
C#
97 lines
3.3 KiB
C#
using WmsLite.Api.Common;
|
|
using WmsLite.Api.Dtos.Template;
|
|
using WmsLite.Api.Infrastructure.Auth;
|
|
using WmsLite.Api.Models;
|
|
using WmsLite.Api.Repositories.Interfaces;
|
|
using WmsLite.Api.Services.Interfaces;
|
|
|
|
namespace WmsLite.Api.Services;
|
|
|
|
public class SysTemplateService : ISysTemplateService
|
|
{
|
|
private readonly ISysTemplateRepository _repository;
|
|
private readonly ICurrentUserContext _userContext;
|
|
|
|
public SysTemplateService(ISysTemplateRepository repository, ICurrentUserContext userContext)
|
|
{
|
|
_repository = repository;
|
|
_userContext = userContext;
|
|
}
|
|
|
|
public async Task<PagedResult<SysTemplateViewDto>> GetPagedListAsync(SysTemplatePagedQuery query)
|
|
{
|
|
query.TenantId = _userContext.TenantId.Trim();
|
|
query.OwnerCode = _userContext.OwnerCode.Trim();
|
|
var page = await _repository.GetPagedListAsync(query);
|
|
return new PagedResult<SysTemplateViewDto>
|
|
{
|
|
PageIndex = page.PageIndex,
|
|
PageSize = page.PageSize,
|
|
TotalCount = page.TotalCount,
|
|
Items = page.Items.ConvertAll(Map)
|
|
};
|
|
}
|
|
|
|
public async Task<SysTemplateViewDto> CreateAsync(SysTemplateSaveRequest request)
|
|
{
|
|
TemplateConfigJsonValidator.ValidateAndParse(request.ConfigJson);
|
|
var bizType = request.BizType.Trim();
|
|
if (await _repository.GetByBizTypeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), bizType) is not null)
|
|
{
|
|
throw new BusinessException($"Template '{bizType}' already exists.");
|
|
}
|
|
|
|
var entity = new SysTemplate
|
|
{
|
|
TenantId = _userContext.TenantId.Trim(),
|
|
OwnerCode = _userContext.OwnerCode.Trim(),
|
|
BizType = bizType,
|
|
BizName = request.BizName.Trim(),
|
|
ConfigJson = request.ConfigJson
|
|
};
|
|
await _repository.InsertAsync(entity);
|
|
return Map(entity);
|
|
}
|
|
|
|
public async Task<SysTemplateViewDto?> UpdateAsync(SysTemplateUpdateRequest request)
|
|
{
|
|
TemplateConfigJsonValidator.ValidateAndParse(request.ConfigJson);
|
|
var entity = await _repository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
if (entity is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var bizType = request.BizType.Trim();
|
|
var duplicate = await _repository.GetByBizTypeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), bizType);
|
|
if (duplicate is not null && duplicate.Id != entity.Id)
|
|
{
|
|
throw new BusinessException($"Template '{bizType}' already exists.");
|
|
}
|
|
|
|
entity.BizType = bizType;
|
|
entity.BizName = request.BizName.Trim();
|
|
entity.ConfigJson = request.ConfigJson;
|
|
await _repository.UpdateAsync(entity);
|
|
return Map(entity);
|
|
}
|
|
|
|
public Task<int> DeleteAsync(long id, string tenantId, string ownerCode)
|
|
{
|
|
return _repository.SoftDeleteAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
}
|
|
|
|
private static SysTemplateViewDto Map(SysTemplate entity)
|
|
{
|
|
return new SysTemplateViewDto
|
|
{
|
|
Id = entity.Id,
|
|
TenantId = entity.TenantId,
|
|
OwnerCode = entity.OwnerCode,
|
|
BizType = entity.BizType,
|
|
BizName = entity.BizName,
|
|
ConfigJson = entity.ConfigJson
|
|
};
|
|
}
|
|
}
|