/// <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>单据号</returns> public string GetBillCodeNologin(string userId, string moduleId, string enCode) { IRepository db = new RepositoryFactory().BaseRepository(); 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; default: break; } } coderuleentity.CurrentNumber = nextBillCode; db.Update <CodeRuleEntity>(coderuleentity); } catch (Exception) { db.Rollback(); return(billCode); } db.Commit(); } return(billCode); }