using Bowin.Common.Utility; using YLShipBuildLandMap.Entity; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace YLShipBuildLandMap.Services.SystemSetting { public class ParameterServices : IParameterServices { private YLShipBuildLandMapContext DbContext { get; set; } public ParameterServices(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } public async Task> GetParameterValue(string parameterKey) where T : struct { var parameter = await DbContext.SysParameter.FirstOrDefaultAsync(x => x.ParameterKey == parameterKey); if (parameter == null || string.IsNullOrEmpty(parameter.Value)) return null; T result; if (parameter.Value.TryParseTo(out result)) { return result; } return null; } public async Task GetParameterValue(string parameterKey) { var parameter = await DbContext.SysParameter.FirstOrDefaultAsync(x => x.ParameterKey == parameterKey); if (parameter == null || string.IsNullOrEmpty(parameter.Value)) return null; return parameter.Value; } public async Task SaveTo(string parameterKey, object value) { var parameter = await DbContext.SysParameter.FirstOrDefaultAsync(x => x.ParameterKey == parameterKey); if (parameter == null) { parameter = new SysParameter(); parameter.ParameterKey = parameterKey; if (value != null) { parameter.Value = value.ToString(); } else { parameter.Value = null; } this.DbContext.Add(parameter); } else { if (value != null) { parameter.Value = value.ToString(); } else { parameter.Value = null; } } await this.DbContext.SaveChangesAsync(); } } }