123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- using LeaRun.Application.Entity.BaseManage;
- using LeaRun.Application.Entity.SystemManage;
- using LeaRun.Application.IService.SystemManage;
- using LeaRun.Data.Repository;
- using LeaRun.Util;
- using LeaRun.Util.Extension;
- using LeaRun.Util.WebControl;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LeaRun.Application.Service.SystemManage
- {
- /// <summary>
- /// 版 本 6.1
- /// Copyright (c) 2013-2016 上海力软信息技术有限公司
- /// 创建人:佘赐雄
- /// 日 期:2015.12.21 16:19
- /// 描 述:编号规则
- /// </summary>
- public class CodeRuleService : RepositoryFactory<CodeRuleEntity>, ICodeRuleService
- {
- #region 获取数据
- /// <summary>
- /// 规则列表
- /// </summary>
- /// <param name="pagination">分页</param>
- /// <param name="queryJson">查询参数</param>
- /// <returns></returns>
- public IEnumerable<CodeRuleEntity> GetPageList(Pagination pagination, string queryJson)
- {
- var expression = LinqExtensions.True<CodeRuleEntity>();
- var queryParam = queryJson.ToJObject();
- //查询条件
- if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
- {
- string condition = queryParam["condition"].ToString();
- string keyword = queryParam["keyword"].ToString();
- switch (condition)
- {
- case "EnCode": //对象编号
- expression = expression.And(t => t.EnCode.Contains(keyword));
- break;
- case "FullName": //对象名称
- expression = expression.And(t => t.FullName.Contains(keyword));
- break;
- default:
- break;
- }
- }
- return this.BaseRepository().FindList(expression, pagination);
- }
- /// <summary>
- /// 规则实体
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <returns></returns>
- public CodeRuleEntity GetEntity(string keyValue)
- {
- return this.BaseRepository().FindEntity(keyValue);
- }
- #endregion
- #region 提交数据
- /// <summary>
- /// 删除规则
- /// </summary>
- /// <param name="keyValue">主键</param>
- public void RemoveForm(string keyValue)
- {
- this.BaseRepository().Delete(keyValue);
- }
- /// <summary>
- /// 保存规则表单(新增、修改)
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <param name="codeRuleEntity">规则实体</param>
- /// <returns></returns>
- public void SaveForm(string keyValue, CodeRuleEntity codeRuleEntity)
- {
- if (!string.IsNullOrEmpty(keyValue))
- {
- codeRuleEntity.Modify(keyValue);
- this.BaseRepository().Update(codeRuleEntity);
- }
- else
- {
- codeRuleEntity.Create();
- this.BaseRepository().Insert(codeRuleEntity);
- }
- }
- #endregion
- #region 验证数据
- /// <summary>
- /// 规则编号不能重复
- /// </summary>
- /// <param name="enCode">编号</param>
- /// <param name="keyValue">主键</param>
- /// <returns></returns>
- public bool ExistEnCode(string enCode, string keyValue)
- {
- var expression = LinqExtensions.True<CodeRuleEntity>();
- expression = expression.And(t => t.EnCode == enCode);
- if (!string.IsNullOrEmpty(keyValue))
- {
- expression = expression.And(t => t.RuleId != keyValue);
- }
- return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
- }
- /// <summary>
- /// 规则名称不能重复
- /// </summary>
- /// <param name="fullName">名称</param>
- /// <param name="keyValue">主键</param>
- /// <returns></returns>
- public bool ExistFullName(string fullName, string keyValue)
- {
- var expression = LinqExtensions.True<CodeRuleEntity>();
- expression = expression.And(t => t.FullName == fullName);
- if (!string.IsNullOrEmpty(keyValue))
- {
- expression = expression.And(t => t.RuleId != keyValue);
- }
- return this.BaseRepository().IQueryable(expression).Count() == 0 ? true : false;
- }
- #endregion
- #region 单据编码处理
- /// <summary>
- /// 获得指定模块或者编号的单据号
- /// </summary>
- /// <param name="userId">用户ID</param>
- /// <param name="moduleId">模块ID</param>
- /// <param name="enCode">模板编码</param>
- /// <returns>单据号</returns>
- public string GetBillCode(string userId, string moduleId, string enCode)
- {
- IRepository db = new RepositoryFactory().BaseRepository();
- UserEntity userEntity = db.FindEntity<UserEntity>(userId);
- CodeRuleEntity coderuleentity = db.FindEntity<CodeRuleEntity>(t => t.ModuleId == moduleId || t.EnCode == enCode);
- //判断种子是否已经产生,如果没有产生种子先插入一条初始种子
- CodeRuleSeedEntity initSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId);
- if (initSeed == null)
- {
- initSeed = new CodeRuleSeedEntity();
- initSeed.Create();
- initSeed.SeedValue = 1;
- initSeed.RuleId = coderuleentity.RuleId;
- db.Insert<CodeRuleSeedEntity>(initSeed);
- }
- else
- {
- db = new RepositoryFactory().BaseRepository().BeginTrans();
- }
- //获得模板ID
- string billCode = "";//单据号
- string nextBillCode = "";//单据号
- bool isOutTime = false;//是否已过期
- if (coderuleentity != null)
- {
- try
- {
- int nowSerious = 0;
- //取得流水号种子
- List<CodeRuleSeedEntity> codeRuleSeedlist = db.IQueryable<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId).ToList();
- //取得最大种子
- CodeRuleSeedEntity maxSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.UserId == null);
- #region 处理隔天流水号归0
- //首先确定最大种子是否是隔天未归0的
- if ((maxSeed.ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
- {
- isOutTime = true;
- maxSeed.SeedValue = 1;
- maxSeed.ModifyDate = DateTime.Now;
- }
- #endregion
- List<CodeRuleFormatEntity> codeRuleFormatList = coderuleentity.RuleFormatJson.ToList<CodeRuleFormatEntity>();
- foreach (CodeRuleFormatEntity codeRuleFormatEntity in codeRuleFormatList)
- {
- switch (codeRuleFormatEntity.ItemType.ToString())
- {
- //自定义项
- case "0":
- billCode = billCode + codeRuleFormatEntity.FormatStr;
- nextBillCode = nextBillCode + codeRuleFormatEntity.FormatStr;
- break;
- //日期
- case "1":
- //日期字符串类型
- billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
- nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
- break;
- //流水号
- case "2":
- //查找当前用户是否已有之前未用掉的种子
- CodeRuleSeedEntity codeRuleSeedEntity = codeRuleSeedlist.Find(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
- //删除已过期的用户未用掉的种子
- if (codeRuleSeedEntity != null && isOutTime)
- {
- db.Delete<CodeRuleSeedEntity>(codeRuleSeedEntity);
- codeRuleSeedEntity = null;
- }
- //如果没有就取当前最大的种子
- if (codeRuleSeedEntity != null)
- {
- nowSerious = (int)codeRuleSeedEntity.SeedValue;
- }
- else
- {
- //取得系统最大的种子
- int maxSerious = (int)maxSeed.SeedValue;
- nowSerious = maxSerious;
- codeRuleSeedEntity = new CodeRuleSeedEntity();
- codeRuleSeedEntity.Create();
- codeRuleSeedEntity.SeedValue = maxSerious;
- codeRuleSeedEntity.UserId = userId;
- codeRuleSeedEntity.RuleId = coderuleentity.RuleId;
- db.Insert<CodeRuleSeedEntity>(codeRuleSeedEntity);
- //处理种子更新
- maxSeed.SeedValue += 1;
- maxSeed.Modify(maxSeed.RuleSeedId);
- db.Update<CodeRuleSeedEntity>(maxSeed);
- }
- string seriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + nowSerious.ToString();
- string NextSeriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + maxSeed.SeedValue.ToString();
- billCode = billCode + seriousStr;
- nextBillCode = nextBillCode + NextSeriousStr;
- break;
- //部门
- case "3":
- DepartmentEntity departmentEntity = db.FindEntity<DepartmentEntity>(userEntity.DepartmentId);
- if (codeRuleFormatEntity.FormatStr == "code")
- {
- billCode = billCode + departmentEntity.EnCode;
- nextBillCode = nextBillCode + departmentEntity.EnCode;
- }
- else
- {
- billCode = billCode + departmentEntity.FullName;
- nextBillCode = nextBillCode + departmentEntity.FullName;
- }
- break;
- //公司
- case "4":
- OrganizeEntity organizeEntity = db.FindEntity<OrganizeEntity>(userEntity.OrganizeId);
- if (codeRuleFormatEntity.FormatStr == "code")
- {
- billCode = billCode + organizeEntity.EnCode;
- nextBillCode = nextBillCode + organizeEntity.EnCode;
- }
- else
- {
- billCode = billCode + organizeEntity.FullName;
- nextBillCode = nextBillCode + organizeEntity.FullName;
- }
- break;
- //用户
- case "5":
- if (codeRuleFormatEntity.FormatStr == "code")
- {
- billCode = billCode + userEntity.EnCode;
- nextBillCode = nextBillCode + userEntity.EnCode;
- }
- else
- {
- billCode = billCode + userEntity.Account;
- nextBillCode = nextBillCode + userEntity.Account;
- }
- break;
- default:
- break;
- }
- }
- coderuleentity.CurrentNumber = nextBillCode;
- db.Update<CodeRuleEntity>(coderuleentity);
- }
- catch (Exception)
- {
- db.Rollback();
- return billCode;
- }
- db.Commit();
- }
- return billCode;
- }
- /// <summary>
- /// 获得指定模块或者编号的单据号(直接使用)
- /// </summary>
- /// <param name="userId">用户ID</param>
- /// <param name="moduleId">模块ID</param>
- /// <param name="enCode">模板编码</param>
- /// <returns>单据号</returns>
- public string SetBillCode(string userId, string moduleId, string enCode, IRepository db = null)
- {
- IRepository dbc = null;
- if (db == null)
- {
- dbc = new RepositoryFactory().BaseRepository();
- }
- else
- {
- dbc = db;
- }
- UserEntity userEntity = db.FindEntity<UserEntity>(userId);
- CodeRuleEntity coderuleentity = db.FindEntity<CodeRuleEntity>(t => t.ModuleId == moduleId || t.EnCode == enCode);
- //判断种子是否已经产生,如果没有产生种子先插入一条初始种子
- CodeRuleSeedEntity initSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId);
- if (initSeed == null)
- {
- initSeed = new CodeRuleSeedEntity();
- initSeed.Create();
- initSeed.SeedValue = 1;
- initSeed.RuleId = coderuleentity.RuleId;
- initSeed.CreateDate = null;
- //db.Insert<CodeRuleSeedEntity>(initSeed);
- }
- //获得模板ID
- string billCode = "";//单据号
- string nextBillCode = "";//单据号
- bool isOutTime = false;//是否已过期
- if (coderuleentity != null)
- {
- try
- {
- int nowSerious = 0;
- //取得流水号种子
- List<CodeRuleSeedEntity> codeRuleSeedlist = db.IQueryable<CodeRuleSeedEntity>(t => t.RuleId == coderuleentity.RuleId).ToList();
- //取得最大种子
- CodeRuleSeedEntity maxSeed = db.FindEntity<CodeRuleSeedEntity>(t => t.UserId == null);
- #region 处理隔天流水号归0
- //首先确定最大种子是否是隔天未归0的
- if ((maxSeed.ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd"))
- {
- isOutTime = true;
- maxSeed.SeedValue = 1;
- maxSeed.ModifyDate = DateTime.Now;
- }
- #endregion
- if (maxSeed == null)
- {
- maxSeed = initSeed;
- }
- List<CodeRuleFormatEntity> codeRuleFormatList = coderuleentity.RuleFormatJson.ToList<CodeRuleFormatEntity>();
- foreach (CodeRuleFormatEntity codeRuleFormatEntity in codeRuleFormatList)
- {
- switch (codeRuleFormatEntity.ItemType.ToString())
- {
- //自定义项
- case "0":
- billCode = billCode + codeRuleFormatEntity.FormatStr;
- nextBillCode = nextBillCode + codeRuleFormatEntity.FormatStr;
- break;
- //日期
- case "1":
- //日期字符串类型
- billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
- nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.FormatStr.Replace("m", "M"));
- break;
- //流水号
- case "2":
- //查找当前用户是否已有之前未用掉的种子
- CodeRuleSeedEntity codeRuleSeedEntity = codeRuleSeedlist.Find(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
- //删除已过期的用户未用掉的种子
- if (codeRuleSeedEntity != null && isOutTime)
- {
- db.Delete<CodeRuleSeedEntity>(codeRuleSeedEntity);
- codeRuleSeedEntity = null;
- }
- //如果没有就取当前最大的种子
- if (codeRuleSeedEntity == null)
- {
- //取得系统最大的种子
- int maxSerious = (int)maxSeed.SeedValue;
- nowSerious = maxSerious;
- codeRuleSeedEntity = new CodeRuleSeedEntity();
- codeRuleSeedEntity.Create();
- codeRuleSeedEntity.SeedValue = maxSerious;
- codeRuleSeedEntity.UserId = userId;
- codeRuleSeedEntity.RuleId = coderuleentity.RuleId;
- //db.Insert<CodeRuleSeedEntity>(codeRuleSeedEntity);
- //处理种子更新
- maxSeed.SeedValue += 1;
- if (maxSeed.CreateDate != null)
- {
- db.Update<CodeRuleSeedEntity>(maxSeed);
- }
- else
- {
- maxSeed.CreateDate = DateTime.Now;
- db.Insert<CodeRuleSeedEntity>(maxSeed);
- }
- }
- else
- {
- nowSerious = (int)codeRuleSeedEntity.SeedValue;
- }
- string seriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + nowSerious.ToString();
- string NextSeriousStr = new string('0', (int)(codeRuleFormatEntity.FormatStr.Length - nowSerious.ToString().Length)) + maxSeed.SeedValue.ToString();
- billCode = billCode + seriousStr;
- nextBillCode = nextBillCode + NextSeriousStr;
- break;
- //部门
- case "3":
- DepartmentEntity departmentEntity = db.FindEntity<DepartmentEntity>(userEntity.DepartmentId);
- if (codeRuleFormatEntity.FormatStr == "code")
- {
- billCode = billCode + departmentEntity.EnCode;
- nextBillCode = nextBillCode + departmentEntity.EnCode;
- }
- else
- {
- billCode = billCode + departmentEntity.FullName;
- nextBillCode = nextBillCode + departmentEntity.FullName;
- }
- break;
- //公司
- case "4":
- OrganizeEntity organizeEntity = db.FindEntity<OrganizeEntity>(userEntity.OrganizeId);
- if (codeRuleFormatEntity.FormatStr == "code")
- {
- billCode = billCode + organizeEntity.EnCode;
- nextBillCode = nextBillCode + organizeEntity.EnCode;
- }
- else
- {
- billCode = billCode + organizeEntity.FullName;
- nextBillCode = nextBillCode + organizeEntity.FullName;
- }
- break;
- //用户
- case "5":
- if (codeRuleFormatEntity.FormatStr == "code")
- {
- billCode = billCode + userEntity.EnCode;
- nextBillCode = nextBillCode + userEntity.EnCode;
- }
- else
- {
- billCode = billCode + userEntity.Account;
- nextBillCode = nextBillCode + userEntity.Account;
- }
- break;
- default:
- break;
- }
- }
- coderuleentity.CurrentNumber = nextBillCode;
- db.Update<CodeRuleEntity>(coderuleentity);
- }
- catch (Exception)
- {
- throw;
- }
- }
- return billCode;
- }
- /// <summary>
- /// 占用单据号
- /// </summary>
- /// <param name="userId">用户ID</param>
- /// <param name="moduleId">模块ID</param>
- /// <param name="enCode">模板编码</param>
- /// <returns>true/false</returns>
- public bool UseRuleSeed(string userId, string moduleId, string enCode, IRepository db = null)
- {
- IRepository dbc = null;
- if (db == null)
- {
- dbc = new RepositoryFactory().BaseRepository();
- }
- else
- {
- dbc = db;
- }
- UserEntity userEntity = dbc.FindEntity<UserEntity>(userId);
- CodeRuleEntity coderuleentity = dbc.FindEntity<CodeRuleEntity>(t => t.ModuleId == moduleId || t.EnCode == enCode);
- try
- {
- if (coderuleentity != null)
- {
- //删除用户已经用掉的种子
- dbc.Delete<CodeRuleSeedEntity>(t => t.UserId == userId && t.RuleId == coderuleentity.RuleId);
- }
- }
- catch (Exception)
- {
- return false;
- }
- return true;
- }
- #endregion
- }
- }
|