CodeRuleService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. using LeaRun.Application.Entity.BaseManage;
  2. using LeaRun.Application.Entity.SystemManage;
  3. using LeaRun.Application.IService.SystemManage;
  4. using LeaRun.Data.Repository;
  5. using LeaRun.Util;
  6. using LeaRun.Util.Extension;
  7. using LeaRun.Util.WebControl;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. namespace LeaRun.Application.Service.SystemManage
  12. {
  13. /// <summary>
  14. /// 版 本 6.1
  15. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  16. /// 创建人:佘赐雄
  17. /// 日 期:2015.12.21 16:19
  18. /// 描 述:编号规则
  19. /// </summary>
  20. public class CodeRuleService : RepositoryFactory<CodeRuleEntity>, ICodeRuleService
  21. {
  22. #region 获取数据
  23. /// <summary>
  24. /// 规则列表
  25. /// </summary>
  26. /// <param name="pagination">分页</param>
  27. /// <param name="queryJson">查询参数</param>
  28. /// <returns></returns>
  29. public IEnumerable<CodeRuleEntity> GetPageList(Pagination pagination, string queryJson)
  30. {
  31. var expression = LinqExtensions.True<CodeRuleEntity>();
  32. var queryParam = queryJson.ToJObject();
  33. //查询条件
  34. if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
  35. {
  36. string condition = queryParam["condition"].ToString();
  37. string keyword = queryParam["keyword"].ToString();
  38. switch (condition)
  39. {
  40. case "EnCode": //对象编号
  41. expression = expression.And(t => t.EnCode.Contains(keyword));
  42. break;
  43. case "FullName": //对象名称
  44. expression = expression.And(t => t.FullName.Contains(keyword));
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. return this.BaseRepository().FindList(expression, pagination);
  51. }
  52. /// <summary>
  53. /// 规则实体
  54. /// </summary>
  55. /// <param name="keyValue">主键值</param>
  56. /// <returns></returns>
  57. public CodeRuleEntity GetEntity(string keyValue)
  58. {
  59. return this.BaseRepository().FindEntity(keyValue);
  60. }
  61. #endregion
  62. #region 提交数据
  63. /// <summary>
  64. /// 删除规则
  65. /// </summary>
  66. /// <param name="keyValue">主键</param>
  67. public void RemoveForm(string keyValue)
  68. {
  69. this.BaseRepository().Delete(keyValue);
  70. }
  71. /// <summary>
  72. /// 保存规则表单(新增、修改)
  73. /// </summary>
  74. /// <param name="keyValue">主键值</param>
  75. /// <param name="codeRuleEntity">规则实体</param>
  76. /// <returns></returns>
  77. public void SaveForm(string keyValue, CodeRuleEntity codeRuleEntity)
  78. {
  79. if (!string.IsNullOrEmpty(keyValue))
  80. {
  81. codeRuleEntity.Modify(keyValue);
  82. this.BaseRepository().Update(codeRuleEntity);
  83. }
  84. else
  85. {
  86. codeRuleEntity.Create();
  87. this.BaseRepository().Insert(codeRuleEntity);
  88. }
  89. }
  90. #endregion
  91. #region 验证数据
  92. /// <summary>
  93. /// 规则编号不能重复
  94. /// </summary>
  95. /// <param name="enCode">编号</param>
  96. /// <param name="keyValue">主键</param>
  97. /// <returns></returns>
  98. public bool ExistEnCode(string enCode, string keyValue)
  99. {
  100. var expression = LinqExtensions.True<CodeRuleEntity>();
  101. expression = expression.And(t => t.EnCode == enCode);
  102. if (!string.IsNullOrEmpty(keyValue))
  103. {
  104. expression = expression.And(t => t.RuleId != keyValue);
  105. }
  106. return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
  107. }
  108. /// <summary>
  109. /// 规则名称不能重复
  110. /// </summary>
  111. /// <param name="fullName">名称</param>
  112. /// <param name="keyValue">主键</param>
  113. /// <returns></returns>
  114. public bool ExistFullName(string fullName, string keyValue)
  115. {
  116. var expression = LinqExtensions.True<CodeRuleEntity>();
  117. expression = expression.And(t => t.FullName == fullName);
  118. if (!string.IsNullOrEmpty(keyValue))
  119. {
  120. expression = expression.And(t => t.RuleId != keyValue);
  121. }
  122. return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
  123. }
  124. #endregion
  125. #region 单据编码处理
  126. /// <summary>
  127. /// 获得指定模块或者编号的单据号
  128. /// </summary>
  129. /// <param name="userId">用户ID</param>
  130. /// <param name="moduleId">模块ID</param>
  131. /// <param name="enCode">模板编码</param>
  132. /// <returns>单据号</returns>
  133. public string GetBillCode(string userId, string moduleId, string enCode)
  134. {
  135. IRepository db = new RepositoryFactory().BaseRepository();
  136. UserEntity userEntity = db.FindEntity<UserEntity>(userId);
  137. CodeRuleEntity coderuleentity = db.FindEntity<CodeRuleEntity>(t => t.ModuleId == moduleId || t.EnCode == enCode);
  138. //判断种子是否已经产生,如果没有产生种子先插入一条初始种子
  139. CodeRuleSeedEntity initSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId);
  140. if (initSeed == null)
  141. {
  142. initSeed = new CodeRuleSeedEntity();
  143. initSeed.Create();
  144. initSeed.SeedValue = 1;
  145. initSeed.RuleId = coderuleentity.RuleId;
  146. db.Insert<CodeRuleSeedEntity>(initSeed);
  147. }
  148. else
  149. {
  150. db = new RepositoryFactory().BaseRepository().BeginTrans();
  151. }
  152. //获得模板ID
  153. string billCode = "";//单据号
  154. string nextBillCode = "";//单据号
  155. bool isOutTime = false;//是否已过期
  156. if (coderuleentity != null)
  157. {
  158. try
  159. {
  160. int nowSerious = 0;
  161. //取得流水号种子
  162. List<CodeRuleSeedEntity> codeRuleSeedlist = db.IQueryable<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId).ToList();
  163. //取得最大种子
  164. CodeRuleSeedEntity maxSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.UserId == null);
  165. #region 处理隔天流水号归0
  166. //首先确定最大种子是否是隔天未归0的
  167. if ((maxSeed.ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
  168. {
  169. isOutTime = true;
  170. maxSeed.SeedValue = 1;
  171. maxSeed.ModifyDate = DateTime.Now;
  172. }
  173. #endregion
  174. List<CodeRuleFormatEntity> codeRuleFormatList = coderuleentity.RuleFormatJson.ToList<CodeRuleFormatEntity>();
  175. foreach (CodeRuleFormatEntity codeRuleFormatEntity in codeRuleFormatList)
  176. {
  177. switch (codeRuleFormatEntity.ItemType.ToString())
  178. {
  179. //自定义项
  180. case "0":
  181. billCode = billCode + codeRuleFormatEntity.FormatStr;
  182. nextBillCode = nextBillCode + codeRuleFormatEntity.FormatStr;
  183. break;
  184. //日期
  185. case "1":
  186. //日期字符串类型
  187. billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
  188. nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
  189. break;
  190. //流水号
  191. case "2":
  192. //查找当前用户是否已有之前未用掉的种子
  193. CodeRuleSeedEntity codeRuleSeedEntity = codeRuleSeedlist.Find(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
  194. //删除已过期的用户未用掉的种子
  195. if (codeRuleSeedEntity != null && isOutTime)
  196. {
  197. db.Delete<CodeRuleSeedEntity>(codeRuleSeedEntity);
  198. codeRuleSeedEntity = null;
  199. }
  200. //如果没有就取当前最大的种子
  201. if (codeRuleSeedEntity != null)
  202. {
  203. nowSerious = (int)codeRuleSeedEntity.SeedValue;
  204. }
  205. else
  206. {
  207. //取得系统最大的种子
  208. int maxSerious = (int)maxSeed.SeedValue;
  209. nowSerious = maxSerious;
  210. codeRuleSeedEntity = new CodeRuleSeedEntity();
  211. codeRuleSeedEntity.Create();
  212. codeRuleSeedEntity.SeedValue = maxSerious;
  213. codeRuleSeedEntity.UserId = userId;
  214. codeRuleSeedEntity.RuleId = coderuleentity.RuleId;
  215. db.Insert<CodeRuleSeedEntity>(codeRuleSeedEntity);
  216. //处理种子更新
  217. maxSeed.SeedValue += 1;
  218. maxSeed.Modify(maxSeed.RuleSeedId);
  219. db.Update<CodeRuleSeedEntity>(maxSeed);
  220. }
  221. string seriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + nowSerious.ToString();
  222. string NextSeriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + maxSeed.SeedValue.ToString();
  223. billCode = billCode + seriousStr;
  224. nextBillCode = nextBillCode + NextSeriousStr;
  225. break;
  226. //部门
  227. case "3":
  228. DepartmentEntity departmentEntity = db.FindEntity<DepartmentEntity>(userEntity.DepartmentId);
  229. if (codeRuleFormatEntity.FormatStr == "code")
  230. {
  231. billCode = billCode + departmentEntity.EnCode;
  232. nextBillCode = nextBillCode + departmentEntity.EnCode;
  233. }
  234. else
  235. {
  236. billCode = billCode + departmentEntity.FullName;
  237. nextBillCode = nextBillCode + departmentEntity.FullName;
  238. }
  239. break;
  240. //公司
  241. case "4":
  242. OrganizeEntity organizeEntity = db.FindEntity<OrganizeEntity>(userEntity.OrganizeId);
  243. if (codeRuleFormatEntity.FormatStr == "code")
  244. {
  245. billCode = billCode + organizeEntity.EnCode;
  246. nextBillCode = nextBillCode + organizeEntity.EnCode;
  247. }
  248. else
  249. {
  250. billCode = billCode + organizeEntity.FullName;
  251. nextBillCode = nextBillCode + organizeEntity.FullName;
  252. }
  253. break;
  254. //用户
  255. case "5":
  256. if (codeRuleFormatEntity.FormatStr == "code")
  257. {
  258. billCode = billCode + userEntity.EnCode;
  259. nextBillCode = nextBillCode + userEntity.EnCode;
  260. }
  261. else
  262. {
  263. billCode = billCode + userEntity.Account;
  264. nextBillCode = nextBillCode + userEntity.Account;
  265. }
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. coderuleentity.CurrentNumber = nextBillCode;
  272. db.Update<CodeRuleEntity>(coderuleentity);
  273. }
  274. catch (Exception)
  275. {
  276. db.Rollback();
  277. return billCode;
  278. }
  279. db.Commit();
  280. }
  281. return billCode;
  282. }
  283. /// <summary>
  284. /// 获得指定模块或者编号的单据号(直接使用)
  285. /// </summary>
  286. /// <param name="userId">用户ID</param>
  287. /// <param name="moduleId">模块ID</param>
  288. /// <param name="enCode">模板编码</param>
  289. /// <returns>单据号</returns>
  290. public string SetBillCode(string userId, string moduleId, string enCode, IRepository db = null)
  291. {
  292. IRepository dbc = null;
  293. if (db == null)
  294. {
  295. dbc = new RepositoryFactory().BaseRepository();
  296. }
  297. else
  298. {
  299. dbc = db;
  300. }
  301. UserEntity userEntity = db.FindEntity<UserEntity>(userId);
  302. CodeRuleEntity coderuleentity = db.FindEntity<CodeRuleEntity>(t => t.ModuleId == moduleId || t.EnCode == enCode);
  303. //判断种子是否已经产生,如果没有产生种子先插入一条初始种子
  304. CodeRuleSeedEntity initSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId);
  305. if (initSeed == null)
  306. {
  307. initSeed = new CodeRuleSeedEntity();
  308. initSeed.Create();
  309. initSeed.SeedValue = 1;
  310. initSeed.RuleId = coderuleentity.RuleId;
  311. initSeed.CreateDate = null;
  312. //db.Insert<CodeRuleSeedEntity>(initSeed);
  313. }
  314. //获得模板ID
  315. string billCode = "";//单据号
  316. string nextBillCode = "";//单据号
  317. bool isOutTime = false;//是否已过期
  318. if (coderuleentity != null)
  319. {
  320. try
  321. {
  322. int nowSerious = 0;
  323. //取得流水号种子
  324. List<CodeRuleSeedEntity> codeRuleSeedlist = db.IQueryable<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId).ToList();
  325. //取得最大种子
  326. CodeRuleSeedEntity maxSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.UserId == null);
  327. #region 处理隔天流水号归0
  328. //首先确定最大种子是否是隔天未归0的
  329. if ((maxSeed.ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
  330. {
  331. isOutTime = true;
  332. maxSeed.SeedValue = 1;
  333. maxSeed.ModifyDate = DateTime.Now;
  334. }
  335. #endregion
  336. if (maxSeed == null)
  337. {
  338. maxSeed = initSeed;
  339. }
  340. List<CodeRuleFormatEntity> codeRuleFormatList = coderuleentity.RuleFormatJson.ToList<CodeRuleFormatEntity>();
  341. foreach (CodeRuleFormatEntity codeRuleFormatEntity in codeRuleFormatList)
  342. {
  343. switch (codeRuleFormatEntity.ItemType.ToString())
  344. {
  345. //自定义项
  346. case "0":
  347. billCode = billCode + codeRuleFormatEntity.FormatStr;
  348. nextBillCode = nextBillCode + codeRuleFormatEntity.FormatStr;
  349. break;
  350. //日期
  351. case "1":
  352. //日期字符串类型
  353. billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
  354. nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
  355. break;
  356. //流水号
  357. case "2":
  358. //查找当前用户是否已有之前未用掉的种子
  359. CodeRuleSeedEntity codeRuleSeedEntity = codeRuleSeedlist.Find(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
  360. //删除已过期的用户未用掉的种子
  361. if (codeRuleSeedEntity != null && isOutTime)
  362. {
  363. db.Delete<CodeRuleSeedEntity>(codeRuleSeedEntity);
  364. codeRuleSeedEntity = null;
  365. }
  366. //如果没有就取当前最大的种子
  367. if (codeRuleSeedEntity == null)
  368. {
  369. //取得系统最大的种子
  370. int maxSerious = (int)maxSeed.SeedValue;
  371. nowSerious = maxSerious;
  372. codeRuleSeedEntity = new CodeRuleSeedEntity();
  373. codeRuleSeedEntity.Create();
  374. codeRuleSeedEntity.SeedValue = maxSerious;
  375. codeRuleSeedEntity.UserId = userId;
  376. codeRuleSeedEntity.RuleId = coderuleentity.RuleId;
  377. //db.Insert<CodeRuleSeedEntity>(codeRuleSeedEntity);
  378. //处理种子更新
  379. maxSeed.SeedValue += 1;
  380. if (maxSeed.CreateDate != null)
  381. {
  382. db.Update<CodeRuleSeedEntity>(maxSeed);
  383. }
  384. else
  385. {
  386. maxSeed.CreateDate = DateTime.Now;
  387. db.Insert<CodeRuleSeedEntity>(maxSeed);
  388. }
  389. }
  390. else
  391. {
  392. nowSerious = (int)codeRuleSeedEntity.SeedValue;
  393. }
  394. string seriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + nowSerious.ToString();
  395. string NextSeriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + maxSeed.SeedValue.ToString();
  396. billCode = billCode + seriousStr;
  397. nextBillCode = nextBillCode + NextSeriousStr;
  398. break;
  399. //部门
  400. case "3":
  401. DepartmentEntity departmentEntity = db.FindEntity<DepartmentEntity>(userEntity.DepartmentId);
  402. if (codeRuleFormatEntity.FormatStr == "code")
  403. {
  404. billCode = billCode + departmentEntity.EnCode;
  405. nextBillCode = nextBillCode + departmentEntity.EnCode;
  406. }
  407. else
  408. {
  409. billCode = billCode + departmentEntity.FullName;
  410. nextBillCode = nextBillCode + departmentEntity.FullName;
  411. }
  412. break;
  413. //公司
  414. case "4":
  415. OrganizeEntity organizeEntity = db.FindEntity<OrganizeEntity>(userEntity.OrganizeId);
  416. if (codeRuleFormatEntity.FormatStr == "code")
  417. {
  418. billCode = billCode + organizeEntity.EnCode;
  419. nextBillCode = nextBillCode + organizeEntity.EnCode;
  420. }
  421. else
  422. {
  423. billCode = billCode + organizeEntity.FullName;
  424. nextBillCode = nextBillCode + organizeEntity.FullName;
  425. }
  426. break;
  427. //用户
  428. case "5":
  429. if (codeRuleFormatEntity.FormatStr == "code")
  430. {
  431. billCode = billCode + userEntity.EnCode;
  432. nextBillCode = nextBillCode + userEntity.EnCode;
  433. }
  434. else
  435. {
  436. billCode = billCode + userEntity.Account;
  437. nextBillCode = nextBillCode + userEntity.Account;
  438. }
  439. break;
  440. default:
  441. break;
  442. }
  443. }
  444. coderuleentity.CurrentNumber = nextBillCode;
  445. db.Update<CodeRuleEntity>(coderuleentity);
  446. }
  447. catch (Exception)
  448. {
  449. throw;
  450. }
  451. }
  452. return billCode;
  453. }
  454. /// <summary>
  455. /// 占用单据号
  456. /// </summary>
  457. /// <param name="userId">用户ID</param>
  458. /// <param name="moduleId">模块ID</param>
  459. /// <param name="enCode">模板编码</param>
  460. /// <returns>true/false</returns>
  461. public bool UseRuleSeed(string userId, string moduleId, string enCode, IRepository db = null)
  462. {
  463. IRepository dbc = null;
  464. if (db == null)
  465. {
  466. dbc = new RepositoryFactory().BaseRepository();
  467. }
  468. else
  469. {
  470. dbc = db;
  471. }
  472. UserEntity userEntity = dbc.FindEntity<UserEntity>(userId);
  473. CodeRuleEntity coderuleentity = dbc.FindEntity<CodeRuleEntity>(t => t.ModuleId == moduleId || t.EnCode == enCode);
  474. try
  475. {
  476. if (coderuleentity != null)
  477. {
  478. //删除用户已经用掉的种子
  479. dbc.Delete<CodeRuleSeedEntity>(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
  480. }
  481. }
  482. catch (Exception)
  483. {
  484. return false;
  485. }
  486. return true;
  487. }
  488. #endregion
  489. }
  490. }