SystemService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using IoTIntegrationPlatform.Common;
  7. using IoTIntegrationPlatform.Interface;
  8. using IoTIntegrationPlatform.Model.common;
  9. using IoTIntegrationPlatform.Model.Model;
  10. using SqlSugar;
  11. namespace IoTIntegrationPlatform.Services
  12. {
  13. public class SystemService : ISystemService
  14. {
  15. private ISqlSugarClient db { get; }
  16. public SystemService(ISqlSugarClient _db)
  17. {
  18. db = _db;
  19. }
  20. #region 类句柄操作
  21. private IntPtr handle;
  22. private bool disposed = false;
  23. //关闭句柄
  24. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
  25. public extern static Boolean CloseHandle(IntPtr handle);
  26. /// <summary>
  27. /// 释放对象资源
  28. /// </summary>
  29. public void Dispose()
  30. {
  31. Dispose(true);
  32. GC.SuppressFinalize(this);
  33. }
  34. /// <summary>
  35. /// 释放对象资源
  36. /// </summary>
  37. /// <param name="disposing"></param>
  38. protected virtual void Dispose(bool disposing)
  39. {
  40. if (!this.disposed)
  41. {
  42. if (handle != IntPtr.Zero)
  43. {
  44. CloseHandle(handle);
  45. handle = IntPtr.Zero;
  46. }
  47. }
  48. disposed = true;
  49. }
  50. ~SystemService()
  51. {
  52. Dispose(false);
  53. }
  54. #endregion
  55. #region 客户信息
  56. /// <summary>
  57. /// 获取客户信息
  58. /// </summary>
  59. /// <param name="customerId"></param>
  60. /// <param name="name"></param>
  61. /// <param name="code"></param>
  62. /// <param name="account"></param>
  63. /// <returns></returns>
  64. public CustomerInfo GetCustomerInfo(int customerId, string name, string code,string account)
  65. {
  66. CustomerInfo model = new CustomerInfo();
  67. try
  68. {
  69. model = db.Queryable<CustomerInfo>()
  70. .Where(it => it.IsValid == true)
  71. .WhereIF(customerId > 0, it => it.CustomerId == customerId)
  72. .WhereIF(!string.IsNullOrEmpty(name), it => it.ContactName.Equals(name))
  73. .WhereIF(!string.IsNullOrEmpty(code), it => it.CustomerCode.Equals(code))
  74. .WhereIF(!string.IsNullOrEmpty(account), it => it.CustomerAccount.Equals(account))
  75. .First();
  76. }
  77. catch (Exception ex)
  78. {
  79. Logging.Error(ex, ex.Message);
  80. }
  81. return model;
  82. }
  83. /// <summary>
  84. /// 获取客户列表
  85. /// </summary>
  86. /// <param name="parm"></param>
  87. /// <param name="page"></param>
  88. /// <returns></returns>
  89. public List<CustomerInfo> GetCustomerList(BaseParm parm, ref int page)
  90. {
  91. List<CustomerInfo> list = new List<CustomerInfo>();
  92. try
  93. {
  94. list = db.Queryable<CustomerInfo>()
  95. .Where(it => it.IsValid == true)
  96. .WhereIF(!string.IsNullOrEmpty(parm.Name), it => it.ContactName.Equals(parm.Name))
  97. .WhereIF(!string.IsNullOrEmpty(parm.Code), it => it.CustomerCode == parm.Code)
  98. .ToPageList(parm.PageIndex, parm.PageSize, ref page);
  99. }
  100. catch (Exception ex)
  101. {
  102. Logging.Error(ex, ex.Message);
  103. }
  104. return list;
  105. }
  106. /// <summary>
  107. /// 添加客户信息
  108. /// </summary>
  109. /// <param name="model"></param>
  110. /// <returns></returns>
  111. public bool AddCustomer(CustomerInfo model)
  112. {
  113. bool result = false;
  114. try
  115. {
  116. model.CreateTime = DateTime.Now;
  117. model.UpdateTime = DateTime.Now;
  118. model.IsValid = true;
  119. int row = db.Insertable(model).ExecuteCommand();
  120. if (row > 0)
  121. {
  122. result = true;
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. Logging.Error(ex, "添加客户");
  128. }
  129. return result;
  130. }
  131. /// <summary>
  132. /// 编辑客户
  133. /// </summary>
  134. /// <param name="model"></param>
  135. /// <returns></returns>
  136. public bool UpdatedCustomer(CustomerInfo model)
  137. {
  138. bool result = false;
  139. try
  140. {
  141. var row = db.Updateable(model).IgnoreColumns(it => new { it.CustomerId, it.CreateTime, it.IsValid })
  142. .Where(it => it.CustomerId == model.CustomerId)
  143. .ExecuteCommand();
  144. if (row > 0)
  145. {
  146. result = true;
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. Logging.Error(ex, ex.Message);
  152. }
  153. return result;
  154. }
  155. /// <summary>
  156. /// 修改客户密码
  157. /// </summary>
  158. /// <param name="model"></param>
  159. /// <returns></returns>
  160. public bool UpdateCustomerPassWord(CustomerInfo model)
  161. {
  162. bool result = false;
  163. try
  164. {
  165. model.UpdateTime = DateTime.Now;
  166. int row = db.Updateable<CustomerInfo>()
  167. .SetColumns(it => new CustomerInfo { UpdateTime = DateTime.Now, CustomerPassword = model.CustomerPassword, SecretKey = model.SecretKey })
  168. .Where(it => it.CustomerId == model.CustomerId)
  169. .ExecuteCommand();
  170. if (row > 0)
  171. {
  172. result = true;
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. Logging.Error(ex, ex.Message);
  178. }
  179. return result;
  180. }
  181. /// <summary>
  182. /// 删除客户
  183. /// </summary>
  184. /// <param name="parm"></param>
  185. /// <returns></returns>
  186. public bool DeleteCustomer(BaseParm parm)
  187. {
  188. bool result = false;
  189. try
  190. {
  191. int row = db.Updateable<CustomerInfo>()
  192. .SetColumns(it => new CustomerInfo { UpdateTime = DateTime.Now, IsValid = false })
  193. .Where(it => it.CustomerId == parm.Id)
  194. .ExecuteCommand();
  195. if (row > 0)
  196. {
  197. result = true;
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. Logging.Error(ex, ex.Message);
  203. }
  204. return result;
  205. }
  206. #endregion
  207. }
  208. }