protected void bt_In_Click(object sender, EventArgs e)
    {
        Save();

        foreach (GridViewRow row in gv_NotInList.Rows)
        {
            CheckBox cb_check = (CheckBox)row.FindControl("cb_Check");
            if (cb_check.Checked)
            {
                PDT_StandardPriceBLL _bll;
                if ((int)ViewState["ID"] != 0)
                    _bll = new PDT_StandardPriceBLL((int)ViewState["ID"]);
                else
                    return;

                PDT_StandardPrice_Detail pd = new PDT_StandardPrice_Detail();
                pd.Product = int.Parse(gv_NotInList.DataKeys[row.RowIndex]["ID"].ToString());

                PDT_ProductBLL productbll = new PDT_ProductBLL(pd.Product);
                PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo((int)Session["OwnerClient"]);
                if (extinfo != null)
                    pd.Price = extinfo.SalesPrice;
                else
                    pd.Price = productbll.Model.StdPrice;
                _bll.AddDetail(pd);
            }
        }
        Response.Redirect("StandardPriceDetail.aspx?ID=" + ViewState["ID"].ToString());
    }
            public TDPStandardPriceDetail(PDT_StandardPrice_Detail m)
            {
                Product = m.Product;
                Price = m.Price;
                Remark = m.Remark;

                PDT_Product p = new PDT_ProductBLL(m.Product).Model;
                if (p != null)
                {
                    ProductName = p.ShortName == "" ? p.FullName : p.ShortName;
                }
            }
    private void Save()
    {
        PDT_StandardPriceBLL _bll;
        if ((int)ViewState["PriceID"] != 0)
            _bll = new PDT_StandardPriceBLL((int)ViewState["PriceID"]);
        else
            _bll = new PDT_StandardPriceBLL();

        panel1.GetData(_bll.Model);

        if (_bll.Model.OrganizeCity == 0)
        {
            MessageBox.Show(this, "适用管理片区必填!");
            return;
        }

        if ((int)ViewState["PriceID"] != 0)
        {
            _bll.Model.UpdateStaff = int.Parse(Session["UserID"].ToString());
            _bll.Update();

            #region 修改明细
            foreach (GridViewRow row in gv_List.Rows)
            {
                PDT_StandardPrice_Detail item1 = new PDT_StandardPrice_Detail();
                item1.ID = int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString());
                item1.StandardPrice = _bll.Model.ID;
                item1.Product = int.Parse(gv_List.DataKeys[row.RowIndex]["Product"].ToString());
                item1.FactoryPrice = decimal.Parse(((TextBox)row.FindControl("tbx_FactoryPrice")).Text);
                item1.TradeOutPrice = decimal.Parse(((TextBox)row.FindControl("tbx_TradeOutPrice")).Text);
                item1.TradeInPrice = decimal.Parse(((TextBox)row.FindControl("tbx_TradeInPrice")).Text);
                item1.StdPrice = decimal.Parse(((TextBox)row.FindControl("tbx_StdPrice")).Text);
                TextBox tbx_RebatePrice = row.FindControl("tbx_RebatePrice") == null ? null : (TextBox)row.FindControl("tbx_RebatePrice");
                if (tbx_RebatePrice != null)
                {
                    decimal rebateprice = 0;
                    decimal.TryParse(tbx_RebatePrice.Text, out rebateprice);
                    item1.RebatePrice = rebateprice;
                }
                TextBox tbx_DIRebatePrice = row.FindControl("tbx_DIRebatePrice") == null ? null : (TextBox)row.FindControl("tbx_DIRebatePrice");
                if (tbx_DIRebatePrice != null)
                {
                    decimal direbateprice = 0;
                    decimal.TryParse(tbx_DIRebatePrice.Text, out direbateprice);
                    item1.DIRebatePrice = direbateprice;
                }
                CheckBox cbx_ISFL = row.FindControl("cbx_ISFL") == null ? null : (CheckBox)row.FindControl("cbx_ISFL");
                if (cbx_ISFL != null)
                {
                    item1.ISFL = cbx_ISFL.Checked ? 1 : 2;
                }
                CheckBox cbx_ISJH = row.FindControl("cbx_ISJH") == null ? null : (CheckBox)row.FindControl("cbx_ISJH");
                if (cbx_ISJH != null)
                {
                    item1.ISJH = cbx_ISJH.Checked ? 1 : 2;
                }

                CheckBox cbx_ISCheckJF = row.FindControl("cbx_ISCheckJF") == null ? null : (CheckBox)row.FindControl("cbx_ISCheckJF");
                if (cbx_ISCheckJF != null)
                {
                    item1.ISCheckJF = cbx_ISCheckJF.Checked ? 1 : 2;
                }
                PDT_StandardPrice_Detail item2 = _bll.GetDetailModel(item1.ID);
                if (item1.FactoryPrice != item2.FactoryPrice || item1.TradeInPrice != item2.TradeInPrice || item1.TradeOutPrice != item2.TradeOutPrice || item1.StdPrice != item2.StdPrice)
                {
                    PDT_StandPriceChangeHistoryBLL history = new PDT_StandPriceChangeHistoryBLL();
                    history.Model.StandardPrice = _bll.Model.ID;
                    history.Model.ChangeType = 2;
                    history.Model.ChageTime = DateTime.Now;
                    history.Model.ChangeStaff = (int)Session["UserID"];
                    history.Model.Product = item1.Product;
                    history.Model.PreFactoryPrice = item2.FactoryPrice;
                    history.Model.PreTradeOutPrice = item2.TradeOutPrice;
                    history.Model.PreTradeInPrice = item2.TradeInPrice;
                    history.Model.PreStdPrice = item2.StdPrice;
                    history.Model.AftFactoryPrice = item1.FactoryPrice;
                    history.Model.AftTradeInPrice = item1.TradeInPrice;
                    history.Model.AftTradeOutPrice = item1.TradeOutPrice;
                    history.Model.AftStdPrice = item1.StdPrice;
                    history.Add();
                }
                _bll.UpdateDetail(item1);
            }
            #endregion
        }
        else
        {
            _bll.Model.ApproveFlag = 2;
            _bll.Model.ActiveFlag = 2;
            _bll.Model.InsertStaff = int.Parse(Session["UserID"].ToString());
            _bll.Add();
            ViewState["PriceID"] = _bll.Model.ID;
        }
    }
    protected void bt_In_Click(object sender, EventArgs e)
    {
        Save();
        if ((int)ViewState["PriceID"] == 0) return;

        PDT_StandardPriceBLL _bll = new PDT_StandardPriceBLL((int)ViewState["PriceID"]);

        foreach (GridViewRow row in gv_List_FacProd.Rows)
        {
            CheckBox cb_check = (CheckBox)row.FindControl("cb_Check");
            if (cb_check.Checked)
            {
                PDT_Product product = new PDT_ProductBLL((int)gv_List_FacProd.DataKeys[row.RowIndex]["ID"]).Model;
                if (product != null)
                {
                    PDT_StandardPrice_Detail detail = new PDT_StandardPrice_Detail();
                    detail.StandardPrice = (int)ViewState["PriceID"];
                    detail.Product = product.ID;
                    detail.FactoryPrice = product.FactoryPrice;
                    detail.TradeOutPrice = product.TradePrice;
                    detail.TradeInPrice = product.NetPrice;
                    detail.StdPrice = product.StdPrice;
                    PDT_StandPriceChangeHistoryBLL history = new PDT_StandPriceChangeHistoryBLL();
                    history.Model.StandardPrice = _bll.Model.ID;
                    history.Model.ChangeType = 1;
                    history.Model.ChageTime = DateTime.Now;
                    history.Model.ChangeStaff = (int)Session["UserID"];
                    history.Model.Product = detail.Product;
                    history.Model.AftFactoryPrice = detail.FactoryPrice;
                    history.Model.AftTradeOutPrice = detail.TradeOutPrice;
                    history.Model.AftTradeInPrice = detail.TradeInPrice;
                    history.Model.AftStdPrice = detail.StdPrice;
                    history.Add();

                    _bll.AddDetail(detail);
                }
            }
        }

        Response.Redirect("PDT_StandardPriceDetail.aspx?PriceID=" + ViewState["PriceID"].ToString());
    }
    private bool Save()
    {
        PDT_StandardPriceBLL _bll;
        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll = new PDT_StandardPriceBLL((int)ViewState["ID"]);
        }
        else
        {
            //新增
            _bll = new PDT_StandardPriceBLL();
        }

        pl_detail.GetData(_bll.Model);

        #region 判断必填项

        #endregion

        #region 判断是否与默认价表重复
        if (_bll.Model.IsDefault.ToUpper() == "Y")
        {
            int _priceid = PDT_StandardPriceBLL.GetDefaultPrice((int)Session["OwnerClient"]);
            if (_priceid != _bll.Model.ID)
            {
                MessageBox.Show(this, "对不起,不能重复新增默认价表,新增的价表必须限制区域或渠道!");
                return false;
            }
        }

        #endregion

        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll.Model.UpdateStaff = (int)Session["UserID"];
            _bll.Update();
        }
        else
        {
            //新增
            _bll.Model.Supplier = (int)Session["OwnerClient"];
            _bll.Model.ApproveFlag = 2;
            _bll.Model.InsertStaff = (int)Session["UserID"];
            ViewState["ID"] = _bll.Add();
        }

        #region 保存修改明细
        foreach (GridViewRow row in gv_List.Rows)
        {
            PDT_StandardPrice_Detail item = new PDT_StandardPrice_Detail();
            item.PriceID = _bll.Model.ID;
            item.ID = int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString());
            item.Product = int.Parse(gv_List.DataKeys[row.RowIndex]["Product"].ToString());

            PDT_Product p = new PDT_ProductBLL(item.Product).Model;

            if (row.FindControl("tbx_Price") != null)
            {
                decimal d = 0;
                if (decimal.TryParse(((TextBox)row.FindControl("tbx_Price")).Text, out d)) item.Price = d;

                PDT_ProductBLL productbll = new PDT_ProductBLL(item.Product);
                item.Price = item.Price / productbll.Model.ConvertFactor;

                PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo((int)Session["OwnerClient"]);
                if (extinfo != null)
                {
                    if (extinfo.MinSalesPrice > 0 && item.Price < extinfo.MinSalesPrice)
                    {
                        MessageBox.Show(this, "对不起,您发布的价格不能小于建议零售价!");
                        return false;
                    }
                    else if (extinfo.MaxSalesPrice > 0 && item.Price > extinfo.MaxSalesPrice)
                    {
                        MessageBox.Show(this, "对不起,您发布的价格不能过高于建议零售价!");
                        return false;
                    }
                }
            }

            if (row.FindControl("tbx_Remark") != null)
                item.Remark = ((TextBox)row.FindControl("tbx_Remark")).Text;

            _bll.UpdateDetail(item);
        }

        #endregion

        return true;
    }