ServiceBaseExtensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using DapperORMCore.Context.DataContext;
  2. using DapperORMCore.Dapper;
  3. using DapperORMCore.Dapper.BaseModel;
  4. using DapperORMCore.Model.BaseModel;
  5. using DapperORMCore.Repository.IRepositories;
  6. using DapperORMCore.String.Consts;
  7. using DapperORMCore.String.Enums;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using WestDistance.DapperORM.Repository.Repositorys;
  13. namespace NXWMS.Code
  14. {
  15. /// <summary>
  16. /// Service类下的Extensions
  17. /// </summary>
  18. public static class ServiceBaseExtensions
  19. {
  20. /// <summary>
  21. /// 获取DB仓储仓储
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. /// <param name="dataRepositoryContext"></param>
  25. /// <returns></returns>
  26. public static IDataRepository<T> GetDataRepository<T>(this IDataRepositoryContext dataRepositoryContext) where T : class
  27. {
  28. return new DataRepository<T>(dataRepositoryContext);
  29. }
  30. /// <summary>
  31. /// 获取服务时间
  32. /// </summary>
  33. /// <returns></returns>
  34. public static DateTime GetServiceNowTime(this IDataRepositoryContext dataRepositoryContext)
  35. {
  36. var result = dataRepositoryContext.GetDataRepository<NowTimeModel>().
  37. Query("select Now=getdate()").FirstOrDefault();
  38. return result.Now;
  39. }
  40. #region FieldKeyInfo
  41. /// <summary>
  42. /// 新增字段信息T
  43. /// </summary>
  44. /// <param name="fieldKeyList"></param>
  45. /// <param name="key"></param>
  46. /// <param name="value"></param>
  47. /// <returns></returns>
  48. public static List<FieldKeyInfo> AddFieldKeyInfo(this List<FieldKeyInfo> fieldKeyList, string key, object value)
  49. {
  50. var type = EnumCSharpPropertyType.STRING;
  51. switch (value.GetType().Name)
  52. {
  53. case "System.String":
  54. break;
  55. case "System.Int32":
  56. type = EnumCSharpPropertyType.INT;
  57. break;
  58. case "System.Double":
  59. type = EnumCSharpPropertyType.DOUBLE;
  60. break;
  61. case "System.Single":
  62. type = EnumCSharpPropertyType.FLOAT;
  63. break;
  64. case "System.Boolean":
  65. type = EnumCSharpPropertyType.BOOL;
  66. break;
  67. case "System.DateTime":
  68. type = EnumCSharpPropertyType.DATE;
  69. break;
  70. case "System.Decimal":
  71. type = EnumCSharpPropertyType.DECIMAL;
  72. break;
  73. }
  74. fieldKeyList.Add(new FieldKeyInfo
  75. {
  76. FieldName = key,
  77. Value = value,
  78. Operation = DBOperationString._Equal,
  79. FileType = type,
  80. WhereItemType = EnumSelectWhereType.And
  81. });
  82. return fieldKeyList;
  83. }
  84. /// <summary>
  85. /// 新增字段信息T
  86. /// </summary>
  87. /// <param name="fieldKeyList"></param>
  88. /// <param name="key"></param>
  89. /// <param name="value"></param>
  90. /// <param name="cSharpPropertyType"></param>
  91. /// <returns></returns>
  92. public static List<FieldKeyInfo> AddFieldKeyInfo(this List<FieldKeyInfo> fieldKeyList, string key, object value, EnumCSharpPropertyType cSharpPropertyType)
  93. {
  94. fieldKeyList.Add(new FieldKeyInfo
  95. {
  96. FieldName = key,
  97. Value = value,
  98. Operation = DBOperationString._Equal,
  99. FileType = cSharpPropertyType,
  100. WhereItemType = EnumSelectWhereType.And
  101. });
  102. return fieldKeyList;
  103. }
  104. /// <summary>
  105. /// 新增字段信息T
  106. /// </summary>
  107. /// <typeparam name="T"></typeparam>
  108. /// <param name="fieldKeyList"></param>
  109. /// <param name="key"></param>
  110. /// <param name="value"></param>
  111. /// <param name="cSharpPropertyType"></param>
  112. /// <param name="operation"></param>
  113. /// <returns></returns>
  114. public static List<FieldKeyInfo<T>> AddFieldKeyInfo<T>(this List<FieldKeyInfo<T>> fieldKeyList, string key, object value,
  115. EnumCSharpPropertyType cSharpPropertyType, string operation = DBOperationString._Equal) where T : class
  116. {
  117. fieldKeyList.Add(new FieldKeyInfo<T>
  118. {
  119. FieldName = key,
  120. Value = value,
  121. Operation = operation,
  122. FileType = cSharpPropertyType,
  123. WhereItemType = EnumSelectWhereType.And
  124. });
  125. return fieldKeyList;
  126. }
  127. /// <summary>
  128. /// 追加字段Key信息T
  129. /// </summary>
  130. /// <typeparam name="T"></typeparam>
  131. /// <param name="fieldKeyList"></param>
  132. /// <param name="key"></param>
  133. /// <param name="value"></param>
  134. /// <param name="cSharpPropertyType"></param>
  135. /// <param name="operation"></param>
  136. /// <param name="isToAdd"></param>
  137. /// <param name="groupIndex"></param>
  138. /// <returns></returns>
  139. public static List<FieldKeyInfo<T>> AddFieldKeyInfo<T>(this List<FieldKeyInfo<T>> fieldKeyList, string key, object value,
  140. EnumCSharpPropertyType cSharpPropertyType, string operation = DBOperationString._Equal, bool isToAdd = true) where T : class
  141. {
  142. if (isToAdd)
  143. fieldKeyList.Add(new FieldKeyInfo<T>
  144. {
  145. FieldName = key,
  146. Value = value,
  147. Operation = operation,
  148. FileType = cSharpPropertyType,
  149. WhereItemType = EnumSelectWhereType.And
  150. });
  151. return fieldKeyList;
  152. }
  153. /// <summary>
  154. /// 追加字段Key信息T
  155. /// </summary>
  156. /// <typeparam name="T"></typeparam>
  157. /// <param name="fieldKeyList"></param>
  158. /// <param name="key"></param>
  159. /// <param name="value"></param>
  160. /// <param name="cSharpPropertyType"></param>
  161. /// <param name="operation"></param>
  162. /// <param name="selectWhereType"></param>
  163. /// <param name="isToAdd"></param>
  164. /// <param name="groupIndex"></param>
  165. /// <returns></returns>
  166. public static List<FieldKeyInfo<T>> AddFieldKeyInfo<T>(this List<FieldKeyInfo<T>> fieldKeyList, string key, object value,
  167. EnumCSharpPropertyType cSharpPropertyType, string operation = DBOperationString._Equal, EnumSelectWhereType selectWhereType = EnumSelectWhereType.And, int groupIndex = -1) where T : class
  168. {
  169. fieldKeyList.Add(new FieldKeyInfo<T>
  170. {
  171. FieldName = key,
  172. Value = value,
  173. Operation = operation,
  174. GroupIndex = groupIndex,
  175. FileType = cSharpPropertyType,
  176. WhereItemType = selectWhereType
  177. });
  178. return fieldKeyList;
  179. }
  180. /// <summary>
  181. /// 追加字段Key信息T
  182. /// </summary>
  183. /// <typeparam name="T"></typeparam>
  184. /// <param name="fieldKeyList"></param>
  185. /// <param name="key"></param>
  186. /// <param name="value"></param>
  187. /// <param name="cSharpPropertyType"></param>
  188. /// <param name="operation"></param>
  189. /// <param name="selectWhereType"></param>
  190. /// <param name="isToAdd"></param>
  191. /// <param name="groupIndex"></param>
  192. /// <returns></returns>
  193. public static List<FieldKeyInfo<T>> AddFieldKeyInfo<T>(this List<FieldKeyInfo<T>> fieldKeyList, string key, object value,
  194. EnumCSharpPropertyType cSharpPropertyType, string operation = DBOperationString._Equal, EnumSelectWhereType selectWhereType = EnumSelectWhereType.And,
  195. int groupIndex = -1, bool isToAdd = true) where T : class
  196. {
  197. if (isToAdd)
  198. fieldKeyList.Add(new FieldKeyInfo<T>
  199. {
  200. FieldName = key,
  201. Value = value,
  202. Operation = operation,
  203. GroupIndex = groupIndex,
  204. FileType = cSharpPropertyType,
  205. WhereItemType = selectWhereType
  206. });
  207. return fieldKeyList;
  208. }
  209. /// <summary>
  210. /// 追加字段Key信息
  211. /// </summary>
  212. /// <param name="fieldKeyList"></param>
  213. /// <param name="key"></param>
  214. /// <param name="value"></param>
  215. /// <param name="cSharpPropertyType"></param>
  216. /// <param name="operation"></param>
  217. /// <param name="isToAdd"></param>
  218. /// <param name="groupIndex"></param>
  219. /// <returns></returns>
  220. public static List<FieldKeyInfo> AddFieldKeyInfo(this List<FieldKeyInfo> fieldKeyList, string key, object value,
  221. EnumCSharpPropertyType cSharpPropertyType, string operation = DBOperationString._Equal,
  222. EnumSelectWhereType selectWhereType = EnumSelectWhereType.And, int groupIndex = -1, bool isToAdd = true)
  223. {
  224. if (isToAdd)
  225. fieldKeyList.Add(new FieldKeyInfo
  226. {
  227. FieldName = key,
  228. Value = value,
  229. Operation = operation,
  230. GroupIndex = groupIndex,
  231. FileType = cSharpPropertyType,
  232. WhereItemType = selectWhereType
  233. });
  234. return fieldKeyList;
  235. }
  236. /// <summary>
  237. /// 追加字段Key信息
  238. /// </summary>
  239. /// <typeparam name="T"></typeparam>
  240. /// <param name="fieldKeyList"></param>
  241. /// <param name="key"></param>
  242. /// <param name="value"></param>
  243. /// <param name="cSharpPropertyType"></param>
  244. /// <param name="operation"></param>
  245. /// <param name="isToAdd"></param>
  246. /// <param name="groupIndex"></param>
  247. /// <returns></returns>
  248. public static List<FieldKeyInfo> AddFieldKeyInfo(this List<FieldKeyInfo> fieldKeyList, string key, object value,
  249. EnumCSharpPropertyType cSharpPropertyType, string operation, bool isToAdd)
  250. {
  251. if (isToAdd)
  252. fieldKeyList.Add(new FieldKeyInfo
  253. {
  254. FieldName = key,
  255. Value = value,
  256. Operation = operation,
  257. FileType = cSharpPropertyType,
  258. WhereItemType = EnumSelectWhereType.And
  259. });
  260. return fieldKeyList;
  261. }
  262. /// <summary>
  263. /// 移除字段Key信息
  264. /// </summary>
  265. /// <param name="fieldKeyList"></param>
  266. /// <param name="key"></param>
  267. /// <returns></returns>
  268. public static List<FieldKeyInfo> RemoveFieldKeyInfo(this List<FieldKeyInfo> fieldKeyList, string key)
  269. {
  270. var result = fieldKeyList.Where(m => m.FieldName == key).FirstOrDefault();
  271. fieldKeyList.Remove(result);
  272. return fieldKeyList;
  273. }
  274. /// <summary>
  275. /// 移除字段Key信息T
  276. /// </summary>
  277. /// <param name="fieldKeyList"></param>
  278. /// <param name="key"></param>
  279. /// <returns></returns>
  280. public static List<FieldKeyInfo<T>> RemoveFieldKeyInfo<T>(this List<FieldKeyInfo<T>> fieldKeyList, string key) where T : class
  281. {
  282. var result = fieldKeyList.Where(m => m.FieldName == key).FirstOrDefault();
  283. fieldKeyList.Remove(result);
  284. return fieldKeyList;
  285. }
  286. #endregion
  287. }
  288. }