ConfigRepository.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Data;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Dapper;
  6. using GCAS.Dto;
  7. using GCAS.Model;
  8. namespace GCAS.Code
  9. {
  10. public class ConfigRepository
  11. {
  12. public async Task<int> UpdateThresholdAsync(ConfigDto entity)
  13. {
  14. string updateSql = "UPDATE config set config.value=@Value where config.key=@Key ";
  15. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  16. {
  17. int result = 0;
  18. result += await conn.ExecuteAsync(updateSql, new { Value = entity.PutThresholdMin, Key = "PutThresholdMin" });
  19. result += await conn.ExecuteAsync(updateSql, new { Value = entity.PutThresholdMax, Key = "PutThresholdMax" });
  20. return result;
  21. }
  22. }
  23. public ConfigDto Get()
  24. {
  25. ConfigDto config = new ConfigDto();
  26. string selectSql = "select config.key,config.value from config";
  27. using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
  28. {
  29. var models = conn.Query<ConfigModel>(selectSql).ToList();
  30. PropertyInfo[] dataPro = config.GetType().GetProperties();
  31. foreach (var item in dataPro)
  32. {
  33. switch (item.PropertyType.Name)
  34. {
  35. case "Int32":
  36. item.SetValue(config, int.Parse(models.FirstOrDefault(c => c.Key == item.Name).Value));
  37. break;
  38. case "Single":
  39. item.SetValue(config, float.Parse(models.FirstOrDefault(c => c.Key == item.Name).Value));
  40. break;
  41. case "Boolean":
  42. item.SetValue(config, models.FirstOrDefault(c => c.Key == item.Name).Value == "1" ? true : false);
  43. break;
  44. }
  45. }
  46. }
  47. return config;
  48. }
  49. }
  50. }