protected void bt_Out_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.Rows)
        {
            CheckBox cb_check = (CheckBox)row.FindControl("cb_Check");
            if (cb_check.Checked)
            {
                PDT_StandardPrice_Detail       item2   = _bll.GetDetailModel(int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString()));
                PDT_StandPriceChangeHistoryBLL history = new PDT_StandPriceChangeHistoryBLL();
                history.Model.StandardPrice    = _bll.Model.ID;
                history.Model.ChangeType       = 3;
                history.Model.ChageTime        = DateTime.Now;
                history.Model.ChangeStaff      = (int)Session["UserID"];
                history.Model.Product          = item2.Product;
                history.Model.PreFactoryPrice  = item2.FactoryPrice;
                history.Model.PreTradeOutPrice = item2.TradeOutPrice;
                history.Model.PreTradeInPrice  = item2.TradeInPrice;
                history.Model.PreStdPrice      = item2.StdPrice;
                history.Add();
                _bll.DeleteDetail(int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString()));
            }
        }
        Response.Redirect("PDT_StandardPriceDetail.aspx?PriceID=" + ViewState["PriceID"].ToString());
    }
    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 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;
        }
    }
    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_Out_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.Rows)
        {
            CheckBox cb_check = (CheckBox)row.FindControl("cb_Check");
            if (cb_check.Checked)
            {
                PDT_StandardPrice_Detail item2 = _bll.GetDetailModel(int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString()));
                PDT_StandPriceChangeHistoryBLL history = new PDT_StandPriceChangeHistoryBLL();
                history.Model.StandardPrice = _bll.Model.ID;
                history.Model.ChangeType = 3;
                history.Model.ChageTime = DateTime.Now;
                history.Model.ChangeStaff = (int)Session["UserID"];
                history.Model.Product = item2.Product;
                history.Model.PreFactoryPrice = item2.FactoryPrice;
                history.Model.PreTradeOutPrice = item2.TradeOutPrice;
                history.Model.PreTradeInPrice = item2.TradeInPrice;
                history.Model.PreStdPrice = item2.StdPrice;
                history.Add();
                _bll.DeleteDetail(int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString()));
            }
        }
        Response.Redirect("PDT_StandardPriceDetail.aspx?PriceID=" + ViewState["PriceID"].ToString());
    }
    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());
    }