コード例 #1
0
        /// <summary>
        /// 生成规则
        /// </summary>
        /// <param name="codeRule"></param>
        /// <returns></returns>
        private string GenerateCodeRule(EnumCodeRule codeRule)
        {
            string codeRuleString = string.Empty;

            switch (codeRule)
            {
            case EnumCodeRule.Year:
                codeRuleString = "{time:yyyy}";
                break;

            case EnumCodeRule.YearMonth:
                codeRuleString = "{time:yyyyMM}";
                break;

            case EnumCodeRule.YearMonthDay:
                codeRuleString = "{time:yyyyMMdd}";
                break;

            //case EnumCodeRule.YearMonthDayH:
            //    codeRuleString = "{time:yyyyMMddHH}";
            //    break;
            case EnumCodeRule.None:
                codeRuleString = "0";
                break;
            }
            return(codeRuleString);
        }
コード例 #2
0
        /// <summary>
        /// 生成编号
        /// </summary>
        /// <param name="key">编码键</param>
        /// <param name="generateRule">规则</param>
        /// <param name="prefix">前缀</param>
        /// <param name="serialNumberLength">流水号长度</param>
        /// <param name="organizationID">机构ID</param>
        /// <returns></returns>
        public string GenerateCode(EnumCodeKey key, EnumCodeRule generateRule, string prefix, int serialNumberLength, int organizationID = -1)
        {
            IUnitOfWork          codeRuleUnitOfWork = IOCContainer.Instance.Resolve <IUnitOfWork>();
            var                  codeRuleRepository = codeRuleUnitOfWork.GetRepository <SYS_CodeRule>();
            BaseRequest <object> request            = new BaseRequest <object>();
            //BaseResponse<CodeRuleModel> response = null;
            string codeKey  = key.ToString();
            var    codeRule = codeRuleRepository.dbSet.Where(it => it.OrganizationID == organizationID && it.CodeKey == codeKey).FirstOrDefault();

            //response = this.Get<SYS_CodeRule, CodeRuleModel>((q) => q.OrganizationID == organizationID && q.CodeKey == key.ToString());

            if (codeRule == null)
            {
                codeRule = new SYS_CodeRule();
                codeRule.OrganizationID = organizationID;
                codeRule.CodeKey        = Enum.GetName(typeof(EnumCodeKey), key);
                codeRule.FlagRule       = this.GenerateCodeRule(generateRule);
                codeRule.GenerateRule   = prefix + (codeRule.FlagRule != "0" ? codeRule.FlagRule : "") + "{number:" + serialNumberLength + "}";
                codeRule.Flag           = this.ReplaceValue(codeRule.FlagRule);
                codeRule.SerialNumber   = 0;
                codeRuleRepository.Insert(codeRule);
            }
            else
            {
                codeRuleRepository.Update(codeRule);
            }
            var flag = this.ReplaceValue(codeRule.FlagRule);

            if (string.IsNullOrEmpty(codeRule.Flag) || codeRule.Flag != flag)
            {
                codeRule.Flag         = flag;
                codeRule.SerialNumber = 1;
            }
            else
            {
                codeRule.SerialNumber++;
            }
            codeRuleUnitOfWork.Save();
            //unitOfWork.BeginTransaction();
            //this.Save<SYS_CodeRule, CodeRuleModel>(response.Data, (q) => q.OrganizationID == organizationID && q.CodeKey == response.Data.CodeKey);
            //unitOfWork.Commit();
            return(ReplaceValue(codeRule.GenerateRule, codeRule.SerialNumber));
        }