using System.Data; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Dapper; using GCAS.Dto; using GCAS.Model; namespace GCAS.Code { public class ConfigRepository { public async Task UpdateThresholdAsync(ConfigDto entity) { string updateSql = "UPDATE config set config.value=@Value where config.key=@Key "; using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { int result = 0; result += await conn.ExecuteAsync(updateSql, new { Value = entity.PutThresholdMin, Key = "PutThresholdMin" }); result += await conn.ExecuteAsync(updateSql, new { Value = entity.PutThresholdMax, Key = "PutThresholdMax" }); return result; } } public ConfigDto Get() { ConfigDto config = new ConfigDto(); string selectSql = "select config.key,config.value from config"; using (IDbConnection conn = DataBaseConfig.GetSqlConnection()) { var models = conn.Query(selectSql).ToList(); PropertyInfo[] dataPro = config.GetType().GetProperties(); foreach (var item in dataPro) { switch (item.PropertyType.Name) { case "Int32": item.SetValue(config, int.Parse(models.FirstOrDefault(c => c.Key == item.Name).Value)); break; case "Single": item.SetValue(config, float.Parse(models.FirstOrDefault(c => c.Key == item.Name).Value)); break; case "Boolean": item.SetValue(config, models.FirstOrDefault(c => c.Key == item.Name).Value == "1" ? true : false); break; } } } return config; } } }