电子药箱通讯服务端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Dapper;
  7. namespace RDH.PharmacyPlatform.Sync.Core
  8. {
  9. internal abstract class BaseDAL
  10. {
  11. public BaseDAL() { ColumnPropMaps = InitMaps(); }
  12. internal Dictionary<string, string> ColumnPropMaps;
  13. internal virtual String TableName { get; set; }//TB_NAME
  14. internal virtual string EntityName { get; set; }
  15. internal virtual String KeyColumnName { get; set; }//KEY_COLUMN
  16. internal abstract Dictionary<String, String> InitMaps();
  17. #region Protected Methods
  18. internal string BuildSelectSql()
  19. {
  20. StringBuilder selectValues = new StringBuilder();
  21. foreach (var c in ColumnPropMaps)
  22. {
  23. selectValues.AppendFormat("{0} {1},", c.Key, c.Value);
  24. }
  25. selectValues.Remove(selectValues.Length - 1, 1);
  26. return string.Format("SELECT {1} FROM {0} ", TableName, selectValues);
  27. }
  28. internal string BuildGetSql()
  29. {
  30. StringBuilder selectValues = new StringBuilder();
  31. string keyWhere = string.Format("{0}=:{1}", KeyColumnName, ColumnPropMaps[KeyColumnName]);
  32. foreach (var c in ColumnPropMaps)
  33. {
  34. selectValues.AppendFormat("{0} {1},", c.Key, c.Value);
  35. }
  36. selectValues.Remove(selectValues.Length - 1, 1);
  37. return string.Format("SELECT {1} FROM {0} WHERE {2}", TableName, selectValues, keyWhere);
  38. }
  39. internal String BuildCountSql()
  40. {
  41. return String.Format("SELECT COUNT({0}) FROM {1}", KeyColumnName, TableName);
  42. }
  43. internal string BuildInsertSql()
  44. {
  45. StringBuilder cols = new StringBuilder();
  46. StringBuilder parms = new StringBuilder();
  47. foreach (var c in ColumnPropMaps)
  48. {
  49. cols.AppendFormat("{0},", c.Key);
  50. parms.AppendFormat(":{0},", c.Value);
  51. }
  52. cols.Remove(cols.Length - 1, 1);
  53. parms.Remove(parms.Length - 1, 1);
  54. return string.Format("INSERT INTO {0} ({1}) VALUES ({2})", TableName, cols, parms);
  55. }
  56. internal string BuildUpdateSql()
  57. {
  58. StringBuilder setters = new StringBuilder();
  59. string keyWhere = string.Format("{0}=:{1}", KeyColumnName, ColumnPropMaps[KeyColumnName]);
  60. foreach (var c in ColumnPropMaps)
  61. {
  62. if (c.Key != KeyColumnName)
  63. {
  64. setters.AppendFormat("{0}=:{1},", c.Key, c.Value);
  65. }
  66. }
  67. setters.Remove(setters.Length - 1, 1);
  68. return string.Format("UPDATE {0} SET {1} WHERE {2}", TableName, setters, keyWhere);
  69. }
  70. internal string BuildDeleteSql()
  71. {
  72. string keyWhere = string.Format("{0}=:{1}", KeyColumnName, ColumnPropMaps[KeyColumnName]);
  73. return string.Format("DELETE FROM {0} WHERE {1}", TableName, keyWhere);
  74. }
  75. #endregion
  76. }
  77. internal abstract class BaseDAL<T> : BaseDAL
  78. {
  79. internal virtual object GetParam(T obj)
  80. {
  81. return obj;
  82. }
  83. #region public Methods
  84. public virtual IEnumerable<T> ListModels()
  85. {
  86. return SqlMapper.Query<T>(ConnectionSessionFactory.GetConnection(), BuildSelectSql());
  87. }
  88. public virtual Boolean DeleteModel(T obj)
  89. {
  90. return SqlMapper.Execute(ConnectionSessionFactory.GetConnection(), this.BuildDeleteSql(), GetParam(obj)) > 0;
  91. }
  92. public Boolean InsertModel(T obj)
  93. {
  94. return SqlMapper.Execute(ConnectionSessionFactory.GetConnection(), BuildInsertSql(), GetParam(obj)) > 0;
  95. }
  96. public Boolean UpdateModel(T obj)
  97. {
  98. return SqlMapper.Execute(ConnectionSessionFactory.GetConnection(), BuildUpdateSql(), GetParam(obj)) > 0;
  99. }
  100. public T GetModel(T obj)
  101. {
  102. return SqlMapper.Query<T>(ConnectionSessionFactory.GetConnection(), BuildGetSql(), GetParam(obj)).FirstOrDefault();
  103. }
  104. #endregion
  105. }
  106. }