Beispiel #1
0
        public string NewId(T saleOrder)
        {
            string result = saleOrder.GenerateIdPrefix();
            IList  list   = SessionFactory.GetCurrentSession().CreateCriteria(typeof(T))
                            .SetProjection(Projections.Max("Id"))
                            .Add(Expression.Like("Id", result + "%"))
                            .List();

            /*
             * 与原函数中的生成规则一致,只生成到ZZZ级别,往后没有再继续生成.(已可以满足需求)
             * select dbs.dbo.fn_mi_GetNext('SorderID','D55')
             */
            string seed = "";

            if (list[0] != null)
            {
                string currentMaxId = list[0].ToString();
                seed = currentMaxId.Substring(currentMaxId.Length - 1);
                if (seed == "Z")
                {
                    seed = "";
                    IList list2 = SessionFactory.GetCurrentSession().CreateCriteria(typeof(T))
                                  .SetProjection(Projections.Max("Id"))
                                  .Add(Expression.Like("Id", result + "[A-Z][A-Z]"))
                                  .List();

                    if (list2[0] != null)
                    {
                        currentMaxId = list2[0].ToString();
                        seed         = currentMaxId.Substring(currentMaxId.Length - 2);
                        if (seed == "ZZ")
                        {
                            seed = "";
                            IList list3 = SessionFactory.GetCurrentSession().CreateCriteria(typeof(T))
                                          .SetProjection(Projections.Max("Id"))
                                          .Add(Expression.Like("Id", result + "[A-Z][A-Z][A-Z]"))
                                          .List();

                            if (list3[0] != null)
                            {
                                currentMaxId = list3[0].ToString();
                                seed         = currentMaxId.Substring(currentMaxId.Length - 3);

                                if (seed == "ZZZ")
                                {
                                    result = result + "AAAA";
                                }
                                else
                                {
                                    result += IdIncrease.StringIncrease(seed);
                                }
                            }
                            else
                            {
                                result = result + "AAA";
                            }
                        }
                        else
                        {
                            result += IdIncrease.StringIncrease(seed);
                        }
                    }
                    else
                    {
                        result = result + "AA";
                    }
                }
                else
                {
                    result += IdIncrease.StringIncrease(seed);
                }
            }
            else
            {
                result += IdIncrease.StringIncrease(seed);
            }

            return(result);
        }