private void QueryAndBindData(ISession session, INVCheckHead head, int pageSize, int pageIndex, bool fetchCount)
    {
        Magic.Framework.ORM.Query.ObjectQuery query = session.CreateObjectQuery(string.Format(@"
select l.LineNumber as LineNumber
    ,s.BarCode as BarCode,m.ItemCode as ItemCode,m.ItemName as ItemName
    ,s.ColorCode as ColorCode,color.ColorText as ColorText,s.SizeCode as SizeCode
    ,l.AreaCode as AreaCode,l.SectionCode as SectionCode,l.BeforeQty as BeforeQty,l.CurrentQty as CurrentQty
from INVCheckLine l
inner join ItemSpec s on l.SKUID=s.SKUID
inner join ItemMaster m on m.ItemID=s.ItemID
left join ItemColor color on color.ColorCode=s.ColorCode
where l.OrderNumber=?ordNum {0}
order by l.LineNumber", this.drpViewType.SelectedValue == "2" ? "and l.BeforeQty<>l.CurrentQty" : ""))
                                                      .Attach(typeof(INVCheckLine)).Attach(typeof(ItemSpec)).Attach(typeof(ItemMaster)).Attach(typeof(ItemColor))
                                                      .SetValue("?ordNum", this.OrderNumber, "l.OrderNumber")
                                                      .SetPage(pageIndex, pageSize);
        if (this.drpArea.SelectedValue.Trim().Length > 0)
        {
            query.And(Exp.Eq("l.AreaCode", this.drpArea.SelectedValue.Trim()));
        }
        if (this.txtSKU.Text.Trim().Length > 0)
        {
            query.And(Exp.Like("s.BarCode", "%" + this.txtSKU.Text.Trim() + "%"));
        }
        if (this.txtItemCode.Text.Trim().Length > 0)
        {
            query.And(Exp.Like("m.ItemCode", "%" + this.txtItemCode.Text.Trim() + "%"));
        }
        if (this.txtName.Text.Trim().Length > 0)
        {
            query.And(Exp.Like("m.ItemName", "%" + this.txtName.Text.Trim() + "%"));
        }
        if (this.txtColor.Text.Trim().Length > 0)
        {
            query.And(Exp.Like("s.ColorCode", "%" + this.txtColor.Text.Trim().ToUpper() + "%"));
        }
        if (this.txtSize.Text.Trim().Length > 0)
        {
            query.And(Exp.Like("s.SizeCode", "%" + this.txtSize.Text.Trim().ToUpper() + "%"));
        }

        this.repeatControl.DataSource = query.DataSet();
        this._head = INVCheckHead.Retrieve(session, this.OrderNumber);
        this.repeatControl.DataBind();
        this._head = null;

        if (fetchCount)
        {
            this.magicPagerMain.RecordCount = this.magicPagerSub.RecordCount = query.Count();
        }
        WebUtil.SetMagicPager(magicPagerMain, pageSize, pageIndex);
        WebUtil.SetMagicPager(magicPagerSub, pageSize, pageIndex);
        this.txtReturnToThisUrl.Value = this.GetReturnUrl();
    }
    protected void MagicItemCommand(object sender, MagicItemEventArgs e)
    {
        try
        {
            if (e.CommandName == "Download")
            {
                #region  载
                DataSet      ds   = null;
                INVCheckHead head = null;
                using (ISession session = new Session())
                {
                    head = INVCheckHead.Retrieve(session, this.OrderNumber);
                    Magic.Framework.ORM.Query.ObjectQuery query = session.CreateObjectQuery(string.Format(@"
select l.LineNumber as LineNumber,l.AreaCode as AreaCode,l.SectionCode as SectionCode
    ,s.BarCode as SKU,m.ItemCode as ItemCode
    ,s.ColorCode as ColorCode,color.ColorText as ColorText,s.SizeCode as SizeCode
    ,{0} as BeforeQty,l.CurrentQty as CurrentQty,m.ItemName as ItemName
from INVCheckLine l
inner join ItemSpec s on l.SKUID=s.SKUID
inner join ItemMaster m on m.ItemID=s.ItemID
left join ItemColor color on color.ColorCode=s.ColorCode
where l.OrderNumber=?ordNum {1}
order by l.LineNumber"
                                                                                                          , head.CheckType == INVCheckType.Implicit && head.Status == INVCheckStatus.Confirm ? "'-'" : "l.BeforeQty"
                                                                                                          , this.drpViewType.SelectedValue == "2" ? "and l.BeforeQty<>l.CurrentQty" : ""))
                                                                  .Attach(typeof(INVCheckLine)).Attach(typeof(ItemSpec)).Attach(typeof(ItemMaster))
                                                                  .Attach(typeof(ItemColor)).Attach(typeof(ItemSize))
                                                                  .SetValue("?ordNum", this.OrderNumber, "l.OrderNumber");
                    if (this.drpArea.SelectedValue.Trim().Length > 0)
                    {
                        query.And(Exp.Eq("l.AreaCode", this.drpArea.SelectedValue.Trim()));
                    }
                    if (this.txtSKU.Text.Trim().Length > 0)
                    {
                        query.And(Exp.Like("s.BarCode", "%" + this.txtSKU.Text.Trim() + "%"));
                    }
                    if (this.txtItemCode.Text.Trim().Length > 0)
                    {
                        query.And(Exp.Like("m.ItemCode", "%" + this.txtItemCode.Text.Trim() + "%"));
                    }
                    if (this.txtName.Text.Trim().Length > 0)
                    {
                        query.And(Exp.Like("m.ItemName", "%" + this.txtName.Text.Trim() + "%"));
                    }
                    if (this.txtColor.Text.Trim().Length > 0)
                    {
                        query.And(Exp.Like("s.ColorCode", "%" + this.txtColor.Text.Trim().ToUpper() + "%"));
                    }
                    if (this.txtSize.Text.Trim().Length > 0)
                    {
                        query.And(Exp.Like("s.SizeCode", "%" + this.txtSize.Text.Trim().ToUpper() + "%"));
                    }
                    ds = query.DataSet();
                }
                if (ds == null)
                {
                    WebUtil.ShowError(this, "没有数据下载或者下载出错了");
                    return;
                }

                string fileName = DownloadUtil.DownloadXls("StockCheck_" + DateTime.Now.ToString("yyMMdd") + ".xls", "CK",
                                                           new List <DownloadFormat>()
                {
                    new DownloadFormat(DataType.NumberText, "行号", "LineNumber"),
                    new DownloadFormat(DataType.Text, "库位", "AreaCode"),
                    new DownloadFormat(DataType.NumberText, "货架", "SectionCode"),
                    new DownloadFormat(DataType.NumberText, "SKU", "SKU"),
                    new DownloadFormat(DataType.NumberText, "货号", "ItemCode"),
                    new DownloadFormat(DataType.Text, "颜色", "ColorCode", "ColorText"),
                    new DownloadFormat(DataType.Text, "尺码", "SizeCode"),
                    new DownloadFormat((head.CheckType == INVCheckType.Implicit && head.Status == INVCheckStatus.Confirm ? DataType.Text : DataType.Number), "系统数量", "BeforeQty"),
                    new DownloadFormat(DataType.Number, "盘点数量", "CurrentQty"),
                    new DownloadFormat(DataType.Text, "商品名称", "ItemName")
                }, ds);
                this.frameDownload.Attributes["src"] = fileName;
                #endregion
            }
            else if (e.CommandName == "Save")
            {
                #region 保存
                using (ISession session = new Session())
                {
                    try
                    {
                        session.BeginTransaction();

                        foreach (RepeaterItem item in this.repeatControl.Items)
                        {
                            TextBox text = item.FindControl("txtQty") as TextBox;
                            if (text == null)
                            {
                                continue;
                            }
                            string lineNum = text.Attributes["lineNumber"];
                            if (string.IsNullOrEmpty(lineNum) || lineNum.Trim().Length <= 0)
                            {
                                continue;
                            }
                            INVCheckLine line = INVCheckLine.Retrieve(session, this.OrderNumber, lineNum);
                            if (line != null)
                            {
                                line.CurrentQty = Cast.Decimal(text.Text, line.CurrentQty);
                                line.Update(session, "CurrentQty");
                            }
                        }

                        session.Commit();
                        WebUtil.ShowMsg(this, "保存成功");
                    }
                    catch (Exception er)
                    {
                        session.Rollback();
                        WebUtil.ShowError(this, er);
                    }
                }
                #endregion
            }
            else if (e.CommandName == "Release")
            {
                #region 发布
                using (ISession session = new Session())
                {
                    try
                    {
                        INVCheckHead head = INVCheckHead.Retrieve(session, this.OrderNumber);
                        session.BeginTransaction();
                        head.Release(session);
                        session.Commit();
                        WebUtil.ShowMsg(this, "发布成功");
                        this.SetView(head);
                    }
                    catch (Exception er)
                    {
                        session.Rollback();
                        WebUtil.ShowError(this, er);
                    }
                }
                #endregion
            }
            else if (e.CommandName == "Close")
            {
                #region 关闭
                using (ISession session = new Session())
                {
                    try
                    {
                        INVCheckHead head = INVCheckHead.Retrieve(session, this.OrderNumber);
                        session.BeginTransaction();
                        head.Close(session);
                        session.Commit();
                        WebUtil.ShowMsg(this, "关闭成功,本次盘点任务已经完成");
                        this.SetView(head);
                    }
                    catch (Exception er)
                    {
                        session.Rollback();
                        WebUtil.ShowError(this, er);
                    }
                }
                #endregion
            }
        }
        catch (Exception err)
        {
            WebUtil.ShowError(this, err);
        }
    }