25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

396 satır
13 KiB

  1. using Learun.DataBase.Repository;
  2. using Learun.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace Learun.Application.Base.SystemModule
  7. {
  8. /// <summary>
  9. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  10. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  11. /// 创建人:陈彬彬
  12. /// 日 期:2017.04.01
  13. /// 描 述:编号规则
  14. /// </summary>
  15. public class CodeRuleService : RepositoryFactory
  16. {
  17. #region 构造函数和属性
  18. private string fieldSql;
  19. public CodeRuleService()
  20. {
  21. fieldSql = @"
  22. t.F_RuleId,
  23. t.F_EnCode,
  24. t.F_FullName,
  25. t.F_CurrentNumber,
  26. t.F_RuleFormatJson,
  27. t.F_SortCode,
  28. t.F_DeleteMark,
  29. t.F_EnabledMark,
  30. t.F_Description,
  31. t.F_CreateDate,
  32. t.F_CreateUserId,
  33. t.F_CreateUserName,
  34. t.F_ModifyDate,
  35. t.F_ModifyUserId,
  36. t.F_ModifyUserName
  37. ";
  38. }
  39. #endregion
  40. #region 获取数据
  41. /// <summary>
  42. /// 规则列表
  43. /// </summary>
  44. /// <param name="pagination">分页</param>
  45. /// <param name="keyword">查询参数</param>
  46. /// <returns></returns>
  47. public IEnumerable<CodeRuleEntity> GetPageList(Pagination pagination, string keyword)
  48. {
  49. try
  50. {
  51. var strSql = new StringBuilder();
  52. strSql.Append(" SELECT ");
  53. strSql.Append(fieldSql);
  54. strSql.Append(" FROM LR_Base_CodeRule t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 ");
  55. if (!string.IsNullOrEmpty(keyword)) {
  56. strSql.Append(" AND ( F_EnCode LIKE @keyword OR F_FullName LIKE @keyword ) ");
  57. keyword = '%' + keyword + '%';
  58. }
  59. return this.BaseRepository().FindList<CodeRuleEntity>(strSql.ToString(), new { keyword = keyword }, pagination);
  60. }
  61. catch (Exception ex)
  62. {
  63. if (ex is ExceptionEx)
  64. {
  65. throw;
  66. }
  67. else
  68. {
  69. throw ExceptionEx.ThrowServiceException(ex);
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 规则列表
  75. /// </summary>
  76. /// <returns></returns>
  77. public IEnumerable<CodeRuleEntity> GetList()
  78. {
  79. try
  80. {
  81. var strSql = new StringBuilder();
  82. strSql.Append(" SELECT ");
  83. strSql.Append(fieldSql);
  84. strSql.Append(" FROM LR_Base_CodeRule t WHERE t.F_EnabledMark = 1 AND t.F_DeleteMark = 0 ");
  85. return this.BaseRepository().FindList<CodeRuleEntity>(strSql.ToString());
  86. }
  87. catch (Exception ex)
  88. {
  89. if (ex is ExceptionEx)
  90. {
  91. throw;
  92. }
  93. else
  94. {
  95. throw ExceptionEx.ThrowServiceException(ex);
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// 规则实体
  101. /// </summary>
  102. /// <param name="keyValue">主键值</param>
  103. /// <returns></returns>
  104. public CodeRuleEntity GetEntity(string keyValue)
  105. {
  106. try
  107. {
  108. return this.BaseRepository().FindEntity<CodeRuleEntity>(keyValue);
  109. }
  110. catch (Exception ex)
  111. {
  112. if (ex is ExceptionEx)
  113. {
  114. throw;
  115. }
  116. else
  117. {
  118. throw ExceptionEx.ThrowServiceException(ex);
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// 规则实体
  124. /// </summary>
  125. /// <param name="enCode">规则编码</param>
  126. /// <returns></returns>
  127. public CodeRuleEntity GetEntityByCode(string enCode) {
  128. try
  129. {
  130. return this.BaseRepository().FindEntity<CodeRuleEntity>(t => t.F_EnCode == enCode);
  131. }
  132. catch (Exception ex)
  133. {
  134. if (ex is ExceptionEx)
  135. {
  136. throw;
  137. }
  138. else
  139. {
  140. throw ExceptionEx.ThrowServiceException(ex);
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// 获取种子
  146. /// </summary>
  147. /// <param name="ruleId"></param>
  148. /// <returns></returns>
  149. public CodeRuleSeedEntity GetSeedEntity(string ruleId)
  150. {
  151. try
  152. {
  153. return this.BaseRepository().FindEntity<CodeRuleSeedEntity>(t => t.F_RuleId == ruleId);
  154. }
  155. catch (Exception ex)
  156. {
  157. if (ex is ExceptionEx)
  158. {
  159. throw;
  160. }
  161. else
  162. {
  163. throw ExceptionEx.ThrowServiceException(ex);
  164. }
  165. }
  166. }
  167. #endregion
  168. #region 提交数据
  169. /// <summary>
  170. /// 删除规则
  171. /// </summary>
  172. /// <param name="keyValue">主键</param>
  173. public void VirtualDelete(string keyValue)
  174. {
  175. try
  176. {
  177. CodeRuleEntity entity = new CodeRuleEntity()
  178. {
  179. F_RuleId = keyValue,
  180. F_DeleteMark = 1
  181. };
  182. this.BaseRepository().Update(entity);
  183. }
  184. catch (Exception ex)
  185. {
  186. if (ex is ExceptionEx)
  187. {
  188. throw;
  189. }
  190. else
  191. {
  192. throw ExceptionEx.ThrowServiceException(ex);
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// 保存规则表单(新增、修改)
  198. /// </summary>
  199. /// <param name="keyValue">主键值</param>
  200. /// <param name="codeRuleEntity">规则实体</param>
  201. /// <returns></returns>
  202. public void SaveEntity(string keyValue, CodeRuleEntity codeRuleEntity, UserInfo userInfo = null)
  203. {
  204. try
  205. {
  206. if (!string.IsNullOrEmpty(keyValue))
  207. {
  208. codeRuleEntity.Modify(keyValue, userInfo);
  209. this.BaseRepository().Update(codeRuleEntity);
  210. }
  211. else
  212. {
  213. codeRuleEntity.Create(userInfo);
  214. this.BaseRepository().Insert(codeRuleEntity);
  215. }
  216. }
  217. catch (Exception ex)
  218. {
  219. if (ex is ExceptionEx)
  220. {
  221. throw;
  222. }
  223. else
  224. {
  225. throw ExceptionEx.ThrowServiceException(ex);
  226. }
  227. }
  228. }
  229. #endregion
  230. #region 验证数据
  231. /// <summary>
  232. /// 规则编号不能重复
  233. /// </summary>
  234. /// <param name="enCode">编号</param>
  235. /// <param name="keyValue">主键</param>
  236. /// <returns></returns>
  237. public bool ExistEnCode(string enCode, string keyValue)
  238. {
  239. try
  240. {
  241. var strSql = new StringBuilder();
  242. strSql.Append(" SELECT t.F_RuleId FROM LR_Base_CodeRule t WHERE t.F_DeleteMark = 0 AND t.F_EnCode = @enCode ");
  243. if (!string.IsNullOrEmpty(keyValue))
  244. {
  245. strSql.Append(" AND t.F_RuleId != @keyValue ");
  246. }
  247. CodeRuleEntity entity = this.BaseRepository().FindEntity<CodeRuleEntity>(strSql.ToString(), new { enCode = enCode, keyValue = keyValue });
  248. return entity == null ? true : false;
  249. }
  250. catch (Exception ex)
  251. {
  252. if (ex is ExceptionEx)
  253. {
  254. throw;
  255. }
  256. else
  257. {
  258. throw ExceptionEx.ThrowServiceException(ex);
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// 规则名称不能重复
  264. /// </summary>
  265. /// <param name="fullName">名称</param>
  266. /// <param name="keyValue">主键</param>
  267. /// <returns></returns>
  268. public bool ExistFullName(string fullName, string keyValue)
  269. {
  270. try
  271. {
  272. var strSql = new StringBuilder();
  273. strSql.Append(" SELECT t.F_RuleId FROM LR_Base_CodeRule t WHERE t.F_DeleteMark = 0 AND t.F_FullName = @fullName ");
  274. if (!string.IsNullOrEmpty(keyValue))
  275. {
  276. strSql.Append(" AND t.F_RuleId != @keyValue ");
  277. }
  278. CodeRuleEntity entity = this.BaseRepository().FindEntity<CodeRuleEntity>(strSql.ToString(), new { fullName = fullName, keyValue = keyValue });
  279. return entity == null ? true : false;
  280. }
  281. catch (Exception ex)
  282. {
  283. if (ex is ExceptionEx)
  284. {
  285. throw;
  286. }
  287. else
  288. {
  289. throw ExceptionEx.ThrowServiceException(ex);
  290. }
  291. }
  292. }
  293. #endregion
  294. #region 单据编码处理
  295. /// <summary>
  296. /// 获取当前编号规则种子列表
  297. /// </summary>
  298. /// <param name="ruleId">编号规则主键</param>
  299. /// <returns></returns>
  300. public List<CodeRuleSeedEntity> GetSeedList(string ruleId, UserInfo userInfo)
  301. {
  302. try
  303. {
  304. //获取当前最大种子
  305. List<CodeRuleSeedEntity> codeRuleSeedList = (List<CodeRuleSeedEntity>)this.BaseRepository().FindList<CodeRuleSeedEntity>(t => t.F_RuleId.Equals(ruleId));
  306. if (codeRuleSeedList.Count == 0)
  307. {
  308. //说明没有种子,插入一条种子
  309. CodeRuleSeedEntity codeRuleSeedEntity = new CodeRuleSeedEntity();
  310. codeRuleSeedEntity.Create(userInfo);
  311. codeRuleSeedEntity.F_SeedValue = 1;
  312. codeRuleSeedEntity.F_RuleId = ruleId;
  313. this.BaseRepository().Insert<CodeRuleSeedEntity>(codeRuleSeedEntity);
  314. codeRuleSeedList.Add(codeRuleSeedEntity);
  315. }
  316. return codeRuleSeedList;
  317. }
  318. catch (Exception ex)
  319. {
  320. if (ex is ExceptionEx)
  321. {
  322. throw;
  323. }
  324. else
  325. {
  326. throw ExceptionEx.ThrowServiceException(ex);
  327. }
  328. }
  329. }
  330. /// <summary>
  331. /// 保存单据编号规则种子
  332. /// </summary>
  333. /// <param name="keyValue">主键</param>
  334. /// <param name="codeRuleSeedEntity">种子实体</param>
  335. public void SaveSeed(string keyValue, CodeRuleSeedEntity codeRuleSeedEntity, UserInfo userInfo)
  336. {
  337. try
  338. {
  339. if (string.IsNullOrEmpty(keyValue))
  340. {
  341. codeRuleSeedEntity.Create(userInfo);
  342. this.BaseRepository().Insert(codeRuleSeedEntity);
  343. }
  344. else
  345. {
  346. codeRuleSeedEntity.Modify(keyValue, userInfo);
  347. this.BaseRepository().Update(codeRuleSeedEntity);
  348. }
  349. }
  350. catch (Exception ex)
  351. {
  352. if (ex is ExceptionEx)
  353. {
  354. throw;
  355. }
  356. else
  357. {
  358. throw ExceptionEx.ThrowServiceException(ex);
  359. }
  360. }
  361. }
  362. /// <summary>
  363. /// 删除种子,表示被占用了
  364. /// </summary>
  365. /// <param name="userId">用户主键</param>
  366. /// <param name="ruleId">规则主键</param>
  367. public void DeleteSeed(string userId, string ruleId)
  368. {
  369. try
  370. {
  371. this.BaseRepository().Delete<CodeRuleSeedEntity>(t => t.F_UserId == userId && t.F_RuleId == ruleId);
  372. }
  373. catch (Exception ex)
  374. {
  375. if (ex is ExceptionEx)
  376. {
  377. throw;
  378. }
  379. else
  380. {
  381. throw ExceptionEx.ThrowServiceException(ex);
  382. }
  383. }
  384. }
  385. #endregion
  386. }
  387. }