电子药箱通讯服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseOrclSnapshotBLL.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using RDH.Data.BLL;
  7. using RDH.Data.Models;
  8. namespace RDH.Data
  9. {
  10. /// <summary>
  11. /// 表示具有快照表的持久层基础类
  12. /// </summary>
  13. /// <typeparam name="T"></typeparam>
  14. public abstract class BaseOrclSnapshotBLL<T> : BaseOrclBLL<T>, ISnapshotBLL<T>
  15. where T : BaseModel
  16. {
  17. public BaseOrclSnapshotBLL() : base()
  18. {
  19. }
  20. protected virtual String BuildInsertSnapshotSql()
  21. {
  22. StringBuilder builder = new StringBuilder();
  23. builder.AppendFormat("INSERT INTO {0}(", this.TABLE_NAME + "_BASE");
  24. foreach (var c in ColumnPropMaps)
  25. {
  26. builder.AppendFormat("{0},", c.Key);
  27. }
  28. //根据参数值添加数据
  29. //builder.Append("SNAPSHOT_KEY) VALUES(");
  30. //foreach (var c in ColumnPropMaps)
  31. //{
  32. // builder.AppendFormat(":{0},", c.Value);
  33. //}
  34. //builder.Append(":SnapshotKey)");
  35. //根据表中数据添加
  36. builder.Remove(builder.Length - 1, 1);
  37. builder.Append(") SELECT ");
  38. foreach (var c in ColumnPropMaps)
  39. {
  40. //if (c.Key == "CREATE_TIME")
  41. //{
  42. // builder.Append("SYSDATE,");
  43. //}
  44. //else
  45. //{
  46. // builder.AppendFormat("{0},", c.Key);
  47. //}
  48. builder.AppendFormat("{0},", c.Key);
  49. }
  50. builder.Remove(builder.Length - 1, 1);
  51. builder.AppendFormat(" FROM {0} WHERE {1}=:{2}",
  52. this.TABLE_NAME,
  53. this.KEY_COLUMN,
  54. this.ColumnPropMaps[this.KEY_COLUMN]);
  55. return builder.ToString();
  56. }
  57. public virtual void UpdateCurrent(T v)
  58. {
  59. Object tempParam = GetParam(v);
  60. SqlMapper.Execute(ConnectionFactory.Current.GetSessionConnection(),
  61. BuildInsertSnapshotSql(),
  62. tempParam);
  63. v.CreateTime = DateTime.Now;
  64. tempParam = GetParam(v);
  65. SqlMapper.Execute(ConnectionFactory.Current.GetSessionConnection(),
  66. BuildUpdateSql(),
  67. tempParam);
  68. }
  69. public virtual void DeleteCurrent(T v)
  70. {
  71. Object tempParam = GetParam(v);
  72. SqlMapper.Execute(ConnectionFactory.Current.GetSessionConnection(),
  73. BuildInsertSnapshotSql(),
  74. tempParam);
  75. SqlMapper.Execute(ConnectionFactory.Current.GetSessionConnection(),
  76. BuildDeleteSql(),
  77. tempParam);
  78. }
  79. internal String BuildLastDataQueryByHistory(DateTime endDate, String partitionKey)
  80. {
  81. StringBuilder builder = new StringBuilder();
  82. builder.Append("SELECT ");
  83. foreach (KeyValuePair<String, String> pair in this.ColumnPropMaps)
  84. {
  85. builder.AppendFormat("t4.{0},", pair.Key, pair.Value);
  86. }
  87. builder.Remove(builder.Length - 1, 1);
  88. builder.AppendFormat(" FROM (SELECT ROW_NUMBER() OVER(PARTITION BY t3.{0} ORDER BY t3.create_time DESC) AS R,",
  89. String.IsNullOrEmpty(partitionKey) ? "key" : partitionKey);
  90. foreach (KeyValuePair<String, String> pair in this.ColumnPropMaps)
  91. {
  92. builder.AppendFormat("t3.{0},", pair.Key);
  93. }
  94. builder.Remove(builder.Length - 1, 1);
  95. builder.Append(" FROM (SELECT ");
  96. foreach (KeyValuePair<String, String> pair in this.ColumnPropMaps)
  97. {
  98. builder.AppendFormat("t1.{0},", pair.Key);
  99. }
  100. builder.Remove(builder.Length - 1, 1);
  101. builder.AppendFormat(" FROM {0} t1 "
  102. + " WHERE t1.create_time<TO_DATE('{1}', 'yyyy-mm-dd')"
  103. + " UNION SELECT ",
  104. this.TABLE_NAME, endDate.ToString("yyyy-MM-dd"));
  105. foreach (KeyValuePair<String, String> pair in this.ColumnPropMaps)
  106. {
  107. builder.AppendFormat("t2.{0},", pair.Key);
  108. }
  109. builder.Remove(builder.Length - 1, 1);
  110. builder.AppendFormat(" FROM {0}_BASE t2"
  111. + " WHERE t2.create_time<TO_DATE('{1}', 'yyyy-mm-dd')) t3) t4"
  112. + " WHERE t4.R=1",
  113. this.TABLE_NAME, endDate.ToString("yyyy-MM-dd"));
  114. return builder.ToString();
  115. }
  116. }
  117. }