SqlHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using System;
  4. using System.Collections.Generic;
  5. using Procurement.ViewModel;
  6. using System.Text.Json;
  7. using Procurement.EntityFrameworkCore.SqlRepositories;
  8. using Procurement.Controllers;
  9. using System.Collections;
  10. using Procurement.Enums;
  11. using Volo.Abp.Domain.Repositories;
  12. using Procurement.Services;
  13. namespace Procurement.Helpers
  14. {
  15. public class SqlHelper
  16. {
  17. /// <summary>
  18. /// ¹¹½¨sql²ÎÊýÊý×é
  19. /// </summary>
  20. /// <param name="json"></param>
  21. /// <returns></returns>
  22. public static async Task<SqlParameterViewModel[]> CreateSqlParameters(ISqlRepository sqlRepository,string proc,JsonElement json)
  23. {
  24. Dictionary<string, JsonElement> dict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
  25. Common common = new Common(sqlRepository);
  26. var items = await common.GetCommonItems("", "parameterlist", proc, "");
  27. List<SqlParameterViewModel> listParams = new List<SqlParameterViewModel>();
  28. foreach(KeyValuePair<string,object> item in items)
  29. {
  30. foreach (var d in (ArrayList)item.Value)
  31. {
  32. Dictionary<string, object> param = (Dictionary<string, object>)d;
  33. string paramMode = param["ParamMode"].ToString().ToUpper();
  34. string paramName = param["ParamName"].ToString();
  35. string dataType = param["DataType"].ToString().ToLower();
  36. object paramValue = null;
  37. if (dict.ContainsKey(paramName))
  38. {
  39. JsonElement valueObj = dict[paramName];
  40. switch (valueObj.ValueKind)
  41. {
  42. case JsonValueKind.Null:
  43. paramValue = null;
  44. break;
  45. case JsonValueKind.Number:
  46. paramValue = valueObj.GetDouble();
  47. break;
  48. case JsonValueKind.False:
  49. paramValue = false;
  50. break;
  51. case JsonValueKind.True:
  52. paramValue = true;
  53. break;
  54. case JsonValueKind.Undefined:
  55. paramValue = null;
  56. break;
  57. case JsonValueKind.String:
  58. paramValue = valueObj.GetString();
  59. break;
  60. }
  61. }
  62. SqlParameterViewModel paramModel = new SqlParameterViewModel { ParameterName = "@" + paramName, Value = paramValue, SqlDbType = SqlDbTypes.VarChar };
  63. if (dataType == "tinyint")
  64. {
  65. paramModel.SqlDbType = SqlDbTypes.Int16;
  66. }
  67. else if (dataType == "int")
  68. {
  69. paramModel.SqlDbType = SqlDbTypes.Int32;
  70. }
  71. else if (dataType == "bigint")
  72. {
  73. paramModel.SqlDbType = SqlDbTypes.Int64;
  74. }
  75. else if (dataType == "decimal")
  76. {
  77. paramModel.SqlDbType = SqlDbTypes.Decimal;
  78. }
  79. else if (dataType == "date" || dataType == "datetime")
  80. {
  81. paramModel.SqlDbType = SqlDbTypes.DateTime;
  82. }
  83. if (paramMode == "OUT")
  84. {
  85. paramModel.Direction = SqlParamDirection.Output;
  86. }
  87. listParams.Add(paramModel);
  88. }
  89. }
  90. return listParams.ToArray();
  91. }
  92. }
  93. }