Exemplo n.º 1
0
    protected void cmdDelete_Click(object sender, EventArgs e)
    {
        LinkButton cmd = sender as LinkButton;

        if (cmd == null)
        {
            return;
        }
        int defId = Magic.Framework.Utils.Cast.Int(cmd.Attributes["defId"], 0);

        if (defId <= 0)
        {
            return;
        }
        using (ISession session = new Session())
        {
            try
            {
                session.BeginTransaction();
                OrderRuleDef.Retrieve(session, defId).Delete(session);
                session.Commit();
                this.QueryAndBindData(session);
            }
            catch (Exception er)
            {
                session.Rollback();
                WebUtil.ShowError(this, er);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtRuleId.Value = WebUtil.Param("defId");

            this.drpTimeStampPrecision.Items.Clear();
            this.drpTimeStampPrecision.Items.Add(new ListItem("", ""));
            this.drpTimeStampPrecision.Items.Add(new ListItem("日", OrderRuleDefPrecision.Date.ToString()));
            this.drpTimeStampPrecision.Items.Add(new ListItem("月", OrderRuleDefPrecision.Month.ToString()));
            this.drpTimeStampPrecision.Items.Add(new ListItem("年", OrderRuleDefPrecision.Year.ToString()));

            if (!this.IsNew)
            {
                using (ISession session = new Session())
                {
                    OrderRuleDef def = OrderRuleDef.Retrieve(session, this.RuleDefId);
                    if (def != null)
                    {
                        this.txtText.Text = def.RuleDefineText;
                        if (def.UsePrefix)
                        {
                            this.drpUsePrefix.SelectedValue = "1";
                            this.txtPrefix.Text             = def.PrefixValue;
                        }
                        if (def.UseTimeStamp)
                        {
                            this.drpUseTimeStamp.SelectedValue = "1";
                            if (def.TimeStampPrecision == OrderRuleDefPrecision.Date || def.TimeStampPrecision == OrderRuleDefPrecision.Month || def.TimeStampPrecision == OrderRuleDefPrecision.Year)
                            {
                                this.drpTimeStampPrecision.SelectedValue = def.TimeStampPrecision.ToString();
                            }
                            this.txtTimeStampPattern.Text = def.TimeStampPattern;
                            this.lblPrevTimeStampVal.Text = def.PrevTimeStampValue.ToString("yyyy-MM-dd");
                        }
                        this.txtSerialLen.Text     = def.SerialLength.ToString();
                        this.lblPrevSerialVal.Text = def.PrevSerialValue.ToString();
                    }
                }
            }
        }
    }
    protected void MagicItemCommand(object sender, MagicItemEventArgs e)
    {
        if (e.CommandName == "Save")
        {
            using (ISession session = new Session())
            {
                OrderRuleDef def = null;
                if (this.IsNew)
                {
                    def = new OrderRuleDef();
                }
                else
                {
                    def = OrderRuleDef.Retrieve(session, this.RuleDefId);
                }
                def.RuleDefineText = this.txtText.Text.Trim();
                def.UsePrefix      = this.drpUsePrefix.SelectedValue == "1" ? true : false;
                if (def.UsePrefix)
                {
                    def.PrefixValue = this.txtPrefix.Text.Trim();
                }
                def.UseTimeStamp = this.drpUseTimeStamp.SelectedValue == "1" ? true : false;
                if (def.UseTimeStamp)
                {
                    def.TimeStampPrecision = Cast.Enum <OrderRuleDefPrecision>(this.drpTimeStampPrecision.SelectedValue, def.TimeStampPrecision);
                    def.TimeStampPattern   = this.txtTimeStampPattern.Text.Trim();
                }
                def.SerialLength = Cast.Int(this.txtSerialLen.Text.Trim(), def.SerialLength);

                if (this.IsNew)
                {
                    def.Create(session);
                }
                else
                {
                    def.Update(session, "RuleDefineText", "UsePrefix", "PrefixValue", "UseTimeStamp", "TimeStampPrecision", "TimeStampPattern", "SerialLength");
                }
                this.Response.Redirect("OrderNumberRuleDefManager.aspx");
            }
        }
    }
Exemplo n.º 4
0
        /// <summary>
        /// 生成一个新的单据号码
        /// </summary>
        /// <param name="session"></param>
        /// <param name="orderTypeCode"></param>
        /// <returns></returns>
        public static string NextOrderNumber(string orderTypeCode)
        {
            string newNumber = "";

            //用独立的连接来做,因为单据生成需要使用事务,如果出错,回滚事务不会影响其它方面
            using (ISession session = new Session())
            {
                OrderTypeDef typeDefine = OrderTypeDef.Retrieve(session, orderTypeCode);
                if (typeDefine == null)
                {
                    throw new Exception("Can not generate new order number, order type " + orderTypeCode + " not defined");
                }
                if (typeDefine.RuleDefineID <= 0)
                {
                    throw new Exception("Can not generate new order number, rule of order type " + orderTypeCode + " not defined");
                }
                OrderRuleDef ruleDefine = OrderRuleDef.Retrieve(session, typeDefine.RuleDefineID);
                if (ruleDefine == null)
                {
                    throw new Exception("Can not generate new order number, rule of order type " + orderTypeCode + " not defined");
                }

                //开始生成新的单据号码
                try
                {
                    DateTime timeStamp        = DateTime.Now;
                    string   pattern          = "";
                    DateTime prevTimeStamp    = ruleDefine.PrevTimeStampValue;
                    int      prevSerialNumber = ruleDefine.PrevSerialValue;

                    //确保用一个事务将当前OrderRuleDef记录锁住(TODO: 数据库锁 vs 内存锁,哪个更好?)
                    session.BeginTransaction();
                    ruleDefine.Version = DateTime.Now;
                    EntityManager.Update(session, ruleDefine, "Version"); //锁住OrderRuleDef记录
                    ruleDefine = EntityManager.Retrieve <OrderRuleDef>(session, ruleDefine.RuleDefineID);

                    //单据前缀
                    System.Text.StringBuilder builder = new System.Text.StringBuilder();
                    if (ruleDefine.UsePrefix && !string.IsNullOrEmpty(ruleDefine.PrefixValue) && ruleDefine.PrefixValue.Trim().Length > 0)
                    {
                        builder.Append(ruleDefine.PrefixValue.Trim());
                    }

                    #region 单据时间戳
                    if (ruleDefine.UseTimeStamp)
                    {
                        if (!string.IsNullOrEmpty(ruleDefine.TimeStampPattern) && ruleDefine.TimeStampPattern.Trim().Length > 0)
                        {
                            pattern = ruleDefine.TimeStampPattern.Trim();
                        }
                        else
                        {
                            pattern = DEFAULT_TIME_PATTERN;
                        }
                        switch (ruleDefine.TimeStampPrecision)
                        {
                        case OrderRuleDefPrecision.Year:
                            timeStamp = Cast.DateTime(timeStamp.Year.ToString() + "-01-01");
                            if (ruleDefine.PrevTimeStampValue.Year != timeStamp.Year)
                            {
                                prevSerialNumber = 0;
                            }
                            break;

                        case OrderRuleDefPrecision.Month:
                            timeStamp = Cast.DateTime(timeStamp.ToString("yyyy-MM-01"));
                            if (ruleDefine.PrevTimeStampValue.Year != timeStamp.Year || ruleDefine.PrevTimeStampValue.Month != timeStamp.Month)
                            {
                                prevSerialNumber = 0;
                            }
                            break;

                        case OrderRuleDefPrecision.Date:
                            timeStamp = Cast.DateTime(timeStamp.ToString("yyyy-MM-dd"));
                            if (ruleDefine.PrevTimeStampValue.Year != timeStamp.Year || ruleDefine.PrevTimeStampValue.Month != timeStamp.Month ||
                                ruleDefine.PrevTimeStampValue.Day != timeStamp.Day)
                            {
                                prevSerialNumber = 0;
                            }
                            break;

                        case OrderRuleDefPrecision.Hour:
                            timeStamp = Cast.DateTime(timeStamp.ToString("yyyy-MM-dd HH:01"));
                            if (ruleDefine.PrevTimeStampValue.Year != timeStamp.Year || ruleDefine.PrevTimeStampValue.Month != timeStamp.Month ||
                                ruleDefine.PrevTimeStampValue.Day != timeStamp.Day || ruleDefine.PrevTimeStampValue.Hour != timeStamp.Hour)
                            {
                                prevSerialNumber = 0;
                            }
                            break;

                        default: break;
                        }
                        builder.Append(timeStamp.ToString(pattern));
                    }
                    #endregion

                    //单据序列号
                    prevSerialNumber++;
                    builder.Append(prevSerialNumber.ToString().PadLeft(ruleDefine.SerialLength <= 0 ? DEFAULT_SERIA_LEN : ruleDefine.SerialLength, '0'));

                    //更新单据规则
                    ruleDefine.PrevTimeStampValue = timeStamp;
                    ruleDefine.PrevSerialValue    = prevSerialNumber;
                    ruleDefine.Update(session, "PrevTimeStampValue", "PrevSerialValue");

                    session.Commit();

                    newNumber = builder.ToString();
                }
                catch (Exception er)
                {
                    session.Rollback();
                    throw new Exception("Can not generate new order number: " + orderTypeCode, er);
                }
            }

            return(newNumber);
        }