
// 








namespace SafeCampus.Application;

/// <summary>
/// <inheritdoc cref="IPositionService"/>
/// </summary>
public class PositionService : DbRepository<SysPosition>, IPositionService
{
    private readonly ISysUserService _sysUserService;
    private readonly ISysPositionService _sysPositionService;

    public PositionService(ISysUserService sysUserService, ISysPositionService sysPositionService)
    {
        _sysUserService = sysUserService;
        _sysPositionService = sysPositionService;
    }

    #region 查询

    /// <inheritdoc />
    public async Task<SqlSugarPagedList<SysPosition>> Page(PositionPageInput input)
    {
        //获取数据范围
        var dataScope = await _sysUserService.GetLoginUserApiDataScope();
        input.OrgIds = dataScope;
        //分页查询
        var pageInfo = await _sysPositionService.Page(input);
        return pageInfo;
    }

    /// <inheritdoc/>
    public async Task<List<PositionTreeOutput>> Tree(PositionTreeInput input)
    {
        var dataScope = await _sysUserService.GetLoginUserApiDataScope();
        input.OrgIds = dataScope;
        var result = await _sysPositionService.Tree(input);
        return result;
    }

    #endregion

    #region 新增

    /// <inheritdoc />
    public async Task Add(PositionAddInput input)
    {
        await CheckInput(input, SystemConst.ADD);//检查参数
        await _sysPositionService.Add(input, ApplicationConst.BIZ_POS);//添加岗位
    }

    /// <inheritdoc/>
    public async Task<List<PositionSelectorOutput>> Selector(PositionSelectorInput input)
    {
        //获取数据范围
        var dataScope = await _sysUserService.GetLoginUserApiDataScope();
        input.OrgIds = dataScope;//赋值机构列表
        var result = await _sysPositionService.Selector(input);//查询
        return result;
    }

    /// <inheritdoc />
    public async Task<SysPosition> Detail(BaseIdInput input)
    {
        var position = await _sysPositionService.GetSysPositionById(input.Id);
        //判断数据范围
        await _sysUserService.CheckApiDataScope(position.OrgId, position.CreateUserId.GetValueOrDefault(), "您没有权限查看该机构");
        return position;
    }

    #endregion

    #region 编辑

    /// <inheritdoc />
    public async Task Edit(PositionEditInput input)
    {
        await CheckInput(input, SystemConst.EDIT);//检查参数
        await _sysPositionService.Edit(input, ApplicationConst.BIZ_POS);//编辑
    }

    #endregion


    #region 删除

    /// <inheritdoc />
    public async Task Delete(BaseIdListInput input)
    {
        //获取所有ID
        var ids = input.Ids;
        //获取要删除的岗位列表
        var positions = (await _sysPositionService.GetListAsync()).Where(it => ids.Contains(it.Id)).ToList();
        //检查数据范围
        var orgIds = positions.Select(it => it.OrgId).ToList();
        var createUserIds = positions.Select(it => it.CreateUserId.GetValueOrDefault()).ToList();
        await _sysUserService.CheckApiDataScope(orgIds, createUserIds, "您没有权限删除该机构");
        await _sysPositionService.Delete(input, ApplicationConst.BIZ_ORG);//删除岗位
    }

    #endregion

    #region 方法

    /// <summary>
    /// 检查输入参数
    /// </summary>
    /// <param name="sysPosition">参数</param>
    /// <param name="operate">操作名称</param>
    private async Task CheckInput(SysPosition sysPosition, string operate)
    {
        var errorMessage = $"您没有权限在该机构下{operate}岗位";
        //如果id大于0表示编辑
        if (sysPosition.Id > 0)
        {
            var position = await _sysPositionService.GetSysPositionById(sysPosition.Id);//获取机构
            sysPosition.CreateUserId = position.CreateUserId;
        }
        await _sysUserService.CheckApiDataScope(sysPosition.OrgId, sysPosition.CreateUserId.GetValueOrDefault(), errorMessage);
    }

    #endregion 方法
}
