AuthScopeAppService.cs 2.26 KB
using FoodLabeling.Application.Contracts.Dtos.AuthScope;
using FoodLabeling.Application.Contracts.IServices;
using FoodLabeling.Application.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Yi.Framework.SqlSugarCore.Abstractions;

namespace FoodLabeling.Application.Services;

/// <summary>
/// 登录后 Company → Region → Location 级联选店(Web 管理端)。
/// </summary>
[Authorize]
public class AuthScopeAppService : ApplicationService, IAuthScopeAppService
{
    private readonly ISqlSugarDbContext _dbContext;
    private readonly IDistributedCache<AdminWorkingScopeCacheItem, AdminWorkingScopeCacheKey> _workingScopeCache;

    public AuthScopeAppService(
        ISqlSugarDbContext dbContext,
        IDistributedCache<AdminWorkingScopeCacheItem, AdminWorkingScopeCacheKey> workingScopeCache)
    {
        _dbContext = dbContext;
        _workingScopeCache = workingScopeCache;
    }

    /// <inheritdoc />
    [HttpGet("auth-scope/companies")]
    public virtual Task<List<AuthScopeCompanyOptionDto>> GetCompaniesAsync() =>
        AuthScopeQueryHelper.GetCompaniesAsync(CurrentUser, _dbContext);

    /// <inheritdoc />
    [HttpGet("auth-scope/regions")]
    public virtual Task<List<AuthScopeRegionOptionDto>> GetRegionsAsync([FromQuery] string partnerId) =>
        AuthScopeQueryHelper.GetRegionsAsync(CurrentUser, _dbContext, partnerId);

    /// <inheritdoc />
    [HttpGet("auth-scope/locations")]
    public virtual Task<List<AuthScopeLocationOptionDto>> GetLocationsAsync(
        [FromQuery] string partnerId,
        [FromQuery] string groupId) =>
        AuthScopeQueryHelper.GetLocationsAsync(CurrentUser, _dbContext, partnerId, groupId);

    /// <inheritdoc />
    [HttpPost("auth-scope/select-location")]
    public virtual Task<AuthScopeSelectLocationOutputDto> SelectLocationAsync(AuthScopeSelectLocationInputVo input) =>
        AuthScopeQueryHelper.SelectLocationAsync(CurrentUser, _dbContext, _workingScopeCache, input);

    /// <inheritdoc />
    [HttpGet("auth-scope/current-scope")]
    public virtual Task<AuthScopeSelectLocationOutputDto?> GetCurrentScopeAsync() =>
        AuthScopeQueryHelper.GetCurrentScopeAsync(CurrentUser, _dbContext, _workingScopeCache);
}