1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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<Nullable<T>> GetParameterValue<T>(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<string> 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();
- }
- }
- }
|