/// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model</param>
        public int AddRecord(BArrangeBillData model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("set nocount on; ");
            strSql.Append("insert into BArrangeBill(");
            strSql.Append(@"arrangeBillNo,arriveBillNo,isrtEmpId,isrtDt,updtEmpId,updtDt,commitEmpId,commitDt,instantState)");
            strSql.Append(" values (");
            strSql.Append(@"@arrangeBillNo,@arriveBillNo,@isrtEmpId,@isrtDt,@updtEmpId,@updtDt,@commitEmpId,@commitDt,@instantState)");
            strSql.Append("; select @@identity; set nocount off; ");
            SqlParameter[] parameters = {
                    new SqlParameter("@arrangeBillNo", SqlDbType.VarChar,20),
                    new SqlParameter("@arriveBillNo", SqlDbType.VarChar,20),
                    new SqlParameter("@isrtEmpId", SqlDbType.Int),
                    new SqlParameter("@isrtDt", SqlDbType.DateTime),
                    new SqlParameter("@updtEmpId", SqlDbType.Int),
                    new SqlParameter("@updtDt", SqlDbType.DateTime),
                    new SqlParameter("@commitEmpId", SqlDbType.Int),
                    new SqlParameter("@commitDt", SqlDbType.DateTime),
                    new SqlParameter("@instantState", SqlDbType.NVarChar,20)
                };
            parameters[0].Value = model.arrangeBillNo;
            parameters[1].Value = model.arriveBillNo;
            parameters[2].Value = model.isrtEmpId;
            parameters[3].Value = model.isrtDt == string.Empty ? null : model.isrtDt;
            parameters[4].Value = model.updtEmpId;
            parameters[5].Value = model.updtDt == string.Empty ? null : model.updtDt;
            parameters[6].Value = model.commitEmpId;
            parameters[7].Value = model.commitDt == string.Empty ? null : model.commitDt;
            parameters[8].Value = model.instantState;

            int id = 0;
            try
            {
                object ret = SqlHelper.ExecuteScalar(this.connection, this.transaction, CommandType.Text, strSql.ToString(), parameters);

                if (ret != null && ret != DBNull.Value)
                {
                    id = Convert.ToInt32(ret);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }
        //*****************************************************************************
        //do it later      do it later      do it later
        //*****************************************************************************
        /// <summary>
        /// 保存排托单信息
        /// </summary>
        /// <param name="arrangeBillModel">排托单实例</param>
        /// <param name="dtArrange">排托明细数据源</param>
        /// <param name="isSubmit">是否提交</param>
        /// <returns></returns>
        public bool SaveBArrangeBill(BArrangeBillData arrangeBillModel, DataTable dtArrange, bool isSubmit)
        {
            bool ret = false;
            BArrangeBillBB arrangeBillBB = new BArrangeBillBB(connection);
            BArriveBillBB arriveBillBB = new BArriveBillBB(connection);
            BArrangeBillDetailBB arrangeBillDetailBB = new BArrangeBillDetailBB(connection);
            SqlTransaction trans = null;

            try
            {
                if (this.transaction == null)
                {
                    trans = this.connection.BeginTransaction("TranSave");
                    arrangeBillBB.Transaction = trans;
                    arriveBillBB.Transaction = trans;
                    arrangeBillDetailBB.Transaction = trans;
                }
                else
                {
                    arrangeBillDetailBB.Transaction = this.transaction;
                    arriveBillBB.Transaction = this.transaction;
                    arrangeBillBB.Transaction = this.transaction;
                }

                BArriveBillData arriveBillModel = arriveBillBB.GetModel(arrangeBillModel.arriveBillNo);

                if (arrangeBillModel.id == 0)//新增排托单
                {
                    #region 新增排托单

                    if (isSubmit)//提交
                    {
                        arriveBillModel.instantState = "03";//排托单已提交
                        arriveBillModel.commitDt = arrangeBillModel.commitDt;
                        arriveBillModel.commitEmpId = arrangeBillModel.commitEmpId;
                    }
                    else//未提交
                    {
                        arriveBillModel.instantState = "02";//已生成排托单
                    }

                    //更改到货单状态
                    arriveBillBB.ModifyRecord(arriveBillModel);

                    //新增排托单信息
                    arrangeBillBB.AddRecord(arrangeBillModel);

                    foreach (DataRow dr in dtArrange.Rows)
                    {
                        if (dr["isdel"].ToString() == "0")//未删除
                        {
                            BArrangeBillDetailData model = new BArrangeBillDetailData();

                            model.arrangeBillNo = dr["arrangeBillNo"].ToString();
                            model.arriveBillNo = dr["arriveBillNo"].ToString();
                            model.boxNum = Convert.ToInt32(dr["boxNum"]);
                            model.financeBillNo = dr["financeBillNo"].ToString();
                            model.isrtDt = arrangeBillModel.isrtDt;
                            model.instantState = "01";
                            model.isrtEmpId = arrangeBillModel.isrtEmpId;
                            model.materialNo = dr["materialNo"].ToString();
                            model.palletIndex = Convert.ToInt32(dr["palletIndex"]);
                            model.region = Convert.ToInt32(dr["region"].ToString());

                            arrangeBillDetailBB.AddRecord(model);
                        }
                    }

                    #endregion 新增排托单
                }
                else
                {
                    #region 修改排托单信息

                    if (isSubmit)//提交
                    {
                        arriveBillModel.instantState = "03";//排托单已提交
                        arriveBillModel.commitDt = arrangeBillModel.commitDt;
                        arriveBillModel.commitEmpId = arrangeBillModel.commitEmpId;
                    }
                    else//未提交
                    {
                        arriveBillModel.instantState = "02";//已生成排托单
                    }

                    //更改到货单状态
                    arriveBillBB.ModifyRecord(arriveBillModel);

                    //修改排托单信息
                    arrangeBillBB.ModifyRecord(arrangeBillModel);

                    foreach (DataRow dr in dtArrange.Rows)
                    {
                        if (Convert.ToInt32(dr["id"]) > 0 && dr["isdel"].ToString() == "1")//删除方法
                        {
                            arrangeBillDetailBB.DeleteRecord(Convert.ToInt32(dr["id"]));
                        }
                        else if (Convert.ToInt32(dr["id"]) > 0 && dr["isdel"].ToString() == "0")//修改
                        {
                            BArrangeBillDetailData model = arrangeBillDetailBB.GetModel(Convert.ToInt32(dr["id"]));

                            model.boxNum = Convert.ToInt32(dr["boxNum"]);
                            model.region = Convert.ToInt32(dr["region"]);
                            model.updtDt = DateTime.Now.ToString();
                            model.updtEmpId = arrangeBillModel.updtEmpId;

                            arrangeBillDetailBB.ModifyRecord(model);
                        }
                        else if (Convert.ToInt32(dr["id"]) == 0 && dr["isdel"].ToString() == "0")//添加
                        {
                            BArrangeBillDetailData model = new BArrangeBillDetailData();

                            model.arrangeBillNo = dr["arrangeBillNo"].ToString();
                            model.arriveBillNo = dr["arriveBillNo"].ToString();
                            model.boxNum = Convert.ToInt32(dr["boxNum"]);
                            model.financeBillNo = dr["financeBillNo"].ToString();
                            model.isrtDt = arrangeBillModel.isrtDt;
                            model.instantState = "01";
                            model.isrtEmpId = arrangeBillModel.isrtEmpId;
                            model.materialNo = dr["materialNo"].ToString();
                            model.palletIndex = Convert.ToInt32(dr["palletIndex"]);
                            model.region = Convert.ToInt32(dr["region"].ToString());

                            arrangeBillDetailBB.AddRecord(model);
                        }
                    }

                    #endregion 修改排托单信息
                }

                if (this.transaction == null) trans.Commit();
                ret = true;
            }
            catch
            {
                if (transaction == null) trans.Rollback("TranSave");
                throw new Exception("新增排拖单出错了!");
            }
            finally
            {
                arrangeBillBB.Dispose();
                arriveBillBB.Dispose();
                arrangeBillDetailBB.Dispose();
            }

            return ret;
        }
        /// <summary>
        /// 更新一条"排托单"信息
        /// </summary>
        /// <param name="model">model</param>
        public bool ModifyRecord(BArrangeBillData model)
        {
            bool ret = false;
            SqlTransaction trans = null;
            try
            {
                if (this.transaction == null)
                {
                    trans = this.connection.BeginTransaction("TransModify");
                    this.arrangeBillBB.Transaction = trans;
                    this.errorDiaryBB.Transaction = trans;
                    this.operatDiaryBB.Transaction = trans;
                }

                this.arrangeBillBB.ModifyRecord(model);

                SOperatDiaryData operatDiaryData = new SOperatDiaryData();
                operatDiaryData.empId = this.empId;
                operatDiaryData.functionId = "";
                operatDiaryData.recordId = model.id.ToString();
                operatDiaryData.operateContent = "排托单修改一条id为“" + model.id.ToString() + "”的记录";
                this.operatDiaryBB.AddRecord(operatDiaryData);

                if (this.transaction == null) trans.Commit();
                ret = true;
            }
            catch (Exception ex)
            {
                if (this.transaction == null) trans.Rollback("TransModify");
                SErrorDiaryData errorDiaryData = new SErrorDiaryData();
                errorDiaryData.empId = this.empId;
                errorDiaryData.functionId = "";
                errorDiaryData.errorText = "排托单修改记录“" + model.id.ToString() + "”时报错:" + ex.Message;
                this.errorDiaryBB.AddRecord(errorDiaryData);
                throw ex;
            }
            finally
            {
            }
            return ret;
        }
        /// <summary>
        /// 增加一条"排托单"信息
        /// </summary>
        /// <param name="model">model</param>
        public int AddRecord(BArrangeBillData model)
        {
            int id = 0;
            SqlTransaction trans = null;
            try
            {
                if (this.transaction == null)
                {
                    trans = this.connection.BeginTransaction("TransAdd");
                    this.arrangeBillBB.Transaction = trans;
                    this.errorDiaryBB.Transaction = trans;
                    this.operatDiaryBB.Transaction = trans;
                }

                id = this.arrangeBillBB.AddRecord(model);

                SOperatDiaryData operatDiaryData = new SOperatDiaryData();
                operatDiaryData.empId = this.empId;
                operatDiaryData.functionId = "";
                operatDiaryData.recordId = id.ToString();
                operatDiaryData.operateContent = "排托单增加一条id为“" + id.ToString() + "”的记录";
                this.operatDiaryBB.AddRecord(operatDiaryData);

                if (this.transaction == null) trans.Commit();
            }
            catch (Exception ex)
            {
                if (this.transaction == null) trans.Rollback("TransAdd");
                SErrorDiaryData errorDiaryData = new SErrorDiaryData();
                errorDiaryData.empId = this.empId;
                errorDiaryData.functionId = "";
                errorDiaryData.errorText = "排托单增加记录时报错:" + ex.Message;
                this.errorDiaryBB.AddRecord(errorDiaryData);
                throw ex;
            }
            finally
            {
            }
            return id;
        }
 /// <summary>
 /// 更新一条数据
 /// </summary>
 /// <param name="model">model</param>
 public bool ModifyRecord(BArrangeBillData model)
 {
     return this.arrangeBillDB.ModifyRecord(model);
 }
 /// <summary>
 /// 增加一条数据
 /// </summary>
 /// <param name="model">model</param>
 public int AddRecord(BArrangeBillData model)
 {
     return this.arrangeBillDB.AddRecord(model);
 }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">model</param>
        public bool ModifyRecord(BArrangeBillData model)
        {
            bool ret = false;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BArrangeBill set ");
            strSql.Append("arrangeBillNo=@arrangeBillNo,");
            strSql.Append("arriveBillNo=@arriveBillNo,");
            strSql.Append("isrtEmpId=@isrtEmpId,");
            strSql.Append("isrtDt=@isrtDt,");
            strSql.Append("updtEmpId=@updtEmpId,");
            strSql.Append("updtDt=@updtDt,");
            strSql.Append("commitEmpId=@commitEmpId,");
            strSql.Append("commitDt=@commitDt,");
            strSql.Append("instantState=@instantState");
            strSql.Append(" where id = @id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int),
                    new SqlParameter("@arrangeBillNo", SqlDbType.VarChar,20),
                    new SqlParameter("@arriveBillNo", SqlDbType.VarChar,20),
                    new SqlParameter("@isrtEmpId", SqlDbType.Int),
                    new SqlParameter("@isrtDt", SqlDbType.DateTime),
                    new SqlParameter("@updtEmpId", SqlDbType.Int),
                    new SqlParameter("@updtDt", SqlDbType.DateTime),
                    new SqlParameter("@commitEmpId", SqlDbType.Int),
                    new SqlParameter("@commitDt", SqlDbType.DateTime),
                    new SqlParameter("@instantState", SqlDbType.NVarChar,20)
                };
            parameters[0].Value = model.id;
            parameters[1].Value = model.arrangeBillNo;
            parameters[2].Value = model.arriveBillNo;
            parameters[3].Value = model.isrtEmpId;
            parameters[4].Value = model.isrtDt == string.Empty ? null : model.isrtDt;
            parameters[5].Value = model.updtEmpId;
            parameters[6].Value = model.updtDt == string.Empty ? null : model.updtDt;
            parameters[7].Value = model.commitEmpId;
            parameters[8].Value = model.commitDt == string.Empty ? null : model.commitDt;
            parameters[9].Value = model.instantState;

            try
            {
                SqlHelper.ExecuteNonQuery(this.connection, this.transaction, CommandType.Text, strSql.ToString(), parameters);
                ret = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ret;
        }
        //*****************************************************************************
        //do it later      do it later      do it later
        //*****************************************************************************
        /// <summary>
        /// 得到一个model
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns>model</returns>
        public BArrangeBillData GetModel(string billNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select id,arrangeBillNo,arriveBillNo,isrtEmpId,isrtDt,updtEmpId,updtDt,commitEmpId,commitDt,instantState from BArrangeBill");
            strSql.Append(" where arrangeBillNo = @billNo ");
            SqlParameter[] parameters = {
                    new SqlParameter("@billNo", SqlDbType.VarChar,50)
                };
            parameters[0].Value = billNo;

            BArrangeBillData model = new BArrangeBillData();
            DataSet ds = SqlHelper.ExecuteDataset(this.connection, this.transaction, CommandType.Text, strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                if (row["id"] != DBNull.Value)
                {
                    model.id = Convert.ToInt32(row["id"]);
                }
                if (row["arrangeBillNo"] != DBNull.Value)
                {
                    model.arrangeBillNo = Convert.ToString(row["arrangeBillNo"]);
                }
                if (row["arriveBillNo"] != DBNull.Value)
                {
                    model.arriveBillNo = Convert.ToString(row["arriveBillNo"]);
                }
                if (row["isrtEmpId"] != DBNull.Value)
                {
                    model.isrtEmpId = Convert.ToInt32(row["isrtEmpId"]);
                }
                if (row["isrtDt"] != DBNull.Value)
                {
                    model.isrtDt = Convert.ToString(row["isrtDt"]);
                }
                if (row["updtEmpId"] != DBNull.Value)
                {
                    model.updtEmpId = Convert.ToInt32(row["updtEmpId"]);
                }
                if (row["updtDt"] != DBNull.Value)
                {
                    model.updtDt = Convert.ToString(row["updtDt"]);
                }
                if (row["commitEmpId"] != DBNull.Value)
                {
                    model.commitEmpId = Convert.ToInt32(row["commitEmpId"]);
                }
                if (row["commitDt"] != DBNull.Value)
                {
                    model.commitDt = Convert.ToString(row["commitDt"]);
                }
                if (row["instantState"] != DBNull.Value)
                {
                    model.instantState = Convert.ToString(row["instantState"]);
                }
                return model;
            }
            else
            {
                return null;
            }
        }
    /// <summary>
    /// 删除排拖单明细
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gridDetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        BArrangeBillBB arrangeBillBB = new BArrangeBillBB();

        try
        {
            string rowid = this.gridDetail.DataKeys[e.RowIndex].Values["rowId"].ToString();//标志
            string arrangeBillNo = this.gridDetail.DataKeys[e.RowIndex].Values["arrangeBillNo"].ToString();//标志
            BArrangeBillData arrangeBillModel = new BArrangeBillData();
            DataRow[] myDataRowArray_Result = null;

            //根据rowid查找排托明细
            myDataRowArray_Result = this.DtResult.Select("rowid='" + rowid + "'");

            if (myDataRowArray_Result.Length > 0)
            {
                //更改删除标志
                myDataRowArray_Result[0]["isdel"] = "1";//删除
                myDataRowArray_Result[0]["boxNum"] = 0;

                //判断排托单是否已经提交
                arrangeBillModel = arrangeBillBB.GetModel(arrangeBillNo);

                if (arrangeBillModel != null && arrangeBillModel.instantState != "01")
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('已提交数据不允许删除!');", true);
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "ShowErr", "ShowErr(\"" + Server.UrlEncode(ex.Message) + "\",3);", true);
            return;
        }
        finally
        {
            this.BindDetailTable();//绑定排托明细列表
            this.SaveStencilDetailViewState();//保存
            this.BindGrid();//绑定到货明细列表
            arrangeBillBB.Dispose();
        }
    }
    /// <summary>
    /// 保存或保存并提交公共方法
    /// </summary>
    /// <param name="isSubmit"></param>
    protected void SubmitArrangeBill(bool isSubmit)
    {
        this.SaveStencilDetailViewState();//保存

        if (this.ValidateArrangeDetail())
        {
            BArrangeBillBC arrangeBillBC = new BArrangeBillBC();
            BArrangeBillBB arrangeBillBB = new BArrangeBillBB();

            try
            {
                BArrangeBillData data = arrangeBillBB.GetModel("PT" + this.ArriveBillNo);

                if (data == null)
                {
                    data = new BArrangeBillData();

                    data.arrangeBillNo = "PT" + this.ArriveBillNo;
                    data.id = 0;
                    data.isrtDt = DateTime.Now.ToString();
                    data.isrtEmpId = this.currentUser.empId;
                    data.arriveBillNo = this.ArriveBillNo;

                    if (isSubmit)
                    {
                        data.instantState = "02";
                        data.commitDt = DateTime.Now.ToString();
                        data.commitEmpId = this.currentUser.empId;
                    }
                    else
                    {
                        data.instantState = "01";
                        data.updtDt = DateTime.Now.ToString();
                        data.updtEmpId = this.currentUser.empId;
                    }
                }
                else
                {
                    if (data.instantState != "01")
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert9", "alert(\"当前排拖单已经提交,无法修改!\");", true);
                        return;
                    }

                    data.updtDt = DateTime.Now.ToString();
                    data.updtEmpId = this.currentUser.empId;

                    if (isSubmit)
                    {
                        data.instantState = "02";
                        data.commitDt = DateTime.Now.ToString();
                        data.commitEmpId = this.currentUser.empId;
                    }
                    else
                    {
                        data.instantState = "01";
                        data.updtDt = DateTime.Now.ToString();
                        data.updtEmpId = this.currentUser.empId;
                    }
                }

                if (arrangeBillBC.SaveBArrangeBill(data, this.DtResult, isSubmit))
                {
                    this.DtResult = null;
                    this.InitData();
                    this.BindDetailTable();//绑定排托明细列表
                    this.BindGrid();//绑定到货明细列表

                    if (isSubmit)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert12", "alert(\"排拖单提交成功!\");" + (isSubmit ? "location.replace('../BArriveBill/BArriveBillList.aspx?&itemno=" + this.itemNo + "&pTypeNo=main');" : ""), true);
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "alert12", "alert(\"排拖单维护成功!\");", true);
                    }
                }
                else
                {
                    this.BindDetailTable();//绑定排托明细列表
                    this.BindGrid();//绑定到货明细列表
                    this.ClientScript.RegisterStartupScript(this.GetType(), "alert12", "alert(\"排拖单维护失败!\");", true);
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "ShowErr", "ShowErr(\"" + Server.UrlEncode(ex.Message) + "\",3);", true);
                return;
            }
            finally
            {
                arrangeBillBB.Dispose();
                arrangeBillBC.Dispose();
            }
        }
    }