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.

CodeRuleBLL.cs 22 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. using Learun.Application.Organization;
  2. using Learun.Cache.Base;
  3. using Learun.Cache.Factory;
  4. using Learun.Util;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace Learun.Application.Base.SystemModule
  8. {
  9. /// <summary>
  10. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  11. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  12. /// 创建人:陈彬彬
  13. /// 日 期:2017.04.17
  14. /// 描 述:编号规则
  15. /// </summary>
  16. public class CodeRuleBLL : CodeRuleIBLL
  17. {
  18. #region 属性
  19. private CodeRuleService codeRuleService = new CodeRuleService();
  20. // 组织单位
  21. DepartmentIBLL departmentIBLL = new DepartmentBLL();
  22. CompanyIBLL companyIBLL = new CompanyBLL();
  23. UserIBLL userIBLL = new UserBLL();
  24. #endregion
  25. #region 缓存定义
  26. /*缓存*/
  27. private ICache cache = CacheFactory.CaChe();
  28. private string cacheKey = "Learun_adms_cuderule_";//+规则编码
  29. private string cacheKeySeed = "Learun_adms_cuderule_seed_";//+规则主键
  30. #endregion
  31. #region 获取数据
  32. /// <summary>
  33. /// 规则列表
  34. /// </summary>
  35. /// <param name="pagination">分页</param>
  36. /// <param name="keyword">查询参数</param>
  37. /// <returns></returns>
  38. public IEnumerable<CodeRuleEntity> GetPageList(Pagination pagination, string keyword)
  39. {
  40. try
  41. {
  42. return codeRuleService.GetPageList(pagination, keyword);
  43. }
  44. catch (Exception ex)
  45. {
  46. if (ex is ExceptionEx)
  47. {
  48. throw;
  49. }
  50. else
  51. {
  52. throw ExceptionEx.ThrowBusinessException(ex);
  53. }
  54. }
  55. }
  56. /// <summary>
  57. /// 规则列表
  58. /// </summary>
  59. /// <returns></returns>
  60. public IEnumerable<CodeRuleEntity> GetList()
  61. {
  62. try
  63. {
  64. return codeRuleService.GetList();
  65. }
  66. catch (Exception ex)
  67. {
  68. if (ex is ExceptionEx)
  69. {
  70. throw;
  71. }
  72. else
  73. {
  74. throw ExceptionEx.ThrowBusinessException(ex);
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// 规则实体
  80. /// </summary>
  81. /// <param name="keyValue">主键值</param>
  82. /// <returns></returns>
  83. public CodeRuleEntity GetEntity(string keyValue)
  84. {
  85. try
  86. {
  87. return codeRuleService.GetEntity(keyValue); ;
  88. }
  89. catch (Exception ex)
  90. {
  91. if (ex is ExceptionEx)
  92. {
  93. throw;
  94. }
  95. else
  96. {
  97. throw ExceptionEx.ThrowBusinessException(ex);
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// 规则实体
  103. /// </summary>
  104. /// <param name="enCode">规则编码</param>
  105. /// <returns></returns>
  106. public CodeRuleEntity GetEntityByCode(string enCode)
  107. {
  108. try
  109. {
  110. CodeRuleEntity codeRuleEntity = cache.Read<CodeRuleEntity>(cacheKey + enCode, CacheId.codeRule);
  111. if (codeRuleEntity == null) {
  112. codeRuleEntity = codeRuleService.GetEntityByCode(enCode);
  113. cache.Write<CodeRuleEntity>(cacheKey + enCode, codeRuleEntity, CacheId.codeRule);
  114. }
  115. return codeRuleEntity;
  116. }
  117. catch (Exception ex)
  118. {
  119. if (ex is ExceptionEx)
  120. {
  121. throw;
  122. }
  123. else
  124. {
  125. throw ExceptionEx.ThrowBusinessException(ex);
  126. }
  127. }
  128. }
  129. #endregion
  130. #region 提交数据
  131. /// <summary>
  132. /// 删除规则
  133. /// </summary>
  134. /// <param name="keyValue">主键</param>
  135. public void VirtualDelete(string keyValue)
  136. {
  137. try
  138. {
  139. CodeRuleEntity entity = codeRuleService.GetEntity(keyValue);
  140. cache.Remove(cacheKey + entity.F_EnCode, CacheId.codeRule);
  141. codeRuleService.VirtualDelete(keyValue);
  142. }
  143. catch (Exception ex)
  144. {
  145. if (ex is ExceptionEx)
  146. {
  147. throw;
  148. }
  149. else
  150. {
  151. throw ExceptionEx.ThrowBusinessException(ex);
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// 保存规则表单(新增、修改)
  157. /// </summary>
  158. /// <param name="keyValue">主键值</param>
  159. /// <param name="codeRuleEntity">规则实体</param>
  160. /// <returns></returns>
  161. public void SaveEntity(string keyValue, CodeRuleEntity codeRuleEntity)
  162. {
  163. try
  164. {
  165. if (!string.IsNullOrEmpty(keyValue))
  166. {
  167. CodeRuleEntity entity = codeRuleService.GetEntity(keyValue);
  168. cache.Remove(cacheKey + entity.F_EnCode, CacheId.codeRule);
  169. }
  170. codeRuleService.SaveEntity(keyValue, codeRuleEntity);
  171. }
  172. catch (Exception ex)
  173. {
  174. if (ex is ExceptionEx)
  175. {
  176. throw;
  177. }
  178. else
  179. {
  180. throw ExceptionEx.ThrowBusinessException(ex);
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 保存规则表单(新增、修改)
  186. /// </summary>
  187. /// <param name="keyValue">主键值</param>
  188. /// <param name="codeRuleEntity">规则实体</param>
  189. /// <returns></returns>
  190. public void SaveEntity(string keyValue, CodeRuleEntity codeRuleEntity, UserInfo userInfo)
  191. {
  192. try
  193. {
  194. if (!string.IsNullOrEmpty(keyValue))
  195. {
  196. CodeRuleEntity entity = codeRuleService.GetEntity(keyValue);
  197. cache.Remove(cacheKey + entity.F_EnCode, CacheId.codeRule);
  198. }
  199. codeRuleService.SaveEntity(keyValue, codeRuleEntity,userInfo);
  200. }
  201. catch (Exception ex)
  202. {
  203. if (ex is ExceptionEx)
  204. {
  205. throw;
  206. }
  207. else
  208. {
  209. throw ExceptionEx.ThrowBusinessException(ex);
  210. }
  211. }
  212. }
  213. #endregion
  214. #region 验证数据
  215. /// <summary>
  216. /// 规则编号不能重复
  217. /// </summary>
  218. /// <param name="enCode">编号</param>
  219. /// <param name="keyValue">主键</param>
  220. /// <returns></returns>
  221. public bool ExistEnCode(string enCode, string keyValue)
  222. {
  223. try
  224. {
  225. return codeRuleService.ExistEnCode(enCode, keyValue);
  226. }
  227. catch (Exception ex)
  228. {
  229. if (ex is ExceptionEx)
  230. {
  231. throw;
  232. }
  233. else
  234. {
  235. throw ExceptionEx.ThrowBusinessException(ex);
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// 规则名称不能重复
  241. /// </summary>
  242. /// <param name="fullName">名称</param>
  243. /// <param name="keyValue">主键</param>
  244. /// <returns></returns>
  245. public bool ExistFullName(string fullName, string keyValue)
  246. {
  247. try
  248. {
  249. return codeRuleService.ExistFullName(fullName, keyValue);
  250. }
  251. catch (Exception ex)
  252. {
  253. if (ex is ExceptionEx)
  254. {
  255. throw;
  256. }
  257. else
  258. {
  259. throw ExceptionEx.ThrowBusinessException(ex);
  260. }
  261. }
  262. }
  263. #endregion
  264. #region 单据编码处理
  265. /// <summary>
  266. /// 获取当前编号规则种子列表
  267. /// </summary>
  268. /// <param name="ruleId">编号规则主键</param>
  269. /// <returns></returns>
  270. public List<CodeRuleSeedEntity> GetSeedList(string ruleId, UserInfo userInfo)
  271. {
  272. try
  273. {
  274. List<CodeRuleSeedEntity> codeRuleSeedList = cache.Read<List<CodeRuleSeedEntity>>(cacheKeySeed + ruleId, CacheId.codeRule);
  275. if (codeRuleSeedList == null) {
  276. codeRuleSeedList = codeRuleService.GetSeedList(ruleId, userInfo);
  277. cache.Write<List<CodeRuleSeedEntity>>(cacheKeySeed + ruleId, codeRuleSeedList, CacheId.codeRule);
  278. }
  279. return codeRuleSeedList;
  280. }
  281. catch (Exception ex)
  282. {
  283. if (ex is ExceptionEx)
  284. {
  285. throw;
  286. }
  287. else
  288. {
  289. throw ExceptionEx.ThrowBusinessException(ex);
  290. }
  291. }
  292. }
  293. /// <summary>
  294. /// 保存单据编号规则种子
  295. /// </summary>
  296. /// <param name="keyValue">主键</param>
  297. /// <param name="codeRuleSeedEntity">种子实体</param>
  298. public void SaveSeed(string keyValue, CodeRuleSeedEntity codeRuleSeedEntity, UserInfo userInfo)
  299. {
  300. try
  301. {
  302. codeRuleService.SaveSeed(keyValue, codeRuleSeedEntity,userInfo);
  303. cache.Remove(cacheKeySeed + codeRuleSeedEntity.F_RuleId, CacheId.codeRule);
  304. }
  305. catch (Exception ex)
  306. {
  307. if (ex is ExceptionEx)
  308. {
  309. throw;
  310. }
  311. else
  312. {
  313. throw ExceptionEx.ThrowBusinessException(ex);
  314. }
  315. }
  316. }
  317. /// <summary>
  318. /// 获得指定模块或者编号的单据号
  319. /// </summary>
  320. /// <param name="enCode">编码</param>
  321. /// <param name="userId">用户ID</param>
  322. /// <returns>单据号</returns>
  323. public string GetBillCode(string enCode,string userId = "")
  324. {
  325. try
  326. {
  327. string billCode = ""; //单据号
  328. string nextBillCode = "";//单据号
  329. bool isOutTime = false; //是否已过期
  330. CodeRuleEntity coderuleentity = GetEntityByCode(enCode);
  331. if (coderuleentity != null)
  332. {
  333. UserInfo userInfo = null;
  334. if (string.IsNullOrEmpty(userId))
  335. {
  336. userInfo = LoginUserInfo.Get();
  337. }
  338. else {
  339. UserEntity userEntity = userIBLL.GetEntityByUserId(userId);
  340. userInfo = new UserInfo
  341. {
  342. userId = userEntity.F_UserId,
  343. enCode = userEntity.F_EnCode,
  344. password = userEntity.F_Password,
  345. secretkey = userEntity.F_Secretkey,
  346. realName = userEntity.F_RealName,
  347. nickName = userEntity.F_NickName,
  348. headIcon = userEntity.F_HeadIcon,
  349. gender = userEntity.F_Gender,
  350. mobile = userEntity.F_Mobile,
  351. telephone = userEntity.F_Telephone,
  352. email = userEntity.F_Email,
  353. oICQ = userEntity.F_OICQ,
  354. weChat = userEntity.F_WeChat,
  355. companyId = userEntity.F_CompanyId,
  356. departmentId = userEntity.F_DepartmentId,
  357. openId = userEntity.F_OpenId,
  358. isSystem = userEntity.F_SecurityLevel == 1 ? true : false
  359. };
  360. }
  361. int nowSerious = 0;
  362. List<CodeRuleFormatModel> codeRuleFormatList = coderuleentity.F_RuleFormatJson.ToList<CodeRuleFormatModel>();
  363. string dateFormatStr = "";
  364. foreach (CodeRuleFormatModel codeRuleFormatEntity in codeRuleFormatList)
  365. {
  366. switch (codeRuleFormatEntity.itemType.ToString())
  367. {
  368. //自定义项
  369. case "0":
  370. billCode = billCode + codeRuleFormatEntity.formatStr;
  371. nextBillCode = nextBillCode + codeRuleFormatEntity.formatStr;
  372. break;
  373. //日期
  374. case "1":
  375. //日期字符串类型
  376. dateFormatStr = codeRuleFormatEntity.formatStr;
  377. billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.formatStr.Replace("m", "M"));
  378. nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.formatStr.Replace("m", "M"));
  379. break;
  380. //流水号
  381. case "2":
  382. CodeRuleSeedEntity maxSeed = null;
  383. CodeRuleSeedEntity codeRuleSeedEntity = null;
  384. List<CodeRuleSeedEntity> seedList = GetSeedList(coderuleentity.F_RuleId, userInfo);
  385. maxSeed = seedList.Find(t => t.F_UserId.IsEmpty());
  386. #region 处理流水号归0
  387. // 首先确定最大种子是否未归0的
  388. if (dateFormatStr.Contains("dd"))
  389. {
  390. if ((maxSeed.F_ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
  391. {
  392. isOutTime = true;
  393. nowSerious = 1;
  394. maxSeed.F_SeedValue = 2;
  395. maxSeed.F_ModifyDate = DateTime.Now;
  396. }
  397. }
  398. else if (dateFormatStr.Contains("mm"))
  399. {
  400. if (((DateTime)maxSeed.F_ModifyDate).ToString("yyyy-MM") != DateTime.Now.ToString("yyyy-MM"))
  401. {
  402. isOutTime = true;
  403. nowSerious = 1;
  404. maxSeed.F_SeedValue = 2;
  405. maxSeed.F_ModifyDate = DateTime.Now;
  406. }
  407. }
  408. else if (dateFormatStr.Contains("yy"))
  409. {
  410. if (((DateTime)maxSeed.F_ModifyDate).ToString("yyyy") != DateTime.Now.ToString("yyyy"))
  411. {
  412. isOutTime = true;
  413. nowSerious = 1;
  414. maxSeed.F_SeedValue = 2;
  415. maxSeed.F_ModifyDate = DateTime.Now;
  416. }
  417. }
  418. #endregion
  419. // 查找当前用户是否已有之前未用掉的种子做更新
  420. codeRuleSeedEntity = seedList.Find(t => t.F_UserId == userInfo.userId && t.F_RuleId == coderuleentity.F_RuleId&& (t.F_CreateDate).ToDateString() == DateTime.Now.ToString("yyyy-MM-dd"));
  421. string keyvalue = codeRuleSeedEntity == null ? "" : codeRuleSeedEntity.F_RuleSeedId;
  422. if (isOutTime)
  423. {
  424. SaveSeed(maxSeed.F_RuleSeedId, maxSeed, userInfo);
  425. }
  426. else if (codeRuleSeedEntity == null)
  427. {
  428. nowSerious = (int)maxSeed.F_SeedValue;
  429. maxSeed.F_SeedValue += 1;
  430. maxSeed.Modify(maxSeed.F_RuleSeedId, userInfo);
  431. SaveSeed(maxSeed.F_RuleSeedId, maxSeed, userInfo);
  432. }
  433. else
  434. {
  435. nowSerious = (int)codeRuleSeedEntity.F_SeedValue;
  436. }
  437. codeRuleSeedEntity = new CodeRuleSeedEntity();
  438. codeRuleSeedEntity.Create(userInfo);
  439. codeRuleSeedEntity.F_SeedValue = nowSerious;
  440. codeRuleSeedEntity.F_UserId = userInfo.userId;
  441. codeRuleSeedEntity.F_RuleId = coderuleentity.F_RuleId;
  442. SaveSeed(keyvalue, codeRuleSeedEntity, userInfo);
  443. // 最大种子已经过期
  444. string seriousStr = new string('0', (int)(codeRuleFormatEntity.formatStr.Length - nowSerious.ToString().Length)) + nowSerious.ToString();
  445. string NextSeriousStr = new string('0', (int)(codeRuleFormatEntity.formatStr.Length - nowSerious.ToString().Length)) + maxSeed.F_SeedValue.ToString();
  446. billCode = billCode + seriousStr;
  447. nextBillCode = nextBillCode + NextSeriousStr;
  448. break;
  449. //部门
  450. case "3":
  451. DepartmentEntity departmentEntity = departmentIBLL.GetEntity(userInfo.companyId, userInfo.departmentId);
  452. if (codeRuleFormatEntity.formatStr == "code")
  453. {
  454. billCode = billCode + departmentEntity.F_EnCode;
  455. nextBillCode = nextBillCode + departmentEntity.F_EnCode;
  456. }
  457. else
  458. {
  459. billCode = billCode + departmentEntity.F_FullName;
  460. nextBillCode = nextBillCode + departmentEntity.F_FullName;
  461. }
  462. break;
  463. //公司
  464. case "4":
  465. CompanyEntity companyEntity = companyIBLL.GetEntity(userInfo.companyId);
  466. if (codeRuleFormatEntity.formatStr == "code")
  467. {
  468. billCode = billCode + companyEntity.F_EnCode;
  469. nextBillCode = nextBillCode + companyEntity.F_EnCode;
  470. }
  471. else
  472. {
  473. billCode = billCode + companyEntity.F_FullName;
  474. nextBillCode = nextBillCode + companyEntity.F_FullName;
  475. }
  476. break;
  477. //用户
  478. case "5":
  479. if (codeRuleFormatEntity.formatStr == "code")
  480. {
  481. billCode = billCode + userInfo.enCode;
  482. nextBillCode = nextBillCode + userInfo.enCode;
  483. }
  484. else
  485. {
  486. billCode = billCode + userInfo.account;
  487. nextBillCode = nextBillCode + userInfo.account;
  488. }
  489. break;
  490. default:
  491. break;
  492. }
  493. }
  494. coderuleentity.F_CurrentNumber = nextBillCode;
  495. SaveEntity(coderuleentity.F_RuleId, coderuleentity, userInfo);
  496. }
  497. return billCode;
  498. }
  499. catch (Exception ex)
  500. {
  501. if (ex is ExceptionEx)
  502. {
  503. throw;
  504. }
  505. else
  506. {
  507. throw ExceptionEx.ThrowBusinessException(ex);
  508. }
  509. }
  510. }
  511. /// <summary>
  512. /// 占用单据号
  513. /// </summary>
  514. /// <param name="enCode">单据编码</param>
  515. /// <param name="userId">用户ID</param>
  516. /// <returns>true/false</returns>
  517. public void UseRuleSeed(string enCode,string userId = "")
  518. {
  519. try
  520. {
  521. CodeRuleEntity codeRuleSeedEntity = GetEntityByCode(enCode);
  522. if (codeRuleSeedEntity != null)
  523. {
  524. if (string.IsNullOrEmpty(userId))
  525. {
  526. UserInfo userInfo = LoginUserInfo.Get();
  527. //删除用户已经用掉的种子
  528. codeRuleService.DeleteSeed(userInfo.userId, codeRuleSeedEntity.F_RuleId);
  529. }
  530. else {
  531. codeRuleService.DeleteSeed(userId, codeRuleSeedEntity.F_RuleId);
  532. }
  533. cache.Remove(cacheKeySeed + codeRuleSeedEntity.F_RuleId, CacheId.codeRule);
  534. }
  535. }
  536. catch (Exception ex)
  537. {
  538. if (ex is ExceptionEx)
  539. {
  540. throw;
  541. }
  542. else
  543. {
  544. throw ExceptionEx.ThrowBusinessException(ex);
  545. }
  546. }
  547. }
  548. #endregion
  549. }
  550. }