/// <summary> /// 保存单据编号规则种子 /// </summary> /// <param name="keyValue">主键</param> /// <param name="codeRuleSeedEntity">种子实体</param> public void SaveSeed(string keyValue, CodeRuleSeedEntity codeRuleSeedEntity, UserInfo userInfo) { try { if (string.IsNullOrEmpty(keyValue)) { codeRuleSeedEntity.Create(userInfo); this.BaseRepository().Insert(codeRuleSeedEntity); } else { codeRuleSeedEntity.Modify(keyValue, userInfo); this.BaseRepository().Update(codeRuleSeedEntity); } } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } }
/// <summary> /// 获得指定模块或者编号的单据号 /// </summary> /// <param name="enCode">编码</param> /// <param name="userId">用户ID</param> /// <returns>单据号</returns> public string GetBillCode(string enCode, string userId = "") { try { string billCode = ""; //单据号 string nextBillCode = ""; //单据号 bool isOutTime = false; //是否已过期 CodeRuleEntity coderuleentity = GetEntityByCode(enCode); if (coderuleentity != null) { UserInfo userInfo = null; if (string.IsNullOrEmpty(userId)) { userInfo = LoginUserInfo.Get(); } else { UserEntity userEntity = userIBLL.GetEntityByUserId(userId); userInfo = new UserInfo { userId = userEntity.F_UserId, enCode = userEntity.F_EnCode, password = userEntity.F_Password, secretkey = userEntity.F_Secretkey, realName = userEntity.F_RealName, nickName = userEntity.F_NickName, headIcon = userEntity.F_HeadIcon, gender = userEntity.F_Gender, mobile = userEntity.F_Mobile, telephone = userEntity.F_Telephone, email = userEntity.F_Email, oICQ = userEntity.F_OICQ, weChat = userEntity.F_WeChat, companyId = userEntity.F_CompanyId, departmentId = userEntity.F_DepartmentId, openId = userEntity.F_OpenId, isSystem = userEntity.F_SecurityLevel == 1 ? true : false }; } int nowSerious = 0; List <CodeRuleFormatModel> codeRuleFormatList = coderuleentity.F_RuleFormatJson.ToList <CodeRuleFormatModel>(); string dateFormatStr = ""; foreach (CodeRuleFormatModel codeRuleFormatEntity in codeRuleFormatList) { switch (codeRuleFormatEntity.itemType.ToString()) { //自定义项 case "0": billCode = billCode + codeRuleFormatEntity.formatStr; nextBillCode = nextBillCode + codeRuleFormatEntity.formatStr; break; //日期 case "1": //日期字符串类型 dateFormatStr = codeRuleFormatEntity.formatStr; billCode = billCode + DateTime.Now.ToString(codeRuleFormatEntity.formatStr.Replace("m", "M")); nextBillCode = nextBillCode + DateTime.Now.ToString(codeRuleFormatEntity.formatStr.Replace("m", "M")); break; //流水号 case "2": CodeRuleSeedEntity maxSeed = null; CodeRuleSeedEntity codeRuleSeedEntity = null; List <CodeRuleSeedEntity> seedList = GetSeedList(coderuleentity.F_RuleId, userInfo); maxSeed = seedList.Find(t => t.F_UserId.IsEmpty()); #region 处理流水号归0 // 首先确定最大种子是否未归0的 if (dateFormatStr.Contains("dd")) { if ((maxSeed.F_ModifyDate).ToDateString() != DateTime.Now.ToString("yyyy-MM-dd")) { isOutTime = true; nowSerious = 1; maxSeed.F_SeedValue = 2; maxSeed.F_ModifyDate = DateTime.Now; } } else if (dateFormatStr.Contains("mm")) { if (((DateTime)maxSeed.F_ModifyDate).ToString("yyyy-MM") != DateTime.Now.ToString("yyyy-MM")) { isOutTime = true; nowSerious = 1; maxSeed.F_SeedValue = 2; maxSeed.F_ModifyDate = DateTime.Now; } } else if (dateFormatStr.Contains("yy")) { if (((DateTime)maxSeed.F_ModifyDate).ToString("yyyy") != DateTime.Now.ToString("yyyy")) { isOutTime = true; nowSerious = 1; maxSeed.F_SeedValue = 2; maxSeed.F_ModifyDate = DateTime.Now; } } #endregion // 查找当前用户是否已有之前未用掉的种子做更新 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")); string keyvalue = codeRuleSeedEntity == null ? "" : codeRuleSeedEntity.F_RuleSeedId; if (isOutTime) { SaveSeed(maxSeed.F_RuleSeedId, maxSeed, userInfo); } else if (codeRuleSeedEntity == null) { nowSerious = (int)maxSeed.F_SeedValue; maxSeed.F_SeedValue += 1; maxSeed.Modify(maxSeed.F_RuleSeedId, userInfo); SaveSeed(maxSeed.F_RuleSeedId, maxSeed, userInfo); } else { nowSerious = (int)codeRuleSeedEntity.F_SeedValue; } codeRuleSeedEntity = new CodeRuleSeedEntity(); codeRuleSeedEntity.Create(userInfo); codeRuleSeedEntity.F_SeedValue = nowSerious; codeRuleSeedEntity.F_UserId = userInfo.userId; codeRuleSeedEntity.F_RuleId = coderuleentity.F_RuleId; SaveSeed(keyvalue, codeRuleSeedEntity, userInfo); // 最大种子已经过期 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.F_SeedValue.ToString(); billCode = billCode + seriousStr; nextBillCode = nextBillCode + NextSeriousStr; break; //部门 case "3": DepartmentEntity departmentEntity = departmentIBLL.GetEntity(userInfo.companyId, userInfo.departmentId); if (codeRuleFormatEntity.formatStr == "code") { billCode = billCode + departmentEntity.F_EnCode; nextBillCode = nextBillCode + departmentEntity.F_EnCode; } else { billCode = billCode + departmentEntity.F_FullName; nextBillCode = nextBillCode + departmentEntity.F_FullName; } break; //公司 case "4": CompanyEntity companyEntity = companyIBLL.GetEntity(userInfo.companyId); if (codeRuleFormatEntity.formatStr == "code") { billCode = billCode + companyEntity.F_EnCode; nextBillCode = nextBillCode + companyEntity.F_EnCode; } else { billCode = billCode + companyEntity.F_FullName; nextBillCode = nextBillCode + companyEntity.F_FullName; } break; //用户 case "5": if (codeRuleFormatEntity.formatStr == "code") { billCode = billCode + userInfo.enCode; nextBillCode = nextBillCode + userInfo.enCode; } else { billCode = billCode + userInfo.account; nextBillCode = nextBillCode + userInfo.account; } break; default: break; } } coderuleentity.F_CurrentNumber = nextBillCode; SaveEntity(coderuleentity.F_RuleId, coderuleentity, userInfo); } return(billCode); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } }