如何将当前登录用户的字段过滤到存储过程中

时间:2016-10-24 03:21:42

标签: c# sql-server asp.net-mvc stored-procedures

我有一个存储过程,我得到两个不同的值,一个是真实值,另一个是目标值

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[IndicatorTrainingGaugeMonth]
AS
SET FMTONLY OFF
BEGIN
    SET NOCOUNT ON;

    SELECT
        ' ' AS MonthName,
        gd.Month,
        ISNULL(SUM(gd.Meta), 0) AS Total
    INTO 
        #Meta
    FROM
        [dbo].[Goal] g (NOLOCK)
    INNER JOIN 
        [dbo].[GoalDetail] gd (NOLOCK) ON g.Id = gd.IdGoal
    INNER JOIN 
        [dbo].[GoalClassification] gc (NOLOCK) ON gd.IdClassification = gc.Id
    WHERE 
        gc.Id IN (2, 7)
        AND g.Year = YEAR(GETDATE())
        AND gd.Month = MONTH(GETDATE())
    GROUP BY 
        gd.Month

    SELECT 
        ' ' AS MonthName,
        MONTH(t.InitDate) AS Month,
        ISNULL(SUM(te.Hours), 0) AS Total
    INTO 
        #REAL
    FROM  
        [dbo].[Training] t (NOLOCK)
    INNER JOIN 
        [dbo].[TrainingEmployee] te (NOLOCK) ON t.Id = te.IdTraining
    WHERE 
        YEAR(t.InitDate) = YEAR (GETDATE())
        AND MONTH(t.InitDate) = MONTH(GETDATE())
    GROUP BY 
        t.InitDate

    SELECT 
        --m.MonthName
        m.Month,
        ISNULL(m.Total, 0) AS Meta,
        ISNULL(r.Total, 0) AS Real,
        --COS ((ISNULL(r.Total,0) - 0) / (ISNULL(m.Total,0) - 0) * PI() )AS Value
    FROM 
        #Meta m
    LEFT JOIN 
        #Real ON m.Month = r.Month

    DROP TABLE #Meta
    DROP TABLE #Real
END

我得到两个值例如:Real:20 Meta(Goal):100

但现在我想通过UserBranch过滤这些结果。在我的表AspNetUsers中,我有一个BranchOfficeId的外键,所以每个用户都有一个分支

如何通过他的分支获取currentUserLogged和过滤器?此致

--- ---更新

我在控制器中获得当前登录的用户:

public ActionResult IndicatorTrainingGauge()
{
    string result = string.Empty;
    var userId = User.Identity.GetUserId();

    var branchOfficeId = UserClass.GetUserBranchOfficeId(userId);

    try
    {
        result = new JavaScriptSerializer().Serialize(Dashboard.IndicatorTrainingGauge(branchOfficeId));
    }
    catch (Exception)
    {
        throw;
    }

    return Content(result, "application/json");
}

我上课了:

public static object IndicatorTrainingGauge(int branchOfficeId)
{
    try
    {
        using (var context = new EF.SA())
        {
            var month = context.IndicatorTrainingGaugeMonth().FirstOrDefault();
            var year = context.IndicatorTrainingGaugeYear().FirstOrDefault();

            return new { month, year };
        }
    }
    catch (Exception)
    {
        throw;
    }
}

注意:IndicatorTrainingGaugeMonth是一个存储过程。

那么我现在需要修改哪些来按分支过滤我的商店?此致

0 个答案:

没有答案
相关问题