Commit 006e5edd by lcx

确定graphql参数内容

parent 3c3c9d97
using GraphQL.Types; using Dapper;
using GraphQL.Types;
using Performance.DtoModels;
using Performance.Infrastructure; using Performance.Infrastructure;
using Performance.Services; using Performance.Services;
using System.Collections.Generic;
using System.Linq;
namespace Performance.Api namespace Performance.Api
{ {
...@@ -8,66 +12,87 @@ public class PerformanceQuery : ObjectGraphType ...@@ -8,66 +12,87 @@ public class PerformanceQuery : ObjectGraphType
{ {
public PerformanceQuery(GraphQLService service) public PerformanceQuery(GraphQLService service)
{ {
Field<ReportDataType>("demo", Field<ListGraphType<ReportDataType>>("info",
arguments: new QueryArguments resolve: context =>
( {
new QueryArgument<IntGraphType>() { Name = QueryParams.reportId }, return service.GetReportsInfo();
new QueryArgument<IntGraphType>() { Name = QueryParams.hospitalId }, }
new QueryArgument<IntGraphType>() { Name = QueryParams.year }, );
new QueryArgument<IntGraphType>() { Name = QueryParams.month },
new QueryArgument<StringGraphType>() { Name = QueryParams.category }
),
resolve: context =>
{
int hospitalId = context.Arguments.ContainsKey(QueryParams.hospitalId) ? ConvertHelper.TryInt(context.Arguments[QueryParams.hospitalId].ToString()) : default;
int year = context.Arguments.ContainsKey(QueryParams.year) ? ConvertHelper.TryInt(context.Arguments[QueryParams.year].ToString()) : default;
int month = context.Arguments.ContainsKey(QueryParams.month) ? ConvertHelper.TryInt(context.Arguments[QueryParams.month].ToString()) : default;
string category = context.Arguments.ContainsKey(QueryParams.category) ? context.Arguments[QueryParams.category].ToString() : string.Empty;
return service.Test(hospitalId, year, month);
}
);
Field<ListGraphType<ReportPerformanceType>>("performances", Field<ReportDataType>("report",
arguments: new QueryArguments arguments: new QueryArguments
( (
new QueryArgument<IntGraphType>() { Name = QueryParams.hospitalId }, new QueryArgument<IntGraphType>() { Name = QueryParams.reportId }
new QueryArgument<IntGraphType>() { Name = QueryParams.year },
new QueryArgument<IntGraphType>() { Name = QueryParams.month },
new QueryArgument<StringGraphType>() { Name = QueryParams.category }
), ),
resolve: context => resolve: context =>
{ {
int hospitalId = context.Arguments.ContainsKey(QueryParams.hospitalId) ? ConvertHelper.TryInt(context.Arguments[QueryParams.hospitalId].ToString()) : default; int reportId = context.Arguments.ContainsKey(QueryParams.reportId)
int year = context.Arguments.ContainsKey(QueryParams.year) ? ConvertHelper.TryInt(context.Arguments[QueryParams.year].ToString()) : default; ? ConvertHelper.To<int>(context.Arguments[QueryParams.reportId])
int month = context.Arguments.ContainsKey(QueryParams.month) ? ConvertHelper.TryInt(context.Arguments[QueryParams.month].ToString()) : default; : 0;
string category = context.Arguments.ContainsKey(QueryParams.category) ? context.Arguments[QueryParams.category].ToString() : string.Empty; return service.GetReport(reportId);
return service.GetPerformances(hospitalId, year, month, category);
} }
); );
Field<ListGraphType<ReportPerformanceType>>("paging", Field<ListGraphType<ChartDataType>>("chardata",
arguments: Arguments(),
resolve: context =>
{
int reportId = context.Arguments.ContainsKey(QueryParams.reportId)
? ConvertHelper.To<int>(context.Arguments[QueryParams.reportId])
: 0;
var parameters = GetDynamicParameters(context.Arguments, QueryParams.hospitalId, QueryParams.year, QueryParams.month);
return service.GetChartData(reportId, parameters);
}
);
Field<ListGraphType<ReportPerformanceType>>("performances",
arguments: new QueryArguments arguments: new QueryArguments
( (
new QueryArgument<IntGraphType>() { Name = QueryParams.pageSize },
new QueryArgument<IntGraphType>() { Name = QueryParams.pageNumber },
new QueryArgument<IntGraphType>() { Name = QueryParams.hospitalId },
new QueryArgument<IntGraphType>() { Name = QueryParams.year },
new QueryArgument<IntGraphType>() { Name = QueryParams.month },
new QueryArgument<StringGraphType>() { Name = QueryParams.category } new QueryArgument<StringGraphType>() { Name = QueryParams.category }
), ),
resolve: context => resolve: context =>
{ {
int pageSize = context.Arguments.ContainsKey(QueryParams.pageSize) ? ConvertHelper.TryInt(context.Arguments[QueryParams.pageSize].ToString()) : 10; return new List<EntityModels.report_performance>();
int pageNumber = context.Arguments.ContainsKey(QueryParams.pageNumber) ? ConvertHelper.TryInt(context.Arguments[QueryParams.pageNumber].ToString()) : 1; }
);
}
int hospitalId = context.Arguments.ContainsKey(QueryParams.hospitalId) ? ConvertHelper.TryInt(context.Arguments[QueryParams.hospitalId].ToString()) : default; public static QueryArguments Arguments(params QueryArgument[] args)
int year = context.Arguments.ContainsKey(QueryParams.year) ? ConvertHelper.TryInt(context.Arguments[QueryParams.year].ToString()) : default; {
int month = context.Arguments.ContainsKey(QueryParams.month) ? ConvertHelper.TryInt(context.Arguments[QueryParams.month].ToString()) : default; var basic = new QueryArguments
{
new QueryArgument<IntGraphType>() { Name = QueryParams.reportId },
new QueryArgument<IntGraphType>() { Name = QueryParams.hospitalId },
new QueryArgument<IntGraphType>() { Name = QueryParams.year },
new QueryArgument<IntGraphType>() { Name = QueryParams.month }
};
string category = context.Arguments.ContainsKey(QueryParams.category) ? context.Arguments[QueryParams.category].ToString() : string.Empty; if (args != null && args.Any())
return service.GetPagingPerformances(hospitalId, year, month, category, pageNumber, pageSize); {
foreach (var item in args)
{
basic.Add(item);
} }
); }
return basic;
}
public DynamicParameters GetDynamicParameters(Dictionary<string, object> arguments, params string[] fields)
{
DynamicParameters parameters = new DynamicParameters();
if (arguments == null || !arguments.Any()) return parameters;
if (fields != null && fields.Any())
{
foreach (var item in fields)
{
if (arguments.ContainsKey(item))
{
parameters.Add(item.ToLower(), arguments[item]);
}
}
}
return parameters;
} }
} }
} }
using GraphQL.Types; using GraphQL.Types;
using Performance.DtoModels; using Performance.DtoModels;
using Performance.Infrastructure;
using Performance.Services; using Performance.Services;
namespace Performance.Api namespace Performance.Api
...@@ -23,10 +24,6 @@ public ReportDataType(GraphQLService service) ...@@ -23,10 +24,6 @@ public ReportDataType(GraphQLService service)
Field(x => x.Formula); Field(x => x.Formula);
Field(x => x.DataType); Field(x => x.DataType);
Field(x => x.FilterValue); Field(x => x.FilterValue);
Field<ListGraphType<ChartDataType>>("charData", resolve: context =>
{
return service.GetChartData(context.Source.ReportID);
});
} }
} }
} }
using Performance.DtoModels; using Dapper;
using Microsoft.Extensions.Logging;
using Performance.DtoModels;
using Performance.EntityModels; using Performance.EntityModels;
using Performance.Infrastructure.Models; using Performance.Infrastructure.Models;
using Performance.Repository; using Performance.Repository;
...@@ -12,96 +14,64 @@ namespace Performance.Services ...@@ -12,96 +14,64 @@ namespace Performance.Services
{ {
public class GraphQLService : IAutoInjection public class GraphQLService : IAutoInjection
{ {
private readonly ILogger logger;
private readonly PerforPeremployeeRepository peremployeeRepository; private readonly PerforPeremployeeRepository peremployeeRepository;
private readonly PerforRepreportRepository repreportRepository;
private readonly PerforReportperformanceRepository reportperformanceRepository; private readonly PerforReportperformanceRepository reportperformanceRepository;
public GraphQLService( public GraphQLService(
ILogger<GraphQLService> logger,
PerforPeremployeeRepository peremployeeRepository, PerforPeremployeeRepository peremployeeRepository,
PerforRepreportRepository repreportRepository,
PerforReportperformanceRepository reportperformanceRepository PerforReportperformanceRepository reportperformanceRepository
) )
{ {
this.logger = logger;
this.peremployeeRepository = peremployeeRepository; this.peremployeeRepository = peremployeeRepository;
this.repreportRepository = repreportRepository;
this.reportperformanceRepository = reportperformanceRepository; this.reportperformanceRepository = reportperformanceRepository;
} }
#region Demo public List<ReportData> GetReportsInfo()
public ReportData Test(int hospitalId, int year, int month)
{ {
return new ReportData(new rep_report()) var reports = repreportRepository.GetEntities();
{ if (reports == null || !reports.Any()) return new List<ReportData>();
ChartData = new List<ChartData>()
};
}
public List<report_performance> GetPerformances(int hospitalId, int year, int month, string category)
{
Expression<Func<report_performance, bool>> exp = (t) => true;
if (hospitalId != 0)
{
exp = exp.And(t => t.HospitalId == hospitalId);
}
if (year != 0)
{
exp = exp.And(t => t.Year == year);
}
if (month != 0)
{
exp = exp.And(t => t.Month == month);
}
if (!string.IsNullOrEmpty(category)) var result = new List<ReportData>();
foreach (var item in reports)
{ {
exp = exp.And(t => category.Split(',').Contains(t.Category)); result.Add(new ReportData(item));
} }
return result;
}
return reportperformanceRepository.GetEntities(exp); public ReportData GetReport(int reportId)
{
if (reportId == 0) return new ReportData(new rep_report());
var report = repreportRepository.GetEntity(t => t.ID == reportId) ?? new rep_report();
return new ReportData(report);
} }
public PageList<report_performance> GetPagingPerformances(int hospitalId, int year, int month, string category, int pageNumber = 1, int pageSize = 10, string orderby = null) public List<ChartData> GetChartData(int reportId, DynamicParameters parameters)
{ {
Expression<Func<report_performance, bool>> exp = (t) => true; if (reportId == 0) return new List<ChartData>();
if (hospitalId != 0) try
{ {
exp = exp.And(t => t.HospitalId == hospitalId); var report = repreportRepository.GetEntity(t => t.ID == reportId);
} if (report == null || string.IsNullOrEmpty(report.Content)) return new List<ChartData>();
if (year != 0) var sql = report.Content.ToLower();
{
exp = exp.And(t => t.Year == year);
}
if (month != 0) var chartData = repreportRepository.DapperQuery<ChartData>(sql, parameters);
{
exp = exp.And(t => t.Month == month);
}
if (!string.IsNullOrEmpty(category)) return chartData != null && chartData.Any() ? chartData.ToList() : new List<ChartData>();
{
exp = exp.And(t => category.Split(',').Contains(t.Category));
} }
catch (Exception ex)
PageList<report_performance> data = reportperformanceRepository.GetAllPaging(exp, pageNumber, pageSize, orderby);
return data;
}
public List<ChartData> GetChartData(int reportId)
{
return new List<ChartData>
{ {
new ChartData { X = "张三", Y = "语文", Value = 89 }, logger.LogError("获取报表数据异常: " + ex.ToString());
new ChartData { X = "张三", Y = "数学", Value = 99 }, return new List<ChartData>();
new ChartData { X = "张三", Y = "英语", Value = 72 }, }
new ChartData { X = "李四", Y = "语文", Value = 92 },
new ChartData { X = "李四", Y = "数学", Value = 93 },
new ChartData { X = "李四", Y = "英语", Value = 80 },
};
} }
#endregion Demo
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment