PowerMettersBaseProvider.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Winsoft.GOV.Framework.Model;
  7. using Dapper;
  8. using System.Data;
  9. namespace Winsoft.GOV.Framework.Provider
  10. {
  11. public class PowerMettersBaseProvider : BaseProvider<PowerMattersBase>
  12. {
  13. private static PowerMettersBaseProvider instance;
  14. private static string cols = " `QL_INNER_CODE`, "
  15. + "`QL_NAME` ";
  16. public void CreateTable()
  17. {
  18. string sql = "create table if not exists PowerMattersBase("
  19. + "QL_NAME varchar(200),"
  20. + "QL_INNER_CODE varchar(50),"
  21. + "QL_DEP varchar(50),"
  22. + "OUGUID varchar(50),"
  23. + "QL_STATE varchar(1) DEFAULT '1'"
  24. + "); "
  25. + "call add_index('PowerMattersBase', 'index1', 'index1(QL_INNER_CODE, OUGUID)'); "
  26. + "call add_index('PowerMattersBase', 'index2', 'index2(QL_INNER_CODE)'); "
  27. + "call add_index('PowerMattersBase', 'index3', 'index3(OUGUID)'); "
  28. + "call add_index('PowerMattersBase', 'index4', 'index4(QL_STATE, OUGUID); ";
  29. using(var con = GetDbConnection())
  30. {
  31. con.Execute(sql);
  32. }
  33. }
  34. public PowerMettersBaseProvider() : base("gov")
  35. {
  36. }
  37. public static PowerMettersBaseProvider Instance
  38. {
  39. get
  40. {
  41. CreateInstance(ref instance);
  42. return instance;
  43. }
  44. }
  45. public void DeleteAll()
  46. {
  47. using (IDbConnection connection = GetDbConnection())
  48. {
  49. connection.Execute("delete from PowerMattersBase");
  50. }
  51. }
  52. public IEnumerable<PowerMattersBase> FindByBranchGUID(string guid)
  53. {
  54. //string sql = "select " + cols + " from powerMettersDetail where QL_INNER_CODE in (select QL_INNER_CODE PowerMattersBase where ouguid=@guid) and QL_STATE = '1'";
  55. string sql = "select " + cols + " from PowerMattersBase where ouguid=@guid and QL_STATE = '1'";
  56. using (IDbConnection connection = GetDbConnection())
  57. {
  58. return connection.Query<PowerMattersBase>(sql, new { guid = guid });
  59. }
  60. }
  61. public IEnumerable<PowerMattersBase> Find(string guid, string ql_kind)
  62. {
  63. string sql = "select " + cols + " from powerMettersDetail where OUGUID=@guid and ql_kind = @ql_kind and QL_INNER_CODE in (SELECT QL_INNER_CODE FROM PowerMattersBase WHERE ouguid=@guid and ql_state='1')";
  64. using (IDbConnection connection = GetDbConnection())
  65. {
  66. return connection.Query<PowerMattersBase>(sql, new { guid = guid, ql_kind = ql_kind });
  67. }
  68. }
  69. public IEnumerable<PowerMattersBase> Search(string ql_name, int itemIndex, int size)
  70. {
  71. string sql = "select " + cols + " from PowerMattersBase where ql_name like @ql_name and ql_state='1'";
  72. return GetItemList(sql, itemIndex, size, new { ql_name = "%" + ql_name + "%" });
  73. }
  74. public bool IsExistByQL_INNER_CODE(string ql_inner_code)
  75. {
  76. string sql = "select ouguid from PowerMattersBase where ql_inner_code=@ql_inner_code";
  77. using (IDbConnection connection = GetDbConnection())
  78. {
  79. IEnumerable<string> tmp = connection.Query<string>(sql, new { ql_inner_code = ql_inner_code });
  80. return tmp != null && tmp.Count() > 0;
  81. }
  82. }
  83. public void Insert(IList<PowerMattersBase> list)
  84. {
  85. string select = "select count(*) from PowerMattersBase where QL_INNER_CODE=@QL_INNER_CODE and OUGUID=@OUGUID;";
  86. string update = "update PowerMattersBase set QL_NAME=@QL_NAME, QL_DEP=@QL_DEP where QL_INNER_CODE=@QL_INNER_CODE and OUGUID=@OUGUID;";
  87. string insert = "insert PowerMattersBase(QL_NAME, QL_INNER_CODE, OUGUID, QL_DEP, QL_STATE) values(@QL_NAME, @QL_INNER_CODE, @OUGUID, @QL_DEP, @QL_STATE);";
  88. using (var conn = GetDbConnection())
  89. {
  90. foreach(PowerMattersBase p in list)
  91. {
  92. try
  93. {
  94. //int count = conn.QueryFirstOrDefault<int>(select, p);
  95. //if (count <= 0)
  96. conn.Execute(insert, p);
  97. //else
  98. // conn.Execute(update, p);
  99. }
  100. catch (Exception e)
  101. {
  102. }
  103. }
  104. }
  105. }
  106. public void Update(PowerMattersBase p)
  107. {
  108. string update = "update PowerMattersBase set QL_NAME=@QL_NAME, QL_DEP=@QL_DEP, QL_STATE=@QL_STATE where QL_INNER_CODE=@QL_INNER_CODE and OUGUID=@OUGUID;";
  109. using (IDbConnection connection = GetDbConnection())
  110. {
  111. connection.Execute(update, p);
  112. }
  113. }
  114. public void Insert(PowerMattersBase p)
  115. {
  116. string insert = "insert PowerMattersBase(QL_NAME, QL_INNER_CODE, OUGUID, QL_DEP, QL_STATE) values(@QL_NAME, @QL_INNER_CODE, @OUGUID, @QL_DEP, @QL_STATE);";
  117. using (IDbConnection connection = GetDbConnection())
  118. {
  119. connection.Execute(insert, p);
  120. }
  121. }
  122. public bool IsOnceRun(string ql_inner_code)
  123. {
  124. string sql = "select ql_inner_code from oncerunpowermatters where ql_inner_code=@ql_inner_code";
  125. using (IDbConnection connection = GetDbConnection())
  126. {
  127. IEnumerable<string> tmp = connection.Query<string>(sql, new { ql_inner_code = ql_inner_code });
  128. return tmp != null && tmp.Count() > 0;
  129. }
  130. }
  131. }
  132. }